3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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 = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'search_autocomplete_count', NULL, 10);
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 = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'search_autocomplete_count', NULL, 10);
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);
222 public static function relationship() {
223 $relType = CRM_Utils_Request
::retrieve('rel_type', 'Positive', CRM_Core_DAO
::$_nullObject, TRUE);
224 $relContactID = CRM_Utils_Request
::retrieve('rel_contact', 'Positive', CRM_Core_DAO
::$_nullObject, TRUE);
225 $relationshipID = CRM_Utils_Array
::value('rel_id', $_REQUEST); // this used only to determine add or update mode
226 $caseID = CRM_Utils_Request
::retrieve('case_id', 'Positive', CRM_Core_DAO
::$_nullObject, TRUE);
228 // check if there are multiple clients for this case, if so then we need create
229 // relationship and also activities for each contacts
231 // get case client list
232 $clientList = CRM_Case_BAO_Case
::getCaseClients($caseID);
234 $ret = array('is_error' => 0);
236 foreach ($clientList as $sourceContactID) {
237 $relationParams = array(
238 'relationship_type_id' => $relType . '_a_b',
239 'contact_check' => array($relContactID => 1),
241 'case_id' => $caseID,
242 'start_date' => date("Ymd"),
245 $relationIds = array('contact' => $sourceContactID);
247 // check if we are editing/updating existing relationship
248 if ($relationshipID && $relationshipID != 'null') {
249 // here we need to retrieve appropriate relationshipID based on client id and relationship type id
250 $caseRelationships = new CRM_Contact_DAO_Relationship();
251 $caseRelationships->case_id
= $caseID;
252 $caseRelationships->relationship_type_id
= $relType;
253 $caseRelationships->contact_id_a
= $sourceContactID;
254 $caseRelationships->find();
256 while ($caseRelationships->fetch()) {
257 $relationIds['relationship'] = $caseRelationships->id
;
258 $relationIds['contactTarget'] = $relContactID;
260 $caseRelationships->free();
263 // create new or update existing relationship
264 $return = CRM_Contact_BAO_Relationship
::legacyCreateMultiple($relationParams, $relationIds);
266 if (!empty($return[4][0])) {
267 $relationshipID = $return[4][0];
269 //create an activity for case role assignment.CRM-4480
270 CRM_Case_BAO_Case
::createCaseRoleActivity($caseID, $relationshipID, $relContactID);
275 'error_message' => ts('The relationship type definition for the case role is not valid for the client and / or staff contact types. You can review and edit relationship types at <a href="%1">Administer >> Option Lists >> Relationship Types</a>.',
276 array(1 => CRM_Utils_System
::url('civicrm/admin/reltype', 'reset=1'))),
281 CRM_Utils_JSON
::output($ret);
285 * Fetch the custom field help.
287 public static function customField() {
288 $fieldId = CRM_Utils_Type
::escape($_REQUEST['id'], 'Integer');
289 $params = array('id' => $fieldId);
290 $returnProperties = array('help_pre', 'help_post');
293 CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_CustomField', $params, $values, $returnProperties);
294 CRM_Utils_JSON
::output($values);
297 public static function groupTree() {
298 CRM_Utils_System
::setHttpHeader('Content-Type', 'application/json');
299 $gids = CRM_Utils_Type
::escape($_GET['gids'], 'String');
300 echo CRM_Contact_BAO_GroupNestingCache
::json($gids);
301 CRM_Utils_System
::civiExit();
305 * Delete custom value.
307 public static function deleteCustomValue() {
308 CRM_Utils_System
::setHttpHeader('Content-Type', 'text/plain');
309 $customValueID = CRM_Utils_Type
::escape($_REQUEST['valueID'], 'Positive');
310 $customGroupID = CRM_Utils_Type
::escape($_REQUEST['groupID'], 'Positive');
312 CRM_Core_BAO_CustomValue
::deleteCustomValue($customValueID, $customGroupID);
313 $contactId = CRM_Utils_Array
::value('contactId', $_REQUEST);
315 echo CRM_Contact_BAO_Contact
::getCountComponent('custom_' . $_REQUEST['groupID'], $contactId);
318 // reset the group contact cache for this group
319 CRM_Contact_BAO_GroupContactCache
::remove();
320 CRM_Utils_System
::civiExit();
324 * check the CMS username.
326 static public function checkUserName() {
327 $signer = new CRM_Utils_Signer(CRM_Core_Key
::privateKey(), array('for', 'ts'));
329 CRM_Utils_Time
::getTimeRaw() > $_REQUEST['ts'] + self
::CHECK_USERNAME_TTL
330 ||
$_REQUEST['for'] != 'civicrm/ajax/cmsuser'
331 ||
!$signer->validate($_REQUEST['sig'], $_REQUEST)
333 $user = array('name' => 'error');
334 CRM_Utils_JSON
::output($user);
337 $config = CRM_Core_Config
::singleton();
338 $username = trim($_REQUEST['cms_name']);
340 $params = array('name' => $username);
343 $config->userSystem
->checkUserNameEmailExists($params, $errors);
345 if (isset($errors['cms_name']) ||
isset($errors['name'])) {
346 //user name is not available
347 $user = array('name' => 'no');
348 CRM_Utils_JSON
::output($user);
351 //user name is available
352 $user = array('name' => 'yes');
353 CRM_Utils_JSON
::output($user);
356 // Not reachable: JSON::output() above exits.
357 CRM_Utils_System
::civiExit();
361 * Function to get email address of a contact.
363 public static function getContactEmail() {
364 if (!empty($_REQUEST['contact_id'])) {
365 $contactID = CRM_Utils_Type
::escape($_REQUEST['contact_id'], 'Positive');
366 if (!CRM_Contact_BAO_Contact_Permission
::allow($contactID, CRM_Core_Permission
::EDIT
)) {
371 ) = CRM_Contact_BAO_Contact_Location
::getEmailDetails($contactID);
373 CRM_Utils_System
::setHttpHeader('Content-Type', 'text/plain');
379 $noemail = CRM_Utils_Array
::value('noemail', $_GET);
381 $name = CRM_Utils_Array
::value('name', $_GET);
383 $name = CRM_Utils_Type
::escape($name, 'String');
385 $queryString = " cc.sort_name LIKE '%$name%'";
388 $queryString = " ( cc.sort_name LIKE '%$name%' OR ce.email LIKE '%$name%' ) ";
392 $cid = CRM_Utils_Array
::value('cid', $_GET);
394 //check cid for integer
395 $contIDS = explode(',', $cid);
396 foreach ($contIDS as $contID) {
397 CRM_Utils_Type
::escape($contID, 'Integer');
399 $queryString = " cc.id IN ( $cid )";
404 $offset = CRM_Utils_Array
::value('offset', $_GET, 0);
405 $rowCount = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'search_autocomplete_count', NULL, 10);
407 $offset = CRM_Utils_Type
::escape($offset, 'Int');
409 // add acl clause here
410 list($aclFrom, $aclWhere) = CRM_Contact_BAO_Contact_Permission
::cacheClause('cc');
412 $aclWhere = " AND $aclWhere";
416 SELECT sort_name name, cc.id
417 FROM civicrm_contact cc
419 WHERE cc.is_deceased = 0 AND {$queryString}
421 LIMIT {$offset}, {$rowCount}
424 // send query to hook to be modified if needed
425 CRM_Utils_Hook
::contactListQuery($query,
427 CRM_Utils_Array
::value('context', $_GET),
428 CRM_Utils_Array
::value('cid', $_GET)
431 $dao = CRM_Core_DAO
::executeQuery($query);
432 while ($dao->fetch()) {
435 'text' => $dao->name
,
441 SELECT sort_name name, ce.email, cc.id
442 FROM civicrm_email ce INNER JOIN civicrm_contact cc ON cc.id = ce.contact_id
444 WHERE ce.on_hold = 0 AND cc.is_deceased = 0 AND cc.do_not_email = 0 AND {$queryString}
446 LIMIT {$offset}, {$rowCount}
449 // send query to hook to be modified if needed
450 CRM_Utils_Hook
::contactListQuery($query,
452 CRM_Utils_Array
::value('context', $_GET),
453 CRM_Utils_Array
::value('cid', $_GET)
456 $dao = CRM_Core_DAO
::executeQuery($query);
458 while ($dao->fetch()) {
461 'text' => '"' . $dao->name
. '" <' . $dao->email
. '>',
462 'id' => (CRM_Utils_Array
::value('id', $_GET)) ?
"{$dao->id}::{$dao->email}" : '"' . $dao->name
. '" <' . $dao->email
. '>',
467 CRM_Utils_JSON
::output($result);
471 CRM_Utils_System
::civiExit();
474 public static function getContactPhone() {
477 //check for mobile type
478 $phoneTypes = CRM_Core_OptionGroup
::values('phone_type', TRUE, FALSE, FALSE, NULL, 'name');
479 $mobileType = CRM_Utils_Array
::value('Mobile', $phoneTypes);
481 $name = CRM_Utils_Array
::value('name', $_GET);
483 $name = CRM_Utils_Type
::escape($name, 'String');
484 $queryString = " ( cc.sort_name LIKE '%$name%' OR cp.phone LIKE '%$name%' ) ";
487 $cid = CRM_Utils_Array
::value('cid', $_GET);
489 //check cid for integer
490 $contIDS = explode(',', $cid);
491 foreach ($contIDS as $contID) {
492 CRM_Utils_Type
::escape($contID, 'Integer');
494 $queryString = " cc.id IN ( $cid )";
499 $offset = CRM_Utils_Array
::value('offset', $_GET, 0);
500 $rowCount = CRM_Utils_Array
::value('rowcount', $_GET, 20);
502 $offset = CRM_Utils_Type
::escape($offset, 'Int');
503 $rowCount = CRM_Utils_Type
::escape($rowCount, 'Int');
505 // add acl clause here
506 list($aclFrom, $aclWhere) = CRM_Contact_BAO_Contact_Permission
::cacheClause('cc');
508 $aclWhere = " AND $aclWhere";
512 SELECT sort_name name, cp.phone, cc.id
513 FROM civicrm_phone cp INNER JOIN civicrm_contact cc ON cc.id = cp.contact_id
515 WHERE cc.is_deceased = 0 AND cc.do_not_sms = 0 AND cp.phone_type_id = {$mobileType} AND {$queryString}
517 LIMIT {$offset}, {$rowCount}
520 // send query to hook to be modified if needed
521 CRM_Utils_Hook
::contactListQuery($query,
523 CRM_Utils_Array
::value('context', $_GET),
524 CRM_Utils_Array
::value('cid', $_GET)
527 $dao = CRM_Core_DAO
::executeQuery($query);
529 while ($dao->fetch()) {
531 'text' => '"' . $dao->name
. '" (' . $dao->phone
. ')',
532 'id' => (CRM_Utils_Array
::value('id', $_GET)) ?
"{$dao->id}::{$dao->phone}" : '"' . $dao->name
. '" <' . $dao->phone
. '>',
538 CRM_Utils_JSON
::output($result);
540 CRM_Utils_System
::civiExit();
544 public static function buildSubTypes() {
545 $parent = CRM_Utils_Array
::value('parentId', $_REQUEST);
549 $contactType = 'Individual';
553 $contactType = 'Household';
557 $contactType = 'Organization';
561 $subTypes = CRM_Contact_BAO_ContactType
::subTypePairs($contactType, FALSE, NULL);
563 CRM_Utils_JSON
::output($subTypes);
566 public static function buildDedupeRules() {
567 $parent = CRM_Utils_Array
::value('parentId', $_REQUEST);
571 $contactType = 'Individual';
575 $contactType = 'Household';
579 $contactType = 'Organization';
583 $dedupeRules = CRM_Dedupe_BAO_RuleGroup
::getByType($contactType);
585 CRM_Utils_JSON
::output($dedupeRules);
589 * Function used for CiviCRM dashboard operations.
591 public static function dashboard() {
592 $operation = CRM_Utils_Type
::escape($_REQUEST['op'], 'String');
594 switch ($operation) {
595 case 'get_widgets_by_column':
596 // This would normally be coming from either the database (this user's settings) or a default/initial dashboard configuration.
597 // get contact id of logged in user
599 $dashlets = CRM_Core_BAO_Dashboard
::getContactDashlets();
603 $dashletID = CRM_Utils_Type
::escape($_GET['id'], 'Positive');
605 $dashlets = CRM_Core_BAO_Dashboard
::getDashletInfo($dashletID);
609 CRM_Core_BAO_Dashboard
::saveDashletChanges($_REQUEST['columns']);
610 CRM_Utils_System
::civiExit();
611 case 'delete_dashlet':
612 $dashletID = CRM_Utils_Type
::escape($_REQUEST['dashlet_id'], 'Positive');
613 CRM_Core_BAO_Dashboard
::deleteDashlet($dashletID);
614 CRM_Utils_System
::civiExit();
617 CRM_Utils_JSON
::output($dashlets);
621 * Retrieve signature based on email id.
623 public static function getSignature() {
624 $emailID = CRM_Utils_Type
::escape($_REQUEST['emailID'], 'Positive');
625 $query = "SELECT signature_text, signature_html FROM civicrm_email WHERE id = {$emailID}";
626 $dao = CRM_Core_DAO
::executeQuery($query);
628 $signatures = array();
629 while ($dao->fetch()) {
631 'signature_text' => $dao->signature_text
,
632 'signature_html' => $dao->signature_html
,
636 CRM_Utils_JSON
::output($signatures);
642 public static function processDupes() {
643 $oper = CRM_Utils_Type
::escape($_REQUEST['op'], 'String');
644 $cid = CRM_Utils_Type
::escape($_REQUEST['cid'], 'Positive');
645 $oid = CRM_Utils_Type
::escape($_REQUEST['oid'], 'Positive');
647 if (!$oper ||
!$cid ||
!$oid) {
651 $exception = new CRM_Dedupe_DAO_Exception();
652 $exception->contact_id1
= $cid;
653 $exception->contact_id2
= $oid;
654 //make sure contact2 > contact1.
656 $exception->contact_id1
= $oid;
657 $exception->contact_id2
= $cid;
659 $exception->find(TRUE);
661 if ($oper == 'dupe-nondupe') {
662 $status = $exception->save();
664 if ($oper == 'nondupe-dupe') {
665 $status = $exception->delete();
668 CRM_Utils_JSON
::output(array('status' => ($status) ?
$oper : $status));
672 * Retrieve list of duplicate pairs from cache table.
674 public static function getDedupes() {
675 $offset = isset($_REQUEST['start']) ? CRM_Utils_Type
::escape($_REQUEST['start'], 'Integer') : 0;
676 $rowCount = isset($_REQUEST['length']) ? CRM_Utils_Type
::escape($_REQUEST['length'], 'Integer') : 25;
678 $gid = isset($_REQUEST['gid']) ? CRM_Utils_Type
::escape($_REQUEST['gid'], 'Integer') : 0;
679 $rgid = isset($_REQUEST['rgid']) ? CRM_Utils_Type
::escape($_REQUEST['rgid'], 'Integer') : 0;
680 $selected = isset($_REQUEST['selected']) ? CRM_Utils_Type
::escape($_REQUEST['selected'], 'Integer') : 0;
686 $contactType = CRM_Core_DAO
::getFieldValue('CRM_Dedupe_DAO_RuleGroup', $rgid, 'contact_type');
689 $cacheKeyString = "merge {$contactType}_{$rgid}_{$gid}";
690 $searchRows = array();
691 $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');
693 foreach ($_REQUEST['columns'] as $columnInfo) {
694 if (!empty($columnInfo['search']['value'])) {
695 $
{$columnInfo['data']} = CRM_Utils_Type
::escape($columnInfo['search']['value'], 'String');
700 $searchData = CRM_Utils_Array
::value('search', $_REQUEST);
701 $searchData['value'] = CRM_Utils_Type
::escape($searchData['value'], 'String');
703 if ($src ||
!empty($searchData['value'])) {
704 $src = $src ?
$src : $searchData['value'];
705 $where[] = " cc1.display_name LIKE '%{$src}%'";
707 if ($dst ||
!empty($searchData['value'])) {
708 $dst = $dst ?
$dst : $searchData['value'];
709 $where[] = " cc2.display_name LIKE '%{$dst}%'";
711 if ($src_email ||
!empty($searchData['value'])) {
712 $src_email = $src_email ?
$src_email : $searchData['value'];
713 $where[] = " (ce1.is_primary = 1 AND ce1.email LIKE '%{$src_email}%')";
715 if ($dst_email ||
!empty($searchData['value'])) {
716 $dst_email = $dst_email ?
$dst_email : $searchData['value'];
717 $where[] = " (ce2.is_primary = 1 AND ce2.email LIKE '%{$dst_email}%')";
719 if ($src_postcode ||
!empty($searchData['value'])) {
720 $src_postcode = $src_postcode ?
$src_postcode : $searchData['value'];
721 $where[] = " (ca1.is_primary = 1 AND ca1.postal_code LIKE '%{$src_postcode}%')";
723 if ($dst_postcode ||
!empty($searchData['value'])) {
724 $dst_postcode = $dst_postcode ?
$dst_postcode : $searchData['value'];
725 $where[] = " (ca2.is_primary = 1 AND ca2.postal_code LIKE '%{$dst_postcode}%')";
727 if ($src_street ||
!empty($searchData['value'])) {
728 $src_street = $src_street ?
$src_street : $searchData['value'];
729 $where[] = " (ca1.is_primary = 1 AND ca1.street_address LIKE '%{$src_street}%')";
731 if ($dst_street ||
!empty($searchData['value'])) {
732 $dst_street = $dst_street ?
$dst_street : $searchData['value'];
733 $where[] = " (ca2.is_primary = 1 AND ca2.street_address LIKE '%{$dst_street}%')";
735 if (!empty($searchData['value'])) {
736 $whereClause = ' ( ' . implode(' OR ', $where) . ' ) ';
739 if (!empty($where)) {
740 $whereClause = implode(' AND ', $where);
743 $whereClause .= $whereClause ?
' AND de.id IS NULL' : ' de.id IS NULL';
746 $whereClause .= ' AND pn.is_selected = 1';
748 $join .= " LEFT JOIN civicrm_dedupe_exception de ON ( pn.entity_id1 = de.contact_id1 AND pn.entity_id2 = de.contact_id2 )";
751 'cc1.contact_type' => 'src_contact_type',
752 'cc1.display_name' => 'src_display_name',
753 'cc1.contact_sub_type' => 'src_contact_sub_type',
754 'cc2.contact_type' => 'dst_contact_type',
755 'cc2.display_name' => 'dst_display_name',
756 'cc2.contact_sub_type' => 'dst_contact_sub_type',
757 'ce1.email' => 'src_email',
758 'ce2.email' => 'dst_email',
759 'ca1.postal_code' => 'src_postcode',
760 'ca2.postal_code' => 'dst_postcode',
761 'ca1.street_address' => 'src_street',
762 'ca2.street_address' => 'dst_street',
766 $join .= " INNER JOIN civicrm_contact cc1 ON cc1.id = pn.entity_id1";
767 $join .= " INNER JOIN civicrm_contact cc2 ON cc2.id = pn.entity_id2";
768 $join .= " LEFT JOIN civicrm_email ce1 ON (ce1.contact_id = pn.entity_id1 AND ce1.is_primary = 1 )";
769 $join .= " LEFT JOIN civicrm_email ce2 ON (ce2.contact_id = pn.entity_id2 AND ce2.is_primary = 1 )";
770 $join .= " LEFT JOIN civicrm_address ca1 ON (ca1.contact_id = pn.entity_id1 AND ca1.is_primary = 1 )";
771 $join .= " LEFT JOIN civicrm_address ca2 ON (ca2.contact_id = pn.entity_id2 AND ca2.is_primary = 1 )";
773 $iTotal = CRM_Core_BAO_PrevNextCache
::getCount($cacheKeyString, $join, $whereClause);
774 foreach ($_REQUEST['order'] as $orderInfo) {
775 if (!empty($orderInfo['column'])) {
776 $orderColumnNumber = $orderInfo['column'];
777 $dir = $orderInfo['dir'];
780 $columnDetails = CRM_Utils_Array
::value($orderColumnNumber, $_REQUEST['columns']);
781 if (!empty($columnDetails)) {
782 switch ($columnDetails['data']) {
784 $whereClause .= " ORDER BY cc1.display_name {$dir}";
788 $whereClause .= " ORDER BY ce1.email {$dir}";
792 $whereClause .= " ORDER BY ca1.street_address {$dir}";
796 $whereClause .= " ORDER BY ca1.postal_code {$dir}";
800 $whereClause .= " ORDER BY cc2.display_name {$dir}";
804 $whereClause .= " ORDER BY ce2.email {$dir}";
808 $whereClause .= " ORDER BY ca2.street_address {$dir}";
812 $whereClause .= " ORDER BY ca2.postal_code {$dir}";
820 $dupePairs = CRM_Core_BAO_PrevNextCache
::retrieve($cacheKeyString, $join, $whereClause, $offset, $rowCount, $select);
821 $iFilteredTotal = CRM_Core_DAO
::singleValueQuery("SELECT FOUND_ROWS()");
824 foreach ($dupePairs as $key => $pairInfo) {
825 $pair =& $pairInfo['data'];
826 $srcContactSubType = CRM_Utils_Array
::value('src_contact_sub_type', $pairInfo);
827 $dstContactSubType = CRM_Utils_Array
::value('dst_contact_sub_type', $pairInfo);
828 $srcTypeImage = CRM_Contact_BAO_Contact_Utils
::getImage($srcContactSubType ?
829 $srcContactSubType : $pairInfo['src_contact_type'],
831 $pairInfo['entity_id1']
833 $dstTypeImage = CRM_Contact_BAO_Contact_Utils
::getImage($dstContactSubType ?
834 $dstContactSubType : $pairInfo['dst_contact_type'],
836 $pairInfo['entity_id2']
839 $searchRows[$count]['is_selected'] = $pairInfo['is_selected'];
840 $searchRows[$count]['is_selected_input'] = "<input type='checkbox' class='crm-dedupe-select' name='pnid_{$pairInfo['prevnext_id']}' value='{$pairInfo['is_selected']}' onclick='toggleDedupeSelect(this)'>";
841 $searchRows[$count]['src_image'] = $srcTypeImage;
842 $searchRows[$count]['src'] = CRM_Utils_System
::href($pair['srcName'], 'civicrm/contact/view', "reset=1&cid={$pairInfo['entity_id1']}");
843 $searchRows[$count]['src_email'] = CRM_Utils_Array
::value('src_email', $pairInfo);
844 $searchRows[$count]['src_street'] = CRM_Utils_Array
::value('src_street', $pairInfo);
845 $searchRows[$count]['src_postcode'] = CRM_Utils_Array
::value('src_postcode', $pairInfo);
846 $searchRows[$count]['dst_image'] = $dstTypeImage;
847 $searchRows[$count]['dst'] = CRM_Utils_System
::href($pair['dstName'], 'civicrm/contact/view', "reset=1&cid={$pairInfo['entity_id2']}");
848 $searchRows[$count]['dst_email'] = CRM_Utils_Array
::value('dst_email', $pairInfo);
849 $searchRows[$count]['dst_street'] = CRM_Utils_Array
::value('dst_street', $pairInfo);
850 $searchRows[$count]['dst_postcode'] = CRM_Utils_Array
::value('dst_postcode', $pairInfo);
851 $searchRows[$count]['conflicts'] = str_replace("',", "',<br/>", CRM_Utils_Array
::value('conflicts', $pair));
852 $searchRows[$count]['weight'] = CRM_Utils_Array
::value('weight', $pair);
854 if (!empty($pairInfo['data']['canMerge'])) {
855 $mergeParams = "reset=1&cid={$pairInfo['entity_id1']}&oid={$pairInfo['entity_id2']}&action=update&rgid={$rgid}";
857 $mergeParams .= "&gid={$gid}";
860 $searchRows[$count]['actions'] = "<a class='crm-dedupe-flip' href='#' data-pnid={$pairInfo['prevnext_id']}>" . ts('flip') . "</a> | ";
861 $searchRows[$count]['actions'] .= CRM_Utils_System
::href(ts('merge'), 'civicrm/contact/merge', $mergeParams);
862 $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>";
865 $searchRows[$count]['actions'] = '<em>' . ts('Insufficient access rights - cannot merge') . '</em>';
871 'data' => $searchRows,
872 'recordsTotal' => $iTotal,
873 'recordsFiltered' => $iFilteredTotal,
875 CRM_Utils_JSON
::output($dupePairs);
879 * Retrieve a PDF Page Format for the PDF Letter form.
881 public function pdfFormat() {
882 $formatId = CRM_Utils_Type
::escape($_REQUEST['formatId'], 'Integer');
884 $pdfFormat = CRM_Core_BAO_PdfFormat
::getById($formatId);
886 CRM_Utils_JSON
::output($pdfFormat);
890 * Retrieve Paper Size dimensions.
892 public static function paperSize() {
893 $paperSizeName = CRM_Utils_Type
::escape($_REQUEST['paperSizeName'], 'String');
895 $paperSize = CRM_Core_BAO_PaperSize
::getByName($paperSizeName);
897 CRM_Utils_JSON
::output($paperSize);
901 * Swap contacts in a dupe pair i.e main with duplicate contact.
903 public static function flipDupePairs($prevNextId = NULL) {
905 $prevNextId = $_REQUEST['pnid'];
908 UPDATE civicrm_prevnext_cache cpc
909 INNER JOIN civicrm_prevnext_cache old on cpc.id = old.id
910 SET cpc.entity_id1 = cpc.entity_id2, cpc.entity_id2 = old.entity_id1 ";
911 if (is_array($prevNextId) && !CRM_Utils_Array
::crmIsEmptyArray($prevNextId)) {
912 $prevNextId = implode(', ', $prevNextId);
913 $prevNextId = CRM_Utils_Type
::escape($prevNextId, 'String');
914 $query .= "WHERE cpc.id IN ({$prevNextId}) AND cpc.is_selected = 1";
917 $prevNextId = CRM_Utils_Type
::escape($prevNextId, 'Positive');
918 $query .= "WHERE cpc.id = $prevNextId";
920 CRM_Core_DAO
::executeQuery($query);
921 CRM_Utils_JSON
::output();
925 * Used to store selected contacts across multiple pages in advanced search.
927 public static function selectUnselectContacts() {
928 $name = CRM_Utils_Array
::value('name', $_REQUEST);
929 $cacheKey = CRM_Utils_Array
::value('qfKey', $_REQUEST);
930 $state = CRM_Utils_Array
::value('state', $_REQUEST, 'checked');
931 $variableType = CRM_Utils_Array
::value('variableType', $_REQUEST, 'single');
933 $actionToPerform = CRM_Utils_Array
::value('action', $_REQUEST, 'select');
935 if ($variableType == 'multiple') {
936 // action post value only works with multiple type variable
938 //multiple names like mark_x_1-mark_x_2 where 1,2 are cids
939 $elements = explode('-', $name);
940 foreach ($elements as $key => $element) {
941 $elements[$key] = self
::_convertToId($element);
943 CRM_Core_BAO_PrevNextCache
::markSelection($cacheKey, $actionToPerform, $elements);
946 CRM_Core_BAO_PrevNextCache
::markSelection($cacheKey, $actionToPerform);
949 elseif ($variableType == 'single') {
950 $cId = self
::_convertToId($name);
951 $action = ($state == 'checked') ?
'select' : 'unselect';
952 CRM_Core_BAO_PrevNextCache
::markSelection($cacheKey, $action, $cId);
954 $contactIds = CRM_Core_BAO_PrevNextCache
::getSelection($cacheKey);
955 $countSelectionCids = count($contactIds[$cacheKey]);
957 $arrRet = array('getCount' => $countSelectionCids);
958 CRM_Utils_JSON
::output($arrRet);
962 * @param string $name
966 public static function _convertToId($name) {
967 if (substr($name, 0, CRM_Core_Form
::CB_PREFIX_LEN
) == CRM_Core_Form
::CB_PREFIX
) {
968 $cId = substr($name, CRM_Core_Form
::CB_PREFIX_LEN
);
973 public static function getAddressDisplay() {
974 $contactId = CRM_Utils_Array
::value('contact_id', $_REQUEST);
976 $addressVal["error_message"] = "no contact id found";
979 $entityBlock = array(
980 'contact_id' => $contactId,
981 'entity_id' => $contactId,
983 $addressVal = CRM_Core_BAO_Address
::getValues($entityBlock);
986 CRM_Utils_JSON
::output($addressVal);
990 * Mark dupe pairs as selected from un-selected state or vice-versa, in dupe cache table.
992 public static function toggleDedupeSelect() {
993 $rgid = CRM_Utils_Type
::escape($_REQUEST['rgid'], 'Integer');
994 $gid = CRM_Utils_Type
::escape($_REQUEST['gid'], 'Integer');
995 $pnid = $_REQUEST['pnid'];
996 $isSelected = CRM_Utils_Type
::escape($_REQUEST['is_selected'], 'Boolean');
998 $contactType = CRM_Core_DAO
::getFieldValue('CRM_Dedupe_DAO_RuleGroup', $rgid, 'contact_type');
999 $cacheKeyString = "merge $contactType";
1000 $cacheKeyString .= $rgid ?
"_{$rgid}" : '_0';
1001 $cacheKeyString .= $gid ?
"_{$gid}" : '_0';
1004 1 => array($isSelected, 'Boolean'),
1005 3 => array("$cacheKeyString%", 'String'), // using % to address rows with conflicts as well
1008 //check pnid is_array or integer
1009 $whereClause = NULL;
1010 if (is_array($pnid) && !CRM_Utils_Array
::crmIsEmptyArray($pnid)) {
1011 $pnid = implode(', ', $pnid);
1012 $pnid = CRM_Utils_Type
::escape($pnid, 'String');
1013 $whereClause = " id IN ( {$pnid} ) ";
1016 $pnid = CRM_Utils_Type
::escape($pnid, 'Integer');
1017 $whereClause = " id = %2";
1018 $params[2] = array($pnid, 'Integer');
1021 $sql = "UPDATE civicrm_prevnext_cache SET is_selected = %1 WHERE {$whereClause} AND cacheKey LIKE %3";
1022 CRM_Core_DAO
::executeQuery($sql, $params);
1024 CRM_Utils_System
::civiExit();
1028 * Retrieve contact relationships.
1030 public static function getContactRelationships() {
1031 $contactID = CRM_Utils_Type
::escape($_GET['cid'], 'Integer');
1032 $context = CRM_Utils_Type
::escape($_GET['context'], 'String');
1033 $relationship_type_id = CRM_Utils_Type
::escape(CRM_Utils_Array
::value('relationship_type_id', $_GET), 'Integer', FALSE);
1035 if (!CRM_Contact_BAO_Contact_Permission
::allow($contactID)) {
1036 return CRM_Utils_System
::permissionDenied();
1039 $sortMapper = array();
1040 foreach ($_GET['columns'] as $key => $value) {
1041 $sortMapper[$key] = $value['data'];
1044 $offset = isset($_GET['start']) ? CRM_Utils_Type
::escape($_GET['start'], 'Integer') : 0;
1045 $rowCount = isset($_GET['length']) ? CRM_Utils_Type
::escape($_GET['length'], 'Integer') : 25;
1046 $sort = isset($_GET['order'][0]['column']) ? CRM_Utils_Array
::value(CRM_Utils_Type
::escape($_GET['order'][0]['column'], 'Integer'), $sortMapper) : NULL;
1047 $sortOrder = isset($_GET['order'][0]['dir']) ? CRM_Utils_Type
::escape($_GET['order'][0]['dir'], 'String') : 'asc';
1050 if ($sort && $sortOrder) {
1051 $params['sortBy'] = $sort . ' ' . $sortOrder;
1054 $params['page'] = ($offset / $rowCount) +
1;
1055 $params['rp'] = $rowCount;
1057 $params['contact_id'] = $contactID;
1058 $params['context'] = $context;
1059 if ($relationship_type_id) {
1060 $params['relationship_type_id'] = $relationship_type_id;
1063 // get the contact relationships
1064 $relationships = CRM_Contact_BAO_Relationship
::getContactRelationshipSelector($params);
1066 CRM_Utils_JSON
::output($relationships);