Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
8eedd10a | 19 | * The basic class that interfaces with the external user framework. |
6a488035 TO |
20 | */ |
21 | class CRM_Core_BAO_UFMatch extends CRM_Core_DAO_UFMatch { | |
22 | ||
b5c2afd0 | 23 | /** |
b44e3f84 | 24 | * Create UF Match, Note that this function is here in it's simplest form @ the moment |
b5c2afd0 | 25 | * |
ced9bfed EM |
26 | * @param $params |
27 | * | |
28 | * @return \CRM_Core_DAO_UFMatch | |
b5c2afd0 | 29 | */ |
00be9182 | 30 | public static function create($params) { |
6a488035 TO |
31 | $hook = empty($params['id']) ? 'create' : 'edit'; |
32 | CRM_Utils_Hook::pre($hook, 'UFMatch', CRM_Utils_Array::value('id', $params), $params); | |
22e263ad | 33 | if (empty($params['domain_id'])) { |
e0e29b3c | 34 | $params['domain_id'] = CRM_Core_Config::domainID(); |
35 | } | |
6a488035 TO |
36 | $dao = new CRM_Core_DAO_UFMatch(); |
37 | $dao->copyValues($params); | |
ce4abc8e | 38 | // Fixme: this function cannot update records |
69301426 | 39 | if (!$dao->find(TRUE)) { |
40 | $dao->save(); | |
ce4abc8e CW |
41 | Civi::$statics[__CLASS__][$params['domain_id']][(int) $dao->contact_id] = (int) $dao->uf_id; |
42 | CRM_Utils_Hook::post($hook, 'UFMatch', $dao->id, $dao); | |
69301426 | 43 | } |
6a488035 TO |
44 | return $dao; |
45 | } | |
46 | ||
6a488035 TO |
47 | /** |
48 | * Given a UF user object, make sure there is a contact | |
49 | * object for this user. If the user has new values, we need | |
50 | * to update the CRM DB with the new values | |
51 | * | |
6a0b768e TO |
52 | * @param Object $user |
53 | * The drupal user object. | |
54 | * @param bool $update | |
55 | * Has the user object been edited. | |
77b97be7 EM |
56 | * @param $uf |
57 | * | |
58 | * @param $ctype | |
59 | * @param bool $isLogin | |
ac15829d SL |
60 | * |
61 | * @throws CRM_Core_Exception | |
6a488035 | 62 | */ |
00be9182 | 63 | public static function synchronize(&$user, $update, $uf, $ctype, $isLogin = FALSE) { |
2b617cb0 | 64 | $userSystem = CRM_Core_Config::singleton()->userSystem; |
6a488035 TO |
65 | $session = CRM_Core_Session::singleton(); |
66 | if (!is_object($session)) { | |
ac15829d | 67 | throw new CRM_Core_Exception('wow, session is not an object?'); |
6a488035 TO |
68 | return; |
69 | } | |
70 | ||
2b617cb0 EM |
71 | $userSystemID = $userSystem->getBestUFID($user); |
72 | $uniqId = $userSystem->getBestUFUniqueIdentifier($user); | |
6a488035 TO |
73 | |
74 | // if the id of the object is zero (true for anon users in drupal) | |
75 | // have we already processed this user, if so early | |
76 | // return. | |
77 | $userID = $session->get('userID'); | |
78 | $ufID = $session->get('ufID'); | |
79 | ||
32998c82 | 80 | if (!$update && $ufID == $userSystemID) { |
6a488035 TO |
81 | return; |
82 | } | |
83 | ||
84 | //check do we have logged in user. | |
85 | $isUserLoggedIn = CRM_Utils_System::isUserLoggedIn(); | |
86 | ||
87 | // reset the session if we are a different user | |
32998c82 | 88 | if ($ufID && $ufID != $userSystemID) { |
6a488035 TO |
89 | $session->reset(); |
90 | ||
91 | //get logged in user ids, and set to session. | |
92 | if ($isUserLoggedIn) { | |
93 | $userIds = self::getUFValues(); | |
94 | $session->set('ufID', CRM_Utils_Array::value('uf_id', $userIds, '')); | |
95 | $session->set('userID', CRM_Utils_Array::value('contact_id', $userIds, '')); | |
6a488035 TO |
96 | } |
97 | } | |
98 | ||
99 | // return early | |
32998c82 | 100 | if ($userSystemID == 0) { |
6a488035 TO |
101 | return; |
102 | } | |
103 | ||
32998c82 | 104 | $ufmatch = self::synchronizeUFMatch($user, $userSystemID, $uniqId, $uf, NULL, $ctype, $isLogin); |
6a488035 TO |
105 | if (!$ufmatch) { |
106 | return; | |
107 | } | |
108 | ||
109 | //make sure we have session w/ consistent ids. | |
353ffa53 TO |
110 | $ufID = $ufmatch->uf_id; |
111 | $userID = $ufmatch->contact_id; | |
6a488035 TO |
112 | if ($isUserLoggedIn) { |
113 | $loggedInUserUfID = CRM_Utils_System::getLoggedInUfID(); | |
114 | //are we processing logged in user. | |
115 | if ($loggedInUserUfID && $loggedInUserUfID != $ufID) { | |
353ffa53 TO |
116 | $userIds = self::getUFValues($loggedInUserUfID); |
117 | $ufID = CRM_Utils_Array::value('uf_id', $userIds, ''); | |
118 | $userID = CRM_Utils_Array::value('contact_id', $userIds, ''); | |
6a488035 TO |
119 | } |
120 | } | |
121 | ||
122 | //set user ids to session. | |
123 | $session->set('ufID', $ufID); | |
124 | $session->set('userID', $userID); | |
6a488035 TO |
125 | |
126 | // add current contact to recently viewed | |
127 | if ($ufmatch->contact_id) { | |
79d7553f | 128 | list($displayName, $contactImage, $contactType, $contactSubtype, $contactImageUrl) |
129 | = CRM_Contact_BAO_Contact::getDisplayAndImage($ufmatch->contact_id, TRUE, TRUE); | |
6a488035 | 130 | |
be2fb01f | 131 | $otherRecent = [ |
6a488035 TO |
132 | 'imageUrl' => $contactImageUrl, |
133 | 'subtype' => $contactSubtype, | |
134 | 'editUrl' => CRM_Utils_System::url('civicrm/contact/add', "reset=1&action=update&cid={$ufmatch->contact_id}"), | |
be2fb01f | 135 | ]; |
6a488035 TO |
136 | |
137 | CRM_Utils_Recent::add($displayName, | |
138 | CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$ufmatch->contact_id}"), | |
139 | $ufmatch->contact_id, | |
140 | $contactType, | |
141 | $ufmatch->contact_id, | |
142 | $displayName, | |
143 | $otherRecent | |
144 | ); | |
145 | } | |
146 | } | |
147 | ||
148 | /** | |
149 | * Synchronize the object with the UF Match entry. Can be called stand-alone from | |
150 | * the drupalUsers script | |
151 | * | |
6a0b768e TO |
152 | * @param Object $user |
153 | * The drupal user object. | |
154 | * @param string $userKey | |
155 | * The id of the user from the uf object. | |
156 | * @param string $uniqId | |
157 | * The OpenID of the user. | |
158 | * @param string $uf | |
159 | * The name of the user framework. | |
160 | * @param int $status | |
161 | * Returns the status if user created or already exits (used for CMS sync). | |
72b3a70c CW |
162 | * @param string $ctype |
163 | * contact type | |
fd31fa4c | 164 | * @param bool $isLogin |
6a488035 | 165 | * |
72b3a70c | 166 | * @return CRM_Core_DAO_UFMatch|bool |
6a488035 | 167 | */ |
00be9182 | 168 | public static function &synchronizeUFMatch(&$user, $userKey, $uniqId, $uf, $status = NULL, $ctype = NULL, $isLogin = FALSE) { |
6a488035 | 169 | $config = CRM_Core_Config::singleton(); |
6a488035 TO |
170 | $newContact = FALSE; |
171 | ||
172 | // make sure that a contact id exists for this user id | |
353ffa53 | 173 | $ufmatch = new CRM_Core_DAO_UFMatch(); |
6a488035 | 174 | $ufmatch->domain_id = CRM_Core_Config::domainID(); |
353ffa53 | 175 | $ufmatch->uf_id = $userKey; |
6a488035 TO |
176 | |
177 | if (!$ufmatch->find(TRUE)) { | |
178 | $transaction = new CRM_Core_Transaction(); | |
179 | ||
180 | $dao = NULL; | |
181 | if (!empty($_POST) && !$isLogin) { | |
182 | $params = $_POST; | |
183 | $params['email'] = $uniqId; | |
49570419 SL |
184 | // dev/core#1858 Ensure that if we have a contactID parameter which is set in the Create user Record contact task form |
185 | // That this contacID value is passed through as the contact_id to the get duplicate contacts function. This is necessary because for Drupal 8 this function gets invoked | |
186 | // Before the civicrm_uf_match record is added where as in D7 it isn't called until the user tries to actually login. | |
187 | if (!empty($params['contactID'])) { | |
188 | $params['contact_id'] = $params['contactID']; | |
189 | } | |
6a488035 | 190 | |
be2fb01f | 191 | $ids = CRM_Contact_BAO_Contact::getDuplicateContacts($params, 'Individual', 'Unsupervised', [], FALSE); |
6a488035 | 192 | |
aaffa79f | 193 | if (!empty($ids) && Civi::settings()->get('uniq_email_per_site')) { |
6a488035 TO |
194 | // restrict dupeIds to ones that belong to current domain/site. |
195 | $siteContacts = CRM_Core_BAO_Domain::getContactList(); | |
196 | foreach ($ids as $index => $dupeId) { | |
197 | if (!in_array($dupeId, $siteContacts)) { | |
198 | unset($ids[$index]); | |
199 | } | |
200 | } | |
201 | // re-index the array | |
202 | $ids = array_values($ids); | |
203 | } | |
204 | if (!empty($ids)) { | |
205 | $dao = new CRM_Core_DAO(); | |
206 | $dao->contact_id = $ids[0]; | |
207 | } | |
208 | } | |
209 | else { | |
210 | $dao = CRM_Contact_BAO_Contact::matchContactOnEmail($uniqId, $ctype); | |
211 | } | |
212 | ||
213 | $found = FALSE; | |
214 | if ($dao) { | |
215 | // ensure there does not exists a contact_id / uf_id pair | |
216 | // in the DB. This might be due to multiple emails per contact | |
217 | // CRM-9091 | |
218 | $sql = " | |
219 | SELECT id | |
220 | FROM civicrm_uf_match | |
221 | WHERE contact_id = %1 | |
222 | AND domain_id = %2 | |
223 | "; | |
be2fb01f CW |
224 | $params = [ |
225 | 1 => [$dao->contact_id, 'Integer'], | |
226 | 2 => [CRM_Core_Config::domainID(), 'Integer'], | |
227 | ]; | |
6a488035 TO |
228 | $conflict = CRM_Core_DAO::singleValueQuery($sql, $params); |
229 | ||
230 | if (!$conflict) { | |
353ffa53 | 231 | $found = TRUE; |
6a488035 | 232 | $ufmatch->contact_id = $dao->contact_id; |
353ffa53 | 233 | $ufmatch->uf_name = $uniqId; |
6a488035 TO |
234 | } |
235 | } | |
236 | ||
237 | if (!$found) { | |
3eb59ab5 AS |
238 | // Not sure why we're testing for this. Is there ever a case |
239 | // in which $user is not an object? | |
b7d765aa | 240 | if (is_object($user)) { |
3eb59ab5 AS |
241 | if ($config->userSystem->is_drupal) { |
242 | $primary_email = $uniqId; | |
243 | } | |
244 | elseif ($uf == 'WordPress') { | |
245 | $primary_email = $user->user_email; | |
246 | } | |
247 | else { | |
248 | $primary_email = $user->email; | |
249 | } | |
be2fb01f | 250 | $params = ['email-Primary' => $primary_email]; |
6a488035 TO |
251 | } |
252 | ||
253 | if ($ctype == 'Organization') { | |
254 | $params['organization_name'] = $uniqId; | |
255 | } | |
256 | elseif ($ctype == 'Household') { | |
257 | $params['household_name'] = $uniqId; | |
258 | } | |
259 | ||
260 | if (!$ctype) { | |
261 | $ctype = "Individual"; | |
262 | } | |
263 | $params['contact_type'] = $ctype; | |
264 | ||
265 | // extract first / middle / last name | |
266 | // for joomla | |
267 | if ($uf == 'Joomla' && $user->name) { | |
268 | CRM_Utils_String::extractName($user->name, $params); | |
269 | } | |
270 | ||
271 | if ($uf == 'WordPress') { | |
272 | if ($user->first_name) { | |
273 | $params['first_name'] = $user->first_name; | |
274 | } | |
275 | ||
276 | if ($user->last_name) { | |
277 | $params['last_name'] = $user->last_name; | |
278 | } | |
279 | } | |
280 | ||
7b4b0b68 | 281 | $contactId = CRM_Contact_BAO_Contact::createProfileContact($params); |
6a488035 | 282 | $ufmatch->contact_id = $contactId; |
353ffa53 | 283 | $ufmatch->uf_name = $uniqId; |
6a488035 TO |
284 | } |
285 | ||
286 | // check that there are not two CMS IDs matching the same CiviCRM contact - this happens when a civicrm | |
287 | // user has two e-mails and there is a cms match for each of them | |
288 | // the gets rid of the nasty fata error but still reports the error | |
289 | $sql = " | |
290 | SELECT uf_id | |
291 | FROM civicrm_uf_match | |
292 | WHERE ( contact_id = %1 | |
293 | OR uf_name = %2 | |
294 | OR uf_id = %3 ) | |
295 | AND domain_id = %4 | |
296 | "; | |
be2fb01f CW |
297 | $params = [ |
298 | 1 => [$ufmatch->contact_id, 'Integer'], | |
299 | 2 => [$ufmatch->uf_name, 'String'], | |
300 | 3 => [$ufmatch->uf_id, 'Integer'], | |
301 | 4 => [$ufmatch->domain_id, 'Integer'], | |
302 | ]; | |
6a488035 TO |
303 | |
304 | $conflict = CRM_Core_DAO::singleValueQuery($sql, $params); | |
305 | ||
306 | if (!$conflict) { | |
307 | $ufmatch = CRM_Core_BAO_UFMatch::create((array) $ufmatch); | |
6a488035 TO |
308 | $newContact = TRUE; |
309 | $transaction->commit(); | |
310 | } | |
311 | else { | |
312 | $msg = ts("Contact ID %1 is a match for %2 user %3 but has already been matched to %4", | |
be2fb01f | 313 | [ |
6a488035 TO |
314 | 1 => $ufmatch->contact_id, |
315 | 2 => $uf, | |
316 | 3 => $ufmatch->uf_id, | |
21dfd5f5 | 317 | 4 => $conflict, |
be2fb01f | 318 | ] |
6a488035 TO |
319 | ); |
320 | unset($conflict); | |
321 | } | |
322 | } | |
323 | ||
324 | if ($status) { | |
325 | return $newContact; | |
326 | } | |
327 | else { | |
328 | return $ufmatch; | |
329 | } | |
330 | } | |
331 | ||
332 | /** | |
fe482240 | 333 | * Update the uf_name in the user object. |
6a488035 | 334 | * |
6a0b768e TO |
335 | * @param int $contactId |
336 | * Id of the contact to update. | |
6a488035 | 337 | */ |
00be9182 | 338 | public static function updateUFName($contactId) { |
5e7f101a | 339 | if (!Civi::settings()->get('syncCMSEmail') || !$contactId) { |
6a488035 TO |
340 | return; |
341 | } | |
5e7f101a | 342 | |
6a488035 TO |
343 | $config = CRM_Core_Config::singleton(); |
344 | $ufName = CRM_Contact_BAO_Contact::getPrimaryEmail($contactId); | |
345 | ||
346 | if (!$ufName) { | |
347 | return; | |
348 | } | |
349 | ||
350 | $update = FALSE; | |
351 | ||
352 | // 1.do check for contact Id. | |
353ffa53 | 353 | $ufmatch = new CRM_Core_DAO_UFMatch(); |
6a488035 | 354 | $ufmatch->contact_id = $contactId; |
353ffa53 | 355 | $ufmatch->domain_id = CRM_Core_Config::domainID(); |
6a488035 TO |
356 | if (!$ufmatch->find(TRUE)) { |
357 | return; | |
358 | } | |
359 | if ($ufmatch->uf_name != $ufName) { | |
360 | $update = TRUE; | |
361 | } | |
362 | ||
363 | // CRM-6928 | |
364 | // 2.do check for duplicate ufName. | |
365 | $ufDupeName = new CRM_Core_DAO_UFMatch(); | |
366 | $ufDupeName->uf_name = $ufName; | |
367 | $ufDupeName->domain_id = CRM_Core_Config::domainID(); | |
368 | if ($ufDupeName->find(TRUE) && | |
369 | $ufDupeName->contact_id != $contactId | |
370 | ) { | |
371 | $update = FALSE; | |
372 | } | |
373 | ||
374 | if (!$update) { | |
375 | return; | |
376 | } | |
377 | ||
378 | // save the updated ufmatch object | |
379 | $ufmatch->uf_name = $ufName; | |
380 | $ufmatch->save(); | |
381 | $config->userSystem->updateCMSName($ufmatch->uf_id, $ufName); | |
382 | } | |
383 | ||
384 | /** | |
fe482240 | 385 | * Update the email value for the contact and user profile. |
6a488035 | 386 | * |
5a4f6742 CW |
387 | * @param int $contactId |
388 | * Contact ID of the user. | |
8eedd10a | 389 | * @param string $emailAddress |
6a0b768e | 390 | * Email to be modified for the user. |
6a488035 | 391 | */ |
00be9182 | 392 | public static function updateContactEmail($contactId, $emailAddress) { |
6a488035 TO |
393 | $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower'; |
394 | $emailAddress = $strtolower($emailAddress); | |
395 | ||
353ffa53 | 396 | $ufmatch = new CRM_Core_DAO_UFMatch(); |
6a488035 | 397 | $ufmatch->contact_id = $contactId; |
353ffa53 | 398 | $ufmatch->domain_id = CRM_Core_Config::domainID(); |
6a488035 TO |
399 | if ($ufmatch->find(TRUE)) { |
400 | // Save the email in UF Match table | |
401 | $ufmatch->uf_name = $emailAddress; | |
402 | CRM_Core_BAO_UFMatch::create((array) $ufmatch); | |
403 | ||
5e7f101a | 404 | // If CMS integration is disabled skip Civi email update if CMS user email is changed |
405 | if (Civi::settings()->get('syncCMSEmail') == FALSE) { | |
406 | return; | |
407 | } | |
408 | ||
6a488035 TO |
409 | //check if the primary email for the contact exists |
410 | //$contactDetails[1] - email | |
411 | //$contactDetails[3] - email id | |
412 | $contactDetails = CRM_Contact_BAO_Contact_Location::getEmailDetails($contactId); | |
413 | ||
414 | if (trim($contactDetails[1])) { | |
56e50a8b | 415 | //update if record is found but different |
6a488035 | 416 | $emailID = $contactDetails[3]; |
56e50a8b AS |
417 | if (trim($contactDetails[1]) != $emailAddress) { |
418 | civicrm_api3('Email', 'create', [ | |
419 | 'id' => $emailID, | |
420 | 'email' => $emailAddress, | |
421 | ]); | |
422 | } | |
6a488035 TO |
423 | } |
424 | else { | |
425 | //else insert a new email record | |
56e50a8b AS |
426 | $result = civicrm_api3('Email', 'create', [ |
427 | 'contact_id' => $contactId, | |
428 | 'email' => $emailAddress, | |
429 | 'is_primary' => 1, | |
430 | ]); | |
a650ed47 | 431 | $emailID = $result['id']; |
6a488035 TO |
432 | } |
433 | ||
434 | CRM_Core_BAO_Log::register($contactId, | |
435 | 'civicrm_email', | |
436 | $emailID | |
437 | ); | |
438 | } | |
439 | } | |
440 | ||
441 | /** | |
fe482240 | 442 | * Delete the object records that are associated with this cms user. |
6a488035 | 443 | * |
6a0b768e TO |
444 | * @param int $ufID |
445 | * Id of the user to delete. | |
6a488035 | 446 | */ |
00be9182 | 447 | public static function deleteUser($ufID) { |
6a488035 TO |
448 | $ufmatch = new CRM_Core_DAO_UFMatch(); |
449 | ||
450 | $ufmatch->uf_id = $ufID; | |
ce4abc8e | 451 | $ufmatch->domain_id = $domainId = CRM_Core_Config::domainID(); |
6a488035 | 452 | $ufmatch->delete(); |
ce4abc8e CW |
453 | |
454 | // Flush cache | |
455 | Civi::$statics[__CLASS__][$domainId] = []; | |
6a488035 TO |
456 | } |
457 | ||
458 | /** | |
fe482240 | 459 | * Get the contact_id given a uf_id. |
6a488035 | 460 | * |
6a0b768e TO |
461 | * @param int $ufID |
462 | * Id of UF for which related contact_id is required. | |
6a488035 | 463 | * |
e97c66ff | 464 | * @return int|null |
a6c01b45 | 465 | * contact_id on success, null otherwise |
6a488035 | 466 | */ |
00be9182 | 467 | public static function getContactId($ufID) { |
ce4abc8e | 468 | if (!$ufID) { |
6a488035 TO |
469 | return NULL; |
470 | } | |
ce4abc8e | 471 | $domainId = CRM_Core_Config::domainID(); |
6a488035 | 472 | |
ce4abc8e CW |
473 | if (!isset(Civi::$statics[__CLASS__][$domainId])) { |
474 | Civi::$statics[__CLASS__][$domainId] = []; | |
475 | } | |
476 | $contactId = array_search($ufID, Civi::$statics[__CLASS__][$domainId]); | |
477 | if ($contactId) { | |
478 | return $contactId; | |
479 | } | |
6a488035 | 480 | $ufmatch = new CRM_Core_DAO_UFMatch(); |
6a488035 | 481 | $ufmatch->uf_id = $ufID; |
ce4abc8e | 482 | $ufmatch->domain_id = $domainId; |
6a488035 | 483 | if ($ufmatch->find(TRUE)) { |
ce4abc8e CW |
484 | $contactId = (int) $ufmatch->contact_id; |
485 | Civi::$statics[__CLASS__][$domainId][$contactId] = (int) $ufID; | |
486 | return $contactId; | |
6a488035 TO |
487 | } |
488 | return NULL; | |
489 | } | |
490 | ||
491 | /** | |
fe482240 | 492 | * Get the uf_id given a contact_id. |
6a488035 | 493 | * |
6a0b768e TO |
494 | * @param int $contactID |
495 | * ID of the contact for which related uf_id is required. | |
6a488035 | 496 | * |
e97c66ff | 497 | * @return int|null |
a6c01b45 | 498 | * uf_id of the given contact_id on success, null otherwise |
6a488035 | 499 | */ |
00be9182 | 500 | public static function getUFId($contactID) { |
ce4abc8e | 501 | if (!$contactID) { |
6a488035 TO |
502 | return NULL; |
503 | } | |
ce4abc8e CW |
504 | $domainId = CRM_Core_Config::domainID(); |
505 | $contactID = (int) $contactID; | |
506 | ||
507 | if (empty(Civi::$statics[__CLASS__][$domainId]) || !array_key_exists($contactID, Civi::$statics[__CLASS__][$domainId])) { | |
508 | Civi::$statics[__CLASS__][$domainId][$contactID] = NULL; | |
509 | $ufmatch = new CRM_Core_DAO_UFMatch(); | |
510 | $ufmatch->contact_id = $contactID; | |
511 | $ufmatch->domain_id = $domainId; | |
512 | if ($ufmatch->find(TRUE)) { | |
513 | Civi::$statics[__CLASS__][$domainId][$contactID] = (int) $ufmatch->uf_id; | |
514 | } | |
6a488035 | 515 | } |
ce4abc8e | 516 | return Civi::$statics[__CLASS__][$domainId][$contactID]; |
6a488035 TO |
517 | } |
518 | ||
b5c2afd0 | 519 | /** |
f0612cdb | 520 | * @deprecated |
b5c2afd0 EM |
521 | * @return bool |
522 | */ | |
00be9182 | 523 | public static function isEmptyTable() { |
f0612cdb | 524 | CRM_Core_Error::deprecatedFunctionWarning('unused function to be removed'); |
6a488035 TO |
525 | $sql = "SELECT count(id) FROM civicrm_uf_match"; |
526 | return CRM_Core_DAO::singleValueQuery($sql) > 0 ? FALSE : TRUE; | |
527 | } | |
528 | ||
529 | /** | |
fe482240 | 530 | * Get the list of contact_id. |
6a488035 | 531 | * |
f0612cdb | 532 | * @deprecated |
a6c01b45 CW |
533 | * @return int |
534 | * contact_id on success, null otherwise | |
6a488035 | 535 | */ |
00be9182 | 536 | public static function getContactIDs() { |
f0612cdb | 537 | CRM_Core_Error::deprecatedFunctionWarning('unused function to be removed'); |
be2fb01f | 538 | $id = []; |
6a488035 TO |
539 | $dao = new CRM_Core_DAO_UFMatch(); |
540 | $dao->find(); | |
541 | while ($dao->fetch()) { | |
542 | $id[] = $dao->contact_id; | |
543 | } | |
544 | return $id; | |
545 | } | |
546 | ||
547 | /** | |
100fef9d | 548 | * See if this user exists, and if so, if they're allowed to login |
6a488035 | 549 | * |
f0612cdb | 550 | * @deprecated |
100fef9d | 551 | * @param int $openId |
2a6da8d7 | 552 | * |
a6c01b45 CW |
553 | * @return bool |
554 | * true if allowed to login, false otherwise | |
6a488035 | 555 | */ |
00be9182 | 556 | public static function getAllowedToLogin($openId) { |
f0612cdb | 557 | CRM_Core_Error::deprecatedFunctionWarning('unused function to be removed'); |
6a488035 TO |
558 | $ufmatch = new CRM_Core_DAO_UFMatch(); |
559 | $ufmatch->uf_name = $openId; | |
560 | $ufmatch->allowed_to_login = 1; | |
561 | if ($ufmatch->find(TRUE)) { | |
562 | return TRUE; | |
563 | } | |
564 | return FALSE; | |
565 | } | |
566 | ||
567 | /** | |
100fef9d | 568 | * Get the next unused uf_id value, since the standalone UF doesn't |
6a488035 TO |
569 | * have id's (it uses OpenIDs, which go in a different field) |
570 | * | |
f0612cdb | 571 | * @deprecated |
a6c01b45 CW |
572 | * @return int |
573 | * next highest unused value for uf_id | |
6a488035 | 574 | */ |
00be9182 | 575 | public static function getNextUfIdValue() { |
f0612cdb | 576 | CRM_Core_Error::deprecatedFunctionWarning('unused function to be removed'); |
6a488035 TO |
577 | $query = "SELECT MAX(uf_id)+1 AS next_uf_id FROM civicrm_uf_match"; |
578 | $dao = CRM_Core_DAO::executeQuery($query); | |
579 | if ($dao->fetch()) { | |
580 | $ufId = $dao->next_uf_id; | |
581 | } | |
582 | ||
583 | if (!isset($ufId)) { | |
584 | $ufId = 1; | |
585 | } | |
586 | return $ufId; | |
587 | } | |
588 | ||
b5c2afd0 EM |
589 | /** |
590 | * @param $email | |
f0612cdb | 591 | * @deprecated |
b5c2afd0 EM |
592 | * @return bool |
593 | */ | |
00be9182 | 594 | public static function isDuplicateUser($email) { |
f0612cdb | 595 | CRM_Core_Error::deprecatedFunctionWarning('unused function to be removed'); |
6a488035 TO |
596 | $session = CRM_Core_Session::singleton(); |
597 | $contactID = $session->get('userID'); | |
598 | if (!empty($email) && isset($contactID)) { | |
599 | $dao = new CRM_Core_DAO_UFMatch(); | |
600 | $dao->uf_name = $email; | |
601 | if ($dao->find(TRUE) && $contactID != $dao->contact_id) { | |
602 | return TRUE; | |
603 | } | |
604 | } | |
605 | return FALSE; | |
606 | } | |
607 | ||
608 | /** | |
609 | * Get uf match values for given uf id or logged in user. | |
610 | * | |
6a0b768e TO |
611 | * @param int $ufID |
612 | * Uf id. | |
6a488035 | 613 | * |
77b97be7 | 614 | * @return array |
76e7a76c | 615 | * uf values. |
77b97be7 | 616 | */ |
00be9182 | 617 | public static function getUFValues($ufID = NULL) { |
6a488035 TO |
618 | if (!$ufID) { |
619 | //get logged in user uf id. | |
620 | $ufID = CRM_Utils_System::getLoggedInUfID(); | |
621 | } | |
622 | if (!$ufID) { | |
be2fb01f | 623 | return []; |
6a488035 TO |
624 | } |
625 | ||
626 | static $ufValues; | |
627 | if ($ufID && !isset($ufValues[$ufID])) { | |
353ffa53 TO |
628 | $ufmatch = new CRM_Core_DAO_UFMatch(); |
629 | $ufmatch->uf_id = $ufID; | |
6a488035 TO |
630 | $ufmatch->domain_id = CRM_Core_Config::domainID(); |
631 | if ($ufmatch->find(TRUE)) { | |
be2fb01f | 632 | $ufValues[$ufID] = [ |
6a488035 TO |
633 | 'uf_id' => $ufmatch->uf_id, |
634 | 'uf_name' => $ufmatch->uf_name, | |
635 | 'contact_id' => $ufmatch->contact_id, | |
636 | 'domain_id' => $ufmatch->domain_id, | |
be2fb01f | 637 | ]; |
6a488035 TO |
638 | } |
639 | } | |
640 | return $ufValues[$ufID]; | |
641 | } | |
96025800 | 642 | |
d343069c CW |
643 | /** |
644 | * @inheritDoc | |
645 | */ | |
20e41014 | 646 | public function addSelectWhereClause() { |
d343069c | 647 | // Prevent default behavior of joining ACLs onto the contact_id field |
be2fb01f | 648 | $clauses = []; |
032346cc CW |
649 | CRM_Utils_Hook::selectWhereClause($this, $clauses); |
650 | return $clauses; | |
d343069c CW |
651 | } |
652 | ||
6a488035 | 653 | } |