Merge pull request #6004 from colemanw/CRM-16310
[civicrm-core.git] / CRM / Contact / Page / AJAX.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 *
33 */
34
35/**
36 * This class contains all contact related functions that are called using AJAX (jQuery)
37 */
38class CRM_Contact_Page_AJAX {
272081ca
TO
39 /**
40 * When a user chooses a username, CHECK_USERNAME_TTL
41 * is the time window in which they can check usernames
42 * (without reloading the overall form).
43 */
44 const CHECK_USERNAME_TTL = 10800; // 3hr; 3*60*60
45
b1dba111
TO
46 const AUTOCOMPLETE_TTL = 21600; // 6hr; 6*60*60
47
06508628 48 /**
06508628
CW
49 * Ajax callback for custom fields of type ContactReference
50 *
51 * Todo: Migrate contact reference fields to use EntityRef
52 */
00be9182 53 public static function contactReference() {
06508628 54 $name = CRM_Utils_Array::value('term', $_GET);
6a488035
TO
55 $name = CRM_Utils_Type::escape($name, 'String');
56 $cfID = CRM_Utils_Type::escape($_GET['id'], 'Positive');
57
58 // check that this is a valid, active custom field of Contact Reference type
06508628 59 $params = array('id' => $cfID);
6a488035 60 $returnProperties = array('filter', 'data_type', 'is_active');
06508628 61 $cf = array();
6a488035 62 CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_CustomField', $params, $cf, $returnProperties);
a4cce21a 63 if (!$cf['id'] || !$cf['is_active'] || $cf['data_type'] != 'ContactReference') {
06508628 64 CRM_Utils_System::civiExit('error');
6a488035
TO
65 }
66
9624538c 67 if (!empty($cf['filter'])) {
6a488035
TO
68 $filterParams = array();
69 parse_str($cf['filter'], $filterParams);
70
71 $action = CRM_Utils_Array::value('action', $filterParams);
72
73 if (!empty($action) &&
74 !in_array($action, array('get', 'lookup'))
75 ) {
06508628 76 CRM_Utils_System::civiExit('error');
6a488035
TO
77 }
78 }
79
80 $list = array_keys(CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
353ffa53
TO
81 'contact_reference_options'
82 ), '1');
6a488035
TO
83
84 $return = array_unique(array_merge(array('sort_name'), $list));
85
06508628 86 $limit = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'search_autocomplete_count', NULL, 10);
6a488035
TO
87
88 $params = array('offset' => 0, 'rowCount' => $limit, 'version' => 3);
89 foreach ($return as $fld) {
90 $params["return.{$fld}"] = 1;
91 }
92
93 if (!empty($action)) {
353ffa53
TO
94 $excludeGet = array(
95 'reset',
96 'key',
97 'className',
98 'fnName',
99 'json',
100 'reset',
101 'context',
102 'timestamp',
103 'limit',
104 'id',
105 's',
106 'q',
408b79bf 107 'action',
353ffa53 108 );
6a488035
TO
109 foreach ($_GET as $param => $val) {
110 if (empty($val) ||
111 in_array($param, $excludeGet) ||
112 strpos($param, 'return.') !== FALSE ||
113 strpos($param, 'api.') !== FALSE
114 ) {
115 continue;
116 }
117 $params[$param] = $val;
118 }
119 }
120
121 if ($name) {
122 $params['sort_name'] = $name;
123 }
124
125 $params['sort'] = 'sort_name';
126
127 // tell api to skip permission chk. dgg
128 $params['check_permissions'] = 0;
129
130 // add filter variable to params
131 if (!empty($filterParams)) {
132 $params = array_merge($params, $filterParams);
133 }
134
135 $contact = civicrm_api('Contact', 'Get', $params);
136
a7488080 137 if (!empty($contact['is_error'])) {
06508628 138 CRM_Utils_System::civiExit('error');
6a488035
TO
139 }
140
d6408252 141 $contactList = array();
6a488035
TO
142 foreach ($contact['values'] as $value) {
143 $view = array();
144 foreach ($return as $fld) {
a7488080 145 if (!empty($value[$fld])) {
6a488035
TO
146 $view[] = $value[$fld];
147 }
148 }
06508628 149 $contactList[] = array('id' => $value['id'], 'text' => implode(' :: ', $view));
6a488035
TO
150 }
151
8be1a839 152 CRM_Utils_JSON::output($contactList);
6a488035
TO
153 }
154
155 /**
100fef9d 156 * Fetch PCP ID by PCP Supporter sort_name, also displays PCP title and associated Contribution Page title
6a488035 157 */
00be9182 158 public static function getPCPList() {
7d86179d 159 $name = CRM_Utils_Array::value('term', $_GET);
353ffa53 160 $name = CRM_Utils_Type::escape($name, 'String');
6a488035
TO
161 $limit = '10';
162
163 $where = ' AND pcp.page_id = cp.id AND pcp.contact_id = cc.id';
164
165 $config = CRM_Core_Config::singleton();
166 if ($config->includeWildCardInName) {
167 $strSearch = "%$name%";
168 }
169 else {
170 $strSearch = "$name%";
171 }
172 $includeEmailFrom = $includeNickName = '';
173 if ($config->includeNickNameInName) {
174 $includeNickName = " OR nick_name LIKE '$strSearch'";
175 }
176 if ($config->includeEmailInName) {
177 $includeEmailFrom = "LEFT JOIN civicrm_email eml ON ( cc.id = eml.contact_id AND eml.is_primary = 1 )";
178 $whereClause = " WHERE ( email LIKE '$strSearch' OR sort_name LIKE '$strSearch' $includeNickName ) {$where} ";
179 }
180 else {
181 $whereClause = " WHERE ( sort_name LIKE '$strSearch' $includeNickName ) {$where} ";
182 }
183
a7488080 184 if (!empty($_GET['limit'])) {
6a488035
TO
185 $limit = CRM_Utils_Type::escape($_GET['limit'], 'Positive');
186 }
187
188 $select = 'cc.sort_name, pcp.title, cp.title';
189 $query = "
190 SELECT id, data
191 FROM (
192 SELECT pcp.id as id, CONCAT_WS( ' :: ', {$select} ) as data, sort_name
193 FROM civicrm_pcp pcp, civicrm_contribution_page cp, civicrm_contact cc
194 {$includeEmailFrom}
900e6829 195 {$whereClause} AND pcp.page_type = 'contribute'
196 UNION ALL
197 SELECT pcp.id as id, CONCAT_WS( ' :: ', {$select} ) as data, sort_name
198 FROM civicrm_pcp pcp, civicrm_event cp, civicrm_contact cc
199 {$includeEmailFrom}
200 {$whereClause} AND pcp.page_type = 'event'
6a488035
TO
201 LIMIT 0, {$limit}
202 ) t
203 ORDER BY sort_name
204 ";
205
206 $dao = CRM_Core_DAO::executeQuery($query);
d6408252 207 $results = array();
6a488035 208 while ($dao->fetch()) {
6bb107ce 209 $results[] = array('id' => $dao->id, 'text' => $dao->data);
6a488035 210 }
ecdef330 211 CRM_Utils_JSON::output($results);
6a488035
TO
212 }
213
00be9182 214 public static function relationship() {
c91df8b4
CW
215 $relType = CRM_Utils_Request::retrieve('rel_type', 'Positive', CRM_Core_DAO::$_nullObject, TRUE);
216 $relContactID = CRM_Utils_Request::retrieve('rel_contact', 'Positive', CRM_Core_DAO::$_nullObject, TRUE);
217 $relationshipID = CRM_Utils_Array::value('rel_id', $_REQUEST); // this used only to determine add or update mode
218 $caseID = CRM_Utils_Request::retrieve('case_id', 'Positive', CRM_Core_DAO::$_nullObject, TRUE);
6a488035 219
14a679f1
KJ
220 // check if there are multiple clients for this case, if so then we need create
221 // relationship and also activities for each contacts
6a488035 222
14a679f1
KJ
223 // get case client list
224 $clientList = CRM_Case_BAO_Case::getCaseClients($caseID);
6a488035 225
c91df8b4
CW
226 $ret = array('is_error' => 0);
227
22e263ad 228 foreach ($clientList as $sourceContactID) {
14a679f1
KJ
229 $relationParams = array(
230 'relationship_type_id' => $relType . '_a_b',
231 'contact_check' => array($relContactID => 1),
232 'is_active' => 1,
233 'case_id' => $caseID,
234 'start_date' => date("Ymd"),
6a488035
TO
235 );
236
14a679f1
KJ
237 $relationIds = array('contact' => $sourceContactID);
238
239 // check if we are editing/updating existing relationship
240 if ($relationshipID && $relationshipID != 'null') {
241 // here we need to retrieve appropriate relationshipID based on client id and relationship type id
242 $caseRelationships = new CRM_Contact_DAO_Relationship();
243 $caseRelationships->case_id = $caseID;
244 $caseRelationships->relationship_type_id = $relType;
245 $caseRelationships->contact_id_a = $sourceContactID;
246 $caseRelationships->find();
247
22e263ad 248 while ($caseRelationships->fetch()) {
14a679f1
KJ
249 $relationIds['relationship'] = $caseRelationships->id;
250 $relationIds['contactTarget'] = $relContactID;
251 }
252 $caseRelationships->free();
253 }
254
255 // create new or update existing relationship
2da59b29 256 $return = CRM_Contact_BAO_Relationship::legacyCreateMultiple($relationParams, $relationIds);
14a679f1 257
a7488080 258 if (!empty($return[4][0])) {
14a679f1 259 $relationshipID = $return[4][0];
14a679f1
KJ
260
261 //create an activity for case role assignment.CRM-4480
262 CRM_Case_BAO_Case::createCaseRoleActivity($caseID, $relationshipID, $relContactID);
263 }
c91df8b4
CW
264 else {
265 $ret = array(
266 'is_error' => 1,
267 'error_message' => ts('The relationship type definition for the case role is not valid for the client and / or staff contact types. You can review and edit relationship types at <a href="%1">Administer >> Option Lists >> Relationship Types</a>.',
21dfd5f5 268 array(1 => CRM_Utils_System::url('civicrm/admin/reltype', 'reset=1'))),
c91df8b4
CW
269 );
270 }
6a488035 271 }
6a488035 272
ecdef330 273 CRM_Utils_JSON::output($ret);
6a488035
TO
274 }
275
276 /**
fe482240 277 * Fetch the custom field help.
6a488035 278 */
00be9182 279 public static function customField() {
353ffa53
TO
280 $fieldId = CRM_Utils_Type::escape($_REQUEST['id'], 'Integer');
281 $params = array('id' => $fieldId);
6a488035 282 $returnProperties = array('help_pre', 'help_post');
353ffa53 283 $values = array();
6a488035
TO
284
285 CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_CustomField', $params, $values, $returnProperties);
ecdef330 286 CRM_Utils_JSON::output($values);
6a488035
TO
287 }
288
00be9182 289 public static function groupTree() {
8be1a839 290 header('Content-Type: application/json');
6a488035
TO
291 $gids = CRM_Utils_Type::escape($_GET['gids'], 'String');
292 echo CRM_Contact_BAO_GroupNestingCache::json($gids);
293 CRM_Utils_System::civiExit();
294 }
295
6a488035 296 /**
fe482240 297 * Delete custom value.
6a488035 298 */
00be9182 299 public static function deleteCustomValue() {
effdc2d9 300 header('Content-Type: text/plain');
6a488035
TO
301 $customValueID = CRM_Utils_Type::escape($_REQUEST['valueID'], 'Positive');
302 $customGroupID = CRM_Utils_Type::escape($_REQUEST['groupID'], 'Positive');
303
304 CRM_Core_BAO_CustomValue::deleteCustomValue($customValueID, $customGroupID);
a4cce21a
JM
305 $contactId = CRM_Utils_Array::value('contactId', $_REQUEST);
306 if ($contactId) {
6a488035
TO
307 echo CRM_Contact_BAO_Contact::getCountComponent('custom_' . $_REQUEST['groupID'], $contactId);
308 }
309
310 // reset the group contact cache for this group
311 CRM_Contact_BAO_GroupContactCache::remove();
312 CRM_Utils_System::civiExit();
313 }
314
6a488035 315 /**
fe482240 316 * check the CMS username.
ce80b209 317 */
6a488035 318 static public function checkUserName() {
272081ca
TO
319 $signer = new CRM_Utils_Signer(CRM_Core_Key::privateKey(), array('for', 'ts'));
320 if (
321 CRM_Utils_Time::getTimeRaw() > $_REQUEST['ts'] + self::CHECK_USERNAME_TTL
322 || $_REQUEST['for'] != 'civicrm/ajax/cmsuser'
323 || !$signer->validate($_REQUEST['sig'], $_REQUEST)
324 ) {
325 $user = array('name' => 'error');
8be1a839 326 CRM_Utils_JSON::output($user);
272081ca
TO
327 }
328
6a488035
TO
329 $config = CRM_Core_Config::singleton();
330 $username = trim($_REQUEST['cms_name']);
331
332 $params = array('name' => $username);
333
334 $errors = array();
335 $config->userSystem->checkUserNameEmailExists($params, $errors);
336
337 if (isset($errors['cms_name']) || isset($errors['name'])) {
338 //user name is not availble
339 $user = array('name' => 'no');
8be1a839 340 CRM_Utils_JSON::output($user);
6a488035
TO
341 }
342 else {
343 //user name is available
344 $user = array('name' => 'yes');
8be1a839 345 CRM_Utils_JSON::output($user);
6a488035 346 }
8be1a839
TO
347
348 // Not reachable: JSON::output() above exits.
6a488035
TO
349 CRM_Utils_System::civiExit();
350 }
351
352 /**
fe482240 353 * Function to get email address of a contact.
6a488035 354 */
00be9182 355 public static function getContactEmail() {
a7488080 356 if (!empty($_REQUEST['contact_id'])) {
6a488035 357 $contactID = CRM_Utils_Type::escape($_REQUEST['contact_id'], 'Positive');
8c0ea1d7
TO
358 if (!CRM_Contact_BAO_Contact_Permission::allow($contactID, CRM_Core_Permission::EDIT)) {
359 return;
360 }
6a488035
TO
361 list($displayName,
362 $userEmail
353ffa53 363 ) = CRM_Contact_BAO_Contact_Location::getEmailDetails($contactID);
effdc2d9
TO
364
365 header('Content-Type: text/plain');
6a488035
TO
366 if ($userEmail) {
367 echo $userEmail;
368 }
369 }
370 else {
371 $noemail = CRM_Utils_Array::value('noemail', $_GET);
372 $queryString = NULL;
a4cce21a
JM
373 $name = CRM_Utils_Array::value('name', $_GET);
374 if ($name) {
6a488035
TO
375 $name = CRM_Utils_Type::escape($name, 'String');
376 if ($noemail) {
377 $queryString = " cc.sort_name LIKE '%$name%'";
378 }
379 else {
380 $queryString = " ( cc.sort_name LIKE '%$name%' OR ce.email LIKE '%$name%' ) ";
381 }
382 }
a4cce21a 383 else {
d75f2f47
EM
384 $cid = CRM_Utils_Array::value('cid', $_GET);
385 if ($cid) {
a4cce21a
JM
386 //check cid for interger
387 $contIDS = explode(',', $cid);
388 foreach ($contIDS as $contID) {
389 CRM_Utils_Type::escape($contID, 'Integer');
390 }
391 $queryString = " cc.id IN ( $cid )";
d75f2f47 392 }
6a488035
TO
393 }
394
395 if ($queryString) {
396 $offset = CRM_Utils_Array::value('offset', $_GET, 0);
cac1236c 397 $rowCount = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'search_autocomplete_count', NULL, 10);
6a488035 398
bf00d1b6 399 $offset = CRM_Utils_Type::escape($offset, 'Int');
bf00d1b6 400
6a488035
TO
401 // add acl clause here
402 list($aclFrom, $aclWhere) = CRM_Contact_BAO_Contact_Permission::cacheClause('cc');
403 if ($aclWhere) {
404 $aclWhere = " AND $aclWhere";
405 }
406 if ($noemail) {
407 $query = "
408SELECT sort_name name, cc.id
409FROM civicrm_contact cc
410 {$aclFrom}
411WHERE cc.is_deceased = 0 AND {$queryString}
412 {$aclWhere}
413LIMIT {$offset}, {$rowCount}
414";
415
416 // send query to hook to be modified if needed
417 CRM_Utils_Hook::contactListQuery($query,
418 $name,
419 CRM_Utils_Array::value('context', $_GET),
420 CRM_Utils_Array::value('cid', $_GET)
421 );
422
423 $dao = CRM_Core_DAO::executeQuery($query);
424 while ($dao->fetch()) {
425 $result[] = array(
6a488035 426 'id' => $dao->id,
cac1236c 427 'text' => $dao->name,
6a488035
TO
428 );
429 }
430 }
431 else {
432 $query = "
433SELECT sort_name name, ce.email, cc.id
434FROM civicrm_email ce INNER JOIN civicrm_contact cc ON cc.id = ce.contact_id
435 {$aclFrom}
436WHERE ce.on_hold = 0 AND cc.is_deceased = 0 AND cc.do_not_email = 0 AND {$queryString}
437 {$aclWhere}
438LIMIT {$offset}, {$rowCount}
439";
440
441 // send query to hook to be modified if needed
442 CRM_Utils_Hook::contactListQuery($query,
443 $name,
444 CRM_Utils_Array::value('context', $_GET),
445 CRM_Utils_Array::value('cid', $_GET)
446 );
447
6a488035
TO
448 $dao = CRM_Core_DAO::executeQuery($query);
449
450 while ($dao->fetch()) {
ce80b209 451 //working here
6a488035 452 $result[] = array(
cac1236c 453 'text' => '"' . $dao->name . '" <' . $dao->email . '>',
6a488035
TO
454 'id' => (CRM_Utils_Array::value('id', $_GET)) ? "{$dao->id}::{$dao->email}" : '"' . $dao->name . '" <' . $dao->email . '>',
455 );
456 }
457 }
6a488035 458 if ($result) {
8be1a839 459 CRM_Utils_JSON::output($result);
6a488035
TO
460 }
461 }
462 }
463 CRM_Utils_System::civiExit();
464 }
465
00be9182 466 public static function getContactPhone() {
6a488035
TO
467
468 $queryString = NULL;
469 //check for mobile type
470 $phoneTypes = CRM_Core_OptionGroup::values('phone_type', TRUE, FALSE, FALSE, NULL, 'name');
471 $mobileType = CRM_Utils_Array::value('Mobile', $phoneTypes);
472
a4cce21a
JM
473 $name = CRM_Utils_Array::value('name', $_GET);
474 if ($name) {
6a488035
TO
475 $name = CRM_Utils_Type::escape($name, 'String');
476 $queryString = " ( cc.sort_name LIKE '%$name%' OR cp.phone LIKE '%$name%' ) ";
477 }
a4cce21a 478 else {
d75f2f47
EM
479 $cid = CRM_Utils_Array::value('cid', $_GET);
480 if ($cid) {
a4cce21a
JM
481 //check cid for interger
482 $contIDS = explode(',', $cid);
483 foreach ($contIDS as $contID) {
484 CRM_Utils_Type::escape($contID, 'Integer');
485 }
486 $queryString = " cc.id IN ( $cid )";
6a488035 487 }
6a488035
TO
488 }
489
490 if ($queryString) {
491 $offset = CRM_Utils_Array::value('offset', $_GET, 0);
492 $rowCount = CRM_Utils_Array::value('rowcount', $_GET, 20);
493
bf00d1b6
DL
494 $offset = CRM_Utils_Type::escape($offset, 'Int');
495 $rowCount = CRM_Utils_Type::escape($rowCount, 'Int');
496
6a488035
TO
497 // add acl clause here
498 list($aclFrom, $aclWhere) = CRM_Contact_BAO_Contact_Permission::cacheClause('cc');
499 if ($aclWhere) {
500 $aclWhere = " AND $aclWhere";
501 }
502
503 $query = "
504SELECT sort_name name, cp.phone, cc.id
505FROM civicrm_phone cp INNER JOIN civicrm_contact cc ON cc.id = cp.contact_id
506 {$aclFrom}
507WHERE cc.is_deceased = 0 AND cc.do_not_sms = 0 AND cp.phone_type_id = {$mobileType} AND {$queryString}
508 {$aclWhere}
509LIMIT {$offset}, {$rowCount}
510";
511
512 // send query to hook to be modified if needed
513 CRM_Utils_Hook::contactListQuery($query,
514 $name,
515 CRM_Utils_Array::value('context', $_GET),
516 CRM_Utils_Array::value('cid', $_GET)
517 );
518
519 $dao = CRM_Core_DAO::executeQuery($query);
520
521 while ($dao->fetch()) {
522 $result[] = array(
b792e485 523 'text' => '"' . $dao->name . '" (' . $dao->phone . ')',
6a488035
TO
524 'id' => (CRM_Utils_Array::value('id', $_GET)) ? "{$dao->id}::{$dao->phone}" : '"' . $dao->name . '" <' . $dao->phone . '>',
525 );
526 }
527 }
528
529 if ($result) {
8be1a839 530 CRM_Utils_JSON::output($result);
6a488035
TO
531 }
532 CRM_Utils_System::civiExit();
533 }
534
535
00be9182 536 public static function buildSubTypes() {
6a488035
TO
537 $parent = CRM_Utils_Array::value('parentId', $_REQUEST);
538
539 switch ($parent) {
540 case 1:
541 $contactType = 'Individual';
542 break;
543
544 case 2:
545 $contactType = 'Household';
546 break;
547
548 case 4:
549 $contactType = 'Organization';
550 break;
551 }
552
553 $subTypes = CRM_Contact_BAO_ContactType::subTypePairs($contactType, FALSE, NULL);
554 asort($subTypes);
ecdef330 555 CRM_Utils_JSON::output($subTypes);
6a488035
TO
556 }
557
00be9182 558 public static function buildDedupeRules() {
6a488035
TO
559 $parent = CRM_Utils_Array::value('parentId', $_REQUEST);
560
561 switch ($parent) {
562 case 1:
563 $contactType = 'Individual';
564 break;
565
566 case 2:
567 $contactType = 'Household';
568 break;
569
570 case 4:
571 $contactType = 'Organization';
572 break;
573 }
574
575 $dedupeRules = CRM_Dedupe_BAO_RuleGroup::getByType($contactType);
576
ecdef330 577 CRM_Utils_JSON::output($dedupeRules);
6a488035
TO
578 }
579
580 /**
fe482240 581 * Function used for CiviCRM dashboard operations.
6a488035 582 */
00be9182 583 public static function dashboard() {
6a488035
TO
584 $operation = CRM_Utils_Type::escape($_REQUEST['op'], 'String');
585
586 switch ($operation) {
587 case 'get_widgets_by_column':
588 // This would normally be coming from either the database (this user's settings) or a default/initial dashboard configuration.
589 // get contact id of logged in user
590
591 $dashlets = CRM_Core_BAO_Dashboard::getContactDashlets();
592 break;
593
594 case 'get_widget':
595 $dashletID = CRM_Utils_Type::escape($_GET['id'], 'Positive');
596
597 $dashlets = CRM_Core_BAO_Dashboard::getDashletInfo($dashletID);
598 break;
599
600 case 'save_columns':
601 CRM_Core_BAO_Dashboard::saveDashletChanges($_REQUEST['columns']);
602 CRM_Utils_System::civiExit();
603 case 'delete_dashlet':
604 $dashletID = CRM_Utils_Type::escape($_REQUEST['dashlet_id'], 'Positive');
605 CRM_Core_BAO_Dashboard::deleteDashlet($dashletID);
606 CRM_Utils_System::civiExit();
607 }
608
ecdef330 609 CRM_Utils_JSON::output($dashlets);
6a488035
TO
610 }
611
612 /**
fe482240 613 * Retrieve signature based on email id.
6a488035 614 */
00be9182 615 public static function getSignature() {
6a488035 616 $emailID = CRM_Utils_Type::escape($_REQUEST['emailID'], 'Positive');
353ffa53
TO
617 $query = "SELECT signature_text, signature_html FROM civicrm_email WHERE id = {$emailID}";
618 $dao = CRM_Core_DAO::executeQuery($query);
6a488035
TO
619
620 $signatures = array();
621 while ($dao->fetch()) {
622 $signatures = array(
623 'signature_text' => $dao->signature_text,
624 'signature_html' => $dao->signature_html,
625 );
626 }
627
ecdef330 628 CRM_Utils_JSON::output($signatures);
6a488035
TO
629 }
630
631 /**
100fef9d 632 * Process dupes.
6a488035 633 */
00be9182 634 public static function processDupes() {
6a488035 635 $oper = CRM_Utils_Type::escape($_REQUEST['op'], 'String');
353ffa53
TO
636 $cid = CRM_Utils_Type::escape($_REQUEST['cid'], 'Positive');
637 $oid = CRM_Utils_Type::escape($_REQUEST['oid'], 'Positive');
6a488035
TO
638
639 if (!$oper || !$cid || !$oid) {
640 return;
641 }
642
643 $exception = new CRM_Dedupe_DAO_Exception();
644 $exception->contact_id1 = $cid;
645 $exception->contact_id2 = $oid;
646 //make sure contact2 > contact1.
647 if ($cid > $oid) {
648 $exception->contact_id1 = $oid;
649 $exception->contact_id2 = $cid;
650 }
651 $exception->find(TRUE);
652 $status = NULL;
653 if ($oper == 'dupe-nondupe') {
654 $status = $exception->save();
655 }
656 if ($oper == 'nondupe-dupe') {
657 $status = $exception->delete();
658 }
659
ecdef330 660 CRM_Utils_JSON::output(array('status' => ($status) ? $oper : $status));
6a488035
TO
661 }
662
00be9182 663 public static function getDedupes() {
6a488035 664
353ffa53
TO
665 $sEcho = CRM_Utils_Type::escape($_REQUEST['sEcho'], 'Integer');
666 $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayStart'], 'Integer') : 0;
667 $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayLength'], 'Integer') : 25;
668 $sort = 'sort_name';
6a488035
TO
669 $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type::escape($_REQUEST['sSortDir_0'], 'String') : 'asc';
670
353ffa53
TO
671 $gid = isset($_REQUEST['gid']) ? CRM_Utils_Type::escape($_REQUEST['gid'], 'Integer') : 0;
672 $rgid = isset($_REQUEST['rgid']) ? CRM_Utils_Type::escape($_REQUEST['rgid'], 'Integer') : 0;
6a488035
TO
673 $contactType = '';
674 if ($rgid) {
675 $contactType = CRM_Core_DAO::getFieldValue('CRM_Dedupe_DAO_RuleGroup', $rgid, 'contact_type');
676 }
677
353ffa53
TO
678 $cacheKeyString = "merge {$contactType}_{$rgid}_{$gid}";
679 $searchRows = array();
6a488035
TO
680 $selectorElements = array('src', 'dst', 'weight', 'actions');
681
6a488035
TO
682 $join = "LEFT JOIN civicrm_dedupe_exception de ON ( pn.entity_id1 = de.contact_id1 AND
683 pn.entity_id2 = de.contact_id2 )";
684 $where = "de.id IS NULL";
685
686 $iFilteredTotal = $iTotal = CRM_Core_BAO_PrevNextCache::getCount($cacheKeyString, $join, $where);
687 $mainContacts = CRM_Core_BAO_PrevNextCache::retrieve($cacheKeyString, $join, $where, $offset, $rowCount);
688
689 foreach ($mainContacts as $mainId => $main) {
690 $searchRows[$mainId]['src'] = CRM_Utils_System::href($main['srcName'], 'civicrm/contact/view', "reset=1&cid={$main['srcID']}");
691 $searchRows[$mainId]['dst'] = CRM_Utils_System::href($main['dstName'], 'civicrm/contact/view', "reset=1&cid={$main['dstID']}");
692 $searchRows[$mainId]['weight'] = CRM_Utils_Array::value('weight', $main);
693
a7488080 694 if (!empty($main['canMerge'])) {
6a488035
TO
695 $mergeParams = "reset=1&cid={$main['srcID']}&oid={$main['dstID']}&action=update&rgid={$rgid}";
696 if ($gid) {
697 $mergeParams .= "&gid={$gid}";
698 }
699
0d75c29c 700 $searchRows[$mainId]['actions'] = '<a class="action-item crm-hover-button" href="' . CRM_Utils_System::url('civicrm/contact/merge', $mergeParams) . '">' . ts('merge') . '</a>';
aeb97cc1 701 $searchRows[$mainId]['actions'] .= "<a class='action-item crm-hover-button crm-notDuplicate' href='#' onClick=\"processDupes( {$main['srcID']}, {$main['dstID']}, 'dupe-nondupe', 'dupe-listing'); return false;\">" . ts('not a duplicate') . "</a>";
6a488035
TO
702 }
703 else {
704 $searchRows[$mainId]['actions'] = '<em>' . ts('Insufficient access rights - cannot merge') . '</em>';
705 }
706 }
707
70dd31e2 708 header('Content-Type: application/json');
6a488035
TO
709 echo CRM_Utils_JSON::encodeDataTableSelector($searchRows, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
710
711 CRM_Utils_System::civiExit();
712 }
713
714 /**
fe482240 715 * Retrieve a PDF Page Format for the PDF Letter form.
6a488035 716 */
00be9182 717 public function pdfFormat() {
6a488035
TO
718 $formatId = CRM_Utils_Type::escape($_REQUEST['formatId'], 'Integer');
719
720 $pdfFormat = CRM_Core_BAO_PdfFormat::getById($formatId);
721
ecdef330 722 CRM_Utils_JSON::output($pdfFormat);
6a488035
TO
723 }
724
725 /**
fe482240 726 * Retrieve Paper Size dimensions.
6a488035 727 */
00be9182 728 public static function paperSize() {
6a488035
TO
729 $paperSizeName = CRM_Utils_Type::escape($_REQUEST['paperSizeName'], 'String');
730
731 $paperSize = CRM_Core_BAO_PaperSize::getByName($paperSizeName);
732
ecdef330 733 CRM_Utils_JSON::output($paperSize);
6a488035
TO
734 }
735
aeb97cc1
CW
736 /**
737 * Used to store selected contacts across multiple pages in advanced search.
738 */
00be9182 739 public static function selectUnselectContacts() {
353ffa53
TO
740 $name = CRM_Utils_Array::value('name', $_REQUEST);
741 $cacheKey = CRM_Utils_Array::value('qfKey', $_REQUEST);
742 $state = CRM_Utils_Array::value('state', $_REQUEST, 'checked');
6a488035
TO
743 $variableType = CRM_Utils_Array::value('variableType', $_REQUEST, 'single');
744
745 $actionToPerform = CRM_Utils_Array::value('action', $_REQUEST, 'select');
746
747 if ($variableType == 'multiple') {
748 // action post value only works with multiple type variable
749 if ($name) {
750 //multiple names like mark_x_1-mark_x_2 where 1,2 are cids
751 $elements = explode('-', $name);
752 foreach ($elements as $key => $element) {
753 $elements[$key] = self::_convertToId($element);
754 }
755 CRM_Core_BAO_PrevNextCache::markSelection($cacheKey, $actionToPerform, $elements);
756 }
757 else {
758 CRM_Core_BAO_PrevNextCache::markSelection($cacheKey, $actionToPerform);
759 }
760 }
761 elseif ($variableType == 'single') {
762 $cId = self::_convertToId($name);
763 $action = ($state == 'checked') ? 'select' : 'unselect';
764 CRM_Core_BAO_PrevNextCache::markSelection($cacheKey, $action, $cId);
765 }
766 $contactIds = CRM_Core_BAO_PrevNextCache::getSelection($cacheKey);
767 $countSelectionCids = count($contactIds[$cacheKey]);
768
769 $arrRet = array('getCount' => $countSelectionCids);
ecdef330 770 CRM_Utils_JSON::output($arrRet);
6a488035
TO
771 }
772
4319322b 773 /**
100fef9d 774 * @param string $name
4319322b
EM
775 *
776 * @return string
777 */
00be9182 778 public static function _convertToId($name) {
6a488035
TO
779 if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
780 $cId = substr($name, CRM_Core_Form::CB_PREFIX_LEN);
781 }
782 return $cId;
783 }
784
00be9182 785 public static function getAddressDisplay() {
6a488035
TO
786 $contactId = CRM_Utils_Array::value('contact_id', $_REQUEST);
787 if (!$contactId) {
788 $addressVal["error_message"] = "no contact id found";
789 }
790 else {
408b79bf 791 $entityBlock = array(
792 'contact_id' => $contactId,
793 'entity_id' => $contactId,
794 );
6a488035
TO
795 $addressVal = CRM_Core_BAO_Address::getValues($entityBlock);
796 }
797
ecdef330 798 CRM_Utils_JSON::output($addressVal);
6a488035 799 }
40458f6c 800
801 /**
fe482240 802 * Retrieve contact relationships.
40458f6c 803 */
804 public static function getContactRelationships() {
805 $contactID = CRM_Utils_Type::escape($_GET['cid'], 'Integer');
806 $context = CRM_Utils_Type::escape($_GET['context'], 'String');
1b09aa03
EM
807 $relationship_type_id = CRM_Utils_Type::escape(CRM_Utils_Array::value('relationship_type_id', $_GET), 'Integer',
808 FALSE);
40458f6c 809
b0266403
CW
810 if (!CRM_Contact_BAO_Contact_Permission::allow($contactID)) {
811 return CRM_Utils_System::permissionDenied();
812 }
813
40458f6c 814 $sortMapper = array(
815 0 => 'relation',
816 1 => 'sort_name',
817 2 => 'start_date',
818 3 => 'end_date',
819 4 => 'city',
820 5 => 'state',
821 6 => 'email',
822 7 => 'phone',
823 8 => 'links',
e1cad725 824 9 => '',
f1321272 825 10 => '',
40458f6c 826 );
827
353ffa53
TO
828 $sEcho = CRM_Utils_Type::escape($_REQUEST['sEcho'], 'Integer');
829 $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayStart'], 'Integer') : 0;
830 $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayLength'], 'Integer') : 25;
831 $sort = isset($_REQUEST['iSortCol_0']) ? CRM_Utils_Array::value(CRM_Utils_Type::escape($_REQUEST['iSortCol_0'], 'Integer'), $sortMapper) : NULL;
40458f6c 832 $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type::escape($_REQUEST['sSortDir_0'], 'String') : 'asc';
833
834 $params = $_POST;
835 if ($sort && $sortOrder) {
836 $params['sortBy'] = $sort . ' ' . $sortOrder;
837 }
838
839 $params['page'] = ($offset / $rowCount) + 1;
840 $params['rp'] = $rowCount;
841
842 $params['contact_id'] = $contactID;
843 $params['context'] = $context;
4c5f31d0 844 if ($relationship_type_id) {
cdee9432
TM
845 $params['relationship_type_id'] = $relationship_type_id;
846 }
40458f6c 847
848 // get the contact relationships
849 $relationships = CRM_Contact_BAO_Relationship::getContactRelationshipSelector($params);
850
851 $iFilteredTotal = $iTotal = $params['total'];
852 $selectorElements = array(
853 'relation',
854 'name',
855 'start_date',
856 'end_date',
857 'city',
858 'state',
859 'email',
860 'phone',
861 'links',
e1cad725 862 'id',
f1321272 863 'is_active',
40458f6c 864 );
865
70dd31e2 866 header('Content-Type: application/json');
40458f6c 867 echo CRM_Utils_JSON::encodeDataTableSelector($relationships, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
868 CRM_Utils_System::civiExit();
869 }
96025800 870
6a488035 871}