3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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-2016
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);
227 * @throws \CiviCRM_API3_Exception
229 public static function relationship() {
230 $relType = CRM_Utils_Request
::retrieve('rel_type', 'String', CRM_Core_DAO
::$_nullObject, TRUE);
231 $relContactID = CRM_Utils_Request
::retrieve('rel_contact', 'Positive', CRM_Core_DAO
::$_nullObject, TRUE);
232 $originalCid = CRM_Utils_Request
::retrieve('cid', 'Positive', CRM_Core_DAO
::$_nullObject);
233 $relationshipID = CRM_Utils_Request
::retrieve('rel_id', 'Positive', CRM_Core_DAO
::$_nullObject);
234 $caseID = CRM_Utils_Request
::retrieve('case_id', 'Positive', CRM_Core_DAO
::$_nullObject, TRUE);
236 if (!CRM_Case_BAO_Case
::accessCase($caseID)) {
237 CRM_Utils_System
::permissionDenied();
240 $ret = array('is_error' => 0);
242 list($relTypeId, $b, $a) = explode('_', $relType);
244 if ($relationshipID && $originalCid) {
245 CRM_Case_BAO_Case
::endCaseRole($caseID, $a, $originalCid, $relTypeId);
248 $clientList = CRM_Case_BAO_Case
::getCaseClients($caseID);
250 // Loop through multiple case clients
251 foreach ($clientList as $i => $sourceContactID) {
252 $result = civicrm_api3('relationship', 'create', array(
253 'case_id' => $caseID,
254 'relationship_type_id' => $relTypeId,
255 "contact_id_$a" => $relContactID,
256 "contact_id_$b" => $sourceContactID,
257 'start_date' => 'now',
259 // Save activity only for the primary (first) client
260 if ($i == 0 && empty($result['is_error'])) {
261 CRM_Case_BAO_Case
::createCaseRoleActivity($caseID, $result['id'], $relContactID);
265 CRM_Utils_JSON
::output($ret);
269 * Fetch the custom field help.
271 public static function customField() {
272 $fieldId = CRM_Utils_Type
::escape($_REQUEST['id'], 'Integer');
273 $params = array('id' => $fieldId);
274 $returnProperties = array('help_pre', 'help_post');
277 CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_CustomField', $params, $values, $returnProperties);
278 CRM_Utils_JSON
::output($values);
281 public static function groupTree() {
282 CRM_Utils_System
::setHttpHeader('Content-Type', 'application/json');
283 $gids = CRM_Utils_Type
::escape($_GET['gids'], 'String');
284 echo CRM_Contact_BAO_GroupNestingCache
::json($gids);
285 CRM_Utils_System
::civiExit();
289 * Delete custom value.
291 public static function deleteCustomValue() {
292 CRM_Utils_System
::setHttpHeader('Content-Type', 'text/plain');
293 $customValueID = CRM_Utils_Type
::escape($_REQUEST['valueID'], 'Positive');
294 $customGroupID = CRM_Utils_Type
::escape($_REQUEST['groupID'], 'Positive');
295 $contactId = CRM_Utils_Request
::retrieve('contactId', 'Positive', CRM_Core_DAO
::$_nullObject);
296 CRM_Core_BAO_CustomValue
::deleteCustomValue($customValueID, $customGroupID);
298 echo CRM_Contact_BAO_Contact
::getCountComponent('custom_' . $customGroupID, $contactId);
301 CRM_Contact_BAO_GroupContactCache
::opportunisticCacheFlush();
302 CRM_Utils_System
::civiExit();
306 * check the CMS username.
308 static public function checkUserName() {
309 $signer = new CRM_Utils_Signer(CRM_Core_Key
::privateKey(), array('for', 'ts'));
310 $sig = CRM_Utils_Request
::retrieve('sig', 'String', CRM_Core_DAO
::$_nullObject);
311 $for = CRM_Utils_Request
::retrieve('for', 'String', CRM_Core_DAO
::$_nullObject);
313 CRM_Utils_Time
::getTimeRaw() > $_REQUEST['ts'] + self
::CHECK_USERNAME_TTL
314 ||
$for != 'civicrm/ajax/cmsuser'
315 ||
!$signer->validate($sig, $_REQUEST)
317 $user = array('name' => 'error');
318 CRM_Utils_JSON
::output($user);
321 $config = CRM_Core_Config
::singleton();
322 $username = trim(CRM_Utils_Array
::value('cms_name', $_REQUEST));
324 $params = array('name' => $username);
327 $config->userSystem
->checkUserNameEmailExists($params, $errors);
329 if (isset($errors['cms_name']) ||
isset($errors['name'])) {
330 //user name is not available
331 $user = array('name' => 'no');
332 CRM_Utils_JSON
::output($user);
335 //user name is available
336 $user = array('name' => 'yes');
337 CRM_Utils_JSON
::output($user);
340 // Not reachable: JSON::output() above exits.
341 CRM_Utils_System
::civiExit();
345 * Function to get email address of a contact.
347 public static function getContactEmail() {
348 if (!empty($_REQUEST['contact_id'])) {
349 $contactID = CRM_Utils_Type
::escape($_REQUEST['contact_id'], 'Positive');
350 if (!CRM_Contact_BAO_Contact_Permission
::allow($contactID, CRM_Core_Permission
::EDIT
)) {
355 ) = CRM_Contact_BAO_Contact_Location
::getEmailDetails($contactID);
357 CRM_Utils_System
::setHttpHeader('Content-Type', 'text/plain');
363 $noemail = CRM_Utils_Array
::value('noemail', $_GET);
365 $name = CRM_Utils_Array
::value('name', $_GET);
367 $name = CRM_Utils_Type
::escape($name, 'String');
369 $queryString = " cc.sort_name LIKE '%$name%'";
372 $queryString = " ( cc.sort_name LIKE '%$name%' OR ce.email LIKE '%$name%' ) ";
376 $cid = CRM_Utils_Array
::value('cid', $_GET);
378 //check cid for integer
379 $contIDS = explode(',', $cid);
380 foreach ($contIDS as $contID) {
381 CRM_Utils_Type
::escape($contID, 'Integer');
383 $queryString = " cc.id IN ( $cid )";
388 $offset = CRM_Utils_Array
::value('offset', $_GET, 0);
389 $rowCount = Civi
::settings()->get('search_autocomplete_count');
391 $offset = CRM_Utils_Type
::escape($offset, 'Int');
393 // add acl clause here
394 list($aclFrom, $aclWhere) = CRM_Contact_BAO_Contact_Permission
::cacheClause('cc');
396 $aclWhere = " AND $aclWhere";
400 SELECT sort_name name, cc.id
401 FROM civicrm_contact cc
403 WHERE cc.is_deceased = 0 AND {$queryString}
405 LIMIT {$offset}, {$rowCount}
408 // send query to hook to be modified if needed
409 CRM_Utils_Hook
::contactListQuery($query,
411 CRM_Utils_Request
::retrieve('context', 'String', CRM_Core_DAO
::$_nullObject),
412 CRM_Utils_Request
::retrieve('cid', 'Positive', CRM_Core_DAO
::$_nullObject)
415 $dao = CRM_Core_DAO
::executeQuery($query);
416 while ($dao->fetch()) {
419 'text' => $dao->name
,
425 SELECT sort_name name, ce.email, cc.id
426 FROM civicrm_email ce INNER JOIN civicrm_contact cc ON cc.id = ce.contact_id
428 WHERE ce.on_hold = 0 AND cc.is_deceased = 0 AND cc.do_not_email = 0 AND {$queryString}
430 LIMIT {$offset}, {$rowCount}
433 // send query to hook to be modified if needed
434 CRM_Utils_Hook
::contactListQuery($query,
436 CRM_Utils_Request
::retrieve('context', 'String', CRM_Core_DAO
::$_nullObject),
437 CRM_Utils_Request
::retrieve('cid', 'Positive', CRM_Core_DAO
::$_nullObject)
440 $dao = CRM_Core_DAO
::executeQuery($query);
442 while ($dao->fetch()) {
445 'text' => '"' . $dao->name
. '" <' . $dao->email
. '>',
446 'id' => (CRM_Utils_Array
::value('id', $_GET)) ?
"{$dao->id}::{$dao->email}" : '"' . $dao->name
. '" <' . $dao->email
. '>',
451 CRM_Utils_JSON
::output($result);
455 CRM_Utils_System
::civiExit();
458 public static function getContactPhone() {
461 //check for mobile type
462 $phoneTypes = CRM_Core_OptionGroup
::values('phone_type', TRUE, FALSE, FALSE, NULL, 'name');
463 $mobileType = CRM_Utils_Array
::value('Mobile', $phoneTypes);
465 $name = CRM_Utils_Array
::value('name', $_GET);
467 $name = CRM_Utils_Type
::escape($name, 'String');
468 $queryString = " ( cc.sort_name LIKE '%$name%' OR cp.phone LIKE '%$name%' ) ";
471 $cid = CRM_Utils_Array
::value('cid', $_GET);
473 //check cid for integer
474 $contIDS = explode(',', $cid);
475 foreach ($contIDS as $contID) {
476 CRM_Utils_Type
::escape($contID, 'Integer');
478 $queryString = " cc.id IN ( $cid )";
483 $offset = CRM_Utils_Array
::value('offset', $_GET, 0);
484 $rowCount = CRM_Utils_Array
::value('rowcount', $_GET, 20);
486 $offset = CRM_Utils_Type
::escape($offset, 'Int');
487 $rowCount = CRM_Utils_Type
::escape($rowCount, 'Int');
489 // add acl clause here
490 list($aclFrom, $aclWhere) = CRM_Contact_BAO_Contact_Permission
::cacheClause('cc');
492 $aclWhere = " AND $aclWhere";
496 SELECT sort_name name, cp.phone, cc.id
497 FROM civicrm_phone cp INNER JOIN civicrm_contact cc ON cc.id = cp.contact_id
499 WHERE cc.is_deceased = 0 AND cc.do_not_sms = 0 AND cp.phone_type_id = {$mobileType} AND {$queryString}
501 LIMIT {$offset}, {$rowCount}
504 // send query to hook to be modified if needed
505 CRM_Utils_Hook
::contactListQuery($query,
507 CRM_Utils_Request
::retrieve('context', 'String', CRM_Core_DAO
::$_nullObject),
508 CRM_Utils_Request
::retrieve('cid', 'Positive', CRM_Core_DAO
::$_nullObject)
511 $dao = CRM_Core_DAO
::executeQuery($query);
513 while ($dao->fetch()) {
515 'text' => '"' . $dao->name
. '" (' . $dao->phone
. ')',
516 'id' => (CRM_Utils_Array
::value('id', $_GET)) ?
"{$dao->id}::{$dao->phone}" : '"' . $dao->name
. '" <' . $dao->phone
. '>',
522 CRM_Utils_JSON
::output($result);
524 CRM_Utils_System
::civiExit();
528 public static function buildSubTypes() {
529 $parent = CRM_Utils_Request
::retrieve('parentId', 'Positive', CRM_Core_DAO
::$_nullObject);
533 $contactType = 'Individual';
537 $contactType = 'Household';
541 $contactType = 'Organization';
545 $subTypes = CRM_Contact_BAO_ContactType
::subTypePairs($contactType, FALSE, NULL);
547 CRM_Utils_JSON
::output($subTypes);
550 public static function buildDedupeRules() {
551 $parent = CRM_Utils_Request
::retrieve('parentId', 'Positive', CRM_Core_DAO
::$_nullObject);
555 $contactType = 'Individual';
559 $contactType = 'Household';
563 $contactType = 'Organization';
567 $dedupeRules = CRM_Dedupe_BAO_RuleGroup
::getByType($contactType);
569 CRM_Utils_JSON
::output($dedupeRules);
573 * Function used for CiviCRM dashboard operations.
575 public static function dashboard() {
576 switch ($_REQUEST['op']) {
578 CRM_Core_BAO_Dashboard
::saveDashletChanges($_REQUEST['columns']);
581 case 'delete_dashlet':
582 $dashletID = CRM_Utils_Type
::escape($_REQUEST['dashlet_id'], 'Positive');
583 CRM_Core_BAO_Dashboard
::deleteDashlet($dashletID);
586 CRM_Utils_System
::civiExit();
590 * Retrieve signature based on email id.
592 public static function getSignature() {
593 $emailID = CRM_Utils_Type
::escape($_REQUEST['emailID'], 'Positive');
594 $query = "SELECT signature_text, signature_html FROM civicrm_email WHERE id = {$emailID}";
595 $dao = CRM_Core_DAO
::executeQuery($query);
597 $signatures = array();
598 while ($dao->fetch()) {
600 'signature_text' => $dao->signature_text
,
601 'signature_html' => $dao->signature_html
,
605 CRM_Utils_JSON
::output($signatures);
611 public static function processDupes() {
612 $oper = CRM_Utils_Type
::escape($_REQUEST['op'], 'String');
613 $cid = CRM_Utils_Type
::escape($_REQUEST['cid'], 'Positive');
614 $oid = CRM_Utils_Type
::escape($_REQUEST['oid'], 'Positive');
616 if (!$oper ||
!$cid ||
!$oid) {
620 $exception = new CRM_Dedupe_DAO_Exception();
621 $exception->contact_id1
= $cid;
622 $exception->contact_id2
= $oid;
623 //make sure contact2 > contact1.
625 $exception->contact_id1
= $oid;
626 $exception->contact_id2
= $cid;
628 $exception->find(TRUE);
630 if ($oper == 'dupe-nondupe') {
631 $status = $exception->save();
633 if ($oper == 'nondupe-dupe') {
634 $status = $exception->delete();
637 CRM_Utils_JSON
::output(array('status' => ($status) ?
$oper : $status));
641 * Retrieve list of duplicate pairs from cache table.
643 public static function getDedupes() {
644 $offset = isset($_REQUEST['start']) ? CRM_Utils_Type
::escape($_REQUEST['start'], 'Integer') : 0;
645 $rowCount = isset($_REQUEST['length']) ? CRM_Utils_Type
::escape($_REQUEST['length'], 'Integer') : 25;
647 $gid = CRM_Utils_Request
::retrieve('gid', 'Positive');
648 $rgid = CRM_Utils_Request
::retrieve('rgid', 'Positive');
649 $selected = isset($_REQUEST['selected']) ? CRM_Utils_Type
::escape($_REQUEST['selected'], 'Integer') : 0;
654 $whereClause = $orderByClause = '';
655 $cacheKeyString = CRM_Dedupe_Merger
::getMergeCacheKeyString($rgid, $gid);
656 $searchRows = array();
658 $searchParams = self
::getSearchOptionsFromRequest();
659 $queryParams = array();
664 $isOrQuery = self
::isOrQuery();
668 'dst' => 'cc1.display_name',
669 'src' => 'cc2.display_name',
670 'dst_email' => 'ce1.email',
671 'src_email' => 'ce2.email',
672 'dst_postcode' => 'ca1.postal_code',
673 'src_postcode' => 'ca2.postal_code',
674 'dst_street' => 'ca1.street',
675 'src_street' => 'ca2.street',
678 foreach ($mappings as $key => $dbName) {
679 if (!empty($searchParams[$key])) {
680 $queryParams[$nextParamKey] = array('%' . $searchParams[$key] . '%', 'String');
681 $where[] = $dbName . " LIKE %{$nextParamKey} ";
687 $whereClause = ' ( ' . implode(' OR ', $where) . ' ) ';
690 if (!empty($where)) {
691 $whereClause = implode(' AND ', $where);
694 $whereClause .= $whereClause ?
' AND de.id IS NULL' : ' de.id IS NULL';
697 $whereClause .= ' AND pn.is_selected = 1';
699 $join .= CRM_Dedupe_Merger
::getJoinOnDedupeTable();
702 'cc1.contact_type' => 'dst_contact_type',
703 'cc1.display_name' => 'dst_display_name',
704 'cc1.contact_sub_type' => 'dst_contact_sub_type',
705 'cc2.contact_type' => 'src_contact_type',
706 'cc2.display_name' => 'src_display_name',
707 'cc2.contact_sub_type' => 'src_contact_sub_type',
708 'ce1.email' => 'dst_email',
709 'ce2.email' => 'src_email',
710 'ca1.postal_code' => 'dst_postcode',
711 'ca2.postal_code' => 'src_postcode',
712 'ca1.street_address' => 'dst_street',
713 'ca2.street_address' => 'src_street',
717 $join .= " INNER JOIN civicrm_contact cc1 ON cc1.id = pn.entity_id1";
718 $join .= " INNER JOIN civicrm_contact cc2 ON cc2.id = pn.entity_id2";
719 $join .= " LEFT JOIN civicrm_email ce1 ON (ce1.contact_id = pn.entity_id1 AND ce1.is_primary = 1 )";
720 $join .= " LEFT JOIN civicrm_email ce2 ON (ce2.contact_id = pn.entity_id2 AND ce2.is_primary = 1 )";
721 $join .= " LEFT JOIN civicrm_address ca1 ON (ca1.contact_id = pn.entity_id1 AND ca1.is_primary = 1 )";
722 $join .= " LEFT JOIN civicrm_address ca2 ON (ca2.contact_id = pn.entity_id2 AND ca2.is_primary = 1 )";
724 $iTotal = CRM_Core_BAO_PrevNextCache
::getCount($cacheKeyString, $join, $whereClause, '=', $queryParams);
725 if (!empty($_REQUEST['order'])) {
726 foreach ($_REQUEST['order'] as $orderInfo) {
727 if (!empty($orderInfo['column'])) {
728 $orderColumnNumber = $orderInfo['column'];
729 $dir = $orderInfo['dir'];
732 $columnDetails = CRM_Utils_Array
::value($orderColumnNumber, $_REQUEST['columns']);
734 if (!empty($columnDetails)) {
735 switch ($columnDetails['data']) {
737 $orderByClause = " ORDER BY cc2.display_name {$dir}";
741 $orderByClause = " ORDER BY ce2.email {$dir}";
745 $orderByClause = " ORDER BY ca2.street_address {$dir}";
749 $orderByClause = " ORDER BY ca2.postal_code {$dir}";
753 $orderByClause = " ORDER BY cc1.display_name {$dir}";
757 $orderByClause = " ORDER BY ce1.email {$dir}";
761 $orderByClause = " ORDER BY ca1.street_address {$dir}";
765 $orderByClause = " ORDER BY ca1.postal_code {$dir}";
769 $orderByClause = " ORDER BY cc1.display_name ASC";
774 $dupePairs = CRM_Core_BAO_PrevNextCache
::retrieve($cacheKeyString, $join, $whereClause, $offset, $rowCount, $select, $orderByClause, TRUE, $queryParams);
775 $iFilteredTotal = CRM_Core_DAO
::singleValueQuery("SELECT FOUND_ROWS()");
778 foreach ($dupePairs as $key => $pairInfo) {
779 $pair = $pairInfo['data'];
780 $srcContactSubType = CRM_Utils_Array
::value('src_contact_sub_type', $pairInfo);
781 $dstContactSubType = CRM_Utils_Array
::value('dst_contact_sub_type', $pairInfo);
782 $srcTypeImage = CRM_Contact_BAO_Contact_Utils
::getImage($srcContactSubType ?
783 $srcContactSubType : $pairInfo['src_contact_type'],
785 $pairInfo['entity_id2']
787 $dstTypeImage = CRM_Contact_BAO_Contact_Utils
::getImage($dstContactSubType ?
788 $dstContactSubType : $pairInfo['dst_contact_type'],
790 $pairInfo['entity_id1']
793 $searchRows[$count]['is_selected'] = $pairInfo['is_selected'];
794 $searchRows[$count]['is_selected_input'] = "<input type='checkbox' class='crm-dedupe-select' name='pnid_{$pairInfo['prevnext_id']}' value='{$pairInfo['is_selected']}' onclick='toggleDedupeSelect(this)'>";
795 $searchRows[$count]['src_image'] = $srcTypeImage;
796 $searchRows[$count]['src'] = CRM_Utils_System
::href($pair['srcName'], 'civicrm/contact/view', "reset=1&cid={$pairInfo['entity_id2']}");
797 $searchRows[$count]['src_email'] = CRM_Utils_Array
::value('src_email', $pairInfo);
798 $searchRows[$count]['src_street'] = CRM_Utils_Array
::value('src_street', $pairInfo);
799 $searchRows[$count]['src_postcode'] = CRM_Utils_Array
::value('src_postcode', $pairInfo);
800 $searchRows[$count]['dst_image'] = $dstTypeImage;
801 $searchRows[$count]['dst'] = CRM_Utils_System
::href($pair['dstName'], 'civicrm/contact/view', "reset=1&cid={$pairInfo['entity_id1']}");
802 $searchRows[$count]['dst_email'] = CRM_Utils_Array
::value('dst_email', $pairInfo);
803 $searchRows[$count]['dst_street'] = CRM_Utils_Array
::value('dst_street', $pairInfo);
804 $searchRows[$count]['dst_postcode'] = CRM_Utils_Array
::value('dst_postcode', $pairInfo);
805 $searchRows[$count]['conflicts'] = str_replace("',", "',<br/>", CRM_Utils_Array
::value('conflicts', $pair));
806 $searchRows[$count]['weight'] = CRM_Utils_Array
::value('weight', $pair);
808 if (!empty($pairInfo['data']['canMerge'])) {
809 $mergeParams = "reset=1&cid={$pairInfo['entity_id1']}&oid={$pairInfo['entity_id2']}&action=update&rgid={$rgid}&limit=" . CRM_Utils_Request
::retrieve('limit', 'Integer');
811 $mergeParams .= "&gid={$gid}";
814 $searchRows[$count]['actions'] = "<a class='crm-dedupe-flip' href='#' data-pnid={$pairInfo['prevnext_id']}>" . ts('flip') . "</a> | ";
815 $searchRows[$count]['actions'] .= CRM_Utils_System
::href(ts('merge'), 'civicrm/contact/merge', $mergeParams);
816 $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>";
819 $searchRows[$count]['actions'] = '<em>' . ts('Insufficient access rights - cannot merge') . '</em>';
825 'data' => $searchRows,
826 'recordsTotal' => $iTotal,
827 'recordsFiltered' => $iFilteredTotal,
829 if (!empty($_REQUEST['is_unit_test'])) {
832 CRM_Utils_JSON
::output($dupePairs);
836 * Get the searchable options from the request.
840 public static function getSearchOptionsFromRequest() {
841 $searchParams = array();
842 $searchData = CRM_Utils_Array
::value('search', $_REQUEST);
843 $searchData['value'] = CRM_Utils_Type
::escape($searchData['value'], 'String');
844 $selectorElements = array(
861 $columns = $_REQUEST['columns'];
863 foreach ($columns as $column) {
864 if (!empty($column['search']['value']) && in_array($column['data'], $selectorElements)) {
865 $searchParams[$column['data']] = CRM_Utils_Type
::escape($column['search']['value'], 'String');
867 elseif (!empty($searchData['value'])) {
868 $searchParams[$column['data']] = $searchData['value'];
871 return $searchParams;
875 * Is the query an OR query.
877 * If a generic search value is passed in - ie. $_REQUEST['search']['value'] = 'abc'
878 * then all fields are searched for this.
880 * It is unclear if there is any code that still passes this in or whether is is just legacy. It
881 * could cause a server-killing query on a large site so it probably is NOT in use if we haven't
886 public static function isOrQuery() {
887 $searchData = CRM_Utils_Array
::value('search', $_REQUEST);
888 return !empty($searchData['value']);
892 * Retrieve a PDF Page Format for the PDF Letter form.
894 public function pdfFormat() {
895 $formatId = CRM_Utils_Type
::escape($_REQUEST['formatId'], 'Integer');
897 $pdfFormat = CRM_Core_BAO_PdfFormat
::getById($formatId);
899 CRM_Utils_JSON
::output($pdfFormat);
903 * Retrieve Paper Size dimensions.
905 public static function paperSize() {
906 $paperSizeName = CRM_Utils_Type
::escape($_REQUEST['paperSizeName'], 'String');
908 $paperSize = CRM_Core_BAO_PaperSize
::getByName($paperSizeName);
910 CRM_Utils_JSON
::output($paperSize);
914 * Swap contacts in a dupe pair i.e main with duplicate contact.
916 * @param int $prevNextId
918 public static function flipDupePairs($prevNextId = NULL) {
920 // @todo figure out if this is always POST & specify that rather than inexact GET
921 $prevNextId = CRM_Utils_Request
::retrieve('pnid', 'Integer');
924 $onlySelected = FALSE;
925 if (is_array($prevNextId) && !CRM_Utils_Array
::crmIsEmptyArray($prevNextId)) {
926 $onlySelected = TRUE;
928 $prevNextId = CRM_Utils_Type
::escapeAll((array) $prevNextId, 'Positive');
929 CRM_Core_BAO_PrevNextCache
::flipPair($prevNextId, $onlySelected);
930 CRM_Utils_System
::civiExit();
934 * Used to store selected contacts across multiple pages in advanced search.
936 public static function selectUnselectContacts() {
937 $name = CRM_Utils_Array
::value('name', $_REQUEST);
938 $cacheKey = CRM_Utils_Array
::value('qfKey', $_REQUEST);
939 $state = CRM_Utils_Array
::value('state', $_REQUEST, 'checked');
940 $variableType = CRM_Utils_Array
::value('variableType', $_REQUEST, 'single');
942 $actionToPerform = CRM_Utils_Array
::value('action', $_REQUEST, 'select');
944 if ($variableType == 'multiple') {
945 // action post value only works with multiple type variable
947 //multiple names like mark_x_1-mark_x_2 where 1,2 are cids
948 $elements = explode('-', $name);
949 foreach ($elements as $key => $element) {
950 $elements[$key] = self
::_convertToId($element);
952 CRM_Utils_Type
::escapeAll($elements, 'Integer');
953 CRM_Core_BAO_PrevNextCache
::markSelection($cacheKey, $actionToPerform, $elements);
956 CRM_Core_BAO_PrevNextCache
::markSelection($cacheKey, $actionToPerform);
959 elseif ($variableType == 'single') {
960 $cId = self
::_convertToId($name);
961 CRM_Utils_Type
::escape($cId, 'Integer');
962 $action = ($state == 'checked') ?
'select' : 'unselect';
963 CRM_Core_BAO_PrevNextCache
::markSelection($cacheKey, $action, $cId);
965 $contactIds = CRM_Core_BAO_PrevNextCache
::getSelection($cacheKey);
966 $countSelectionCids = count($contactIds[$cacheKey]);
968 $arrRet = array('getCount' => $countSelectionCids);
969 CRM_Utils_JSON
::output($arrRet);
973 * @param string $name
977 public static function _convertToId($name) {
978 if (substr($name, 0, CRM_Core_Form
::CB_PREFIX_LEN
) == CRM_Core_Form
::CB_PREFIX
) {
979 $cId = substr($name, CRM_Core_Form
::CB_PREFIX_LEN
);
984 public static function getAddressDisplay() {
985 $contactId = CRM_Utils_Request
::retrieve('contact_id', 'Positive', CRM_Core_DAO
::$_nullObject);
987 $addressVal["error_message"] = "no contact id found";
990 $entityBlock = array(
991 'contact_id' => $contactId,
992 'entity_id' => $contactId,
994 $addressVal = CRM_Core_BAO_Address
::getValues($entityBlock);
997 CRM_Utils_JSON
::output($addressVal);
1001 * Mark dupe pairs as selected from un-selected state or vice-versa, in dupe cache table.
1003 public static function toggleDedupeSelect() {
1004 $rgid = CRM_Utils_Type
::escape($_REQUEST['rgid'], 'Integer');
1005 $gid = CRM_Utils_Type
::escape($_REQUEST['gid'], 'Integer');
1006 $pnid = $_REQUEST['pnid'];
1007 $isSelected = CRM_Utils_Type
::escape($_REQUEST['is_selected'], 'Boolean');
1009 $cacheKeyString = CRM_Dedupe_Merger
::getMergeCacheKeyString($rgid, $gid);
1012 1 => array($isSelected, 'Boolean'),
1013 3 => array("$cacheKeyString%", 'String'), // using % to address rows with conflicts as well
1016 //check pnid is_array or integer
1017 $whereClause = NULL;
1018 if (is_array($pnid) && !CRM_Utils_Array
::crmIsEmptyArray($pnid)) {
1019 CRM_Utils_Type
::escapeAll($pnid, 'Positive');
1020 $pnid = implode(', ', $pnid);
1021 $whereClause = " id IN ( {$pnid} ) ";
1024 $pnid = CRM_Utils_Type
::escape($pnid, 'Integer');
1025 $whereClause = " id = %2";
1026 $params[2] = array($pnid, 'Integer');
1029 $sql = "UPDATE civicrm_prevnext_cache SET is_selected = %1 WHERE {$whereClause} AND cacheKey LIKE %3";
1030 CRM_Core_DAO
::executeQuery($sql, $params);
1032 CRM_Utils_System
::civiExit();
1036 * Retrieve contact relationships.
1038 public static function getContactRelationships() {
1039 $contactID = CRM_Utils_Type
::escape($_GET['cid'], 'Integer');
1040 $context = CRM_Utils_Type
::escape($_GET['context'], 'String');
1041 $relationship_type_id = CRM_Utils_Type
::escape(CRM_Utils_Array
::value('relationship_type_id', $_GET), 'Integer', FALSE);
1043 if (!CRM_Contact_BAO_Contact_Permission
::allow($contactID)) {
1044 return CRM_Utils_System
::permissionDenied();
1047 $params = CRM_Core_Page_AJAX
::defaultSortAndPagerParams();
1049 $params['contact_id'] = $contactID;
1050 $params['context'] = $context;
1051 if ($relationship_type_id) {
1052 $params['relationship_type_id'] = $relationship_type_id;
1055 // get the contact relationships
1056 $relationships = CRM_Contact_BAO_Relationship
::getContactRelationshipSelector($params);
1058 CRM_Utils_JSON
::output($relationships);