Repository navigation
Expand file tree
/
Copy pathuser_matchmaking.module
More file actions
257 lines (215 loc) · 8.58 KB
/
Copy pathuser_matchmaking.module
File metadata and controls
257 lines (215 loc) · 8.58 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
254
255
256
257
<?php
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Url;
function _user_matchmaking_get_settings(): array
{
$config = \Drupal::config('user_matchmaking.settings');
return [
'field_offers' => (string) $config->get('fields.offers'),
'field_needs' => (string) $config->get('fields.needs'),
'field_notification' => (string) $config->get('fields.notification'),
'enable_emails' => $config->get('mail.enable_emails'),
];
}
/**
* Implements hook_entity_insert()
*/
function user_matchmaking_entity_insert(EntityInterface $entity)
{
if ($entity->getEntityTypeId() !== 'user' || !empty($entity->_user_matchmaking_skip)) {
return;
}
$settings = _user_matchmaking_get_settings();
if (empty($settings['field_offers']) || empty($settings['field_needs'])) {
return;
}
if ((int) $entity->get('status')->value !== 1) {
return;
}
$field_offers = $settings['field_offers'];
if (!$entity->hasField($field_offers) || $entity->get($field_offers)->isEmpty()) {
return;
}
_user_matchmaking_core($entity, $settings);
}
/**
* Implements hook_entity_update()
*/
function user_matchmaking_entity_update(EntityInterface $entity)
{
if ($entity->getEntityTypeId() !== 'user' || !empty($entity->_user_matchmaking_skip) || empty($entity->original)) {
return;
}
$settings = _user_matchmaking_get_settings();
if (empty($settings['field_offers']) || empty($settings['field_needs'])) {
return;
}
$field_offers = $settings['field_offers'];
$old_status = (int) $entity->original->get('status')->value;
$new_status = (int) $entity->get('status')->value;
if ($new_status !== 1 || !$entity->hasField($field_offers) || $entity->get($field_offers)->isEmpty()) {
return;
}
$offers_changed = ($entity->original->get($field_offers)->getValue() != $entity->get($field_offers)->getValue());
$status_activated = ($old_status === 0 && $new_status === 1);
if ($offers_changed || $status_activated) {
_user_matchmaking_core($entity, $settings);
}
}
function _user_matchmaking_core(EntityInterface $offerer, array $settings): void
{
$field_offers = $settings['field_offers'];
$field_needs = $settings['field_needs'];
$field_notification = $settings['field_notification'];
$emails_enabled = $settings['enable_emails'];
$offerer_uid = $offerer->id();
$offer_ids = array_column($offerer->get($field_offers)->getValue(), 'target_id');
// match needs in offers id
$seeker_uids = \Drupal::entityQuery('user')
->accessCheck(FALSE)
->condition('status', 1)
->condition($field_needs, $offer_ids, 'IN')
->condition('uid', $offerer_uid, '!=')
->execute();
if (empty($seeker_uids)) {
return;
}
$seekers = \Drupal::entityTypeManager()->getStorage('user')->loadMultiple($seeker_uids);
$match_store = \Drupal::entityTypeManager()->getStorage('user_match');
$timestamp = time();
$offered_entities = [];
$field_def = \Drupal::service('entity_field.manager')
->getFieldDefinitions('user', 'user')[$field_offers] ?? NULL;
if ($field_def && $field_def->getSetting('target_type') === 'taxonomy_term') {
$offered_entities = \Drupal::entityTypeManager()
->getStorage('taxonomy_term')
->loadMultiple($offer_ids);
}
$existing_ids = \Drupal::entityQuery('user_match')
->accessCheck(FALSE)
->condition('offerer_uid', [$offerer_uid, ...array_keys($seekers)], 'IN')
->condition('seeker_uid', [$offerer_uid, ...array_keys($seekers)], 'IN')
->execute();
$existing = [];
foreach ($match_store->loadMultiple($existing_ids) as $match) {
$offerer_id = $match->get('offerer_uid')->target_id;
$seeker_id = $match->get('seeker_uid')->target_id;
$match_key = $offerer_id . ':' . $seeker_id;
$existing[$match_key] = $match;
}
$notified_seekers = [];
foreach ($seekers as $seeker) {
$seeker_uid = $seeker->id();
$seeker_ids = array_column($seeker->get($field_needs)->getValue(), 'target_id');
$matched_ids = array_intersect($offer_ids, $seeker_ids);
$matched_entities = array_intersect_key($offered_entities, array_flip($matched_ids));
_user_matchmaking_save_match($match_store, $existing, "$offerer_uid:$seeker_uid", $offerer_uid, $seeker_uid, $timestamp);
_user_matchmaking_save_match($match_store, $existing, "$seeker_uid:$offerer_uid", $seeker_uid, $offerer_uid, $timestamp);
if (!empty($field_notification) && $seeker->hasField($field_notification)) {
// trigger once
$seeker->_user_matchmaking_skip = TRUE;
$seeker->set($field_notification, 1)->save();
}
$notified_seekers[] = $seeker;
if ($emails_enabled) {
_user_matchmaking_mail_seeker($seeker, $offerer, $matched_entities);
}
}
if (!empty($notified_seekers)) {
if (!empty($field_notification) && $offerer->hasField($field_notification)) {
$offerer->_user_matchmaking_skip = TRUE;
$offerer->set($field_notification, 1)->save();
}
if ($emails_enabled) {
_user_matchmaking_mail_offerer($offerer, count($notified_seekers), $notified_seekers);
}
}
}
// create or update a UserMatch entity
function _user_matchmaking_save_match($store, array $existing, string $key, $offerer_uid, $seeker_uid, int $timestamp): void
{
if (isset($existing[$key])) {
$existing[$key]->save();
} else {
$store->create([
'offerer_uid' => $offerer_uid,
'seeker_uid' => $seeker_uid,
'created' => $timestamp,
])->save();
}
}
/**
* Implements hook_mail()
*/
function user_matchmaking_mail($key, &$message, $params): void
{
$config = $params['config'];
switch ($key) {
case 'seeker_match_notification':
$user_url = \Drupal::service('url_generator')->generateFromRoute(
'entity.user.canonical',
['user' => $params['offerer']->id()],
['absolute' => TRUE]
);
$replace = [
'[offerer:display-name]' => $params['offerer']->getDisplayName(),
'[seeker:display-name]' => $params['seeker']->getDisplayName(),
'[matched_terms]' => $params['matched_terms'],
'[offerer:url]' => $user_url,
];
$message['subject'] = strtr($config->get('mail.seeker_match_notification.subject'), $replace);
$message['body'][] = strtr($config->get('mail.seeker_match_notification.body'), $replace);
break;
case 'offerer_match_summary':
$lines = [];
foreach ($params['matched_seekers'] as $seeker) {
$lines[] = '- ' . $seeker->getDisplayName();
}
$replace = [
'[offerer:display-name]' => $params['offerer']->getDisplayName(),
'[match_count]' => (string) $params['match_count'],
'[matched_users_list]' => implode("\n", $lines),
'[matches_url]' => (string) $config->get('mail.matches_url'),
];
$message['subject'] = strtr($config->get('mail.offerer_match_summary.subject'), $replace);
$message['body'][] = strtr($config->get('mail.offerer_match_summary.body'), $replace);
break;
}
}
// theme hook for notification icon
function user_matchmaking_theme($existing, $type, $theme, $path): array
{
return [
'user_matchmaking_notification_bell' => [
'variables' => [
'active' => FALSE,
],
'template' => 'user-matchmaking-notification-bell',
],
];
}
/**
* Implements hook_requirements()
* /admin/reports/status
*/
function user_matchmaking_requirements($phase): array
{
if ($phase !== 'runtime') {
return [];
}
$config = \Drupal::config('user_matchmaking.settings');
$configured = $config->get('fields.offers') && $config->get('fields.needs');
$settings_url = Url::fromRoute('user_matchmaking.settings')->toString();
return [
'user_matchmaking' => [
'title' => t('User Matchmaking'),
'value' => $configured ? t('Configured') : t('Not configured'),
'description' => $configured ? '' : t(
'Matching fields are not set. <a href=":url">Configure now</a>.',
[':url' => $settings_url]
),
'severity' => $configured ? REQUIREMENT_OK : REQUIREMENT_WARNING,
],
];
}
// @todo logs