Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
0f03f337 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
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 | |
0f03f337 | 31 | * @copyright CiviCRM LLC (c) 2004-2017 |
6a488035 TO |
32 | */ |
33 | class CRM_Contact_BAO_Contact extends CRM_Contact_DAO_Contact { | |
34 | ||
35 | /** | |
36 | * SQL function used to format the phone_numeric field via trigger. | |
37 | * @see self::triggerInfo() | |
38 | * | |
39 | * Note that this is also used by the 4.3 upgrade script. | |
40 | * @see CRM_Upgrade_Incremental_php_FourThree | |
41 | */ | |
42 | const DROP_STRIP_FUNCTION_43 = "DROP FUNCTION IF EXISTS civicrm_strip_non_numeric"; | |
43 | const CREATE_STRIP_FUNCTION_43 = " | |
44 | CREATE FUNCTION civicrm_strip_non_numeric(input VARCHAR(255) CHARACTER SET utf8) | |
45 | RETURNS VARCHAR(255) CHARACTER SET utf8 | |
46 | DETERMINISTIC | |
47 | NO SQL | |
48 | BEGIN | |
49 | DECLARE output VARCHAR(255) CHARACTER SET utf8 DEFAULT ''; | |
50 | DECLARE iterator INT DEFAULT 1; | |
51 | WHILE iterator < (LENGTH(input) + 1) DO | |
52 | IF SUBSTRING(input, iterator, 1) IN ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9') THEN | |
53 | SET output = CONCAT(output, SUBSTRING(input, iterator, 1)); | |
54 | END IF; | |
55 | SET iterator = iterator + 1; | |
56 | END WHILE; | |
57 | RETURN output; | |
58 | END"; | |
59 | ||
60 | /** | |
fe482240 | 61 | * The types of communication preferences. |
6a488035 TO |
62 | * |
63 | * @var array | |
64 | */ | |
22f80e87 EM |
65 | static $_commPrefs = array( |
66 | 'do_not_phone', | |
67 | 'do_not_email', | |
68 | 'do_not_mail', | |
69 | 'do_not_sms', | |
70 | 'do_not_trade', | |
71 | ); | |
6a488035 TO |
72 | |
73 | /** | |
fe482240 | 74 | * Types of greetings. |
6a488035 TO |
75 | * |
76 | * @var array | |
77 | */ | |
22f80e87 EM |
78 | static $_greetingTypes = array( |
79 | 'addressee', | |
80 | 'email_greeting', | |
81 | 'postal_greeting', | |
82 | ); | |
6a488035 TO |
83 | |
84 | /** | |
fe482240 | 85 | * Static field for all the contact information that we can potentially import. |
6a488035 TO |
86 | * |
87 | * @var array | |
6a488035 TO |
88 | */ |
89 | static $_importableFields = array(); | |
90 | ||
91 | /** | |
fe482240 | 92 | * Static field for all the contact information that we can potentially export. |
6a488035 TO |
93 | * |
94 | * @var array | |
6a488035 TO |
95 | */ |
96 | static $_exportableFields = NULL; | |
86538308 EM |
97 | |
98 | /** | |
22f80e87 | 99 | * Class constructor. |
86538308 | 100 | */ |
00be9182 | 101 | public function __construct() { |
6a488035 TO |
102 | parent::__construct(); |
103 | } | |
104 | ||
105 | /** | |
22f80e87 | 106 | * Takes an associative array and creates a contact object. |
6a488035 | 107 | * |
22f80e87 | 108 | * The function extracts all the params it needs to initialize the create a |
6a488035 TO |
109 | * contact object. the params array could contain additional unused name/value |
110 | * pairs | |
111 | * | |
77c5b619 | 112 | * @param array $params |
22f80e87 | 113 | * (reference) an assoc array of name/value pairs. |
6a488035 | 114 | * |
a1a2a83d | 115 | * @return CRM_Contact_BAO_Contact|CRM_Core_Error|NULL |
22f80e87 EM |
116 | * Created or updated contact object or error object. |
117 | * (error objects are being phased out in favour of exceptions) | |
6a488035 | 118 | */ |
00be9182 | 119 | public static function add(&$params) { |
6a488035 TO |
120 | $contact = new CRM_Contact_DAO_Contact(); |
121 | ||
122 | if (empty($params)) { | |
a1a2a83d | 123 | return NULL; |
6a488035 TO |
124 | } |
125 | ||
22f80e87 | 126 | // Fix for validate contact sub type CRM-5143. |
9bb77b8e TO |
127 | if (isset($params['contact_sub_type'])) { |
128 | if (empty($params['contact_sub_type'])) { | |
6a488035 TO |
129 | $params['contact_sub_type'] = 'null'; |
130 | } | |
131 | else { | |
132 | if (!CRM_Contact_BAO_ContactType::isExtendsContactType($params['contact_sub_type'], | |
133 | $params['contact_type'], TRUE | |
9bb77b8e TO |
134 | ) |
135 | ) { | |
6a488035 TO |
136 | // we'll need to fix tests to handle this |
137 | // CRM-7925 | |
138 | CRM_Core_Error::fatal(ts('The Contact Sub Type does not match the Contact type for this record')); | |
139 | } | |
f2181617 | 140 | $params['contact_sub_type'] = CRM_Utils_Array::implodePadded($params['contact_sub_type']); |
6a488035 TO |
141 | } |
142 | } | |
143 | else { | |
22f80e87 EM |
144 | // Reset the value. |
145 | // CRM-101XX. | |
6a488035 TO |
146 | $params['contact_sub_type'] = 'null'; |
147 | } | |
148 | ||
22f80e87 | 149 | // Fixed contact source. |
6a488035 TO |
150 | if (isset($params['contact_source'])) { |
151 | $params['source'] = $params['contact_source']; | |
152 | } | |
153 | ||
22f80e87 | 154 | // Fix for preferred communication method. |
ae97282e | 155 | $prefComm = CRM_Utils_Array::value('preferred_communication_method', $params, ''); |
6a488035 TO |
156 | if ($prefComm && is_array($prefComm)) { |
157 | unset($params['preferred_communication_method']); | |
6a488035 | 158 | |
f2181617 | 159 | CRM_Utils_Array::formatArrayKeys($prefComm); |
160 | $prefComm = CRM_Utils_Array::implodePadded($prefComm); | |
6a488035 TO |
161 | } |
162 | ||
ae97282e | 163 | $contact->preferred_communication_method = $prefComm; |
164 | ||
6a488035 TO |
165 | $allNull = $contact->copyValues($params); |
166 | ||
167 | $contact->id = CRM_Utils_Array::value('contact_id', $params); | |
168 | ||
169 | if ($contact->contact_type == 'Individual') { | |
170 | $allNull = FALSE; | |
171 | ||
22f80e87 | 172 | // Format individual fields. |
6a488035 TO |
173 | CRM_Contact_BAO_Individual::format($params, $contact); |
174 | } | |
175 | elseif ($contact->contact_type == 'Household') { | |
176 | if (isset($params['household_name'])) { | |
177 | $allNull = FALSE; | |
178 | $contact->display_name = $contact->sort_name = CRM_Utils_Array::value('household_name', $params, ''); | |
179 | } | |
180 | } | |
181 | elseif ($contact->contact_type == 'Organization') { | |
182 | if (isset($params['organization_name'])) { | |
183 | $allNull = FALSE; | |
184 | $contact->display_name = $contact->sort_name = CRM_Utils_Array::value('organization_name', $params, ''); | |
185 | } | |
186 | } | |
187 | ||
6a488035 TO |
188 | $privacy = CRM_Utils_Array::value('privacy', $params); |
189 | if ($privacy && | |
190 | is_array($privacy) && | |
191 | !empty($privacy) | |
192 | ) { | |
193 | $allNull = FALSE; | |
194 | foreach (self::$_commPrefs as $name) { | |
195 | $contact->$name = CRM_Utils_Array::value($name, $privacy, FALSE); | |
196 | } | |
197 | } | |
198 | ||
22f80e87 EM |
199 | // Since hash was required, make sure we have a 0 value for it (CRM-1063). |
200 | // @todo - does this mean we can remove this block? | |
201 | // Fixed in 1.5 by making hash optional, only do this in create mode, not update. | |
6a488035 TO |
202 | if ((!array_key_exists('hash', $contact) || !$contact->hash) && !$contact->id) { |
203 | $allNull = FALSE; | |
204 | $contact->hash = md5(uniqid(rand(), TRUE)); | |
205 | } | |
206 | ||
207 | // Even if we don't need $employerId, it's important to call getFieldValue() before | |
208 | // the contact is saved because we want the existing value to be cached. | |
209 | // createCurrentEmployerRelationship() needs the old value not the updated one. CRM-10788 | |
210 | $employerId = empty($contact->id) ? NULL : CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contact->id, 'employer_id'); | |
211 | ||
212 | if (!$allNull) { | |
213 | $contact->save(); | |
214 | ||
215 | CRM_Core_BAO_Log::register($contact->id, | |
216 | 'civicrm_contact', | |
217 | $contact->id | |
218 | ); | |
219 | } | |
220 | ||
fad0497c | 221 | if ($contact->contact_type == 'Individual' && (isset($params['current_employer']) || isset($params['employer_id']))) { |
22f80e87 | 222 | // Create current employer. |
f06fc7d2 | 223 | $newEmployer = !empty($params['employer_id']) ? $params['employer_id'] : CRM_Utils_Array::value('current_employer', $params); |
224 | ||
225 | $newContact = FALSE; | |
226 | if (empty($params['contact_id'])) { | |
227 | $newContact = TRUE; | |
6a488035 | 228 | } |
fad0497c | 229 | if ($newEmployer) { |
6eae3162 | 230 | CRM_Contact_BAO_Contact_Utils::createCurrentEmployerRelationship($contact->id, $newEmployer, $employerId, $newContact); |
6a488035 TO |
231 | } |
232 | else { | |
6a488035 TO |
233 | if ($employerId) { |
234 | CRM_Contact_BAO_Contact_Utils::clearCurrentEmployer($contact->id, $employerId); | |
235 | } | |
236 | } | |
237 | } | |
238 | ||
22f80e87 | 239 | // Update cached employer name. |
6a488035 TO |
240 | if ($contact->contact_type == 'Organization') { |
241 | CRM_Contact_BAO_Contact_Utils::updateCurrentEmployer($contact->id); | |
242 | } | |
243 | ||
244 | return $contact; | |
245 | } | |
246 | ||
247 | /** | |
22f80e87 EM |
248 | * Create contact. |
249 | * | |
6a488035 TO |
250 | * takes an associative array and creates a contact object and all the associated |
251 | * derived objects (i.e. individual, location, email, phone etc) | |
252 | * | |
253 | * This function is invoked from within the web form layer and also from the api layer | |
254 | * | |
77c5b619 TO |
255 | * @param array $params |
256 | * (reference ) an assoc array of name/value pairs. | |
257 | * @param bool $fixAddress | |
258 | * If we need to fix address. | |
259 | * @param bool $invokeHooks | |
260 | * If we need to invoke hooks. | |
6a488035 | 261 | * |
60fd90d8 DG |
262 | * @param bool $skipDelete |
263 | * Unclear parameter, passed to website create | |
264 | * | |
22f80e87 | 265 | * @todo explain this parameter |
d9f93da9 EM |
266 | * |
267 | * @throws Exception | |
22f80e87 EM |
268 | * @return CRM_Contact_BAO_Contact|CRM_Core_Error |
269 | * Created or updated contribution object. We are deprecating returning an error in | |
270 | * favour of exceptions | |
6a488035 | 271 | */ |
60fd90d8 | 272 | public static function &create(&$params, $fixAddress = TRUE, $invokeHooks = TRUE, $skipDelete = FALSE) { |
9bb77b8e TO |
273 | $contact = NULL; |
274 | if (empty($params['contact_type']) && empty($params['contact_id'])) { | |
275 | return $contact; | |
276 | } | |
6a488035 | 277 | |
9bb77b8e TO |
278 | $isEdit = TRUE; |
279 | if ($invokeHooks) { | |
280 | if (!empty($params['contact_id'])) { | |
281 | CRM_Utils_Hook::pre('edit', $params['contact_type'], $params['contact_id'], $params); | |
282 | } | |
283 | else { | |
284 | CRM_Utils_Hook::pre('create', $params['contact_type'], NULL, $params); | |
285 | $isEdit = FALSE; | |
6a488035 | 286 | } |
9bb77b8e | 287 | } |
6a488035 | 288 | |
9bb77b8e | 289 | $config = CRM_Core_Config::singleton(); |
6a488035 | 290 | |
22f80e87 | 291 | // CRM-6942: set preferred language to the current language if it’s unset (and we’re creating a contact). |
9bb77b8e | 292 | if (empty($params['contact_id'])) { |
9747df8a | 293 | // A case could be made for checking isset rather than empty but this is more consistent with previous behaviour. |
294 | if (empty($params['preferred_language']) && ($language = CRM_Core_I18n::getContactDefaultLanguage()) != FALSE) { | |
295 | $params['preferred_language'] = $language; | |
296 | } | |
297 | ||
298 | // CRM-9739: set greeting & addressee if unset and we’re creating a contact. | |
9bb77b8e TO |
299 | foreach (self::$_greetingTypes as $greeting) { |
300 | if (empty($params[$greeting . '_id'])) { | |
22f80e87 EM |
301 | if ($defaultGreetingTypeId |
302 | = CRM_Contact_BAO_Contact_Utils::defaultGreeting($params['contact_type'], $greeting) | |
9bb77b8e TO |
303 | ) { |
304 | $params[$greeting . '_id'] = $defaultGreetingTypeId; | |
6a488035 TO |
305 | } |
306 | } | |
307 | } | |
9bb77b8e | 308 | } |
6a488035 | 309 | |
9bb77b8e | 310 | $transaction = new CRM_Core_Transaction(); |
6a488035 | 311 | |
9bb77b8e TO |
312 | $contact = self::add($params); |
313 | if (!$contact) { | |
b44e3f84 | 314 | // Not dying here is stupid, since we get into weird situation and into a bug that |
9bb77b8e TO |
315 | // is impossible to figure out for the user or for us |
316 | // CRM-7925 | |
317 | CRM_Core_Error::fatal(); | |
318 | } | |
6a488035 | 319 | |
9bb77b8e | 320 | $params['contact_id'] = $contact->id; |
6a488035 | 321 | |
aaffa79f | 322 | if (Civi::settings()->get('is_enabled')) { |
22f80e87 | 323 | // Enabling multisite causes the contact to be added to the domain group. |
9bb77b8e TO |
324 | $domainGroupID = CRM_Core_BAO_Domain::getGroupId(); |
325 | if (!empty($domainGroupID)) { | |
326 | if (!empty($params['group']) && is_array($params['group'])) { | |
327 | $params['group'][$domainGroupID] = 1; | |
6a488035 | 328 | } |
9bb77b8e TO |
329 | else { |
330 | $params['group'] = array($domainGroupID => 1); | |
6a488035 TO |
331 | } |
332 | } | |
9bb77b8e | 333 | } |
6a488035 | 334 | |
9bb77b8e TO |
335 | if (array_key_exists('group', $params)) { |
336 | $contactIds = array($params['contact_id']); | |
337 | foreach ($params['group'] as $groupId => $flag) { | |
338 | if ($flag == 1) { | |
339 | CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIds, $groupId); | |
340 | } | |
341 | elseif ($flag == -1) { | |
342 | CRM_Contact_BAO_GroupContact::removeContactsFromGroup($contactIds, $groupId); | |
343 | } | |
6a488035 | 344 | } |
9bb77b8e | 345 | } |
6a488035 | 346 | |
22f80e87 | 347 | // Add location Block data. |
9bb77b8e TO |
348 | $blocks = CRM_Core_BAO_Location::create($params, $fixAddress); |
349 | foreach ($blocks as $name => $value) { | |
350 | $contact->$name = $value; | |
351 | } | |
139a60f3 | 352 | if (!empty($params['updateBlankLocInfo'])) { |
353 | $skipDelete = TRUE; | |
354 | } | |
6a488035 | 355 | |
60fd90d8 DG |
356 | //add website |
357 | CRM_Core_BAO_Website::create($params['website'], $contact->id, $skipDelete); | |
6a488035 | 358 | |
9bb77b8e TO |
359 | //get userID from session |
360 | $session = CRM_Core_Session::singleton(); | |
361 | $userID = $session->get('userID'); | |
362 | // add notes | |
363 | if (!empty($params['note'])) { | |
364 | if (is_array($params['note'])) { | |
365 | foreach ($params['note'] as $note) { | |
6a488035 TO |
366 | $contactId = $contact->id; |
367 | if (isset($note['contact_id'])) { | |
368 | $contactId = $note['contact_id']; | |
369 | } | |
370 | //if logged in user, overwrite contactId | |
371 | if ($userID) { | |
372 | $contactId = $userID; | |
373 | } | |
374 | ||
375 | $noteParams = array( | |
376 | 'entity_id' => $contact->id, | |
377 | 'entity_table' => 'civicrm_contact', | |
9bb77b8e TO |
378 | 'note' => $note['note'], |
379 | 'subject' => CRM_Utils_Array::value('subject', $note), | |
6a488035 TO |
380 | 'contact_id' => $contactId, |
381 | ); | |
382 | CRM_Core_BAO_Note::add($noteParams, CRM_Core_DAO::$_nullArray); | |
383 | } | |
384 | } | |
9bb77b8e TO |
385 | else { |
386 | $contactId = $contact->id; | |
387 | if (isset($note['contact_id'])) { | |
388 | $contactId = $note['contact_id']; | |
389 | } | |
390 | //if logged in user, overwrite contactId | |
391 | if ($userID) { | |
392 | $contactId = $userID; | |
393 | } | |
6a488035 | 394 | |
9bb77b8e TO |
395 | $noteParams = array( |
396 | 'entity_id' => $contact->id, | |
397 | 'entity_table' => 'civicrm_contact', | |
398 | 'note' => $params['note'], | |
399 | 'subject' => CRM_Utils_Array::value('subject', $params), | |
400 | 'contact_id' => $contactId, | |
6a488035 | 401 | ); |
9bb77b8e | 402 | CRM_Core_BAO_Note::add($noteParams, CRM_Core_DAO::$_nullArray); |
6a488035 | 403 | } |
9bb77b8e | 404 | } |
6a488035 | 405 | |
9bb77b8e TO |
406 | // update the UF user_unique_id if that has changed |
407 | CRM_Core_BAO_UFMatch::updateUFName($contact->id); | |
408 | ||
409 | if (!empty($params['custom']) && | |
410 | is_array($params['custom']) | |
411 | ) { | |
412 | CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_contact', $contact->id); | |
413 | } | |
414 | ||
415 | // make a civicrm_subscription_history entry only on contact create (CRM-777) | |
416 | if (empty($params['contact_id'])) { | |
417 | $subscriptionParams = array( | |
418 | 'contact_id' => $contact->id, | |
419 | 'status' => 'Added', | |
420 | 'method' => 'Admin', | |
6a488035 | 421 | ); |
9bb77b8e TO |
422 | CRM_Contact_BAO_SubscriptionHistory::create($subscriptionParams); |
423 | } | |
6a488035 | 424 | |
9bb77b8e | 425 | $transaction->commit(); |
6a488035 | 426 | |
9bb77b8e TO |
427 | // CRM-6367: fetch the right label for contact type’s display |
428 | $contact->contact_type_display = CRM_Core_DAO::getFieldValue( | |
429 | 'CRM_Contact_DAO_ContactType', | |
430 | $contact->contact_type, | |
431 | 'label', | |
432 | 'name' | |
433 | ); | |
434 | ||
435 | if (!$config->doNotResetCache) { | |
436 | // Note: doNotResetCache flag is currently set by import contact process and merging, | |
437 | // since resetting and | |
438 | // rebuilding cache could be expensive (for many contacts). We might come out with better | |
439 | // approach in future. | |
440 | CRM_Contact_BAO_Contact_Utils::clearContactCaches($contact->id); | |
441 | } | |
442 | ||
443 | if ($invokeHooks) { | |
444 | if ($isEdit) { | |
445 | CRM_Utils_Hook::post('edit', $params['contact_type'], $contact->id, $contact); | |
446 | } | |
447 | else { | |
448 | CRM_Utils_Hook::post('create', $params['contact_type'], $contact->id, $contact); | |
6a488035 | 449 | } |
9bb77b8e | 450 | } |
6a488035 | 451 | |
9bb77b8e TO |
452 | // process greetings CRM-4575, cache greetings |
453 | self::processGreetings($contact); | |
6a488035 | 454 | |
9bb77b8e | 455 | return $contact; |
6a488035 TO |
456 | } |
457 | ||
458 | /** | |
22f80e87 | 459 | * Get the display name and image of a contact. |
6a488035 | 460 | * |
77c5b619 TO |
461 | * @param int $id |
462 | * The contactId. | |
6a488035 | 463 | * |
22f80e87 EM |
464 | * @param bool $includeTypeInReturnParameters |
465 | * Should type be part of the returned array? | |
d9f93da9 | 466 | * |
a6c01b45 CW |
467 | * @return array |
468 | * the displayName and contactImage for this contact | |
6a488035 | 469 | */ |
22f80e87 | 470 | public static function getDisplayAndImage($id, $includeTypeInReturnParameters = FALSE) { |
95fec3ce | 471 | //CRM-14276 added the * on the civicrm_contact table so that we have all the contact info available |
250b3b1f | 472 | $sql = " |
95fec3ce | 473 | SELECT civicrm_contact.*, |
6a488035 TO |
474 | civicrm_email.email as email |
475 | FROM civicrm_contact | |
476 | LEFT JOIN civicrm_email ON civicrm_email.contact_id = civicrm_contact.id | |
477 | AND civicrm_email.is_primary = 1 | |
478 | WHERE civicrm_contact.id = " . CRM_Utils_Type::escape($id, 'Integer'); | |
479 | $dao = new CRM_Core_DAO(); | |
480 | $dao->query($sql); | |
481 | if ($dao->fetch()) { | |
482 | $image = CRM_Contact_BAO_Contact_Utils::getImage($dao->contact_sub_type ? | |
250b3b1f | 483 | $dao->contact_sub_type : $dao->contact_type, FALSE, $id |
6a488035 TO |
484 | ); |
485 | $imageUrl = CRM_Contact_BAO_Contact_Utils::getImage($dao->contact_sub_type ? | |
250b3b1f | 486 | $dao->contact_sub_type : $dao->contact_type, TRUE, $id |
6a488035 TO |
487 | ); |
488 | ||
489 | // use email if display_name is empty | |
490 | if (empty($dao->display_name)) { | |
250b3b1f | 491 | $displayName = $dao->email; |
492 | } | |
493 | else { | |
494 | $displayName = $dao->display_name; | |
495 | } | |
496 | ||
497 | CRM_Utils_Hook::alterDisplayName($displayName, $id, $dao); | |
95fec3ce | 498 | |
22f80e87 | 499 | return $includeTypeInReturnParameters ? array( |
967fddb5 | 500 | $displayName, |
250b3b1f | 501 | $image, |
502 | $dao->contact_type, | |
503 | $dao->contact_sub_type, | |
504 | $imageUrl, | |
967fddb5 | 505 | ) : array($displayName, $image, $imageUrl); |
6a488035 TO |
506 | } |
507 | return NULL; | |
508 | } | |
509 | ||
870156d6 | 510 | /** |
511 | * Add billing fields to the params if appropriate. | |
512 | * | |
513 | * If we have ANY name fields then we want to ignore all the billing name fields. However, if we | |
514 | * don't then we should set the name fields to the filling fields AND add the preserveDBName | |
515 | * parameter (which will tell the BAO only to set those fields if none already exist. | |
516 | * | |
517 | * We specifically don't want to set first name from billing and last name form an on-page field. Mixing & | |
518 | * matching is best done by hipsters. | |
519 | * | |
520 | * @param array $params | |
521 | */ | |
522 | public static function addBillingNameFieldsIfOtherwiseNotSet(&$params) { | |
afa4787b | 523 | $nameFields = array('first_name', 'middle_name', 'last_name', 'nick_name', 'prefix_id', 'suffix_id'); |
870156d6 | 524 | foreach ($nameFields as $field) { |
525 | if (!empty($params[$field])) { | |
526 | return; | |
527 | } | |
528 | } | |
529 | // There are only 3 - we can iterate through them twice :-) | |
530 | foreach ($nameFields as $field) { | |
531 | if (!empty($params['billing_' . $field])) { | |
532 | $params[$field] = $params['billing_' . $field]; | |
533 | } | |
534 | $params['preserveDBName'] = TRUE; | |
535 | } | |
536 | ||
537 | } | |
538 | ||
688ad538 | 539 | /** |
22f80e87 EM |
540 | * Create last viewed link to recently updated contact. |
541 | * | |
77c5b619 | 542 | * @param array $crudLinkSpec |
55d2c6f1 EM |
543 | * - action: int, CRM_Core_Action::UPDATE or CRM_Core_Action::VIEW [default: VIEW] |
544 | * - entity_table: string, eg "civicrm_contact" | |
545 | * - entity_id: int | |
546 | * | |
72b3a70c | 547 | * @return array|NULL |
16b10e64 CW |
548 | * NULL if unavailable, or |
549 | * [path: string, query: string, title: string] | |
688ad538 TO |
550 | * @see CRM_Utils_System::createDefaultCrudLink |
551 | */ | |
552 | public function createDefaultCrudLink($crudLinkSpec) { | |
553 | switch ($crudLinkSpec['action']) { | |
554 | case CRM_Core_Action::VIEW: | |
6c552737 | 555 | $result = array( |
688ad538 TO |
556 | 'title' => $this->display_name, |
557 | 'path' => 'civicrm/contact/view', | |
558 | 'query' => array( | |
559 | 'reset' => 1, | |
560 | 'cid' => $this->id, | |
561 | ), | |
562 | ); | |
6c552737 | 563 | return $result; |
ea100cb5 | 564 | |
688ad538 | 565 | case CRM_Core_Action::UPDATE: |
6c552737 | 566 | $result = array( |
688ad538 TO |
567 | 'title' => $this->display_name, |
568 | 'path' => 'civicrm/contact/add', | |
569 | 'query' => array( | |
570 | 'reset' => 1, | |
571 | 'action' => 'update', | |
572 | 'cid' => $this->id, | |
573 | ), | |
574 | ); | |
6c552737 | 575 | return $result; |
688ad538 TO |
576 | } |
577 | return NULL; | |
578 | } | |
579 | ||
6a488035 | 580 | /** |
6a488035 TO |
581 | * Get the values for pseudoconstants for name->value and reverse. |
582 | * | |
77c5b619 TO |
583 | * @param array $defaults |
584 | * (reference) the default values, some of which need to be resolved. | |
585 | * @param bool $reverse | |
586 | * True if we want to resolve the values in the reverse direction (value -> name). | |
6a488035 | 587 | */ |
00be9182 | 588 | public static function resolveDefaults(&$defaults, $reverse = FALSE) { |
22f80e87 | 589 | // Hack for birth_date. |
a7488080 | 590 | if (!empty($defaults['birth_date'])) { |
6a488035 TO |
591 | if (is_array($defaults['birth_date'])) { |
592 | $defaults['birth_date'] = CRM_Utils_Date::format($defaults['birth_date'], '-'); | |
593 | } | |
594 | } | |
595 | ||
e6c4755b CW |
596 | CRM_Utils_Array::lookupValue($defaults, 'prefix', CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id'), $reverse); |
597 | CRM_Utils_Array::lookupValue($defaults, 'suffix', CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id'), $reverse); | |
26cf88b5 | 598 | CRM_Utils_Array::lookupValue($defaults, 'gender', CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'), $reverse); |
aa62b355 | 599 | CRM_Utils_Array::lookupValue($defaults, 'communication_style', CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'communication_style_id'), $reverse); |
6a488035 TO |
600 | |
601 | //lookup value of email/postal greeting, addressee, CRM-4575 | |
602 | foreach (self::$_greetingTypes as $greeting) { | |
9bb77b8e TO |
603 | $filterCondition = array( |
604 | 'contact_type' => CRM_Utils_Array::value('contact_type', $defaults), | |
6a488035 TO |
605 | 'greeting_type' => $greeting, |
606 | ); | |
607 | CRM_Utils_Array::lookupValue($defaults, $greeting, | |
608 | CRM_Core_PseudoConstant::greeting($filterCondition), $reverse | |
609 | ); | |
610 | } | |
611 | ||
612 | $blocks = array('address', 'im', 'phone'); | |
613 | foreach ($blocks as $name) { | |
614 | if (!array_key_exists($name, $defaults) || !is_array($defaults[$name])) { | |
615 | continue; | |
616 | } | |
617 | foreach ($defaults[$name] as $count => & $values) { | |
618 | ||
619 | //get location type id. | |
b2b0530a | 620 | CRM_Utils_Array::lookupValue($values, 'location_type', CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'), $reverse); |
6a488035 TO |
621 | |
622 | if ($name == 'address') { | |
623 | // FIXME: lookupValue doesn't work for vcard_name | |
a7488080 | 624 | if (!empty($values['location_type_id'])) { |
8f785c9e | 625 | $vcardNames = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id', array('labelColumn' => 'vcard_name')); |
6a488035 TO |
626 | $values['vcard_name'] = $vcardNames[$values['location_type_id']]; |
627 | } | |
628 | ||
629 | if (!CRM_Utils_Array::lookupValue($values, | |
630 | 'country', | |
631 | CRM_Core_PseudoConstant::country(), | |
632 | $reverse | |
633 | ) && | |
634 | $reverse | |
635 | ) { | |
636 | CRM_Utils_Array::lookupValue($values, | |
637 | 'country', | |
638 | CRM_Core_PseudoConstant::countryIsoCode(), | |
639 | $reverse | |
640 | ); | |
641 | } | |
642 | ||
643 | // CRM-7597 | |
644 | // if we find a country id above, we need to restrict it to that country | |
645 | // rather than the list of all countries | |
646 | ||
647 | if (!empty($values['country_id'])) { | |
648 | $stateProvinceList = CRM_Core_PseudoConstant::stateProvinceForCountry($values['country_id']); | |
649 | } | |
650 | else { | |
651 | $stateProvinceList = CRM_Core_PseudoConstant::stateProvince(); | |
652 | } | |
653 | if (!CRM_Utils_Array::lookupValue($values, | |
654 | 'state_province', | |
655 | $stateProvinceList, | |
656 | $reverse | |
657 | ) && | |
658 | $reverse | |
659 | ) { | |
660 | ||
661 | if (!empty($values['country_id'])) { | |
662 | $stateProvinceList = CRM_Core_PseudoConstant::stateProvinceForCountry($values['country_id'], 'abbreviation'); | |
663 | } | |
664 | else { | |
665 | $stateProvinceList = CRM_Core_PseudoConstant::stateProvinceAbbreviation(); | |
666 | } | |
667 | CRM_Utils_Array::lookupValue($values, | |
668 | 'state_province', | |
669 | $stateProvinceList, | |
670 | $reverse | |
671 | ); | |
672 | } | |
673 | ||
674 | if (!empty($values['state_province_id'])) { | |
675 | $countyList = CRM_Core_PseudoConstant::countyForState($values['state_province_id']); | |
676 | } | |
677 | else { | |
678 | $countyList = CRM_Core_PseudoConstant::county(); | |
679 | } | |
680 | CRM_Utils_Array::lookupValue($values, | |
681 | 'county', | |
682 | $countyList, | |
683 | $reverse | |
684 | ); | |
685 | } | |
686 | ||
687 | if ($name == 'im') { | |
688 | CRM_Utils_Array::lookupValue($values, | |
689 | 'provider', | |
e7e657f0 | 690 | CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id'), |
6a488035 TO |
691 | $reverse |
692 | ); | |
693 | } | |
694 | ||
695 | if ($name == 'phone') { | |
696 | CRM_Utils_Array::lookupValue($values, | |
697 | 'phone_type', | |
b4f964d9 | 698 | CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id'), |
6a488035 TO |
699 | $reverse |
700 | ); | |
701 | } | |
702 | ||
22f80e87 | 703 | // Kill the reference. |
6a488035 TO |
704 | unset($values); |
705 | } | |
706 | } | |
707 | } | |
708 | ||
709 | /** | |
0965e988 | 710 | * Fetch object based on array of properties. |
6a488035 | 711 | * |
77c5b619 TO |
712 | * @param array $params |
713 | * (reference ) an assoc array of name/value pairs. | |
714 | * @param array $defaults | |
715 | * (reference ) an assoc array to hold the name / value pairs. | |
6a488035 | 716 | * in a hierarchical manner |
77c5b619 TO |
717 | * @param bool $microformat |
718 | * For location in microformat. | |
6a488035 | 719 | * |
16b10e64 | 720 | * @return CRM_Contact_BAO_Contact |
6a488035 | 721 | */ |
00be9182 | 722 | public static function &retrieve(&$params, &$defaults, $microformat = FALSE) { |
6a488035 TO |
723 | if (array_key_exists('contact_id', $params)) { |
724 | $params['id'] = $params['contact_id']; | |
725 | } | |
726 | elseif (array_key_exists('id', $params)) { | |
727 | $params['contact_id'] = $params['id']; | |
728 | } | |
729 | ||
730 | $contact = self::getValues($params, $defaults); | |
731 | ||
732 | unset($params['id']); | |
733 | ||
734 | //get the block information for this contact | |
735 | $entityBlock = array('contact_id' => $params['contact_id']); | |
9bb77b8e TO |
736 | $blocks = CRM_Core_BAO_Location::getValues($entityBlock, $microformat); |
737 | $defaults = array_merge($defaults, $blocks); | |
738 | foreach ($blocks as $block => $value) { | |
739 | $contact->$block = $value; | |
740 | } | |
6a488035 TO |
741 | |
742 | if (!isset($params['noNotes'])) { | |
743 | $contact->notes = CRM_Core_BAO_Note::getValues($params, $defaults); | |
744 | } | |
745 | ||
746 | if (!isset($params['noRelationships'])) { | |
747 | $contact->relationship = CRM_Contact_BAO_Relationship::getValues($params, $defaults); | |
748 | } | |
749 | ||
750 | if (!isset($params['noGroups'])) { | |
751 | $contact->groupContact = CRM_Contact_BAO_GroupContact::getValues($params, $defaults); | |
752 | } | |
753 | ||
754 | if (!isset($params['noWebsite'])) { | |
755 | $contact->website = CRM_Core_BAO_Website::getValues($params, $defaults); | |
756 | } | |
757 | ||
758 | return $contact; | |
759 | } | |
760 | ||
761 | /** | |
0965e988 | 762 | * Get the display name of a contact. |
6a488035 | 763 | * |
77c5b619 TO |
764 | * @param int $id |
765 | * Id of the contact. | |
6a488035 | 766 | * |
72b3a70c CW |
767 | * @return null|string |
768 | * display name of the contact if found | |
6a488035 | 769 | */ |
00be9182 | 770 | public static function displayName($id) { |
6a488035 TO |
771 | $displayName = NULL; |
772 | if ($id) { | |
773 | $displayName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $id, 'display_name'); | |
774 | } | |
775 | ||
776 | return $displayName; | |
777 | } | |
778 | ||
779 | /** | |
55d2c6f1 | 780 | * Delete a contact and all its associated records. |
6a488035 | 781 | * |
77c5b619 TO |
782 | * @param int $id |
783 | * Id of the contact to delete. | |
784 | * @param bool $restore | |
785 | * Whether to actually restore, not delete. | |
786 | * @param bool $skipUndelete | |
787 | * Whether to force contact delete or not. | |
dbb0d30b | 788 | * @param bool $checkPermissions |
6a488035 | 789 | * |
55d2c6f1 EM |
790 | * @return bool |
791 | * Was contact deleted? | |
6a488035 | 792 | */ |
fa6448fa | 793 | public static function deleteContact($id, $restore = FALSE, $skipUndelete = FALSE, $checkPermissions = TRUE) { |
6a488035 TO |
794 | |
795 | if (!$id) { | |
796 | return FALSE; | |
797 | } | |
b0033001 | 798 | // If trash is disabled in system settings then we always skip |
89595c92 | 799 | if (!Civi::settings()->get('contact_undelete')) { |
b0033001 CW |
800 | $skipUndelete = TRUE; |
801 | } | |
6a488035 TO |
802 | |
803 | // make sure we have edit permission for this contact | |
804 | // before we delete | |
fa6448fa EM |
805 | if ($checkPermissions && (($skipUndelete && !CRM_Core_Permission::check('delete contacts')) || |
806 | ($restore && !CRM_Core_Permission::check('access deleted contacts'))) | |
6a488035 TO |
807 | ) { |
808 | return FALSE; | |
809 | } | |
2efcf0c2 | 810 | |
f182074e PN |
811 | // CRM-12929 |
812 | // Restrict contact to be delete if contact has financial trxns | |
813 | $error = NULL; | |
814 | if ($skipUndelete && CRM_Financial_BAO_FinancialItem::checkContactPresent(array($id), $error)) { | |
815 | return FALSE; | |
816 | } | |
6a488035 TO |
817 | |
818 | // make sure this contact_id does not have any membership types | |
819 | $membershipTypeID = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', | |
820 | $id, | |
821 | 'id', | |
822 | 'member_of_contact_id' | |
823 | ); | |
824 | if ($membershipTypeID) { | |
825 | return FALSE; | |
826 | } | |
827 | ||
828 | $contact = new CRM_Contact_DAO_Contact(); | |
829 | $contact->id = $id; | |
830 | if (!$contact->find(TRUE)) { | |
831 | return FALSE; | |
832 | } | |
833 | ||
834 | $contactType = $contact->contact_type; | |
6a488035 | 835 | // currently we only clear employer cache. |
690c56c3 | 836 | // we are now deleting inherited membership if any. |
6a488035 | 837 | if ($contact->contact_type == 'Organization') { |
690c56c3 | 838 | $action = $restore ? CRM_Core_Action::ENABLE : CRM_Core_Action::DISABLE; |
839 | $relationshipDtls = CRM_Contact_BAO_Relationship::getRelationship($id); | |
840 | if (!empty($relationshipDtls)) { | |
841 | foreach ($relationshipDtls as $rId => $details) { | |
842 | CRM_Contact_BAO_Relationship::disableEnableRelationship($rId, $action); | |
843 | } | |
844 | } | |
6a488035 TO |
845 | CRM_Contact_BAO_Contact_Utils::clearAllEmployee($id); |
846 | } | |
847 | ||
690c56c3 | 848 | if ($restore) { |
849 | return self::contactTrashRestore($contact, TRUE); | |
850 | } | |
851 | ||
6a488035 TO |
852 | // start a new transaction |
853 | $transaction = new CRM_Core_Transaction(); | |
854 | ||
b0033001 CW |
855 | if ($skipUndelete) { |
856 | CRM_Utils_Hook::pre('delete', $contactType, $id, CRM_Core_DAO::$_nullArray); | |
6a488035 TO |
857 | |
858 | //delete billing address if exists. | |
859 | CRM_Contribute_BAO_Contribution::deleteAddress(NULL, $id); | |
860 | ||
861 | // delete the log entries since we dont have triggers enabled as yet | |
862 | $logDAO = new CRM_Core_DAO_Log(); | |
863 | $logDAO->entity_table = 'civicrm_contact'; | |
864 | $logDAO->entity_id = $id; | |
865 | $logDAO->delete(); | |
1071730c | 866 | |
c3d24ba7 PN |
867 | // delete contact participants CRM-12155 |
868 | CRM_Event_BAO_Participant::deleteContactParticipant($id); | |
6a488035 | 869 | |
c3d24ba7 PN |
870 | // delete contact contributions CRM-12155 |
871 | CRM_Contribute_BAO_Contribution::deleteContactContribution($id); | |
1071730c | 872 | |
6a488035 TO |
873 | // do activity cleanup, CRM-5604 |
874 | CRM_Activity_BAO_Activity::cleanupActivity($id); | |
875 | ||
876 | // delete all notes related to contact | |
877 | CRM_Core_BAO_Note::cleanContactNotes($id); | |
878 | ||
55033e60 PJ |
879 | // delete cases related to contact |
880 | $contactCases = CRM_Case_BAO_Case::retrieveCaseIdsByContactId($id); | |
881 | if (!empty($contactCases)) { | |
d3b60a94 | 882 | foreach ($contactCases as $caseId) { |
55033e60 PJ |
883 | //check if case is associate with other contact or not. |
884 | $caseContactId = CRM_Case_BAO_Case::getCaseClients($caseId); | |
d3b60a94 | 885 | if (count($caseContactId) <= 1) { |
55033e60 PJ |
886 | CRM_Case_BAO_Case::deleteCase($caseId); |
887 | } | |
888 | } | |
889 | } | |
890 | ||
6a488035 TO |
891 | $contact->delete(); |
892 | } | |
893 | else { | |
894 | self::contactTrashRestore($contact); | |
895 | } | |
896 | ||
897 | //delete the contact id from recently view | |
898 | CRM_Utils_Recent::delContact($id); | |
ae44c3b9 | 899 | self::updateContactCache($id, empty($restore)); |
6a488035 TO |
900 | |
901 | // delete any dupe cache entry | |
902 | CRM_Core_BAO_PrevNextCache::deleteItem($id); | |
903 | ||
904 | $transaction->commit(); | |
905 | ||
b0033001 CW |
906 | if ($skipUndelete) { |
907 | CRM_Utils_Hook::post('delete', $contactType, $contact->id, $contact); | |
908 | } | |
6a488035 TO |
909 | |
910 | // also reset the DB_DO global array so we can reuse the memory | |
911 | // http://issues.civicrm.org/jira/browse/CRM-4387 | |
912 | CRM_Core_DAO::freeResult(); | |
913 | ||
914 | return TRUE; | |
915 | } | |
916 | ||
ae44c3b9 | 917 | /** |
918 | * Action to update any caches relating to a recently update contact. | |
919 | * | |
920 | * I was going to call this from delete as well as from create to ensure the delete is being | |
921 | * done whenever a contact is set to is_deleted=1 BUT I found create is already over-aggressive in | |
922 | * that regard so adding it to delete seems to be enough to remove it from CRM_Contact_BAO_Contact_Permission | |
923 | * where the call involved a subquery that was locking the table. | |
924 | * | |
925 | * @param int $contactID | |
926 | * @param bool $isTrashed | |
927 | */ | |
928 | public static function updateContactCache($contactID, $isTrashed = FALSE) { | |
929 | ||
930 | if ($isTrashed) { | |
931 | CRM_Contact_BAO_GroupContactCache::removeContact($contactID); | |
932 | // This has been moved to here from CRM_Contact_BAO_Contact_Permission as that was causing | |
933 | // a table-locking query. It still seems a bit inadequate as it assumes the acl users can't see deleted | |
934 | // but this should not cause any change as long as contacts are not being trashed outside the | |
935 | // main functions for that. | |
936 | CRM_Core_DAO::executeQuery('DELETE FROM civicrm_acl_contact_cache WHERE contact_id = %1', array(1 => array($contactID, 'Integer'))); | |
937 | } | |
938 | else { | |
2b68a50c | 939 | CRM_Contact_BAO_GroupContactCache::opportunisticCacheFlush(); |
ae44c3b9 | 940 | } |
941 | } | |
942 | ||
6a488035 | 943 | /** |
55d2c6f1 | 944 | * Delete the image of a contact. |
6a488035 | 945 | * |
77c5b619 TO |
946 | * @param int $id |
947 | * Id of the contact. | |
6a488035 | 948 | * |
55d2c6f1 EM |
949 | * @return bool |
950 | * Was contact image deleted? | |
6a488035 TO |
951 | */ |
952 | public static function deleteContactImage($id) { | |
953 | if (!$id) { | |
954 | return FALSE; | |
955 | } | |
956 | $query = " | |
957 | UPDATE civicrm_contact | |
958 | SET image_URL=NULL | |
959 | WHERE id={$id}; "; | |
33621c4f | 960 | CRM_Core_DAO::executeQuery($query); |
6a488035 TO |
961 | return TRUE; |
962 | } | |
963 | ||
6a488035 | 964 | /** |
0965e988 | 965 | * Return proportional height and width of the image. |
6a488035 | 966 | * |
df8d3074 | 967 | * @param int $imageWidth |
77c5b619 | 968 | * Width of image. |
6a488035 | 969 | * |
df8d3074 | 970 | * @param int $imageHeight |
77c5b619 | 971 | * Height of image. |
6a488035 | 972 | * |
0965e988 EM |
973 | * @return array |
974 | * Thumb dimension of image | |
6a488035 TO |
975 | */ |
976 | public static function getThumbSize($imageWidth, $imageHeight) { | |
977 | $thumbWidth = 100; | |
978 | if ($imageWidth && $imageHeight) { | |
979 | $imageRatio = $imageWidth / $imageHeight; | |
980 | } | |
981 | else { | |
982 | $imageRatio = 1; | |
983 | } | |
984 | if ($imageRatio > 1) { | |
985 | $imageThumbWidth = $thumbWidth; | |
986 | $imageThumbHeight = round($thumbWidth / $imageRatio); | |
987 | } | |
988 | else { | |
989 | $imageThumbHeight = $thumbWidth; | |
990 | $imageThumbWidth = round($thumbWidth * $imageRatio); | |
991 | } | |
992 | ||
993 | return array($imageThumbWidth, $imageThumbHeight); | |
994 | } | |
995 | ||
996 | /** | |
0965e988 | 997 | * Validate type of contact image. |
6a488035 | 998 | * |
c490a46a | 999 | * @param array $params |
d3e86119 | 1000 | * @param string $imageIndex |
77c5b619 | 1001 | * Index of image field. |
d3e86119 | 1002 | * @param string $statusMsg |
77c5b619 TO |
1003 | * Status message to be set after operation. |
1004 | * @param string $opType | |
1005 | * Type of operation like fatal, bounce etc. | |
6a488035 | 1006 | * |
0965e988 | 1007 | * @return bool |
a6c01b45 | 1008 | * true if valid image extension |
6a488035 | 1009 | */ |
9bb77b8e TO |
1010 | public static function processImageParams( |
1011 | &$params, | |
6a488035 | 1012 | $imageIndex = 'image_URL', |
9bb77b8e TO |
1013 | $statusMsg = NULL, |
1014 | $opType = 'status' | |
6a488035 TO |
1015 | ) { |
1016 | $mimeType = array( | |
1017 | 'image/jpeg', | |
1018 | 'image/jpg', | |
1019 | 'image/png', | |
1020 | 'image/bmp', | |
1021 | 'image/p-jpeg', | |
1022 | 'image/gif', | |
1023 | 'image/x-png', | |
1024 | ); | |
1025 | ||
1026 | if (in_array($params[$imageIndex]['type'], $mimeType)) { | |
5da97e99 | 1027 | $photo = basename($params[$imageIndex]['name']); |
9bb77b8e | 1028 | $params[$imageIndex] = CRM_Utils_System::url('civicrm/contact/imagefile', 'photo=' . $photo, TRUE, NULL, TRUE, TRUE); |
6a488035 TO |
1029 | return TRUE; |
1030 | } | |
1031 | else { | |
1032 | unset($params[$imageIndex]); | |
1033 | if (!$statusMsg) { | |
1034 | $statusMsg = ts('Image could not be uploaded due to invalid type extension.'); | |
1035 | } | |
1036 | if ($opType == 'status') { | |
1037 | CRM_Core_Session::setStatus($statusMsg, 'Sorry', 'error'); | |
1038 | } | |
1039 | // FIXME: additional support for fatal, bounce etc could be added. | |
1040 | return FALSE; | |
1041 | } | |
1042 | } | |
1043 | ||
1044 | /** | |
0965e988 | 1045 | * Extract contact id from url for deleting contact image. |
6a488035 TO |
1046 | */ |
1047 | public static function processImage() { | |
1048 | ||
1050d25c CW |
1049 | $action = CRM_Utils_Request::retrieve('action', 'String'); |
1050 | $cid = CRM_Utils_Request::retrieve('cid', 'Positive'); | |
6a488035 | 1051 | // retrieve contact id in case of Profile context |
1050d25c | 1052 | $id = CRM_Utils_Request::retrieve('id', 'Positive'); |
6a488035 TO |
1053 | $cid = $cid ? $cid : $id; |
1054 | if ($action & CRM_Core_Action::DELETE) { | |
1050d25c | 1055 | if (CRM_Utils_Request::retrieve('confirmed', 'Boolean')) { |
6a488035 TO |
1056 | CRM_Contact_BAO_Contact::deleteContactImage($cid); |
1057 | CRM_Core_Session::setStatus(ts('Contact image deleted successfully'), ts('Image Deleted'), 'success'); | |
1058 | $session = CRM_Core_Session::singleton(); | |
1059 | $toUrl = $session->popUserContext(); | |
1060 | CRM_Utils_System::redirect($toUrl); | |
1061 | } | |
1062 | } | |
1063 | } | |
1064 | ||
1065 | /** | |
0965e988 | 1066 | * Function to set is_delete true or restore deleted contact. |
6a488035 | 1067 | * |
b0033001 | 1068 | * @param CRM_Contact_DAO_Contact $contact |
d3e86119 TO |
1069 | * Contact DAO object. |
1070 | * @param bool $restore | |
1071 | * True to set the is_delete = 1 else false to restore deleted contact, | |
b0033001 CW |
1072 | * i.e. is_delete = 0 |
1073 | * | |
1074 | * @return bool | |
6a488035 | 1075 | */ |
00be9182 | 1076 | public static function contactTrashRestore($contact, $restore = FALSE) { |
b0033001 CW |
1077 | $updateParams = array( |
1078 | 'id' => $contact->id, | |
1079 | 'is_deleted' => $restore ? 0 : 1, | |
1080 | ); | |
6a488035 | 1081 | |
b0033001 | 1082 | CRM_Utils_Hook::pre('update', $contact->contact_type, $contact->id, $updateParams); |
6a488035 TO |
1083 | |
1084 | $params = array(1 => array($contact->id, 'Integer')); | |
b0033001 | 1085 | if (!$restore) { |
6a488035 TO |
1086 | $query = "DELETE FROM civicrm_uf_match WHERE contact_id = %1"; |
1087 | CRM_Core_DAO::executeQuery($query, $params); | |
1088 | } | |
1089 | ||
b0033001 CW |
1090 | $contact->copyValues($updateParams); |
1091 | $contact->save(); | |
1092 | ||
1093 | CRM_Utils_Hook::post('update', $contact->contact_type, $contact->id, $contact); | |
6a488035 | 1094 | |
b0033001 | 1095 | return TRUE; |
6a488035 TO |
1096 | } |
1097 | ||
1098 | /** | |
1099 | * Get contact type for a contact. | |
1100 | * | |
77c5b619 TO |
1101 | * @param int $id |
1102 | * Id of the contact whose contact type is needed. | |
6a488035 | 1103 | * |
a6c01b45 CW |
1104 | * @return string |
1105 | * contact_type if $id found else null "" | |
6a488035 TO |
1106 | */ |
1107 | public static function getContactType($id) { | |
1108 | return CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $id, 'contact_type'); | |
1109 | } | |
1110 | ||
1111 | /** | |
1112 | * Get contact sub type for a contact. | |
1113 | * | |
77c5b619 TO |
1114 | * @param int $id |
1115 | * Id of the contact whose contact sub type is needed. | |
6a488035 | 1116 | * |
364c80f1 | 1117 | * @param string $implodeDelimiter |
dd244018 | 1118 | * |
a6c01b45 CW |
1119 | * @return string |
1120 | * contact_sub_type if $id found else null "" | |
6a488035 TO |
1121 | */ |
1122 | public static function getContactSubType($id, $implodeDelimiter = NULL) { | |
1123 | $subtype = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $id, 'contact_sub_type'); | |
1124 | if (!$subtype) { | |
1125 | return $implodeDelimiter ? NULL : array(); | |
1126 | } | |
1127 | ||
f2181617 | 1128 | $subtype = CRM_Utils_Array::explodePadded($subtype); |
6a488035 TO |
1129 | |
1130 | if ($implodeDelimiter) { | |
1131 | $subtype = implode($implodeDelimiter, $subtype); | |
1132 | } | |
1133 | return $subtype; | |
1134 | } | |
1135 | ||
1136 | /** | |
1137 | * Get pair of contact-type and sub-type for a contact. | |
1138 | * | |
77c5b619 TO |
1139 | * @param int $id |
1140 | * Id of the contact whose contact sub/contact type is needed. | |
6a488035 TO |
1141 | * |
1142 | * @return array | |
6a488035 TO |
1143 | */ |
1144 | public static function getContactTypes($id) { | |
9bb77b8e | 1145 | $params = array('id' => $id); |
6a488035 TO |
1146 | $details = array(); |
1147 | $contact = CRM_Core_DAO::commonRetrieve('CRM_Contact_DAO_Contact', | |
1148 | $params, | |
1149 | $details, | |
1150 | array('contact_type', 'contact_sub_type') | |
1151 | ); | |
1152 | ||
1153 | if ($contact) { | |
1154 | $contactTypes = array(); | |
0965e988 | 1155 | if ($contact->contact_sub_type) { |
f2181617 | 1156 | $contactTypes = CRM_Utils_Array::explodePadded($contact->contact_sub_type); |
0965e988 | 1157 | } |
6a488035 TO |
1158 | array_unshift($contactTypes, $contact->contact_type); |
1159 | ||
1160 | return $contactTypes; | |
1161 | } | |
1162 | else { | |
1163 | CRM_Core_Error::fatal(); | |
1164 | } | |
1165 | } | |
1166 | ||
1167 | /** | |
0965e988 | 1168 | * Combine all the importable fields from the lower levels object. |
6a488035 TO |
1169 | * |
1170 | * The ordering is important, since currently we do not have a weight | |
1071730c | 1171 | * scheme. Adding weight is super important |
6a488035 | 1172 | * |
dd244018 | 1173 | * @param int|string $contactType contact Type |
77c5b619 TO |
1174 | * @param bool $status |
1175 | * Status is used to manipulate first title. | |
1176 | * @param bool $showAll | |
1177 | * If true returns all fields (includes disabled fields). | |
1178 | * @param bool $isProfile | |
1179 | * If its profile mode. | |
1180 | * @param bool $checkPermission | |
1181 | * If false, do not include permissioning clause (for custom data). | |
6a488035 | 1182 | * |
dd244018 EM |
1183 | * @param bool $withMultiCustomFields |
1184 | * | |
a6c01b45 CW |
1185 | * @return array |
1186 | * array of importable Fields | |
6a488035 | 1187 | */ |
5c9ff055 | 1188 | public static function importableFields( |
9bb77b8e TO |
1189 | $contactType = 'Individual', |
1190 | $status = FALSE, | |
1191 | $showAll = FALSE, | |
1192 | $isProfile = FALSE, | |
6a488035 TO |
1193 | $checkPermission = TRUE, |
1194 | $withMultiCustomFields = FALSE | |
1195 | ) { | |
1196 | if (empty($contactType)) { | |
1197 | $contactType = 'All'; | |
1198 | } | |
1199 | ||
1200 | $cacheKeyString = "importableFields $contactType"; | |
1201 | $cacheKeyString .= $status ? '_1' : '_0'; | |
1202 | $cacheKeyString .= $showAll ? '_1' : '_0'; | |
1203 | $cacheKeyString .= $isProfile ? '_1' : '_0'; | |
1204 | $cacheKeyString .= $checkPermission ? '_1' : '_0'; | |
1205 | ||
1206 | $fields = CRM_Utils_Array::value($cacheKeyString, self::$_importableFields); | |
1207 | ||
1208 | if (!$fields) { | |
1209 | // check if we can retrieve from database cache | |
1210 | $fields = CRM_Core_BAO_Cache::getItem('contact fields', $cacheKeyString); | |
1211 | } | |
1212 | ||
1213 | if (!$fields) { | |
1214 | $fields = CRM_Contact_DAO_Contact::import(); | |
1215 | ||
1216 | // get the fields thar are meant for contact types | |
1217 | if (in_array($contactType, array( | |
9bb77b8e TO |
1218 | 'Individual', |
1219 | 'Household', | |
1220 | 'Organization', | |
21dfd5f5 | 1221 | 'All', |
9bb77b8e | 1222 | ))) { |
6a488035 TO |
1223 | $fields = array_merge($fields, CRM_Core_OptionValue::getFields('', $contactType)); |
1224 | } | |
1225 | ||
1226 | $locationFields = array_merge(CRM_Core_DAO_Address::import(), | |
1227 | CRM_Core_DAO_Phone::import(), | |
1228 | CRM_Core_DAO_Email::import(), | |
1229 | CRM_Core_DAO_IM::import(TRUE), | |
1230 | CRM_Core_DAO_OpenID::import() | |
1231 | ); | |
1232 | ||
1233 | $locationFields = array_merge($locationFields, | |
1234 | CRM_Core_BAO_CustomField::getFieldsForImport('Address', | |
1235 | FALSE, | |
1236 | FALSE, | |
1237 | FALSE, | |
1238 | FALSE | |
1239 | ) | |
1240 | ); | |
1241 | ||
1242 | foreach ($locationFields as $key => $field) { | |
1243 | $locationFields[$key]['hasLocationType'] = TRUE; | |
1244 | } | |
1245 | ||
1246 | $fields = array_merge($fields, $locationFields); | |
1247 | ||
1071730c DL |
1248 | $fields = array_merge($fields, CRM_Contact_DAO_Contact::import()); |
1249 | $fields = array_merge($fields, CRM_Core_DAO_Note::import()); | |
6a488035 TO |
1250 | |
1251 | //website fields | |
1252 | $fields = array_merge($fields, CRM_Core_DAO_Website::import()); | |
887e764d | 1253 | $fields['url']['hasWebsiteType'] = TRUE; |
6a488035 TO |
1254 | |
1255 | if ($contactType != 'All') { | |
1256 | $fields = array_merge($fields, | |
1257 | CRM_Core_BAO_CustomField::getFieldsForImport($contactType, | |
1258 | $showAll, | |
1259 | TRUE, | |
1260 | FALSE, | |
1261 | FALSE, | |
1262 | $withMultiCustomFields | |
1263 | ) | |
1264 | ); | |
1265 | //unset the fields, which are not related to their | |
1266 | //contact type. | |
1267 | $commonValues = array( | |
8c77c68b KJ |
1268 | 'Individual' => array( |
1269 | 'household_name', | |
1270 | 'legal_name', | |
1271 | 'sic_code', | |
21dfd5f5 | 1272 | 'organization_name', |
8c77c68b | 1273 | ), |
6a488035 | 1274 | 'Household' => array( |
8c77c68b KJ |
1275 | 'first_name', |
1276 | 'middle_name', | |
1277 | 'last_name', | |
e171748b | 1278 | 'formal_title', |
8c77c68b | 1279 | 'job_title', |
04ffef8d CW |
1280 | 'gender_id', |
1281 | 'prefix_id', | |
1282 | 'suffix_id', | |
8c77c68b KJ |
1283 | 'birth_date', |
1284 | 'organization_name', | |
1285 | 'legal_name', | |
1286 | 'legal_identifier', | |
1287 | 'sic_code', | |
1288 | 'home_URL', | |
1289 | 'is_deceased', | |
6a488035 TO |
1290 | 'deceased_date', |
1291 | ), | |
1292 | 'Organization' => array( | |
8c77c68b KJ |
1293 | 'first_name', |
1294 | 'middle_name', | |
1295 | 'last_name', | |
e171748b | 1296 | 'formal_title', |
8c77c68b | 1297 | 'job_title', |
04ffef8d CW |
1298 | 'gender_id', |
1299 | 'prefix_id', | |
1300 | 'suffix_id', | |
8c77c68b KJ |
1301 | 'birth_date', |
1302 | 'household_name', | |
1303 | 'is_deceased', | |
1304 | 'deceased_date', | |
6a488035 TO |
1305 | ), |
1306 | ); | |
1307 | foreach ($commonValues[$contactType] as $value) { | |
1308 | unset($fields[$value]); | |
1309 | } | |
1310 | } | |
1311 | else { | |
9bb77b8e | 1312 | foreach (array('Individual', 'Household', 'Organization') as $type) { |
6a488035 TO |
1313 | $fields = array_merge($fields, |
1314 | CRM_Core_BAO_CustomField::getFieldsForImport($type, | |
1315 | $showAll, | |
1316 | FALSE, | |
1317 | FALSE, | |
1318 | FALSE, | |
1319 | $withMultiCustomFields | |
1320 | ) | |
1321 | ); | |
1322 | } | |
1323 | } | |
1324 | ||
1325 | if ($isProfile) { | |
8c77c68b KJ |
1326 | $fields = array_merge($fields, array( |
1327 | 'group' => array( | |
1328 | 'title' => ts('Group(s)'), | |
1329 | 'name' => 'group', | |
1330 | ), | |
1331 | 'tag' => array( | |
1332 | 'title' => ts('Tag(s)'), | |
1333 | 'name' => 'tag', | |
1334 | ), | |
1335 | 'note' => array( | |
1336 | 'title' => ts('Note(s)'), | |
1337 | 'name' => 'note', | |
1338 | ), | |
d4dcd180 | 1339 | 'communication_style_id' => array( |
1340 | 'title' => ts('Communication Style'), | |
1341 | 'name' => 'communication_style_id', | |
1342 | ), | |
8c77c68b | 1343 | )); |
6a488035 TO |
1344 | } |
1345 | ||
1346 | //Sorting fields in alphabetical order(CRM-1507) | |
1347 | $fields = CRM_Utils_Array::crmArraySortByField($fields, 'title'); | |
1348 | ||
1349 | CRM_Core_BAO_Cache::setItem($fields, 'contact fields', $cacheKeyString); | |
1350 | } | |
1351 | ||
1352 | self::$_importableFields[$cacheKeyString] = $fields; | |
1353 | ||
1354 | if (!$isProfile) { | |
1355 | if (!$status) { | |
1356 | $fields = array_merge(array('do_not_import' => array('title' => ts('- do not import -'))), | |
1357 | self::$_importableFields[$cacheKeyString] | |
1358 | ); | |
1359 | } | |
1360 | else { | |
1361 | $fields = array_merge(array('' => array('title' => ts('- Contact Fields -'))), | |
1362 | self::$_importableFields[$cacheKeyString] | |
1363 | ); | |
1364 | } | |
1365 | } | |
1366 | return $fields; | |
1367 | } | |
1368 | ||
1369 | /** | |
0965e988 | 1370 | * Combine all the exportable fields from the lower levels object. |
6a488035 | 1371 | * |
0965e988 | 1372 | * Currently we are using importable fields as exportable fields |
6a488035 | 1373 | * |
8232cf2d | 1374 | * @param int|string $contactType contact Type |
77c5b619 TO |
1375 | * @param bool $status |
1376 | * True while exporting primary contacts. | |
1377 | * @param bool $export | |
1378 | * True when used during export. | |
1379 | * @param bool $search | |
1380 | * True when used during search, might conflict with export param?. | |
6a488035 | 1381 | * |
8232cf2d E |
1382 | * @param bool $withMultiRecord |
1383 | * | |
a6c01b45 CW |
1384 | * @return array |
1385 | * array of exportable Fields | |
6a488035 | 1386 | */ |
5837835b | 1387 | public static function &exportableFields($contactType = 'Individual', $status = FALSE, $export = FALSE, $search = FALSE, $withMultiRecord = FALSE, $checkPermissions = TRUE) { |
6a488035 TO |
1388 | if (empty($contactType)) { |
1389 | $contactType = 'All'; | |
1390 | } | |
1391 | ||
1392 | $cacheKeyString = "exportableFields $contactType"; | |
1393 | $cacheKeyString .= $export ? '_1' : '_0'; | |
1394 | $cacheKeyString .= $status ? '_1' : '_0'; | |
1395 | $cacheKeyString .= $search ? '_1' : '_0'; | |
bb341097 EM |
1396 | //CRM-14501 it turns out that the impact of permissioning here is sometimes inconsistent. The field that |
1397 | //calculates custom fields takes into account the logged in user & caches that for all users | |
1398 | //as an interim fix we will cache the fields by contact | |
1399 | $cacheKeyString .= '_' . CRM_Core_Session::getLoggedInContactID(); | |
6a488035 TO |
1400 | |
1401 | if (!self::$_exportableFields || !CRM_Utils_Array::value($cacheKeyString, self::$_exportableFields)) { | |
1402 | if (!self::$_exportableFields) { | |
1403 | self::$_exportableFields = array(); | |
1404 | } | |
1405 | ||
1406 | // check if we can retrieve from database cache | |
1407 | $fields = CRM_Core_BAO_Cache::getItem('contact fields', $cacheKeyString); | |
1408 | ||
1409 | if (!$fields) { | |
6a488035 TO |
1410 | $fields = CRM_Contact_DAO_Contact::export(); |
1411 | ||
5c9ff055 EM |
1412 | // The fields are meant for contact types. |
1413 | if (in_array($contactType, array( | |
1414 | 'Individual', | |
1415 | 'Household', | |
1416 | 'Organization', | |
1417 | 'All', | |
1418 | ) | |
1419 | )) { | |
6a488035 TO |
1420 | $fields = array_merge($fields, CRM_Core_OptionValue::getFields('', $contactType)); |
1421 | } | |
1422 | // add current employer for individuals | |
1423 | $fields = array_merge($fields, array( | |
1424 | 'current_employer' => | |
1425 | array( | |
1426 | 'name' => 'organization_name', | |
1427 | 'title' => ts('Current Employer'), | |
1428 | ), | |
9bb77b8e | 1429 | )); |
6a488035 TO |
1430 | |
1431 | $locationType = array( | |
8c77c68b KJ |
1432 | 'location_type' => array( |
1433 | 'name' => 'location_type', | |
6a488035 TO |
1434 | 'where' => 'civicrm_location_type.name', |
1435 | 'title' => ts('Location Type'), | |
21dfd5f5 | 1436 | ), |
9bb77b8e | 1437 | ); |
6a488035 TO |
1438 | |
1439 | $IMProvider = array( | |
8c77c68b KJ |
1440 | 'im_provider' => array( |
1441 | 'name' => 'im_provider', | |
6a488035 TO |
1442 | 'where' => 'civicrm_im.provider_id', |
1443 | 'title' => ts('IM Provider'), | |
21dfd5f5 | 1444 | ), |
9bb77b8e | 1445 | ); |
6a488035 TO |
1446 | |
1447 | $locationFields = array_merge($locationType, | |
1448 | CRM_Core_DAO_Address::export(), | |
1449 | CRM_Core_DAO_Phone::export(), | |
1450 | CRM_Core_DAO_Email::export(), | |
1451 | $IMProvider, | |
1452 | CRM_Core_DAO_IM::export(TRUE), | |
1453 | CRM_Core_DAO_OpenID::export() | |
1454 | ); | |
1455 | ||
1456 | $locationFields = array_merge($locationFields, | |
1457 | CRM_Core_BAO_CustomField::getFieldsForImport('Address') | |
1458 | ); | |
1459 | ||
1460 | foreach ($locationFields as $key => $field) { | |
1461 | $locationFields[$key]['hasLocationType'] = TRUE; | |
1462 | } | |
1463 | ||
1464 | $fields = array_merge($fields, $locationFields); | |
1465 | ||
1466 | //add world region | |
1467 | $fields = array_merge($fields, | |
1468 | CRM_Core_DAO_Worldregion::export() | |
1469 | ); | |
1470 | ||
6a488035 TO |
1471 | $fields = array_merge($fields, |
1472 | CRM_Contact_DAO_Contact::export() | |
1473 | ); | |
1474 | ||
1475 | //website fields | |
1476 | $fields = array_merge($fields, CRM_Core_DAO_Website::export()); | |
1477 | ||
1478 | if ($contactType != 'All') { | |
1479 | $fields = array_merge($fields, | |
5837835b | 1480 | CRM_Core_BAO_CustomField::getFieldsForImport($contactType, $status, FALSE, $search, $checkPermissions, $withMultiRecord) |
6a488035 TO |
1481 | ); |
1482 | } | |
1483 | else { | |
1484 | foreach (array( | |
9bb77b8e TO |
1485 | 'Individual', |
1486 | 'Household', | |
21dfd5f5 | 1487 | 'Organization', |
9bb77b8e | 1488 | ) as $type) { |
6a488035 | 1489 | $fields = array_merge($fields, |
5837835b | 1490 | CRM_Core_BAO_CustomField::getFieldsForImport($type, FALSE, FALSE, $search, $checkPermissions, $withMultiRecord) |
6a488035 TO |
1491 | ); |
1492 | } | |
1493 | } | |
48ef7bf8 | 1494 | $fields['current_employer_id']['title'] = ts('Current Employer ID'); |
6a488035 TO |
1495 | //fix for CRM-791 |
1496 | if ($export) { | |
8c77c68b KJ |
1497 | $fields = array_merge($fields, array( |
1498 | 'groups' => array( | |
1499 | 'title' => ts('Group(s)'), | |
1500 | 'name' => 'groups', | |
1501 | ), | |
1502 | 'tags' => array( | |
1503 | 'title' => ts('Tag(s)'), | |
1504 | 'name' => 'tags', | |
1505 | ), | |
1506 | 'notes' => array( | |
1507 | 'title' => ts('Note(s)'), | |
1508 | 'name' => 'notes', | |
1509 | ), | |
1510 | )); | |
6a488035 TO |
1511 | } |
1512 | else { | |
8c77c68b KJ |
1513 | $fields = array_merge($fields, array( |
1514 | 'group' => array( | |
1515 | 'title' => ts('Group(s)'), | |
1516 | 'name' => 'group', | |
1517 | ), | |
1518 | 'tag' => array( | |
1519 | 'title' => ts('Tag(s)'), | |
1520 | 'name' => 'tag', | |
1521 | ), | |
1522 | 'note' => array( | |
1523 | 'title' => ts('Note(s)'), | |
1524 | 'name' => 'note', | |
1525 | ), | |
1526 | )); | |
6a488035 TO |
1527 | } |
1528 | ||
1529 | //Sorting fields in alphabetical order(CRM-1507) | |
1530 | foreach ($fields as $k => $v) { | |
1531 | $sortArray[$k] = CRM_Utils_Array::value('title', $v); | |
1532 | } | |
1533 | ||
1534 | $fields = array_merge($sortArray, $fields); | |
1535 | //unset the field which are not related to their contact type. | |
1536 | if ($contactType != 'All') { | |
1537 | $commonValues = array( | |
8c77c68b KJ |
1538 | 'Individual' => array( |
1539 | 'household_name', | |
1540 | 'legal_name', | |
1541 | 'sic_code', | |
1542 | 'organization_name', | |
1543 | 'email_greeting_custom', | |
1544 | 'postal_greeting_custom', | |
6a488035 TO |
1545 | 'addressee_custom', |
1546 | ), | |
1547 | 'Household' => array( | |
8c77c68b KJ |
1548 | 'first_name', |
1549 | 'middle_name', | |
1550 | 'last_name', | |
e171748b | 1551 | 'formal_title', |
8c77c68b | 1552 | 'job_title', |
04ffef8d CW |
1553 | 'gender_id', |
1554 | 'prefix_id', | |
1555 | 'suffix_id', | |
8c77c68b KJ |
1556 | 'birth_date', |
1557 | 'organization_name', | |
1558 | 'legal_name', | |
1559 | 'legal_identifier', | |
1560 | 'sic_code', | |
1561 | 'home_URL', | |
1562 | 'is_deceased', | |
1563 | 'deceased_date', | |
1564 | 'current_employer', | |
1565 | 'email_greeting_custom', | |
1566 | 'postal_greeting_custom', | |
1567 | 'addressee_custom', | |
1568 | 'prefix_id', | |
21dfd5f5 | 1569 | 'suffix_id', |
6a488035 TO |
1570 | ), |
1571 | 'Organization' => array( | |
8c77c68b KJ |
1572 | 'first_name', |
1573 | 'middle_name', | |
1574 | 'last_name', | |
e171748b | 1575 | 'formal_title', |
8c77c68b | 1576 | 'job_title', |
04ffef8d CW |
1577 | 'gender_id', |
1578 | 'prefix_id', | |
1579 | 'suffix_id', | |
8c77c68b KJ |
1580 | 'birth_date', |
1581 | 'household_name', | |
6a488035 | 1582 | 'email_greeting_custom', |
8c77c68b KJ |
1583 | 'postal_greeting_custom', |
1584 | 'prefix_id', | |
1585 | 'suffix_id', | |
1586 | 'gender_id', | |
1587 | 'addressee_custom', | |
1588 | 'is_deceased', | |
1589 | 'deceased_date', | |
1590 | 'current_employer', | |
6a488035 TO |
1591 | ), |
1592 | ); | |
1593 | foreach ($commonValues[$contactType] as $value) { | |
1594 | unset($fields[$value]); | |
1595 | } | |
1596 | } | |
1597 | ||
1598 | CRM_Core_BAO_Cache::setItem($fields, 'contact fields', $cacheKeyString); | |
1599 | } | |
1600 | self::$_exportableFields[$cacheKeyString] = $fields; | |
1601 | } | |
1602 | ||
1603 | if (!$status) { | |
1604 | $fields = self::$_exportableFields[$cacheKeyString]; | |
1605 | } | |
1606 | else { | |
1607 | $fields = array_merge(array('' => array('title' => ts('- Contact Fields -'))), | |
1608 | self::$_exportableFields[$cacheKeyString] | |
1609 | ); | |
1610 | } | |
1611 | ||
1612 | return $fields; | |
1613 | } | |
1614 | ||
1615 | /** | |
0965e988 | 1616 | * Get the all contact details (Hierarchical). |
6a488035 | 1617 | * |
77c5b619 TO |
1618 | * @param int $contactId |
1619 | * Contact id. | |
1620 | * @param array $fields | |
1621 | * Fields array. | |
6a488035 | 1622 | * |
0965e988 EM |
1623 | * @return array |
1624 | * Contact details | |
6a488035 | 1625 | */ |
00be9182 | 1626 | public static function getHierContactDetails($contactId, &$fields) { |
6a488035 TO |
1627 | $params = array(array('contact_id', '=', $contactId, 0, 0)); |
1628 | $options = array(); | |
1629 | ||
1630 | $returnProperties = self::makeHierReturnProperties($fields, $contactId); | |
1631 | ||
5c9ff055 EM |
1632 | // We don't know the contents of return properties, but we need the lower |
1633 | // level ids of the contact so add a few fields. | |
6c552737 TO |
1634 | $returnProperties['first_name'] = 1; |
1635 | $returnProperties['organization_name'] = 1; | |
1636 | $returnProperties['household_name'] = 1; | |
1637 | $returnProperties['contact_type'] = 1; | |
9bb77b8e | 1638 | $returnProperties['contact_sub_type'] = 1; |
6a488035 TO |
1639 | return list($query, $options) = CRM_Contact_BAO_Query::apiQuery($params, $returnProperties, $options); |
1640 | } | |
1641 | ||
1642 | /** | |
db7de9c1 EM |
1643 | * Given a set of flat profile style field names, create a hierarchy. |
1644 | * | |
1645 | * This is for the query to use, create the right sql. | |
6a488035 | 1646 | * |
8232cf2d | 1647 | * @param $fields |
77c5b619 TO |
1648 | * @param int $contactId |
1649 | * Contact id. | |
6a488035 | 1650 | * |
a6c01b45 | 1651 | * @return array |
db7de9c1 | 1652 | * A hierarchical property tree if appropriate |
6a488035 | 1653 | */ |
00be9182 | 1654 | public static function &makeHierReturnProperties($fields, $contactId = NULL) { |
b2b0530a | 1655 | $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'); |
6a488035 TO |
1656 | |
1657 | $returnProperties = array(); | |
1658 | ||
9bb77b8e | 1659 | $multipleFields = array('website' => 'url'); |
6a488035 TO |
1660 | foreach ($fields as $name => $dontCare) { |
1661 | if (strpos($name, '-') !== FALSE) { | |
1662 | list($fieldName, $id, $type) = CRM_Utils_System::explode('-', $name, 3); | |
1663 | ||
1664 | if (!in_array($fieldName, $multipleFields)) { | |
1665 | if ($id == 'Primary') { | |
1666 | $locationTypeName = 1; | |
1667 | } | |
1668 | else { | |
1669 | $locationTypeName = CRM_Utils_Array::value($id, $locationTypes); | |
1670 | if (!$locationTypeName) { | |
1671 | continue; | |
1672 | } | |
1673 | } | |
1674 | ||
a7488080 | 1675 | if (empty($returnProperties['location'])) { |
6a488035 TO |
1676 | $returnProperties['location'] = array(); |
1677 | } | |
a7488080 | 1678 | if (empty($returnProperties['location'][$locationTypeName])) { |
6a488035 TO |
1679 | $returnProperties['location'][$locationTypeName] = array(); |
1680 | $returnProperties['location'][$locationTypeName]['location_type'] = $id; | |
1681 | } | |
1682 | if (in_array($fieldName, array( | |
9bb77b8e TO |
1683 | 'phone', |
1684 | 'im', | |
1685 | 'email', | |
1686 | 'openid', | |
21dfd5f5 | 1687 | 'phone_ext', |
9bb77b8e | 1688 | ))) { |
6a488035 TO |
1689 | if ($type) { |
1690 | $returnProperties['location'][$locationTypeName][$fieldName . '-' . $type] = 1; | |
1691 | } | |
1692 | else { | |
1693 | $returnProperties['location'][$locationTypeName][$fieldName] = 1; | |
1694 | } | |
1695 | } | |
1696 | elseif (substr($fieldName, 0, 14) === 'address_custom') { | |
1697 | $returnProperties['location'][$locationTypeName][substr($fieldName, 8)] = 1; | |
1698 | } | |
1699 | else { | |
1700 | $returnProperties['location'][$locationTypeName][$fieldName] = 1; | |
1701 | } | |
1702 | } | |
1703 | else { | |
1704 | $returnProperties['website'][$id][$fieldName] = 1; | |
1705 | } | |
1706 | } | |
1707 | else { | |
1708 | $returnProperties[$name] = 1; | |
1709 | } | |
1710 | } | |
1711 | ||
1712 | return $returnProperties; | |
1713 | } | |
1714 | ||
1715 | /** | |
57507ae6 | 1716 | * Return the primary location type of a contact. |
6a488035 TO |
1717 | * |
1718 | * $params int $contactId contact_id | |
1719 | * $params boolean $isPrimaryExist if true, return primary contact location type otherwise null | |
1720 | * $params boolean $skipDefaultPriamry if true, return primary contact location type otherwise null | |
1721 | * | |
100fef9d | 1722 | * @param int $contactId |
8232cf2d E |
1723 | * @param bool $skipDefaultPriamry |
1724 | * @param null $block | |
1725 | * | |
a6c01b45 CW |
1726 | * @return int |
1727 | * $locationType location_type_id | |
6a488035 | 1728 | */ |
00be9182 | 1729 | public static function getPrimaryLocationType($contactId, $skipDefaultPriamry = FALSE, $block = NULL) { |
9bb77b8e | 1730 | if ($block) { |
6a488035 | 1731 | $entityBlock = array('contact_id' => $contactId); |
9bb77b8e TO |
1732 | $blocks = CRM_Core_BAO_Location::getValues($entityBlock); |
1733 | foreach ($blocks[$block] as $key => $value) { | |
1734 | if (!empty($value['is_primary'])) { | |
1735 | $locationType = CRM_Utils_Array::value('location_type_id', $value); | |
6a488035 TO |
1736 | } |
1737 | } | |
1738 | } | |
1739 | else { | |
1740 | $query = " | |
1741 | SELECT | |
1742 | IF ( civicrm_email.location_type_id IS NULL, | |
1743 | IF ( civicrm_address.location_type_id IS NULL, | |
1744 | IF ( civicrm_phone.location_type_id IS NULL, | |
1745 | IF ( civicrm_im.location_type_id IS NULL, | |
1746 | IF ( civicrm_openid.location_type_id IS NULL, null, civicrm_openid.location_type_id) | |
1747 | ,civicrm_im.location_type_id) | |
1748 | ,civicrm_phone.location_type_id) | |
1749 | ,civicrm_address.location_type_id) | |
1750 | ,civicrm_email.location_type_id) as locationType | |
1751 | FROM civicrm_contact | |
1752 | LEFT JOIN civicrm_email ON ( civicrm_email.is_primary = 1 AND civicrm_email.contact_id = civicrm_contact.id ) | |
1753 | LEFT JOIN civicrm_address ON ( civicrm_address.is_primary = 1 AND civicrm_address.contact_id = civicrm_contact.id) | |
1754 | LEFT JOIN civicrm_phone ON ( civicrm_phone.is_primary = 1 AND civicrm_phone.contact_id = civicrm_contact.id) | |
1755 | LEFT JOIN civicrm_im ON ( civicrm_im.is_primary = 1 AND civicrm_im.contact_id = civicrm_contact.id) | |
1756 | LEFT JOIN civicrm_openid ON ( civicrm_openid.is_primary = 1 AND civicrm_openid.contact_id = civicrm_contact.id) | |
1757 | WHERE civicrm_contact.id = %1 "; | |
1758 | ||
1759 | $params = array(1 => array($contactId, 'Integer')); | |
1760 | ||
1761 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
1762 | ||
1763 | $locationType = NULL; | |
1764 | if ($dao->fetch()) { | |
1765 | $locationType = $dao->locationType; | |
1766 | } | |
1767 | } | |
1768 | if (isset($locationType)) { | |
1769 | return $locationType; | |
1770 | } | |
1771 | elseif ($skipDefaultPriamry) { | |
1772 | // if there is no primary contact location then return null | |
1773 | return NULL; | |
1774 | } | |
1775 | else { | |
1776 | // if there is no primart contact location, then return default | |
1777 | // location type of the system | |
1778 | $defaultLocationType = CRM_Core_BAO_LocationType::getDefault(); | |
1779 | return $defaultLocationType->id; | |
1780 | } | |
1781 | } | |
1782 | ||
1783 | /** | |
57507ae6 | 1784 | * Get the display name, primary email and location type of a contact. |
6a488035 | 1785 | * |
77c5b619 TO |
1786 | * @param int $id |
1787 | * Id of the contact. | |
6a488035 | 1788 | * |
a6c01b45 | 1789 | * @return array |
16b10e64 | 1790 | * Array of display_name, email if found, do_not_email or (null,null,null) |
6a488035 | 1791 | */ |
00be9182 | 1792 | public static function getContactDetails($id) { |
6a488035 TO |
1793 | // check if the contact type |
1794 | $contactType = self::getContactType($id); | |
1795 | ||
1796 | $nameFields = ($contactType == 'Individual') ? "civicrm_contact.first_name, civicrm_contact.last_name, civicrm_contact.display_name" : "civicrm_contact.display_name"; | |
1797 | ||
1798 | $sql = " | |
1799 | SELECT $nameFields, civicrm_email.email, civicrm_contact.do_not_email, civicrm_email.on_hold, civicrm_contact.is_deceased | |
1800 | FROM civicrm_contact LEFT JOIN civicrm_email ON (civicrm_contact.id = civicrm_email.contact_id) | |
1801 | WHERE civicrm_contact.id = %1 | |
1802 | ORDER BY civicrm_email.is_primary DESC"; | |
1803 | $params = array(1 => array($id, 'Integer')); | |
1804 | $dao = CRM_Core_DAO::executeQuery($sql, $params); | |
1805 | ||
1806 | if ($dao->fetch()) { | |
1807 | if ($contactType == 'Individual') { | |
1808 | if ($dao->first_name || $dao->last_name) { | |
1809 | $name = "{$dao->first_name} {$dao->last_name}"; | |
1810 | } | |
1811 | else { | |
1812 | $name = $dao->display_name; | |
1813 | } | |
1814 | } | |
1815 | else { | |
1816 | $name = $dao->display_name; | |
1817 | } | |
9bb77b8e | 1818 | $email = $dao->email; |
6a488035 | 1819 | $doNotEmail = $dao->do_not_email ? TRUE : FALSE; |
9bb77b8e | 1820 | $onHold = $dao->on_hold ? TRUE : FALSE; |
6a488035 TO |
1821 | $isDeceased = $dao->is_deceased ? TRUE : FALSE; |
1822 | return array($name, $email, $doNotEmail, $onHold, $isDeceased); | |
1823 | } | |
1824 | return array(NULL, NULL, NULL, NULL, NULL); | |
1825 | } | |
1826 | ||
1827 | /** | |
100fef9d | 1828 | * Add/edit/register contacts through profile. |
6a488035 | 1829 | * |
77c5b619 TO |
1830 | * @param array $params |
1831 | * Array of profile fields to be edited/added. | |
1832 | * @param array $fields | |
1833 | * Array of fields from UFGroup. | |
1834 | * @param int $contactID | |
1835 | * Id of the contact to be edited/added. | |
1836 | * @param int $addToGroupID | |
1837 | * Specifies the default group to which contact is added. | |
1838 | * @param int $ufGroupId | |
1839 | * Uf group id (profile id). | |
57507ae6 | 1840 | * @param string $ctype |
77c5b619 TO |
1841 | * @param bool $visibility |
1842 | * Basically lets us know where this request is coming from. | |
6a488035 TO |
1843 | * if via a profile from web, we restrict what groups are changed |
1844 | * | |
a6c01b45 CW |
1845 | * @return int |
1846 | * contact id created/edited | |
6a488035 | 1847 | */ |
57507ae6 | 1848 | public static function createProfileContact( |
6a488035 TO |
1849 | &$params, |
1850 | &$fields, | |
1851 | $contactID = NULL, | |
1852 | $addToGroupID = NULL, | |
1853 | $ufGroupId = NULL, | |
9bb77b8e TO |
1854 | $ctype = NULL, |
1855 | $visibility = FALSE | |
6a488035 TO |
1856 | ) { |
1857 | // add ufGroupID to params array ( CRM-2012 ) | |
1858 | if ($ufGroupId) { | |
1859 | $params['uf_group_id'] = $ufGroupId; | |
1860 | } | |
870156d6 | 1861 | self::addBillingNameFieldsIfOtherwiseNotSet($params); |
6a488035 | 1862 | |
85c882c5 | 1863 | // If a user has logged in, or accessed via a checksum |
1864 | // Then deliberately 'blanking' a value in the profile should remove it from their record | |
1865 | $session = CRM_Core_Session::singleton(); | |
1866 | $params['updateBlankLocInfo'] = TRUE; | |
1867 | if (($session->get('authSrc') & (CRM_Core_Permission::AUTH_SRC_CHECKSUM + CRM_Core_Permission::AUTH_SRC_LOGIN)) == 0) { | |
1868 | $params['updateBlankLocInfo'] = FALSE; | |
1869 | } | |
1870 | ||
6a488035 TO |
1871 | if ($contactID) { |
1872 | $editHook = TRUE; | |
1873 | CRM_Utils_Hook::pre('edit', 'Profile', $contactID, $params); | |
1874 | } | |
1875 | else { | |
1876 | $editHook = FALSE; | |
1877 | CRM_Utils_Hook::pre('create', 'Profile', NULL, $params); | |
1878 | } | |
1879 | ||
1880 | list($data, $contactDetails) = self::formatProfileContactParams($params, $fields, $contactID, $ufGroupId, $ctype); | |
1881 | ||
1882 | // manage is_opt_out | |
1883 | if (array_key_exists('is_opt_out', $fields) && array_key_exists('is_opt_out', $params)) { | |
9bb77b8e TO |
1884 | $wasOptOut = CRM_Utils_Array::value('is_opt_out', $contactDetails, FALSE); |
1885 | $isOptOut = CRM_Utils_Array::value('is_opt_out', $params, FALSE); | |
6a488035 TO |
1886 | $data['is_opt_out'] = $isOptOut; |
1887 | // on change, create new civicrm_subscription_history entry | |
8cc574cf | 1888 | if (($wasOptOut != $isOptOut) && !empty($contactDetails['contact_id'])) { |
6a488035 TO |
1889 | $shParams = array( |
1890 | 'contact_id' => $contactDetails['contact_id'], | |
1891 | 'status' => $isOptOut ? 'Removed' : 'Added', | |
1892 | 'method' => 'Web', | |
1893 | ); | |
1894 | CRM_Contact_BAO_SubscriptionHistory::create($shParams); | |
1895 | } | |
1896 | } | |
1897 | ||
1898 | $contact = self::create($data); | |
1899 | ||
1900 | // contact is null if the profile does not have any contact fields | |
1901 | if ($contact) { | |
1902 | $contactID = $contact->id; | |
1903 | } | |
1904 | ||
1905 | if (empty($contactID)) { | |
1906 | CRM_Core_Error::fatal('Cannot proceed without a valid contact id'); | |
1907 | } | |
1908 | ||
1909 | // Process group and tag | |
a7488080 | 1910 | if (!empty($fields['group'])) { |
6a488035 TO |
1911 | $method = 'Admin'; |
1912 | // this for sure means we are coming in via profile since i added it to fix | |
1913 | // removing contacts from user groups -- lobo | |
1914 | if ($visibility) { | |
1915 | $method = 'Web'; | |
1916 | } | |
1917 | CRM_Contact_BAO_GroupContact::create($params['group'], $contactID, $visibility, $method); | |
1918 | } | |
1919 | ||
a7488080 | 1920 | if (!empty($fields['tag'])) { |
6a488035 TO |
1921 | CRM_Core_BAO_EntityTag::create($params['tag'], 'civicrm_contact', $contactID); |
1922 | } | |
1923 | ||
1924 | //to add profile in default group | |
1925 | if (is_array($addToGroupID)) { | |
1926 | $contactIds = array($contactID); | |
1927 | foreach ($addToGroupID as $groupId) { | |
1928 | CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIds, $groupId); | |
1929 | } | |
1930 | } | |
1931 | elseif ($addToGroupID) { | |
1932 | $contactIds = array($contactID); | |
1933 | CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIds, $addToGroupID); | |
1934 | } | |
1935 | ||
2b68a50c | 1936 | CRM_Contact_BAO_GroupContactCache::opportunisticCacheFlush(); |
6a488035 TO |
1937 | |
1938 | if ($editHook) { | |
1939 | CRM_Utils_Hook::post('edit', 'Profile', $contactID, $params); | |
1940 | } | |
1941 | else { | |
1942 | CRM_Utils_Hook::post('create', 'Profile', $contactID, $params); | |
1943 | } | |
1944 | return $contactID; | |
1945 | } | |
1946 | ||
86538308 | 1947 | /** |
57507ae6 EM |
1948 | * Format profile contact parameters. |
1949 | * | |
c490a46a | 1950 | * @param array $params |
86538308 | 1951 | * @param $fields |
100fef9d CW |
1952 | * @param int $contactID |
1953 | * @param int $ufGroupId | |
86538308 EM |
1954 | * @param null $ctype |
1955 | * @param bool $skipCustom | |
1956 | * | |
1957 | * @return array | |
1958 | */ | |
57507ae6 | 1959 | public static function formatProfileContactParams( |
6a488035 TO |
1960 | &$params, |
1961 | &$fields, | |
1962 | $contactID = NULL, | |
1963 | $ufGroupId = NULL, | |
1964 | $ctype = NULL, | |
1965 | $skipCustom = FALSE | |
1966 | ) { | |
1967 | ||
1968 | $data = $contactDetails = array(); | |
1969 | ||
1970 | // get the contact details (hier) | |
1971 | if ($contactID) { | |
1972 | list($details, $options) = self::getHierContactDetails($contactID, $fields); | |
1973 | ||
1974 | $contactDetails = $details[$contactID]; | |
1975 | $data['contact_type'] = CRM_Utils_Array::value('contact_type', $contactDetails); | |
1976 | $data['contact_sub_type'] = CRM_Utils_Array::value('contact_sub_type', $contactDetails); | |
1977 | } | |
1978 | else { | |
1979 | //we should get contact type only if contact | |
1980 | if ($ufGroupId) { | |
1981 | $data['contact_type'] = CRM_Core_BAO_UFField::getProfileType($ufGroupId); | |
1982 | ||
1983 | //special case to handle profile with only contact fields | |
1984 | if ($data['contact_type'] == 'Contact') { | |
1985 | $data['contact_type'] = 'Individual'; | |
1986 | } | |
1987 | elseif (CRM_Contact_BAO_ContactType::isaSubType($data['contact_type'])) { | |
1988 | $data['contact_type'] = CRM_Contact_BAO_ContactType::getBasicType($data['contact_type']); | |
1989 | } | |
1990 | } | |
1991 | elseif ($ctype) { | |
1992 | $data['contact_type'] = $ctype; | |
1993 | } | |
1994 | else { | |
1995 | $data['contact_type'] = 'Individual'; | |
1996 | } | |
1997 | } | |
1998 | ||
1999 | //fix contact sub type CRM-5125 | |
2000 | if (array_key_exists('contact_sub_type', $params) && | |
2001 | !empty($params['contact_sub_type']) | |
2002 | ) { | |
f2181617 | 2003 | $data['contact_sub_type'] = CRM_Utils_Array::implodePadded($params['contact_sub_type']); |
6a488035 TO |
2004 | } |
2005 | elseif (array_key_exists('contact_sub_type_hidden', $params) && | |
2006 | !empty($params['contact_sub_type_hidden']) | |
2007 | ) { | |
2008 | // if profile was used, and had any subtype, we obtain it from there | |
aa7a7a14 | 2009 | //CRM-13596 - add to existing contact types, rather than overwriting |
f2181617 | 2010 | $data_contact_sub_type_arr = CRM_Utils_Array::explodePadded($data['contact_sub_type']); |
aa7a7a14 | 2011 | if (!in_array($params['contact_sub_type_hidden'], $data_contact_sub_type_arr)) { |
dbc839f9 | 2012 | //CRM-20517 - make sure contact_sub_type gets the correct delimiters |
2013 | $data['contact_sub_type'] = trim($data['contact_sub_type'], CRM_Core_DAO::VALUE_SEPARATOR); | |
2014 | $data['contact_sub_type'] = CRM_Core_DAO::VALUE_SEPARATOR . $data['contact_sub_type'] . CRM_Utils_Array::implodePadded($params['contact_sub_type_hidden']); | |
aa7a7a14 | 2015 | } |
6a488035 TO |
2016 | } |
2017 | ||
2018 | if ($ctype == 'Organization') { | |
2019 | $data['organization_name'] = CRM_Utils_Array::value('organization_name', $contactDetails); | |
2020 | } | |
2021 | elseif ($ctype == 'Household') { | |
2022 | $data['household_name'] = CRM_Utils_Array::value('household_name', $contactDetails); | |
2023 | } | |
2024 | ||
2025 | $locationType = array(); | |
2026 | $count = 1; | |
2027 | ||
2028 | if ($contactID) { | |
2029 | //add contact id | |
2030 | $data['contact_id'] = $contactID; | |
2031 | $primaryLocationType = self::getPrimaryLocationType($contactID); | |
2032 | } | |
2033 | else { | |
2034 | $defaultLocation = CRM_Core_BAO_LocationType::getDefault(); | |
2035 | $defaultLocationId = $defaultLocation->id; | |
2036 | } | |
2037 | ||
b576d770 | 2038 | $billingLocationTypeId = CRM_Core_BAO_LocationType::getBilling(); |
6a488035 TO |
2039 | |
2040 | $blocks = array('email', 'phone', 'im', 'openid'); | |
2041 | ||
2042 | $multiplFields = array('url'); | |
2043 | // prevent overwritten of formatted array, reset all block from | |
2044 | // params if it is not in valid format (since import pass valid format) | |
2045 | foreach ($blocks as $blk) { | |
2046 | if (array_key_exists($blk, $params) && | |
2047 | !is_array($params[$blk]) | |
2048 | ) { | |
2049 | unset($params[$blk]); | |
2050 | } | |
2051 | } | |
2052 | ||
2053 | $primaryPhoneLoc = NULL; | |
ece0bc24 | 2054 | $session = CRM_Core_Session::singleton(); |
6a488035 | 2055 | foreach ($params as $key => $value) { |
6a488035 TO |
2056 | list($fieldName, $locTypeId, $typeId) = CRM_Utils_System::explode('-', $key, 3); |
2057 | ||
6a488035 TO |
2058 | if ($locTypeId == 'Primary') { |
2059 | if ($contactID) { | |
9bb77b8e | 2060 | if (in_array($fieldName, $blocks)) { |
6a488035 TO |
2061 | $locTypeId = self::getPrimaryLocationType($contactID, FALSE, $fieldName); |
2062 | } | |
9bb77b8e | 2063 | else { |
6a488035 TO |
2064 | $locTypeId = self::getPrimaryLocationType($contactID, FALSE, 'address'); |
2065 | } | |
2066 | $primaryLocationType = $locTypeId; | |
2067 | } | |
2068 | else { | |
2069 | $locTypeId = $defaultLocationId; | |
2070 | } | |
2071 | } | |
2072 | ||
2073 | if (is_numeric($locTypeId) && | |
2074 | !in_array($fieldName, $multiplFields) && | |
2075 | substr($fieldName, 0, 7) != 'custom_' | |
2076 | ) { | |
2077 | $index = $locTypeId; | |
2078 | ||
2079 | if (is_numeric($typeId)) { | |
2080 | $index .= '-' . $typeId; | |
2081 | } | |
2082 | if (!in_array($index, $locationType)) { | |
2083 | $locationType[$count] = $index; | |
2084 | $count++; | |
2085 | } | |
2086 | ||
2087 | $loc = CRM_Utils_Array::key($index, $locationType); | |
2088 | ||
9bb77b8e | 2089 | $blockName = in_array($fieldName, $blocks) ? $fieldName : 'address'; |
6a488035 TO |
2090 | |
2091 | $data[$blockName][$loc]['location_type_id'] = $locTypeId; | |
2092 | ||
2093 | //set is_billing true, for location type "Billing" | |
2094 | if ($locTypeId == $billingLocationTypeId) { | |
2095 | $data[$blockName][$loc]['is_billing'] = 1; | |
2096 | } | |
2097 | ||
2098 | if ($contactID) { | |
2099 | //get the primary location type | |
2100 | if ($locTypeId == $primaryLocationType) { | |
2101 | $data[$blockName][$loc]['is_primary'] = 1; | |
2102 | } | |
2103 | } | |
2104 | elseif ($locTypeId == $defaultLocationId) { | |
2105 | $data[$blockName][$loc]['is_primary'] = 1; | |
2106 | } | |
2107 | ||
9bb77b8e | 2108 | if (in_array($fieldName, array('phone'))) { |
6a488035 TO |
2109 | if ($typeId) { |
2110 | $data['phone'][$loc]['phone_type_id'] = $typeId; | |
2111 | } | |
2112 | else { | |
2113 | $data['phone'][$loc]['phone_type_id'] = ''; | |
2114 | } | |
2115 | $data['phone'][$loc]['phone'] = $value; | |
2116 | ||
2117 | //special case to handle primary phone with different phone types | |
2118 | // in this case we make first phone type as primary | |
2119 | if (isset($data['phone'][$loc]['is_primary']) && !$primaryPhoneLoc) { | |
2120 | $primaryPhoneLoc = $loc; | |
2121 | } | |
2122 | ||
2123 | if ($loc != $primaryPhoneLoc) { | |
2124 | unset($data['phone'][$loc]['is_primary']); | |
2125 | } | |
2126 | } | |
2127 | elseif ($fieldName == 'phone_ext') { | |
2128 | $data['phone'][$loc]['phone_ext'] = $value; | |
2129 | } | |
2130 | elseif ($fieldName == 'email') { | |
2131 | $data['email'][$loc]['email'] = $value; | |
0c043d46 | 2132 | if (empty($contactID)) { |
2133 | $data['email'][$loc]['is_primary'] = 1; | |
2134 | } | |
6a488035 TO |
2135 | } |
2136 | elseif ($fieldName == 'im') { | |
2137 | if (isset($params[$key . '-provider_id'])) { | |
2138 | $data['im'][$loc]['provider_id'] = $params[$key . '-provider_id']; | |
2139 | } | |
2140 | if (strpos($key, '-provider_id') !== FALSE) { | |
2141 | $data['im'][$loc]['provider_id'] = $params[$key]; | |
2142 | } | |
2143 | else { | |
2144 | $data['im'][$loc]['name'] = $value; | |
2145 | } | |
2146 | } | |
2147 | elseif ($fieldName == 'openid') { | |
2148 | $data['openid'][$loc]['openid'] = $value; | |
2149 | } | |
2150 | else { | |
2151 | if ($fieldName === 'state_province') { | |
2152 | // CRM-3393 | |
2153 | if (is_numeric($value) && ((int ) $value) >= 1000) { | |
2154 | $data['address'][$loc]['state_province_id'] = $value; | |
2155 | } | |
2156 | elseif (empty($value)) { | |
2157 | $data['address'][$loc]['state_province_id'] = ''; | |
2158 | } | |
2159 | else { | |
2160 | $data['address'][$loc]['state_province'] = $value; | |
2161 | } | |
2162 | } | |
2163 | elseif ($fieldName === 'country') { | |
2164 | // CRM-3393 | |
2165 | if (is_numeric($value) && ((int ) $value) >= 1000 | |
2166 | ) { | |
2167 | $data['address'][$loc]['country_id'] = $value; | |
2168 | } | |
2169 | elseif (empty($value)) { | |
2170 | $data['address'][$loc]['country_id'] = ''; | |
2171 | } | |
2172 | else { | |
2173 | $data['address'][$loc]['country'] = $value; | |
2174 | } | |
2175 | } | |
2176 | elseif ($fieldName === 'county') { | |
2177 | $data['address'][$loc]['county_id'] = $value; | |
2178 | } | |
2179 | elseif ($fieldName == 'address_name') { | |
2180 | $data['address'][$loc]['name'] = $value; | |
2181 | } | |
2182 | elseif (substr($fieldName, 0, 14) === 'address_custom') { | |
2183 | $data['address'][$loc][substr($fieldName, 8)] = $value; | |
2184 | } | |
2185 | else { | |
2186 | $data['address'][$loc][$fieldName] = $value; | |
2187 | } | |
2188 | } | |
2189 | } | |
2190 | else { | |
2191 | if (substr($key, 0, 4) === 'url-') { | |
2192 | $websiteField = explode('-', $key); | |
887e764d PN |
2193 | $data['website'][$websiteField[1]]['website_type_id'] = $websiteField[1]; |
2194 | $data['website'][$websiteField[1]]['url'] = $value; | |
6a488035 | 2195 | } |
6a488035 | 2196 | elseif (in_array($key, self::$_greetingTypes, TRUE)) { |
41665e8f | 2197 | //save email/postal greeting and addressee values if any, CRM-4575 |
6a488035 TO |
2198 | $data[$key . '_id'] = $value; |
2199 | } | |
2200 | elseif (!$skipCustom && ($customFieldId = CRM_Core_BAO_CustomField::getKeyID($key))) { | |
2201 | // for autocomplete transfer hidden value instead of label | |
2202 | if ($params[$key] && isset($params[$key . '_id'])) { | |
2203 | $value = $params[$key . '_id']; | |
2204 | } | |
2205 | ||
2206 | // we need to append time with date | |
2207 | if ($params[$key] && isset($params[$key . '_time'])) { | |
2208 | $value .= ' ' . $params[$key . '_time']; | |
2209 | } | |
2210 | ||
ece0bc24 DS |
2211 | // if auth source is not checksum / login && $value is blank, do not proceed - CRM-10128 |
2212 | if (($session->get('authSrc') & (CRM_Core_Permission::AUTH_SRC_CHECKSUM + CRM_Core_Permission::AUTH_SRC_LOGIN)) == 0 && | |
9bb77b8e TO |
2213 | ($value == '' || !isset($value)) |
2214 | ) { | |
ece0bc24 | 2215 | continue; |
69739cb8 | 2216 | } |
ece0bc24 | 2217 | |
6a488035 | 2218 | $valueId = NULL; |
a7488080 | 2219 | if (!empty($params['customRecordValues'])) { |
6a488035 TO |
2220 | if (is_array($params['customRecordValues']) && !empty($params['customRecordValues'])) { |
2221 | foreach ($params['customRecordValues'] as $recId => $customFields) { | |
2222 | if (is_array($customFields) && !empty($customFields)) { | |
2223 | foreach ($customFields as $customFieldName) { | |
2224 | if ($customFieldName == $key) { | |
2225 | $valueId = $recId; | |
2226 | break; | |
2227 | } | |
2228 | } | |
2229 | } | |
2230 | } | |
2231 | } | |
2232 | } | |
2233 | ||
aa7a7a14 AW |
2234 | //CRM-13596 - check for contact_sub_type_hidden first |
2235 | if (array_key_exists('contact_sub_type_hidden', $params)) { | |
2236 | $type = $params['contact_sub_type_hidden']; | |
9c302451 | 2237 | } |
aa7a7a14 AW |
2238 | else { |
2239 | $type = $data['contact_type']; | |
2240 | if (!empty($data['contact_sub_type'])) { | |
77896892 | 2241 | $type = CRM_Utils_Array::explodePadded($data['contact_sub_type']); |
aa7a7a14 | 2242 | } |
6a488035 TO |
2243 | } |
2244 | ||
2245 | CRM_Core_BAO_CustomField::formatCustomField($customFieldId, | |
2246 | $data['custom'], | |
2247 | $value, | |
2248 | $type, | |
2249 | $valueId, | |
c8f7094b | 2250 | $contactID, |
2251 | FALSE, | |
2252 | FALSE | |
6a488035 TO |
2253 | ); |
2254 | } | |
2255 | elseif ($key == 'edit') { | |
2256 | continue; | |
2257 | } | |
2258 | else { | |
2259 | if ($key == 'location') { | |
2260 | foreach ($value as $locationTypeId => $field) { | |
2261 | foreach ($field as $block => $val) { | |
2262 | if ($block == 'address' && array_key_exists('address_name', $val)) { | |
2263 | $value[$locationTypeId][$block]['name'] = $value[$locationTypeId][$block]['address_name']; | |
2264 | } | |
2265 | } | |
2266 | } | |
2267 | } | |
9bb77b8e | 2268 | if ($key == 'phone' && isset($params['phone_ext'])) { |
6a488035 | 2269 | $data[$key] = $value; |
9bb77b8e TO |
2270 | foreach ($value as $cnt => $phoneBlock) { |
2271 | if ($params[$key][$cnt]['location_type_id'] == $params['phone_ext'][$cnt]['location_type_id']) { | |
6a488035 TO |
2272 | $data[$key][$cnt]['phone_ext'] = CRM_Utils_Array::retrieveValueRecursive($params['phone_ext'][$cnt], 'phone_ext'); |
2273 | } | |
2274 | } | |
2275 | } | |
4c9b6178 | 2276 | elseif (in_array($key, |
9bb77b8e TO |
2277 | array( |
2278 | 'nick_name', | |
69739cb8 EM |
2279 | 'job_title', |
2280 | 'middle_name', | |
2281 | 'birth_date', | |
4512fd44 | 2282 | 'gender_id', |
69739cb8 EM |
2283 | 'current_employer', |
2284 | 'prefix_id', | |
21dfd5f5 | 2285 | 'suffix_id', |
9bb77b8e | 2286 | )) && |
55ff2e3f | 2287 | ($value == '' || !isset($value)) && |
bc1e91c8 | 2288 | ($session->get('authSrc') & (CRM_Core_Permission::AUTH_SRC_CHECKSUM + CRM_Core_Permission::AUTH_SRC_LOGIN)) == 0 || |
2289 | ($key == 'current_employer' && empty($params['current_employer']))) { | |
69739cb8 | 2290 | // CRM-10128: if auth source is not checksum / login && $value is blank, do not fill $data with empty value |
55ff2e3f DS |
2291 | // to avoid update with empty values |
2292 | continue; | |
69739cb8 | 2293 | } |
6a488035 TO |
2294 | else { |
2295 | $data[$key] = $value; | |
2296 | } | |
2297 | } | |
2298 | } | |
2299 | } | |
2300 | ||
2301 | if (!isset($data['contact_type'])) { | |
2302 | $data['contact_type'] = 'Individual'; | |
2303 | } | |
2304 | ||
2305 | //set the values for checkboxes (do_not_email, do_not_mail, do_not_trade, do_not_phone) | |
2306 | $privacy = CRM_Core_SelectValues::privacy(); | |
2307 | foreach ($privacy as $key => $value) { | |
2308 | if (array_key_exists($key, $fields)) { | |
29748d00 | 2309 | // do not reset values for existing contacts, if fields are added to a profile |
6a488035 TO |
2310 | if (array_key_exists($key, $params)) { |
2311 | $data[$key] = $params[$key]; | |
29748d00 C |
2312 | if (empty($params[$key])) { |
2313 | $data[$key] = 0; | |
2314 | } | |
6a488035 TO |
2315 | } |
2316 | elseif (!$contactID) { | |
2317 | $data[$key] = 0; | |
2318 | } | |
2319 | } | |
2320 | } | |
2321 | ||
2322 | return array($data, $contactDetails); | |
2323 | } | |
2324 | ||
2325 | /** | |
57507ae6 EM |
2326 | * Find the get contact details. |
2327 | * | |
2328 | * This function does not respect ACLs for now, which might need to be rectified at some | |
2329 | * stage based on how its used. | |
6a488035 | 2330 | * |
77c5b619 TO |
2331 | * @param string $mail |
2332 | * Primary email address of the contact. | |
2333 | * @param string $ctype | |
2334 | * Contact type. | |
6a488035 | 2335 | * |
1273d77c | 2336 | * @return object|null |
a6c01b45 | 2337 | * $dao contact details |
6a488035 | 2338 | */ |
1273d77c | 2339 | public static function matchContactOnEmail($mail, $ctype = NULL) { |
6a488035 | 2340 | $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower'; |
9bb77b8e TO |
2341 | $mail = $strtolower(trim($mail)); |
2342 | $query = " | |
6a488035 TO |
2343 | SELECT civicrm_contact.id as contact_id, |
2344 | civicrm_contact.hash as hash, | |
2345 | civicrm_contact.contact_type as contact_type, | |
2346 | civicrm_contact.contact_sub_type as contact_sub_type | |
2347 | FROM civicrm_contact | |
2348 | INNER JOIN civicrm_email ON ( civicrm_contact.id = civicrm_email.contact_id )"; | |
2349 | ||
aaffa79f | 2350 | if (Civi::settings()->get('uniq_email_per_site')) { |
6a488035 TO |
2351 | // try to find a match within a site (multisite). |
2352 | $groups = CRM_Core_BAO_Domain::getChildGroupIds(); | |
2353 | if (!empty($groups)) { | |
2354 | $query .= " | |
2355 | INNER JOIN civicrm_group_contact gc ON | |
2356 | (civicrm_contact.id = gc.contact_id AND gc.status = 'Added' AND gc.group_id IN (" . implode(',', $groups) . "))"; | |
2357 | } | |
2358 | } | |
2359 | ||
2360 | $query .= " | |
2361 | WHERE civicrm_email.email = %1 AND civicrm_contact.is_deleted=0"; | |
2362 | $p = array(1 => array($mail, 'String')); | |
2363 | ||
2364 | if ($ctype) { | |
2365 | $query .= " AND civicrm_contact.contact_type = %3"; | |
2366 | $p[3] = array($ctype, 'String'); | |
2367 | } | |
2368 | ||
2369 | $query .= " ORDER BY civicrm_email.is_primary DESC"; | |
2370 | ||
2371 | $dao = CRM_Core_DAO::executeQuery($query, $p); | |
2372 | ||
2373 | if ($dao->fetch()) { | |
2374 | return $dao; | |
2375 | } | |
1273d77c | 2376 | return NULL; |
6a488035 TO |
2377 | } |
2378 | ||
2379 | /** | |
57507ae6 | 2380 | * Find the contact details associated with an OpenID. |
6a488035 | 2381 | * |
77c5b619 TO |
2382 | * @param string $openId |
2383 | * OpenId of the contact. | |
2384 | * @param string $ctype | |
2385 | * Contact type. | |
6a488035 | 2386 | * |
1273d77c | 2387 | * @return object|null |
a6c01b45 | 2388 | * $dao contact details |
6a488035 | 2389 | */ |
1273d77c | 2390 | public static function matchContactOnOpenId($openId, $ctype = NULL) { |
6a488035 | 2391 | $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower'; |
9bb77b8e TO |
2392 | $openId = $strtolower(trim($openId)); |
2393 | $query = " | |
6a488035 TO |
2394 | SELECT civicrm_contact.id as contact_id, |
2395 | civicrm_contact.hash as hash, | |
2396 | civicrm_contact.contact_type as contact_type, | |
2397 | civicrm_contact.contact_sub_type as contact_sub_type | |
2398 | FROM civicrm_contact | |
2399 | INNER JOIN civicrm_openid ON ( civicrm_contact.id = civicrm_openid.contact_id ) | |
2400 | WHERE civicrm_openid.openid = %1"; | |
2401 | $p = array(1 => array($openId, 'String')); | |
2402 | ||
2403 | if ($ctype) { | |
2404 | $query .= " AND civicrm_contact.contact_type = %3"; | |
2405 | $p[3] = array($ctype, 'String'); | |
2406 | } | |
2407 | ||
2408 | $query .= " ORDER BY civicrm_openid.is_primary DESC"; | |
2409 | ||
2410 | $dao = CRM_Core_DAO::executeQuery($query, $p); | |
2411 | ||
2412 | if ($dao->fetch()) { | |
2413 | return $dao; | |
2414 | } | |
1273d77c | 2415 | return NULL; |
6a488035 TO |
2416 | } |
2417 | ||
2418 | /** | |
57507ae6 | 2419 | * Get primary email of the contact. |
6a488035 | 2420 | * |
77c5b619 TO |
2421 | * @param int $contactID |
2422 | * Contact id. | |
6a488035 | 2423 | * |
a6c01b45 | 2424 | * @return string |
57507ae6 | 2425 | * Email address if present else null |
6a488035 TO |
2426 | */ |
2427 | public static function getPrimaryEmail($contactID) { | |
2428 | // fetch the primary email | |
2429 | $query = " | |
2430 | SELECT civicrm_email.email as email | |
2431 | FROM civicrm_contact | |
2432 | LEFT JOIN civicrm_email ON ( civicrm_contact.id = civicrm_email.contact_id ) | |
2433 | WHERE civicrm_email.is_primary = 1 | |
2434 | AND civicrm_contact.id = %1"; | |
2435 | $p = array(1 => array($contactID, 'Integer')); | |
2436 | $dao = CRM_Core_DAO::executeQuery($query, $p); | |
2437 | ||
2438 | $email = NULL; | |
2439 | if ($dao->fetch()) { | |
2440 | $email = $dao->email; | |
2441 | } | |
2442 | $dao->free(); | |
2443 | return $email; | |
2444 | } | |
2445 | ||
2446 | /** | |
57507ae6 | 2447 | * Function to get primary OpenID of the contact. |
6a488035 | 2448 | * |
77c5b619 TO |
2449 | * @param int $contactID |
2450 | * Contact id. | |
6a488035 | 2451 | * |
a6c01b45 CW |
2452 | * @return string |
2453 | * >openid OpenID if present else null | |
6a488035 TO |
2454 | */ |
2455 | public static function getPrimaryOpenId($contactID) { | |
2456 | // fetch the primary OpenID | |
2457 | $query = " | |
2458 | SELECT civicrm_openid.openid as openid | |
2459 | FROM civicrm_contact | |
2460 | LEFT JOIN civicrm_openid ON ( civicrm_contact.id = civicrm_openid.contact_id ) | |
2461 | WHERE civicrm_contact.id = %1 | |
2462 | AND civicrm_openid.is_primary = 1"; | |
2463 | $p = array(1 => array($contactID, 'Integer')); | |
2464 | $dao = CRM_Core_DAO::executeQuery($query, $p); | |
2465 | ||
2466 | $openId = NULL; | |
2467 | if ($dao->fetch()) { | |
2468 | $openId = $dao->openid; | |
2469 | } | |
2470 | $dao->free(); | |
2471 | return $openId; | |
2472 | } | |
2473 | ||
2474 | /** | |
57507ae6 | 2475 | * Fetch the object and store the values in the values array. |
6a488035 | 2476 | * |
77c5b619 TO |
2477 | * @param array $params |
2478 | * Input parameters to find object. | |
2479 | * @param array $values | |
2480 | * Output values of the object. | |
6a488035 | 2481 | * |
57507ae6 EM |
2482 | * @return CRM_Contact_BAO_Contact|null |
2483 | * The found object or null | |
6a488035 TO |
2484 | */ |
2485 | public static function getValues(&$params, &$values) { | |
2486 | $contact = new CRM_Contact_BAO_Contact(); | |
2487 | ||
2488 | $contact->copyValues($params); | |
2489 | ||
2490 | if ($contact->find(TRUE)) { | |
2491 | ||
2492 | CRM_Core_DAO::storeValues($contact, $values); | |
2493 | ||
2494 | $privacy = array(); | |
2495 | foreach (self::$_commPrefs as $name) { | |
2496 | if (isset($contact->$name)) { | |
2497 | $privacy[$name] = $contact->$name; | |
2498 | } | |
2499 | } | |
2500 | ||
2501 | if (!empty($privacy)) { | |
2502 | $values['privacy'] = $privacy; | |
2503 | } | |
2504 | ||
2505 | // communication Prefferance | |
2506 | $preffComm = $comm = array(); | |
2507 | $comm = explode(CRM_Core_DAO::VALUE_SEPARATOR, | |
2508 | $contact->preferred_communication_method | |
2509 | ); | |
2510 | foreach ($comm as $value) { | |
2511 | $preffComm[$value] = 1; | |
2512 | } | |
2513 | $temp = array('preferred_communication_method' => $contact->preferred_communication_method); | |
2514 | ||
2515 | $names = array( | |
9bb77b8e TO |
2516 | 'preferred_communication_method' => array( |
2517 | 'newName' => 'preferred_communication_method_display', | |
6a488035 | 2518 | 'groupName' => 'preferred_communication_method', |
21dfd5f5 | 2519 | ), |
9bb77b8e | 2520 | ); |
6a488035 | 2521 | |
ed0ca248 | 2522 | // @todo This can be figured out from metadata & we can avoid the uncached query. |
6a488035 TO |
2523 | CRM_Core_OptionGroup::lookupValues($temp, $names, FALSE); |
2524 | ||
2525 | $values['preferred_communication_method'] = $preffComm; | |
2526 | $values['preferred_communication_method_display'] = CRM_Utils_Array::value('preferred_communication_method_display', $temp); | |
2527 | ||
cd223eef | 2528 | if ($contact->preferred_mail_format) { |
2529 | $preferredMailingFormat = CRM_Core_SelectValues::pmf(); | |
2530 | $values['preferred_mail_format'] = $preferredMailingFormat[$contact->preferred_mail_format]; | |
2531 | } | |
6a488035 TO |
2532 | |
2533 | // get preferred languages | |
2534 | if (!empty($contact->preferred_language)) { | |
a8c23526 | 2535 | $values['preferred_language'] = CRM_Core_PseudoConstant::getLabel('CRM_Contact_DAO_Contact', 'preferred_language', $contact->preferred_language); |
6a488035 TO |
2536 | } |
2537 | ||
2538 | // Calculating Year difference | |
2539 | if ($contact->birth_date) { | |
2540 | $birthDate = CRM_Utils_Date::customFormat($contact->birth_date, '%Y%m%d'); | |
2541 | if ($birthDate < date('Ymd')) { | |
9bb77b8e | 2542 | $age = CRM_Utils_Date::calculateAge($birthDate); |
6a488035 TO |
2543 | $values['age']['y'] = CRM_Utils_Array::value('years', $age); |
2544 | $values['age']['m'] = CRM_Utils_Array::value('months', $age); | |
2545 | } | |
6a488035 TO |
2546 | } |
2547 | ||
2548 | $contact->contact_id = $contact->id; | |
2549 | ||
2550 | return $contact; | |
2551 | } | |
2552 | return NULL; | |
2553 | } | |
2554 | ||
2555 | /** | |
db7de9c1 | 2556 | * Given the component name and returns the count of participation of contact. |
6a488035 | 2557 | * |
77c5b619 TO |
2558 | * @param string $component |
2559 | * Input component name. | |
2560 | * @param int $contactId | |
2561 | * Input contact id. | |
2562 | * @param string $tableName | |
2563 | * Optional tableName if component is custom group. | |
6a488035 | 2564 | * |
72b3a70c CW |
2565 | * @return int |
2566 | * total number in database | |
6a488035 | 2567 | */ |
00be9182 | 2568 | public static function getCountComponent($component, $contactId, $tableName = NULL) { |
6a488035 TO |
2569 | $object = NULL; |
2570 | switch ($component) { | |
2571 | case 'tag': | |
2572 | return CRM_Core_BAO_EntityTag::getContactTags($contactId, TRUE); | |
2573 | ||
2574 | case 'rel': | |
6c552737 | 2575 | $result = CRM_Contact_BAO_Relationship::getRelationship($contactId, |
468da094 | 2576 | CRM_Contact_BAO_Relationship::CURRENT, |
b7d19957 | 2577 | 0, 1, 0, |
a93664c8 | 2578 | NULL, NULL, |
2579 | TRUE | |
6a488035 | 2580 | ); |
b7d19957 | 2581 | return $result; |
6a488035 TO |
2582 | |
2583 | case 'group': | |
468da094 | 2584 | |
6a488035 TO |
2585 | return CRM_Contact_BAO_GroupContact::getContactGroup($contactId, "Added", NULL, TRUE); |
2586 | ||
2587 | case 'log': | |
2588 | if (CRM_Core_BAO_Log::useLoggingReport()) { | |
2589 | return FALSE; | |
2590 | } | |
2591 | return CRM_Core_BAO_Log::getContactLogCount($contactId); | |
2592 | ||
2593 | case 'note': | |
2594 | return CRM_Core_BAO_Note::getContactNoteCount($contactId); | |
2595 | ||
2596 | case 'contribution': | |
2597 | return CRM_Contribute_BAO_Contribution::contributionCount($contactId); | |
2598 | ||
2599 | case 'membership': | |
2600 | return CRM_Member_BAO_Membership::getContactMembershipCount($contactId, TRUE); | |
2601 | ||
2602 | case 'participant': | |
2603 | return CRM_Event_BAO_Participant::getContactParticipantCount($contactId); | |
2604 | ||
2605 | case 'pledge': | |
2606 | return CRM_Pledge_BAO_Pledge::getContactPledgeCount($contactId); | |
2607 | ||
2608 | case 'case': | |
2609 | return CRM_Case_BAO_Case::caseCount($contactId); | |
2610 | ||
2611 | case 'grant': | |
2612 | return CRM_Grant_BAO_Grant::getContactGrantCount($contactId); | |
2613 | ||
2614 | case 'activity': | |
2615 | $input = array( | |
2616 | 'contact_id' => $contactId, | |
2617 | 'admin' => FALSE, | |
2618 | 'caseId' => NULL, | |
2619 | 'context' => 'activity', | |
2620 | ); | |
6ab43e1b | 2621 | return CRM_Activity_BAO_Activity::deprecatedGetActivitiesCount($input); |
6a488035 | 2622 | |
36bb946c | 2623 | case 'mailing': |
7c006637 | 2624 | $params = array('contact_id' => $contactId); |
553f116a | 2625 | return CRM_Mailing_BAO_Mailing::getContactMailingsCount($params); |
36bb946c | 2626 | |
6a488035 TO |
2627 | default: |
2628 | $custom = explode('_', $component); | |
2629 | if ($custom['0'] = 'custom') { | |
2630 | if (!$tableName) { | |
2631 | $tableName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $custom['1'], 'table_name'); | |
2632 | } | |
2633 | $queryString = "SELECT count(id) FROM {$tableName} WHERE entity_id = {$contactId}"; | |
2634 | return CRM_Core_DAO::singleValueQuery($queryString); | |
2635 | } | |
2636 | } | |
2637 | } | |
2638 | ||
2639 | /** | |
567b2076 | 2640 | * Process greetings and cache. |
6a488035 | 2641 | * |
77c5b619 TO |
2642 | * @param object $contact |
2643 | * Contact object after save. | |
2644 | * @param bool $useDefaults | |
2645 | * Use default greeting values. | |
6a488035 | 2646 | */ |
00be9182 | 2647 | public static function processGreetings(&$contact, $useDefaults = FALSE) { |
6a488035 TO |
2648 | if ($useDefaults) { |
2649 | //retrieve default greetings | |
2650 | $defaultGreetings = CRM_Core_PseudoConstant::greetingDefaults(); | |
2651 | $contactDefaults = $defaultGreetings[$contact->contact_type]; | |
2652 | } | |
2653 | ||
2654 | // note that contact object not always has required greeting related | |
2655 | // fields that are required to calculate greeting and | |
2656 | // also other fields used in tokens etc, | |
2657 | // hence we need to retrieve it again. | |
9bb77b8e | 2658 | if ($contact->_query !== FALSE) { |
1a902da0 BS |
2659 | $contact->find(TRUE); |
2660 | } | |
6a488035 TO |
2661 | |
2662 | // store object values to an array | |
2663 | $contactDetails = array(); | |
2664 | CRM_Core_DAO::storeValues($contact, $contactDetails); | |
2665 | $contactDetails = array(array($contact->id => $contactDetails)); | |
2666 | ||
2667 | $emailGreetingString = $postalGreetingString = $addresseeString = NULL; | |
2668 | $updateQueryString = array(); | |
2669 | ||
2670 | //cache email and postal greeting to greeting display | |
2671 | if ($contact->email_greeting_custom != 'null' && $contact->email_greeting_custom) { | |
2672 | $emailGreetingString = $contact->email_greeting_custom; | |
2673 | } | |
2674 | elseif ($contact->email_greeting_id != 'null' && $contact->email_greeting_id) { | |
2675 | // the filter value for Individual contact type is set to 1 | |
2676 | $filter = array( | |
2677 | 'contact_type' => $contact->contact_type, | |
2678 | 'greeting_type' => 'email_greeting', | |
2679 | ); | |
2680 | ||
9bb77b8e | 2681 | $emailGreeting = CRM_Core_PseudoConstant::greeting($filter); |
6a488035 TO |
2682 | $emailGreetingString = $emailGreeting[$contact->email_greeting_id]; |
2683 | $updateQueryString[] = " email_greeting_custom = NULL "; | |
2684 | } | |
2685 | else { | |
2686 | if ($useDefaults) { | |
2687 | reset($contactDefaults['email_greeting']); | |
9bb77b8e | 2688 | $emailGreetingID = key($contactDefaults['email_greeting']); |
6a488035 TO |
2689 | $emailGreetingString = $contactDefaults['email_greeting'][$emailGreetingID]; |
2690 | $updateQueryString[] = " email_greeting_id = $emailGreetingID "; | |
2691 | $updateQueryString[] = " email_greeting_custom = NULL "; | |
2692 | } | |
2693 | elseif ($contact->email_greeting_custom) { | |
2694 | $updateQueryString[] = " email_greeting_display = NULL "; | |
2695 | } | |
2696 | } | |
2697 | ||
2698 | if ($emailGreetingString) { | |
73d64eb6 | 2699 | CRM_Contact_BAO_Contact_Utils::processGreetingTemplate($emailGreetingString, |
6a488035 TO |
2700 | $contactDetails, |
2701 | $contact->id, | |
2702 | 'CRM_Contact_BAO_Contact' | |
2703 | ); | |
2704 | $emailGreetingString = CRM_Core_DAO::escapeString(CRM_Utils_String::stripSpaces($emailGreetingString)); | |
2705 | $updateQueryString[] = " email_greeting_display = '{$emailGreetingString}'"; | |
2706 | } | |
2707 | ||
2708 | //postal greetings | |
2709 | if ($contact->postal_greeting_custom != 'null' && $contact->postal_greeting_custom) { | |
2710 | $postalGreetingString = $contact->postal_greeting_custom; | |
2711 | } | |
2712 | elseif ($contact->postal_greeting_id != 'null' && $contact->postal_greeting_id) { | |
2713 | $filter = array( | |
2714 | 'contact_type' => $contact->contact_type, | |
2715 | 'greeting_type' => 'postal_greeting', | |
2716 | ); | |
2717 | $postalGreeting = CRM_Core_PseudoConstant::greeting($filter); | |
2718 | $postalGreetingString = $postalGreeting[$contact->postal_greeting_id]; | |
2719 | $updateQueryString[] = " postal_greeting_custom = NULL "; | |
2720 | } | |
2721 | else { | |
2722 | if ($useDefaults) { | |
2723 | reset($contactDefaults['postal_greeting']); | |
2724 | $postalGreetingID = key($contactDefaults['postal_greeting']); | |
2725 | $postalGreetingString = $contactDefaults['postal_greeting'][$postalGreetingID]; | |
2726 | $updateQueryString[] = " postal_greeting_id = $postalGreetingID "; | |
2727 | $updateQueryString[] = " postal_greeting_custom = NULL "; | |
2728 | } | |
2729 | elseif ($contact->postal_greeting_custom) { | |
2730 | $updateQueryString[] = " postal_greeting_display = NULL "; | |
2731 | } | |
2732 | } | |
2733 | ||
2734 | if ($postalGreetingString) { | |
73d64eb6 | 2735 | CRM_Contact_BAO_Contact_Utils::processGreetingTemplate($postalGreetingString, |
6a488035 TO |
2736 | $contactDetails, |
2737 | $contact->id, | |
2738 | 'CRM_Contact_BAO_Contact' | |
2739 | ); | |
2740 | $postalGreetingString = CRM_Core_DAO::escapeString(CRM_Utils_String::stripSpaces($postalGreetingString)); | |
2741 | $updateQueryString[] = " postal_greeting_display = '{$postalGreetingString}'"; | |
2742 | } | |
2743 | ||
2744 | // addressee | |
2745 | if ($contact->addressee_custom != 'null' && $contact->addressee_custom) { | |
2746 | $addresseeString = $contact->addressee_custom; | |
2747 | } | |
2748 | elseif ($contact->addressee_id != 'null' && $contact->addressee_id) { | |
2749 | $filter = array( | |
2750 | 'contact_type' => $contact->contact_type, | |
2751 | 'greeting_type' => 'addressee', | |
2752 | ); | |
2753 | ||
9bb77b8e TO |
2754 | $addressee = CRM_Core_PseudoConstant::greeting($filter); |
2755 | $addresseeString = $addressee[$contact->addressee_id]; | |
6a488035 TO |
2756 | $updateQueryString[] = " addressee_custom = NULL "; |
2757 | } | |
2758 | else { | |
2759 | if ($useDefaults) { | |
2760 | reset($contactDefaults['addressee']); | |
9bb77b8e TO |
2761 | $addresseeID = key($contactDefaults['addressee']); |
2762 | $addresseeString = $contactDefaults['addressee'][$addresseeID]; | |
6a488035 TO |
2763 | $updateQueryString[] = " addressee_id = $addresseeID "; |
2764 | $updateQueryString[] = " addressee_custom = NULL "; | |
2765 | } | |
2766 | elseif ($contact->addressee_custom) { | |
2767 | $updateQueryString[] = " addressee_display = NULL "; | |
2768 | } | |
2769 | } | |
2770 | ||
2771 | if ($addresseeString) { | |
73d64eb6 | 2772 | CRM_Contact_BAO_Contact_Utils::processGreetingTemplate($addresseeString, |
6a488035 TO |
2773 | $contactDetails, |
2774 | $contact->id, | |
2775 | 'CRM_Contact_BAO_Contact' | |
2776 | ); | |
2777 | $addresseeString = CRM_Core_DAO::escapeString(CRM_Utils_String::stripSpaces($addresseeString)); | |
2778 | $updateQueryString[] = " addressee_display = '{$addresseeString}'"; | |
2779 | } | |
2780 | ||
2781 | if (!empty($updateQueryString)) { | |
2782 | $updateQueryString = implode(',', $updateQueryString); | |
2783 | $queryString = "UPDATE civicrm_contact SET {$updateQueryString} WHERE id = {$contact->id}"; | |
2784 | CRM_Core_DAO::executeQuery($queryString); | |
2785 | } | |
2786 | } | |
2787 | ||
2788 | /** | |
100fef9d | 2789 | * Retrieve loc block ids w/ given condition. |
6a488035 | 2790 | * |
77c5b619 TO |
2791 | * @param int $contactId |
2792 | * Contact id. | |
2793 | * @param array $criteria | |
2794 | * Key => value pair which should be. | |
6a488035 | 2795 | * fulfill by return record ids. |
77c5b619 TO |
2796 | * @param string $condOperator |
2797 | * Operator use for grouping multiple conditions. | |
6a488035 | 2798 | * |
a6c01b45 CW |
2799 | * @return array |
2800 | * loc block ids which fulfill condition. | |
6a488035 | 2801 | */ |
00be9182 | 2802 | public static function getLocBlockIds($contactId, $criteria = array(), $condOperator = 'AND') { |
6a488035 TO |
2803 | $locBlockIds = array(); |
2804 | if (!$contactId) { | |
2805 | return $locBlockIds; | |
2806 | } | |
2807 | ||
46a4f1e9 | 2808 | foreach (array('Email', 'OpenID', 'Phone', 'Address', 'IM') as $block) { |
6a488035 | 2809 | $name = strtolower($block); |
46a4f1e9 CW |
2810 | $className = "CRM_Core_DAO_$block"; |
2811 | $blockDAO = new $className(); | |
6a488035 TO |
2812 | |
2813 | // build the condition. | |
2814 | if (is_array($criteria)) { | |
46a4f1e9 | 2815 | $fields = $blockDAO->fields(); |
6a488035 TO |
2816 | $conditions = array(); |
2817 | foreach ($criteria as $field => $value) { | |
2818 | if (array_key_exists($field, $fields)) { | |
2819 | $cond = "( $field = $value )"; | |
2820 | // value might be zero or null. | |
2821 | if (!$value || strtolower($value) == 'null') { | |
2822 | $cond = "( $field = 0 OR $field IS NULL )"; | |
2823 | } | |
2824 | $conditions[] = $cond; | |
2825 | } | |
2826 | } | |
2827 | if (!empty($conditions)) { | |
2828 | $blockDAO->whereAdd(implode(" $condOperator ", $conditions)); | |
2829 | } | |
2830 | } | |
2831 | ||
2832 | $blockDAO->contact_id = $contactId; | |
2833 | $blockDAO->find(); | |
2834 | while ($blockDAO->fetch()) { | |
2835 | $locBlockIds[$name][] = $blockDAO->id; | |
2836 | } | |
2837 | $blockDAO->free(); | |
2838 | } | |
2839 | ||
2840 | return $locBlockIds; | |
2841 | } | |
2842 | ||
2843 | /** | |
100fef9d | 2844 | * Build context menu items. |
6a488035 | 2845 | * |
100fef9d | 2846 | * @param int $contactId |
77b97be7 | 2847 | * |
a6c01b45 | 2848 | * @return array |
16b10e64 | 2849 | * Array of context menu for logged in user. |
6a488035 | 2850 | */ |
00be9182 | 2851 | public static function contextMenu($contactId = NULL) { |
6a488035 | 2852 | $menu = array( |
9309f5c6 E |
2853 | 'view' => array( |
2854 | 'title' => ts('View Contact'), | |
2855 | 'weight' => 0, | |
2856 | 'ref' => 'view-contact', | |
2857 | 'class' => 'no-popup', | |
2858 | 'key' => 'view', | |
2859 | 'permissions' => array('view all contacts'), | |
2860 | ), | |
2861 | 'add' => array( | |
2862 | 'title' => ts('Edit Contact'), | |
2863 | 'weight' => 0, | |
2864 | 'ref' => 'edit-contact', | |
2865 | 'class' => 'no-popup', | |
2866 | 'key' => 'add', | |
2867 | 'permissions' => array('edit all contacts'), | |
2868 | ), | |
2869 | 'delete' => array( | |
2870 | 'title' => ts('Delete Contact'), | |
2871 | 'weight' => 0, | |
2872 | 'ref' => 'delete-contact', | |
2873 | 'key' => 'delete', | |
2874 | 'permissions' => array('access deleted contacts', 'delete contacts'), | |
2875 | ), | |
2876 | 'contribution' => array( | |
2877 | 'title' => ts('Add Contribution'), | |
2878 | 'weight' => 5, | |
2879 | 'ref' => 'new-contribution', | |
2880 | 'key' => 'contribution', | |
2881 | 'tab' => 'contribute', | |
2882 | 'component' => 'CiviContribute', | |
2883 | 'href' => CRM_Utils_System::url('civicrm/contact/view/contribution', | |
2884 | 'reset=1&action=add&context=contribution' | |
6a488035 | 2885 | ), |
9309f5c6 E |
2886 | 'permissions' => array( |
2887 | 'access CiviContribute', | |
2888 | 'edit contributions', | |
6a488035 | 2889 | ), |
9309f5c6 E |
2890 | ), |
2891 | 'participant' => array( | |
2892 | 'title' => ts('Register for Event'), | |
2893 | 'weight' => 10, | |
2894 | 'ref' => 'new-participant', | |
2895 | 'key' => 'participant', | |
2896 | 'tab' => 'participant', | |
2897 | 'component' => 'CiviEvent', | |
2898 | 'href' => CRM_Utils_System::url('civicrm/contact/view/participant', 'reset=1&action=add&context=participant'), | |
2899 | 'permissions' => array( | |
2900 | 'access CiviEvent', | |
2901 | 'edit event participants', | |
6a488035 | 2902 | ), |
9309f5c6 E |
2903 | ), |
2904 | 'activity' => array( | |
2905 | 'title' => ts('Record Activity'), | |
2906 | 'weight' => 35, | |
2907 | 'ref' => 'new-activity', | |
2908 | 'key' => 'activity', | |
2909 | 'permissions' => array('edit all contacts'), | |
2910 | ), | |
2911 | 'pledge' => array( | |
2912 | 'title' => ts('Add Pledge'), | |
2913 | 'weight' => 15, | |
2914 | 'ref' => 'new-pledge', | |
2915 | 'key' => 'pledge', | |
2916 | 'tab' => 'pledge', | |
2917 | 'href' => CRM_Utils_System::url('civicrm/contact/view/pledge', | |
2918 | 'reset=1&action=add&context=pledge' | |
6a488035 | 2919 | ), |
9309f5c6 E |
2920 | 'component' => 'CiviPledge', |
2921 | 'permissions' => array( | |
2922 | 'access CiviPledge', | |
2923 | 'edit pledges', | |
1fb73100 | 2924 | ), |
9309f5c6 E |
2925 | ), |
2926 | 'membership' => array( | |
2927 | 'title' => ts('Add Membership'), | |
2928 | 'weight' => 20, | |
2929 | 'ref' => 'new-membership', | |
2930 | 'key' => 'membership', | |
2931 | 'tab' => 'member', | |
2932 | 'component' => 'CiviMember', | |
2933 | 'href' => CRM_Utils_System::url('civicrm/contact/view/membership', | |
2934 | 'reset=1&action=add&context=membership' | |
1fb73100 | 2935 | ), |
9309f5c6 E |
2936 | 'permissions' => array( |
2937 | 'access CiviMember', | |
2938 | 'edit memberships', | |
1fb73100 | 2939 | ), |
9309f5c6 E |
2940 | ), |
2941 | 'case' => array( | |
2942 | 'title' => ts('Add Case'), | |
2943 | 'weight' => 25, | |
2944 | 'ref' => 'new-case', | |
2945 | 'key' => 'case', | |
2946 | 'tab' => 'case', | |
2947 | 'component' => 'CiviCase', | |
2948 | 'href' => CRM_Utils_System::url('civicrm/case/add', 'reset=1&action=add&context=case'), | |
2949 | 'permissions' => array('add cases'), | |
2950 | ), | |
2951 | 'grant' => array( | |
2952 | 'title' => ts('Add Grant'), | |
2953 | 'weight' => 26, | |
2954 | 'ref' => 'new-grant', | |
2955 | 'key' => 'grant', | |
2956 | 'tab' => 'grant', | |
2957 | 'component' => 'CiviGrant', | |
2958 | 'href' => CRM_Utils_System::url('civicrm/contact/view/grant', | |
2959 | 'reset=1&action=add&context=grant' | |
1fb73100 | 2960 | ), |
9309f5c6 E |
2961 | 'permissions' => array('edit grants'), |
2962 | ), | |
2963 | 'rel' => array( | |
2964 | 'title' => ts('Add Relationship'), | |
2965 | 'weight' => 30, | |
2966 | 'ref' => 'new-relationship', | |
2967 | 'key' => 'rel', | |
2968 | 'tab' => 'rel', | |
2969 | 'href' => CRM_Utils_System::url('civicrm/contact/view/rel', | |
2970 | 'reset=1&action=add' | |
1fb73100 | 2971 | ), |
9309f5c6 E |
2972 | 'permissions' => array('edit all contacts'), |
2973 | ), | |
2974 | 'note' => array( | |
2975 | 'title' => ts('Add Note'), | |
2976 | 'weight' => 40, | |
2977 | 'ref' => 'new-note', | |
2978 | 'key' => 'note', | |
2979 | 'tab' => 'note', | |
2980 | 'class' => 'medium-popup', | |
2981 | 'href' => CRM_Utils_System::url('civicrm/contact/view/note', | |
2982 | 'reset=1&action=add' | |
6a488035 | 2983 | ), |
9309f5c6 | 2984 | 'permissions' => array('edit all contacts'), |
6a488035 | 2985 | ), |
9309f5c6 E |
2986 | 'email' => array( |
2987 | 'title' => ts('Send an Email'), | |
2988 | 'weight' => 45, | |
2989 | 'ref' => 'new-email', | |
2990 | 'key' => 'email', | |
2991 | 'permissions' => array('view all contacts'), | |
2992 | ), | |
2993 | 'group' => array( | |
2994 | 'title' => ts('Add to Group'), | |
2995 | 'weight' => 50, | |
2996 | 'ref' => 'group-add-contact', | |
2997 | 'key' => 'group', | |
2998 | 'tab' => 'group', | |
2999 | 'permissions' => array('edit groups'), | |
3000 | ), | |
3001 | 'tag' => array( | |
3002 | 'title' => ts('Tag Contact'), | |
3003 | 'weight' => 55, | |
3004 | 'ref' => 'tag-contact', | |
3005 | 'key' => 'tag', | |
3006 | 'tab' => 'tag', | |
3007 | 'permissions' => array('edit all contacts'), | |
3008 | ), | |
3009 | ); | |
3010 | ||
3011 | $menu['otherActions'] = array( | |
3012 | 'print' => array( | |
3013 | 'title' => ts('Print Summary'), | |
3014 | 'description' => ts('Printer-friendly view of this page.'), | |
3015 | 'weight' => 5, | |
3016 | 'ref' => 'crm-contact-print', | |
3017 | 'key' => 'print', | |
3018 | 'tab' => 'print', | |
3019 | 'href' => CRM_Utils_System::url('civicrm/contact/view/print', | |
3020 | "reset=1&print=1" | |
1fb73100 | 3021 | ), |
9309f5c6 E |
3022 | 'class' => 'print', |
3023 | 'icon' => 'crm-i fa-print', | |
3024 | ), | |
3025 | 'vcard' => array( | |
3026 | 'title' => ts('vCard'), | |
3027 | 'description' => ts('vCard record for this contact.'), | |
3028 | 'weight' => 10, | |
3029 | 'ref' => 'crm-contact-vcard', | |
3030 | 'key' => 'vcard', | |
3031 | 'tab' => 'vcard', | |
3032 | 'href' => CRM_Utils_System::url('civicrm/contact/view/vcard', | |
3033 | "reset=1" | |
1fb73100 | 3034 | ), |
9309f5c6 E |
3035 | 'class' => 'vcard', |
3036 | 'icon' => 'crm-i fa-list-alt', | |
6a488035 TO |
3037 | ), |
3038 | ); | |
3039 | ||
9309f5c6 | 3040 | if (CRM_Core_Permission::check('access Contact Dashboard')) { |
1fb73100 E |
3041 | $menu['otherActions']['dashboard'] = array( |
3042 | 'title' => ts('Contact Dashboard'), | |
3043 | 'description' => ts('Contact Dashboard'), | |
3044 | 'weight' => 15, | |
3045 | 'ref' => 'crm-contact-dashboard', | |
3046 | 'key' => 'dashboard', | |
3047 | 'tab' => 'dashboard', | |
3048 | 'class' => 'dashboard', | |
9309f5c6 E |
3049 | // NOTE: As an alternative you can also build url on CMS specific way |
3050 | // as CRM_Core_Config::singleton()->userSystem->getUserRecordUrl($contactId) | |
3051 | 'href' => CRM_Utils_System::url('civicrm/user', "reset=1&id={$contactId}"), | |
1fb73100 E |
3052 | 'icon' => 'crm-i fa-tachometer', |
3053 | ); | |
3054 | } | |
3055 | ||
9309f5c6 E |
3056 | $uid = CRM_Core_BAO_UFMatch::getUFId($contactId); |
3057 | if ($uid) { | |
1fb73100 E |
3058 | $menu['otherActions']['user-record'] = array( |
3059 | 'title' => ts('User Record'), | |
3060 | 'description' => ts('User Record'), | |
3061 | 'weight' => 20, | |
3062 | 'ref' => 'crm-contact-user-record', | |
3063 | 'key' => 'user-record', | |
3064 | 'tab' => 'user-record', | |
3065 | 'class' => 'user-record', | |
9309f5c6 | 3066 | 'href' => CRM_Core_Config::singleton()->userSystem->getUserRecordUrl($contactId), |
1fb73100 E |
3067 | 'icon' => 'crm-i fa-user', |
3068 | ); | |
3069 | } | |
9309f5c6 | 3070 | elseif (CRM_Core_Config::singleton()->userSystem->checkPermissionAddUser()) { |
1fb73100 E |
3071 | $menu['otherActions']['user-add'] = array( |
3072 | 'title' => ts('Create User Record'), | |
3073 | 'description' => ts('Create User Record'), | |
3074 | 'weight' => 25, | |
3075 | 'ref' => 'crm-contact-user-add', | |
3076 | 'key' => 'user-add', | |
3077 | 'tab' => 'user-add', | |
3078 | 'class' => 'user-add', | |
9309f5c6 | 3079 | 'href' => CRM_Utils_System::url('civicrm/contact/view/useradd', 'reset=1&action=add&cid=' . $contactId), |
1fb73100 E |
3080 | 'icon' => 'crm-i fa-user-plus', |
3081 | ); | |
3082 | } | |
3083 | ||
6a488035 TO |
3084 | CRM_Utils_Hook::summaryActions($menu, $contactId); |
3085 | //1. check for component is active. | |
3086 | //2. check for user permissions. | |
3087 | //3. check for acls. | |
3088 | //3. edit and view contact are directly accessible to user. | |
3089 | ||
3090 | $aclPermissionedTasks = array( | |
9bb77b8e TO |
3091 | 'view-contact', |
3092 | 'edit-contact', | |
3093 | 'new-activity', | |
3094 | 'new-email', | |
3095 | 'group-add-contact', | |
3096 | 'tag-contact', | |
3097 | 'delete-contact', | |
6a488035 TO |
3098 | ); |
3099 | $corePermission = CRM_Core_Permission::getPermission(); | |
3100 | ||
6a488035 | 3101 | $contextMenu = array(); |
9309f5c6 E |
3102 | foreach ($menu as $key => $values) { |
3103 | if ($key != 'otherActions') { | |
6a488035 | 3104 | |
1fb73100 | 3105 | // user does not have necessary permissions. |
9309f5c6 | 3106 | if (!self::checkUserMenuPermissions($aclPermissionedTasks, $corePermission, $values)) { |
1fb73100 E |
3107 | continue; |
3108 | } | |
1fb73100 E |
3109 | // build directly accessible action menu. |
3110 | if (in_array($values['ref'], array( | |
3111 | 'view-contact', | |
3112 | 'edit-contact', | |
3113 | ))) { | |
3114 | $contextMenu['primaryActions'][$key] = array( | |
3115 | 'title' => $values['title'], | |
3116 | 'ref' => $values['ref'], | |
3117 | 'class' => CRM_Utils_Array::value('class', $values), | |
3118 | 'key' => $values['key'], | |
3119 | ); | |
3120 | continue; | |
3121 | } | |
3122 | ||
3123 | // finally get menu item for -more- action widget. | |
9309f5c6 | 3124 | $contextMenu['moreActions'][$values['weight']] = array( |
6a488035 TO |
3125 | 'title' => $values['title'], |
3126 | 'ref' => $values['ref'], | |
1fb73100 E |
3127 | 'href' => CRM_Utils_Array::value('href', $values), |
3128 | 'tab' => CRM_Utils_Array::value('tab', $values), | |
8e4ec1f5 | 3129 | 'class' => CRM_Utils_Array::value('class', $values), |
6a488035 TO |
3130 | 'key' => $values['key'], |
3131 | ); | |
6a488035 | 3132 | } |
9309f5c6 E |
3133 | else { |
3134 | foreach ($values as $value) { | |
3135 | // user does not have necessary permissions. | |
3136 | if (!self::checkUserMenuPermissions($aclPermissionedTasks, $corePermission, $value)) { | |
3137 | continue; | |
3138 | } | |
3139 | ||
3140 | // finally get menu item for -more- action widget. | |
3141 | $contextMenu['otherActions'][$value['weight']] = array( | |
3142 | 'title' => $value['title'], | |
3143 | 'ref' => $value['ref'], | |
3144 | 'href' => CRM_Utils_Array::value('href', $value), | |
3145 | 'tab' => CRM_Utils_Array::value('tab', $value), | |
3146 | 'class' => CRM_Utils_Array::value('class', $value), | |
3147 | 'icon' => CRM_Utils_Array::value('icon', $value), | |
3148 | 'key' => $value['key'], | |
3149 | ); | |
3150 | } | |
3151 | } | |
6a488035 TO |
3152 | } |
3153 | ||
3154 | ksort($contextMenu['moreActions']); | |
1fb73100 | 3155 | ksort($contextMenu['otherActions']); |
6a488035 TO |
3156 | |
3157 | return $contextMenu; | |
3158 | } | |
3159 | ||
9309f5c6 E |
3160 | /** |
3161 | * Check if user has permissions to access items in action menu. | |
3162 | * | |
3163 | * @param array $aclPermissionedTasks | |
3164 | * Array containing ACL related tasks. | |
3165 | * @param string $corePermission | |
3166 | * The permission of the user (edit or view or null). | |
3167 | * @param array $menuOptions | |
3168 | * Array containing params of the menu (title, href, etc). | |
3169 | * | |
3170 | * @return bool | |
3171 | * TRUE if user has all permissions, FALSE if otherwise. | |
3172 | */ | |
3173 | public static function checkUserMenuPermissions($aclPermissionedTasks, $corePermission, $menuOptions) { | |
3174 | $componentName = CRM_Utils_Array::value('component', $menuOptions); | |
3175 | ||
3176 | // if component action - make sure component is enable. | |
3177 | if ($componentName && !in_array($componentName, CRM_Core_Config::singleton()->enableComponents)) { | |
3178 | return FALSE; | |
3179 | } | |
3180 | ||
3181 | // make sure user has all required permissions. | |
3182 | $hasAllPermissions = FALSE; | |
3183 | ||
3184 | $permissions = CRM_Utils_Array::value('permissions', $menuOptions); | |
3185 | if (!is_array($permissions) || empty($permissions)) { | |
3186 | $hasAllPermissions = TRUE; | |
3187 | } | |
3188 | ||
3189 | // iterate for required permissions in given permissions array. | |
3190 | if (!$hasAllPermissions) { | |
3191 | $hasPermissions = 0; | |
3192 | foreach ($permissions as $permission) { | |
3193 | if (CRM_Core_Permission::check($permission)) { | |
3194 | $hasPermissions++; | |
3195 | } | |
3196 | } | |
3197 | ||
3198 | if (count($permissions) == $hasPermissions) { | |
3199 | $hasAllPermissions = TRUE; | |
3200 | } | |
3201 | ||
3202 | // if still user does not have required permissions, check acl. | |
3203 | if (!$hasAllPermissions && $menuOptions['ref'] != 'delete-contact') { | |
c2463857 | 3204 | if (in_array($menuOptions['ref'], $aclPermissionedTasks) && |
9309f5c6 E |
3205 | $corePermission == CRM_Core_Permission::EDIT |
3206 | ) { | |
3207 | $hasAllPermissions = TRUE; | |
3208 | } | |
3209 | elseif (in_array($menuOptions['ref'], array( | |
3210 | 'new-email', | |
3211 | ))) { | |
3212 | // grant permissions for these tasks. | |
3213 | $hasAllPermissions = TRUE; | |
3214 | } | |
3215 | } | |
3216 | } | |
3217 | ||
3218 | return $hasAllPermissions; | |
3219 | } | |
3220 | ||
6a488035 | 3221 | /** |
567b2076 EM |
3222 | * Retrieve display name of contact that address is shared. |
3223 | * | |
3224 | * This is based on $masterAddressId or $contactId . | |
6a488035 | 3225 | * |
77c5b619 TO |
3226 | * @param int $masterAddressId |
3227 | * Master id. | |
3228 | * @param int $contactId | |
3229 | * Contact id. | |
6a488035 | 3230 | * |
72b3a70c CW |
3231 | * @return string|null |
3232 | * the found display name or null. | |
6a488035 | 3233 | */ |
00be9182 | 3234 | public static function getMasterDisplayName($masterAddressId = NULL, $contactId = NULL) { |
6a488035 TO |
3235 | $masterDisplayName = NULL; |
3236 | $sql = NULL; | |
3237 | if (!$masterAddressId && !$contactId) { | |
3238 | return $masterDisplayName; | |
3239 | } | |
3240 | ||
3241 | if ($masterAddressId) { | |
3242 | $sql = " | |
3243 | SELECT display_name from civicrm_contact | |
3244 | LEFT JOIN civicrm_address ON ( civicrm_address.contact_id = civicrm_contact.id ) | |
3245 | WHERE civicrm_address.id = " . $masterAddressId; | |
3246 | } | |
3247 | elseif ($contactId) { | |
3248 | $sql = " | |
3249 | SELECT display_name from civicrm_contact cc, civicrm_address add1 | |
3250 | LEFT JOIN civicrm_address add2 ON ( add1.master_id = add2.id ) | |
3251 | WHERE cc.id = add2.contact_id AND add1.contact_id = " . $contactId; | |
3252 | } | |
3253 | ||
3254 | $masterDisplayName = CRM_Core_DAO::singleValueQuery($sql); | |
3255 | return $masterDisplayName; | |
3256 | } | |
3257 | ||
3258 | /** | |
567b2076 | 3259 | * Get the creation/modification times for a contact. |
6a488035 | 3260 | * |
100fef9d | 3261 | * @param int $contactId |
77b97be7 | 3262 | * |
567b2076 | 3263 | * @return array |
5c766a0b | 3264 | * Dates - ('created_date' => $, 'modified_date' => $) |
77b97be7 | 3265 | */ |
00be9182 | 3266 | public static function getTimestamps($contactId) { |
6a488035 TO |
3267 | $timestamps = CRM_Core_DAO::executeQuery( |
3268 | 'SELECT created_date, modified_date | |
3269 | FROM civicrm_contact | |
3270 | WHERE id = %1', | |
3271 | array( | |
3272 | 1 => array($contactId, 'Integer'), | |
3273 | ) | |
3274 | ); | |
3275 | if ($timestamps->fetch()) { | |
3276 | return array( | |
3277 | 'created_date' => $timestamps->created_date, | |
3278 | 'modified_date' => $timestamps->modified_date, | |
3279 | ); | |
3280 | } | |
3281 | else { | |
3282 | return NULL; | |
3283 | } | |
3284 | } | |
3285 | ||
0590c631 | 3286 | /** |
567b2076 EM |
3287 | * Generate triggers to update the timestamp. |
3288 | * | |
3289 | * The corresponding civicrm_contact row is updated on insert/update/delete | |
3290 | * to a table that extends civicrm_contact. | |
0590c631 DK |
3291 | * Don't regenerate triggers for all such tables if only asked for one table. |
3292 | * | |
3293 | * @param array $info | |
3294 | * Reference to the array where generated trigger information is being stored | |
3295 | * @param string|null $reqTableName | |
3296 | * Name of the table for which triggers are being generated, or NULL if all tables | |
3297 | * @param array $relatedTableNames | |
3298 | * Array of all core or all custom table names extending civicrm_contact | |
3299 | * @param string $contactRefColumn | |
3300 | * 'contact_id' if processing core tables, 'entity_id' if processing custom tables | |
0590c631 DK |
3301 | * |
3302 | * @link https://issues.civicrm.org/jira/browse/CRM-15602 | |
3303 | * @see triggerInfo | |
0590c631 | 3304 | */ |
00be9182 | 3305 | public static function generateTimestampTriggers(&$info, $reqTableName, $relatedTableNames, $contactRefColumn) { |
0590c631 DK |
3306 | // Safety |
3307 | $contactRefColumn = CRM_Core_DAO::escapeString($contactRefColumn); | |
3308 | // If specific related table requested, just process that one | |
3309 | if (in_array($reqTableName, $relatedTableNames)) { | |
3310 | $relatedTableNames = array($reqTableName); | |
3311 | } | |
3312 | ||
3313 | // If no specific table requested (include all related tables), | |
3314 | // or a specific related table requested (as matched above) | |
3315 | if (empty($reqTableName) || in_array($reqTableName, $relatedTableNames)) { | |
3316 | $info[] = array( | |
3317 | 'table' => $relatedTableNames, | |
3318 | 'when' => 'AFTER', | |
3319 | 'event' => array('INSERT', 'UPDATE'), | |
3320 | 'sql' => "\nUPDATE civicrm_contact SET modified_date = CURRENT_TIMESTAMP WHERE id = NEW.$contactRefColumn;\n", | |
3321 | ); | |
3322 | $info[] = array( | |
3323 | 'table' => $relatedTableNames, | |
3324 | 'when' => 'AFTER', | |
3325 | 'event' => array('DELETE'), | |
3326 | 'sql' => "\nUPDATE civicrm_contact SET modified_date = CURRENT_TIMESTAMP WHERE id = OLD.$contactRefColumn;\n", | |
3327 | ); | |
3328 | } | |
3329 | } | |
3330 | ||
6a488035 | 3331 | /** |
db7de9c1 | 3332 | * Get a list of triggers for the contact table. |
6a488035 TO |
3333 | * |
3334 | * @see hook_civicrm_triggerInfo | |
3335 | * @see CRM_Core_DAO::triggerRebuild | |
3336 | * @see http://issues.civicrm.org/jira/browse/CRM-10554 | |
2e2605fe EM |
3337 | * |
3338 | * @param $info | |
3339 | * @param null $tableName | |
6a488035 | 3340 | */ |
00be9182 | 3341 | public static function triggerInfo(&$info, $tableName = NULL) { |
6a488035 TO |
3342 | //during upgrade, first check for valid version and then create triggers |
3343 | //i.e the columns created_date and modified_date are introduced in 4.3.alpha1 so dont create triggers for older version | |
3344 | if (CRM_Core_Config::isUpgradeMode()) { | |
3345 | $currentVer = CRM_Core_BAO_Domain::version(TRUE); | |
3346 | //if current version is less than 4.3.alpha1 dont create below triggers | |
3347 | if (version_compare($currentVer, '4.3.alpha1') < 0) { | |
3348 | return; | |
3349 | } | |
3350 | } | |
3351 | ||
0590c631 | 3352 | // Update timestamp for civicrm_contact itself |
6a488035 TO |
3353 | if ($tableName == NULL || $tableName == self::getTableName()) { |
3354 | $info[] = array( | |
3355 | 'table' => array(self::getTableName()), | |
3356 | 'when' => 'BEFORE', | |
3357 | 'event' => array('INSERT'), | |
3358 | 'sql' => "\nSET NEW.created_date = CURRENT_TIMESTAMP;\n", | |
3359 | ); | |
3360 | } | |
3361 | ||
3362 | // Update timestamp when modifying closely related core tables | |
3363 | $relatedTables = array( | |
3364 | 'civicrm_address', | |
3365 | 'civicrm_email', | |
3366 | 'civicrm_im', | |
3367 | 'civicrm_phone', | |
3368 | 'civicrm_website', | |
3369 | ); | |
0590c631 | 3370 | self::generateTimestampTriggers($info, $tableName, $relatedTables, 'contact_id'); |
6a488035 TO |
3371 | |
3372 | // Update timestamp when modifying related custom-data tables | |
3373 | $customGroupTables = array(); | |
3374 | $customGroupDAO = CRM_Core_BAO_CustomGroup::getAllCustomGroupsByBaseEntity('Contact'); | |
3375 | $customGroupDAO->is_multiple = 0; | |
3376 | $customGroupDAO->find(); | |
3377 | while ($customGroupDAO->fetch()) { | |
3378 | $customGroupTables[] = $customGroupDAO->table_name; | |
3379 | } | |
3380 | if (!empty($customGroupTables)) { | |
0590c631 | 3381 | self::generateTimestampTriggers($info, $tableName, $customGroupTables, 'entity_id'); |
6a488035 TO |
3382 | } |
3383 | ||
3384 | // Update phone table to populate phone_numeric field | |
3385 | if (!$tableName || $tableName == 'civicrm_phone') { | |
3386 | // Define stored sql function needed for phones | |
1b31daf6 | 3387 | $sqlTriggers = Civi::service('sql_triggers'); |
3388 | $sqlTriggers->enqueueQuery(self::DROP_STRIP_FUNCTION_43); | |
3389 | $sqlTriggers->enqueueQuery(self::CREATE_STRIP_FUNCTION_43); | |
6a488035 TO |
3390 | $info[] = array( |
3391 | 'table' => array('civicrm_phone'), | |
3392 | 'when' => 'BEFORE', | |
3393 | 'event' => array('INSERT', 'UPDATE'), | |
3394 | 'sql' => "\nSET NEW.phone_numeric = civicrm_strip_non_numeric(NEW.phone);\n", | |
3395 | ); | |
3396 | } | |
3397 | } | |
3398 | ||
3399 | /** | |
db7de9c1 | 3400 | * Check if contact is being used in civicrm_domain based on $contactId. |
6a488035 | 3401 | * |
77c5b619 TO |
3402 | * @param int $contactId |
3403 | * Contact id. | |
6a488035 | 3404 | * |
a6c01b45 CW |
3405 | * @return bool |
3406 | * true if present else false. | |
6a488035 | 3407 | */ |
00be9182 | 3408 | public static function checkDomainContact($contactId) { |
db7de9c1 | 3409 | if (!$contactId) { |
6a488035 | 3410 | return FALSE; |
db7de9c1 | 3411 | } |
9bb77b8e | 3412 | $domainId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain', $contactId, 'id', 'contact_id'); |
6a488035 TO |
3413 | |
3414 | if ($domainId) { | |
3415 | return TRUE; | |
9bb77b8e TO |
3416 | } |
3417 | else { | |
6a488035 TO |
3418 | return FALSE; |
3419 | } | |
3420 | } | |
12445e1c | 3421 | |
dc86f881 CW |
3422 | /** |
3423 | * Get options for a given contact field. | |
db7de9c1 | 3424 | * |
dc86f881 CW |
3425 | * @see CRM_Core_DAO::buildOptions |
3426 | * | |
3427 | * TODO: Should we always assume chainselect? What fn should be responsible for controlling that flow? | |
3428 | * TODO: In context of chainselect, what to return if e.g. a country has no states? | |
3429 | * | |
77c5b619 TO |
3430 | * @param string $fieldName |
3431 | * @param string $context | |
63e9c3fd | 3432 | * @see CRM_Core_DAO::buildOptionsContext |
77c5b619 | 3433 | * @param array $props |
16b10e64 | 3434 | * whatever is known about this dao object. |
77b97be7 | 3435 | * |
5c766a0b | 3436 | * @return array|bool |
dc86f881 CW |
3437 | */ |
3438 | public static function buildOptions($fieldName, $context = NULL, $props = array()) { | |
3439 | $params = array(); | |
3440 | // Special logic for fields whose options depend on context or properties | |
3441 | switch ($fieldName) { | |
3442 | case 'contact_sub_type': | |
3443 | if (!empty($props['contact_type'])) { | |
3444 | $params['condition'] = "parent_id = (SELECT id FROM civicrm_contact_type WHERE name='{$props['contact_type']}')"; | |
3445 | } | |
3446 | break; | |
ea100cb5 | 3447 | |
bee6039a CW |
3448 | case 'contact_type': |
3449 | if ($context == 'search') { | |
76773c5a CW |
3450 | // CRM-15495 - EntityRef filters and basic search forms expect this format |
3451 | // FIXME: Search builder does not | |
bee6039a CW |
3452 | return CRM_Contact_BAO_ContactType::getSelectElements(); |
3453 | } | |
3454 | break; | |
985f4890 CW |
3455 | |
3456 | // The contact api supports some related entities so we'll honor that by fetching their options | |
3457 | case 'group_id': | |
3458 | case 'group': | |
3459 | return CRM_Contact_BAO_GroupContact::buildOptions('group_id', $context, $props); | |
3460 | ||
3461 | case 'tag_id': | |
3462 | case 'tag': | |
3463 | $props['entity_table'] = 'civicrm_contact'; | |
3464 | return CRM_Core_BAO_EntityTag::buildOptions('tag_id', $context, $props); | |
3465 | ||
3493947a | 3466 | case 'state_province_id': |
d0bfb983 | 3467 | case 'state_province': |
3468 | case 'state_province_name': | |
3493947a | 3469 | case 'country_id': |
d0bfb983 | 3470 | case 'country': |
3493947a | 3471 | case 'county_id': |
3472 | case 'worldregion': | |
3473 | case 'worldregion_id': | |
3474 | return CRM_Core_BAO_Address::buildOptions($fieldName, 'get', $props); | |
3475 | ||
dc86f881 | 3476 | } |
786ad6e1 | 3477 | return CRM_Core_PseudoConstant::get(__CLASS__, $fieldName, $params, $context); |
dc86f881 | 3478 | } |
1071730c | 3479 | |
12445e1c | 3480 | /** |
db7de9c1 EM |
3481 | * Delete a contact-related object that has an 'is_primary' field. |
3482 | * | |
12445e1c CW |
3483 | * Ensures that is_primary gets assigned to another object if available |
3484 | * Also calls pre/post hooks | |
3485 | * | |
f47539f6 | 3486 | * @param string $type |
3487 | * @param int $id | |
77b97be7 | 3488 | * @return bool |
12445e1c CW |
3489 | */ |
3490 | public static function deleteObjectWithPrimary($type, $id) { | |
3491 | if (!$id || !is_numeric($id)) { | |
a65e2e55 | 3492 | return FALSE; |
12445e1c CW |
3493 | } |
3494 | $daoName = "CRM_Core_DAO_$type"; | |
3495 | $obj = new $daoName(); | |
3496 | $obj->id = $id; | |
3497 | $obj->find(); | |
3498 | if ($obj->fetch()) { | |
3499 | CRM_Utils_Hook::pre('delete', $type, $id, CRM_Core_DAO::$_nullArray); | |
3500 | $contactId = $obj->contact_id; | |
3501 | $obj->delete(); | |
3502 | } | |
3503 | else { | |
a65e2e55 | 3504 | return FALSE; |
12445e1c | 3505 | } |
50fa40e8 CW |
3506 | // is_primary is only relavent if this field belongs to a contact |
3507 | if ($contactId) { | |
3508 | $dao = new $daoName(); | |
3509 | $dao->contact_id = $contactId; | |
3510 | $dao->is_primary = 1; | |
3511 | // Pick another record to be primary (if one isn't already) | |
3512 | if (!$dao->find(TRUE)) { | |
3513 | $dao->is_primary = 0; | |
3514 | $dao->find(); | |
3515 | if ($dao->fetch()) { | |
3516 | $dao->is_primary = 1; | |
3517 | $dao->save(); | |
3518 | } | |
3519 | } | |
3520 | $dao->free(); | |
12445e1c | 3521 | } |
12445e1c CW |
3522 | CRM_Utils_Hook::post('delete', $type, $id, $obj); |
3523 | $obj->free(); | |
a65e2e55 | 3524 | return TRUE; |
12445e1c | 3525 | } |
96025800 | 3526 | |
d343069c CW |
3527 | /** |
3528 | * @inheritDoc | |
3529 | */ | |
20e41014 | 3530 | public function addSelectWhereClause() { |
0b80f0b4 CW |
3531 | // We always return an array with these keys, even if they are empty, |
3532 | // because this tells the query builder that we have considered these fields for acls | |
032346cc | 3533 | $clauses = array( |
0b80f0b4 CW |
3534 | 'id' => (array) CRM_Contact_BAO_Contact_Permission::cacheSubquery(), |
3535 | 'is_deleted' => CRM_Core_Permission::check('access deleted contacts') ? array() : array('!= 1'), | |
b53bcc5d | 3536 | ); |
032346cc CW |
3537 | CRM_Utils_Hook::selectWhereClause($this, $clauses); |
3538 | return $clauses; | |
d343069c CW |
3539 | } |
3540 | ||
fedc3428 | 3541 | /** |
3542 | * Get any existing duplicate contacts based on the input parameters. | |
3543 | * | |
3544 | * @param array $input | |
3545 | * Input parameters to be matched. | |
3546 | * @param string $contactType | |
3547 | * @param string $rule | |
3548 | * - Supervised | |
3549 | * - Unsupervised | |
3550 | * @param $excludedContactIDs | |
3551 | * An array of ids not to be included in the results. | |
3552 | * @param bool $checkPermissions | |
3553 | * @param int $ruleGroupID | |
3554 | * ID of the rule group to be used if an override is desirable. | |
3555 | * | |
3556 | * @return array | |
3557 | */ | |
3558 | public static function getDuplicateContacts($input, $contactType, $rule = 'Unsupervised', $excludedContactIDs = array(), $checkPermissions = TRUE, $ruleGroupID = NULL) { | |
3559 | $dedupeParams = CRM_Dedupe_Finder::formatParams($input, $contactType); | |
3560 | $dedupeParams['check_permission'] = $checkPermissions; | |
3561 | $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, $contactType, $rule, $excludedContactIDs, $ruleGroupID); | |
3562 | return $ids; | |
3563 | } | |
3564 | ||
3565 | /** | |
3566 | * Get the first duplicate contacts based on the input parameters. | |
3567 | * | |
3568 | * @param array $input | |
3569 | * Input parameters to be matched. | |
3570 | * @param string $contactType | |
3571 | * @param string $rule | |
3572 | * - Supervised | |
3573 | * - Unsupervised | |
3574 | * @param $excludedContactIDs | |
3575 | * An array of ids not to be included in the results. | |
3576 | * @param bool $checkPermissions | |
3577 | * @param int $ruleGroupID | |
3578 | * ID of the rule group to be used if an override is desirable. | |
3579 | * | |
3580 | * @return int|NULL | |
3581 | */ | |
3582 | public static function getFirstDuplicateContact($input, $contactType, $rule = 'Unsupervised', $excludedContactIDs = array(), $checkPermissions = TRUE, $ruleGroupID = NULL) { | |
3583 | $ids = self::getDuplicateContacts($input, $contactType, $rule, $excludedContactIDs, $checkPermissions, $ruleGroupID); | |
3584 | if (empty($ids)) { | |
3585 | return NULL; | |
3586 | } | |
3587 | return $ids[0]; | |
3588 | } | |
3589 | ||
6ebecfea | 3590 | /** |
3591 | * Check if a field is associated with an entity that has a location type. | |
3592 | * | |
3593 | * (ie. is an address, phone, email etc field). | |
3594 | * | |
3595 | * @param string $fieldTitle | |
3596 | * Title of the field (not the name - create a new function for that if required). | |
3597 | * | |
3598 | * @return bool | |
3599 | */ | |
3600 | public static function isFieldHasLocationType($fieldTitle) { | |
3601 | foreach (CRM_Contact_BAO_Contact::importableFields() as $key => $field) { | |
3602 | if ($field['title'] === $fieldTitle) { | |
3603 | return CRM_Utils_Array::value('hasLocationType', $field); | |
3604 | } | |
3605 | } | |
3606 | return FALSE; | |
3607 | } | |
3608 | ||
6a488035 | 3609 | } |