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