copyright and version fixes
[civicrm-core.git] / CRM / Contact / BAO / Contact / Utils.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35 class CRM_Contact_BAO_Contact_Utils {
36
37 /**
38 * given a contact type, get the contact image
39 *
40 * @param string $contactType contact type
41 * @param boolean $urlOnly if we need to return only image url
42 * @param int $contactId contact id
43 * @param boolean $addProfileOverlay if profile overlay class should be added
44 *
45 * @return string
46 * @access public
47 * @static
48 */
49 static function getImage($contactType, $urlOnly = FALSE, $contactId = NULL, $addProfileOverlay = TRUE) {
50 static $imageInfo = array();
51
52 $contactType = explode(CRM_Core_DAO::VALUE_SEPARATOR, trim($contactType, CRM_Core_DAO::VALUE_SEPARATOR));
53 $contactType = $contactType[0];
54
55 if (!array_key_exists($contactType, $imageInfo)) {
56 $imageInfo[$contactType] = array();
57
58 $typeInfo = array();
59 $params = array('name' => $contactType);
60 CRM_Contact_BAO_ContactType::retrieve($params, $typeInfo);
61
62 if (!empty($typeInfo['image_URL'])) {
63 $imageUrl = $typeInfo['image_URL'];
64 $config = CRM_Core_Config::singleton();
65
66 if (!preg_match("/^(\/|(http(s)?:)).+$/i", $imageUrl)) {
67 $imageUrl = $config->resourceBase . $imageUrl;
68 }
69 $imageInfo[$contactType]['image'] = "<div class=\"icon crm-icon {$typeInfo['name']}-icon\" style=\"background: url('{$imageUrl}')\" title=\"{$contactType}\"></div>";
70 $imageInfo[$contactType]['url'] = $imageUrl;
71 }
72 else {
73 $isSubtype = (array_key_exists('parent_id', $typeInfo) &&
74 $typeInfo['parent_id']
75 ) ? TRUE : FALSE;
76
77 if ($isSubtype) {
78 $type = CRM_Contact_BAO_ContactType::getBasicType($typeInfo['name']) . '-subtype';
79 }
80 else {
81 $type = CRM_Utils_Array::value('name', $typeInfo);
82 }
83
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>";
87 }
88 else{
89 $imageInfo[$contactType]['image'] = "<div class=\"icon crm-icon {$type}-icon\" title=\"{$contactType}\"></div>";
90 }
91 $imageInfo[$contactType]['url'] = NULL;
92 }
93 }
94
95 if ($addProfileOverlay) {
96 static $summaryOverlayProfileId = NULL;
97 if (!$summaryOverlayProfileId) {
98 $summaryOverlayProfileId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', 'summary_overlay', 'id', 'name');
99 }
100
101 $profileURL = CRM_Utils_System::url('civicrm/profile/view',
102 "reset=1&gid={$summaryOverlayProfileId}&id={$contactId}&snippet=4"
103 );
104
105 $imageInfo[$contactType]['summary-link'] = '<a href="' . $profileURL . '" class="crm-summary-link">' . $imageInfo[$contactType]['image'] . '</a>';
106 }
107 else {
108 $imageInfo[$contactType]['summary-link'] = $imageInfo[$contactType]['image'];
109 }
110
111 return $urlOnly ? $imageInfo[$contactType]['url'] : $imageInfo[$contactType]['summary-link'];
112 }
113
114 /**
115 * function check for mix contact ids(individual+household etc...)
116 *
117 * @param array $contactIds array of contact ids
118 *
119 * @return boolen true or false true if mix contact array else fale
120 *
121 * @access public
122 * @static
123 */
124 public static function checkContactType(&$contactIds) {
125 if (empty($contactIds)) {
126 return FALSE;
127 }
128
129 $idString = implode(',', $contactIds);
130 $query = "
131 SELECT count( DISTINCT contact_type )
132 FROM civicrm_contact
133 WHERE id IN ( $idString )
134 ";
135 $count = CRM_Core_DAO::singleValueQuery($query,
136 CRM_Core_DAO::$_nullArray
137 );
138 return $count > 1 ? TRUE : FALSE;
139 }
140
141 /**
142 * Generate a checksum for a contactID
143 *
144 * @param int $contactID
145 * @param int $ts timestamp that checksum was generated
146 * @param int $live life of this checksum in hours/ 'inf' for infinite
147 * @param string $hash contact hash, if sent, prevents a query in inner loop
148 *
149 * @return array ( $cs, $ts, $live )
150 * @static
151 * @access public
152 */
153 static function generateChecksum($contactID, $ts = NULL, $live = NULL, $hash = NULL) {
154 // return a warning message if we dont get a contactID
155 // this typically happens when we do a message preview
156 // or an anon mailing view - CRM-8298
157 if (!$contactID) {
158 return 'invalidChecksum';
159 }
160
161 if (!$hash) {
162 $hash = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
163 $contactID, 'hash'
164 );
165 }
166
167 if (!$hash) {
168 $hash = md5(uniqid(rand(), TRUE));
169 CRM_Core_DAO::setFieldValue('CRM_Contact_DAO_Contact',
170 $contactID,
171 'hash', $hash
172 );
173 }
174
175 if (!$ts) {
176 $ts = time();
177 }
178
179 if (!$live) {
180 $days = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
181 'checksum_timeout',
182 NULL,
183 7
184 );
185 $live = 24 * $days;
186 }
187
188 $cs = md5("{$hash}_{$contactID}_{$ts}_{$live}");
189 return "{$cs}_{$ts}_{$live}";
190 }
191
192 /**
193 * Make sure the checksum is valid for the passed in contactID
194 *
195 * @param int $contactID
196 * @param string $inputCheck checksum to match against
197 *
198 * @return boolean true if valid, else false
199 * @static
200 * @access public
201 */
202 static function validChecksum($contactID, $inputCheck) {
203
204 $input = CRM_Utils_System::explode('_', $inputCheck, 3);
205
206 $inputCS = CRM_Utils_Array::value(0, $input);
207 $inputTS = CRM_Utils_Array::value(1, $input);
208 $inputLF = CRM_Utils_Array::value(2, $input);
209
210 $check = self::generateChecksum($contactID, $inputTS, $inputLF);
211
212 if ($check != $inputCheck) {
213 return FALSE;
214 }
215
216 // no life limit for checksum
217 if ($inputLF == 'inf') {
218 return TRUE;
219 }
220
221 // checksum matches so now check timestamp
222 $now = time();
223 return ($inputTS + ($inputLF * 60 * 60) >= $now);
224 }
225
226 /**
227 * Function to get the count of contact loctions
228 *
229 * @param int $contactId contact id
230 *
231 * @return int $locationCount max locations for the contact
232 * @static
233 * @access public
234 */
235 static function maxLocations($contactId) {
236 $contactLocations = array();
237
238 // find number of location blocks for this contact and adjust value accordinly
239 // get location type from email
240 $query = "
241 ( SELECT location_type_id FROM civicrm_email WHERE contact_id = {$contactId} )
242 UNION
243 ( SELECT location_type_id FROM civicrm_phone WHERE contact_id = {$contactId} )
244 UNION
245 ( SELECT location_type_id FROM civicrm_im WHERE contact_id = {$contactId} )
246 UNION
247 ( SELECT location_type_id FROM civicrm_address WHERE contact_id = {$contactId} )
248 ";
249 $dao = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray);
250 return $dao->N;
251 }
252
253 /**
254 * Create Current employer relationship for a individual
255 *
256 * @param int $contactID contact id of the individual
257 * @param string $organization it can be name or id of organization
258 *
259 * @access public
260 * @static
261 */
262 static function createCurrentEmployerRelationship($contactID, $organizationId, $previousEmployerID = NULL) {
263 if ($organizationId && is_numeric($organizationId)) {
264 $cid = array('contact' => $contactID);
265
266 // get the relationship type id of "Employee of"
267 $relTypeId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', 'Employee of', 'id', 'name_a_b');
268 if (!$relTypeId) {
269 CRM_Core_Error::fatal(ts("You seem to have deleted the relationship type 'Employee of'"));
270 }
271
272 // create employee of relationship
273 $relationshipParams = array(
274 'is_active' => TRUE,
275 'relationship_type_id' => $relTypeId . '_a_b',
276 'contact_check' => array($organizationId => TRUE),
277 );
278 list($valid, $invalid, $duplicate,
279 $saved, $relationshipIds
280 ) = CRM_Contact_BAO_Relationship::create($relationshipParams, $cid);
281
282
283 // In case we change employer, clean prveovious employer related records.
284 if (!$previousEmployerID) {
285 $previousEmployerID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contactID, 'employer_id');
286 }
287 if ($previousEmployerID &&
288 $previousEmployerID != $organizationId
289 ) {
290 self::clearCurrentEmployer($contactID, $previousEmployerID);
291 }
292
293 // set current employer
294 self::setCurrentEmployer(array($contactID => $organizationId));
295
296 $relationshipParams['relationship_ids'] = $relationshipIds;
297 // handle related meberships. CRM-3792
298 self::currentEmployerRelatedMembership($contactID, $organizationId, $relationshipParams, $duplicate, $previousEmployerID);
299 }
300 }
301
302 /**
303 * create related memberships for current employer
304 *
305 * @param int $contactID contact id of the individual
306 * @param int $employerID contact id of the organization.
307 * @param array $relationshipParams relationship params.
308 * @param boolean $duplicate are we triggered existing relationship.
309 *
310 * @access public
311 * @static
312 */
313 static function currentEmployerRelatedMembership($contactID, $employerID, $relationshipParams, $duplicate = FALSE, $previousEmpID = NULL) {
314 $ids = array();
315 $action = CRM_Core_Action::ADD;
316
317 //we do not know that triggered relationship record is active.
318 if ($duplicate) {
319 $relationship = new CRM_Contact_DAO_Relationship();
320 $relationship->contact_id_a = $contactID;
321 $relationship->contact_id_b = $employerID;
322 $relationship->relationship_type_id = $relationshipParams['relationship_type_id'];
323 if ($relationship->find(TRUE)) {
324 $action = CRM_Core_Action::UPDATE;
325 $ids['contact'] = $contactID;
326 $ids['contactTarget'] = $employerID;
327 $ids['relationship'] = $relationship->id;
328 CRM_Contact_BAO_Relationship::setIsActive($relationship->id, TRUE);
329 }
330 $relationship->free();
331 }
332
333 //need to handle related meberships. CRM-3792
334 if ($previousEmpID != $employerID) {
335 CRM_Contact_BAO_Relationship::relatedMemberships($contactID, $relationshipParams, $ids, $action);
336 }
337 }
338
339 /**
340 * Function to set current employer id and organization name
341 *
342 * @param array $currentEmployerParams associated array of contact id and its employer id
343 *
344 */
345 static function setCurrentEmployer($currentEmployerParams) {
346 foreach ($currentEmployerParams as $contactId => $orgId) {
347 $query = "UPDATE civicrm_contact contact_a,civicrm_contact contact_b
348 SET contact_a.employer_id=contact_b.id, contact_a.organization_name=contact_b.organization_name
349 WHERE contact_a.id ={$contactId} AND contact_b.id={$orgId}; ";
350
351 //FIXME : currently civicrm mysql_query support only single statement
352 //execution, though mysql 5.0 support multiple statement execution.
353 $dao = CRM_Core_DAO::executeQuery($query);
354 }
355 }
356
357 /**
358 * Function to update cached current employer name
359 *
360 * @param int $organizationId current employer id
361 *
362 */
363 static function updateCurrentEmployer($organizationId) {
364 $query = "UPDATE civicrm_contact contact_a,civicrm_contact contact_b
365 SET contact_a.organization_name=contact_b.organization_name
366 WHERE contact_a.employer_id=contact_b.id AND contact_b.id={$organizationId}; ";
367
368 $dao = CRM_Core_DAO::executeQuery($query);
369 }
370
371 /**
372 * Function to clear cached current employer name
373 *
374 * @param int $contactId contact id ( mostly individual contact id)
375 * @param int $employerId contact id ( mostly organization contact id)
376 *
377 */
378 static function clearCurrentEmployer($contactId, $employerId = NULL) {
379 $query = "UPDATE civicrm_contact
380 SET organization_name=NULL, employer_id = NULL
381 WHERE id={$contactId}; ";
382
383 $dao = CRM_Core_DAO::executeQuery($query);
384
385 // need to handle related meberships. CRM-3792
386 if ($employerId) {
387 //1. disable corresponding relationship.
388 //2. delete related membership.
389
390 //get the relationship type id of "Employee of"
391 $relTypeId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', 'Employee of', 'id', 'name_a_b');
392 if (!$relTypeId) {
393 CRM_Core_Error::fatal(ts("You seem to have deleted the relationship type 'Employee of'"));
394 }
395 $relMembershipParams['relationship_type_id'] = $relTypeId . '_a_b';
396 $relMembershipParams['contact_check'][$employerId] = 1;
397
398 //get relationship id.
399 if (CRM_Contact_BAO_Relationship::checkDuplicateRelationship($relMembershipParams, $contactId, $employerId)) {
400 $relationship = new CRM_Contact_DAO_Relationship();
401 $relationship->contact_id_a = $contactId;
402 $relationship->contact_id_b = $employerId;
403 $relationship->relationship_type_id = $relTypeId;
404
405 if ($relationship->find(TRUE)) {
406 CRM_Contact_BAO_Relationship::setIsActive($relationship->id, FALSE);
407 CRM_Contact_BAO_Relationship::relatedMemberships($contactId, $relMembershipParams,
408 $ids = array(
409 ), CRM_Core_Action::DELETE
410 );
411 }
412 $relationship->free();
413 }
414 }
415 }
416
417 /**
418 * Function to build form for related contacts / on behalf of organization.
419 *
420 * @param $form object invoking Object
421 * @param $contactType string contact type
422 * @param $title string fieldset title
423 * @param $maxLocationBlocks int number of location blocks
424 *
425 * @static
426 *
427 */
428 static function buildOnBehalfForm(&$form,
429 $contactType = 'Individual',
430 $countryID = NULL,
431 $stateID = NULL,
432 $title = 'Contact Information',
433 $contactEditMode = FALSE,
434 $maxLocationBlocks = 1
435 ) {
436 if ($title == 'Contact Information') {
437 $title = ts('Contact Information');
438 }
439
440 $config = CRM_Core_Config::singleton();
441
442 $form->assign('contact_type', $contactType);
443 $form->assign('fieldSetTitle', $title);
444 $form->assign('contactEditMode', $contactEditMode);
445
446 $attributes = CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact');
447 if ($form->_contactId) {
448 $form->assign('orgId', $form->_contactId);
449 }
450
451 switch ($contactType) {
452 case 'Organization':
453 $session = CRM_Core_Session::singleton();
454 $contactID = $session->get('userID');
455
456 if ($contactID) {
457 $employers = CRM_Contact_BAO_Relationship::getPermissionedEmployer($contactID);
458 }
459
460 $locDataURL = CRM_Utils_System::url('civicrm/ajax/permlocation', 'cid=', FALSE, NULL, FALSE);
461 $form->assign('locDataURL', $locDataURL);
462
463 if (!$contactEditMode && $contactID && (count($employers) >= 1)) {
464
465 $dataURL = CRM_Utils_System::url('civicrm/ajax/employer',
466 'cid=' . $contactID,
467 FALSE, NULL, FALSE
468 );
469 $form->assign('employerDataURL', $dataURL);
470
471 $form->add('text', 'organization_id', ts('Select an existing related Organization OR Enter a new one'));
472 $form->add('hidden', 'onbehalfof_id', '', array('id' => 'onbehalfof_id'));
473 $orgOptions = array('0' => ts('Create new organization'),
474 '1' => ts('Select existing organization'),
475 );
476 $orgOptionExtra = array('onclick' => "showHideByValue('org_option','true','select_org','table-row','radio',true);showHideByValue('org_option','true','create_org','table-row','radio',false);");
477 $form->addRadio('org_option', ts('options'), $orgOptions, $orgOptionExtra);
478 $form->assign('relatedOrganizationFound', TRUE);
479 }
480
481 $form->add('text', 'organization_name', ts('Organization Name'), $attributes['organization_name'], TRUE);
482 break;
483
484 case 'Household':
485 $form->add('text', 'household_name', ts('Household Name'),
486 $attributes['household_name']
487 );
488 break;
489
490 default:
491 // individual
492 $form->addElement('select', 'prefix_id', ts('Prefix'),
493 array('' => ts('- prefix -')) + CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id')
494 );
495 $form->addElement('text', 'first_name', ts('First Name'),
496 $attributes['first_name']
497 );
498 $form->addElement('text', 'middle_name', ts('Middle Name'),
499 $attributes['middle_name']
500 );
501 $form->addElement('text', 'last_name', ts('Last Name'),
502 $attributes['last_name']
503 );
504 $form->addElement('select', 'suffix_id', ts('Suffix'),
505 array('' => ts('- suffix -')) + CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id')
506 );
507 }
508
509 $addressSequence = $config->addressSequence();
510 $form->assign('addressSequence', array_fill_keys($addressSequence, 1));
511
512 //Primary Phone
513 $form->addElement('text',
514 'phone[1][phone]',
515 ts('Primary Phone'),
516 CRM_Core_DAO::getAttribute('CRM_Core_DAO_Phone',
517 'phone'
518 )
519 );
520 //Primary Email
521 $form->addElement('text',
522 'email[1][email]',
523 ts('Primary Email'),
524 CRM_Core_DAO::getAttribute('CRM_Core_DAO_Email',
525 'email'
526 )
527 );
528 //build the address block
529 CRM_Contact_Form_Edit_Address::buildQuickForm($form);
530
531 // also fix the state country selector
532 CRM_Contact_Form_Edit_Address::fixStateSelect($form,
533 'address[1][country_id]',
534 'address[1][state_province_id]',
535 "address[1][county_id]",
536 $countryID,
537 $stateID
538 );
539 }
540
541 /**
542 * Function to clear cache employer name and employer id
543 * of all employee when employer get deleted.
544 *
545 * @param int $employerId contact id of employer ( organization id )
546 *
547 */
548 static function clearAllEmployee($employerId) {
549 $query = "
550 UPDATE civicrm_contact
551 SET organization_name=NULL, employer_id = NULL
552 WHERE employer_id={$employerId}; ";
553
554 $dao = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray);
555 }
556
557 /**
558 * Given an array of contact ids this function will return array with links to view contact page
559 *
560 * @param array $contactIDs associated contact id's
561 * @param int $originalId associated with the contact which is edited
562 *
563 *
564 * @return array $contactViewLinks returns array with links to contact view
565 * @static
566 * @access public
567 */
568 static function formatContactIDSToLinks($contactIDs, $addViewLink = TRUE, $addEditLink = TRUE, $originalId = NULL) {
569 $contactLinks = array();
570 if (!is_array($contactIDs) || empty($contactIDs)) {
571 return $contactLinks;
572 }
573
574 // does contact has sufficient permissions.
575 $permissions = array(
576 'view' => 'view all contacts',
577 'edit' => 'edit all contacts',
578 'merge' => 'merge duplicate contacts',
579 );
580
581 $permissionedContactIds = array();
582 foreach ($permissions as $task => $permission) {
583 // give permission.
584 if (CRM_Core_Permission::check($permission)) {
585 foreach ($contactIDs as $contactId) {
586 $permissionedContactIds[$contactId][$task] = TRUE;
587 }
588 continue;
589 }
590
591 // check permission on acl basis.
592 if (in_array($task, array(
593 'view', 'edit'))) {
594 $aclPermission = CRM_Core_Permission::VIEW;
595 if ($task == 'edit') {
596 $aclPermission = CRM_Core_Permission::EDIT;
597 }
598 foreach ($contactIDs as $contactId) {
599 if (CRM_Contact_BAO_Contact_Permission::allow($contactId, $aclPermission)) {
600 $permissionedContactIds[$contactId][$task] = TRUE;
601 }
602 }
603 }
604 }
605
606 // retrieve display names for all contacts
607 $query = '
608 SELECT c.id, c.display_name, c.contact_type, ce.email
609 FROM civicrm_contact c
610 LEFT JOIN civicrm_email ce ON ( ce.contact_id=c.id AND ce.is_primary = 1 )
611 WHERE c.id IN (' . implode(',', $contactIDs) . ' ) LIMIT 20';
612
613 $dao = CRM_Core_DAO::executeQuery($query);
614
615 $contactLinks['msg'] = NULL;
616 $i = 0;
617 while ($dao->fetch()) {
618
619 $contactLinks['rows'][$i]['display_name'] = $dao->display_name;
620 $contactLinks['rows'][$i]['primary_email'] = $dao->email;
621
622 // get the permission for current contact id.
623 $hasPermissions = CRM_Utils_Array::value($dao->id, $permissionedContactIds);
624 if (!is_array($hasPermissions) || empty($hasPermissions)) {
625 $i++;
626 continue;
627 }
628
629 // do check for view.
630 if (array_key_exists('view', $hasPermissions)) {
631 $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>';
632 if (!$contactLinks['msg']) {
633 $contactLinks['msg'] = 'view';
634 }
635 }
636 if (array_key_exists('edit', $hasPermissions)) {
637 $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>';
638 if (!$contactLinks['msg'] || $contactLinks['msg'] != 'merge') {
639 $contactLinks['msg'] = 'edit';
640 }
641 }
642 if (!empty($originalId) && array_key_exists('merge', $hasPermissions)) {
643 $rgBao = new CRM_Dedupe_BAO_RuleGroup();
644 $rgBao->contact_type = $dao->contact_type;
645 $rgBao->used = 'Supervised';
646 if ($rgBao->find(TRUE)) {
647 $rgid = $rgBao->id;
648 }
649 if ($rgid && isset($dao->id)) {
650 //get an url to merge the contact
651 $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>';
652 $contactLinks['msg'] = 'merge';
653 }
654 }
655
656 $i++;
657 }
658
659 return $contactLinks;
660 }
661
662 /**
663 * This function retrieve component related contact information.
664 *
665 * @param array $componentIds array of component Ids.
666 * @param array $returnProperties array of return elements.
667 *
668 * @return $contactDetails array of contact info.
669 * @static
670 */
671 static function contactDetails($componentIds, $componentName, $returnProperties = array(
672 )) {
673 $contactDetails = array();
674 if (empty($componentIds) ||
675 !in_array($componentName, array('CiviContribute', 'CiviMember', 'CiviEvent', 'Activity'))
676 ) {
677 return $contactDetails;
678 }
679
680 if (empty($returnProperties)) {
681 $autocompleteContactSearch = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
682 'contact_autocomplete_options'
683 );
684 $returnProperties = array_fill_keys(array_merge(array('sort_name'),
685 array_keys($autocompleteContactSearch)
686 ), 1);
687 }
688
689 $compTable = NULL;
690 if ($componentName == 'CiviContribute') {
691 $compTable = 'civicrm_contribution';
692 }
693 elseif ($componentName == 'CiviMember') {
694 $compTable = 'civicrm_membership';
695 }
696 elseif ($componentName == 'Activity') {
697 $compTable = 'civicrm_activity';
698 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
699 }
700 else {
701 $compTable = 'civicrm_participant';
702 }
703
704 $select = $from = array();
705 foreach ($returnProperties as $property => $ignore) {
706 $value = (in_array($property, array(
707 'city', 'street_address'))) ? 'address' : $property;
708 switch ($property) {
709 case 'sort_name':
710 if ($componentName == 'Activity') {
711 $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
712 $select[] = "contact.$property as $property";
713 $from[$value] = "
714 INNER JOIN civicrm_activity_contact acs ON (acs.activity_id = {$compTable}.id AND acs.record_type_id = {$sourceID})
715 INNER JOIN civicrm_contact contact ON ( contact.id = acs.contact_id )";
716 }
717 else {
718 $select[] = "$property as $property";
719 $from[$value] = "INNER JOIN civicrm_contact contact ON ( contact.id = $compTable.contact_id )";
720 }
721 break;
722
723 case 'target_sort_name':
724 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
725 $select[] = "contact_target.sort_name as $property";
726 $from[$value] = "
727 INNER JOIN civicrm_activity_contact act ON (act.activity_id = {$compTable}.id AND act.record_type_id = {$targetID})
728 INNER JOIN civicrm_contact contact_target ON ( contact_target.id = act.contact_id )";
729 break;
730
731 case 'email':
732 case 'phone':
733 case 'city':
734 case 'street_address':
735 $select[] = "$property as $property";
736 // Grab target contact properties if this is for activity
737 if ($componentName == 'Activity') {
738 $from[$value] = "LEFT JOIN civicrm_{$value} {$value} ON ( contact_target.id = {$value}.contact_id AND {$value}.is_primary = 1 ) ";
739 }
740 else {
741 $from[$value] = "LEFT JOIN civicrm_{$value} {$value} ON ( contact.id = {$value}.contact_id AND {$value}.is_primary = 1 ) ";
742 }
743 break;
744
745 case 'country':
746 case 'state_province':
747 $select[] = "{$property}.name as $property";
748 if (!in_array('address', $from)) {
749 // Grab target contact properties if this is for activity
750 if ($componentName == 'Activity') {
751 $from['address'] = 'LEFT JOIN civicrm_address address ON ( contact_target.id = address.contact_id AND address.is_primary = 1) ';
752 }
753 else {
754 $from['address'] = 'LEFT JOIN civicrm_address address ON ( contact.id = address.contact_id AND address.is_primary = 1) ';
755 }
756 }
757 $from[$value] = " LEFT JOIN civicrm_{$value} {$value} ON ( address.{$value}_id = {$value}.id ) ";
758 break;
759 }
760 }
761
762 //finally retrieve contact details.
763 if (!empty($select) && !empty($from)) {
764 $fromClause = implode(' ', $from);
765 $selectClause = implode(', ', $select);
766 $whereClause = "{$compTable}.id IN (" . implode(',', $componentIds) . ')';
767
768 $query = "
769 SELECT contact.id as contactId, $compTable.id as componentId, $selectClause
770 FROM $compTable as $compTable $fromClause
771 WHERE $whereClause
772 Group By componentId";
773
774 $contact = CRM_Core_DAO::executeQuery($query);
775 while ($contact->fetch()) {
776 $contactDetails[$contact->componentId]['contact_id'] = $contact->contactId;
777 foreach ($returnProperties as $property => $ignore) {
778 $contactDetails[$contact->componentId][$property] = $contact->$property;
779 }
780 }
781 $contact->free();
782 }
783
784 return $contactDetails;
785 }
786
787 /**
788 * Function handles shared contact address processing
789 * In this function we just modify submitted values so that new address created for the user
790 * has same address as shared contact address. We copy the address so that search etc will be
791 * much efficient.
792 *
793 * @param array $address this is associated array which contains submitted form values
794 *
795 * @return void
796 * @static
797 * @access public
798 */
799 static function processSharedAddress(&$address) {
800 if (!is_array($address)) {
801 return;
802 }
803
804 // Sharing contact address during create mode is pretty straight forward.
805 // In update mode we should check following:
806 // - We should check if user has uncheck shared contact address
807 // - If yes then unset the master_id or may be just delete the address that copied master
808 // Normal update process will automatically create new address with submitted values
809
810 // 1. loop through entire subnitted address array
811 $masterAddress = array();
812 $skipFields = array('is_primary', 'location_type_id', 'is_billing', 'master_id');
813 foreach ($address as & $values) {
814 // 2. check if master id exists, if not continue
815 if (empty($values['master_id']) || empty($values['use_shared_address'])) {
816 // we should unset master id when use uncheck share address for existing address
817 $values['master_id'] = 'null';
818 continue;
819 }
820
821 // 3. get the address details for master_id
822 $masterAddress = new CRM_Core_BAO_Address();
823 $masterAddress->id = CRM_Utils_Array::value('master_id', $values);
824 $masterAddress->find(TRUE);
825
826 // 4. modify submitted params and update it with shared contact address
827 // make sure you preserve specific form values like location type, is_primary_ is_billing, master_id
828 // CRM-10336: Also empty any fields from the existing address block if they don't exist in master (otherwise they will persist)
829 foreach ($values as $field => $submittedValue) {
830 if (!in_array($field, $skipFields)){
831 if (isset($masterAddress->$field)) {
832 $values[$field] = $masterAddress->$field;
833 } else {
834 $values[$field] = '';
835 }
836 }
837 }
838 }
839 }
840
841 /**
842 * Function to get the list of contact name give address associated array
843 *
844 * @param array $addresses associated array of
845 *
846 * @return $contactNames associated array of contact names
847 * @static
848 */
849 static function getAddressShareContactNames(&$addresses) {
850 $contactNames = array();
851 // get the list of master id's for address
852 $masterAddressIds = array();
853 foreach ($addresses as $key => $addressValue) {
854 if (!empty($addressValue['master_id'])) {
855 $masterAddressIds[] = $addressValue['master_id'];
856 }
857 }
858
859 if (!empty($masterAddressIds)) {
860 $query = 'SELECT ca.id, cc.display_name, cc.id as cid, cc.is_deleted
861 FROM civicrm_contact cc
862 INNER JOIN civicrm_address ca ON cc.id = ca.contact_id
863 WHERE ca.id IN ( ' . implode(',', $masterAddressIds) . ')';
864 $dao = CRM_Core_DAO::executeQuery($query);
865
866 while ($dao->fetch()) {
867 $contactViewUrl = CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$dao->cid}");
868 $contactNames[$dao->id] = array(
869 'name' => "<a href='{$contactViewUrl}'>{$dao->display_name}</a>",
870 'is_deleted' => $dao->is_deleted,
871 );
872 }
873 }
874 return $contactNames;
875 }
876
877 /**
878 * Clear the contact cache so things are kosher. We started off being super aggressive with clearing
879 * caches, but are backing off from this with every release. Compromise between ease of coding versus
880 * performance versus being accurate at that very instant
881 *
882 * @param $contactID - the contactID that was edited / deleted
883 *
884 * @return void
885 * @static
886 */
887 static function clearContactCaches($contactID = NULL) {
888 // clear acl cache if any.
889 CRM_ACL_BAO_Cache::resetCache();
890
891 if (empty($contactID)) {
892 // also clear prev/next dedupe cache - if no contactID passed in
893 CRM_Core_BAO_PrevNextCache::deleteItem();
894 }
895
896 // reset the group contact cache for this group
897 CRM_Contact_BAO_GroupContactCache::remove();
898 }
899
900 public static function updateGreeting($params) {
901 $contactType = $params['ct'];
902 $greeting = $params['gt'];
903 $valueID = $id = CRM_Utils_Array::value('id', $params);
904 $force = CRM_Utils_Array::value('force', $params);
905
906 // if valueID is not passed use default value
907 if (!$valueID) {
908 $valueID = $id = self::defaultGreeting($contactType, $greeting);
909 }
910
911 $filter = array(
912 'contact_type' => $contactType,
913 'greeting_type' => $greeting,
914 );
915
916 $allGreetings = CRM_Core_PseudoConstant::greeting($filter);
917 $originalGreetingString = $greetingString = CRM_Utils_Array::value($valueID, $allGreetings);
918 if (!$greetingString) {
919 CRM_Core_Error::fatal(ts('Incorrect greeting value id %1, or no default greeting for this contact type and greeting type.', array(1 => $valueID)));
920 }
921
922 // build return properties based on tokens
923 $greetingTokens = CRM_Utils_Token::getTokens($greetingString);
924 $tokens = CRM_Utils_Array::value('contact', $greetingTokens);
925 $greetingsReturnProperties = array();
926 if (is_array($tokens)) {
927 $greetingsReturnProperties = array_fill_keys(array_values($tokens), 1);
928 }
929
930 // Process ALL contacts only when force=1 or force=2 is passed. Else only contacts with NULL greeting or addressee value are updated.
931 $processAll = $processOnlyIdSet = FALSE;
932 if ($force == 1) {
933 $processAll = TRUE;
934 }
935 elseif ($force == 2) {
936 $processOnlyIdSet = TRUE;
937 }
938
939 //FIXME : apiQuery should handle these clause.
940 $filterContactFldIds = $filterIds = array();
941 $idFldName = $displayFldName = NULL;
942 if (in_array($greeting, CRM_Contact_BAO_Contact::$_greetingTypes)) {
943 $idFldName = $greeting . '_id';
944 $displayFldName = $greeting . '_display';
945 }
946
947 if ($idFldName) {
948 // if $force == 1 then update all contacts else only
949 // those with NULL greeting or addressee value CRM-9476
950 if ($processAll) {
951 $sql = "SELECT DISTINCT id, $idFldName FROM civicrm_contact WHERE contact_type = %1 ";
952 }
953 else {
954 $sql = "
955 SELECT DISTINCT id, $idFldName
956 FROM civicrm_contact
957 WHERE contact_type = %1
958 AND ({$idFldName} IS NULL
959 OR ( {$idFldName} IS NOT NULL AND ({$displayFldName} IS NULL OR {$displayFldName} = '')) )";
960 }
961
962 $dao = CRM_Core_DAO::executeQuery($sql, array(1 => array($contactType, 'String')));
963 while ($dao->fetch()) {
964 $filterContactFldIds[$dao->id] = $dao->$idFldName;
965
966 if (!CRM_Utils_System::isNull($dao->$idFldName)) {
967 $filterIds[$dao->id] = $dao->$idFldName;
968 }
969 }
970 }
971
972 if (empty($filterContactFldIds)) {
973 $filterContactFldIds[] = 0;
974 }
975
976 // retrieve only required contact information
977 $extraParams[] = array('contact_type', '=', $contactType, 0, 0);
978 // we do token replacement in the replaceGreetingTokens hook
979 list($greetingDetails) = CRM_Utils_Token::getTokenDetails(array_keys($filterContactFldIds),
980 $greetingsReturnProperties,
981 FALSE, FALSE, $extraParams
982 );
983 // perform token replacement and build update SQL
984 $contactIds = array();
985 $cacheFieldQuery = "UPDATE civicrm_contact SET {$greeting}_display = CASE id ";
986 foreach ($greetingDetails as $contactID => $contactDetails) {
987 if (!$processAll &&
988 !array_key_exists($contactID, $filterContactFldIds)
989 ) {
990 continue;
991 }
992
993 if ($processOnlyIdSet && !array_key_exists($contactID, $filterIds)) {
994 continue;
995 }
996
997 if ($id) {
998 $greetingString = $originalGreetingString;
999 $contactIds[] = $contactID;
1000 }
1001 else {
1002 if ($greetingBuffer = CRM_Utils_Array::value($filterContactFldIds[$contactID], $allGreetings)) {
1003 $greetingString = $greetingBuffer;
1004 }
1005 }
1006
1007 self::processGreetingTemplate($greetingString, $contactDetails, $contactID, 'CRM_UpdateGreeting');
1008 $greetingString = CRM_Core_DAO::escapeString($greetingString);
1009 $cacheFieldQuery .= " WHEN {$contactID} THEN '{$greetingString}' ";
1010
1011 $allContactIds[] = $contactID;
1012 }
1013
1014 if (!empty($allContactIds)) {
1015 $cacheFieldQuery .= " ELSE {$greeting}_display
1016 END;";
1017 if (!empty($contactIds)) {
1018 // need to update greeting _id field.
1019 // reset greeting _custom
1020 $resetCustomGreeting = '';
1021 if ($valueID != 4) {
1022 $resetCustomGreeting = ", {$greeting}_custom = NULL ";
1023 }
1024
1025 $queryString = "
1026 UPDATE civicrm_contact
1027 SET {$greeting}_id = {$valueID}
1028 {$resetCustomGreeting}
1029 WHERE id IN (" . implode(',', $contactIds) . ")";
1030 CRM_Core_DAO::executeQuery($queryString);
1031 }
1032
1033 // now update cache field
1034 CRM_Core_DAO::executeQuery($cacheFieldQuery);
1035 }
1036 }
1037
1038 /**
1039 * Fetch the default greeting for a given contact type
1040 *
1041 * @param string $contactType contact type
1042 * @param string $greetingType greeting type
1043 *
1044 * @return int or null
1045 */
1046 static function defaultGreeting($contactType, $greetingType) {
1047 $contactTypeFilters = array('Individual' => 1, 'Household' => 2, 'Organization' => 3);
1048 if (!isset($contactTypeFilters[$contactType])) {
1049 return;
1050 }
1051 $filter = $contactTypeFilters[$contactType];
1052
1053 $id = CRM_Core_OptionGroup::values($greetingType, NULL, NULL, NULL,
1054 " AND is_default = 1 AND (filter = {$filter} OR filter = 0)",
1055 'value'
1056 );
1057 if (!empty($id)) {
1058 return current($id);
1059 }
1060 }
1061
1062 /**
1063 * Process a greeting template string to produce the individualised greeting text.
1064 *
1065 * This works just like message templates for mailings:
1066 * the template is processed with the token substitution mechanism,
1067 * to supply the individual contact data;
1068 * and it is also processed with Smarty,
1069 * to allow for conditionals etc. based on the contact data.
1070 *
1071 * Note: We don't pass any variables to Smarty --
1072 * all variable data is inserted into the input string
1073 * by the token substitution mechanism,
1074 * before Smarty is invoked.
1075 *
1076 * @param string $templateString the greeting template string with contact tokens + Smarty syntax
1077 *
1078 * @return void
1079 * @static
1080 */
1081 static function processGreetingTemplate(&$templateString, $contactDetails, $contactID, $className) {
1082 CRM_Utils_Token::replaceGreetingTokens($templateString, $contactDetails, $contactID, $className, TRUE);
1083
1084 $smarty = CRM_Core_Smarty::singleton();
1085 $templateString = $smarty->fetch("string:$templateString");
1086 }
1087 }
1088