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