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