3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
36 * This class contains all contact related functions that are called using AJAX (jQuery)
38 class CRM_Contact_Page_AJAX
{
40 * When a user chooses a username, CHECK_USERNAME_TTL
41 * is the time window in which they can check usernames
42 * (without reloading the overall form).
44 const CHECK_USERNAME_TTL
= 10800; // 3hr; 3*60*60
46 const AUTOCOMPLETE_TTL
= 21600; // 6hr; 6*60*60
49 * Ajax callback for custom fields of type ContactReference
51 * Todo: Migrate contact reference fields to use EntityRef
53 public static function contactReference() {
54 $name = CRM_Utils_Array
::value('term', $_GET);
55 $name = CRM_Utils_Type
::escape($name, 'String');
56 $cfID = CRM_Utils_Type
::escape($_GET['id'], 'Positive');
58 // check that this is a valid, active custom field of Contact Reference type
59 $params = array('id' => $cfID);
60 $returnProperties = array('filter', 'data_type', 'is_active');
62 CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_CustomField', $params, $cf, $returnProperties);
63 if (!$cf['id'] ||
!$cf['is_active'] ||
$cf['data_type'] != 'ContactReference') {
64 CRM_Utils_System
::civiExit('error');
67 if (!empty($cf['filter'])) {
68 $filterParams = array();
69 parse_str($cf['filter'], $filterParams);
71 $action = CRM_Utils_Array
::value('action', $filterParams);
72 if (!empty($action) && !in_array($action, array('get', 'lookup'))) {
73 CRM_Utils_System
::civiExit('error');
76 if (!empty($filterParams['group'])) {
77 $filterParams['group'] = explode(',', $filterParams['group']);
81 $list = array_keys(CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
82 'contact_reference_options'
85 $return = array_unique(array_merge(array('sort_name'), $list));
87 $limit = Civi
::settings()->get('search_autocomplete_count');
89 $params = array('offset' => 0, 'rowCount' => $limit, 'version' => 3);
90 foreach ($return as $fld) {
91 $params["return.{$fld}"] = 1;
94 if (!empty($action)) {
110 foreach ($_GET as $param => $val) {
112 in_array($param, $excludeGet) ||
113 strpos($param, 'return.') !== FALSE ||
114 strpos($param, 'api.') !== FALSE
118 $params[$param] = $val;
123 $params['sort_name'] = $name;
126 $params['sort'] = 'sort_name';
128 // tell api to skip permission chk. dgg
129 $params['check_permissions'] = 0;
131 // add filter variable to params
132 if (!empty($filterParams)) {
133 $params = array_merge($params, $filterParams);
136 $contact = civicrm_api('Contact', 'Get', $params);
138 if (!empty($contact['is_error'])) {
139 CRM_Utils_System
::civiExit('error');
142 $contactList = array();
143 foreach ($contact['values'] as $value) {
145 foreach ($return as $fld) {
146 if (!empty($value[$fld])) {
147 $view[] = $value[$fld];
150 $contactList[] = array('id' => $value['id'], 'text' => implode(' :: ', $view));
153 if (!empty($_GET['is_unit_test'])) {
156 CRM_Utils_JSON
::output($contactList);
160 * Fetch PCP ID by PCP Supporter sort_name, also displays PCP title and associated Contribution Page title
162 public static function getPCPList() {
163 $name = CRM_Utils_Array
::value('term', $_GET);
164 $name = CRM_Utils_Type
::escape($name, 'String');
165 $limit = $max = Civi
::settings()->get('search_autocomplete_count');
167 $where = ' AND pcp.page_id = cp.id AND pcp.contact_id = cc.id';
169 $config = CRM_Core_Config
::singleton();
170 if ($config->includeWildCardInName
) {
171 $strSearch = "%$name%";
174 $strSearch = "$name%";
176 $includeEmailFrom = $includeNickName = '';
177 if ($config->includeNickNameInName
) {
178 $includeNickName = " OR nick_name LIKE '$strSearch'";
180 if ($config->includeEmailInName
) {
181 $includeEmailFrom = "LEFT JOIN civicrm_email eml ON ( cc.id = eml.contact_id AND eml.is_primary = 1 )";
182 $whereClause = " WHERE ( email LIKE '$strSearch' OR sort_name LIKE '$strSearch' $includeNickName ) {$where} ";
185 $whereClause = " WHERE ( sort_name LIKE '$strSearch' $includeNickName ) {$where} ";
188 $offset = $count = 0;
189 if (!empty($_GET['page_num'])) {
190 $page = (int) $_GET['page_num'];
191 $offset = $limit * ($page - 1);
195 $select = 'cc.sort_name, pcp.title, cp.title';
199 SELECT pcp.id as id, CONCAT_WS( ' :: ', {$select} ) as data, sort_name
200 FROM civicrm_pcp pcp, civicrm_contribution_page cp, civicrm_contact cc
202 {$whereClause} AND pcp.page_type = 'contribute'
204 SELECT pcp.id as id, CONCAT_WS( ' :: ', {$select} ) as data, sort_name
205 FROM civicrm_pcp pcp, civicrm_event cp, civicrm_contact cc
207 {$whereClause} AND pcp.page_type = 'event'
210 LIMIT $offset, $limit
213 $dao = CRM_Core_DAO
::executeQuery($query);
214 $output = array('results' => array(), 'more' => FALSE);
215 while ($dao->fetch()) {
216 if (++
$count > $max) {
217 $output['more'] = TRUE;
220 $output['results'][] = array('id' => $dao->id
, 'text' => $dao->data
);
223 CRM_Utils_JSON
::output($output);
226 public static function relationship() {
227 $relType = CRM_Utils_Request
::retrieve('rel_type', 'String', CRM_Core_DAO
::$_nullObject, TRUE);
228 $relContactID = CRM_Utils_Request
::retrieve('rel_contact', 'Positive', CRM_Core_DAO
::$_nullObject, TRUE);
229 $originalCid = CRM_Utils_Request
::retrieve('cid', 'Positive');
230 $relationshipID = CRM_Utils_Request
::retrieve('rel_id', 'Positive');
231 $caseID = CRM_Utils_Request
::retrieve('case_id', 'Positive', CRM_Core_DAO
::$_nullObject, TRUE);
233 if (!CRM_Case_BAO_Case
::accessCase($caseID)) {
234 CRM_Utils_System
::permissionDenied();
237 $ret = array('is_error' => 0);
239 list($relTypeId, $b, $a) = explode('_', $relType);
241 if ($relationshipID && $originalCid) {
242 CRM_Case_BAO_Case
::endCaseRole($caseID, $a, $originalCid, $relTypeId);
245 $clientList = CRM_Case_BAO_Case
::getCaseClients($caseID);
247 // Loop through multiple case clients
248 foreach ($clientList as $i => $sourceContactID) {
250 $result = civicrm_api3('relationship', 'create', array(
251 'case_id' => $caseID,
252 'relationship_type_id' => $relTypeId,
253 "contact_id_$a" => $relContactID,
254 "contact_id_$b" => $sourceContactID,
255 'start_date' => 'now',
258 catch (CiviCRM_API3_Exception
$e) {
259 $ret['is_error'] = 1;
260 $ret['error_message'] = $e->getMessage();
262 // Save activity only for the primary (first) client
263 if ($i == 0 && empty($result['is_error'])) {
264 CRM_Case_BAO_Case
::createCaseRoleActivity($caseID, $result['id'], $relContactID, $sourceContactID);
267 if (!empty($_REQUEST['is_unit_test'])) {
271 CRM_Utils_JSON
::output($ret);
275 * Fetch the custom field help.
277 public static function customField() {
278 $fieldId = CRM_Utils_Type
::escape($_REQUEST['id'], 'Integer');
279 $params = array('id' => $fieldId);
280 $returnProperties = array('help_pre', 'help_post');
283 CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_CustomField', $params, $values, $returnProperties);
284 CRM_Utils_JSON
::output($values);
287 public static function groupTree() {
288 CRM_Utils_System
::setHttpHeader('Content-Type', 'application/json');
289 $gids = CRM_Utils_Type
::escape($_GET['gids'], 'String');
290 echo CRM_Contact_BAO_GroupNestingCache
::json($gids);
291 CRM_Utils_System
::civiExit();
295 * Delete custom value.
297 public static function deleteCustomValue() {
298 CRM_Utils_System
::setHttpHeader('Content-Type', 'text/plain');
299 $customValueID = CRM_Utils_Type
::escape($_REQUEST['valueID'], 'Positive');
300 $customGroupID = CRM_Utils_Type
::escape($_REQUEST['groupID'], 'Positive');
301 $contactId = CRM_Utils_Request
::retrieve('contactId', 'Positive');
302 CRM_Core_BAO_CustomValue
::deleteCustomValue($customValueID, $customGroupID);
304 echo CRM_Contact_BAO_Contact
::getCountComponent('custom_' . $customGroupID, $contactId);
307 CRM_Contact_BAO_GroupContactCache
::opportunisticCacheFlush();
308 CRM_Utils_System
::civiExit();
312 * check the CMS username.
314 static public function checkUserName() {
315 $signer = new CRM_Utils_Signer(CRM_Core_Key
::privateKey(), array('for', 'ts'));
316 $sig = CRM_Utils_Request
::retrieve('sig', 'String');
317 $for = CRM_Utils_Request
::retrieve('for', 'String');
319 CRM_Utils_Time
::getTimeRaw() > $_REQUEST['ts'] + self
::CHECK_USERNAME_TTL
320 ||
$for != 'civicrm/ajax/cmsuser'
321 ||
!$signer->validate($sig, $_REQUEST)
323 $user = array('name' => 'error');
324 CRM_Utils_JSON
::output($user);
327 $config = CRM_Core_Config
::singleton();
328 $username = trim(CRM_Utils_Array
::value('cms_name', $_REQUEST));
330 $params = array('name' => $username);
333 $config->userSystem
->checkUserNameEmailExists($params, $errors);
335 if (isset($errors['cms_name']) ||
isset($errors['name'])) {
336 //user name is not available
337 $user = array('name' => 'no');
338 CRM_Utils_JSON
::output($user);
341 //user name is available
342 $user = array('name' => 'yes');
343 CRM_Utils_JSON
::output($user);
346 // Not reachable: JSON::output() above exits.
347 CRM_Utils_System
::civiExit();
351 * Function to get email address of a contact.
353 public static function getContactEmail() {
354 if (!empty($_REQUEST['contact_id'])) {
355 $contactID = CRM_Utils_Type
::escape($_REQUEST['contact_id'], 'Positive');
356 if (!CRM_Contact_BAO_Contact_Permission
::allow($contactID, CRM_Core_Permission
::EDIT
)) {
361 ) = CRM_Contact_BAO_Contact_Location
::getEmailDetails($contactID);
363 CRM_Utils_System
::setHttpHeader('Content-Type', 'text/plain');
369 $noemail = CRM_Utils_Array
::value('noemail', $_GET);
371 $name = CRM_Utils_Array
::value('name', $_GET);
373 $name = CRM_Utils_Type
::escape($name, 'String');
375 $queryString = " cc.sort_name LIKE '%$name%'";
378 $queryString = " ( cc.sort_name LIKE '%$name%' OR ce.email LIKE '%$name%' ) ";
382 $cid = CRM_Utils_Array
::value('cid', $_GET);
384 //check cid for integer
385 $contIDS = explode(',', $cid);
386 foreach ($contIDS as $contID) {
387 CRM_Utils_Type
::escape($contID, 'Integer');
389 $queryString = " cc.id IN ( $cid )";
395 $offset = CRM_Utils_Array
::value('offset', $_GET, 0);
396 $rowCount = Civi
::settings()->get('search_autocomplete_count');
398 $offset = CRM_Utils_Type
::escape($offset, 'Int');
400 // add acl clause here
401 list($aclFrom, $aclWhere) = CRM_Contact_BAO_Contact_Permission
::cacheClause('cc');
403 $aclWhere = " AND $aclWhere";
407 SELECT sort_name name, cc.id
408 FROM civicrm_contact cc
410 WHERE cc.is_deceased = 0 AND {$queryString}
412 LIMIT {$offset}, {$rowCount}
415 // send query to hook to be modified if needed
416 CRM_Utils_Hook
::contactListQuery($query,
418 CRM_Utils_Request
::retrieve('context', 'String'),
419 CRM_Utils_Request
::retrieve('cid', 'Positive')
422 $dao = CRM_Core_DAO
::executeQuery($query);
423 while ($dao->fetch()) {
426 'text' => $dao->name
,
432 SELECT sort_name name, ce.email, cc.id
433 FROM civicrm_email ce INNER JOIN civicrm_contact cc ON cc.id = ce.contact_id
435 WHERE ce.on_hold = 0 AND cc.is_deceased = 0 AND cc.do_not_email = 0 AND {$queryString}
437 LIMIT {$offset}, {$rowCount}
440 // send query to hook to be modified if needed
441 CRM_Utils_Hook
::contactListQuery($query,
443 CRM_Utils_Request
::retrieve('context', 'String'),
444 CRM_Utils_Request
::retrieve('cid', 'Positive')
447 $dao = CRM_Core_DAO
::executeQuery($query);
449 while ($dao->fetch()) {
452 'text' => '"' . $dao->name
. '" <' . $dao->email
. '>',
453 'id' => (CRM_Utils_Array
::value('id', $_GET)) ?
"{$dao->id}::{$dao->email}" : '"' . $dao->name
. '" <' . $dao->email
. '>',
457 CRM_Utils_JSON
::output($result);
460 CRM_Utils_System
::civiExit();
463 public static function getContactPhone() {
466 //check for mobile type
467 $phoneTypes = CRM_Core_OptionGroup
::values('phone_type', TRUE, FALSE, FALSE, NULL, 'name');
468 $mobileType = CRM_Utils_Array
::value('Mobile', $phoneTypes);
470 $name = CRM_Utils_Array
::value('name', $_GET);
472 $name = CRM_Utils_Type
::escape($name, 'String');
473 $queryString = " ( cc.sort_name LIKE '%$name%' OR cp.phone LIKE '%$name%' ) ";
476 $cid = CRM_Utils_Array
::value('cid', $_GET);
478 //check cid for integer
479 $contIDS = explode(',', $cid);
480 foreach ($contIDS as $contID) {
481 CRM_Utils_Type
::escape($contID, 'Integer');
483 $queryString = " cc.id IN ( $cid )";
489 $offset = CRM_Utils_Array
::value('offset', $_GET, 0);
490 $rowCount = CRM_Utils_Array
::value('rowcount', $_GET, 20);
492 $offset = CRM_Utils_Type
::escape($offset, 'Int');
493 $rowCount = CRM_Utils_Type
::escape($rowCount, 'Int');
495 // add acl clause here
496 list($aclFrom, $aclWhere) = CRM_Contact_BAO_Contact_Permission
::cacheClause('cc');
498 $aclWhere = " AND $aclWhere";
502 SELECT sort_name name, cp.phone, cc.id
503 FROM civicrm_phone cp INNER JOIN civicrm_contact cc ON cc.id = cp.contact_id
505 WHERE cc.is_deceased = 0 AND cc.do_not_sms = 0 AND cp.phone_type_id = {$mobileType} AND {$queryString}
507 LIMIT {$offset}, {$rowCount}
510 // send query to hook to be modified if needed
511 CRM_Utils_Hook
::contactListQuery($query,
513 CRM_Utils_Request
::retrieve('context', 'String'),
514 CRM_Utils_Request
::retrieve('cid', 'Positive')
517 $dao = CRM_Core_DAO
::executeQuery($query);
519 while ($dao->fetch()) {
521 'text' => '"' . $dao->name
. '" (' . $dao->phone
. ')',
522 'id' => (CRM_Utils_Array
::value('id', $_GET)) ?
"{$dao->id}::{$dao->phone}" : '"' . $dao->name
. '" <' . $dao->phone
. '>',
525 CRM_Utils_JSON
::output($result);
527 CRM_Utils_System
::civiExit();
531 public static function buildSubTypes() {
532 $parent = CRM_Utils_Request
::retrieve('parentId', 'Positive');
536 $contactType = 'Individual';
540 $contactType = 'Household';
544 $contactType = 'Organization';
548 $subTypes = CRM_Contact_BAO_ContactType
::subTypePairs($contactType, FALSE, NULL);
550 CRM_Utils_JSON
::output($subTypes);
553 public static function buildDedupeRules() {
554 $parent = CRM_Utils_Request
::retrieve('parentId', 'Positive');
558 $contactType = 'Individual';
562 $contactType = 'Household';
566 $contactType = 'Organization';
570 $dedupeRules = CRM_Dedupe_BAO_RuleGroup
::getByType($contactType);
572 CRM_Utils_JSON
::output($dedupeRules);
576 * Function used for CiviCRM dashboard operations.
578 public static function dashboard() {
579 switch ($_REQUEST['op']) {
581 CRM_Core_BAO_Dashboard
::saveDashletChanges($_REQUEST['columns']);
584 case 'delete_dashlet':
585 $dashletID = CRM_Utils_Type
::escape($_REQUEST['dashlet_id'], 'Positive');
586 CRM_Core_BAO_Dashboard
::deleteDashlet($dashletID);
589 CRM_Utils_System
::civiExit();
593 * Retrieve signature based on email id.
595 public static function getSignature() {
596 $emailID = CRM_Utils_Type
::escape($_REQUEST['emailID'], 'Positive');
597 $query = "SELECT signature_text, signature_html FROM civicrm_email WHERE id = {$emailID}";
598 $dao = CRM_Core_DAO
::executeQuery($query);
600 $signatures = array();
601 while ($dao->fetch()) {
603 'signature_text' => $dao->signature_text
,
604 'signature_html' => $dao->signature_html
,
608 CRM_Utils_JSON
::output($signatures);
614 public static function processDupes() {
615 $oper = CRM_Utils_Type
::escape($_REQUEST['op'], 'String');
616 $cid = CRM_Utils_Type
::escape($_REQUEST['cid'], 'Positive');
617 $oid = CRM_Utils_Type
::escape($_REQUEST['oid'], 'Positive');
619 if (!$oper ||
!$cid ||
!$oid) {
623 $exception = new CRM_Dedupe_DAO_Exception();
624 $exception->contact_id1
= $cid;
625 $exception->contact_id2
= $oid;
626 //make sure contact2 > contact1.
628 $exception->contact_id1
= $oid;
629 $exception->contact_id2
= $cid;
631 $exception->find(TRUE);
633 if ($oper == 'dupe-nondupe') {
634 $status = $exception->save();
636 if ($oper == 'nondupe-dupe') {
637 $status = $exception->delete();
640 CRM_Utils_JSON
::output(array('status' => ($status) ?
$oper : $status));
644 * Retrieve list of duplicate pairs from cache table.
646 public static function getDedupes() {
647 $offset = isset($_REQUEST['start']) ? CRM_Utils_Type
::escape($_REQUEST['start'], 'Integer') : 0;
648 $rowCount = isset($_REQUEST['length']) ? CRM_Utils_Type
::escape($_REQUEST['length'], 'Integer') : 25;
650 $gid = CRM_Utils_Request
::retrieve('gid', 'Positive');
651 $rgid = CRM_Utils_Request
::retrieve('rgid', 'Positive');
653 $criteria = CRM_Utils_Request
::retrieve('criteria', 'Json', $null, FALSE, '{}');
654 $selected = isset($_REQUEST['selected']) ? CRM_Utils_Type
::escape($_REQUEST['selected'], 'Integer') : 0;
659 $whereClause = $orderByClause = '';
660 $cacheKeyString = CRM_Dedupe_Merger
::getMergeCacheKeyString($rgid, $gid, json_decode($criteria, TRUE));
662 $searchRows = array();
664 $searchParams = self
::getSearchOptionsFromRequest();
665 $queryParams = array();
670 $isOrQuery = self
::isOrQuery();
674 'dst' => 'cc1.display_name',
675 'src' => 'cc2.display_name',
676 'dst_email' => 'ce1.email',
677 'src_email' => 'ce2.email',
678 'dst_postcode' => 'ca1.postal_code',
679 'src_postcode' => 'ca2.postal_code',
680 'dst_street' => 'ca1.street',
681 'src_street' => 'ca2.street',
684 foreach ($mappings as $key => $dbName) {
685 if (!empty($searchParams[$key])) {
687 $wildcard = strstr($key, 'postcode') ?
'' : '%';
688 $queryParams[$nextParamKey] = array($wildcard . $searchParams[$key] . '%', 'String');
689 $where[] = $dbName . " LIKE %{$nextParamKey} ";
695 $whereClause = ' ( ' . implode(' OR ', $where) . ' ) ';
698 if (!empty($where)) {
699 $whereClause = implode(' AND ', $where);
702 $whereClause .= $whereClause ?
' AND de.id IS NULL' : ' de.id IS NULL';
705 $whereClause .= ' AND pn.is_selected = 1';
707 $join .= CRM_Dedupe_Merger
::getJoinOnDedupeTable();
710 'cc1.contact_type' => 'dst_contact_type',
711 'cc1.display_name' => 'dst_display_name',
712 'cc1.contact_sub_type' => 'dst_contact_sub_type',
713 'cc2.contact_type' => 'src_contact_type',
714 'cc2.display_name' => 'src_display_name',
715 'cc2.contact_sub_type' => 'src_contact_sub_type',
716 'ce1.email' => 'dst_email',
717 'ce2.email' => 'src_email',
718 'ca1.postal_code' => 'dst_postcode',
719 'ca2.postal_code' => 'src_postcode',
720 'ca1.street_address' => 'dst_street',
721 'ca2.street_address' => 'src_street',
725 $join .= " INNER JOIN civicrm_contact cc1 ON cc1.id = pn.entity_id1";
726 $join .= " INNER JOIN civicrm_contact cc2 ON cc2.id = pn.entity_id2";
727 $join .= " LEFT JOIN civicrm_email ce1 ON (ce1.contact_id = pn.entity_id1 AND ce1.is_primary = 1 )";
728 $join .= " LEFT JOIN civicrm_email ce2 ON (ce2.contact_id = pn.entity_id2 AND ce2.is_primary = 1 )";
729 $join .= " LEFT JOIN civicrm_address ca1 ON (ca1.contact_id = pn.entity_id1 AND ca1.is_primary = 1 )";
730 $join .= " LEFT JOIN civicrm_address ca2 ON (ca2.contact_id = pn.entity_id2 AND ca2.is_primary = 1 )";
732 $iTotal = CRM_Core_BAO_PrevNextCache
::getCount($cacheKeyString, $join, $whereClause, '=', $queryParams);
733 if (!empty($_REQUEST['order'])) {
734 foreach ($_REQUEST['order'] as $orderInfo) {
735 if (!empty($orderInfo['column'])) {
736 $orderColumnNumber = $orderInfo['column'];
737 $dir = $orderInfo['dir'];
740 $columnDetails = CRM_Utils_Array
::value($orderColumnNumber, $_REQUEST['columns']);
742 if (!empty($columnDetails)) {
743 switch ($columnDetails['data']) {
745 $orderByClause = " ORDER BY cc2.display_name {$dir}";
749 $orderByClause = " ORDER BY ce2.email {$dir}";
753 $orderByClause = " ORDER BY ca2.street_address {$dir}";
757 $orderByClause = " ORDER BY ca2.postal_code {$dir}";
761 $orderByClause = " ORDER BY cc1.display_name {$dir}";
765 $orderByClause = " ORDER BY ce1.email {$dir}";
769 $orderByClause = " ORDER BY ca1.street_address {$dir}";
773 $orderByClause = " ORDER BY ca1.postal_code {$dir}";
777 $orderByClause = " ORDER BY cc1.display_name ASC";
782 $dupePairs = CRM_Core_BAO_PrevNextCache
::retrieve($cacheKeyString, $join, $whereClause, $offset, $rowCount, $select, $orderByClause, TRUE, $queryParams);
783 $iFilteredTotal = CRM_Core_DAO
::singleValueQuery("SELECT FOUND_ROWS()");
786 foreach ($dupePairs as $key => $pairInfo) {
787 $pair = $pairInfo['data'];
788 $srcContactSubType = CRM_Utils_Array
::value('src_contact_sub_type', $pairInfo);
789 $dstContactSubType = CRM_Utils_Array
::value('dst_contact_sub_type', $pairInfo);
790 $srcTypeImage = CRM_Contact_BAO_Contact_Utils
::getImage($srcContactSubType ?
791 $srcContactSubType : $pairInfo['src_contact_type'],
793 $pairInfo['entity_id2']
795 $dstTypeImage = CRM_Contact_BAO_Contact_Utils
::getImage($dstContactSubType ?
796 $dstContactSubType : $pairInfo['dst_contact_type'],
798 $pairInfo['entity_id1']
801 $searchRows[$count]['is_selected'] = $pairInfo['is_selected'];
802 $searchRows[$count]['is_selected_input'] = "<input type='checkbox' class='crm-dedupe-select' name='pnid_{$pairInfo['prevnext_id']}' value='{$pairInfo['is_selected']}' onclick='toggleDedupeSelect(this)'>";
803 $searchRows[$count]['src_image'] = $srcTypeImage;
804 $searchRows[$count]['src'] = CRM_Utils_System
::href($pair['srcName'], 'civicrm/contact/view', "reset=1&cid={$pairInfo['entity_id2']}");
805 $searchRows[$count]['src_email'] = CRM_Utils_Array
::value('src_email', $pairInfo);
806 $searchRows[$count]['src_street'] = CRM_Utils_Array
::value('src_street', $pairInfo);
807 $searchRows[$count]['src_postcode'] = CRM_Utils_Array
::value('src_postcode', $pairInfo);
808 $searchRows[$count]['dst_image'] = $dstTypeImage;
809 $searchRows[$count]['dst'] = CRM_Utils_System
::href($pair['dstName'], 'civicrm/contact/view', "reset=1&cid={$pairInfo['entity_id1']}");
810 $searchRows[$count]['dst_email'] = CRM_Utils_Array
::value('dst_email', $pairInfo);
811 $searchRows[$count]['dst_street'] = CRM_Utils_Array
::value('dst_street', $pairInfo);
812 $searchRows[$count]['dst_postcode'] = CRM_Utils_Array
::value('dst_postcode', $pairInfo);
813 $searchRows[$count]['conflicts'] = str_replace("',", "',<br/>", CRM_Utils_Array
::value('conflicts', $pair));
814 $searchRows[$count]['weight'] = CRM_Utils_Array
::value('weight', $pair);
816 if (!empty($pairInfo['data']['canMerge'])) {
819 'cid' => $pairInfo['entity_id1'],
820 'oid' => $pairInfo['entity_id2'],
821 'action' => 'update',
823 'criteria' => $criteria,
824 'limit' => CRM_Utils_Request
::retrieve('limit', 'Integer'),
827 $mergeParams['gid'] = $gid;
830 $searchRows[$count]['actions'] = "<a class='crm-dedupe-flip' href='#' data-pnid={$pairInfo['prevnext_id']}>" . ts('flip') . "</a> | ";
831 $searchRows[$count]['actions'] .= CRM_Utils_System
::href(ts('merge'), 'civicrm/contact/merge', $mergeParams);
832 $searchRows[$count]['actions'] .= " | <a id='notDuplicate' href='#' onClick=\"processDupes( {$pairInfo['entity_id1']}, {$pairInfo['entity_id2']}, 'dupe-nondupe', 'dupe-listing'); return false;\">" . ts('not a duplicate') . "</a>";
835 $searchRows[$count]['actions'] = '<em>' . ts('Insufficient access rights - cannot merge') . '</em>';
841 'data' => $searchRows,
842 'recordsTotal' => $iTotal,
843 'recordsFiltered' => $iFilteredTotal,
845 if (!empty($_REQUEST['is_unit_test'])) {
848 CRM_Utils_JSON
::output($dupePairs);
852 * Get the searchable options from the request.
856 public static function getSearchOptionsFromRequest() {
857 $searchParams = array();
858 $searchData = CRM_Utils_Array
::value('search', $_REQUEST);
859 $searchData['value'] = CRM_Utils_Type
::escape($searchData['value'], 'String');
860 $selectorElements = array(
877 $columns = $_REQUEST['columns'];
879 foreach ($columns as $column) {
880 if (!empty($column['search']['value']) && in_array($column['data'], $selectorElements)) {
881 $searchParams[$column['data']] = CRM_Utils_Type
::escape($column['search']['value'], 'String');
883 elseif (!empty($searchData['value'])) {
884 $searchParams[$column['data']] = $searchData['value'];
887 return $searchParams;
891 * Is the query an OR query.
893 * If a generic search value is passed in - ie. $_REQUEST['search']['value'] = 'abc'
894 * then all fields are searched for this.
896 * It is unclear if there is any code that still passes this in or whether is is just legacy. It
897 * could cause a server-killing query on a large site so it probably is NOT in use if we haven't
902 public static function isOrQuery() {
903 $searchData = CRM_Utils_Array
::value('search', $_REQUEST);
904 return !empty($searchData['value']);
908 * Retrieve a PDF Page Format for the PDF Letter form.
910 public function pdfFormat() {
911 $formatId = CRM_Utils_Type
::escape($_REQUEST['formatId'], 'Integer');
913 $pdfFormat = CRM_Core_BAO_PdfFormat
::getById($formatId);
915 CRM_Utils_JSON
::output($pdfFormat);
919 * Retrieve Paper Size dimensions.
921 public static function paperSize() {
922 $paperSizeName = CRM_Utils_Type
::escape($_REQUEST['paperSizeName'], 'String');
924 $paperSize = CRM_Core_BAO_PaperSize
::getByName($paperSizeName);
926 CRM_Utils_JSON
::output($paperSize);
930 * Swap contacts in a dupe pair i.e main with duplicate contact.
932 * @param int $prevNextId
934 public static function flipDupePairs($prevNextId = NULL) {
936 // @todo figure out if this is always POST & specify that rather than inexact GET
937 $prevNextId = CRM_Utils_Request
::retrieve('pnid', 'Integer');
940 $onlySelected = FALSE;
941 if (is_array($prevNextId) && !CRM_Utils_Array
::crmIsEmptyArray($prevNextId)) {
942 $onlySelected = TRUE;
944 $prevNextId = CRM_Utils_Type
::escapeAll((array) $prevNextId, 'Positive');
945 CRM_Core_BAO_PrevNextCache
::flipPair($prevNextId, $onlySelected);
946 CRM_Utils_System
::civiExit();
950 * Used to store selected contacts across multiple pages in advanced search.
952 public static function selectUnselectContacts() {
953 $name = CRM_Utils_Array
::value('name', $_REQUEST);
954 $cacheKey = CRM_Utils_Array
::value('qfKey', $_REQUEST);
955 $state = CRM_Utils_Array
::value('state', $_REQUEST, 'checked');
956 $variableType = CRM_Utils_Array
::value('variableType', $_REQUEST, 'single');
958 $actionToPerform = CRM_Utils_Array
::value('action', $_REQUEST, 'select');
960 if ($variableType == 'multiple') {
961 // action post value only works with multiple type variable
963 //multiple names like mark_x_1-mark_x_2 where 1,2 are cids
964 $elements = explode('-', $name);
965 foreach ($elements as $key => $element) {
966 $elements[$key] = self
::_convertToId($element);
968 CRM_Utils_Type
::escapeAll($elements, 'Integer');
969 CRM_Core_BAO_PrevNextCache
::markSelection($cacheKey, $actionToPerform, $elements);
972 CRM_Core_BAO_PrevNextCache
::markSelection($cacheKey, $actionToPerform);
975 elseif ($variableType == 'single') {
976 $cId = self
::_convertToId($name);
977 CRM_Utils_Type
::escape($cId, 'Integer');
978 $action = ($state == 'checked') ?
'select' : 'unselect';
979 CRM_Core_BAO_PrevNextCache
::markSelection($cacheKey, $action, $cId);
981 $contactIds = CRM_Core_BAO_PrevNextCache
::getSelection($cacheKey);
982 $countSelectionCids = count($contactIds[$cacheKey]);
984 $arrRet = array('getCount' => $countSelectionCids);
985 CRM_Utils_JSON
::output($arrRet);
989 * @param string $name
993 public static function _convertToId($name) {
994 if (substr($name, 0, CRM_Core_Form
::CB_PREFIX_LEN
) == CRM_Core_Form
::CB_PREFIX
) {
995 $cId = substr($name, CRM_Core_Form
::CB_PREFIX_LEN
);
1000 public static function getAddressDisplay() {
1001 $contactId = CRM_Utils_Request
::retrieve('contact_id', 'Positive');
1003 $addressVal["error_message"] = "no contact id found";
1006 $entityBlock = array(
1007 'contact_id' => $contactId,
1008 'entity_id' => $contactId,
1010 $addressVal = CRM_Core_BAO_Address
::getValues($entityBlock);
1013 CRM_Utils_JSON
::output($addressVal);
1017 * Mark dupe pairs as selected from un-selected state or vice-versa, in dupe cache table.
1019 public static function toggleDedupeSelect() {
1020 $rgid = CRM_Utils_Type
::escape($_REQUEST['rgid'], 'Integer');
1021 $gid = CRM_Utils_Type
::escape($_REQUEST['gid'], 'Integer');
1022 $pnid = $_REQUEST['pnid'];
1023 $isSelected = CRM_Utils_Type
::escape($_REQUEST['is_selected'], 'Boolean');
1024 $criteria = CRM_Utils_Request
::retrieve('criteria', 'Json', $null, FALSE, '{}');
1026 $cacheKeyString = CRM_Dedupe_Merger
::getMergeCacheKeyString($rgid, $gid, json_decode($criteria, TRUE));
1029 1 => array($isSelected, 'Boolean'),
1030 3 => array("$cacheKeyString%", 'String'), // using % to address rows with conflicts as well
1033 //check pnid is_array or integer
1034 $whereClause = NULL;
1035 if (is_array($pnid) && !CRM_Utils_Array
::crmIsEmptyArray($pnid)) {
1036 CRM_Utils_Type
::escapeAll($pnid, 'Positive');
1037 $pnid = implode(', ', $pnid);
1038 $whereClause = " id IN ( {$pnid} ) ";
1041 $pnid = CRM_Utils_Type
::escape($pnid, 'Integer');
1042 $whereClause = " id = %2";
1043 $params[2] = array($pnid, 'Integer');
1046 $sql = "UPDATE civicrm_prevnext_cache SET is_selected = %1 WHERE {$whereClause} AND cacheKey LIKE %3";
1047 CRM_Core_DAO
::executeQuery($sql, $params);
1049 CRM_Utils_System
::civiExit();
1053 * Retrieve contact relationships.
1055 public static function getContactRelationships() {
1056 $contactID = CRM_Utils_Type
::escape($_GET['cid'], 'Integer');
1057 $context = CRM_Utils_Type
::escape($_GET['context'], 'String');
1058 $relationship_type_id = CRM_Utils_Type
::escape(CRM_Utils_Array
::value('relationship_type_id', $_GET), 'Integer', FALSE);
1060 if (!CRM_Contact_BAO_Contact_Permission
::allow($contactID)) {
1061 return CRM_Utils_System
::permissionDenied();
1064 $params = CRM_Core_Page_AJAX
::defaultSortAndPagerParams();
1066 $params['contact_id'] = $contactID;
1067 $params['context'] = $context;
1068 if ($relationship_type_id) {
1069 $params['relationship_type_id'] = $relationship_type_id;
1072 // get the contact relationships
1073 $relationships = CRM_Contact_BAO_Relationship
::getContactRelationshipSelector($params);
1075 CRM_Utils_JSON
::output($relationships);