3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
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 'contact_sub_type' => 'contact_sub_type'
57 foreach ($whitelist as $key => $param) {
58 if (!empty($_GET[$key])) {
59 $params[$param] = $_GET[$key];
63 //CRM-10687: Allow quicksearch by multiple fields
64 if (!empty($params['field_name'])) {
65 if ($params['field_name'] == 'phone_numeric') {
66 $params['name'] = preg_replace('/[^\d]/', '', $params['name']);
68 if (!$params['name']) {
69 CRM_Utils_System
::civiExit();
82 foreach ($whitelist as $key) {
83 if (!empty($_GET[$key]) && is_numeric($_GET[$key])) {
84 $params[$key] = $_GET[$key];
88 $result = civicrm_api('Contact', 'getquick', $params);
89 if (empty($result['is_error']) && !empty($result['values'])) {
90 foreach ($result['values'] as $key => $val) {
91 echo "{$val['data']}|{$val['id']}\n";
94 CRM_Utils_System
::civiExit();
97 static function contactReference() {
98 $name = CRM_Utils_Array
::value('s', $_GET);
99 $name = CRM_Utils_Type
::escape($name, 'String');
100 $cfID = CRM_Utils_Type
::escape($_GET['id'], 'Positive');
102 // check that this is a valid, active custom field of Contact Reference type
103 $params = array('id' => $cfID);
104 $returnProperties = array('filter', 'data_type', 'is_active');
105 $fldValues = array();
106 CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_CustomField', $params, $cf, $returnProperties);
107 if (!$cf['id'] ||
!$cf['is_active'] ||
$cf['data_type'] = !'ContactReference') {
108 echo "$name|error\n";
109 CRM_Utils_System
::civiExit();
113 $filterParams = array();
114 parse_str($cf['filter'], $filterParams);
116 $action = CRM_Utils_Array
::value('action', $filterParams);
118 if (!empty($action) &&
119 !in_array($action, array('get', 'lookup'))
121 echo "$name|error\n";
122 CRM_Utils_System
::civiExit();
126 $list = array_keys(CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
127 'contact_reference_options'
130 $return = array_unique(array_merge(array('sort_name'), $list));
132 $config = CRM_Core_Config
::singleton();
135 if (CRM_Utils_Array
::value('limit', $_GET)) {
136 $limit = CRM_Utils_Type
::escape($_GET['limit'], 'Positive');
139 $params = array('offset' => 0, 'rowCount' => $limit, 'version' => 3);
140 foreach ($return as $fld) {
141 $params["return.{$fld}"] = 1;
144 if (!empty($action)) {
145 $excludeGet = array('reset', 'key', 'className', 'fnName', 'json', 'reset', 'context', 'timestamp', 'limit', 'id', 's', 'q', 'action');
146 foreach ($_GET as $param => $val) {
148 in_array($param, $excludeGet) ||
149 strpos($param, 'return.') !== FALSE ||
150 strpos($param, 'api.') !== FALSE
154 $params[$param] = $val;
159 $params['sort_name'] = $name;
162 $params['sort'] = 'sort_name';
164 // tell api to skip permission chk. dgg
165 $params['check_permissions'] = 0;
167 // add filter variable to params
168 if (!empty($filterParams)) {
169 $params = array_merge($params, $filterParams);
172 $contact = civicrm_api('Contact', 'Get', $params);
174 if (CRM_Utils_Array
::value('is_error', $contact)) {
175 echo "$name|error\n";
176 CRM_Utils_System
::civiExit();
180 foreach ($contact['values'] as $value) {
182 foreach ($return as $fld) {
183 if (CRM_Utils_Array
::value($fld, $value)) {
184 $view[] = $value[$fld];
187 echo $contactList = implode(' :: ', $view) . "|" . $value['id'] . "\n";
191 echo "$name|$name\n";
194 CRM_Utils_System
::civiExit();
198 * Function to fetch PCP ID by PCP Supporter sort_name, also displays PCP title and associated Contribution Page title
200 static function getPCPList() {
201 $name = CRM_Utils_Array
::value('s', $_GET);
202 $name = CRM_Utils_Type
::escape($name, 'String');
205 $where = ' AND pcp.page_id = cp.id AND pcp.contact_id = cc.id';
207 $config = CRM_Core_Config
::singleton();
208 if ($config->includeWildCardInName
) {
209 $strSearch = "%$name%";
212 $strSearch = "$name%";
214 $includeEmailFrom = $includeNickName = '';
215 if ($config->includeNickNameInName
) {
216 $includeNickName = " OR nick_name LIKE '$strSearch'";
218 if ($config->includeEmailInName
) {
219 $includeEmailFrom = "LEFT JOIN civicrm_email eml ON ( cc.id = eml.contact_id AND eml.is_primary = 1 )";
220 $whereClause = " WHERE ( email LIKE '$strSearch' OR sort_name LIKE '$strSearch' $includeNickName ) {$where} ";
223 $whereClause = " WHERE ( sort_name LIKE '$strSearch' $includeNickName ) {$where} ";
226 if (CRM_Utils_Array
::value('limit', $_GET)) {
227 $limit = CRM_Utils_Type
::escape($_GET['limit'], 'Positive');
230 $select = 'cc.sort_name, pcp.title, cp.title';
234 SELECT pcp.id as id, CONCAT_WS( ' :: ', {$select} ) as data, sort_name
235 FROM civicrm_pcp pcp, civicrm_contribution_page cp, civicrm_contact cc
243 $dao = CRM_Core_DAO
::executeQuery($query);
245 while ($dao->fetch()) {
246 echo $pcpList = "$dao->data|$dao->id\n";
249 CRM_Utils_System
::civiExit();
253 * Function to fetch the values
255 static function autocomplete() {
256 $fieldID = CRM_Utils_Type
::escape($_GET['cfid'], 'Integer');
257 $optionGroupID = CRM_Utils_Type
::escape($_GET['ogid'], 'Integer');
258 $label = CRM_Utils_Type
::escape($_GET['s'], 'String');
260 $selectOption = CRM_Core_BAO_CustomOption
::valuesByID($fieldID, $optionGroupID);
262 $completeList = NULL;
263 foreach ($selectOption as $id => $value) {
264 if (strtolower($label) == strtolower(substr($value, 0, strlen($label)))) {
265 echo $completeList = "$value|$id\n";
268 CRM_Utils_System
::civiExit();
271 static function relationship() {
272 $relType = CRM_Utils_Array
::value('rel_type', $_REQUEST);
273 $relContactID = CRM_Utils_Array
::value('rel_contact', $_REQUEST);
274 $sourceContactID = CRM_Utils_Array
::value('contact_id', $_REQUEST); // we no longer need this.
275 $relationshipID = CRM_Utils_Array
::value('rel_id', $_REQUEST); // this used only to determine add or update mode
276 $caseID = CRM_Utils_Array
::value('case_id', $_REQUEST);
278 // check if there are multiple clients for this case, if so then we need create
279 // relationship and also activities for each contacts
281 // get case client list
282 $clientList = CRM_Case_BAO_Case
::getCaseClients($caseID);
284 foreach($clientList as $sourceContactID) {
285 $relationParams = array(
286 'relationship_type_id' => $relType . '_a_b',
287 'contact_check' => array($relContactID => 1),
289 'case_id' => $caseID,
290 'start_date' => date("Ymd"),
293 $relationIds = array('contact' => $sourceContactID);
295 // check if we are editing/updating existing relationship
296 if ($relationshipID && $relationshipID != 'null') {
297 // here we need to retrieve appropriate relationshipID based on client id and relationship type id
298 $caseRelationships = new CRM_Contact_DAO_Relationship();
299 $caseRelationships->case_id
= $caseID;
300 $caseRelationships->relationship_type_id
= $relType;
301 $caseRelationships->contact_id_a
= $sourceContactID;
302 $caseRelationships->find();
304 while($caseRelationships->fetch()) {
305 $relationIds['relationship'] = $caseRelationships->id
;
306 $relationIds['contactTarget'] = $relContactID;
308 $caseRelationships->free();
311 // create new or update existing relationship
312 $return = CRM_Contact_BAO_Relationship
::create($relationParams, $relationIds);
314 $status = 'process-relationship-fail';
315 if (CRM_Utils_Array
::value(0, $return[4])) {
316 $relationshipID = $return[4][0];
317 $status = 'process-relationship-success';
319 //create an activity for case role assignment.CRM-4480
320 CRM_Case_BAO_Case
::createCaseRoleActivity($caseID, $relationshipID, $relContactID);
324 $relation['status'] = $status;
325 echo json_encode($relation);
326 CRM_Utils_System
::civiExit();
330 * Function to fetch the custom field help
332 static function customField() {
333 $fieldId = CRM_Utils_Type
::escape($_REQUEST['id'], 'Integer');
334 $params = array('id' => $fieldId);
335 $returnProperties = array('help_pre', 'help_post');
338 CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_CustomField', $params, $values, $returnProperties);
339 echo json_encode($values);
340 CRM_Utils_System
::civiExit();
344 * Function to obtain list of permissioned employer for the given contact-id.
346 static function getPermissionedEmployer() {
347 $cid = CRM_Utils_Type
::escape($_GET['cid'], 'Integer');
348 $name = trim(CRM_Utils_Type
::escape($_GET['s'], 'String'));
349 $name = str_replace('*', '%', $name);
351 $elements = CRM_Contact_BAO_Relationship
::getPermissionedEmployer($cid, $name);
353 if (!empty($elements)) {
354 foreach ($elements as $cid => $name) {
355 echo $element = $name['name'] . "|$cid\n";
358 CRM_Utils_System
::civiExit();
362 static function groupTree() {
363 $gids = CRM_Utils_Type
::escape($_GET['gids'], 'String');
364 echo CRM_Contact_BAO_GroupNestingCache
::json($gids);
365 CRM_Utils_System
::civiExit();
369 * Function for building contact combo box
371 static function search() {
373 $name = CRM_Utils_Array
::value('name', $_GET, '');
374 if (!array_key_exists('name', $_GET)) {
375 $name = CRM_Utils_Array
::value('s', $_GET) . '%';
378 $name = CRM_Utils_Type
::escape($name, 'String');
380 if (CRM_Utils_Array
::value('id', $_GET)) {
382 if (is_numeric($_GET['id'])) {
383 $id = CRM_Utils_Type
::escape($_GET['id'], 'Integer');
384 $whereIdClause = " AND civicrm_contact.id = {$id}";
392 if ($name ||
isset($id)) {
395 //contact's based of relationhip type
397 if (isset($_GET['rel'])) {
398 $relation = explode('_', $_GET['rel']);
399 $relType = CRM_Utils_Type
::escape($relation[0], 'Integer');
400 $rel = CRM_Utils_Type
::escape($relation[2], 'String');
403 //shared household info
405 if (isset($_GET['sh'])) {
406 $shared = CRM_Utils_Type
::escape($_GET['sh'], 'Integer');
408 $contactType = 'Household';
409 $cName = 'household_name';
412 $contactType = 'Organization';
413 $cName = 'organization_name';
417 // contacts of type household
418 $hh = $addStreet = $addCity = NULL;
419 if (isset($_GET['hh'])) {
420 $hh = CRM_Utils_Type
::escape($_GET['hh'], 'Integer');
424 $organization = $street = $city = NULL;
425 if (isset($_GET['org'])) {
426 $organization = CRM_Utils_Type
::escape($_GET['org'], 'Integer');
429 if (isset($_GET['org']) ||
isset($_GET['hh'])) {
431 if ($splitName = explode(' :: ', $name)) {
432 $contactName = trim(CRM_Utils_Array
::value('0', $splitName));
433 $street = trim(CRM_Utils_Array
::value('1', $splitName));
434 $city = trim(CRM_Utils_Array
::value('2', $splitName));
437 $contactName = $name;
441 $addStreet = "AND civicrm_address.street_address LIKE '$street%'";
444 $addCity = "AND civicrm_address.city LIKE '$city%'";
451 SELECT CONCAT_WS(' :: ',sort_name,LEFT(street_address,25),city) 'sort_name',
452 civicrm_contact.id 'id'
454 LEFT JOIN civicrm_address ON ( civicrm_contact.id = civicrm_address.contact_id
455 AND civicrm_address.is_primary=1
457 WHERE civicrm_contact.contact_type='Organization' AND organization_name LIKE '%$contactName%'
458 {$addStreet} {$addCity} {$whereIdClause}
459 ORDER BY organization_name ";
463 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} ";
467 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'
469 LEFT JOIN civicrm_address ON (civicrm_contact.id =civicrm_address.contact_id AND civicrm_address.is_primary =1 )
470 WHERE civicrm_contact.contact_type ='Household'
471 AND household_name LIKE '%$contactName%' {$addStreet} {$addCity} {$whereIdClause} ORDER BY household_name ";
474 if (CRM_Utils_Array
::value('case', $_GET)) {
476 SELECT distinct(c.id), c.sort_name
477 FROM civicrm_contact c
478 LEFT JOIN civicrm_relationship ON civicrm_relationship.contact_id_{$rel} = c.id
479 WHERE c.sort_name LIKE '%$name%'
480 AND civicrm_relationship.relationship_type_id = $relType
490 WHERE sort_name LIKE '%$name'
492 ORDER BY sort_name ";
496 if (isset($_GET['limit'])) {
497 $limit = CRM_Utils_Type
::escape($_GET['limit'], 'Positive');
500 $query .= " LIMIT 0,{$limit}";
502 $dao = CRM_Core_DAO
::executeQuery($query);
505 while ($dao->fetch()) {
506 echo $dao->sort_name
;
507 CRM_Utils_System
::civiExit();
511 while ($dao->fetch()) {
513 $elements[] = array('name' => addslashes($dao->sort_name
),
518 echo $elements = "$dao->sort_name|$dao->id|$dao->location_type_id|$dao->is_primary|$dao->is_billing\n";
521 //for adding new household address / organization
522 if (empty($elements) && !$json && ($hh ||
$organization)) {
523 echo CRM_Utils_Array
::value('s', $_GET);
528 if (isset($_GET['sh'])) {
530 CRM_Utils_System
::civiExit();
533 if (empty($elements)) {
534 $name = str_replace('%', '', $name);
542 echo json_encode($elements);
544 CRM_Utils_System
::civiExit();
549 * Function to check how many contact exits in db for given criteria,
550 * if one then return contact id else null
552 static function contact() {
553 $name = CRM_Utils_Type
::escape($_GET['name'], 'String');
558 WHERE sort_name LIKE '%$name%'";
560 $dao = CRM_Core_DAO
::executeQuery($query);
566 CRM_Utils_System
::civiExit();
570 * Function to delete custom value
573 static function deleteCustomValue() {
574 $customValueID = CRM_Utils_Type
::escape($_REQUEST['valueID'], 'Positive');
575 $customGroupID = CRM_Utils_Type
::escape($_REQUEST['groupID'], 'Positive');
577 CRM_Core_BAO_CustomValue
::deleteCustomValue($customValueID, $customGroupID);
578 if ($contactId = CRM_Utils_Array
::value('contactId', $_REQUEST)) {
579 echo CRM_Contact_BAO_Contact
::getCountComponent('custom_' . $_REQUEST['groupID'], $contactId);
582 // reset the group contact cache for this group
583 CRM_Contact_BAO_GroupContactCache
::remove();
584 CRM_Utils_System
::civiExit();
588 * Function to perform enable / disable actions on record.
591 static function enableDisable() {
592 $op = CRM_Utils_Type
::escape($_REQUEST['op'], 'String');
593 $recordID = CRM_Utils_Type
::escape($_REQUEST['recordID'], 'Positive');
594 $recordBAO = CRM_Utils_Type
::escape($_REQUEST['recordBAO'], 'String');
597 if ($op == 'disable-enable') {
600 elseif ($op == 'enable-disable') {
603 $status = array('status' => 'record-updated-fail');
604 if (isset($isActive)) {
605 // first munge and clean the recordBAO and get rid of any non alpha numeric characters
606 $recordBAO = CRM_Utils_String
::munge($recordBAO);
607 $recordClass = explode('_', $recordBAO);
609 // make sure recordClass is namespaced (we cant check CRM since extensions can also use this)
610 // but it should be at least 3 levels deep
611 if (count($recordClass) >= 3) {
612 require_once (str_replace('_', DIRECTORY_SEPARATOR
, $recordBAO) . ".php");
613 $method = 'setIsActive';
615 if (method_exists($recordBAO, $method)) {
616 $updated = call_user_func_array(array($recordBAO, $method),
617 array($recordID, $isActive)
620 $status = array('status' => 'record-updated-success');
623 // call hook enableDisable
624 CRM_Utils_Hook
::enableDisable($recordBAO, $recordID, $isActive);
627 echo json_encode($status);
628 CRM_Utils_System
::civiExit();
633 *Function to check the CMS username
636 static public function checkUserName() {
637 $config = CRM_Core_Config
::singleton();
638 $username = trim($_REQUEST['cms_name']);
640 $params = array('name' => $username);
643 $config->userSystem
->checkUserNameEmailExists($params, $errors);
645 if (isset($errors['cms_name']) ||
isset($errors['name'])) {
646 //user name is not availble
647 $user = array('name' => 'no');
648 echo json_encode($user);
651 //user name is available
652 $user = array('name' => 'yes');
653 echo json_encode($user);
655 CRM_Utils_System
::civiExit();
659 * Function to get email address of a contact
661 static function getContactEmail() {
662 if (CRM_Utils_Array
::value('contact_id', $_REQUEST)) {
663 $contactID = CRM_Utils_Type
::escape($_REQUEST['contact_id'], 'Positive');
666 ) = CRM_Contact_BAO_Contact_Location
::getEmailDetails($contactID);
672 $noemail = CRM_Utils_Array
::value('noemail', $_GET);
674 if ($name = CRM_Utils_Array
::value('name', $_GET)) {
675 $name = CRM_Utils_Type
::escape($name, 'String');
677 $queryString = " cc.sort_name LIKE '%$name%'";
680 $queryString = " ( cc.sort_name LIKE '%$name%' OR ce.email LIKE '%$name%' ) ";
683 elseif ($cid = CRM_Utils_Array
::value('cid', $_GET)) {
684 //check cid for interger
685 $contIDS = explode(',', $cid);
686 foreach ($contIDS as $contID) {
687 CRM_Utils_Type
::escape($contID, 'Integer');
689 $queryString = " cc.id IN ( $cid )";
693 $offset = CRM_Utils_Array
::value('offset', $_GET, 0);
694 $rowCount = CRM_Utils_Array
::value('rowcount', $_GET, 20);
696 $offset = CRM_Utils_Type
::escape($offset, 'Int');
697 $rowCount = CRM_Utils_Type
::escape($rowCount, 'Int');
699 // add acl clause here
700 list($aclFrom, $aclWhere) = CRM_Contact_BAO_Contact_Permission
::cacheClause('cc');
702 $aclWhere = " AND $aclWhere";
706 SELECT sort_name name, cc.id
707 FROM civicrm_contact cc
709 WHERE cc.is_deceased = 0 AND {$queryString}
711 LIMIT {$offset}, {$rowCount}
714 // send query to hook to be modified if needed
715 CRM_Utils_Hook
::contactListQuery($query,
717 CRM_Utils_Array
::value('context', $_GET),
718 CRM_Utils_Array
::value('cid', $_GET)
721 $dao = CRM_Core_DAO
::executeQuery($query);
722 while ($dao->fetch()) {
724 'name' => $dao->name
,
731 SELECT sort_name name, ce.email, cc.id
732 FROM civicrm_email ce INNER JOIN civicrm_contact cc ON cc.id = ce.contact_id
734 WHERE ce.on_hold = 0 AND cc.is_deceased = 0 AND cc.do_not_email = 0 AND {$queryString}
736 LIMIT {$offset}, {$rowCount}
739 // send query to hook to be modified if needed
740 CRM_Utils_Hook
::contactListQuery($query,
742 CRM_Utils_Array
::value('context', $_GET),
743 CRM_Utils_Array
::value('cid', $_GET)
747 $dao = CRM_Core_DAO
::executeQuery($query);
749 while ($dao->fetch()) {
751 'name' => '"' . $dao->name
. '" <' . $dao->email
. '>',
752 'id' => (CRM_Utils_Array
::value('id', $_GET)) ?
"{$dao->id}::{$dao->email}" : '"' . $dao->name
. '" <' . $dao->email
. '>',
758 echo json_encode($result);
762 CRM_Utils_System
::civiExit();
765 static function getContactPhone() {
768 //check for mobile type
769 $phoneTypes = CRM_Core_OptionGroup
::values('phone_type', TRUE, FALSE, FALSE, NULL, 'name');
770 $mobileType = CRM_Utils_Array
::value('Mobile', $phoneTypes);
772 if ($name = CRM_Utils_Array
::value('name', $_GET)) {
773 $name = CRM_Utils_Type
::escape($name, 'String');
774 $queryString = " ( cc.sort_name LIKE '%$name%' OR cp.phone LIKE '%$name%' ) ";
776 elseif ($cid = CRM_Utils_Array
::value('cid', $_GET)) {
777 //check cid for interger
778 $contIDS = explode(',', $cid);
779 foreach ($contIDS as $contID) {
780 CRM_Utils_Type
::escape($contID, 'Integer');
782 $queryString = " cc.id IN ( $cid )";
786 $offset = CRM_Utils_Array
::value('offset', $_GET, 0);
787 $rowCount = CRM_Utils_Array
::value('rowcount', $_GET, 20);
789 $offset = CRM_Utils_Type
::escape($offset, 'Int');
790 $rowCount = CRM_Utils_Type
::escape($rowCount, 'Int');
792 // add acl clause here
793 list($aclFrom, $aclWhere) = CRM_Contact_BAO_Contact_Permission
::cacheClause('cc');
795 $aclWhere = " AND $aclWhere";
799 SELECT sort_name name, cp.phone, cc.id
800 FROM civicrm_phone cp INNER JOIN civicrm_contact cc ON cc.id = cp.contact_id
802 WHERE cc.is_deceased = 0 AND cc.do_not_sms = 0 AND cp.phone_type_id = {$mobileType} AND {$queryString}
804 LIMIT {$offset}, {$rowCount}
807 // send query to hook to be modified if needed
808 CRM_Utils_Hook
::contactListQuery($query,
810 CRM_Utils_Array
::value('context', $_GET),
811 CRM_Utils_Array
::value('cid', $_GET)
814 $dao = CRM_Core_DAO
::executeQuery($query);
816 while ($dao->fetch()) {
818 'name' => '"' . $dao->name
. '" <' . $dao->phone
. '>',
819 'id' => (CRM_Utils_Array
::value('id', $_GET)) ?
"{$dao->id}::{$dao->phone}" : '"' . $dao->name
. '" <' . $dao->phone
. '>',
825 echo json_encode($result);
827 CRM_Utils_System
::civiExit();
831 static function buildSubTypes() {
832 $parent = CRM_Utils_Array
::value('parentId', $_REQUEST);
836 $contactType = 'Individual';
840 $contactType = 'Household';
844 $contactType = 'Organization';
848 $subTypes = CRM_Contact_BAO_ContactType
::subTypePairs($contactType, FALSE, NULL);
850 echo json_encode($subTypes);
851 CRM_Utils_System
::civiExit();
854 static function buildDedupeRules() {
855 $parent = CRM_Utils_Array
::value('parentId', $_REQUEST);
859 $contactType = 'Individual';
863 $contactType = 'Household';
867 $contactType = 'Organization';
871 $dedupeRules = CRM_Dedupe_BAO_RuleGroup
::getByType($contactType);
873 echo json_encode($dedupeRules);
874 CRM_Utils_System
::civiExit();
878 * Function used for CiviCRM dashboard operations
880 static function dashboard() {
881 $operation = CRM_Utils_Type
::escape($_REQUEST['op'], 'String');
883 switch ($operation) {
884 case 'get_widgets_by_column':
885 // This would normally be coming from either the database (this user's settings) or a default/initial dashboard configuration.
886 // get contact id of logged in user
888 $dashlets = CRM_Core_BAO_Dashboard
::getContactDashlets();
892 $dashletID = CRM_Utils_Type
::escape($_GET['id'], 'Positive');
894 $dashlets = CRM_Core_BAO_Dashboard
::getDashletInfo($dashletID);
898 CRM_Core_BAO_Dashboard
::saveDashletChanges($_REQUEST['columns']);
899 CRM_Utils_System
::civiExit();
900 case 'delete_dashlet':
901 $dashletID = CRM_Utils_Type
::escape($_REQUEST['dashlet_id'], 'Positive');
902 CRM_Core_BAO_Dashboard
::deleteDashlet($dashletID);
903 CRM_Utils_System
::civiExit();
906 echo json_encode($dashlets);
907 CRM_Utils_System
::civiExit();
911 * Function to retrieve signature based on email id
913 static function getSignature() {
914 $emailID = CRM_Utils_Type
::escape($_REQUEST['emailID'], 'Positive');
915 $query = "SELECT signature_text, signature_html FROM civicrm_email WHERE id = {$emailID}";
916 $dao = CRM_Core_DAO
::executeQuery($query);
918 $signatures = array();
919 while ($dao->fetch()) {
921 'signature_text' => $dao->signature_text
,
922 'signature_html' => $dao->signature_html
,
926 echo json_encode($signatures);
927 CRM_Utils_System
::civiExit();
930 static function relationshipContacts() {
931 $searchValues = $searchRows = array();
934 $relType = CRM_Utils_Type
::escape($_REQUEST['relType'], 'String');
935 $typeName = isset($_REQUEST['typeName']) ? CRM_Utils_Type
::escape($_REQUEST['typeName'], 'String') : '';
936 $relContact = CRM_Utils_Type
::escape($_REQUEST['relContact'], 'String');
937 $currentContactId = CRM_Utils_Type
::escape($_REQUEST['cid'], 'Integer');
939 if (in_array($typeName, array(
940 'Employee of', 'Employer of'))) {
945 1 => 'sort_name', (2 +
$addCount) => 'city', (3 +
$addCount) => 'state_province',
946 (4 +
$addCount) => 'email', (5 +
$addCount) => 'phone',
949 $sEcho = CRM_Utils_Type
::escape($_REQUEST['sEcho'], 'Integer');
950 $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type
::escape($_REQUEST['iDisplayStart'], 'Integer') : 0;
951 $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type
::escape($_REQUEST['iDisplayLength'], 'Integer') : 25;
952 $sort = isset($_REQUEST['iSortCol_0']) ?
$sortMapper[CRM_Utils_Type
::escape($_REQUEST['iSortCol_0'], 'Integer')] : 'sort_name';
953 $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type
::escape($_REQUEST['sSortDir_0'], 'String') : 'asc';
955 $searchValues[] = array('sort_name', 'LIKE', $relContact, 0, 1);
957 list($rid, $direction) = explode('_', $relType, 2);
959 $relationshipType = new CRM_Contact_DAO_RelationshipType();
961 $relationshipType->id
= $rid;
962 if ($relationshipType->find(TRUE)) {
963 if ($direction == 'a_b') {
964 $type = $relationshipType->contact_type_b
;
965 $subType = $relationshipType->contact_sub_type_b
;
968 $type = $relationshipType->contact_type_a
;
969 $subType = $relationshipType->contact_sub_type_a
;
972 if ($type == 'Individual' ||
$type == 'Organization' ||
$type == 'Household') {
973 $searchValues[] = array('contact_type', '=', $type, 0, 0);
977 $searchValues[] = array('contact_sub_type', '=', $subType, 0, 0);
981 // exclude current contact
982 $searchValues[] = array('contact_id', '!=', $currentContactId, 0, 0);
984 $query = new CRM_Contact_BAO_Query($searchValues);
985 $searchCount = $query->searchQuery(0, 0, NULL, TRUE);
986 $iTotal = $searchCount;
988 if ($searchCount > 0) {
989 // get the result of the search
990 $result = $query->searchQuery($offset, $rowCount, $sort, FALSE, FALSE,
991 FALSE, FALSE, FALSE, NULL, $sortOrder
994 $config = CRM_Core_Config
::singleton();
996 while ($result->fetch()) {
997 $query->convertToPseudoNames($result);
998 $contactID = $result->contact_id
;
999 $typeImage = CRM_Contact_BAO_Contact_Utils
::getImage($result->contact_sub_type ?
1000 $result->contact_sub_type
: $result->contact_type
,
1004 $searchRows[$contactID]['id'] = $contactID;
1005 $searchRows[$contactID]['name'] = $typeImage . ' ' . $result->sort_name
;
1006 $searchRows[$contactID]['city'] = $result->city
;
1007 $searchRows[$contactID]['state'] = $result->state_province
;
1008 $searchRows[$contactID]['email'] = $result->email
;
1009 $searchRows[$contactID]['phone'] = $result->phone
;
1013 foreach ($searchRows as $cid => $row) {
1014 $searchRows[$cid]['check'] = '<input type="checkbox" id="contact_check[' . $cid . ']" name="contact_check[' . $cid . ']" value=' . $cid . ' />';
1016 if ($typeName == 'Employee of') {
1017 $searchRows[$cid]['employee_of'] = '<input type="radio" name="employee_of" value=' . $cid . ' >';
1019 elseif ($typeName == 'Employer of') {
1020 $searchRows[$cid]['employer_of'] = '<input type="checkbox" name="employer_of[' . $cid . ']" value=' . $cid . ' />';
1024 $selectorElements = array('check', 'name');
1025 if ($typeName == 'Employee of') {
1026 $selectorElements[] = 'employee_of';
1028 elseif ($typeName == 'Employer of') {
1029 $selectorElements[] = 'employer_of';
1031 $selectorElements = array_merge($selectorElements, array('city', 'state', 'email', 'phone'));
1033 $iFilteredTotal = $iTotal;
1034 echo CRM_Utils_JSON
::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
1035 CRM_Utils_System
::civiExit();
1039 * Function to process dupes.
1042 static function processDupes() {
1043 $oper = CRM_Utils_Type
::escape($_REQUEST['op'], 'String');
1044 $cid = CRM_Utils_Type
::escape($_REQUEST['cid'], 'Positive');
1045 $oid = CRM_Utils_Type
::escape($_REQUEST['oid'], 'Positive');
1047 if (!$oper ||
!$cid ||
!$oid) {
1051 $exception = new CRM_Dedupe_DAO_Exception();
1052 $exception->contact_id1
= $cid;
1053 $exception->contact_id2
= $oid;
1054 //make sure contact2 > contact1.
1056 $exception->contact_id1
= $oid;
1057 $exception->contact_id2
= $cid;
1059 $exception->find(TRUE);
1061 if ($oper == 'dupe-nondupe') {
1062 $status = $exception->save();
1064 if ($oper == 'nondupe-dupe') {
1065 $status = $exception->delete();
1068 echo json_encode(array('status' => ($status) ?
$oper : $status));
1069 CRM_Utils_System
::civiExit();
1072 static function getDedupes() {
1074 $sEcho = CRM_Utils_Type
::escape($_REQUEST['sEcho'], 'Integer');
1075 $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type
::escape($_REQUEST['iDisplayStart'], 'Integer') : 0;
1076 $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type
::escape($_REQUEST['iDisplayLength'], 'Integer') : 25;
1077 $sort = 'sort_name';
1078 $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type
::escape($_REQUEST['sSortDir_0'], 'String') : 'asc';
1080 $gid = isset($_REQUEST['gid']) ? CRM_Utils_Type
::escape($_REQUEST['gid'], 'Integer') : 0;
1081 $rgid = isset($_REQUEST['rgid']) ? CRM_Utils_Type
::escape($_REQUEST['rgid'], 'Integer') : 0;
1084 $contactType = CRM_Core_DAO
::getFieldValue('CRM_Dedupe_DAO_RuleGroup', $rgid, 'contact_type');
1087 $cacheKeyString = "merge {$contactType}_{$rgid}_{$gid}";
1088 $searchRows = array();
1089 $selectorElements = array('src', 'dst', 'weight', 'actions');
1092 $join = "LEFT JOIN civicrm_dedupe_exception de ON ( pn.entity_id1 = de.contact_id1 AND
1093 pn.entity_id2 = de.contact_id2 )";
1094 $where = "de.id IS NULL";
1096 $iFilteredTotal = $iTotal = CRM_Core_BAO_PrevNextCache
::getCount($cacheKeyString, $join, $where);
1097 $mainContacts = CRM_Core_BAO_PrevNextCache
::retrieve($cacheKeyString, $join, $where, $offset, $rowCount);
1099 foreach ($mainContacts as $mainId => $main) {
1100 $searchRows[$mainId]['src'] = CRM_Utils_System
::href($main['srcName'], 'civicrm/contact/view', "reset=1&cid={$main['srcID']}");
1101 $searchRows[$mainId]['dst'] = CRM_Utils_System
::href($main['dstName'], 'civicrm/contact/view', "reset=1&cid={$main['dstID']}");
1102 $searchRows[$mainId]['weight'] = CRM_Utils_Array
::value('weight', $main);
1104 if (CRM_Utils_Array
::value('canMerge', $main)) {
1105 $mergeParams = "reset=1&cid={$main['srcID']}&oid={$main['dstID']}&action=update&rgid={$rgid}";
1107 $mergeParams .= "&gid={$gid}";
1110 $searchRows[$mainId]['actions'] = CRM_Utils_System
::href(ts('merge'), 'civicrm/contact/merge', $mergeParams);
1111 $searchRows[$mainId]['actions'] .= " | <a id='notDuplicate' href='#' onClick=\"processDupes( {$main['srcID']}, {$main['dstID']}, 'dupe-nondupe', 'dupe-listing'); return false;\">" . ts('not a duplicate') . "</a>";
1114 $searchRows[$mainId]['actions'] = '<em>' . ts('Insufficient access rights - cannot merge') . '</em>';
1118 echo CRM_Utils_JSON
::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
1120 CRM_Utils_System
::civiExit();
1124 * Function to retrieve a PDF Page Format for the PDF Letter form
1126 function pdfFormat() {
1127 $formatId = CRM_Utils_Type
::escape($_REQUEST['formatId'], 'Integer');
1129 $pdfFormat = CRM_Core_BAO_PdfFormat
::getById($formatId);
1131 echo json_encode($pdfFormat);
1132 CRM_Utils_System
::civiExit();
1136 * Function to retrieve Paper Size dimensions
1138 static function paperSize() {
1139 $paperSizeName = CRM_Utils_Type
::escape($_REQUEST['paperSizeName'], 'String');
1141 $paperSize = CRM_Core_BAO_PaperSize
::getByName($paperSizeName);
1143 echo json_encode($paperSize);
1144 CRM_Utils_System
::civiExit();
1147 static function relationshipContactTypeList() {
1148 $relType = CRM_Utils_Array
::value('relType', $_REQUEST);
1150 $types = CRM_Contact_BAO_Relationship
::getValidContactTypeList($relType);
1152 $elements = array();
1153 foreach ($types as $key => $label) {
1154 $elements[] = array(
1160 echo json_encode($elements);
1161 CRM_Utils_System
::civiExit();
1164 static function selectUnselectContacts() {
1165 $name = CRM_Utils_Array
::value('name', $_REQUEST);
1166 $cacheKey = CRM_Utils_Array
::value('qfKey', $_REQUEST);
1167 $state = CRM_Utils_Array
::value('state', $_REQUEST, 'checked');
1168 $variableType = CRM_Utils_Array
::value('variableType', $_REQUEST, 'single');
1170 $actionToPerform = CRM_Utils_Array
::value('action', $_REQUEST, 'select');
1172 if ($variableType == 'multiple') {
1173 // action post value only works with multiple type variable
1175 //multiple names like mark_x_1-mark_x_2 where 1,2 are cids
1176 $elements = explode('-', $name);
1177 foreach ($elements as $key => $element) {
1178 $elements[$key] = self
::_convertToId($element);
1180 CRM_Core_BAO_PrevNextCache
::markSelection($cacheKey, $actionToPerform, $elements);
1183 CRM_Core_BAO_PrevNextCache
::markSelection($cacheKey, $actionToPerform);
1186 elseif ($variableType == 'single') {
1187 $cId = self
::_convertToId($name);
1188 $action = ($state == 'checked') ?
'select' : 'unselect';
1189 CRM_Core_BAO_PrevNextCache
::markSelection($cacheKey, $action, $cId);
1191 $contactIds = CRM_Core_BAO_PrevNextCache
::getSelection($cacheKey);
1192 $countSelectionCids = count($contactIds[$cacheKey]);
1194 $arrRet = array('getCount' => $countSelectionCids);
1195 echo json_encode($arrRet);
1196 CRM_Utils_System
::civiExit();
1199 static function _convertToId($name) {
1200 if (substr($name, 0, CRM_Core_Form
::CB_PREFIX_LEN
) == CRM_Core_Form
::CB_PREFIX
) {
1201 $cId = substr($name, CRM_Core_Form
::CB_PREFIX_LEN
);
1206 static function getAddressDisplay() {
1207 $contactId = CRM_Utils_Array
::value('contact_id', $_REQUEST);
1209 $addressVal["error_message"] = "no contact id found";
1214 'contact_id' => $contactId,
1215 'entity_id' => $contactId,
1217 $addressVal = CRM_Core_BAO_Address
::getValues($entityBlock);
1220 echo json_encode($addressVal);
1221 CRM_Utils_System
::civiExit();