3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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
51 public static function getContactList() {
52 // if context is 'customfield'
53 if (CRM_Utils_Array
::value('context', $_GET) == 'customfield') {
54 return self
::contactReference();
57 $params = array('version' => 3, 'check_permissions' => TRUE);
60 // FIXME: param keys don't match input keys, using this array to translate
63 'fieldName' => 'field_name',
64 'tableName' => 'table_name',
65 'context' => 'context',
67 'contact_sub_type' => 'contact_sub_type',
68 'contact_type' => 'contact_type',
70 foreach ($whitelist as $key => $param) {
71 if (!empty($_GET[$key])) {
72 $params[$param] = $_GET[$key];
76 //CRM-10687: Allow quicksearch by multiple fields
77 if (!empty($params['field_name'])) {
78 if ($params['field_name'] == 'phone_numeric') {
79 $params['name'] = preg_replace('/[^\d]/', '', $params['name']);
81 if (!$params['name']) {
82 CRM_Utils_System
::civiExit();
95 foreach ($whitelist as $key) {
96 if (!empty($_GET[$key]) && is_numeric($_GET[$key])) {
97 $params[$key] = $_GET[$key];
101 $result = civicrm_api('Contact', 'getquick', $params);
102 CRM_Core_Page_AJAX
::autocompleteResults(CRM_Utils_Array
::value('values', $result), 'data');
106 * Ajax callback for custom fields of type ContactReference
108 * Todo: Migrate contact reference fields to use EntityRef
110 public static function contactReference() {
111 $name = CRM_Utils_Array
::value('term', $_GET);
112 $name = CRM_Utils_Type
::escape($name, 'String');
113 $cfID = CRM_Utils_Type
::escape($_GET['id'], 'Positive');
115 // check that this is a valid, active custom field of Contact Reference type
116 $params = array('id' => $cfID);
117 $returnProperties = array('filter', 'data_type', 'is_active');
119 CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_CustomField', $params, $cf, $returnProperties);
120 if (!$cf['id'] ||
!$cf['is_active'] ||
$cf['data_type'] != 'ContactReference') {
121 CRM_Utils_System
::civiExit('error');
124 if (!empty($cf['filter'])) {
125 $filterParams = array();
126 parse_str($cf['filter'], $filterParams);
128 $action = CRM_Utils_Array
::value('action', $filterParams);
130 if (!empty($action) &&
131 !in_array($action, array('get', 'lookup'))
133 CRM_Utils_System
::civiExit('error');
137 $list = array_keys(CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
138 'contact_reference_options'
141 $return = array_unique(array_merge(array('sort_name'), $list));
143 $limit = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'search_autocomplete_count', NULL, 10);
145 $params = array('offset' => 0, 'rowCount' => $limit, 'version' => 3);
146 foreach ($return as $fld) {
147 $params["return.{$fld}"] = 1;
150 if (!empty($action)) {
151 $excludeGet = array('reset', 'key', 'className', 'fnName', 'json', 'reset', 'context', 'timestamp', 'limit', 'id', 's', 'q', 'action');
152 foreach ($_GET as $param => $val) {
154 in_array($param, $excludeGet) ||
155 strpos($param, 'return.') !== FALSE ||
156 strpos($param, 'api.') !== FALSE
160 $params[$param] = $val;
165 $params['sort_name'] = $name;
168 $params['sort'] = 'sort_name';
170 // tell api to skip permission chk. dgg
171 $params['check_permissions'] = 0;
173 // add filter variable to params
174 if (!empty($filterParams)) {
175 $params = array_merge($params, $filterParams);
178 $contact = civicrm_api('Contact', 'Get', $params);
180 if (!empty($contact['is_error'])) {
181 CRM_Utils_System
::civiExit('error');
184 $contactList = array();
185 foreach ($contact['values'] as $value) {
187 foreach ($return as $fld) {
188 if (!empty($value[$fld])) {
189 $view[] = $value[$fld];
192 $contactList[] = array('id' => $value['id'], 'text' => implode(' :: ', $view));
195 CRM_Utils_System
::civiExit(json_encode($contactList));
199 * Fetch PCP ID by PCP Supporter sort_name, also displays PCP title and associated Contribution Page title
201 public static function getPCPList() {
202 $name = CRM_Utils_Array
::value('s', $_GET);
203 $name = CRM_Utils_Type
::escape($name, 'String');
206 $where = ' AND pcp.page_id = cp.id AND pcp.contact_id = cc.id';
208 $config = CRM_Core_Config
::singleton();
209 if ($config->includeWildCardInName
) {
210 $strSearch = "%$name%";
213 $strSearch = "$name%";
215 $includeEmailFrom = $includeNickName = '';
216 if ($config->includeNickNameInName
) {
217 $includeNickName = " OR nick_name LIKE '$strSearch'";
219 if ($config->includeEmailInName
) {
220 $includeEmailFrom = "LEFT JOIN civicrm_email eml ON ( cc.id = eml.contact_id AND eml.is_primary = 1 )";
221 $whereClause = " WHERE ( email LIKE '$strSearch' OR sort_name LIKE '$strSearch' $includeNickName ) {$where} ";
224 $whereClause = " WHERE ( sort_name LIKE '$strSearch' $includeNickName ) {$where} ";
227 if (!empty($_GET['limit'])) {
228 $limit = CRM_Utils_Type
::escape($_GET['limit'], 'Positive');
231 $select = 'cc.sort_name, pcp.title, cp.title';
235 SELECT pcp.id as id, CONCAT_WS( ' :: ', {$select} ) as data, sort_name
236 FROM civicrm_pcp pcp, civicrm_contribution_page cp, civicrm_contact cc
238 {$whereClause} AND pcp.page_type = 'contribute'
240 SELECT pcp.id as id, CONCAT_WS( ' :: ', {$select} ) as data, sort_name
241 FROM civicrm_pcp pcp, civicrm_event cp, civicrm_contact cc
243 {$whereClause} AND pcp.page_type = 'event'
249 $dao = CRM_Core_DAO
::executeQuery($query);
251 while ($dao->fetch()) {
252 $results[] = array('id' => $dao->id
, 'text' => $dao->data
);
254 CRM_Utils_JSON
::output($results);
257 public static function relationship() {
258 $relType = CRM_Utils_Request
::retrieve('rel_type', 'Positive', CRM_Core_DAO
::$_nullObject, TRUE);
259 $relContactID = CRM_Utils_Request
::retrieve('rel_contact', 'Positive', CRM_Core_DAO
::$_nullObject, TRUE);
260 $relationshipID = CRM_Utils_Array
::value('rel_id', $_REQUEST); // this used only to determine add or update mode
261 $caseID = CRM_Utils_Request
::retrieve('case_id', 'Positive', CRM_Core_DAO
::$_nullObject, TRUE);
263 // check if there are multiple clients for this case, if so then we need create
264 // relationship and also activities for each contacts
266 // get case client list
267 $clientList = CRM_Case_BAO_Case
::getCaseClients($caseID);
269 $ret = array('is_error' => 0);
271 foreach ($clientList as $sourceContactID) {
272 $relationParams = array(
273 'relationship_type_id' => $relType . '_a_b',
274 'contact_check' => array($relContactID => 1),
276 'case_id' => $caseID,
277 'start_date' => date("Ymd"),
280 $relationIds = array('contact' => $sourceContactID);
282 // check if we are editing/updating existing relationship
283 if ($relationshipID && $relationshipID != 'null') {
284 // here we need to retrieve appropriate relationshipID based on client id and relationship type id
285 $caseRelationships = new CRM_Contact_DAO_Relationship();
286 $caseRelationships->case_id
= $caseID;
287 $caseRelationships->relationship_type_id
= $relType;
288 $caseRelationships->contact_id_a
= $sourceContactID;
289 $caseRelationships->find();
291 while ($caseRelationships->fetch()) {
292 $relationIds['relationship'] = $caseRelationships->id
;
293 $relationIds['contactTarget'] = $relContactID;
295 $caseRelationships->free();
298 // create new or update existing relationship
299 $return = CRM_Contact_BAO_Relationship
::createMultiple($relationParams, $relationIds);
301 if (!empty($return[4][0])) {
302 $relationshipID = $return[4][0];
304 //create an activity for case role assignment.CRM-4480
305 CRM_Case_BAO_Case
::createCaseRoleActivity($caseID, $relationshipID, $relContactID);
310 '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>.',
311 array(1 => CRM_Utils_System
::url('civicrm/admin/reltype', 'reset=1'))),
316 CRM_Utils_JSON
::output($ret);
320 * Fetch the custom field help
322 public static function customField() {
323 $fieldId = CRM_Utils_Type
::escape($_REQUEST['id'], 'Integer');
324 $params = array('id' => $fieldId);
325 $returnProperties = array('help_pre', 'help_post');
328 CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_CustomField', $params, $values, $returnProperties);
329 CRM_Utils_JSON
::output($values);
332 public static function groupTree() {
333 $gids = CRM_Utils_Type
::escape($_GET['gids'], 'String');
334 echo CRM_Contact_BAO_GroupNestingCache
::json($gids);
335 CRM_Utils_System
::civiExit();
340 * Old quicksearch function. No longer used in core.
341 * @todo: Remove this function and associated menu entry in CiviCRM 5
343 public static function search() {
345 $name = CRM_Utils_Array
::value('name', $_GET, '');
346 if (!array_key_exists('name', $_GET)) {
347 $name = CRM_Utils_Array
::value('s', $_GET) . '%';
350 $name = CRM_Utils_Type
::escape($name, 'String');
352 if (!empty($_GET['id'])) {
354 if (is_numeric($_GET['id'])) {
355 $id = CRM_Utils_Type
::escape($_GET['id'], 'Integer');
356 $whereIdClause = " AND civicrm_contact.id = {$id}";
364 if ($name ||
isset($id)) {
367 //contact's based of relationhip type
369 if (isset($_GET['rel'])) {
370 $relation = explode('_', $_GET['rel']);
371 $relType = CRM_Utils_Type
::escape($relation[0], 'Integer');
372 $rel = CRM_Utils_Type
::escape($relation[2], 'String');
375 //shared household info
377 if (isset($_GET['sh'])) {
378 $shared = CRM_Utils_Type
::escape($_GET['sh'], 'Integer');
380 $contactType = 'Household';
381 $cName = 'household_name';
384 $contactType = 'Organization';
385 $cName = 'organization_name';
389 // contacts of type household
390 $hh = $addStreet = $addCity = NULL;
391 if (isset($_GET['hh'])) {
392 $hh = CRM_Utils_Type
::escape($_GET['hh'], 'Integer');
396 $organization = $street = $city = NULL;
397 if (isset($_GET['org'])) {
398 $organization = CRM_Utils_Type
::escape($_GET['org'], 'Integer');
401 if (isset($_GET['org']) ||
isset($_GET['hh'])) {
403 $splitName = explode(' :: ', $name);
405 $contactName = trim(CRM_Utils_Array
::value('0', $splitName));
406 $street = trim(CRM_Utils_Array
::value('1', $splitName));
407 $city = trim(CRM_Utils_Array
::value('2', $splitName));
410 $contactName = $name;
414 $addStreet = "AND civicrm_address.street_address LIKE '$street%'";
417 $addCity = "AND civicrm_address.city LIKE '$city%'";
424 SELECT CONCAT_WS(' :: ',sort_name,LEFT(street_address,25),city) 'sort_name',
425 civicrm_contact.id 'id'
427 LEFT JOIN civicrm_address ON ( civicrm_contact.id = civicrm_address.contact_id
428 AND civicrm_address.is_primary=1
430 WHERE civicrm_contact.contact_type='Organization' AND organization_name LIKE '%$contactName%'
431 {$addStreet} {$addCity} {$whereIdClause}
432 ORDER BY organization_name ";
436 SELECT CONCAT_WS(':::' , sort_name, supplemental_address_1, sp.abbreviation, postal_code, cc.name )'sort_name' , civicrm_contact.id 'id' , civicrm_contact.display_name 'disp' FROM civicrm_contact LEFT JOIN civicrm_address ON (civicrm_contact.id =civicrm_address.contact_id AND civicrm_address.is_primary =1 )LEFT JOIN civicrm_state_province sp ON (civicrm_address.state_province_id =sp.id )LEFT JOIN civicrm_country cc ON (civicrm_address.country_id =cc.id )WHERE civicrm_contact.contact_type ='{$contactType}' AND {$cName} LIKE '%$name%' {$whereIdClause} ORDER BY {$cName} ";
440 SELECT CONCAT_WS(' :: ' , sort_name, LEFT(street_address,25),city) 'sort_name' , location_type_id 'location_type_id', is_primary 'is_primary', is_billing 'is_billing', civicrm_contact.id 'id'
442 LEFT JOIN civicrm_address ON (civicrm_contact.id =civicrm_address.contact_id AND civicrm_address.is_primary =1 )
443 WHERE civicrm_contact.contact_type ='Household'
444 AND household_name LIKE '%$contactName%' {$addStreet} {$addCity} {$whereIdClause} ORDER BY household_name ";
447 if (!empty($_GET['case'])) {
449 SELECT distinct(c.id), c.sort_name
450 FROM civicrm_contact c
451 LEFT JOIN civicrm_relationship ON civicrm_relationship.contact_id_{$rel} = c.id
452 WHERE c.sort_name LIKE '%$name%'
453 AND civicrm_relationship.relationship_type_id = $relType
463 WHERE sort_name LIKE '%$name'
465 ORDER BY sort_name ";
469 if (isset($_GET['limit'])) {
470 $limit = CRM_Utils_Type
::escape($_GET['limit'], 'Positive');
473 $query .= " LIMIT 0,{$limit}";
475 $dao = CRM_Core_DAO
::executeQuery($query);
478 while ($dao->fetch()) {
479 echo $dao->sort_name
;
480 CRM_Utils_System
::civiExit();
484 while ($dao->fetch()) {
487 'name' => addslashes($dao->sort_name
),
492 echo $elements = "$dao->sort_name|$dao->id|$dao->location_type_id|$dao->is_primary|$dao->is_billing\n";
495 //for adding new household address / organization
496 if (empty($elements) && !$json && ($hh ||
$organization)) {
497 echo CRM_Utils_Array
::value('s', $_GET);
502 if (isset($_GET['sh'])) {
504 CRM_Utils_System
::civiExit();
507 if (empty($elements)) {
508 $name = str_replace('%', '', $name);
516 echo json_encode($elements);
518 CRM_Utils_System
::civiExit();
522 * Delete custom value
524 public static function deleteCustomValue() {
525 $customValueID = CRM_Utils_Type
::escape($_REQUEST['valueID'], 'Positive');
526 $customGroupID = CRM_Utils_Type
::escape($_REQUEST['groupID'], 'Positive');
528 CRM_Core_BAO_CustomValue
::deleteCustomValue($customValueID, $customGroupID);
529 $contactId = CRM_Utils_Array
::value('contactId', $_REQUEST);
531 echo CRM_Contact_BAO_Contact
::getCountComponent('custom_' . $_REQUEST['groupID'], $contactId);
534 // reset the group contact cache for this group
535 CRM_Contact_BAO_GroupContactCache
::remove();
536 CRM_Utils_System
::civiExit();
540 * Perform enable / disable actions on record.
542 public static function enableDisable() {
543 $op = CRM_Utils_Type
::escape($_REQUEST['op'], 'String');
544 $recordID = CRM_Utils_Type
::escape($_REQUEST['recordID'], 'Positive');
545 $recordBAO = CRM_Utils_Type
::escape($_REQUEST['recordBAO'], 'String');
548 if ($op == 'disable-enable') {
551 elseif ($op == 'enable-disable') {
554 $status = array('status' => 'record-updated-fail');
555 if (isset($isActive)) {
556 // first munge and clean the recordBAO and get rid of any non alpha numeric characters
557 $recordBAO = CRM_Utils_String
::munge($recordBAO);
558 $recordClass = explode('_', $recordBAO);
560 // make sure recordClass is namespaced (we cant check CRM since extensions can also use this)
561 // but it should be at least 3 levels deep
562 if (count($recordClass) >= 3) {
563 require_once str_replace('_', DIRECTORY_SEPARATOR
, $recordBAO) . ".php";
564 $method = 'setIsActive';
566 if (method_exists($recordBAO, $method)) {
567 $updated = call_user_func_array(array($recordBAO, $method),
568 array($recordID, $isActive)
571 $status = array('status' => 'record-updated-success');
574 // call hook enableDisable
575 CRM_Utils_Hook
::enableDisable($recordBAO, $recordID, $isActive);
578 CRM_Utils_JSON
::output($status);
583 * check the CMS username
585 static public function checkUserName() {
586 $signer = new CRM_Utils_Signer(CRM_Core_Key
::privateKey(), array('for', 'ts'));
588 CRM_Utils_Time
::getTimeRaw() > $_REQUEST['ts'] + self
::CHECK_USERNAME_TTL
589 ||
$_REQUEST['for'] != 'civicrm/ajax/cmsuser'
590 ||
!$signer->validate($_REQUEST['sig'], $_REQUEST)
592 $user = array('name' => 'error');
593 echo json_encode($user);
594 CRM_Utils_System
::civiExit();
597 $config = CRM_Core_Config
::singleton();
598 $username = trim($_REQUEST['cms_name']);
600 $params = array('name' => $username);
603 $config->userSystem
->checkUserNameEmailExists($params, $errors);
605 if (isset($errors['cms_name']) ||
isset($errors['name'])) {
606 //user name is not availble
607 $user = array('name' => 'no');
608 echo json_encode($user);
611 //user name is available
612 $user = array('name' => 'yes');
613 echo json_encode($user);
615 CRM_Utils_System
::civiExit();
619 * Function to get email address of a contact
621 public static function getContactEmail() {
622 if (!empty($_REQUEST['contact_id'])) {
623 $contactID = CRM_Utils_Type
::escape($_REQUEST['contact_id'], 'Positive');
624 if (!CRM_Contact_BAO_Contact_Permission
::allow($contactID, CRM_Core_Permission
::EDIT
)) {
629 ) = CRM_Contact_BAO_Contact_Location
::getEmailDetails($contactID);
635 $noemail = CRM_Utils_Array
::value('noemail', $_GET);
637 $name = CRM_Utils_Array
::value('name', $_GET);
639 $name = CRM_Utils_Type
::escape($name, 'String');
641 $queryString = " cc.sort_name LIKE '%$name%'";
644 $queryString = " ( cc.sort_name LIKE '%$name%' OR ce.email LIKE '%$name%' ) ";
648 $cid = CRM_Utils_Array
::value('cid', $_GET);
650 //check cid for interger
651 $contIDS = explode(',', $cid);
652 foreach ($contIDS as $contID) {
653 CRM_Utils_Type
::escape($contID, 'Integer');
655 $queryString = " cc.id IN ( $cid )";
660 $offset = CRM_Utils_Array
::value('offset', $_GET, 0);
661 $rowCount = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'search_autocomplete_count', NULL, 10);
663 $offset = CRM_Utils_Type
::escape($offset, 'Int');
665 // add acl clause here
666 list($aclFrom, $aclWhere) = CRM_Contact_BAO_Contact_Permission
::cacheClause('cc');
668 $aclWhere = " AND $aclWhere";
672 SELECT sort_name name, cc.id
673 FROM civicrm_contact cc
675 WHERE cc.is_deceased = 0 AND {$queryString}
677 LIMIT {$offset}, {$rowCount}
680 // send query to hook to be modified if needed
681 CRM_Utils_Hook
::contactListQuery($query,
683 CRM_Utils_Array
::value('context', $_GET),
684 CRM_Utils_Array
::value('cid', $_GET)
687 $dao = CRM_Core_DAO
::executeQuery($query);
688 while ($dao->fetch()) {
691 'text' => $dao->name
,
697 SELECT sort_name name, ce.email, cc.id
698 FROM civicrm_email ce INNER JOIN civicrm_contact cc ON cc.id = ce.contact_id
700 WHERE ce.on_hold = 0 AND cc.is_deceased = 0 AND cc.do_not_email = 0 AND {$queryString}
702 LIMIT {$offset}, {$rowCount}
705 // send query to hook to be modified if needed
706 CRM_Utils_Hook
::contactListQuery($query,
708 CRM_Utils_Array
::value('context', $_GET),
709 CRM_Utils_Array
::value('cid', $_GET)
712 $dao = CRM_Core_DAO
::executeQuery($query);
714 while ($dao->fetch()) {
717 'text' => '"' . $dao->name
. '" <' . $dao->email
. '>',
718 'id' => (CRM_Utils_Array
::value('id', $_GET)) ?
"{$dao->id}::{$dao->email}" : '"' . $dao->name
. '" <' . $dao->email
. '>',
723 echo json_encode($result);
727 CRM_Utils_System
::civiExit();
730 public static function getContactPhone() {
733 //check for mobile type
734 $phoneTypes = CRM_Core_OptionGroup
::values('phone_type', TRUE, FALSE, FALSE, NULL, 'name');
735 $mobileType = CRM_Utils_Array
::value('Mobile', $phoneTypes);
737 $name = CRM_Utils_Array
::value('name', $_GET);
739 $name = CRM_Utils_Type
::escape($name, 'String');
740 $queryString = " ( cc.sort_name LIKE '%$name%' OR cp.phone LIKE '%$name%' ) ";
743 $cid = CRM_Utils_Array
::value('cid', $_GET);
745 //check cid for interger
746 $contIDS = explode(',', $cid);
747 foreach ($contIDS as $contID) {
748 CRM_Utils_Type
::escape($contID, 'Integer');
750 $queryString = " cc.id IN ( $cid )";
755 $offset = CRM_Utils_Array
::value('offset', $_GET, 0);
756 $rowCount = CRM_Utils_Array
::value('rowcount', $_GET, 20);
758 $offset = CRM_Utils_Type
::escape($offset, 'Int');
759 $rowCount = CRM_Utils_Type
::escape($rowCount, 'Int');
761 // add acl clause here
762 list($aclFrom, $aclWhere) = CRM_Contact_BAO_Contact_Permission
::cacheClause('cc');
764 $aclWhere = " AND $aclWhere";
768 SELECT sort_name name, cp.phone, cc.id
769 FROM civicrm_phone cp INNER JOIN civicrm_contact cc ON cc.id = cp.contact_id
771 WHERE cc.is_deceased = 0 AND cc.do_not_sms = 0 AND cp.phone_type_id = {$mobileType} AND {$queryString}
773 LIMIT {$offset}, {$rowCount}
776 // send query to hook to be modified if needed
777 CRM_Utils_Hook
::contactListQuery($query,
779 CRM_Utils_Array
::value('context', $_GET),
780 CRM_Utils_Array
::value('cid', $_GET)
783 $dao = CRM_Core_DAO
::executeQuery($query);
785 while ($dao->fetch()) {
787 'text' => '"' . $dao->name
. '" (' . $dao->phone
. ')',
788 'id' => (CRM_Utils_Array
::value('id', $_GET)) ?
"{$dao->id}::{$dao->phone}" : '"' . $dao->name
. '" <' . $dao->phone
. '>',
794 echo json_encode($result);
796 CRM_Utils_System
::civiExit();
800 public static function buildSubTypes() {
801 $parent = CRM_Utils_Array
::value('parentId', $_REQUEST);
805 $contactType = 'Individual';
809 $contactType = 'Household';
813 $contactType = 'Organization';
817 $subTypes = CRM_Contact_BAO_ContactType
::subTypePairs($contactType, FALSE, NULL);
819 CRM_Utils_JSON
::output($subTypes);
822 public static function buildDedupeRules() {
823 $parent = CRM_Utils_Array
::value('parentId', $_REQUEST);
827 $contactType = 'Individual';
831 $contactType = 'Household';
835 $contactType = 'Organization';
839 $dedupeRules = CRM_Dedupe_BAO_RuleGroup
::getByType($contactType);
841 CRM_Utils_JSON
::output($dedupeRules);
845 * Function used for CiviCRM dashboard operations
847 public static function dashboard() {
848 $operation = CRM_Utils_Type
::escape($_REQUEST['op'], 'String');
850 switch ($operation) {
851 case 'get_widgets_by_column':
852 // This would normally be coming from either the database (this user's settings) or a default/initial dashboard configuration.
853 // get contact id of logged in user
855 $dashlets = CRM_Core_BAO_Dashboard
::getContactDashlets();
859 $dashletID = CRM_Utils_Type
::escape($_GET['id'], 'Positive');
861 $dashlets = CRM_Core_BAO_Dashboard
::getDashletInfo($dashletID);
865 CRM_Core_BAO_Dashboard
::saveDashletChanges($_REQUEST['columns']);
866 CRM_Utils_System
::civiExit();
867 case 'delete_dashlet':
868 $dashletID = CRM_Utils_Type
::escape($_REQUEST['dashlet_id'], 'Positive');
869 CRM_Core_BAO_Dashboard
::deleteDashlet($dashletID);
870 CRM_Utils_System
::civiExit();
873 CRM_Utils_JSON
::output($dashlets);
877 * Retrieve signature based on email id
879 public static function getSignature() {
880 $emailID = CRM_Utils_Type
::escape($_REQUEST['emailID'], 'Positive');
881 $query = "SELECT signature_text, signature_html FROM civicrm_email WHERE id = {$emailID}";
882 $dao = CRM_Core_DAO
::executeQuery($query);
884 $signatures = array();
885 while ($dao->fetch()) {
887 'signature_text' => $dao->signature_text
,
888 'signature_html' => $dao->signature_html
,
892 CRM_Utils_JSON
::output($signatures);
898 public static function processDupes() {
899 $oper = CRM_Utils_Type
::escape($_REQUEST['op'], 'String');
900 $cid = CRM_Utils_Type
::escape($_REQUEST['cid'], 'Positive');
901 $oid = CRM_Utils_Type
::escape($_REQUEST['oid'], 'Positive');
903 if (!$oper ||
!$cid ||
!$oid) {
907 $exception = new CRM_Dedupe_DAO_Exception();
908 $exception->contact_id1
= $cid;
909 $exception->contact_id2
= $oid;
910 //make sure contact2 > contact1.
912 $exception->contact_id1
= $oid;
913 $exception->contact_id2
= $cid;
915 $exception->find(TRUE);
917 if ($oper == 'dupe-nondupe') {
918 $status = $exception->save();
920 if ($oper == 'nondupe-dupe') {
921 $status = $exception->delete();
924 CRM_Utils_JSON
::output(array('status' => ($status) ?
$oper : $status));
927 public static function getDedupes() {
929 $sEcho = CRM_Utils_Type
::escape($_REQUEST['sEcho'], 'Integer');
930 $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type
::escape($_REQUEST['iDisplayStart'], 'Integer') : 0;
931 $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type
::escape($_REQUEST['iDisplayLength'], 'Integer') : 25;
933 $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type
::escape($_REQUEST['sSortDir_0'], 'String') : 'asc';
935 $gid = isset($_REQUEST['gid']) ? CRM_Utils_Type
::escape($_REQUEST['gid'], 'Integer') : 0;
936 $rgid = isset($_REQUEST['rgid']) ? CRM_Utils_Type
::escape($_REQUEST['rgid'], 'Integer') : 0;
939 $contactType = CRM_Core_DAO
::getFieldValue('CRM_Dedupe_DAO_RuleGroup', $rgid, 'contact_type');
942 $cacheKeyString = "merge {$contactType}_{$rgid}_{$gid}";
943 $searchRows = array();
944 $selectorElements = array('src', 'dst', 'weight', 'actions');
946 $join = "LEFT JOIN civicrm_dedupe_exception de ON ( pn.entity_id1 = de.contact_id1 AND
947 pn.entity_id2 = de.contact_id2 )";
948 $where = "de.id IS NULL";
950 $iFilteredTotal = $iTotal = CRM_Core_BAO_PrevNextCache
::getCount($cacheKeyString, $join, $where);
951 $mainContacts = CRM_Core_BAO_PrevNextCache
::retrieve($cacheKeyString, $join, $where, $offset, $rowCount);
953 foreach ($mainContacts as $mainId => $main) {
954 $searchRows[$mainId]['src'] = CRM_Utils_System
::href($main['srcName'], 'civicrm/contact/view', "reset=1&cid={$main['srcID']}");
955 $searchRows[$mainId]['dst'] = CRM_Utils_System
::href($main['dstName'], 'civicrm/contact/view', "reset=1&cid={$main['dstID']}");
956 $searchRows[$mainId]['weight'] = CRM_Utils_Array
::value('weight', $main);
958 if (!empty($main['canMerge'])) {
959 $mergeParams = "reset=1&cid={$main['srcID']}&oid={$main['dstID']}&action=update&rgid={$rgid}";
961 $mergeParams .= "&gid={$gid}";
964 $searchRows[$mainId]['actions'] = CRM_Utils_System
::href(ts('merge'), 'civicrm/contact/merge', $mergeParams);
965 $searchRows[$mainId]['actions'] .= " | <a id='notDuplicate' href='#' onClick=\"processDupes( {$main['srcID']}, {$main['dstID']}, 'dupe-nondupe', 'dupe-listing'); return false;\">" . ts('not a duplicate') . "</a>";
968 $searchRows[$mainId]['actions'] = '<em>' . ts('Insufficient access rights - cannot merge') . '</em>';
972 echo CRM_Utils_JSON
::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
974 CRM_Utils_System
::civiExit();
978 * Retrieve a PDF Page Format for the PDF Letter form
980 public function pdfFormat() {
981 $formatId = CRM_Utils_Type
::escape($_REQUEST['formatId'], 'Integer');
983 $pdfFormat = CRM_Core_BAO_PdfFormat
::getById($formatId);
985 CRM_Utils_JSON
::output($pdfFormat);
989 * Retrieve Paper Size dimensions
991 public static function paperSize() {
992 $paperSizeName = CRM_Utils_Type
::escape($_REQUEST['paperSizeName'], 'String');
994 $paperSize = CRM_Core_BAO_PaperSize
::getByName($paperSizeName);
996 CRM_Utils_JSON
::output($paperSize);
999 public static function selectUnselectContacts() {
1000 $name = CRM_Utils_Array
::value('name', $_REQUEST);
1001 $cacheKey = CRM_Utils_Array
::value('qfKey', $_REQUEST);
1002 $state = CRM_Utils_Array
::value('state', $_REQUEST, 'checked');
1003 $variableType = CRM_Utils_Array
::value('variableType', $_REQUEST, 'single');
1005 $actionToPerform = CRM_Utils_Array
::value('action', $_REQUEST, 'select');
1007 if ($variableType == 'multiple') {
1008 // action post value only works with multiple type variable
1010 //multiple names like mark_x_1-mark_x_2 where 1,2 are cids
1011 $elements = explode('-', $name);
1012 foreach ($elements as $key => $element) {
1013 $elements[$key] = self
::_convertToId($element);
1015 CRM_Core_BAO_PrevNextCache
::markSelection($cacheKey, $actionToPerform, $elements);
1018 CRM_Core_BAO_PrevNextCache
::markSelection($cacheKey, $actionToPerform);
1021 elseif ($variableType == 'single') {
1022 $cId = self
::_convertToId($name);
1023 $action = ($state == 'checked') ?
'select' : 'unselect';
1024 CRM_Core_BAO_PrevNextCache
::markSelection($cacheKey, $action, $cId);
1026 $contactIds = CRM_Core_BAO_PrevNextCache
::getSelection($cacheKey);
1027 $countSelectionCids = count($contactIds[$cacheKey]);
1029 $arrRet = array('getCount' => $countSelectionCids);
1030 CRM_Utils_JSON
::output($arrRet);
1034 * @param string $name
1038 public static function _convertToId($name) {
1039 if (substr($name, 0, CRM_Core_Form
::CB_PREFIX_LEN
) == CRM_Core_Form
::CB_PREFIX
) {
1040 $cId = substr($name, CRM_Core_Form
::CB_PREFIX_LEN
);
1045 public static function getAddressDisplay() {
1046 $contactId = CRM_Utils_Array
::value('contact_id', $_REQUEST);
1048 $addressVal["error_message"] = "no contact id found";
1053 'contact_id' => $contactId,
1054 'entity_id' => $contactId,
1056 $addressVal = CRM_Core_BAO_Address
::getValues($entityBlock);
1059 CRM_Utils_JSON
::output($addressVal);
1063 * Retrieve contact relationships
1065 public static function getContactRelationships() {
1066 $contactID = CRM_Utils_Type
::escape($_GET['cid'], 'Integer');
1067 $context = CRM_Utils_Type
::escape($_GET['context'], 'String');
1069 $sortMapper = array(
1083 $sEcho = CRM_Utils_Type
::escape($_REQUEST['sEcho'], 'Integer');
1084 $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type
::escape($_REQUEST['iDisplayStart'], 'Integer') : 0;
1085 $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type
::escape($_REQUEST['iDisplayLength'], 'Integer') : 25;
1086 $sort = isset($_REQUEST['iSortCol_0']) ? CRM_Utils_Array
::value(CRM_Utils_Type
::escape($_REQUEST['iSortCol_0'], 'Integer'), $sortMapper) : NULL;
1087 $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type
::escape($_REQUEST['sSortDir_0'], 'String') : 'asc';
1090 if ($sort && $sortOrder) {
1091 $params['sortBy'] = $sort . ' ' . $sortOrder;
1094 $params['page'] = ($offset / $rowCount) +
1;
1095 $params['rp'] = $rowCount;
1097 $params['contact_id'] = $contactID;
1098 $params['context'] = $context;
1100 // get the contact relationships
1101 $relationships = CRM_Contact_BAO_Relationship
::getContactRelationshipSelector($params);
1103 $iFilteredTotal = $iTotal = $params['total'];
1104 $selectorElements = array(
1118 echo CRM_Utils_JSON
::encodeDataTableSelector($relationships, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
1119 CRM_Utils_System
::civiExit();