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