3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
36 * This class contains all contact related functions that are called using AJAX (jQuery)
38 class CRM_Contact_Page_AJAX
{
39 static function getContactList() {
40 // if context is 'customfield'
41 if (CRM_Utils_Array
::value('context', $_GET) == 'customfield') {
42 return self
::contactReference();
45 $params = array('version' => 3, 'check_permissions' => TRUE);
48 // FIXME: param keys don't match input keys, using this array to translate
51 'fieldName' => 'field_name',
52 'tableName' => 'table_name',
53 'context' => 'context',
55 foreach ($whitelist as $key => $param) {
56 if (!empty($_GET[$key])) {
57 $params[$param] = $_GET[$key];
61 //CRM-10687: Allow quicksearch by multiple fields
62 if (!empty($params['field_name'])) {
63 if ($params['field_name'] == 'phone_numeric') {
64 $params['name'] = preg_replace('/[^\d]/', '', $params['name']);
66 if (!$params['name']) {
67 CRM_Utils_System
::civiExit();
80 foreach ($whitelist as $key) {
81 if (!empty($_GET[$key]) && is_numeric($_GET[$key])) {
82 $params[$key] = $_GET[$key];
86 $result = civicrm_api('Contact', 'getquick', $params);
87 if (empty($result['is_error']) && !empty($result['values'])) {
88 foreach ($result['values'] as $key => $val) {
89 echo "{$val['data']}|{$val['id']}\n";
92 CRM_Utils_System
::civiExit();
95 static function contactReference() {
96 $name = CRM_Utils_Array
::value('s', $_GET);
97 $name = CRM_Utils_Type
::escape($name, 'String');
98 $cfID = CRM_Utils_Type
::escape($_GET['id'], 'Positive');
100 // check that this is a valid, active custom field of Contact Reference type
101 $params = array('id' => $cfID);
102 $returnProperties = array('filter', 'data_type', 'is_active');
103 $fldValues = array();
104 CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_CustomField', $params, $cf, $returnProperties);
105 if (!$cf['id'] ||
!$cf['is_active'] ||
$cf['data_type'] = !'ContactReference') {
106 echo "$name|error\n";
107 CRM_Utils_System
::civiExit();
111 $filterParams = array();
112 parse_str($cf['filter'], $filterParams);
114 $action = CRM_Utils_Array
::value('action', $filterParams);
116 if (!empty($action) &&
117 !in_array($action, array('get', 'lookup'))
119 echo "$name|error\n";
120 CRM_Utils_System
::civiExit();
124 $list = array_keys(CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
125 'contact_reference_options'
128 $return = array_unique(array_merge(array('sort_name'), $list));
130 $config = CRM_Core_Config
::singleton();
133 if (CRM_Utils_Array
::value('limit', $_GET)) {
134 $limit = CRM_Utils_Type
::escape($_GET['limit'], 'Positive');
137 $params = array('offset' => 0, 'rowCount' => $limit, 'version' => 3);
138 foreach ($return as $fld) {
139 $params["return.{$fld}"] = 1;
142 if (!empty($action)) {
143 $excludeGet = array('reset', 'key', 'className', 'fnName', 'json', 'reset', 'context', 'timestamp', 'limit', 'id', 's', 'q', 'action');
144 foreach ($_GET as $param => $val) {
146 in_array($param, $excludeGet) ||
147 strpos($param, 'return.') !== FALSE ||
148 strpos($param, 'api.') !== FALSE
152 $params[$param] = $val;
157 $params['sort_name'] = $name;
160 $params['sort'] = 'sort_name';
162 // tell api to skip permission chk. dgg
163 $params['check_permissions'] = 0;
165 // add filter variable to params
166 if (!empty($filterParams)) {
167 $params = array_merge($params, $filterParams);
170 $contact = civicrm_api('Contact', 'Get', $params);
172 if (CRM_Utils_Array
::value('is_error', $contact)) {
173 echo "$name|error\n";
174 CRM_Utils_System
::civiExit();
178 foreach ($contact['values'] as $value) {
180 foreach ($return as $fld) {
181 if (CRM_Utils_Array
::value($fld, $value)) {
182 $view[] = $value[$fld];
185 echo $contactList = implode(' :: ', $view) . "|" . $value['id'] . "\n";
189 echo "$name|$name\n";
192 CRM_Utils_System
::civiExit();
196 * Function to fetch PCP ID by PCP Supporter sort_name, also displays PCP title and associated Contribution Page title
198 static function getPCPList() {
199 $name = CRM_Utils_Array
::value('s', $_GET);
200 $name = CRM_Utils_Type
::escape($name, 'String');
203 $where = ' AND pcp.page_id = cp.id AND pcp.contact_id = cc.id';
205 $config = CRM_Core_Config
::singleton();
206 if ($config->includeWildCardInName
) {
207 $strSearch = "%$name%";
210 $strSearch = "$name%";
212 $includeEmailFrom = $includeNickName = '';
213 if ($config->includeNickNameInName
) {
214 $includeNickName = " OR nick_name LIKE '$strSearch'";
216 if ($config->includeEmailInName
) {
217 $includeEmailFrom = "LEFT JOIN civicrm_email eml ON ( cc.id = eml.contact_id AND eml.is_primary = 1 )";
218 $whereClause = " WHERE ( email LIKE '$strSearch' OR sort_name LIKE '$strSearch' $includeNickName ) {$where} ";
221 $whereClause = " WHERE ( sort_name LIKE '$strSearch' $includeNickName ) {$where} ";
224 if (CRM_Utils_Array
::value('limit', $_GET)) {
225 $limit = CRM_Utils_Type
::escape($_GET['limit'], 'Positive');
228 $select = 'cc.sort_name, pcp.title, cp.title';
232 SELECT pcp.id as id, CONCAT_WS( ' :: ', {$select} ) as data, sort_name
233 FROM civicrm_pcp pcp, civicrm_contribution_page cp, civicrm_contact cc
241 $dao = CRM_Core_DAO
::executeQuery($query);
243 while ($dao->fetch()) {
244 echo $pcpList = "$dao->data|$dao->id\n";
247 CRM_Utils_System
::civiExit();
251 * Function to fetch the values
253 static function autocomplete() {
254 $fieldID = CRM_Utils_Type
::escape($_GET['cfid'], 'Integer');
255 $optionGroupID = CRM_Utils_Type
::escape($_GET['ogid'], 'Integer');
256 $label = CRM_Utils_Type
::escape($_GET['s'], 'String');
258 $selectOption = CRM_Core_BAO_CustomOption
::valuesByID($fieldID, $optionGroupID);
260 $completeList = NULL;
261 foreach ($selectOption as $id => $value) {
262 if (strtolower($label) == strtolower(substr($value, 0, strlen($label)))) {
263 echo $completeList = "$value|$id\n";
266 CRM_Utils_System
::civiExit();
269 static function relationship() {
270 $relType = CRM_Utils_Array
::value('rel_type', $_REQUEST);
271 $relContactID = CRM_Utils_Array
::value('rel_contact', $_REQUEST);
272 $sourceContactID = CRM_Utils_Array
::value('contact_id', $_REQUEST); // we no longer need this.
273 $relationshipID = CRM_Utils_Array
::value('rel_id', $_REQUEST); // this used only to determine add or update mode
274 $caseID = CRM_Utils_Array
::value('case_id', $_REQUEST);
276 // check if there are multiple clients for this case, if so then we need create
277 // relationship and also activities for each contacts
279 // get case client list
280 $clientList = CRM_Case_BAO_Case
::getCaseClients($caseID);
282 foreach($clientList as $sourceContactID) {
283 $relationParams = array(
284 'relationship_type_id' => $relType . '_a_b',
285 'contact_check' => array($relContactID => 1),
287 'case_id' => $caseID,
288 'start_date' => date("Ymd"),
291 $relationIds = array('contact' => $sourceContactID);
293 // check if we are editing/updating existing relationship
294 if ($relationshipID && $relationshipID != 'null') {
295 // here we need to retrieve appropriate relationshipID based on client id and relationship type id
296 $caseRelationships = new CRM_Contact_DAO_Relationship();
297 $caseRelationships->case_id
= $caseID;
298 $caseRelationships->relationship_type_id
= $relType;
299 $caseRelationships->contact_id_a
= $sourceContactID;
300 $caseRelationships->find();
302 while($caseRelationships->fetch()) {
303 $relationIds['relationship'] = $caseRelationships->id
;
304 $relationIds['contactTarget'] = $relContactID;
306 $caseRelationships->free();
309 // create new or update existing relationship
310 $return = CRM_Contact_BAO_Relationship
::create($relationParams, $relationIds);
312 $status = 'process-relationship-fail';
313 if (CRM_Utils_Array
::value(0, $return[4])) {
314 $relationshipID = $return[4][0];
315 $status = 'process-relationship-success';
317 //create an activity for case role assignment.CRM-4480
318 CRM_Case_BAO_Case
::createCaseRoleActivity($caseID, $relationshipID, $relContactID);
322 $relation['status'] = $status;
323 echo json_encode($relation);
324 CRM_Utils_System
::civiExit();
328 * Function to fetch the custom field help
330 static function customField() {
331 $fieldId = CRM_Utils_Type
::escape($_REQUEST['id'], 'Integer');
332 $params = array('id' => $fieldId);
333 $returnProperties = array('help_pre', 'help_post');
336 CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_CustomField', $params, $values, $returnProperties);
337 echo json_encode($values);
338 CRM_Utils_System
::civiExit();
342 * Function to obtain list of permissioned employer for the given contact-id.
344 static function getPermissionedEmployer() {
345 $cid = CRM_Utils_Type
::escape($_GET['cid'], 'Integer');
346 $name = trim(CRM_Utils_Type
::escape($_GET['s'], 'String'));
347 $name = str_replace('*', '%', $name);
349 $elements = CRM_Contact_BAO_Relationship
::getPermissionedEmployer($cid, $name);
351 if (!empty($elements)) {
352 foreach ($elements as $cid => $name) {
353 echo $element = $name['name'] . "|$cid\n";
356 CRM_Utils_System
::civiExit();
360 static function groupTree() {
361 $gids = CRM_Utils_Type
::escape($_GET['gids'], 'String');
362 echo CRM_Contact_BAO_GroupNestingCache
::json($gids);
363 CRM_Utils_System
::civiExit();
367 * Function for building contact combo box
369 static function search() {
371 $name = CRM_Utils_Array
::value('name', $_GET, '');
372 if (!array_key_exists('name', $_GET)) {
373 $name = CRM_Utils_Array
::value('s', $_GET) . '%';
376 $name = CRM_Utils_Type
::escape($name, 'String');
378 if (CRM_Utils_Array
::value('id', $_GET)) {
380 if (is_numeric($_GET['id'])) {
381 $id = CRM_Utils_Type
::escape($_GET['id'], 'Integer');
382 $whereIdClause = " AND civicrm_contact.id = {$id}";
390 if ($name ||
isset($id)) {
393 //contact's based of relationhip type
395 if (isset($_GET['rel'])) {
396 $relation = explode('_', $_GET['rel']);
397 $relType = CRM_Utils_Type
::escape($relation[0], 'Integer');
398 $rel = CRM_Utils_Type
::escape($relation[2], 'String');
401 //shared household info
403 if (isset($_GET['sh'])) {
404 $shared = CRM_Utils_Type
::escape($_GET['sh'], 'Integer');
406 $contactType = 'Household';
407 $cName = 'household_name';
410 $contactType = 'Organization';
411 $cName = 'organization_name';
415 // contacts of type household
416 $hh = $addStreet = $addCity = NULL;
417 if (isset($_GET['hh'])) {
418 $hh = CRM_Utils_Type
::escape($_GET['hh'], 'Integer');
422 $organization = $street = $city = NULL;
423 if (isset($_GET['org'])) {
424 $organization = CRM_Utils_Type
::escape($_GET['org'], 'Integer');
427 if (isset($_GET['org']) ||
isset($_GET['hh'])) {
429 if ($splitName = explode(' :: ', $name)) {
430 $contactName = trim(CRM_Utils_Array
::value('0', $splitName));
431 $street = trim(CRM_Utils_Array
::value('1', $splitName));
432 $city = trim(CRM_Utils_Array
::value('2', $splitName));
435 $contactName = $name;
439 $addStreet = "AND civicrm_address.street_address LIKE '$street%'";
442 $addCity = "AND civicrm_address.city LIKE '$city%'";
449 SELECT CONCAT_WS(' :: ',sort_name,LEFT(street_address,25),city) 'sort_name',
450 civicrm_contact.id 'id'
452 LEFT JOIN civicrm_address ON ( civicrm_contact.id = civicrm_address.contact_id
453 AND civicrm_address.is_primary=1
455 WHERE civicrm_contact.contact_type='Organization' AND organization_name LIKE '%$contactName%'
456 {$addStreet} {$addCity} {$whereIdClause}
457 ORDER BY organization_name ";
461 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} ";
465 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'
467 LEFT JOIN civicrm_address ON (civicrm_contact.id =civicrm_address.contact_id AND civicrm_address.is_primary =1 )
468 WHERE civicrm_contact.contact_type ='Household'
469 AND household_name LIKE '%$contactName%' {$addStreet} {$addCity} {$whereIdClause} ORDER BY household_name ";
472 if (CRM_Utils_Array
::value('case', $_GET)) {
474 SELECT distinct(c.id), c.sort_name
475 FROM civicrm_contact c
476 LEFT JOIN civicrm_relationship ON civicrm_relationship.contact_id_{$rel} = c.id
477 WHERE c.sort_name LIKE '%$name%'
478 AND civicrm_relationship.relationship_type_id = $relType
488 WHERE sort_name LIKE '%$name'
490 ORDER BY sort_name ";
494 if (isset($_GET['limit'])) {
495 $limit = CRM_Utils_Type
::escape($_GET['limit'], 'Positive');
498 $query .= " LIMIT 0,{$limit}";
500 $dao = CRM_Core_DAO
::executeQuery($query);
503 while ($dao->fetch()) {
504 echo $dao->sort_name
;
505 CRM_Utils_System
::civiExit();
509 while ($dao->fetch()) {
511 $elements[] = array('name' => addslashes($dao->sort_name
),
516 echo $elements = "$dao->sort_name|$dao->id|$dao->location_type_id|$dao->is_primary|$dao->is_billing\n";
519 //for adding new household address / organization
520 if (empty($elements) && !$json && ($hh ||
$organization)) {
521 echo CRM_Utils_Array
::value('s', $_GET);
526 if (isset($_GET['sh'])) {
528 CRM_Utils_System
::civiExit();
531 if (empty($elements)) {
532 $name = str_replace('%', '', $name);
540 echo json_encode($elements);
542 CRM_Utils_System
::civiExit();
547 * Function to check how many contact exits in db for given criteria,
548 * if one then return contact id else null
550 static function contact() {
551 $name = CRM_Utils_Type
::escape($_GET['name'], 'String');
556 WHERE sort_name LIKE '%$name%'";
558 $dao = CRM_Core_DAO
::executeQuery($query);
564 CRM_Utils_System
::civiExit();
568 * Function to delete custom value
571 static function deleteCustomValue() {
572 $customValueID = CRM_Utils_Type
::escape($_REQUEST['valueID'], 'Positive');
573 $customGroupID = CRM_Utils_Type
::escape($_REQUEST['groupID'], 'Positive');
575 CRM_Core_BAO_CustomValue
::deleteCustomValue($customValueID, $customGroupID);
576 if ($contactId = CRM_Utils_Array
::value('contactId', $_REQUEST)) {
577 echo CRM_Contact_BAO_Contact
::getCountComponent('custom_' . $_REQUEST['groupID'], $contactId);
580 // reset the group contact cache for this group
581 CRM_Contact_BAO_GroupContactCache
::remove();
582 CRM_Utils_System
::civiExit();
586 * Function to perform enable / disable actions on record.
589 static function enableDisable() {
590 $op = CRM_Utils_Type
::escape($_REQUEST['op'], 'String');
591 $recordID = CRM_Utils_Type
::escape($_REQUEST['recordID'], 'Positive');
592 $recordBAO = CRM_Utils_Type
::escape($_REQUEST['recordBAO'], 'String');
595 if ($op == 'disable-enable') {
598 elseif ($op == 'enable-disable') {
601 $status = array('status' => 'record-updated-fail');
602 if (isset($isActive)) {
603 // first munge and clean the recordBAO and get rid of any non alpha numeric characters
604 $recordBAO = CRM_Utils_String
::munge($recordBAO);
605 $recordClass = explode('_', $recordBAO);
607 // make sure recordClass is namespaced (we cant check CRM since extensions can also use this)
608 // but it should be at least 3 levels deep
609 if (count($recordClass) >= 3) {
610 require_once (str_replace('_', DIRECTORY_SEPARATOR
, $recordBAO) . ".php");
611 $method = 'setIsActive';
613 if (method_exists($recordBAO, $method)) {
614 $updated = call_user_func_array(array($recordBAO, $method),
615 array($recordID, $isActive)
618 $status = array('status' => 'record-updated-success');
621 // call hook enableDisable
622 CRM_Utils_Hook
::enableDisable($recordBAO, $recordID, $isActive);
625 echo json_encode($status);
626 CRM_Utils_System
::civiExit();
631 *Function to check the CMS username
634 static public function checkUserName() {
635 $config = CRM_Core_Config
::singleton();
636 $username = trim($_REQUEST['cms_name']);
638 $params = array('name' => $username);
641 $config->userSystem
->checkUserNameEmailExists($params, $errors);
643 if (isset($errors['cms_name']) ||
isset($errors['name'])) {
644 //user name is not availble
645 $user = array('name' => 'no');
646 echo json_encode($user);
649 //user name is available
650 $user = array('name' => 'yes');
651 echo json_encode($user);
653 CRM_Utils_System
::civiExit();
657 * Function to get email address of a contact
659 static function getContactEmail() {
660 if (CRM_Utils_Array
::value('contact_id', $_REQUEST)) {
661 $contactID = CRM_Utils_Type
::escape($_REQUEST['contact_id'], 'Positive');
664 ) = CRM_Contact_BAO_Contact_Location
::getEmailDetails($contactID);
670 $noemail = CRM_Utils_Array
::value('noemail', $_GET);
672 if ($name = CRM_Utils_Array
::value('name', $_GET)) {
673 $name = CRM_Utils_Type
::escape($name, 'String');
675 $queryString = " cc.sort_name LIKE '%$name%'";
678 $queryString = " ( cc.sort_name LIKE '%$name%' OR ce.email LIKE '%$name%' ) ";
681 elseif ($cid = CRM_Utils_Array
::value('cid', $_GET)) {
682 //check cid for interger
683 $contIDS = explode(',', $cid);
684 foreach ($contIDS as $contID) {
685 CRM_Utils_Type
::escape($contID, 'Integer');
687 $queryString = " cc.id IN ( $cid )";
691 $offset = CRM_Utils_Array
::value('offset', $_GET, 0);
692 $rowCount = CRM_Utils_Array
::value('rowcount', $_GET, 20);
694 // add acl clause here
695 list($aclFrom, $aclWhere) = CRM_Contact_BAO_Contact_Permission
::cacheClause('cc');
697 $aclWhere = " AND $aclWhere";
701 SELECT sort_name name, cc.id
702 FROM civicrm_contact cc
704 WHERE cc.is_deceased = 0 AND {$queryString}
706 LIMIT {$offset}, {$rowCount}
709 // send query to hook to be modified if needed
710 CRM_Utils_Hook
::contactListQuery($query,
712 CRM_Utils_Array
::value('context', $_GET),
713 CRM_Utils_Array
::value('cid', $_GET)
716 $dao = CRM_Core_DAO
::executeQuery($query);
717 while ($dao->fetch()) {
719 'name' => $dao->name
,
726 SELECT sort_name name, ce.email, cc.id
727 FROM civicrm_email ce INNER JOIN civicrm_contact cc ON cc.id = ce.contact_id
729 WHERE ce.on_hold = 0 AND cc.is_deceased = 0 AND cc.do_not_email = 0 AND {$queryString}
731 LIMIT {$offset}, {$rowCount}
734 // send query to hook to be modified if needed
735 CRM_Utils_Hook
::contactListQuery($query,
737 CRM_Utils_Array
::value('context', $_GET),
738 CRM_Utils_Array
::value('cid', $_GET)
742 $dao = CRM_Core_DAO
::executeQuery($query);
744 while ($dao->fetch()) {
746 'name' => '"' . $dao->name
. '" <' . $dao->email
. '>',
747 'id' => (CRM_Utils_Array
::value('id', $_GET)) ?
"{$dao->id}::{$dao->email}" : '"' . $dao->name
. '" <' . $dao->email
. '>',
753 echo json_encode($result);
757 CRM_Utils_System
::civiExit();
760 static function getContactPhone() {
763 //check for mobile type
764 $phoneTypes = CRM_Core_OptionGroup
::values('phone_type', TRUE, FALSE, FALSE, NULL, 'name');
765 $mobileType = CRM_Utils_Array
::value('Mobile', $phoneTypes);
767 if ($name = CRM_Utils_Array
::value('name', $_GET)) {
768 $name = CRM_Utils_Type
::escape($name, 'String');
769 $queryString = " ( cc.sort_name LIKE '%$name%' OR cp.phone LIKE '%$name%' ) ";
771 elseif ($cid = CRM_Utils_Array
::value('cid', $_GET)) {
772 //check cid for interger
773 $contIDS = explode(',', $cid);
774 foreach ($contIDS as $contID) {
775 CRM_Utils_Type
::escape($contID, 'Integer');
777 $queryString = " cc.id IN ( $cid )";
781 $offset = CRM_Utils_Array
::value('offset', $_GET, 0);
782 $rowCount = CRM_Utils_Array
::value('rowcount', $_GET, 20);
784 // add acl clause here
785 list($aclFrom, $aclWhere) = CRM_Contact_BAO_Contact_Permission
::cacheClause('cc');
787 $aclWhere = " AND $aclWhere";
791 SELECT sort_name name, cp.phone, cc.id
792 FROM civicrm_phone cp INNER JOIN civicrm_contact cc ON cc.id = cp.contact_id
794 WHERE cc.is_deceased = 0 AND cc.do_not_sms = 0 AND cp.phone_type_id = {$mobileType} AND {$queryString}
796 LIMIT {$offset}, {$rowCount}
799 // send query to hook to be modified if needed
800 CRM_Utils_Hook
::contactListQuery($query,
802 CRM_Utils_Array
::value('context', $_GET),
803 CRM_Utils_Array
::value('cid', $_GET)
806 $dao = CRM_Core_DAO
::executeQuery($query);
808 while ($dao->fetch()) {
810 'name' => '"' . $dao->name
. '" <' . $dao->phone
. '>',
811 'id' => (CRM_Utils_Array
::value('id', $_GET)) ?
"{$dao->id}::{$dao->phone}" : '"' . $dao->name
. '" <' . $dao->phone
. '>',
817 echo json_encode($result);
819 CRM_Utils_System
::civiExit();
823 static function buildSubTypes() {
824 $parent = CRM_Utils_Array
::value('parentId', $_REQUEST);
828 $contactType = 'Individual';
832 $contactType = 'Household';
836 $contactType = 'Organization';
840 $subTypes = CRM_Contact_BAO_ContactType
::subTypePairs($contactType, FALSE, NULL);
842 echo json_encode($subTypes);
843 CRM_Utils_System
::civiExit();
846 static function buildDedupeRules() {
847 $parent = CRM_Utils_Array
::value('parentId', $_REQUEST);
851 $contactType = 'Individual';
855 $contactType = 'Household';
859 $contactType = 'Organization';
863 $dedupeRules = CRM_Dedupe_BAO_RuleGroup
::getByType($contactType);
865 echo json_encode($dedupeRules);
866 CRM_Utils_System
::civiExit();
870 * Function used for CiviCRM dashboard operations
872 static function dashboard() {
873 $operation = CRM_Utils_Type
::escape($_REQUEST['op'], 'String');
875 switch ($operation) {
876 case 'get_widgets_by_column':
877 // This would normally be coming from either the database (this user's settings) or a default/initial dashboard configuration.
878 // get contact id of logged in user
880 $dashlets = CRM_Core_BAO_Dashboard
::getContactDashlets();
884 $dashletID = CRM_Utils_Type
::escape($_GET['id'], 'Positive');
886 $dashlets = CRM_Core_BAO_Dashboard
::getDashletInfo($dashletID);
890 CRM_Core_BAO_Dashboard
::saveDashletChanges($_REQUEST['columns']);
891 CRM_Utils_System
::civiExit();
892 case 'delete_dashlet':
893 $dashletID = CRM_Utils_Type
::escape($_REQUEST['dashlet_id'], 'Positive');
894 CRM_Core_BAO_Dashboard
::deleteDashlet($dashletID);
895 CRM_Utils_System
::civiExit();
898 echo json_encode($dashlets);
899 CRM_Utils_System
::civiExit();
903 * Function to retrieve signature based on email id
905 static function getSignature() {
906 $emailID = CRM_Utils_Type
::escape($_REQUEST['emailID'], 'Positive');
907 $query = "SELECT signature_text, signature_html FROM civicrm_email WHERE id = {$emailID}";
908 $dao = CRM_Core_DAO
::executeQuery($query);
910 $signatures = array();
911 while ($dao->fetch()) {
913 'signature_text' => $dao->signature_text
,
914 'signature_html' => $dao->signature_html
,
918 echo json_encode($signatures);
919 CRM_Utils_System
::civiExit();
922 static function relationshipContacts() {
923 $searchValues = $searchRows = array();
926 $relType = CRM_Utils_Type
::escape($_REQUEST['relType'], 'String');
927 $typeName = isset($_REQUEST['typeName']) ? CRM_Utils_Type
::escape($_REQUEST['typeName'], 'String') : '';
928 $relContact = CRM_Utils_Type
::escape($_REQUEST['relContact'], 'String');
929 $currentContactId = CRM_Utils_Type
::escape($_REQUEST['cid'], 'Integer');
931 if (in_array($typeName, array(
932 'Employee of', 'Employer of'))) {
937 1 => 'sort_name', (2 +
$addCount) => 'city', (3 +
$addCount) => 'state_province',
938 (4 +
$addCount) => 'email', (5 +
$addCount) => 'phone',
941 $sEcho = CRM_Utils_Type
::escape($_REQUEST['sEcho'], 'Integer');
942 $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type
::escape($_REQUEST['iDisplayStart'], 'Integer') : 0;
943 $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type
::escape($_REQUEST['iDisplayLength'], 'Integer') : 25;
944 $sort = isset($_REQUEST['iSortCol_0']) ?
$sortMapper[CRM_Utils_Type
::escape($_REQUEST['iSortCol_0'], 'Integer')] : 'sort_name';
945 $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type
::escape($_REQUEST['sSortDir_0'], 'String') : 'asc';
947 $searchValues[] = array('sort_name', 'LIKE', $relContact, 0, 1);
949 list($rid, $direction) = explode('_', $relType, 2);
951 $relationshipType = new CRM_Contact_DAO_RelationshipType();
953 $relationshipType->id
= $rid;
954 if ($relationshipType->find(TRUE)) {
955 if ($direction == 'a_b') {
956 $type = $relationshipType->contact_type_b
;
957 $subType = $relationshipType->contact_sub_type_b
;
960 $type = $relationshipType->contact_type_a
;
961 $subType = $relationshipType->contact_sub_type_a
;
964 if ($type == 'Individual' ||
$type == 'Organization' ||
$type == 'Household') {
965 $searchValues[] = array('contact_type', '=', $type, 0, 0);
969 $searchValues[] = array('contact_sub_type', '=', $subType, 0, 0);
973 // exclude current contact
974 $searchValues[] = array('contact_id', '!=', $currentContactId, 0, 0);
976 $query = new CRM_Contact_BAO_Query($searchValues);
977 $searchCount = $query->searchQuery(0, 0, NULL, TRUE);
978 $iTotal = $searchCount;
980 if ($searchCount > 0) {
981 // get the result of the search
982 $result = $query->searchQuery($offset, $rowCount, $sort, FALSE, FALSE,
983 FALSE, FALSE, FALSE, NULL, $sortOrder
986 $config = CRM_Core_Config
::singleton();
988 while ($result->fetch()) {
989 $contactID = $result->contact_id
;
990 $typeImage = CRM_Contact_BAO_Contact_Utils
::getImage($result->contact_sub_type ?
991 $result->contact_sub_type
: $result->contact_type
,
995 $searchRows[$contactID]['id'] = $contactID;
996 $searchRows[$contactID]['name'] = $typeImage . ' ' . $result->sort_name
;
997 $searchRows[$contactID]['city'] = $result->city
;
998 $searchRows[$contactID]['state'] = $result->state_province
;
999 $searchRows[$contactID]['email'] = $result->email
;
1000 $searchRows[$contactID]['phone'] = $result->phone
;
1004 foreach ($searchRows as $cid => $row) {
1005 $searchRows[$cid]['check'] = '<input type="checkbox" id="contact_check[' . $cid . ']" name="contact_check[' . $cid . ']" value=' . $cid . ' />';
1007 if ($typeName == 'Employee of') {
1008 $searchRows[$cid]['employee_of'] = '<input type="radio" name="employee_of" value=' . $cid . ' >';
1010 elseif ($typeName == 'Employer of') {
1011 $searchRows[$cid]['employer_of'] = '<input type="checkbox" name="employer_of[' . $cid . ']" value=' . $cid . ' />';
1015 $selectorElements = array('check', 'name');
1016 if ($typeName == 'Employee of') {
1017 $selectorElements[] = 'employee_of';
1019 elseif ($typeName == 'Employer of') {
1020 $selectorElements[] = 'employer_of';
1022 $selectorElements = array_merge($selectorElements, array('city', 'state', 'email', 'phone'));
1024 $iFilteredTotal = $iTotal;
1025 echo CRM_Utils_JSON
::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
1026 CRM_Utils_System
::civiExit();
1030 * Function to process dupes.
1033 static function processDupes() {
1034 $oper = CRM_Utils_Type
::escape($_REQUEST['op'], 'String');
1035 $cid = CRM_Utils_Type
::escape($_REQUEST['cid'], 'Positive');
1036 $oid = CRM_Utils_Type
::escape($_REQUEST['oid'], 'Positive');
1038 if (!$oper ||
!$cid ||
!$oid) {
1042 $exception = new CRM_Dedupe_DAO_Exception();
1043 $exception->contact_id1
= $cid;
1044 $exception->contact_id2
= $oid;
1045 //make sure contact2 > contact1.
1047 $exception->contact_id1
= $oid;
1048 $exception->contact_id2
= $cid;
1050 $exception->find(TRUE);
1052 if ($oper == 'dupe-nondupe') {
1053 $status = $exception->save();
1055 if ($oper == 'nondupe-dupe') {
1056 $status = $exception->delete();
1059 echo json_encode(array('status' => ($status) ?
$oper : $status));
1060 CRM_Utils_System
::civiExit();
1063 static function getDedupes() {
1065 $sEcho = CRM_Utils_Type
::escape($_REQUEST['sEcho'], 'Integer');
1066 $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type
::escape($_REQUEST['iDisplayStart'], 'Integer') : 0;
1067 $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type
::escape($_REQUEST['iDisplayLength'], 'Integer') : 25;
1068 $sort = isset($_REQUEST['iSortCol_0']) ?
$sortMapper[CRM_Utils_Type
::escape($_REQUEST['iSortCol_0'], 'Integer')] : 'sort_name';
1069 $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type
::escape($_REQUEST['sSortDir_0'], 'String') : 'asc';
1071 $gid = isset($_REQUEST['gid']) ? CRM_Utils_Type
::escape($_REQUEST['gid'], 'Integer') : 0;
1072 $rgid = isset($_REQUEST['rgid']) ? CRM_Utils_Type
::escape($_REQUEST['rgid'], 'Integer') : 0;
1075 $contactType = CRM_Core_DAO
::getFieldValue('CRM_Dedupe_DAO_RuleGroup', $rgid, 'contact_type');
1078 $cacheKeyString = "merge {$contactType}_{$rgid}_{$gid}";
1079 $searchRows = array();
1080 $selectorElements = array('src', 'dst', 'weight', 'actions');
1083 $join = "LEFT JOIN civicrm_dedupe_exception de ON ( pn.entity_id1 = de.contact_id1 AND
1084 pn.entity_id2 = de.contact_id2 )";
1085 $where = "de.id IS NULL";
1087 $iFilteredTotal = $iTotal = CRM_Core_BAO_PrevNextCache
::getCount($cacheKeyString, $join, $where);
1088 $mainContacts = CRM_Core_BAO_PrevNextCache
::retrieve($cacheKeyString, $join, $where, $offset, $rowCount);
1090 foreach ($mainContacts as $mainId => $main) {
1091 $searchRows[$mainId]['src'] = CRM_Utils_System
::href($main['srcName'], 'civicrm/contact/view', "reset=1&cid={$main['srcID']}");
1092 $searchRows[$mainId]['dst'] = CRM_Utils_System
::href($main['dstName'], 'civicrm/contact/view', "reset=1&cid={$main['dstID']}");
1093 $searchRows[$mainId]['weight'] = CRM_Utils_Array
::value('weight', $main);
1095 if (CRM_Utils_Array
::value('canMerge', $main)) {
1096 $mergeParams = "reset=1&cid={$main['srcID']}&oid={$main['dstID']}&action=update&rgid={$rgid}";
1098 $mergeParams .= "&gid={$gid}";
1101 $searchRows[$mainId]['actions'] = CRM_Utils_System
::href(ts('merge'), 'civicrm/contact/merge', $mergeParams);
1102 $searchRows[$mainId]['actions'] .= " | <a id='notDuplicate' href='#' onClick=\"processDupes( {$main['srcID']}, {$main['dstID']}, 'dupe-nondupe', 'dupe-listing'); return false;\">" . ts('not a duplicate') . "</a>";
1105 $searchRows[$mainId]['actions'] = '<em>' . ts('Insufficient access rights - cannot merge') . '</em>';
1109 echo CRM_Utils_JSON
::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
1111 CRM_Utils_System
::civiExit();
1115 * Function to retrieve a PDF Page Format for the PDF Letter form
1117 function pdfFormat() {
1118 $formatId = CRM_Utils_Type
::escape($_REQUEST['formatId'], 'Integer');
1120 $pdfFormat = CRM_Core_BAO_PdfFormat
::getById($formatId);
1122 echo json_encode($pdfFormat);
1123 CRM_Utils_System
::civiExit();
1127 * Function to retrieve Paper Size dimensions
1129 function paperSize() {
1130 $paperSizeName = CRM_Utils_Type
::escape($_REQUEST['paperSizeName'], 'String');
1132 $paperSize = CRM_Core_BAO_PaperSize
::getByName($paperSizeName);
1134 echo json_encode($paperSize);
1135 CRM_Utils_System
::civiExit();
1138 static function relationshipContactTypeList() {
1139 $relType = CRM_Utils_Array
::value('relType', $_REQUEST);
1141 $types = CRM_Contact_BAO_Relationship
::getValidContactTypeList($relType);
1143 $elements = array();
1144 foreach ($types as $key => $label) {
1145 $elements[] = array(
1151 echo json_encode($elements);
1152 CRM_Utils_System
::civiExit();
1155 static function selectUnselectContacts() {
1156 $name = CRM_Utils_Array
::value('name', $_REQUEST);
1157 $cacheKey = CRM_Utils_Array
::value('qfKey', $_REQUEST);
1158 $state = CRM_Utils_Array
::value('state', $_REQUEST, 'checked');
1159 $variableType = CRM_Utils_Array
::value('variableType', $_REQUEST, 'single');
1161 $actionToPerform = CRM_Utils_Array
::value('action', $_REQUEST, 'select');
1163 if ($variableType == 'multiple') {
1164 // action post value only works with multiple type variable
1166 //multiple names like mark_x_1-mark_x_2 where 1,2 are cids
1167 $elements = explode('-', $name);
1168 foreach ($elements as $key => $element) {
1169 $elements[$key] = self
::_convertToId($element);
1171 CRM_Core_BAO_PrevNextCache
::markSelection($cacheKey, $actionToPerform, $elements);
1174 CRM_Core_BAO_PrevNextCache
::markSelection($cacheKey, $actionToPerform);
1177 elseif ($variableType == 'single') {
1178 $cId = self
::_convertToId($name);
1179 $action = ($state == 'checked') ?
'select' : 'unselect';
1180 CRM_Core_BAO_PrevNextCache
::markSelection($cacheKey, $action, $cId);
1182 $contactIds = CRM_Core_BAO_PrevNextCache
::getSelection($cacheKey);
1183 $countSelectionCids = count($contactIds[$cacheKey]);
1185 $arrRet = array('getCount' => $countSelectionCids);
1186 echo json_encode($arrRet);
1187 CRM_Utils_System
::civiExit();
1190 static function _convertToId($name) {
1191 if (substr($name, 0, CRM_Core_Form
::CB_PREFIX_LEN
) == CRM_Core_Form
::CB_PREFIX
) {
1192 $cId = substr($name, CRM_Core_Form
::CB_PREFIX_LEN
);
1197 static function getAddressDisplay() {
1198 $contactId = CRM_Utils_Array
::value('contact_id', $_REQUEST);
1200 $addressVal["error_message"] = "no contact id found";
1205 'contact_id' => $contactId,
1206 'entity_id' => $contactId,
1208 $addressVal = CRM_Core_BAO_Address
::getValues($entityBlock);
1211 echo json_encode($addressVal);
1212 CRM_Utils_System
::civiExit();