3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
35 class CRM_Contact_BAO_Contact_Utils
{
38 * Given a contact type, get the contact image
40 * @param string $contactType
42 * @param bool $urlOnly
43 * If we need to return only image url.
44 * @param int $contactId
46 * @param bool $addProfileOverlay
47 * If profile overlay class should be added.
52 public static function getImage($contactType, $urlOnly = FALSE, $contactId = NULL, $addProfileOverlay = TRUE) {
53 static $imageInfo = array();
55 $contactType = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, trim($contactType, CRM_Core_DAO
::VALUE_SEPARATOR
));
56 $contactType = $contactType[0];
58 if (!array_key_exists($contactType, $imageInfo)) {
59 $imageInfo[$contactType] = array();
62 $params = array('name' => $contactType);
63 CRM_Contact_BAO_ContactType
::retrieve($params, $typeInfo);
65 if (!empty($typeInfo['image_URL'])) {
66 $imageUrl = $typeInfo['image_URL'];
67 $config = CRM_Core_Config
::singleton();
69 if (!preg_match("/^(\/|(http(s)?:)).+$/i", $imageUrl)) {
70 $imageUrl = $config->resourceBase
. $imageUrl;
72 $imageInfo[$contactType]['image'] = "<div class=\"icon crm-icon {$typeInfo['name']}-icon\" style=\"background: url('{$imageUrl}')\" title=\"{$contactType}\"></div>";
73 $imageInfo[$contactType]['url'] = $imageUrl;
76 $isSubtype = (array_key_exists('parent_id', $typeInfo) &&
77 $typeInfo['parent_id']
81 $type = CRM_Contact_BAO_ContactType
::getBasicType($typeInfo['name']) . '-subtype';
84 $type = CRM_Utils_Array
::value('name', $typeInfo);
87 // do not add title since it hides contact name
88 if ($addProfileOverlay) {
89 $imageInfo[$contactType]['image'] = "<div class=\"icon crm-icon {$type}-icon\"></div>";
92 $imageInfo[$contactType]['image'] = "<div class=\"icon crm-icon {$type}-icon\" title=\"{$contactType}\"></div>";
94 $imageInfo[$contactType]['url'] = NULL;
98 if ($addProfileOverlay) {
99 static $summaryOverlayProfileId = NULL;
100 if (!$summaryOverlayProfileId) {
101 $summaryOverlayProfileId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', 'summary_overlay', 'id', 'name');
104 $profileURL = CRM_Utils_System
::url('civicrm/profile/view',
105 "reset=1&gid={$summaryOverlayProfileId}&id={$contactId}&snippet=4"
108 $imageInfo[$contactType]['summary-link'] = '<a href="' . $profileURL . '" class="crm-summary-link">' . $imageInfo[$contactType]['image'] . '</a>';
111 $imageInfo[$contactType]['summary-link'] = $imageInfo[$contactType]['image'];
114 return $urlOnly ?
$imageInfo[$contactType]['url'] : $imageInfo[$contactType]['summary-link'];
118 * Function check for mix contact ids(individual+household etc...)
120 * @param array $contactIds
121 * Array of contact ids.
123 * @return boolen true or false true if mix contact array else fale
127 public static function checkContactType(&$contactIds) {
128 if (empty($contactIds)) {
132 $idString = implode(',', $contactIds);
134 SELECT count( DISTINCT contact_type )
136 WHERE id IN ( $idString )
138 $count = CRM_Core_DAO
::singleValueQuery($query,
139 CRM_Core_DAO
::$_nullArray
141 return $count > 1 ?
TRUE : FALSE;
145 * Generate a checksum for a $entityId of type $entityType
147 * @param int $entityId
149 * Timestamp that checksum was generated.
151 * Life of this checksum in hours/ 'inf' for infinite.
152 * @param string $hash
153 * Contact hash, if sent, prevents a query in inner loop.
155 * @param string $entityType
156 * @param null $hashSize
158 * @return array ( $cs, $ts, $live )
161 public static function generateChecksum($entityId, $ts = NULL, $live = NULL, $hash = NULL, $entityType = 'contact', $hashSize = NULL) {
162 // return a warning message if we dont get a entityId
163 // this typically happens when we do a message preview
164 // or an anon mailing view - CRM-8298
166 return 'invalidChecksum';
170 if ($entityType == 'contact') {
171 $hash = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
175 elseif ($entityType == 'mailing') {
176 $hash = CRM_Core_DAO
::getFieldValue('CRM_Mailing_DAO_Mailing',
183 $hash = md5(uniqid(rand(), TRUE));
185 $hash = substr($hash, 0, $hashSize);
188 if ($entityType == 'contact') {
189 CRM_Core_DAO
::setFieldValue('CRM_Contact_DAO_Contact',
194 elseif ($entityType == 'mailing') {
195 CRM_Core_DAO
::setFieldValue('CRM_Mailing_DAO_Mailing',
207 $days = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
215 $cs = md5("{$hash}_{$entityId}_{$ts}_{$live}");
216 return "{$cs}_{$ts}_{$live}";
220 * Make sure the checksum is valid for the passed in contactID
222 * @param int $contactID
223 * @param string $inputCheck
224 * Checksum to match against.
226 * @return boolean true if valid, else false
229 public static function validChecksum($contactID, $inputCheck) {
231 $input = CRM_Utils_System
::explode('_', $inputCheck, 3);
233 $inputCS = CRM_Utils_Array
::value(0, $input);
234 $inputTS = CRM_Utils_Array
::value(1, $input);
235 $inputLF = CRM_Utils_Array
::value(2, $input);
237 $check = self
::generateChecksum($contactID, $inputTS, $inputLF);
239 if ($check != $inputCheck) {
243 // no life limit for checksum
244 if ($inputLF == 'inf') {
248 // checksum matches so now check timestamp
250 return ($inputTS +
($inputLF * 60 * 60) >= $now);
254 * Get the count of contact loctions
256 * @param int $contactId
259 * @return int $locationCount max locations for the contact
262 public static function maxLocations($contactId) {
263 $contactLocations = array();
265 // find number of location blocks for this contact and adjust value accordinly
266 // get location type from email
268 ( SELECT location_type_id FROM civicrm_email WHERE contact_id = {$contactId} )
270 ( SELECT location_type_id FROM civicrm_phone WHERE contact_id = {$contactId} )
272 ( SELECT location_type_id FROM civicrm_im WHERE contact_id = {$contactId} )
274 ( SELECT location_type_id FROM civicrm_address WHERE contact_id = {$contactId} )
276 $dao = CRM_Core_DAO
::executeQuery($query, CRM_Core_DAO
::$_nullArray);
281 * Create Current employer relationship for a individual
283 * @param int $contactID
284 * Contact id of the individual.
285 * @param $organization
287 * @param int $previousEmployerID
288 * @param bool $newContact
292 public static function createCurrentEmployerRelationship($contactID, $organization, $previousEmployerID = NULL, $newContact = FALSE) {
293 //if organization name is passed. CRM-15368,CRM-15547
294 if ($organization && !is_numeric($organization)) {
295 $organizationParams['organization_name'] = $organization;
296 $dedupeParams = CRM_Dedupe_Finder
::formatParams($organizationParams, 'Organization');
298 $dedupeParams['check_permission'] = FALSE;
299 $dupeIDs = CRM_Dedupe_Finder
::dupesByParams($dedupeParams, 'Organization', 'Unsupervised');
301 if (is_array($dupeIDs) && !empty($dupeIDs)) {
302 // we should create relationship only w/ first org CRM-4193
303 foreach ($dupeIDs as $orgId) {
304 $organization = $orgId;
309 //create new organization
311 'contact_type' => 'Organization',
312 'organization_name' => $organization,
314 $org = CRM_Contact_BAO_Contact
::create($newOrg);
315 $organization = $org->id
;
319 if ($organization && is_numeric($organization)) {
320 $cid = array('contact' => $contactID);
322 // get the relationship type id of "Employee of"
323 $relTypeId = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_RelationshipType', 'Employee of', 'id', 'name_a_b');
325 CRM_Core_Error
::fatal(ts("You seem to have deleted the relationship type 'Employee of'"));
328 // create employee of relationship
329 $relationshipParams = array(
331 'relationship_type_id' => $relTypeId . '_a_b',
332 'contact_check' => array($organization => TRUE),
334 list($valid, $invalid, $duplicate,
335 $saved, $relationshipIds
336 ) = CRM_Contact_BAO_Relationship
::createMultiple($relationshipParams, $cid);
339 // In case we change employer, clean previous employer related records.
340 if (!$previousEmployerID && !$newContact) {
341 $previousEmployerID = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $contactID, 'employer_id');
343 if ($previousEmployerID &&
344 $previousEmployerID != $organization
346 self
::clearCurrentEmployer($contactID, $previousEmployerID);
349 // set current employer
350 self
::setCurrentEmployer(array($contactID => $organization));
352 $relationshipParams['relationship_ids'] = $relationshipIds;
353 // handle related meberships. CRM-3792
354 self
::currentEmployerRelatedMembership($contactID, $organization, $relationshipParams, $duplicate, $previousEmployerID);
359 * Create related memberships for current employer
361 * @param int $contactID
362 * Contact id of the individual.
363 * @param int $employerID
364 * Contact id of the organization.
365 * @param array $relationshipParams
366 * Relationship params.
367 * @param bool $duplicate
368 * Are we triggered existing relationship.
370 * @param int $previousEmpID
372 * @throws CiviCRM_API3_Exception
375 public static function currentEmployerRelatedMembership($contactID, $employerID, $relationshipParams, $duplicate = FALSE, $previousEmpID = NULL) {
377 $action = CRM_Core_Action
::ADD
;
379 //we do not know that triggered relationship record is active.
381 $relationship = new CRM_Contact_DAO_Relationship();
382 $relationship->contact_id_a
= $contactID;
383 $relationship->contact_id_b
= $employerID;
384 $relationship->relationship_type_id
= $relationshipParams['relationship_type_id'];
385 if ($relationship->find(TRUE)) {
386 $action = CRM_Core_Action
::UPDATE
;
387 $ids['contact'] = $contactID;
388 $ids['contactTarget'] = $employerID;
389 $ids['relationship'] = $relationship->id
;
390 CRM_Contact_BAO_Relationship
::setIsActive($relationship->id
, TRUE);
392 $relationship->free();
395 //need to handle related meberships. CRM-3792
396 if ($previousEmpID != $employerID) {
397 CRM_Contact_BAO_Relationship
::relatedMemberships($contactID, $relationshipParams, $ids, $action);
402 * Set current employer id and organization name
404 * @param array $currentEmployerParams
405 * Associated array of contact id and its employer id.
408 public static function setCurrentEmployer($currentEmployerParams) {
409 foreach ($currentEmployerParams as $contactId => $orgId) {
410 $query = "UPDATE civicrm_contact contact_a,civicrm_contact contact_b
411 SET contact_a.employer_id=contact_b.id, contact_a.organization_name=contact_b.organization_name
412 WHERE contact_a.id ={$contactId} AND contact_b.id={$orgId}; ";
414 //FIXME : currently civicrm mysql_query support only single statement
415 //execution, though mysql 5.0 support multiple statement execution.
416 $dao = CRM_Core_DAO
::executeQuery($query);
421 * Update cached current employer name
423 * @param int $organizationId
424 * Current employer id.
427 public static function updateCurrentEmployer($organizationId) {
428 $query = "UPDATE civicrm_contact contact_a,civicrm_contact contact_b
429 SET contact_a.organization_name=contact_b.organization_name
430 WHERE contact_a.employer_id=contact_b.id AND contact_b.id={$organizationId}; ";
432 $dao = CRM_Core_DAO
::executeQuery($query);
436 * Clear cached current employer name
438 * @param int $contactId
439 * Contact id ( mostly individual contact id).
440 * @param int $employerId
441 * Contact id ( mostly organization contact id).
444 public static function clearCurrentEmployer($contactId, $employerId = NULL) {
445 $query = "UPDATE civicrm_contact
446 SET organization_name=NULL, employer_id = NULL
447 WHERE id={$contactId}; ";
449 $dao = CRM_Core_DAO
::executeQuery($query);
451 // need to handle related meberships. CRM-3792
453 //1. disable corresponding relationship.
454 //2. delete related membership.
456 //get the relationship type id of "Employee of"
457 $relTypeId = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_RelationshipType', 'Employee of', 'id', 'name_a_b');
459 CRM_Core_Error
::fatal(ts("You seem to have deleted the relationship type 'Employee of'"));
461 $relMembershipParams['relationship_type_id'] = $relTypeId . '_a_b';
462 $relMembershipParams['contact_check'][$employerId] = 1;
464 //get relationship id.
465 if (CRM_Contact_BAO_Relationship
::checkDuplicateRelationship($relMembershipParams, $contactId, $employerId)) {
466 $relationship = new CRM_Contact_DAO_Relationship();
467 $relationship->contact_id_a
= $contactId;
468 $relationship->contact_id_b
= $employerId;
469 $relationship->relationship_type_id
= $relTypeId;
471 if ($relationship->find(TRUE)) {
472 CRM_Contact_BAO_Relationship
::setIsActive($relationship->id
, FALSE);
473 CRM_Contact_BAO_Relationship
::relatedMemberships($contactId, $relMembershipParams,
475 CRM_Core_Action
::DELETE
478 $relationship->free();
484 * Build form for related contacts / on behalf of organization.
486 * @param CRM_Core_Form $form
487 * @param string $contactType
489 * @param int $countryID
490 * @param int $stateID
491 * @param string $title
496 public static function buildOnBehalfForm(&$form, $contactType, $countryID, $stateID, $title) {
498 $config = CRM_Core_Config
::singleton();
500 $form->assign('contact_type', $contactType);
501 $form->assign('fieldSetTitle', $title);
502 $form->assign('contactEditMode', TRUE);
504 $attributes = CRM_Core_DAO
::getAttribute('CRM_Contact_DAO_Contact');
505 if ($form->_contactId
) {
506 $form->assign('orgId', $form->_contactId
);
509 switch ($contactType) {
511 $form->add('text', 'organization_name', ts('Organization Name'), $attributes['organization_name'], TRUE);
515 $form->add('text', 'household_name', ts('Household Name'), $attributes['household_name']);
520 $form->addElement('select', 'prefix_id', ts('Prefix'),
521 array('' => ts('- prefix -')) + CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'prefix_id')
523 $form->addElement('text', 'first_name', ts('First Name'),
524 $attributes['first_name']
526 $form->addElement('text', 'middle_name', ts('Middle Name'),
527 $attributes['middle_name']
529 $form->addElement('text', 'last_name', ts('Last Name'),
530 $attributes['last_name']
532 $form->addElement('select', 'suffix_id', ts('Suffix'),
533 array('' => ts('- suffix -')) + CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'suffix_id')
537 $addressSequence = $config->addressSequence();
538 $form->assign('addressSequence', array_fill_keys($addressSequence, 1));
541 $form->addElement('text',
544 CRM_Core_DAO
::getAttribute('CRM_Core_DAO_Phone',
549 $form->addElement('text',
552 CRM_Core_DAO
::getAttribute('CRM_Core_DAO_Email',
556 //build the address block
557 CRM_Contact_Form_Edit_Address
::buildQuickForm($form);
561 * Clear cache employer name and employer id
562 * of all employee when employer get deleted.
564 * @param int $employerId
565 * Contact id of employer ( organization id ).
568 public static function clearAllEmployee($employerId) {
570 UPDATE civicrm_contact
571 SET organization_name=NULL, employer_id = NULL
572 WHERE employer_id={$employerId}; ";
574 $dao = CRM_Core_DAO
::executeQuery($query, CRM_Core_DAO
::$_nullArray);
578 * Given an array of contact ids this function will return array with links to view contact page
580 * @param array $contactIDs
581 * Associated contact id's.
582 * @param bool $addViewLink
583 * @param bool $addEditLink
584 * @param int $originalId
585 * Associated with the contact which is edited.
588 * @return array $contactViewLinks returns array with links to contact view
591 public static function formatContactIDSToLinks($contactIDs, $addViewLink = TRUE, $addEditLink = TRUE, $originalId = NULL) {
592 $contactLinks = array();
593 if (!is_array($contactIDs) ||
empty($contactIDs)) {
594 return $contactLinks;
597 // does contact has sufficient permissions.
598 $permissions = array(
599 'view' => 'view all contacts',
600 'edit' => 'edit all contacts',
601 'merge' => 'merge duplicate contacts',
604 $permissionedContactIds = array();
605 foreach ($permissions as $task => $permission) {
607 if (CRM_Core_Permission
::check($permission)) {
608 foreach ($contactIDs as $contactId) {
609 $permissionedContactIds[$contactId][$task] = TRUE;
614 // check permission on acl basis.
615 if (in_array($task, array(
617 $aclPermission = CRM_Core_Permission
::VIEW
;
618 if ($task == 'edit') {
619 $aclPermission = CRM_Core_Permission
::EDIT
;
621 foreach ($contactIDs as $contactId) {
622 if (CRM_Contact_BAO_Contact_Permission
::allow($contactId, $aclPermission)) {
623 $permissionedContactIds[$contactId][$task] = TRUE;
629 // retrieve display names for all contacts
631 SELECT c.id, c.display_name, c.contact_type, ce.email
632 FROM civicrm_contact c
633 LEFT JOIN civicrm_email ce ON ( ce.contact_id=c.id AND ce.is_primary = 1 )
634 WHERE c.id IN (' . implode(',', $contactIDs) . ' ) LIMIT 20';
636 $dao = CRM_Core_DAO
::executeQuery($query);
638 $contactLinks['msg'] = NULL;
640 while ($dao->fetch()) {
642 $contactLinks['rows'][$i]['display_name'] = $dao->display_name
;
643 $contactLinks['rows'][$i]['primary_email'] = $dao->email
;
645 // get the permission for current contact id.
646 $hasPermissions = CRM_Utils_Array
::value($dao->id
, $permissionedContactIds);
647 if (!is_array($hasPermissions) ||
empty($hasPermissions)) {
652 // do check for view.
653 if (array_key_exists('view', $hasPermissions)) {
654 $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>';
655 if (!$contactLinks['msg']) {
656 $contactLinks['msg'] = 'view';
659 if (array_key_exists('edit', $hasPermissions)) {
660 $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>';
661 if (!$contactLinks['msg'] ||
$contactLinks['msg'] != 'merge') {
662 $contactLinks['msg'] = 'edit';
665 if (!empty($originalId) && array_key_exists('merge', $hasPermissions)) {
666 $rgBao = new CRM_Dedupe_BAO_RuleGroup();
667 $rgBao->contact_type
= $dao->contact_type
;
668 $rgBao->used
= 'Supervised';
669 if ($rgBao->find(TRUE)) {
672 if ($rgid && isset($dao->id
)) {
673 //get an url to merge the contact
674 $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>';
675 $contactLinks['msg'] = 'merge';
682 return $contactLinks;
686 * This function retrieve component related contact information.
688 * @param array $componentIds
689 * Array of component Ids.
690 * @param string $componentName
691 * @param array $returnProperties
692 * Array of return elements.
694 * @return array $contactDetails array of contact info.@static
696 public static function contactDetails($componentIds, $componentName, $returnProperties = array()) {
697 $contactDetails = array();
698 if (empty($componentIds) ||
699 !in_array($componentName, array('CiviContribute', 'CiviMember', 'CiviEvent', 'Activity'))
701 return $contactDetails;
704 if (empty($returnProperties)) {
705 $autocompleteContactSearch = CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
706 'contact_autocomplete_options'
708 $returnProperties = array_fill_keys(array_merge(array('sort_name'),
709 array_keys($autocompleteContactSearch)
714 if ($componentName == 'CiviContribute') {
715 $compTable = 'civicrm_contribution';
717 elseif ($componentName == 'CiviMember') {
718 $compTable = 'civicrm_membership';
720 elseif ($componentName == 'Activity') {
721 $compTable = 'civicrm_activity';
722 $activityContacts = CRM_Core_OptionGroup
::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
725 $compTable = 'civicrm_participant';
728 $select = $from = array();
729 foreach ($returnProperties as $property => $ignore) {
730 $value = (in_array($property, array(
731 'city', 'street_address'))) ?
'address' : $property;
734 if ($componentName == 'Activity') {
735 $sourceID = CRM_Utils_Array
::key('Activity Source', $activityContacts);
736 $select[] = "contact.$property as $property";
738 INNER JOIN civicrm_activity_contact acs ON (acs.activity_id = {$compTable}.id AND acs.record_type_id = {$sourceID})
739 INNER JOIN civicrm_contact contact ON ( contact.id = acs.contact_id )";
742 $select[] = "$property as $property";
743 $from[$value] = "INNER JOIN civicrm_contact contact ON ( contact.id = $compTable.contact_id )";
747 case 'target_sort_name':
748 $targetID = CRM_Utils_Array
::key('Activity Targets', $activityContacts);
749 $select[] = "contact_target.sort_name as $property";
751 INNER JOIN civicrm_activity_contact act ON (act.activity_id = {$compTable}.id AND act.record_type_id = {$targetID})
752 INNER JOIN civicrm_contact contact_target ON ( contact_target.id = act.contact_id )";
758 case 'street_address':
759 $select[] = "$property as $property";
760 // Grab target contact properties if this is for activity
761 if ($componentName == 'Activity') {
762 $from[$value] = "LEFT JOIN civicrm_{$value} {$value} ON ( contact_target.id = {$value}.contact_id AND {$value}.is_primary = 1 ) ";
765 $from[$value] = "LEFT JOIN civicrm_{$value} {$value} ON ( contact.id = {$value}.contact_id AND {$value}.is_primary = 1 ) ";
770 case 'state_province':
771 $select[] = "{$property}.name as $property";
772 if (!in_array('address', $from)) {
773 // Grab target contact properties if this is for activity
774 if ($componentName == 'Activity') {
775 $from['address'] = 'LEFT JOIN civicrm_address address ON ( contact_target.id = address.contact_id AND address.is_primary = 1) ';
778 $from['address'] = 'LEFT JOIN civicrm_address address ON ( contact.id = address.contact_id AND address.is_primary = 1) ';
781 $from[$value] = " LEFT JOIN civicrm_{$value} {$value} ON ( address.{$value}_id = {$value}.id ) ";
786 //finally retrieve contact details.
787 if (!empty($select) && !empty($from)) {
788 $fromClause = implode(' ', $from);
789 $selectClause = implode(', ', $select);
790 $whereClause = "{$compTable}.id IN (" . implode(',', $componentIds) . ')';
793 SELECT contact.id as contactId, $compTable.id as componentId, $selectClause
794 FROM $compTable as $compTable $fromClause
796 Group By componentId";
798 $contact = CRM_Core_DAO
::executeQuery($query);
799 while ($contact->fetch()) {
800 $contactDetails[$contact->componentId
]['contact_id'] = $contact->contactId
;
801 foreach ($returnProperties as $property => $ignore) {
802 $contactDetails[$contact->componentId
][$property] = $contact->$property;
808 return $contactDetails;
812 * Function handles shared contact address processing
813 * In this function we just modify submitted values so that new address created for the user
814 * has same address as shared contact address. We copy the address so that search etc will be
817 * @param array $address
818 * This is associated array which contains submitted form values.
823 public static function processSharedAddress(&$address) {
824 if (!is_array($address)) {
828 // Sharing contact address during create mode is pretty straight forward.
829 // In update mode we should check following:
830 // - We should check if user has uncheck shared contact address
831 // - If yes then unset the master_id or may be just delete the address that copied master
832 // Normal update process will automatically create new address with submitted values
834 // 1. loop through entire subnitted address array
835 $masterAddress = array();
836 $skipFields = array('is_primary', 'location_type_id', 'is_billing', 'master_id');
837 foreach ($address as & $values) {
838 // 2. check if master id exists, if not continue
839 if (empty($values['master_id']) ||
empty($values['use_shared_address'])) {
840 // we should unset master id when use uncheck share address for existing address
841 $values['master_id'] = 'null';
845 // 3. get the address details for master_id
846 $masterAddress = new CRM_Core_BAO_Address();
847 $masterAddress->id
= CRM_Utils_Array
::value('master_id', $values);
848 $masterAddress->find(TRUE);
850 // 4. modify submitted params and update it with shared contact address
851 // make sure you preserve specific form values like location type, is_primary_ is_billing, master_id
852 // CRM-10336: Also empty any fields from the existing address block if they don't exist in master (otherwise they will persist)
853 foreach ($values as $field => $submittedValue) {
854 if (!in_array($field, $skipFields)) {
855 if (isset($masterAddress->$field)) {
856 $values[$field] = $masterAddress->$field;
859 $values[$field] = '';
867 * Get the list of contact name give address associated array
869 * @param array $addresses
870 * Associated array of.
872 * @return array $contactNames associated array of contact names@static
874 public static function getAddressShareContactNames(&$addresses) {
875 $contactNames = array();
876 // get the list of master id's for address
877 $masterAddressIds = array();
878 foreach ($addresses as $key => $addressValue) {
879 if (!empty($addressValue['master_id'])) {
880 $masterAddressIds[] = $addressValue['master_id'];
884 if (!empty($masterAddressIds)) {
885 $query = 'SELECT ca.id, cc.display_name, cc.id as cid, cc.is_deleted
886 FROM civicrm_contact cc
887 INNER JOIN civicrm_address ca ON cc.id = ca.contact_id
888 WHERE ca.id IN ( ' . implode(',', $masterAddressIds) . ')';
889 $dao = CRM_Core_DAO
::executeQuery($query);
891 while ($dao->fetch()) {
892 $contactViewUrl = CRM_Utils_System
::url('civicrm/contact/view', "reset=1&cid={$dao->cid}");
893 $contactNames[$dao->id
] = array(
894 'name' => "<a href='{$contactViewUrl}'>{$dao->display_name}</a>",
895 'is_deleted' => $dao->is_deleted
,
896 'contact_id' => $dao->cid
,
900 return $contactNames;
904 * Clear the contact cache so things are kosher. We started off being super aggressive with clearing
905 * caches, but are backing off from this with every release. Compromise between ease of coding versus
906 * performance versus being accurate at that very instant
909 * The contactID that was edited / deleted.
914 public static function clearContactCaches($contactID = NULL) {
915 // clear acl cache if any.
916 CRM_ACL_BAO_Cache
::resetCache();
918 if (empty($contactID)) {
919 // also clear prev/next dedupe cache - if no contactID passed in
920 CRM_Core_BAO_PrevNextCache
::deleteItem();
923 // reset the group contact cache for this group
924 CRM_Contact_BAO_GroupContactCache
::remove();
928 * @param array $params
932 public static function updateGreeting($params) {
933 $contactType = $params['ct'];
934 $greeting = $params['gt'];
935 $valueID = $id = CRM_Utils_Array
::value('id', $params);
936 $force = CRM_Utils_Array
::value('force', $params);
937 $limit = CRM_Utils_Array
::value('limit', $params);
939 // if valueID is not passed use default value
941 $valueID = $id = self
::defaultGreeting($contactType, $greeting);
945 'contact_type' => $contactType,
946 'greeting_type' => $greeting,
949 $allGreetings = CRM_Core_PseudoConstant
::greeting($filter);
950 $originalGreetingString = $greetingString = CRM_Utils_Array
::value($valueID, $allGreetings);
951 if (!$greetingString) {
952 CRM_Core_Error
::fatal(ts('Incorrect greeting value id %1, or no default greeting for this contact type and greeting type.', array(1 => $valueID)));
955 // build return properties based on tokens
956 $greetingTokens = CRM_Utils_Token
::getTokens($greetingString);
957 $tokens = CRM_Utils_Array
::value('contact', $greetingTokens);
958 $greetingsReturnProperties = array();
959 if (is_array($tokens)) {
960 $greetingsReturnProperties = array_fill_keys(array_values($tokens), 1);
963 // Process ALL contacts only when force=1 or force=2 is passed. Else only contacts with NULL greeting or addressee value are updated.
964 $processAll = $processOnlyIdSet = FALSE;
968 elseif ($force == 2) {
969 $processOnlyIdSet = TRUE;
972 //FIXME : apiQuery should handle these clause.
973 $filterContactFldIds = $filterIds = array();
974 $idFldName = $displayFldName = NULL;
975 if (in_array($greeting, CRM_Contact_BAO_Contact
::$_greetingTypes)) {
976 $idFldName = $greeting . '_id';
977 $displayFldName = $greeting . '_display';
981 $queryParams = array(1 => array($contactType, 'String'));
983 // if $force == 1 then update all contacts else only
984 // those with NULL greeting or addressee value CRM-9476
986 $sql = "SELECT DISTINCT id, $idFldName FROM civicrm_contact WHERE contact_type = %1 ";
990 SELECT DISTINCT id, $idFldName
992 WHERE contact_type = %1
993 AND ({$idFldName} IS NULL
994 OR ( {$idFldName} IS NOT NULL AND ({$displayFldName} IS NULL OR {$displayFldName} = '')) )";
998 $sql .= " LIMIT 0, %2";
999 $queryParams +
= array(2 => array($limit, 'Integer'));
1002 $dao = CRM_Core_DAO
::executeQuery($sql, $queryParams);
1003 while ($dao->fetch()) {
1004 $filterContactFldIds[$dao->id
] = $dao->$idFldName;
1006 if (!CRM_Utils_System
::isNull($dao->$idFldName)) {
1007 $filterIds[$dao->id
] = $dao->$idFldName;
1012 if (empty($filterContactFldIds)) {
1013 $filterContactFldIds[] = 0;
1016 // retrieve only required contact information
1017 $extraParams[] = array('contact_type', '=', $contactType, 0, 0);
1018 // we do token replacement in the replaceGreetingTokens hook
1019 list($greetingDetails) = CRM_Utils_Token
::getTokenDetails(array_keys($filterContactFldIds),
1020 $greetingsReturnProperties,
1021 FALSE, FALSE, $extraParams
1023 // perform token replacement and build update SQL
1024 $contactIds = array();
1025 $cacheFieldQuery = "UPDATE civicrm_contact SET {$greeting}_display = CASE id ";
1026 foreach ($greetingDetails as $contactID => $contactDetails) {
1028 !array_key_exists($contactID, $filterContactFldIds)
1033 if ($processOnlyIdSet && !array_key_exists($contactID, $filterIds)) {
1038 $greetingString = $originalGreetingString;
1039 $contactIds[] = $contactID;
1042 if ($greetingBuffer = CRM_Utils_Array
::value($filterContactFldIds[$contactID], $allGreetings)) {
1043 $greetingString = $greetingBuffer;
1047 self
::processGreetingTemplate($greetingString, $contactDetails, $contactID, 'CRM_UpdateGreeting');
1048 $greetingString = CRM_Core_DAO
::escapeString($greetingString);
1049 $cacheFieldQuery .= " WHEN {$contactID} THEN '{$greetingString}' ";
1051 $allContactIds[] = $contactID;
1054 if (!empty($allContactIds)) {
1055 $cacheFieldQuery .= " ELSE {$greeting}_display
1057 if (!empty($contactIds)) {
1058 // need to update greeting _id field.
1059 // reset greeting _custom
1060 $resetCustomGreeting = '';
1061 if ($valueID != 4) {
1062 $resetCustomGreeting = ", {$greeting}_custom = NULL ";
1066 UPDATE civicrm_contact
1067 SET {$greeting}_id = {$valueID}
1068 {$resetCustomGreeting}
1069 WHERE id IN (" . implode(',', $contactIds) . ")";
1070 CRM_Core_DAO
::executeQuery($queryString);
1073 // now update cache field
1074 CRM_Core_DAO
::executeQuery($cacheFieldQuery);
1079 * Fetch the default greeting for a given contact type
1081 * @param string $contactType
1083 * @param string $greetingType
1086 * @return int or null
1088 public static function defaultGreeting($contactType, $greetingType) {
1089 $contactTypeFilters = array('Individual' => 1, 'Household' => 2, 'Organization' => 3);
1090 if (!isset($contactTypeFilters[$contactType])) {
1093 $filter = $contactTypeFilters[$contactType];
1095 $id = CRM_Core_OptionGroup
::values($greetingType, NULL, NULL, NULL,
1096 " AND is_default = 1 AND (filter = {$filter} OR filter = 0)",
1100 return current($id);
1105 * Process a greeting template string to produce the individualised greeting text.
1107 * This works just like message templates for mailings:
1108 * the template is processed with the token substitution mechanism,
1109 * to supply the individual contact data;
1110 * and it is also processed with Smarty,
1111 * to allow for conditionals etc. based on the contact data.
1113 * Note: We don't pass any variables to Smarty --
1114 * all variable data is inserted into the input string
1115 * by the token substitution mechanism,
1116 * before Smarty is invoked.
1118 * @param string $templateString
1119 * The greeting template string with contact tokens + Smarty syntax.
1121 * @param $contactDetails
1122 * @param int $contactID
1123 * @param string $className
1128 public static function processGreetingTemplate(&$templateString, $contactDetails, $contactID, $className) {
1129 CRM_Utils_Token
::replaceGreetingTokens($templateString, $contactDetails, $contactID, $className, TRUE);
1131 $smarty = CRM_Core_Smarty
::singleton();
1132 $templateString = $smarty->fetch("string:$templateString");