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