-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathApplications.php
More file actions
253 lines (219 loc) · 6.47 KB
/
Applications.php
File metadata and controls
253 lines (219 loc) · 6.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php
namespace Drupal\asu_application;
use Drupal\user\Entity\User;
/**
* Handles applications.
*/
class Applications {
const HIGH_ENUM = 'HIGH';
const MEDIUM = 10;
const MEDIUM_ENUM = 'MEDIUM';
const LOW = 5;
const LOW_ENUM = 'LOW';
/**
* Array of application objects.
*
* @var array|\Drupal\Core\Entity\EntityInterface[]
*/
private array $applications;
/**
* Applications constructor.
*
* @param string $userId
* User id.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public function __construct(?string $userId = NULL) {
$applicationStorage = \Drupal::entityTypeManager()
->getStorage('asu_application');
try {
if (!$userId) {
$this->applications = $applicationStorage->loadMultiple();
}
else {
$applicationIds = $this->resolveApplicationIdsByUser($userId);
$this->applications = empty($applicationIds)
? []
: $applicationStorage->loadMultiple($applicationIds);
}
}
catch (\Exception $e) {
$this->applications = [];
}
}
/**
* Resolve application ids where user is owner or mapped co-applicant.
*/
private function resolveApplicationIdsByUser(string $userId): array {
$ids = \Drupal::database()
->select('asu_application', 'a')
->fields('a', ['id'])
->condition('a.uid', (int) $userId)
->condition('a.status', 1)
->execute()
->fetchCol();
$schema = \Drupal::database()->schema();
if (!$schema->tableExists('asu_application_co_applicant_map')) {
return array_map('intval', $ids);
}
$user = User::load((int) $userId);
$samlHash = $user && $user->hasField('field_saml_hash')
? $user->get('field_saml_hash')->value
: NULL;
if (empty($samlHash)) {
return array_values(array_unique(array_map('intval', $ids)));
}
$coApplicantQuery = \Drupal::database()
->select('asu_application_co_applicant_map', 'm');
$coApplicantQuery->fields('m', ['application_id']);
$coApplicantQuery->innerJoin('asu_application', 'a', 'a.id = m.application_id');
$coApplicantIds = $coApplicantQuery
->condition('a.status', 1)
->condition('m.co_applicant_saml_hash', $samlHash)
->execute()
->fetchCol();
$allIds = array_merge($ids, $coApplicantIds);
return array_values(array_unique(array_map('intval', $allIds)));
}
/**
* Get all applications for all applications.
*
* @return static
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public static function create(): self {
return new self();
}
/**
* Get applications by user.
*
* @param string $userId
* User whose applications are resolved.
*
* @return static
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public static function applicationsByUser(string $userId): self {
return new self($userId);
}
/**
* Get applications for all projects.
*
* @return array
* Array of apartment ids by project.
*/
public function getApartmentApplicationsByProject(): array {
if (empty($this->applications)) {
return [];
}
$applicationsByProject = [];
/** @var \Drupal\asu_application\Entity\Application $application */
foreach ($this->applications as $application) {
$apartmentIds = $application->getApartmentIds();
$applicationsByProject[$application->getProjectId()] = $apartmentIds;
}
return $applicationsByProject;
}
/**
* Returns pairs of project and application ids.
*
* @return array
* Array of ['project_id' => ..., 'application_id' => ...]
*/
public function getApplicationsProjectPairs(): array {
$result = [];
foreach ($this->applications as $application) {
$projectId = $application->getProjectId();
$applicationId = $application->id();
if ($projectId && $applicationId) {
$result[] = [
'project_id' => (int) $projectId,
'application_id' => (int) $applicationId,
];
}
}
return $result;
}
/**
* Get applications for single project.
*
* @param int|string $id
* Id of the project.
*
* @return array
* Array of apartment ids by project.
*/
public function getApartmentApplicationStatusesForProject($id): array {
if (empty($this->applications)) {
return [];
}
$applicationsForProject = [];
/** @var \Drupal\asu_application\Entity\Application $application */
foreach ($this->applications as $application) {
if ($application->getProjectId() == $id) {
$applicationsForProject[] = $application;
}
}
$this->applications = $applicationsForProject;
return $this->getApartmentApplicationStatuses();
}
/**
* Get apartment applications.
*/
public function getApartmentApplications() {
if (empty($this->applications)) {
return [];
}
$applications = [];
/** @var \Drupal\asu_application\Entity\Application $application */
foreach ($this->applications as $application) {
$apartmentIds = $application->getApartmentIds();
$applications = array_merge($applications, $apartmentIds);
}
return $applications;
}
/**
* Return application count as status text instead of numeric.
*
* @return array
* Apartment application status.
*/
public function getApartmentApplicationStatuses(): array {
$applications = $this->getApartmentApplications();
$counts = array_count_values($applications);
$applicationStatuses = [];
foreach ($counts as $key => $count) {
$applicationStatuses[$key] = $this::resolveApplicationCountEnum($count);
}
return $applicationStatuses;
}
/**
* Resolve the enum for application count.
*
* @param int $count
* Number of applications for an apartment.
*
* @return string
* Application count as enum.
*/
public static function resolveApplicationCountEnum(int $count): string {
if ($count === 0) {
return self::LOW_ENUM;
}
if ($count <= self::LOW) {
return self::LOW_ENUM;
}
if (in_array($count, range(self::LOW, self::MEDIUM))) {
return self::MEDIUM_ENUM;
}
if ($count > self::MEDIUM) {
return self::HIGH_ENUM;
}
}
}