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);
73 if (!empty($action) &&
74 !in_array($action, array('get', 'lookup'))
76 CRM_Utils_System
::civiExit('error');
80 $list = array_keys(CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
81 'contact_reference_options'
84 $return = array_unique(array_merge(array('sort_name'), $list));
86 $limit = Civi
::settings()->get('search_autocomplete_count');
88 $params = array('offset' => 0, 'rowCount' => $limit, 'version' => 3);
89 foreach ($return as $fld) {
90 $params["return.{$fld}"] = 1;
93 if (!empty($action)) {
109 foreach ($_GET as $param => $val) {
111 in_array($param, $excludeGet) ||
112 strpos($param, 'return.') !== FALSE ||
113 strpos($param, 'api.') !== FALSE
117 $params[$param] = $val;
122 $params['sort_name'] = $name;
125 $params['sort'] = 'sort_name';
127 // tell api to skip permission chk. dgg
128 $params['check_permissions'] = 0;
130 // add filter variable to params
131 if (!empty($filterParams)) {
132 $params = array_merge($params, $filterParams);
135 $contact = civicrm_api('Contact', 'Get', $params);
137 if (!empty($contact['is_error'])) {
138 CRM_Utils_System
::civiExit('error');
141 $contactList = array();
142 foreach ($contact['values'] as $value) {
144 foreach ($return as $fld) {
145 if (!empty($value[$fld])) {
146 $view[] = $value[$fld];
149 $contactList[] = array('id' => $value['id'], 'text' => implode(' :: ', $view));
152 CRM_Utils_JSON
::output($contactList);
156 * Fetch PCP ID by PCP Supporter sort_name, also displays PCP title and associated Contribution Page title
158 public static function getPCPList() {
159 $name = CRM_Utils_Array
::value('term', $_GET);
160 $name = CRM_Utils_Type
::escape($name, 'String');
161 $limit = $max = Civi
::settings()->get('search_autocomplete_count');
163 $where = ' AND pcp.page_id = cp.id AND pcp.contact_id = cc.id';
165 $config = CRM_Core_Config
::singleton();
166 if ($config->includeWildCardInName
) {
167 $strSearch = "%$name%";
170 $strSearch = "$name%";
172 $includeEmailFrom = $includeNickName = '';
173 if ($config->includeNickNameInName
) {
174 $includeNickName = " OR nick_name LIKE '$strSearch'";
176 if ($config->includeEmailInName
) {
177 $includeEmailFrom = "LEFT JOIN civicrm_email eml ON ( cc.id = eml.contact_id AND eml.is_primary = 1 )";
178 $whereClause = " WHERE ( email LIKE '$strSearch' OR sort_name LIKE '$strSearch' $includeNickName ) {$where} ";
181 $whereClause = " WHERE ( sort_name LIKE '$strSearch' $includeNickName ) {$where} ";
184 $offset = $count = 0;
185 if (!empty($_GET['page_num'])) {
186 $page = (int) $_GET['page_num'];
187 $offset = $limit * ($page - 1);
191 $select = 'cc.sort_name, pcp.title, cp.title';
195 SELECT pcp.id as id, CONCAT_WS( ' :: ', {$select} ) as data, sort_name
196 FROM civicrm_pcp pcp, civicrm_contribution_page cp, civicrm_contact cc
198 {$whereClause} AND pcp.page_type = 'contribute'
200 SELECT pcp.id as id, CONCAT_WS( ' :: ', {$select} ) as data, sort_name
201 FROM civicrm_pcp pcp, civicrm_event cp, civicrm_contact cc
203 {$whereClause} AND pcp.page_type = 'event'
206 LIMIT $offset, $limit
209 $dao = CRM_Core_DAO
::executeQuery($query);
210 $output = array('results' => array(), 'more' => FALSE);
211 while ($dao->fetch()) {
212 if (++
$count > $max) {
213 $output['more'] = TRUE;
216 $output['results'][] = array('id' => $dao->id
, 'text' => $dao->data
);
219 CRM_Utils_JSON
::output($output);
223 * @throws \CiviCRM_API3_Exception
225 public static function relationship() {
226 $relType = CRM_Utils_Request
::retrieve('rel_type', 'String', CRM_Core_DAO
::$_nullObject, TRUE);
227 $relContactID = CRM_Utils_Request
::retrieve('rel_contact', 'Positive', CRM_Core_DAO
::$_nullObject, TRUE);
228 $originalCid = CRM_Utils_Request
::retrieve('cid', 'Positive', CRM_Core_DAO
::$_nullObject);
229 $relationshipID = CRM_Utils_Request
::retrieve('rel_id', 'Positive', CRM_Core_DAO
::$_nullObject);
230 $caseID = CRM_Utils_Request
::retrieve('case_id', 'Positive', CRM_Core_DAO
::$_nullObject, TRUE);
232 if (!CRM_Case_BAO_Case
::accessCase($caseID)) {
233 CRM_Utils_System
::permissionDenied();
236 $ret = array('is_error' => 0);
238 list($relTypeId, $b, $a) = explode('_', $relType);
240 if ($relationshipID && $originalCid) {
241 CRM_Case_BAO_Case
::endCaseRole($caseID, $a, $originalCid, $relTypeId);
244 $clientList = CRM_Case_BAO_Case
::getCaseClients($caseID);
246 // Loop through multiple case clients
247 foreach ($clientList as $i => $sourceContactID) {
248 $result = civicrm_api3('relationship', 'create', array(
249 'case_id' => $caseID,
250 'relationship_type_id' => $relTypeId,
251 "contact_id_$a" => $relContactID,
252 "contact_id_$b" => $sourceContactID,
253 'start_date' => 'now',
255 // Save activity only for the primary (first) client
256 if ($i == 0 && empty($result['is_error'])) {
257 CRM_Case_BAO_Case
::createCaseRoleActivity($caseID, $result['id'], $relContactID);
261 CRM_Utils_JSON
::output($ret);
265 * Fetch the custom field help.
267 public static function customField() {
268 $fieldId = CRM_Utils_Type
::escape($_REQUEST['id'], 'Integer');
269 $params = array('id' => $fieldId);
270 $returnProperties = array('help_pre', 'help_post');
273 CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_CustomField', $params, $values, $returnProperties);
274 CRM_Utils_JSON
::output($values);
277 public static function groupTree() {
278 CRM_Utils_System
::setHttpHeader('Content-Type', 'application/json');
279 $gids = CRM_Utils_Type
::escape($_GET['gids'], 'String');
280 echo CRM_Contact_BAO_GroupNestingCache
::json($gids);
281 CRM_Utils_System
::civiExit();
285 * Delete custom value.
287 public static function deleteCustomValue() {
288 CRM_Utils_System
::setHttpHeader('Content-Type', 'text/plain');
289 $customValueID = CRM_Utils_Type
::escape($_REQUEST['valueID'], 'Positive');
290 $customGroupID = CRM_Utils_Type
::escape($_REQUEST['groupID'], 'Positive');
291 $contactId = CRM_Utils_Request
::retrieve('contactId', 'Positive', CRM_Core_DAO
::$_nullObject);
292 CRM_Core_BAO_CustomValue
::deleteCustomValue($customValueID, $customGroupID);
294 echo CRM_Contact_BAO_Contact
::getCountComponent('custom_' . $customGroupID, $contactId);
297 CRM_Contact_BAO_GroupContactCache
::opportunisticCacheFlush();
298 CRM_Utils_System
::civiExit();
302 * check the CMS username.
304 static public function checkUserName() {
305 $signer = new CRM_Utils_Signer(CRM_Core_Key
::privateKey(), array('for', 'ts'));
306 $sig = CRM_Utils_Request
::retrieve('sig', 'String', CRM_Core_DAO
::$_nullObject);
307 $for = CRM_Utils_Request
::retrieve('for', 'String', CRM_Core_DAO
::$_nullObject);
309 CRM_Utils_Time
::getTimeRaw() > $_REQUEST['ts'] + self
::CHECK_USERNAME_TTL
310 ||
$for != 'civicrm/ajax/cmsuser'
311 ||
!$signer->validate($sig, $_REQUEST)
313 $user = array('name' => 'error');
314 CRM_Utils_JSON
::output($user);
317 $config = CRM_Core_Config
::singleton();
318 $username = trim(CRM_Utils_Array
::value('cms_name', $_REQUEST));
320 $params = array('name' => $username);
323 $config->userSystem
->checkUserNameEmailExists($params, $errors);
325 if (isset($errors['cms_name']) ||
isset($errors['name'])) {
326 //user name is not available
327 $user = array('name' => 'no');
328 CRM_Utils_JSON
::output($user);
331 //user name is available
332 $user = array('name' => 'yes');
333 CRM_Utils_JSON
::output($user);
336 // Not reachable: JSON::output() above exits.
337 CRM_Utils_System
::civiExit();
341 * Function to get email address of a contact.
343 public static function getContactEmail() {
344 if (!empty($_REQUEST['contact_id'])) {
345 $contactID = CRM_Utils_Type
::escape($_REQUEST['contact_id'], 'Positive');
346 if (!CRM_Contact_BAO_Contact_Permission
::allow($contactID, CRM_Core_Permission
::EDIT
)) {
351 ) = CRM_Contact_BAO_Contact_Location
::getEmailDetails($contactID);
353 CRM_Utils_System
::setHttpHeader('Content-Type', 'text/plain');
359 $noemail = CRM_Utils_Array
::value('noemail', $_GET);
361 $name = CRM_Utils_Array
::value('name', $_GET);
363 $name = CRM_Utils_Type
::escape($name, 'String');
365 $queryString = " cc.sort_name LIKE '%$name%'";
368 $queryString = " ( cc.sort_name LIKE '%$name%' OR ce.email LIKE '%$name%' ) ";
372 $cid = CRM_Utils_Array
::value('cid', $_GET);
374 //check cid for integer
375 $contIDS = explode(',', $cid);
376 foreach ($contIDS as $contID) {
377 CRM_Utils_Type
::escape($contID, 'Integer');
379 $queryString = " cc.id IN ( $cid )";
384 $offset = CRM_Utils_Array
::value('offset', $_GET, 0);
385 $rowCount = Civi
::settings()->get('search_autocomplete_count');
387 $offset = CRM_Utils_Type
::escape($offset, 'Int');
389 // add acl clause here
390 list($aclFrom, $aclWhere) = CRM_Contact_BAO_Contact_Permission
::cacheClause('cc');
392 $aclWhere = " AND $aclWhere";
396 SELECT sort_name name, cc.id
397 FROM civicrm_contact cc
399 WHERE cc.is_deceased = 0 AND {$queryString}
401 LIMIT {$offset}, {$rowCount}
404 // send query to hook to be modified if needed
405 CRM_Utils_Hook
::contactListQuery($query,
407 CRM_Utils_Request
::retrieve('context', 'String', CRM_Core_DAO
::$_nullObject),
408 CRM_Utils_Request
::retrieve('cid', 'Positive', CRM_Core_DAO
::$_nullObject)
411 $dao = CRM_Core_DAO
::executeQuery($query);
412 while ($dao->fetch()) {
415 'text' => $dao->name
,
421 SELECT sort_name name, ce.email, cc.id
422 FROM civicrm_email ce INNER JOIN civicrm_contact cc ON cc.id = ce.contact_id
424 WHERE ce.on_hold = 0 AND cc.is_deceased = 0 AND cc.do_not_email = 0 AND {$queryString}
426 LIMIT {$offset}, {$rowCount}
429 // send query to hook to be modified if needed
430 CRM_Utils_Hook
::contactListQuery($query,
432 CRM_Utils_Request
::retrieve('context', 'String', CRM_Core_DAO
::$_nullObject),
433 CRM_Utils_Request
::retrieve('cid', 'Positive', CRM_Core_DAO
::$_nullObject)
436 $dao = CRM_Core_DAO
::executeQuery($query);
438 while ($dao->fetch()) {
441 'text' => '"' . $dao->name
. '" <' . $dao->email
. '>',
442 'id' => (CRM_Utils_Array
::value('id', $_GET)) ?
"{$dao->id}::{$dao->email}" : '"' . $dao->name
. '" <' . $dao->email
. '>',
447 CRM_Utils_JSON
::output($result);
451 CRM_Utils_System
::civiExit();
454 public static function getContactPhone() {
457 //check for mobile type
458 $phoneTypes = CRM_Core_OptionGroup
::values('phone_type', TRUE, FALSE, FALSE, NULL, 'name');
459 $mobileType = CRM_Utils_Array
::value('Mobile', $phoneTypes);
461 $name = CRM_Utils_Array
::value('name', $_GET);
463 $name = CRM_Utils_Type
::escape($name, 'String');
464 $queryString = " ( cc.sort_name LIKE '%$name%' OR cp.phone LIKE '%$name%' ) ";
467 $cid = CRM_Utils_Array
::value('cid', $_GET);
469 //check cid for integer
470 $contIDS = explode(',', $cid);
471 foreach ($contIDS as $contID) {
472 CRM_Utils_Type
::escape($contID, 'Integer');
474 $queryString = " cc.id IN ( $cid )";
479 $offset = CRM_Utils_Array
::value('offset', $_GET, 0);
480 $rowCount = CRM_Utils_Array
::value('rowcount', $_GET, 20);
482 $offset = CRM_Utils_Type
::escape($offset, 'Int');
483 $rowCount = CRM_Utils_Type
::escape($rowCount, 'Int');
485 // add acl clause here
486 list($aclFrom, $aclWhere) = CRM_Contact_BAO_Contact_Permission
::cacheClause('cc');
488 $aclWhere = " AND $aclWhere";
492 SELECT sort_name name, cp.phone, cc.id
493 FROM civicrm_phone cp INNER JOIN civicrm_contact cc ON cc.id = cp.contact_id
495 WHERE cc.is_deceased = 0 AND cc.do_not_sms = 0 AND cp.phone_type_id = {$mobileType} AND {$queryString}
497 LIMIT {$offset}, {$rowCount}
500 // send query to hook to be modified if needed
501 CRM_Utils_Hook
::contactListQuery($query,
503 CRM_Utils_Request
::retrieve('context', 'String', CRM_Core_DAO
::$_nullObject),
504 CRM_Utils_Request
::retrieve('cid', 'Positive', CRM_Core_DAO
::$_nullObject)
507 $dao = CRM_Core_DAO
::executeQuery($query);
509 while ($dao->fetch()) {
511 'text' => '"' . $dao->name
. '" (' . $dao->phone
. ')',
512 'id' => (CRM_Utils_Array
::value('id', $_GET)) ?
"{$dao->id}::{$dao->phone}" : '"' . $dao->name
. '" <' . $dao->phone
. '>',
518 CRM_Utils_JSON
::output($result);
520 CRM_Utils_System
::civiExit();
524 public static function buildSubTypes() {
525 $parent = CRM_Utils_Request
::retrieve('parentId', 'Positive', CRM_Core_DAO
::$_nullObject);
529 $contactType = 'Individual';
533 $contactType = 'Household';
537 $contactType = 'Organization';
541 $subTypes = CRM_Contact_BAO_ContactType
::subTypePairs($contactType, FALSE, NULL);
543 CRM_Utils_JSON
::output($subTypes);
546 public static function buildDedupeRules() {
547 $parent = CRM_Utils_Request
::retrieve('parentId', 'Positive', CRM_Core_DAO
::$_nullObject);
551 $contactType = 'Individual';
555 $contactType = 'Household';
559 $contactType = 'Organization';
563 $dedupeRules = CRM_Dedupe_BAO_RuleGroup
::getByType($contactType);
565 CRM_Utils_JSON
::output($dedupeRules);
569 * Function used for CiviCRM dashboard operations.
571 public static function dashboard() {
572 $operation = CRM_Utils_Type
::escape($_REQUEST['op'], 'String');
574 switch ($operation) {
575 case 'get_widgets_by_column':
576 // This would normally be coming from either the database (this user's settings) or a default/initial dashboard configuration.
577 // get contact id of logged in user
579 $dashlets = CRM_Core_BAO_Dashboard
::getContactDashlets();
583 $dashletID = CRM_Utils_Type
::escape($_GET['id'], 'Positive');
585 $dashlets = CRM_Core_BAO_Dashboard
::getDashletInfo($dashletID);
589 CRM_Core_BAO_Dashboard
::saveDashletChanges($_REQUEST['columns']);
590 CRM_Utils_System
::civiExit();
591 case 'delete_dashlet':
592 $dashletID = CRM_Utils_Type
::escape($_REQUEST['dashlet_id'], 'Positive');
593 CRM_Core_BAO_Dashboard
::deleteDashlet($dashletID);
594 CRM_Utils_System
::civiExit();
597 CRM_Utils_JSON
::output($dashlets);
601 * Retrieve signature based on email id.
603 public static function getSignature() {
604 $emailID = CRM_Utils_Type
::escape($_REQUEST['emailID'], 'Positive');
605 $query = "SELECT signature_text, signature_html FROM civicrm_email WHERE id = {$emailID}";
606 $dao = CRM_Core_DAO
::executeQuery($query);
608 $signatures = array();
609 while ($dao->fetch()) {
611 'signature_text' => $dao->signature_text
,
612 'signature_html' => $dao->signature_html
,
616 CRM_Utils_JSON
::output($signatures);
622 public static function processDupes() {
623 $oper = CRM_Utils_Type
::escape($_REQUEST['op'], 'String');
624 $cid = CRM_Utils_Type
::escape($_REQUEST['cid'], 'Positive');
625 $oid = CRM_Utils_Type
::escape($_REQUEST['oid'], 'Positive');
627 if (!$oper ||
!$cid ||
!$oid) {
631 $exception = new CRM_Dedupe_DAO_Exception();
632 $exception->contact_id1
= $cid;
633 $exception->contact_id2
= $oid;
634 //make sure contact2 > contact1.
636 $exception->contact_id1
= $oid;
637 $exception->contact_id2
= $cid;
639 $exception->find(TRUE);
641 if ($oper == 'dupe-nondupe') {
642 $status = $exception->save();
644 if ($oper == 'nondupe-dupe') {
645 $status = $exception->delete();
648 CRM_Utils_JSON
::output(array('status' => ($status) ?
$oper : $status));
652 * Retrieve list of duplicate pairs from cache table.
654 public static function getDedupes() {
655 $offset = isset($_REQUEST['start']) ? CRM_Utils_Type
::escape($_REQUEST['start'], 'Integer') : 0;
656 $rowCount = isset($_REQUEST['length']) ? CRM_Utils_Type
::escape($_REQUEST['length'], 'Integer') : 25;
658 $gid = isset($_REQUEST['gid']) ? CRM_Utils_Type
::escape($_REQUEST['gid'], 'Integer') : 0;
659 $rgid = isset($_REQUEST['rgid']) ? CRM_Utils_Type
::escape($_REQUEST['rgid'], 'Integer') : 0;
660 $selected = isset($_REQUEST['selected']) ? CRM_Utils_Type
::escape($_REQUEST['selected'], 'Integer') : 0;
666 $contactType = CRM_Core_DAO
::getFieldValue('CRM_Dedupe_DAO_RuleGroup', $rgid, 'contact_type');
669 $cacheKeyString = CRM_Dedupe_Merger
::getMergeCacheKeyString($rgid, $gid);
670 $searchRows = array();
671 $selectorElements = array('is_selected', 'is_selected_input', 'src_image', 'src', 'src_email', 'src_street', 'src_postcode', 'dst_image', 'dst', 'dst_email', 'dst_street', 'dst_postcode', 'conflicts', 'weight', 'actions');
673 foreach ($_REQUEST['columns'] as $columnInfo) {
674 if (!empty($columnInfo['search']['value'])) {
675 $
{$columnInfo['data']} = CRM_Utils_Type
::escape($columnInfo['search']['value'], 'String');
680 $searchData = CRM_Utils_Array
::value('search', $_REQUEST);
681 $searchData['value'] = CRM_Utils_Type
::escape($searchData['value'], 'String');
683 if ($src ||
!empty($searchData['value'])) {
684 $src = $src ?
$src : $searchData['value'];
685 $where[] = " cc1.display_name LIKE '%{$src}%'";
687 if ($dst ||
!empty($searchData['value'])) {
688 $dst = $dst ?
$dst : $searchData['value'];
689 $where[] = " cc2.display_name LIKE '%{$dst}%'";
691 if ($src_email ||
!empty($searchData['value'])) {
692 $src_email = $src_email ?
$src_email : $searchData['value'];
693 $where[] = " (ce1.is_primary = 1 AND ce1.email LIKE '%{$src_email}%')";
695 if ($dst_email ||
!empty($searchData['value'])) {
696 $dst_email = $dst_email ?
$dst_email : $searchData['value'];
697 $where[] = " (ce2.is_primary = 1 AND ce2.email LIKE '%{$dst_email}%')";
699 if ($src_postcode ||
!empty($searchData['value'])) {
700 $src_postcode = $src_postcode ?
$src_postcode : $searchData['value'];
701 $where[] = " (ca1.is_primary = 1 AND ca1.postal_code LIKE '%{$src_postcode}%')";
703 if ($dst_postcode ||
!empty($searchData['value'])) {
704 $dst_postcode = $dst_postcode ?
$dst_postcode : $searchData['value'];
705 $where[] = " (ca2.is_primary = 1 AND ca2.postal_code LIKE '%{$dst_postcode}%')";
707 if ($src_street ||
!empty($searchData['value'])) {
708 $src_street = $src_street ?
$src_street : $searchData['value'];
709 $where[] = " (ca1.is_primary = 1 AND ca1.street_address LIKE '%{$src_street}%')";
711 if ($dst_street ||
!empty($searchData['value'])) {
712 $dst_street = $dst_street ?
$dst_street : $searchData['value'];
713 $where[] = " (ca2.is_primary = 1 AND ca2.street_address LIKE '%{$dst_street}%')";
715 if (!empty($searchData['value'])) {
716 $whereClause = ' ( ' . implode(' OR ', $where) . ' ) ';
719 if (!empty($where)) {
720 $whereClause = implode(' AND ', $where);
723 $whereClause .= $whereClause ?
' AND de.id IS NULL' : ' de.id IS NULL';
726 $whereClause .= ' AND pn.is_selected = 1';
728 $join .= CRM_Dedupe_Merger
::getJoinOnDedupeTable();
731 'cc1.contact_type' => 'src_contact_type',
732 'cc1.display_name' => 'src_display_name',
733 'cc1.contact_sub_type' => 'src_contact_sub_type',
734 'cc2.contact_type' => 'dst_contact_type',
735 'cc2.display_name' => 'dst_display_name',
736 'cc2.contact_sub_type' => 'dst_contact_sub_type',
737 'ce1.email' => 'src_email',
738 'ce2.email' => 'dst_email',
739 'ca1.postal_code' => 'src_postcode',
740 'ca2.postal_code' => 'dst_postcode',
741 'ca1.street_address' => 'src_street',
742 'ca2.street_address' => 'dst_street',
746 $join .= " INNER JOIN civicrm_contact cc1 ON cc1.id = pn.entity_id1";
747 $join .= " INNER JOIN civicrm_contact cc2 ON cc2.id = pn.entity_id2";
748 $join .= " LEFT JOIN civicrm_email ce1 ON (ce1.contact_id = pn.entity_id1 AND ce1.is_primary = 1 )";
749 $join .= " LEFT JOIN civicrm_email ce2 ON (ce2.contact_id = pn.entity_id2 AND ce2.is_primary = 1 )";
750 $join .= " LEFT JOIN civicrm_address ca1 ON (ca1.contact_id = pn.entity_id1 AND ca1.is_primary = 1 )";
751 $join .= " LEFT JOIN civicrm_address ca2 ON (ca2.contact_id = pn.entity_id2 AND ca2.is_primary = 1 )";
753 $iTotal = CRM_Core_BAO_PrevNextCache
::getCount($cacheKeyString, $join, $whereClause);
754 foreach ($_REQUEST['order'] as $orderInfo) {
755 if (!empty($orderInfo['column'])) {
756 $orderColumnNumber = $orderInfo['column'];
757 $dir = $orderInfo['dir'];
760 $columnDetails = CRM_Utils_Array
::value($orderColumnNumber, $_REQUEST['columns']);
761 if (!empty($columnDetails)) {
762 switch ($columnDetails['data']) {
764 $orderByClause = " ORDER BY cc1.display_name {$dir}";
768 $orderByClause = " ORDER BY ce1.email {$dir}";
772 $orderByClause = " ORDER BY ca1.street_address {$dir}";
776 $orderByClause = " ORDER BY ca1.postal_code {$dir}";
780 $orderByClause = " ORDER BY cc2.display_name {$dir}";
784 $orderByClause = " ORDER BY ce2.email {$dir}";
788 $orderByClause = " ORDER BY ca2.street_address {$dir}";
792 $orderByClause = " ORDER BY ca2.postal_code {$dir}";
800 $dupePairs = CRM_Core_BAO_PrevNextCache
::retrieve($cacheKeyString, $join, $whereClause, $offset, $rowCount, $select, $orderByClause);
801 $iFilteredTotal = CRM_Core_DAO
::singleValueQuery("SELECT FOUND_ROWS()");
804 foreach ($dupePairs as $key => $pairInfo) {
805 $pair =& $pairInfo['data'];
806 $srcContactSubType = CRM_Utils_Array
::value('src_contact_sub_type', $pairInfo);
807 $dstContactSubType = CRM_Utils_Array
::value('dst_contact_sub_type', $pairInfo);
808 $srcTypeImage = CRM_Contact_BAO_Contact_Utils
::getImage($srcContactSubType ?
809 $srcContactSubType : $pairInfo['src_contact_type'],
811 $pairInfo['entity_id1']
813 $dstTypeImage = CRM_Contact_BAO_Contact_Utils
::getImage($dstContactSubType ?
814 $dstContactSubType : $pairInfo['dst_contact_type'],
816 $pairInfo['entity_id2']
819 $searchRows[$count]['is_selected'] = $pairInfo['is_selected'];
820 $searchRows[$count]['is_selected_input'] = "<input type='checkbox' class='crm-dedupe-select' name='pnid_{$pairInfo['prevnext_id']}' value='{$pairInfo['is_selected']}' onclick='toggleDedupeSelect(this)'>";
821 $searchRows[$count]['src_image'] = $srcTypeImage;
822 $searchRows[$count]['src'] = CRM_Utils_System
::href($pair['srcName'], 'civicrm/contact/view', "reset=1&cid={$pairInfo['entity_id1']}");
823 $searchRows[$count]['src_email'] = CRM_Utils_Array
::value('src_email', $pairInfo);
824 $searchRows[$count]['src_street'] = CRM_Utils_Array
::value('src_street', $pairInfo);
825 $searchRows[$count]['src_postcode'] = CRM_Utils_Array
::value('src_postcode', $pairInfo);
826 $searchRows[$count]['dst_image'] = $dstTypeImage;
827 $searchRows[$count]['dst'] = CRM_Utils_System
::href($pair['dstName'], 'civicrm/contact/view', "reset=1&cid={$pairInfo['entity_id2']}");
828 $searchRows[$count]['dst_email'] = CRM_Utils_Array
::value('dst_email', $pairInfo);
829 $searchRows[$count]['dst_street'] = CRM_Utils_Array
::value('dst_street', $pairInfo);
830 $searchRows[$count]['dst_postcode'] = CRM_Utils_Array
::value('dst_postcode', $pairInfo);
831 $searchRows[$count]['conflicts'] = str_replace("',", "',<br/>", CRM_Utils_Array
::value('conflicts', $pair));
832 $searchRows[$count]['weight'] = CRM_Utils_Array
::value('weight', $pair);
834 if (!empty($pairInfo['data']['canMerge'])) {
835 $mergeParams = "reset=1&cid={$pairInfo['entity_id1']}&oid={$pairInfo['entity_id2']}&action=update&rgid={$rgid}";
837 $mergeParams .= "&gid={$gid}";
840 $searchRows[$count]['actions'] = "<a class='crm-dedupe-flip' href='#' data-pnid={$pairInfo['prevnext_id']}>" . ts('flip') . "</a> | ";
841 $searchRows[$count]['actions'] .= CRM_Utils_System
::href(ts('merge'), 'civicrm/contact/merge', $mergeParams);
842 $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>";
845 $searchRows[$count]['actions'] = '<em>' . ts('Insufficient access rights - cannot merge') . '</em>';
851 'data' => $searchRows,
852 'recordsTotal' => $iTotal,
853 'recordsFiltered' => $iFilteredTotal,
855 CRM_Utils_JSON
::output($dupePairs);
859 * Retrieve a PDF Page Format for the PDF Letter form.
861 public function pdfFormat() {
862 $formatId = CRM_Utils_Type
::escape($_REQUEST['formatId'], 'Integer');
864 $pdfFormat = CRM_Core_BAO_PdfFormat
::getById($formatId);
866 CRM_Utils_JSON
::output($pdfFormat);
870 * Retrieve Paper Size dimensions.
872 public static function paperSize() {
873 $paperSizeName = CRM_Utils_Type
::escape($_REQUEST['paperSizeName'], 'String');
875 $paperSize = CRM_Core_BAO_PaperSize
::getByName($paperSizeName);
877 CRM_Utils_JSON
::output($paperSize);
881 * Swap contacts in a dupe pair i.e main with duplicate contact.
883 * @param int $prevNextId
885 public static function flipDupePairs($prevNextId = NULL) {
887 $prevNextId = $_REQUEST['pnid'];
890 UPDATE civicrm_prevnext_cache cpc
891 INNER JOIN civicrm_prevnext_cache old on cpc.id = old.id
892 SET cpc.entity_id1 = cpc.entity_id2, cpc.entity_id2 = old.entity_id1 ";
893 if (is_array($prevNextId) && !CRM_Utils_Array
::crmIsEmptyArray($prevNextId)) {
894 CRM_Utils_Type
::escapeAll($prevNextId, 'Positive');
895 $prevNextId = implode(', ', $prevNextId);
896 $query .= "WHERE cpc.id IN ({$prevNextId}) AND cpc.is_selected = 1";
899 $prevNextId = CRM_Utils_Type
::escape($prevNextId, 'Positive');
900 $query .= "WHERE cpc.id = $prevNextId";
902 CRM_Core_DAO
::executeQuery($query);
903 CRM_Utils_JSON
::output();
907 * Used to store selected contacts across multiple pages in advanced search.
909 public static function selectUnselectContacts() {
910 $name = CRM_Utils_Array
::value('name', $_REQUEST);
911 $cacheKey = CRM_Utils_Array
::value('qfKey', $_REQUEST);
912 $state = CRM_Utils_Array
::value('state', $_REQUEST, 'checked');
913 $variableType = CRM_Utils_Array
::value('variableType', $_REQUEST, 'single');
915 $actionToPerform = CRM_Utils_Array
::value('action', $_REQUEST, 'select');
917 if ($variableType == 'multiple') {
918 // action post value only works with multiple type variable
920 //multiple names like mark_x_1-mark_x_2 where 1,2 are cids
921 $elements = explode('-', $name);
922 foreach ($elements as $key => $element) {
923 $elements[$key] = self
::_convertToId($element);
925 CRM_Utils_Type
::escapeAll($elements, 'Integer');
926 CRM_Core_BAO_PrevNextCache
::markSelection($cacheKey, $actionToPerform, $elements);
929 CRM_Core_BAO_PrevNextCache
::markSelection($cacheKey, $actionToPerform);
932 elseif ($variableType == 'single') {
933 $cId = self
::_convertToId($name);
934 CRM_Utils_Type
::escape($cId, 'Integer');
935 $action = ($state == 'checked') ?
'select' : 'unselect';
936 CRM_Core_BAO_PrevNextCache
::markSelection($cacheKey, $action, $cId);
938 $contactIds = CRM_Core_BAO_PrevNextCache
::getSelection($cacheKey);
939 $countSelectionCids = count($contactIds[$cacheKey]);
941 $arrRet = array('getCount' => $countSelectionCids);
942 CRM_Utils_JSON
::output($arrRet);
946 * @param string $name
950 public static function _convertToId($name) {
951 if (substr($name, 0, CRM_Core_Form
::CB_PREFIX_LEN
) == CRM_Core_Form
::CB_PREFIX
) {
952 $cId = substr($name, CRM_Core_Form
::CB_PREFIX_LEN
);
957 public static function getAddressDisplay() {
958 $contactId = CRM_Utils_Request
::retrieve('contact_id', 'Positive', CRM_Core_DAO
::$_nullObject);
960 $addressVal["error_message"] = "no contact id found";
963 $entityBlock = array(
964 'contact_id' => $contactId,
965 'entity_id' => $contactId,
967 $addressVal = CRM_Core_BAO_Address
::getValues($entityBlock);
970 CRM_Utils_JSON
::output($addressVal);
974 * Mark dupe pairs as selected from un-selected state or vice-versa, in dupe cache table.
976 public static function toggleDedupeSelect() {
977 $rgid = CRM_Utils_Type
::escape($_REQUEST['rgid'], 'Integer');
978 $gid = CRM_Utils_Type
::escape($_REQUEST['gid'], 'Integer');
979 $pnid = $_REQUEST['pnid'];
980 $isSelected = CRM_Utils_Type
::escape($_REQUEST['is_selected'], 'Boolean');
982 $cacheKeyString = CRM_Dedupe_Merger
::getMergeCacheKeyString($rgid, $gid);
985 1 => array($isSelected, 'Boolean'),
986 3 => array("$cacheKeyString%", 'String'), // using % to address rows with conflicts as well
989 //check pnid is_array or integer
991 if (is_array($pnid) && !CRM_Utils_Array
::crmIsEmptyArray($pnid)) {
992 CRM_Utils_Type
::escapeAll($pnid, 'Positive');
993 $pnid = implode(', ', $pnid);
994 $whereClause = " id IN ( {$pnid} ) ";
997 $pnid = CRM_Utils_Type
::escape($pnid, 'Integer');
998 $whereClause = " id = %2";
999 $params[2] = array($pnid, 'Integer');
1002 $sql = "UPDATE civicrm_prevnext_cache SET is_selected = %1 WHERE {$whereClause} AND cacheKey LIKE %3";
1003 CRM_Core_DAO
::executeQuery($sql, $params);
1005 CRM_Utils_System
::civiExit();
1009 * Retrieve contact relationships.
1011 public static function getContactRelationships() {
1012 $contactID = CRM_Utils_Type
::escape($_GET['cid'], 'Integer');
1013 $context = CRM_Utils_Type
::escape($_GET['context'], 'String');
1014 $relationship_type_id = CRM_Utils_Type
::escape(CRM_Utils_Array
::value('relationship_type_id', $_GET), 'Integer', FALSE);
1016 if (!CRM_Contact_BAO_Contact_Permission
::allow($contactID)) {
1017 return CRM_Utils_System
::permissionDenied();
1020 $params = CRM_Core_Page_AJAX
::defaultSortAndPagerParams();
1022 $params['contact_id'] = $contactID;
1023 $params['context'] = $context;
1024 if ($relationship_type_id) {
1025 $params['relationship_type_id'] = $relationship_type_id;
1028 // get the contact relationships
1029 $relationships = CRM_Contact_BAO_Relationship
::getContactRelationshipSelector($params);
1031 CRM_Utils_JSON
::output($relationships);