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