3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
33 class CRM_Contact_BAO_Contact_Utils
{
36 * Given a contact type, get the contact image.
38 * @param string $contactType
40 * @param bool $urlOnly
41 * If we need to return only image url.
42 * @param int $contactId
44 * @param bool $addProfileOverlay
45 * If profile overlay class should be added.
49 public static function getImage($contactType, $urlOnly = FALSE, $contactId = NULL, $addProfileOverlay = TRUE) {
50 static $imageInfo = [];
52 $contactType = CRM_Utils_Array
::explodePadded($contactType);
53 $contactType = $contactType[0];
55 if (!array_key_exists($contactType, $imageInfo)) {
56 $imageInfo[$contactType] = [];
59 $params = ['name' => $contactType];
60 CRM_Contact_BAO_ContactType
::retrieve($params, $typeInfo);
62 if (!empty($typeInfo['image_URL'])) {
63 $imageUrl = $typeInfo['image_URL'];
64 $config = CRM_Core_Config
::singleton();
66 if (!preg_match("/^(\/|(http(s)?:)).+$/i", $imageUrl)) {
67 $imageUrl = $config->resourceBase
. $imageUrl;
69 $imageInfo[$contactType]['image'] = "<div class=\"icon crm-icon {$typeInfo['name']}-icon\" style=\"background: url('{$imageUrl}')\" title=\"{$contactType}\"></div>";
70 $imageInfo[$contactType]['url'] = $imageUrl;
73 $isSubtype = (array_key_exists('parent_id', $typeInfo) &&
74 $typeInfo['parent_id']
78 $type = CRM_Contact_BAO_ContactType
::getBasicType($typeInfo['name']) . '-subtype';
81 $type = CRM_Utils_Array
::value('name', $typeInfo);
84 // do not add title since it hides contact name
85 if ($addProfileOverlay) {
86 $imageInfo[$contactType]['image'] = "<div class=\"icon crm-icon {$type}-icon\"></div>";
89 $imageInfo[$contactType]['image'] = "<div class=\"icon crm-icon {$type}-icon\" title=\"{$contactType}\"></div>";
91 $imageInfo[$contactType]['url'] = NULL;
95 if ($addProfileOverlay) {
96 static $summaryOverlayProfileId = NULL;
97 if (!$summaryOverlayProfileId) {
98 $summaryOverlayProfileId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', 'summary_overlay', 'id', 'name');
101 $profileURL = CRM_Utils_System
::url('civicrm/profile/view',
102 "reset=1&gid={$summaryOverlayProfileId}&id={$contactId}&snippet=4"
105 $imageInfo[$contactType]['summary-link'] = '<a href="' . $profileURL . '" class="crm-summary-link">' . $imageInfo[$contactType]['image'] . '</a>';
108 $imageInfo[$contactType]['summary-link'] = $imageInfo[$contactType]['image'];
111 return $urlOnly ?
$imageInfo[$contactType]['url'] : $imageInfo[$contactType]['summary-link'];
115 * Function check for mix contact ids(individual+household etc...)
117 * @param array $contactIds
118 * Array of contact ids.
121 * true if mix contact array else false
124 public static function checkContactType(&$contactIds) {
125 if (empty($contactIds)) {
129 $idString = implode(',', $contactIds);
131 SELECT count( DISTINCT contact_type )
133 WHERE id IN ( $idString )
135 $count = CRM_Core_DAO
::singleValueQuery($query,
136 CRM_Core_DAO
::$_nullArray
138 return $count > 1 ?
TRUE : FALSE;
142 * Generate a checksum for a $entityId of type $entityType
144 * @param int $entityId
146 * Timestamp that checksum was generated.
148 * Life of this checksum in hours/ 'inf' for infinite.
149 * @param string $hash
150 * Contact hash, if sent, prevents a query in inner loop.
152 * @param string $entityType
153 * @param null $hashSize
156 * ( $cs, $ts, $live )
158 public static function generateChecksum($entityId, $ts = NULL, $live = NULL, $hash = NULL, $entityType = 'contact', $hashSize = NULL) {
159 // return a warning message if we dont get a entityId
160 // this typically happens when we do a message preview
161 // or an anon mailing view - CRM-8298
163 return 'invalidChecksum';
167 if ($entityType == 'contact') {
168 $hash = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
172 elseif ($entityType == 'mailing') {
173 $hash = CRM_Core_DAO
::getFieldValue('CRM_Mailing_DAO_Mailing',
180 $hash = md5(uniqid(rand(), TRUE));
182 $hash = substr($hash, 0, $hashSize);
185 if ($entityType == 'contact') {
186 CRM_Core_DAO
::setFieldValue('CRM_Contact_DAO_Contact',
191 elseif ($entityType == 'mailing') {
192 CRM_Core_DAO
::setFieldValue('CRM_Mailing_DAO_Mailing',
204 $days = Civi
::settings()->get('checksum_timeout');
208 $cs = md5("{$hash}_{$entityId}_{$ts}_{$live}");
209 return "{$cs}_{$ts}_{$live}";
213 * Make sure the checksum is valid for the passed in contactID.
215 * @param int $contactID
216 * @param string $inputCheck
217 * Checksum to match against.
220 * true if valid, else false
222 public static function validChecksum($contactID, $inputCheck) {
224 $input = CRM_Utils_System
::explode('_', $inputCheck, 3);
226 $inputCS = CRM_Utils_Array
::value(0, $input);
227 $inputTS = CRM_Utils_Array
::value(1, $input);
228 $inputLF = CRM_Utils_Array
::value(2, $input);
230 $check = self
::generateChecksum($contactID, $inputTS, $inputLF);
232 if (!hash_equals($check, $inputCheck)) {
236 // no life limit for checksum
237 if ($inputLF == 'inf') {
241 // checksum matches so now check timestamp
243 return ($inputTS +
($inputLF * 60 * 60) >= $now);
247 * Get the count of contact loctions.
249 * @param int $contactId
253 * max locations for the contact
255 public static function maxLocations($contactId) {
256 $contactLocations = [];
258 // find number of location blocks for this contact and adjust value accordinly
259 // get location type from email
261 ( SELECT location_type_id FROM civicrm_email WHERE contact_id = {$contactId} )
263 ( SELECT location_type_id FROM civicrm_phone WHERE contact_id = {$contactId} )
265 ( SELECT location_type_id FROM civicrm_im WHERE contact_id = {$contactId} )
267 ( SELECT location_type_id FROM civicrm_address WHERE contact_id = {$contactId} )
269 $dao = CRM_Core_DAO
::executeQuery($query);
274 * Create Current employer relationship for a individual.
276 * @param int $contactID
277 * Contact id of the individual.
278 * @param $organization
280 * @param int $previousEmployerID
281 * @param bool $newContact
284 public static function createCurrentEmployerRelationship($contactID, $organization, $previousEmployerID = NULL, $newContact = FALSE) {
285 //if organization name is passed. CRM-15368,CRM-15547
286 if ($organization && !is_numeric($organization)) {
287 $dupeIDs = CRM_Contact_BAO_Contact
::getDuplicateContacts(['organization_name' => $organization], 'Organization', 'Unsupervised', [], FALSE);
289 if (is_array($dupeIDs) && !empty($dupeIDs)) {
290 // we should create relationship only w/ first org CRM-4193
291 foreach ($dupeIDs as $orgId) {
292 $organization = $orgId;
297 //create new organization
299 'contact_type' => 'Organization',
300 'organization_name' => $organization,
302 $org = CRM_Contact_BAO_Contact
::create($newOrg);
303 $organization = $org->id
;
307 if ($organization && is_numeric($organization)) {
308 $cid = ['contact' => $contactID];
310 // get the relationship type id of "Employee of"
311 $relTypeId = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_RelationshipType', 'Employee of', 'id', 'name_a_b');
313 CRM_Core_Error
::fatal(ts("You seem to have deleted the relationship type 'Employee of'"));
316 // create employee of relationship
317 $relationshipParams = [
319 'relationship_type_id' => $relTypeId . '_a_b',
320 'contact_check' => [$organization => TRUE],
322 list($valid, $invalid, $duplicate, $saved, $relationshipIds)
323 = CRM_Contact_BAO_Relationship
::legacyCreateMultiple($relationshipParams, $cid);
325 // In case we change employer, clean previous employer related records.
326 if (!$previousEmployerID && !$newContact) {
327 $previousEmployerID = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $contactID, 'employer_id');
329 if ($previousEmployerID &&
330 $previousEmployerID != $organization
332 self
::clearCurrentEmployer($contactID, $previousEmployerID);
335 // set current employer
336 self
::setCurrentEmployer([$contactID => $organization]);
338 $relationshipParams['relationship_ids'] = $relationshipIds;
339 // Handle related memberships. CRM-3792
340 self
::currentEmployerRelatedMembership($contactID, $organization, $relationshipParams, $duplicate, $previousEmployerID);
345 * Create related memberships for current employer.
347 * @param int $contactID
348 * Contact id of the individual.
349 * @param int $employerID
350 * Contact id of the organization.
351 * @param array $relationshipParams
352 * Relationship params.
353 * @param bool $duplicate
354 * Are we triggered existing relationship.
356 * @param int $previousEmpID
358 * @throws CiviCRM_API3_Exception
360 public static function currentEmployerRelatedMembership($contactID, $employerID, $relationshipParams, $duplicate = FALSE, $previousEmpID = NULL) {
362 $action = CRM_Core_Action
::ADD
;
364 //we do not know that triggered relationship record is active.
366 $relationship = new CRM_Contact_DAO_Relationship();
367 $relationship->contact_id_a
= $contactID;
368 $relationship->contact_id_b
= $employerID;
369 $relationship->relationship_type_id
= $relationshipParams['relationship_type_id'];
370 if ($relationship->find(TRUE)) {
371 $action = CRM_Core_Action
::UPDATE
;
372 $ids['contact'] = $contactID;
373 $ids['contactTarget'] = $employerID;
374 $ids['relationship'] = $relationship->id
;
375 CRM_Contact_BAO_Relationship
::setIsActive($relationship->id
, TRUE);
377 $relationship->free();
380 //need to handle related meberships. CRM-3792
381 if ($previousEmpID != $employerID) {
382 CRM_Contact_BAO_Relationship
::relatedMemberships($contactID, $relationshipParams, $ids, $action);
387 * Set current employer id and organization name.
389 * @param array $currentEmployerParams
390 * Associated array of contact id and its employer id.
392 public static function setCurrentEmployer($currentEmployerParams) {
393 foreach ($currentEmployerParams as $contactId => $orgId) {
394 $query = "UPDATE civicrm_contact contact_a,civicrm_contact contact_b
395 SET contact_a.employer_id=contact_b.id, contact_a.organization_name=contact_b.organization_name
396 WHERE contact_a.id ={$contactId} AND contact_b.id={$orgId}; ";
398 //FIXME : currently civicrm mysql_query support only single statement
399 //execution, though mysql 5.0 support multiple statement execution.
400 $dao = CRM_Core_DAO
::executeQuery($query);
405 * Update cached current employer name.
407 * @param int $organizationId
408 * Current employer id.
410 public static function updateCurrentEmployer($organizationId) {
411 $query = "UPDATE civicrm_contact contact_a,civicrm_contact contact_b
412 SET contact_a.organization_name=contact_b.organization_name
413 WHERE contact_a.employer_id=contact_b.id AND contact_b.id={$organizationId}; ";
415 $dao = CRM_Core_DAO
::executeQuery($query);
419 * Clear cached current employer name.
421 * @param int $contactId
422 * Contact id ( mostly individual contact id).
423 * @param int $employerId
424 * Contact id ( mostly organization contact id).
426 public static function clearCurrentEmployer($contactId, $employerId = NULL) {
427 $query = "UPDATE civicrm_contact
428 SET organization_name=NULL, employer_id = NULL
429 WHERE id={$contactId}; ";
431 $dao = CRM_Core_DAO
::executeQuery($query);
433 // need to handle related meberships. CRM-3792
435 //1. disable corresponding relationship.
436 //2. delete related membership.
438 //get the relationship type id of "Employee of"
439 $relTypeId = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_RelationshipType', 'Employee of', 'id', 'name_a_b');
441 CRM_Core_Error
::fatal(ts("You seem to have deleted the relationship type 'Employee of'"));
443 $relMembershipParams['relationship_type_id'] = $relTypeId . '_a_b';
444 $relMembershipParams['contact_check'][$employerId] = 1;
446 //get relationship id.
447 if (CRM_Contact_BAO_Relationship
::checkDuplicateRelationship($relMembershipParams, $contactId, $employerId)) {
448 $relationship = new CRM_Contact_DAO_Relationship();
449 $relationship->contact_id_a
= $contactId;
450 $relationship->contact_id_b
= $employerId;
451 $relationship->relationship_type_id
= $relTypeId;
453 if ($relationship->find(TRUE)) {
454 CRM_Contact_BAO_Relationship
::setIsActive($relationship->id
, FALSE);
455 CRM_Contact_BAO_Relationship
::relatedMemberships($contactId, $relMembershipParams,
457 CRM_Core_Action
::DELETE
460 $relationship->free();
466 * Build form for related contacts / on behalf of organization.
468 * @param CRM_Core_Form $form
469 * @param string $contactType
471 * @param int $countryID
472 * @param int $stateID
473 * @param string $title
477 public static function buildOnBehalfForm(&$form, $contactType, $countryID, $stateID, $title) {
479 $config = CRM_Core_Config
::singleton();
481 $form->assign('contact_type', $contactType);
482 $form->assign('fieldSetTitle', $title);
483 $form->assign('contactEditMode', TRUE);
485 $attributes = CRM_Core_DAO
::getAttribute('CRM_Contact_DAO_Contact');
486 if ($form->_contactId
) {
487 $form->assign('orgId', $form->_contactId
);
490 switch ($contactType) {
492 $form->add('text', 'organization_name', ts('Organization Name'), $attributes['organization_name'], TRUE);
496 $form->add('text', 'household_name', ts('Household Name'), $attributes['household_name']);
501 $form->addElement('select', 'prefix_id', ts('Prefix'),
502 ['' => ts('- prefix -')] + CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'prefix_id')
504 $form->addElement('text', 'first_name', ts('First Name'),
505 $attributes['first_name']
507 $form->addElement('text', 'middle_name', ts('Middle Name'),
508 $attributes['middle_name']
510 $form->addElement('text', 'last_name', ts('Last Name'),
511 $attributes['last_name']
513 $form->addElement('select', 'suffix_id', ts('Suffix'),
514 ['' => ts('- suffix -')] + CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'suffix_id')
518 $addressSequence = $config->addressSequence();
519 $form->assign('addressSequence', array_fill_keys($addressSequence, 1));
522 $form->addElement('text',
525 CRM_Core_DAO
::getAttribute('CRM_Core_DAO_Phone',
530 $form->addElement('text',
533 CRM_Core_DAO
::getAttribute('CRM_Core_DAO_Email',
537 //build the address block
538 CRM_Contact_Form_Edit_Address
::buildQuickForm($form);
542 * Clear cache employer name and employer id
543 * of all employee when employer get deleted.
545 * @param int $employerId
546 * Contact id of employer ( organization id ).
548 public static function clearAllEmployee($employerId) {
550 UPDATE civicrm_contact
551 SET organization_name=NULL, employer_id = NULL
552 WHERE employer_id={$employerId}; ";
554 $dao = CRM_Core_DAO
::executeQuery($query);
558 * Given an array of contact ids this function will return array with links to view contact page.
560 * @param array $contactIDs
561 * Associated contact id's.
562 * @param bool $addViewLink
563 * @param bool $addEditLink
564 * @param int $originalId
565 * Associated with the contact which is edited.
569 * returns array with links to contact view
571 public static function formatContactIDSToLinks($contactIDs, $addViewLink = TRUE, $addEditLink = TRUE, $originalId = NULL) {
573 if (!is_array($contactIDs) ||
empty($contactIDs)) {
574 return $contactLinks;
577 // does contact has sufficient permissions.
579 'view' => 'view all contacts',
580 'edit' => 'edit all contacts',
581 'merge' => 'merge duplicate contacts',
584 $permissionedContactIds = [];
585 foreach ($permissions as $task => $permission) {
587 if (CRM_Core_Permission
::check($permission)) {
588 foreach ($contactIDs as $contactId) {
589 $permissionedContactIds[$contactId][$task] = TRUE;
594 // check permission on acl basis.
595 if (in_array($task, [
599 $aclPermission = CRM_Core_Permission
::VIEW
;
600 if ($task == 'edit') {
601 $aclPermission = CRM_Core_Permission
::EDIT
;
603 foreach ($contactIDs as $contactId) {
604 if (CRM_Contact_BAO_Contact_Permission
::allow($contactId, $aclPermission)) {
605 $permissionedContactIds[$contactId][$task] = TRUE;
611 // retrieve display names for all contacts
613 SELECT c.id, c.display_name, c.contact_type, ce.email
614 FROM civicrm_contact c
615 LEFT JOIN civicrm_email ce ON ( ce.contact_id=c.id AND ce.is_primary = 1 )
616 WHERE c.id IN (' . implode(',', $contactIDs) . ' ) LIMIT 20';
618 $dao = CRM_Core_DAO
::executeQuery($query);
620 $contactLinks['msg'] = NULL;
622 while ($dao->fetch()) {
624 $contactLinks['rows'][$i]['display_name'] = $dao->display_name
;
625 $contactLinks['rows'][$i]['primary_email'] = $dao->email
;
627 // get the permission for current contact id.
628 $hasPermissions = CRM_Utils_Array
::value($dao->id
, $permissionedContactIds);
629 if (!is_array($hasPermissions) ||
empty($hasPermissions)) {
634 // do check for view.
635 if (array_key_exists('view', $hasPermissions)) {
636 $contactLinks['rows'][$i]['view'] = '<a class="action-item" href="' . CRM_Utils_System
::url('civicrm/contact/view', 'reset=1&cid=' . $dao->id
) . '" target="_blank">' . ts('View') . '</a>';
637 if (!$contactLinks['msg']) {
638 $contactLinks['msg'] = 'view';
641 if (array_key_exists('edit', $hasPermissions)) {
642 $contactLinks['rows'][$i]['edit'] = '<a class="action-item" href="' . CRM_Utils_System
::url('civicrm/contact/add', 'reset=1&action=update&cid=' . $dao->id
) . '" target="_blank">' . ts('Edit') . '</a>';
643 if (!$contactLinks['msg'] ||
$contactLinks['msg'] != 'merge') {
644 $contactLinks['msg'] = 'edit';
647 if (!empty($originalId) && array_key_exists('merge', $hasPermissions)) {
648 $rgBao = new CRM_Dedupe_BAO_RuleGroup();
649 $rgBao->contact_type
= $dao->contact_type
;
650 $rgBao->used
= 'Supervised';
651 if ($rgBao->find(TRUE)) {
654 if ($rgid && isset($dao->id
)) {
655 //get an url to merge the contact
656 $contactLinks['rows'][$i]['merge'] = '<a class="action-item" href="' . CRM_Utils_System
::url('civicrm/contact/merge', "reset=1&cid=" . $originalId . '&oid=' . $dao->id
. '&action=update&rgid=' . $rgid) . '">' . ts('Merge') . '</a>';
657 $contactLinks['msg'] = 'merge';
664 return $contactLinks;
668 * This function retrieve component related contact information.
670 * @param array $componentIds
671 * Array of component Ids.
672 * @param string $componentName
673 * @param array $returnProperties
674 * Array of return elements.
677 * array of contact info.
679 public static function contactDetails($componentIds, $componentName, $returnProperties = []) {
680 $contactDetails = [];
681 if (empty($componentIds) ||
682 !in_array($componentName, ['CiviContribute', 'CiviMember', 'CiviEvent', 'Activity', 'CiviCase'])
684 return $contactDetails;
687 if (empty($returnProperties)) {
688 $autocompleteContactSearch = CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
689 'contact_autocomplete_options'
691 $returnProperties = array_fill_keys(array_merge(['sort_name'],
692 array_keys($autocompleteContactSearch)
697 if ($componentName == 'CiviContribute') {
698 $compTable = 'civicrm_contribution';
700 elseif ($componentName == 'CiviMember') {
701 $compTable = 'civicrm_membership';
703 elseif ($componentName == 'Activity') {
704 $compTable = 'civicrm_activity';
705 $activityContacts = CRM_Activity_BAO_ActivityContact
::buildOptions('record_type_id', 'validate');
707 elseif ($componentName == 'CiviCase') {
708 $compTable = 'civicrm_case';
711 $compTable = 'civicrm_participant';
714 $select = $from = [];
715 foreach ($returnProperties as $property => $ignore) {
716 $value = (in_array($property, [
720 ])) ?
'address' : $property;
723 if ($componentName == 'Activity') {
724 $sourceID = CRM_Utils_Array
::key('Activity Source', $activityContacts);
725 $select[] = "contact.$property as $property";
727 INNER JOIN civicrm_activity_contact acs ON (acs.activity_id = {$compTable}.id AND acs.record_type_id = {$sourceID})
728 INNER JOIN civicrm_contact contact ON ( contact.id = acs.contact_id )";
730 elseif ($componentName == 'CiviCase') {
731 $select[] = "contact.$property as $property";
733 INNER JOIN civicrm_case_contact ccs ON (ccs.case_id = {$compTable}.id)
734 INNER JOIN civicrm_contact contact ON ( contact.id = ccs.contact_id )";
737 $select[] = "$property as $property";
738 $from[$value] = "INNER JOIN civicrm_contact contact ON ( contact.id = $compTable.contact_id )";
742 case 'target_sort_name':
743 $targetID = CRM_Utils_Array
::key('Activity Targets', $activityContacts);
744 $select[] = "contact_target.sort_name as $property";
746 INNER JOIN civicrm_activity_contact act ON (act.activity_id = {$compTable}.id AND act.record_type_id = {$targetID})
747 INNER JOIN civicrm_contact contact_target ON ( contact_target.id = act.contact_id )";
753 case 'street_address':
755 $select[] = "$property as $property";
756 // Grab target contact properties if this is for activity
757 if ($componentName == 'Activity') {
758 $from[$value] = "LEFT JOIN civicrm_{$value} {$value} ON ( contact_target.id = {$value}.contact_id AND {$value}.is_primary = 1 ) ";
761 $from[$value] = "LEFT JOIN civicrm_{$value} {$value} ON ( contact.id = {$value}.contact_id AND {$value}.is_primary = 1 ) ";
766 case 'state_province':
767 $select[] = "{$property}.name as $property";
768 if (!in_array('address', $from)) {
769 // Grab target contact properties if this is for activity
770 if ($componentName == 'Activity') {
771 $from['address'] = 'LEFT JOIN civicrm_address address ON ( contact_target.id = address.contact_id AND address.is_primary = 1) ';
774 $from['address'] = 'LEFT JOIN civicrm_address address ON ( contact.id = address.contact_id AND address.is_primary = 1) ';
777 $from[$value] = " LEFT JOIN civicrm_{$value} {$value} ON ( address.{$value}_id = {$value}.id ) ";
782 //finally retrieve contact details.
783 if (!empty($select) && !empty($from)) {
784 $fromClause = implode(' ', $from);
785 $selectClause = implode(', ', $select);
786 $whereClause = "{$compTable}.id IN (" . implode(',', $componentIds) . ')';
787 $groupBy = CRM_Contact_BAO_Query
::getGroupByFromSelectColumns($select, ["{$compTable}.id", 'contact.id']);
790 SELECT contact.id as contactId, $compTable.id as componentId, $selectClause
791 FROM $compTable as $compTable $fromClause
795 $contact = CRM_Core_DAO
::executeQuery($query);
796 while ($contact->fetch()) {
797 $contactDetails[$contact->componentId
]['contact_id'] = $contact->contactId
;
798 foreach ($returnProperties as $property => $ignore) {
799 $contactDetails[$contact->componentId
][$property] = $contact->$property;
805 return $contactDetails;
809 * Function handles shared contact address processing.
810 * In this function we just modify submitted values so that new address created for the user
811 * has same address as shared contact address. We copy the address so that search etc will be
812 * much more efficient.
814 * @param array $address
815 * This is associated array which contains submitted form values.
817 public static function processSharedAddress(&$address) {
818 if (!is_array($address)) {
822 // In create mode sharing a contact's address is pretty straight forward.
823 // In update mode we should check if the user stops sharing. If yes:
824 // - Set the master_id to an empty value
825 // Normal update process will automatically create new address with submitted values
827 // 1. loop through entire submitted address array
828 $skipFields = ['is_primary', 'location_type_id', 'is_billing', 'master_id', 'update_current_employer'];
829 foreach ($address as & $values) {
830 // 2. check if "Use another contact's address" is checked, if not continue
831 // Additionally, if master_id is set (address was shared), set master_id to empty value.
832 if (empty($values['use_shared_address'])) {
833 if (!empty($values['master_id'])) {
834 $values['master_id'] = '';
839 // Set update_current_employer checkbox value
840 $values['update_current_employer'] = !empty($values['update_current_employer']);
842 // 3. get the address details for master_id
843 $masterAddress = new CRM_Core_BAO_Address();
844 $masterAddress->id
= CRM_Utils_Array
::value('master_id', $values);
845 $masterAddress->find(TRUE);
847 // 4. modify submitted params and update it with shared contact address
848 // make sure you preserve specific form values like location type, is_primary_ is_billing, master_id
849 // CRM-10336: Also empty any fields from the existing address block if they don't exist in master (otherwise they will persist)
850 foreach ($values as $field => $submittedValue) {
851 if (!in_array($field, $skipFields)) {
852 if (isset($masterAddress->$field)) {
853 $values[$field] = $masterAddress->$field;
856 $values[$field] = '';
861 //5. modify the params to include county_id if it exist in master contact.
862 // CRM-16152: This is a hack since QF does not submit disabled field.
863 if (!empty($masterAddress->county_id
) && empty($values['county_id'])) {
864 $values['county_id'] = $masterAddress->county_id
;
870 * Get the list of contact name give address associated array.
872 * @param array $addresses
873 * Associated array of.
876 * associated array of contact names
878 public static function getAddressShareContactNames(&$addresses) {
880 // get the list of master id's for address
881 $masterAddressIds = [];
882 foreach ($addresses as $key => $addressValue) {
883 if (!empty($addressValue['master_id'])) {
884 $masterAddressIds[] = $addressValue['master_id'];
888 if (!empty($masterAddressIds)) {
889 $query = 'SELECT ca.id, cc.display_name, cc.id as cid, cc.is_deleted
890 FROM civicrm_contact cc
891 INNER JOIN civicrm_address ca ON cc.id = ca.contact_id
892 WHERE ca.id IN ( ' . implode(',', $masterAddressIds) . ')';
893 $dao = CRM_Core_DAO
::executeQuery($query);
895 while ($dao->fetch()) {
896 $contactViewUrl = CRM_Utils_System
::url('civicrm/contact/view', "reset=1&cid={$dao->cid}");
897 $contactNames[$dao->id
] = [
898 'name' => "<a href='{$contactViewUrl}'>{$dao->display_name}</a>",
899 'is_deleted' => $dao->is_deleted
,
900 'contact_id' => $dao->cid
,
904 return $contactNames;
908 * Clear the contact cache so things are kosher. We started off being super aggressive with clearing
909 * caches, but are backing off from this with every release. Compromise between ease of coding versus
910 * performance versus being accurate at that very instant
912 * @param bool $isEmptyPrevNextTable
913 * Should the civicrm_prev_next table be cleared of any contact entries.
914 * This is currently done from import but not other places and would
915 * likely affect user experience in unexpected ways. Existing behaviour retained
918 public static function clearContactCaches($isEmptyPrevNextTable = FALSE) {
919 if (!CRM_Core_Config
::isPermitCacheFlushMode()) {
922 if ($isEmptyPrevNextTable) {
923 // These two calls are redundant in default deployments, but they're
924 // meaningful if "prevnext" is memory-backed.
925 Civi
::service('prevnext')->deleteItem();
926 CRM_Core_BAO_PrevNextCache
::deleteItem();
928 // clear acl cache if any.
929 CRM_ACL_BAO_Cache
::resetCache();
930 CRM_Contact_BAO_GroupContactCache
::opportunisticCacheFlush();
934 * @param array $params
938 public static function updateGreeting($params) {
939 $contactType = $params['ct'];
940 $greeting = $params['gt'];
941 $valueID = $id = CRM_Utils_Array
::value('id', $params);
942 $force = CRM_Utils_Array
::value('force', $params);
943 $limit = CRM_Utils_Array
::value('limit', $params);
945 // if valueID is not passed use default value
947 $valueID = $id = self
::defaultGreeting($contactType, $greeting);
951 'contact_type' => $contactType,
952 'greeting_type' => $greeting,
955 $allGreetings = CRM_Core_PseudoConstant
::greeting($filter);
956 $originalGreetingString = $greetingString = CRM_Utils_Array
::value($valueID, $allGreetings);
957 if (!$greetingString) {
958 CRM_Core_Error
::fatal(ts('Incorrect greeting value id %1, or no default greeting for this contact type and greeting type.', [1 => $valueID]));
961 // build return properties based on tokens
962 $greetingTokens = CRM_Utils_Token
::getTokens($greetingString);
963 $tokens = CRM_Utils_Array
::value('contact', $greetingTokens);
964 $greetingsReturnProperties = [];
965 if (is_array($tokens)) {
966 $greetingsReturnProperties = array_fill_keys(array_values($tokens), 1);
969 // Process ALL contacts only when force=1 or force=2 is passed. Else only contacts with NULL greeting or addressee value are updated.
970 $processAll = $processOnlyIdSet = FALSE;
974 elseif ($force == 2) {
975 $processOnlyIdSet = TRUE;
978 //FIXME : apiQuery should handle these clause.
979 $filterContactFldIds = $filterIds = [];
980 $idFldName = $displayFldName = NULL;
981 if (in_array($greeting, CRM_Contact_BAO_Contact
::$_greetingTypes)) {
982 $idFldName = $greeting . '_id';
983 $displayFldName = $greeting . '_display';
987 $queryParams = [1 => [$contactType, 'String']];
989 // if $force == 1 then update all contacts else only
990 // those with NULL greeting or addressee value CRM-9476
992 $sql = "SELECT DISTINCT id, $idFldName FROM civicrm_contact WHERE contact_type = %1 ";
996 SELECT DISTINCT id, $idFldName
998 WHERE contact_type = %1
999 AND ({$idFldName} IS NULL
1000 OR ( {$idFldName} IS NOT NULL AND ({$displayFldName} IS NULL OR {$displayFldName} = '')) )";
1004 $sql .= " LIMIT 0, %2";
1005 $queryParams +
= [2 => [$limit, 'Integer']];
1008 $dao = CRM_Core_DAO
::executeQuery($sql, $queryParams);
1009 while ($dao->fetch()) {
1010 $filterContactFldIds[$dao->id
] = $dao->$idFldName;
1012 if (!CRM_Utils_System
::isNull($dao->$idFldName)) {
1013 $filterIds[$dao->id
] = $dao->$idFldName;
1018 if (empty($filterContactFldIds)) {
1019 $filterContactFldIds[] = 0;
1022 // retrieve only required contact information
1023 $extraParams[] = ['contact_type', '=', $contactType, 0, 0];
1024 // we do token replacement in the replaceGreetingTokens hook
1025 list($greetingDetails) = CRM_Utils_Token
::getTokenDetails(array_keys($filterContactFldIds),
1026 $greetingsReturnProperties,
1027 FALSE, FALSE, $extraParams
1029 // perform token replacement and build update SQL
1031 $cacheFieldQuery = "UPDATE civicrm_contact SET {$greeting}_display = CASE id ";
1032 foreach ($greetingDetails as $contactID => $contactDetails) {
1034 !array_key_exists($contactID, $filterContactFldIds)
1039 if ($processOnlyIdSet && !array_key_exists($contactID, $filterIds)) {
1044 $greetingString = $originalGreetingString;
1045 $contactIds[] = $contactID;
1048 if ($greetingBuffer = CRM_Utils_Array
::value($filterContactFldIds[$contactID], $allGreetings)) {
1049 $greetingString = $greetingBuffer;
1053 self
::processGreetingTemplate($greetingString, $contactDetails, $contactID, 'CRM_UpdateGreeting');
1054 $greetingString = CRM_Core_DAO
::escapeString($greetingString);
1055 $cacheFieldQuery .= " WHEN {$contactID} THEN '{$greetingString}' ";
1057 $allContactIds[] = $contactID;
1060 if (!empty($allContactIds)) {
1061 $cacheFieldQuery .= " ELSE {$greeting}_display
1063 if (!empty($contactIds)) {
1064 // need to update greeting _id field.
1065 // reset greeting _custom
1066 $resetCustomGreeting = '';
1067 if ($valueID != 4) {
1068 $resetCustomGreeting = ", {$greeting}_custom = NULL ";
1072 UPDATE civicrm_contact
1073 SET {$greeting}_id = {$valueID}
1074 {$resetCustomGreeting}
1075 WHERE id IN (" . implode(',', $contactIds) . ")";
1076 CRM_Core_DAO
::executeQuery($queryString);
1079 // now update cache field
1080 CRM_Core_DAO
::executeQuery($cacheFieldQuery);
1085 * Fetch the default greeting for a given contact type.
1087 * @param string $contactType
1089 * @param string $greetingType
1094 public static function defaultGreeting($contactType, $greetingType) {
1095 $contactTypeFilters = [
1098 'Organization' => 3,
1100 if (!isset($contactTypeFilters[$contactType])) {
1103 $filter = $contactTypeFilters[$contactType];
1105 $id = CRM_Core_OptionGroup
::values($greetingType, NULL, NULL, NULL,
1106 " AND is_default = 1 AND (filter = {$filter} OR filter = 0)",
1110 return current($id);
1115 * Get the tokens that will need to be resolved to populate the contact's greetings.
1117 * @param array $contactParams
1120 * Array of tokens. The ALL ke
1122 public static function getTokensRequiredForContactGreetings($contactParams) {
1124 foreach (['addressee', 'email_greeting', 'postal_greeting'] as $greeting) {
1126 if (!empty($contactParams[$greeting . '_id'])) {
1127 $string = CRM_Core_PseudoConstant
::getLabel('CRM_Contact_BAO_Contact', $greeting . '_id', $contactParams[$greeting . '_id']);
1129 $string = isset($contactParams[$greeting . '_custom']) ?
$contactParams[$greeting . '_custom'] : $string;
1130 if (empty($string)) {
1131 $tokens[$greeting] = [];
1134 $tokens[$greeting] = CRM_Utils_Token
::getTokens($string);
1137 $allTokens = array_merge_recursive($tokens['addressee'], $tokens['email_greeting'], $tokens['postal_greeting']);
1138 $tokens['all'] = $allTokens;
1143 * Process a greeting template string to produce the individualised greeting text.
1145 * This works just like message templates for mailings:
1146 * the template is processed with the token substitution mechanism,
1147 * to supply the individual contact data;
1148 * and it is also processed with Smarty,
1149 * to allow for conditionals etc. based on the contact data.
1151 * Note: We don't pass any variables to Smarty --
1152 * all variable data is inserted into the input string
1153 * by the token substitution mechanism,
1154 * before Smarty is invoked.
1156 * @param string $templateString
1157 * The greeting template string with contact tokens + Smarty syntax.
1159 * @param array $contactDetails
1160 * @param int $contactID
1161 * @param string $className
1163 public static function processGreetingTemplate(&$templateString, $contactDetails, $contactID, $className) {
1164 CRM_Utils_Token
::replaceGreetingTokens($templateString, $contactDetails, $contactID, $className, TRUE);
1166 $smarty = CRM_Core_Smarty
::singleton();
1167 $templateString = $smarty->fetch("string:$templateString");
1171 * Determine if a contact ID is real/valid.
1173 * @param int $contactId
1174 * The hypothetical contact ID
1177 public static function isContactId($contactId) {
1179 // ensure that this is a valid contact id (for session inconsistency rules)
1180 $cid = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',