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