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',
56 foreach ($whitelist as $key => $param) {
57 if (!empty($_GET[$key])) {
58 $params[$param] = $_GET[$key];
62 //CRM-10687: Allow quicksearch by multiple fields
63 if (!empty($params['field_name'])) {
64 if ($params['field_name'] == 'phone_numeric') {
65 $params['name'] = preg_replace('/[^\d]/', '', $params['name']);
67 if (!$params['name']) {
68 CRM_Utils_System
::civiExit();
81 foreach ($whitelist as $key) {
82 if (!empty($_GET[$key]) && is_numeric($_GET[$key])) {
83 $params[$key] = $_GET[$key];
87 $result = civicrm_api('Contact', 'getquick', $params);
88 if (empty($result['is_error']) && !empty($result['values'])) {
89 foreach ($result['values'] as $key => $val) {
90 echo "{$val['data']}|{$val['id']}\n";
93 CRM_Utils_System
::civiExit();
96 static function contactReference() {
97 $name = CRM_Utils_Array
::value('s', $_GET);
98 $name = CRM_Utils_Type
::escape($name, 'String');
99 $cfID = CRM_Utils_Type
::escape($_GET['id'], 'Positive');
101 // check that this is a valid, active custom field of Contact Reference type
102 $params = array('id' => $cfID);
103 $returnProperties = array('filter', 'data_type', 'is_active');
104 $fldValues = array();
105 CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_CustomField', $params, $cf, $returnProperties);
106 if (!$cf['id'] ||
!$cf['is_active'] ||
$cf['data_type'] = !'ContactReference') {
107 echo "$name|error\n";
108 CRM_Utils_System
::civiExit();
112 $filterParams = array();
113 parse_str($cf['filter'], $filterParams);
115 $action = CRM_Utils_Array
::value('action', $filterParams);
117 if (!empty($action) &&
118 !in_array($action, array('get', 'lookup'))
120 echo "$name|error\n";
121 CRM_Utils_System
::civiExit();
125 $list = array_keys(CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
126 'contact_reference_options'
129 $return = array_unique(array_merge(array('sort_name'), $list));
131 $config = CRM_Core_Config
::singleton();
134 if (CRM_Utils_Array
::value('limit', $_GET)) {
135 $limit = CRM_Utils_Type
::escape($_GET['limit'], 'Positive');
138 $params = array('offset' => 0, 'rowCount' => $limit, 'version' => 3);
139 foreach ($return as $fld) {
140 $params["return.{$fld}"] = 1;
143 if (!empty($action)) {
144 $excludeGet = array('reset', 'key', 'className', 'fnName', 'json', 'reset', 'context', 'timestamp', 'limit', 'id', 's', 'q', 'action');
145 foreach ($_GET as $param => $val) {
147 in_array($param, $excludeGet) ||
148 strpos($param, 'return.') !== FALSE ||
149 strpos($param, 'api.') !== FALSE
153 $params[$param] = $val;
158 $params['sort_name'] = $name;
161 $params['sort'] = 'sort_name';
163 // tell api to skip permission chk. dgg
164 $params['check_permissions'] = 0;
166 // add filter variable to params
167 if (!empty($filterParams)) {
168 $params = array_merge($params, $filterParams);
171 $contact = civicrm_api('Contact', 'Get', $params);
173 if (CRM_Utils_Array
::value('is_error', $contact)) {
174 echo "$name|error\n";
175 CRM_Utils_System
::civiExit();
179 foreach ($contact['values'] as $value) {
181 foreach ($return as $fld) {
182 if (CRM_Utils_Array
::value($fld, $value)) {
183 $view[] = $value[$fld];
186 echo $contactList = implode(' :: ', $view) . "|" . $value['id'] . "\n";
190 echo "$name|$name\n";
193 CRM_Utils_System
::civiExit();
197 * Function to fetch PCP ID by PCP Supporter sort_name, also displays PCP title and associated Contribution Page title
199 static function getPCPList() {
200 $name = CRM_Utils_Array
::value('s', $_GET);
201 $name = CRM_Utils_Type
::escape($name, 'String');
204 $where = ' AND pcp.page_id = cp.id AND pcp.contact_id = cc.id';
206 $config = CRM_Core_Config
::singleton();
207 if ($config->includeWildCardInName
) {
208 $strSearch = "%$name%";
211 $strSearch = "$name%";
213 $includeEmailFrom = $includeNickName = '';
214 if ($config->includeNickNameInName
) {
215 $includeNickName = " OR nick_name LIKE '$strSearch'";
217 if ($config->includeEmailInName
) {
218 $includeEmailFrom = "LEFT JOIN civicrm_email eml ON ( cc.id = eml.contact_id AND eml.is_primary = 1 )";
219 $whereClause = " WHERE ( email LIKE '$strSearch' OR sort_name LIKE '$strSearch' $includeNickName ) {$where} ";
222 $whereClause = " WHERE ( sort_name LIKE '$strSearch' $includeNickName ) {$where} ";
225 if (CRM_Utils_Array
::value('limit', $_GET)) {
226 $limit = CRM_Utils_Type
::escape($_GET['limit'], 'Positive');
229 $select = 'cc.sort_name, pcp.title, cp.title';
233 SELECT pcp.id as id, CONCAT_WS( ' :: ', {$select} ) as data, sort_name
234 FROM civicrm_pcp pcp, civicrm_contribution_page cp, civicrm_contact cc
242 $dao = CRM_Core_DAO
::executeQuery($query);
244 while ($dao->fetch()) {
245 echo $pcpList = "$dao->data|$dao->id\n";
248 CRM_Utils_System
::civiExit();
252 * Function to fetch the values
254 static function autocomplete() {
255 $fieldID = CRM_Utils_Type
::escape($_GET['cfid'], 'Integer');
256 $optionGroupID = CRM_Utils_Type
::escape($_GET['ogid'], 'Integer');
257 $label = CRM_Utils_Type
::escape($_GET['s'], 'String');
259 $selectOption = CRM_Core_BAO_CustomOption
::valuesByID($fieldID, $optionGroupID);
261 $completeList = NULL;
262 foreach ($selectOption as $id => $value) {
263 if (strtolower($label) == strtolower(substr($value, 0, strlen($label)))) {
264 echo $completeList = "$value|$id\n";
267 CRM_Utils_System
::civiExit();
270 static function relationship() {
271 $relType = CRM_Utils_Array
::value('rel_type', $_REQUEST);
272 $relContactID = CRM_Utils_Array
::value('rel_contact', $_REQUEST);
273 $sourceContactID = CRM_Utils_Array
::value('contact_id', $_REQUEST); // we no longer need this.
274 $relationshipID = CRM_Utils_Array
::value('rel_id', $_REQUEST); // this used only to determine add or update mode
275 $caseID = CRM_Utils_Array
::value('case_id', $_REQUEST);
277 // check if there are multiple clients for this case, if so then we need create
278 // relationship and also activities for each contacts
280 // get case client list
281 $clientList = CRM_Case_BAO_Case
::getCaseClients($caseID);
283 foreach($clientList as $sourceContactID) {
284 $relationParams = array(
285 'relationship_type_id' => $relType . '_a_b',
286 'contact_check' => array($relContactID => 1),
288 'case_id' => $caseID,
289 'start_date' => date("Ymd"),
292 $relationIds = array('contact' => $sourceContactID);
294 // check if we are editing/updating existing relationship
295 if ($relationshipID && $relationshipID != 'null') {
296 // here we need to retrieve appropriate relationshipID based on client id and relationship type id
297 $caseRelationships = new CRM_Contact_DAO_Relationship();
298 $caseRelationships->case_id
= $caseID;
299 $caseRelationships->relationship_type_id
= $relType;
300 $caseRelationships->contact_id_a
= $sourceContactID;
301 $caseRelationships->find();
303 while($caseRelationships->fetch()) {
304 $relationIds['relationship'] = $caseRelationships->id
;
305 $relationIds['contactTarget'] = $relContactID;
307 $caseRelationships->free();
310 // create new or update existing relationship
311 $return = CRM_Contact_BAO_Relationship
::create($relationParams, $relationIds);
313 $status = 'process-relationship-fail';
314 if (CRM_Utils_Array
::value(0, $return[4])) {
315 $relationshipID = $return[4][0];
316 $status = 'process-relationship-success';
318 //create an activity for case role assignment.CRM-4480
319 CRM_Case_BAO_Case
::createCaseRoleActivity($caseID, $relationshipID, $relContactID);
323 $relation['status'] = $status;
324 echo json_encode($relation);
325 CRM_Utils_System
::civiExit();
329 * Function to fetch the custom field help
331 static function customField() {
332 $fieldId = CRM_Utils_Type
::escape($_REQUEST['id'], 'Integer');
333 $params = array('id' => $fieldId);
334 $returnProperties = array('help_pre', 'help_post');
337 CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_CustomField', $params, $values, $returnProperties);
338 echo json_encode($values);
339 CRM_Utils_System
::civiExit();
343 * Function to obtain list of permissioned employer for the given contact-id.
345 static function getPermissionedEmployer() {
346 $cid = CRM_Utils_Type
::escape($_GET['cid'], 'Integer');
347 $name = trim(CRM_Utils_Type
::escape($_GET['s'], 'String'));
348 $name = str_replace('*', '%', $name);
350 $elements = CRM_Contact_BAO_Relationship
::getPermissionedEmployer($cid, $name);
352 if (!empty($elements)) {
353 foreach ($elements as $cid => $name) {
354 echo $element = $name['name'] . "|$cid\n";
357 CRM_Utils_System
::civiExit();
361 static function groupTree() {
362 $gids = CRM_Utils_Type
::escape($_GET['gids'], 'String');
363 echo CRM_Contact_BAO_GroupNestingCache
::json($gids);
364 CRM_Utils_System
::civiExit();
368 * Function for building contact combo box
370 static function search() {
372 $name = CRM_Utils_Array
::value('name', $_GET, '');
373 if (!array_key_exists('name', $_GET)) {
374 $name = CRM_Utils_Array
::value('s', $_GET) . '%';
377 $name = CRM_Utils_Type
::escape($name, 'String');
379 if (CRM_Utils_Array
::value('id', $_GET)) {
381 if (is_numeric($_GET['id'])) {
382 $id = CRM_Utils_Type
::escape($_GET['id'], 'Integer');
383 $whereIdClause = " AND civicrm_contact.id = {$id}";
391 if ($name ||
isset($id)) {
394 //contact's based of relationhip type
396 if (isset($_GET['rel'])) {
397 $relation = explode('_', $_GET['rel']);
398 $relType = CRM_Utils_Type
::escape($relation[0], 'Integer');
399 $rel = CRM_Utils_Type
::escape($relation[2], 'String');
402 //shared household info
404 if (isset($_GET['sh'])) {
405 $shared = CRM_Utils_Type
::escape($_GET['sh'], 'Integer');
407 $contactType = 'Household';
408 $cName = 'household_name';
411 $contactType = 'Organization';
412 $cName = 'organization_name';
416 // contacts of type household
417 $hh = $addStreet = $addCity = NULL;
418 if (isset($_GET['hh'])) {
419 $hh = CRM_Utils_Type
::escape($_GET['hh'], 'Integer');
423 $organization = $street = $city = NULL;
424 if (isset($_GET['org'])) {
425 $organization = CRM_Utils_Type
::escape($_GET['org'], 'Integer');
428 if (isset($_GET['org']) ||
isset($_GET['hh'])) {
430 if ($splitName = explode(' :: ', $name)) {
431 $contactName = trim(CRM_Utils_Array
::value('0', $splitName));
432 $street = trim(CRM_Utils_Array
::value('1', $splitName));
433 $city = trim(CRM_Utils_Array
::value('2', $splitName));
436 $contactName = $name;
440 $addStreet = "AND civicrm_address.street_address LIKE '$street%'";
443 $addCity = "AND civicrm_address.city LIKE '$city%'";
450 SELECT CONCAT_WS(' :: ',sort_name,LEFT(street_address,25),city) 'sort_name',
451 civicrm_contact.id 'id'
453 LEFT JOIN civicrm_address ON ( civicrm_contact.id = civicrm_address.contact_id
454 AND civicrm_address.is_primary=1
456 WHERE civicrm_contact.contact_type='Organization' AND organization_name LIKE '%$contactName%'
457 {$addStreet} {$addCity} {$whereIdClause}
458 ORDER BY organization_name ";
462 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} ";
466 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'
468 LEFT JOIN civicrm_address ON (civicrm_contact.id =civicrm_address.contact_id AND civicrm_address.is_primary =1 )
469 WHERE civicrm_contact.contact_type ='Household'
470 AND household_name LIKE '%$contactName%' {$addStreet} {$addCity} {$whereIdClause} ORDER BY household_name ";
473 if (CRM_Utils_Array
::value('case', $_GET)) {
475 SELECT distinct(c.id), c.sort_name
476 FROM civicrm_contact c
477 LEFT JOIN civicrm_relationship ON civicrm_relationship.contact_id_{$rel} = c.id
478 WHERE c.sort_name LIKE '%$name%'
479 AND civicrm_relationship.relationship_type_id = $relType
489 WHERE sort_name LIKE '%$name'
491 ORDER BY sort_name ";
495 if (isset($_GET['limit'])) {
496 $limit = CRM_Utils_Type
::escape($_GET['limit'], 'Positive');
499 $query .= " LIMIT 0,{$limit}";
501 $dao = CRM_Core_DAO
::executeQuery($query);
504 while ($dao->fetch()) {
505 echo $dao->sort_name
;
506 CRM_Utils_System
::civiExit();
510 while ($dao->fetch()) {
512 $elements[] = array('name' => addslashes($dao->sort_name
),
517 echo $elements = "$dao->sort_name|$dao->id|$dao->location_type_id|$dao->is_primary|$dao->is_billing\n";
520 //for adding new household address / organization
521 if (empty($elements) && !$json && ($hh ||
$organization)) {
522 echo CRM_Utils_Array
::value('s', $_GET);
527 if (isset($_GET['sh'])) {
529 CRM_Utils_System
::civiExit();
532 if (empty($elements)) {
533 $name = str_replace('%', '', $name);
541 echo json_encode($elements);
543 CRM_Utils_System
::civiExit();
548 * Function to check how many contact exits in db for given criteria,
549 * if one then return contact id else null
551 static function contact() {
552 $name = CRM_Utils_Type
::escape($_GET['name'], 'String');
557 WHERE sort_name LIKE '%$name%'";
559 $dao = CRM_Core_DAO
::executeQuery($query);
565 CRM_Utils_System
::civiExit();
569 * Function to delete custom value
572 static function deleteCustomValue() {
573 $customValueID = CRM_Utils_Type
::escape($_REQUEST['valueID'], 'Positive');
574 $customGroupID = CRM_Utils_Type
::escape($_REQUEST['groupID'], 'Positive');
576 CRM_Core_BAO_CustomValue
::deleteCustomValue($customValueID, $customGroupID);
577 if ($contactId = CRM_Utils_Array
::value('contactId', $_REQUEST)) {
578 echo CRM_Contact_BAO_Contact
::getCountComponent('custom_' . $_REQUEST['groupID'], $contactId);
581 // reset the group contact cache for this group
582 CRM_Contact_BAO_GroupContactCache
::remove();
583 CRM_Utils_System
::civiExit();
587 * Function to perform enable / disable actions on record.
590 static function enableDisable() {
591 $op = CRM_Utils_Type
::escape($_REQUEST['op'], 'String');
592 $recordID = CRM_Utils_Type
::escape($_REQUEST['recordID'], 'Positive');
593 $recordBAO = CRM_Utils_Type
::escape($_REQUEST['recordBAO'], 'String');
596 if ($op == 'disable-enable') {
599 elseif ($op == 'enable-disable') {
602 $status = array('status' => 'record-updated-fail');
603 if (isset($isActive)) {
604 // first munge and clean the recordBAO and get rid of any non alpha numeric characters
605 $recordBAO = CRM_Utils_String
::munge($recordBAO);
606 $recordClass = explode('_', $recordBAO);
608 // make sure recordClass is namespaced (we cant check CRM since extensions can also use this)
609 // but it should be at least 3 levels deep
610 if (count($recordClass) >= 3) {
611 require_once (str_replace('_', DIRECTORY_SEPARATOR
, $recordBAO) . ".php");
612 $method = 'setIsActive';
614 if (method_exists($recordBAO, $method)) {
615 $updated = call_user_func_array(array($recordBAO, $method),
616 array($recordID, $isActive)
619 $status = array('status' => 'record-updated-success');
622 // call hook enableDisable
623 CRM_Utils_Hook
::enableDisable($recordBAO, $recordID, $isActive);
626 echo json_encode($status);
627 CRM_Utils_System
::civiExit();
632 *Function to check the CMS username
635 static public function checkUserName() {
636 $config = CRM_Core_Config
::singleton();
637 $username = trim($_REQUEST['cms_name']);
639 $params = array('name' => $username);
642 $config->userSystem
->checkUserNameEmailExists($params, $errors);
644 if (isset($errors['cms_name']) ||
isset($errors['name'])) {
645 //user name is not availble
646 $user = array('name' => 'no');
647 echo json_encode($user);
650 //user name is available
651 $user = array('name' => 'yes');
652 echo json_encode($user);
654 CRM_Utils_System
::civiExit();
658 * Function to get email address of a contact
660 static function getContactEmail() {
661 if (CRM_Utils_Array
::value('contact_id', $_REQUEST)) {
662 $contactID = CRM_Utils_Type
::escape($_REQUEST['contact_id'], 'Positive');
665 ) = CRM_Contact_BAO_Contact_Location
::getEmailDetails($contactID);
671 $noemail = CRM_Utils_Array
::value('noemail', $_GET);
673 if ($name = CRM_Utils_Array
::value('name', $_GET)) {
674 $name = CRM_Utils_Type
::escape($name, 'String');
676 $queryString = " cc.sort_name LIKE '%$name%'";
679 $queryString = " ( cc.sort_name LIKE '%$name%' OR ce.email LIKE '%$name%' ) ";
682 elseif ($cid = CRM_Utils_Array
::value('cid', $_GET)) {
683 //check cid for interger
684 $contIDS = explode(',', $cid);
685 foreach ($contIDS as $contID) {
686 CRM_Utils_Type
::escape($contID, 'Integer');
688 $queryString = " cc.id IN ( $cid )";
692 $offset = CRM_Utils_Array
::value('offset', $_GET, 0);
693 $rowCount = CRM_Utils_Array
::value('rowcount', $_GET, 20);
695 // add acl clause here
696 list($aclFrom, $aclWhere) = CRM_Contact_BAO_Contact_Permission
::cacheClause('cc');
698 $aclWhere = " AND $aclWhere";
702 SELECT sort_name name, cc.id
703 FROM civicrm_contact cc
705 WHERE cc.is_deceased = 0 AND {$queryString}
707 LIMIT {$offset}, {$rowCount}
710 // send query to hook to be modified if needed
711 CRM_Utils_Hook
::contactListQuery($query,
713 CRM_Utils_Array
::value('context', $_GET),
714 CRM_Utils_Array
::value('cid', $_GET)
717 $dao = CRM_Core_DAO
::executeQuery($query);
718 while ($dao->fetch()) {
720 'name' => $dao->name
,
727 SELECT sort_name name, ce.email, cc.id
728 FROM civicrm_email ce INNER JOIN civicrm_contact cc ON cc.id = ce.contact_id
730 WHERE ce.on_hold = 0 AND cc.is_deceased = 0 AND cc.do_not_email = 0 AND {$queryString}
732 LIMIT {$offset}, {$rowCount}
735 // send query to hook to be modified if needed
736 CRM_Utils_Hook
::contactListQuery($query,
738 CRM_Utils_Array
::value('context', $_GET),
739 CRM_Utils_Array
::value('cid', $_GET)
743 $dao = CRM_Core_DAO
::executeQuery($query);
745 while ($dao->fetch()) {
747 'name' => '"' . $dao->name
. '" <' . $dao->email
. '>',
748 'id' => (CRM_Utils_Array
::value('id', $_GET)) ?
"{$dao->id}::{$dao->email}" : '"' . $dao->name
. '" <' . $dao->email
. '>',
754 echo json_encode($result);
758 CRM_Utils_System
::civiExit();
761 static function getContactPhone() {
764 //check for mobile type
765 $phoneTypes = CRM_Core_OptionGroup
::values('phone_type', TRUE, FALSE, FALSE, NULL, 'name');
766 $mobileType = CRM_Utils_Array
::value('Mobile', $phoneTypes);
768 if ($name = CRM_Utils_Array
::value('name', $_GET)) {
769 $name = CRM_Utils_Type
::escape($name, 'String');
770 $queryString = " ( cc.sort_name LIKE '%$name%' OR cp.phone LIKE '%$name%' ) ";
772 elseif ($cid = CRM_Utils_Array
::value('cid', $_GET)) {
773 //check cid for interger
774 $contIDS = explode(',', $cid);
775 foreach ($contIDS as $contID) {
776 CRM_Utils_Type
::escape($contID, 'Integer');
778 $queryString = " cc.id IN ( $cid )";
782 $offset = CRM_Utils_Array
::value('offset', $_GET, 0);
783 $rowCount = CRM_Utils_Array
::value('rowcount', $_GET, 20);
785 // add acl clause here
786 list($aclFrom, $aclWhere) = CRM_Contact_BAO_Contact_Permission
::cacheClause('cc');
788 $aclWhere = " AND $aclWhere";
792 SELECT sort_name name, cp.phone, cc.id
793 FROM civicrm_phone cp INNER JOIN civicrm_contact cc ON cc.id = cp.contact_id
795 WHERE cc.is_deceased = 0 AND cc.do_not_sms = 0 AND cp.phone_type_id = {$mobileType} AND {$queryString}
797 LIMIT {$offset}, {$rowCount}
800 // send query to hook to be modified if needed
801 CRM_Utils_Hook
::contactListQuery($query,
803 CRM_Utils_Array
::value('context', $_GET),
804 CRM_Utils_Array
::value('cid', $_GET)
807 $dao = CRM_Core_DAO
::executeQuery($query);
809 while ($dao->fetch()) {
811 'name' => '"' . $dao->name
. '" <' . $dao->phone
. '>',
812 'id' => (CRM_Utils_Array
::value('id', $_GET)) ?
"{$dao->id}::{$dao->phone}" : '"' . $dao->name
. '" <' . $dao->phone
. '>',
818 echo json_encode($result);
820 CRM_Utils_System
::civiExit();
824 static function buildSubTypes() {
825 $parent = CRM_Utils_Array
::value('parentId', $_REQUEST);
829 $contactType = 'Individual';
833 $contactType = 'Household';
837 $contactType = 'Organization';
841 $subTypes = CRM_Contact_BAO_ContactType
::subTypePairs($contactType, FALSE, NULL);
843 echo json_encode($subTypes);
844 CRM_Utils_System
::civiExit();
847 static function buildDedupeRules() {
848 $parent = CRM_Utils_Array
::value('parentId', $_REQUEST);
852 $contactType = 'Individual';
856 $contactType = 'Household';
860 $contactType = 'Organization';
864 $dedupeRules = CRM_Dedupe_BAO_RuleGroup
::getByType($contactType);
866 echo json_encode($dedupeRules);
867 CRM_Utils_System
::civiExit();
871 * Function used for CiviCRM dashboard operations
873 static function dashboard() {
874 $operation = CRM_Utils_Type
::escape($_REQUEST['op'], 'String');
876 switch ($operation) {
877 case 'get_widgets_by_column':
878 // This would normally be coming from either the database (this user's settings) or a default/initial dashboard configuration.
879 // get contact id of logged in user
881 $dashlets = CRM_Core_BAO_Dashboard
::getContactDashlets();
885 $dashletID = CRM_Utils_Type
::escape($_GET['id'], 'Positive');
887 $dashlets = CRM_Core_BAO_Dashboard
::getDashletInfo($dashletID);
891 CRM_Core_BAO_Dashboard
::saveDashletChanges($_REQUEST['columns']);
892 CRM_Utils_System
::civiExit();
893 case 'delete_dashlet':
894 $dashletID = CRM_Utils_Type
::escape($_REQUEST['dashlet_id'], 'Positive');
895 CRM_Core_BAO_Dashboard
::deleteDashlet($dashletID);
896 CRM_Utils_System
::civiExit();
899 echo json_encode($dashlets);
900 CRM_Utils_System
::civiExit();
904 * Function to retrieve signature based on email id
906 static function getSignature() {
907 $emailID = CRM_Utils_Type
::escape($_REQUEST['emailID'], 'Positive');
908 $query = "SELECT signature_text, signature_html FROM civicrm_email WHERE id = {$emailID}";
909 $dao = CRM_Core_DAO
::executeQuery($query);
911 $signatures = array();
912 while ($dao->fetch()) {
914 'signature_text' => $dao->signature_text
,
915 'signature_html' => $dao->signature_html
,
919 echo json_encode($signatures);
920 CRM_Utils_System
::civiExit();
923 static function relationshipContacts() {
924 $searchValues = $searchRows = array();
927 $relType = CRM_Utils_Type
::escape($_REQUEST['relType'], 'String');
928 $typeName = isset($_REQUEST['typeName']) ? CRM_Utils_Type
::escape($_REQUEST['typeName'], 'String') : '';
929 $relContact = CRM_Utils_Type
::escape($_REQUEST['relContact'], 'String');
930 $currentContactId = CRM_Utils_Type
::escape($_REQUEST['cid'], 'Integer');
932 if (in_array($typeName, array(
933 'Employee of', 'Employer of'))) {
938 1 => 'sort_name', (2 +
$addCount) => 'city', (3 +
$addCount) => 'state_province',
939 (4 +
$addCount) => 'email', (5 +
$addCount) => 'phone',
942 $sEcho = CRM_Utils_Type
::escape($_REQUEST['sEcho'], 'Integer');
943 $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type
::escape($_REQUEST['iDisplayStart'], 'Integer') : 0;
944 $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type
::escape($_REQUEST['iDisplayLength'], 'Integer') : 25;
945 $sort = isset($_REQUEST['iSortCol_0']) ?
$sortMapper[CRM_Utils_Type
::escape($_REQUEST['iSortCol_0'], 'Integer')] : 'sort_name';
946 $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type
::escape($_REQUEST['sSortDir_0'], 'String') : 'asc';
948 $searchValues[] = array('sort_name', 'LIKE', $relContact, 0, 1);
950 list($rid, $direction) = explode('_', $relType, 2);
952 $relationshipType = new CRM_Contact_DAO_RelationshipType();
954 $relationshipType->id
= $rid;
955 if ($relationshipType->find(TRUE)) {
956 if ($direction == 'a_b') {
957 $type = $relationshipType->contact_type_b
;
958 $subType = $relationshipType->contact_sub_type_b
;
961 $type = $relationshipType->contact_type_a
;
962 $subType = $relationshipType->contact_sub_type_a
;
965 if ($type == 'Individual' ||
$type == 'Organization' ||
$type == 'Household') {
966 $searchValues[] = array('contact_type', '=', $type, 0, 0);
970 $searchValues[] = array('contact_sub_type', '=', $subType, 0, 0);
974 // exclude current contact
975 $searchValues[] = array('contact_id', '!=', $currentContactId, 0, 0);
977 $query = new CRM_Contact_BAO_Query($searchValues);
978 $searchCount = $query->searchQuery(0, 0, NULL, TRUE);
979 $iTotal = $searchCount;
981 if ($searchCount > 0) {
982 // get the result of the search
983 $result = $query->searchQuery($offset, $rowCount, $sort, FALSE, FALSE,
984 FALSE, FALSE, FALSE, NULL, $sortOrder
987 $config = CRM_Core_Config
::singleton();
989 while ($result->fetch()) {
990 $contactID = $result->contact_id
;
991 $typeImage = CRM_Contact_BAO_Contact_Utils
::getImage($result->contact_sub_type ?
992 $result->contact_sub_type
: $result->contact_type
,
996 $searchRows[$contactID]['id'] = $contactID;
997 $searchRows[$contactID]['name'] = $typeImage . ' ' . $result->sort_name
;
998 $searchRows[$contactID]['city'] = $result->city
;
999 $searchRows[$contactID]['state'] = $result->state_province
;
1000 $searchRows[$contactID]['email'] = $result->email
;
1001 $searchRows[$contactID]['phone'] = $result->phone
;
1005 foreach ($searchRows as $cid => $row) {
1006 $searchRows[$cid]['check'] = '<input type="checkbox" id="contact_check[' . $cid . ']" name="contact_check[' . $cid . ']" value=' . $cid . ' />';
1008 if ($typeName == 'Employee of') {
1009 $searchRows[$cid]['employee_of'] = '<input type="radio" name="employee_of" value=' . $cid . ' >';
1011 elseif ($typeName == 'Employer of') {
1012 $searchRows[$cid]['employer_of'] = '<input type="checkbox" name="employer_of[' . $cid . ']" value=' . $cid . ' />';
1016 $selectorElements = array('check', 'name');
1017 if ($typeName == 'Employee of') {
1018 $selectorElements[] = 'employee_of';
1020 elseif ($typeName == 'Employer of') {
1021 $selectorElements[] = 'employer_of';
1023 $selectorElements = array_merge($selectorElements, array('city', 'state', 'email', 'phone'));
1025 $iFilteredTotal = $iTotal;
1026 echo CRM_Utils_JSON
::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
1027 CRM_Utils_System
::civiExit();
1031 * Function to process dupes.
1034 static function processDupes() {
1035 $oper = CRM_Utils_Type
::escape($_REQUEST['op'], 'String');
1036 $cid = CRM_Utils_Type
::escape($_REQUEST['cid'], 'Positive');
1037 $oid = CRM_Utils_Type
::escape($_REQUEST['oid'], 'Positive');
1039 if (!$oper ||
!$cid ||
!$oid) {
1043 $exception = new CRM_Dedupe_DAO_Exception();
1044 $exception->contact_id1
= $cid;
1045 $exception->contact_id2
= $oid;
1046 //make sure contact2 > contact1.
1048 $exception->contact_id1
= $oid;
1049 $exception->contact_id2
= $cid;
1051 $exception->find(TRUE);
1053 if ($oper == 'dupe-nondupe') {
1054 $status = $exception->save();
1056 if ($oper == 'nondupe-dupe') {
1057 $status = $exception->delete();
1060 echo json_encode(array('status' => ($status) ?
$oper : $status));
1061 CRM_Utils_System
::civiExit();
1064 static function getDedupes() {
1066 $sEcho = CRM_Utils_Type
::escape($_REQUEST['sEcho'], 'Integer');
1067 $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type
::escape($_REQUEST['iDisplayStart'], 'Integer') : 0;
1068 $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type
::escape($_REQUEST['iDisplayLength'], 'Integer') : 25;
1069 $sort = isset($_REQUEST['iSortCol_0']) ?
$sortMapper[CRM_Utils_Type
::escape($_REQUEST['iSortCol_0'], 'Integer')] : 'sort_name';
1070 $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type
::escape($_REQUEST['sSortDir_0'], 'String') : 'asc';
1072 $gid = isset($_REQUEST['gid']) ? CRM_Utils_Type
::escape($_REQUEST['gid'], 'Integer') : 0;
1073 $rgid = isset($_REQUEST['rgid']) ? CRM_Utils_Type
::escape($_REQUEST['rgid'], 'Integer') : 0;
1076 $contactType = CRM_Core_DAO
::getFieldValue('CRM_Dedupe_DAO_RuleGroup', $rgid, 'contact_type');
1079 $cacheKeyString = "merge {$contactType}_{$rgid}_{$gid}";
1080 $searchRows = array();
1081 $selectorElements = array('src', 'dst', 'weight', 'actions');
1084 $join = "LEFT JOIN civicrm_dedupe_exception de ON ( pn.entity_id1 = de.contact_id1 AND
1085 pn.entity_id2 = de.contact_id2 )";
1086 $where = "de.id IS NULL";
1088 $iFilteredTotal = $iTotal = CRM_Core_BAO_PrevNextCache
::getCount($cacheKeyString, $join, $where);
1089 $mainContacts = CRM_Core_BAO_PrevNextCache
::retrieve($cacheKeyString, $join, $where, $offset, $rowCount);
1091 foreach ($mainContacts as $mainId => $main) {
1092 $searchRows[$mainId]['src'] = CRM_Utils_System
::href($main['srcName'], 'civicrm/contact/view', "reset=1&cid={$main['srcID']}");
1093 $searchRows[$mainId]['dst'] = CRM_Utils_System
::href($main['dstName'], 'civicrm/contact/view', "reset=1&cid={$main['dstID']}");
1094 $searchRows[$mainId]['weight'] = CRM_Utils_Array
::value('weight', $main);
1096 if (CRM_Utils_Array
::value('canMerge', $main)) {
1097 $mergeParams = "reset=1&cid={$main['srcID']}&oid={$main['dstID']}&action=update&rgid={$rgid}";
1099 $mergeParams .= "&gid={$gid}";
1102 $searchRows[$mainId]['actions'] = CRM_Utils_System
::href(ts('merge'), 'civicrm/contact/merge', $mergeParams);
1103 $searchRows[$mainId]['actions'] .= " | <a id='notDuplicate' href='#' onClick=\"processDupes( {$main['srcID']}, {$main['dstID']}, 'dupe-nondupe', 'dupe-listing'); return false;\">" . ts('not a duplicate') . "</a>";
1106 $searchRows[$mainId]['actions'] = '<em>' . ts('Insufficient access rights - cannot merge') . '</em>';
1110 echo CRM_Utils_JSON
::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
1112 CRM_Utils_System
::civiExit();
1116 * Function to retrieve a PDF Page Format for the PDF Letter form
1118 function pdfFormat() {
1119 $formatId = CRM_Utils_Type
::escape($_REQUEST['formatId'], 'Integer');
1121 $pdfFormat = CRM_Core_BAO_PdfFormat
::getById($formatId);
1123 echo json_encode($pdfFormat);
1124 CRM_Utils_System
::civiExit();
1128 * Function to retrieve Paper Size dimensions
1130 function paperSize() {
1131 $paperSizeName = CRM_Utils_Type
::escape($_REQUEST['paperSizeName'], 'String');
1133 $paperSize = CRM_Core_BAO_PaperSize
::getByName($paperSizeName);
1135 echo json_encode($paperSize);
1136 CRM_Utils_System
::civiExit();
1139 static function relationshipContactTypeList() {
1140 $relType = CRM_Utils_Array
::value('relType', $_REQUEST);
1142 $types = CRM_Contact_BAO_Relationship
::getValidContactTypeList($relType);
1144 $elements = array();
1145 foreach ($types as $key => $label) {
1146 $elements[] = array(
1152 echo json_encode($elements);
1153 CRM_Utils_System
::civiExit();
1156 static function selectUnselectContacts() {
1157 $name = CRM_Utils_Array
::value('name', $_REQUEST);
1158 $cacheKey = CRM_Utils_Array
::value('qfKey', $_REQUEST);
1159 $state = CRM_Utils_Array
::value('state', $_REQUEST, 'checked');
1160 $variableType = CRM_Utils_Array
::value('variableType', $_REQUEST, 'single');
1162 $actionToPerform = CRM_Utils_Array
::value('action', $_REQUEST, 'select');
1164 if ($variableType == 'multiple') {
1165 // action post value only works with multiple type variable
1167 //multiple names like mark_x_1-mark_x_2 where 1,2 are cids
1168 $elements = explode('-', $name);
1169 foreach ($elements as $key => $element) {
1170 $elements[$key] = self
::_convertToId($element);
1172 CRM_Core_BAO_PrevNextCache
::markSelection($cacheKey, $actionToPerform, $elements);
1175 CRM_Core_BAO_PrevNextCache
::markSelection($cacheKey, $actionToPerform);
1178 elseif ($variableType == 'single') {
1179 $cId = self
::_convertToId($name);
1180 $action = ($state == 'checked') ?
'select' : 'unselect';
1181 CRM_Core_BAO_PrevNextCache
::markSelection($cacheKey, $action, $cId);
1183 $contactIds = CRM_Core_BAO_PrevNextCache
::getSelection($cacheKey);
1184 $countSelectionCids = count($contactIds[$cacheKey]);
1186 $arrRet = array('getCount' => $countSelectionCids);
1187 echo json_encode($arrRet);
1188 CRM_Utils_System
::civiExit();
1191 static function _convertToId($name) {
1192 if (substr($name, 0, CRM_Core_Form
::CB_PREFIX_LEN
) == CRM_Core_Form
::CB_PREFIX
) {
1193 $cId = substr($name, CRM_Core_Form
::CB_PREFIX_LEN
);
1198 static function getAddressDisplay() {
1199 $contactId = CRM_Utils_Array
::value('contact_id', $_REQUEST);
1201 $addressVal["error_message"] = "no contact id found";
1206 'contact_id' => $contactId,
1207 'entity_id' => $contactId,
1209 $addressVal = CRM_Core_BAO_Address
::getValues($entityBlock);
1212 echo json_encode($addressVal);
1213 CRM_Utils_System
::civiExit();