Merge pull request #2387 from pratik-joshi/CRM-13973
[civicrm-core.git] / CRM / Admin / Page / AJAX.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
232624b1 4 | CiviCRM version 4.4 |
6a488035
TO
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
32 * $Id$
33 *
34 */
35
36/**
37 * This class contains all the function that are called using AJAX
38 */
39class CRM_Admin_Page_AJAX {
40
41 /**
73538697
CW
42 * CRM-12337 Output navigation menu as executable javascript
43 * @see smarty_function_crmNavigationMenu
44 */
45 static function getNavigationMenu() {
46 $session = CRM_Core_Session::singleton();
47 $contactID = $session->get('userID');
48 if ($contactID) {
49 // Set headers to encourage browsers to cache for a long time
50 // If we want to refresh the menu we will send a different url
73538697
CW
51 $year = 60*60*24*364;
52 header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + $year));
53 header('Content-Type: application/javascript');
54 header("Cache-Control: max-age=$year, public");
55
56 // Render template as a javascript file
57 $smarty = CRM_Core_Smarty::singleton();
58 $navigation = CRM_Core_BAO_Navigation::createNavigation($contactID);
e203a505 59 $smarty->assign('timeGenerated', date('d M Y H:i:s'));
73538697
CW
60 $smarty->assign('navigation', $navigation);
61 print $smarty->fetch('CRM/common/Navigation.tpl');
62 }
0a15daa2 63 CRM_Utils_System::civiExit();
73538697
CW
64 }
65
66 /**
67 * Return menu tree as json data for editing
6a488035
TO
68 */
69 static function getNavigationList() {
70 echo CRM_Core_BAO_Navigation::buildNavigation(TRUE, FALSE);
71 CRM_Utils_System::civiExit();
72 }
73
74 /**
75 * Function to process drag/move action for menu tree
76 */
77 static function menuTree() {
73538697 78 CRM_Core_BAO_Navigation::processNavigation($_GET);
6a488035
TO
79 }
80
81 /**
82 * Function to build status message while
83 * enabling/ disabling various objects
84 */
85 static function getStatusMsg() {
12798ddc
CW
86 require_once('api/v3/utils.php');
87 $recordID = CRM_Utils_Type::escape($_GET['id'], 'Integer');
88 $entity = CRM_Utils_Type::escape($_GET['entity'], 'String');
4d17a233 89 $ret = array();
6a488035 90
4d17a233 91 if ($recordID && $entity && $recordBAO = _civicrm_api3_get_BAO($entity)) {
6a488035
TO
92 switch ($recordBAO) {
93 case 'CRM_Core_BAO_UFGroup':
6a488035
TO
94 $method = 'getUFJoinRecord';
95 $result = array($recordBAO, $method);
96 $ufJoin = call_user_func_array(($result), array($recordID, TRUE));
97 if (!empty($ufJoin)) {
4d17a233 98 $ret['content'] = ts('This profile is currently used for %1.', array(1 => implode(', ', $ufJoin))) . ' <br/><br/>' . ts('If you disable the profile - it will be removed from these forms and/or modules. Do you want to continue?');
6a488035
TO
99 }
100 else {
4d17a233 101 $ret['content'] = ts('Are you sure you want to disable this profile?');
6a488035
TO
102 }
103 break;
104
9da8dc8c 105 case 'CRM_Price_BAO_PriceSet':
9da8dc8c 106 $usedBy = CRM_Price_BAO_PriceSet::getUsedBy($recordID);
107 $priceSet = CRM_Price_BAO_PriceSet::getTitle($recordID);
6a488035
TO
108
109 if (!CRM_Utils_System::isNull($usedBy)) {
110 $template = CRM_Core_Smarty::singleton();
111 $template->assign('usedBy', $usedBy);
112 $comps = array(
113 'Event' => 'civicrm_event',
114 'Contribution' => 'civicrm_contribution_page',
c34e4bb4 115 'EventTemplate' => 'civicrm_event_template'
6a488035
TO
116 );
117 $contexts = array();
118 foreach ($comps as $name => $table) {
119 if (array_key_exists($table, $usedBy)) {
120 $contexts[] = $name;
121 }
122 }
123 $template->assign('contexts', $contexts);
124
4d17a233 125 $ret['illegal'] = TRUE;
6a488035 126 $table = $template->fetch('CRM/Price/Page/table.tpl');
4d17a233 127 $ret['content'] = ts('Unable to disable the \'%1\' price set - it is currently in use by one or more active events, contribution pages or contributions.', array(
6a488035
TO
128 1 => $priceSet)) . "<br/> $table";
129 }
130 else {
4d17a233 131 $ret['content'] = ts('Are you sure you want to disable \'%1\' Price Set?', array(1 => $priceSet));
6a488035
TO
132 }
133 break;
134
135 case 'CRM_Event_BAO_Event':
4d17a233 136 $ret['content'] = ts('Are you sure you want to disable this Event?');
6a488035
TO
137 break;
138
139 case 'CRM_Core_BAO_UFField':
4d17a233 140 $ret['content'] = ts('Are you sure you want to disable this CiviCRM Profile field?');
6a488035
TO
141 break;
142
143 case 'CRM_Contribute_BAO_ManagePremiums':
4d17a233 144 $ret['content'] = ts('Are you sure you want to disable this premium? This action will remove the premium from any contribution pages that currently offer it. However it will not delete the premium record - so you can re-enable it and add it back to your contribution page(s) at a later time.');
6a488035
TO
145 break;
146
147 case 'CRM_Contact_BAO_RelationshipType':
4d17a233 148 $ret['content'] = ts('Are you sure you want to disable this relationship type?') . '<br/><br/>' . ts('Users will no longer be able to select this value when adding or editing relationships between contacts.');
6a488035
TO
149 break;
150
7b3622bf 151 case 'CRM_Financial_BAO_FinancialType':
4d17a233 152 $ret['content'] = ts('Are you sure you want to disable this financial type?');
6a488035 153 break;
8ef12e64 154
7b3622bf
PN
155 case 'CRM_Financial_BAO_FinancialAccount':
156 if (!CRM_Financial_BAO_FinancialAccount::getARAccounts($recordID)) {
4d17a233
CW
157 $ret['illegal'] = TRUE;
158 $ret['content'] = ts('The selected financial account cannot be disabled because at least one Accounts Receivable type account is required (to ensure that accounting transactions are in balance).');
7b3622bf
PN
159 }
160 else {
4d17a233 161 $ret['content'] = ts('Are you sure you want to disable this financial account?');
7b3622bf
PN
162 }
163 break;
6a488035 164
8ef12e64 165 case 'CRM_Financial_BAO_PaymentProcessor':
4d17a233 166 $ret['content'] = ts('Are you sure you want to disable this payment processor?') . ' <br/><br/>' . ts('Users will no longer be able to select this value when adding or editing transaction pages.');
6a488035
TO
167 break;
168
169 case 'CRM_Financial_BAO_PaymentProcessorType':
4d17a233 170 $ret['content'] = ts('Are you sure you want to disable this payment processor type?');
6a488035
TO
171 break;
172
173 case 'CRM_Core_BAO_LocationType':
4d17a233 174 $ret['content'] = ts('Are you sure you want to disable this location type?') . ' <br/><br/>' . ts('Users will no longer be able to select this value when adding or editing contact locations.');
6a488035
TO
175 break;
176
177 case 'CRM_Event_BAO_ParticipantStatusType':
4d17a233 178 $ret['content'] = ts('Are you sure you want to disable this Participant Status?') . '<br/><br/> ' . ts('Users will no longer be able to select this value when adding or editing Participant Status.');
6a488035
TO
179 break;
180
181 case 'CRM_Mailing_BAO_Component':
4d17a233 182 $ret['content'] = ts('Are you sure you want to disable this component?');
6a488035
TO
183 break;
184
185 case 'CRM_Core_BAO_CustomField':
4d17a233 186 $ret['content'] = ts('Are you sure you want to disable this custom data field?');
6a488035
TO
187 break;
188
189 case 'CRM_Core_BAO_CustomGroup':
4d17a233 190 $ret['content'] = ts('Are you sure you want to disable this custom data group? Any profile fields that are linked to custom fields of this group will be disabled.');
6a488035
TO
191 break;
192
c6327d7d 193 case 'CRM_Core_BAO_MessageTemplate':
4d17a233 194 $ret['content'] = ts('Are you sure you want to disable this message tempate?');
6a488035
TO
195 break;
196
197 case 'CRM_ACL_BAO_ACL':
4d17a233 198 $ret['content'] = ts('Are you sure you want to disable this ACL?');
6a488035
TO
199 break;
200
201 case 'CRM_ACL_BAO_EntityRole':
4d17a233 202 $ret['content'] = ts('Are you sure you want to disable this ACL Role Assignment?');
6a488035
TO
203 break;
204
205 case 'CRM_Member_BAO_MembershipType':
4d17a233 206 $ret['content'] = ts('Are you sure you want to disable this membership type?');
6a488035
TO
207 break;
208
209 case 'CRM_Member_BAO_MembershipStatus':
4d17a233 210 $ret['content'] = ts('Are you sure you want to disable this membership status rule?');
6a488035
TO
211 break;
212
9da8dc8c 213 case 'CRM_Price_BAO_PriceField':
4d17a233 214 $ret['content'] = ts('Are you sure you want to disable this price field?');
6a488035
TO
215 break;
216
217 case 'CRM_Contact_BAO_Group':
4d17a233 218 $ret['content'] = ts('Are you sure you want to disable this Group?');
6a488035
TO
219 break;
220
221 case 'CRM_Core_BAO_OptionGroup':
4d17a233 222 $ret['content'] = ts('Are you sure you want to disable this Option?');
6a488035
TO
223 break;
224
225 case 'CRM_Contact_BAO_ContactType':
4d17a233 226 $ret['content'] = ts('Are you sure you want to disable this Contact Type?');
6a488035
TO
227 break;
228
229 case 'CRM_Core_BAO_OptionValue':
6a488035 230 $label = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $recordID, 'label');
4d17a233
CW
231 $ret['content'] = ts('Are you sure you want to disable the \'%1\' option ?', array(1 => $label));
232 $ret['content'] .= '<br /><br />' . ts('WARNING - Disabling an option which has been assigned to existing records will result in that option being cleared when the record is edited.');
6a488035
TO
233 break;
234
235 case 'CRM_Contribute_BAO_ContributionRecur':
236 $recurDetails = CRM_Contribute_BAO_ContributionRecur::getSubscriptionDetails($recordID);
4d17a233
CW
237 $ret['content'] = ts('Are you sure you want to mark this recurring contribution as cancelled?');
238 $ret['content'] .= '<br /><br /><strong>' . ts('WARNING - This action sets the CiviCRM recurring contribution status to Cancelled, but does NOT send a cancellation request to the payment processor. You will need to ensure that this recurring payment (subscription) is cancelled by the payment processor.') . '</strong>';
6a488035 239 if ($recurDetails->membership_id) {
4d17a233 240 $ret['content'] .= '<br /><br /><strong>' . ts('This recurring contribution is linked to an auto-renew membership. If you cancel it, the associated membership will no longer renew automatically. However, the current membership status will not be affected.') . '</strong>';
6a488035
TO
241 }
242 break;
8ef12e64 243
6a488035 244 default:
4d17a233 245 $ret['content'] = ts('Are you sure you want to disable this record?');
6a488035
TO
246 break;
247 }
248 }
4d17a233
CW
249 else {
250 $ret = array('status' => 'error', 'content' => 'Error: Unknown entity type.', 'illegal' => TRUE);
251 }
252 CRM_Core_Page_AJAX::returnJsonResponse($ret);
6a488035
TO
253 }
254
255 static function getTagList() {
256 $name = CRM_Utils_Type::escape($_GET['name'], 'String');
257 $parentId = CRM_Utils_Type::escape($_GET['parentId'], 'Integer');
258
259 $isSearch = NULL;
260 if (isset($_GET['search'])) {
261 $isSearch = CRM_Utils_Type::escape($_GET['search'], 'Integer');
262 }
263
264 $tags = array();
265
266 // always add current search term as possible tag
267 // here we append :::value to determine if existing / new tag should be created
268 if (!$isSearch) {
269 $tags[] = array(
270 'name' => $name,
271 'id' => $name . ":::value",
272 );
273 }
274
275 $query = "SELECT id, name FROM civicrm_tag WHERE parent_id = {$parentId} and name LIKE '%{$name}%'";
276 $dao = CRM_Core_DAO::executeQuery($query);
277
278 while ($dao->fetch()) {
279 // make sure we return tag name entered by user only if it does not exists in db
280 if ($name == $dao->name) {
281 $tags = array();
282 }
283 // escape double quotes, which break results js
284 $tags[] = array('name' => addcslashes($dao->name, '"'),
285 'id' => $dao->id,
286 );
287 }
288
289 echo json_encode($tags);
290 CRM_Utils_System::civiExit();
291 }
292
293 static function mergeTagList() {
294 $name = CRM_Utils_Type::escape($_GET['s'], 'String');
295 $fromId = CRM_Utils_Type::escape($_GET['fromId'], 'Integer');
296 $limit = CRM_Utils_Type::escape($_GET['limit'], 'Integer');
297
298 // build used-for clause to be used in main query
299 $usedForTagA = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $fromId, 'used_for');
300 $usedForClause = array();
301 if ($usedForTagA) {
302 $usedForTagA = explode(",", $usedForTagA);
303 foreach ($usedForTagA as $key => $value) {
304 $usedForClause[] = "t1.used_for LIKE '%{$value}%'";
305 }
306 }
307 $usedForClause = !empty($usedForClause) ? implode(' OR ', $usedForClause) : '1';
308 sort($usedForTagA);
309
310 // query to list mergable tags
311 $query = "
312SELECT t1.name, t1.id, t1.used_for, t2.name as parent
8ef12e64 313FROM civicrm_tag t1
6a488035 314LEFT JOIN civicrm_tag t2 ON t1.parent_id = t2.id
8ef12e64 315WHERE t1.id <> {$fromId} AND
6a488035 316 t1.name LIKE '%{$name}%' AND
8ef12e64 317 ({$usedForClause})
6a488035
TO
318LIMIT $limit";
319 $dao = CRM_Core_DAO::executeQuery($query);
320
321 while ($dao->fetch()) {
322 $warning = 0;
323 if (!empty($dao->used_for)) {
324 $usedForTagB = explode(',', $dao->used_for);
325 sort($usedForTagB);
326 $usedForDiff = array_diff($usedForTagA, $usedForTagB);
327 if (!empty($usedForDiff)) {
328 $warning = 1;
329 }
330 }
331 $tag = addcslashes($dao->name, '"') . "|{$dao->id}|{$warning}\n";
332 echo $tag = $dao->parent ? (addcslashes($dao->parent, '"') . ' :: ' . $tag) : $tag;
333 }
334 CRM_Utils_System::civiExit();
335 }
336
337 static function processTags() {
338 $skipTagCreate = $skipEntityAction = $entityId = NULL;
339 $action = CRM_Utils_Type::escape($_POST['action'], 'String');
340 $parentId = CRM_Utils_Type::escape($_POST['parentId'], 'Integer');
341 if ($_POST['entityId']) {
342 $entityId = CRM_Utils_Type::escape($_POST['entityId'], 'Integer');
343 }
344
345 $entityTable = CRM_Utils_Type::escape($_POST['entityTable'], 'String');
346
347 if ($_POST['skipTagCreate']) {
348 $skipTagCreate = CRM_Utils_Type::escape($_POST['skipTagCreate'], 'Integer');
349 }
350
351 if ($_POST['skipEntityAction']) {
352 $skipEntityAction = CRM_Utils_Type::escape($_POST['skipEntityAction'], 'Integer');
353 }
354
355 // check if user has selected existing tag or is creating new tag
356 // this is done to allow numeric tags etc.
357 $tagValue = explode(':::', $_POST['tagID']);
358
359 $createNewTag = FALSE;
360 $tagID = $tagValue[0];
361 if (isset($tagValue[1]) && $tagValue[1] == 'value') {
362 $createNewTag = TRUE;
363 }
364
365 $tagInfo = array();
366 // if action is select
367 if ($action == 'select') {
368 // check the value of tagID
369 // if numeric that means existing tag
370 // else create new tag
371 if (!$skipTagCreate && $createNewTag) {
372 $params = array(
373 'name' => $tagID,
374 'parent_id' => $parentId,
375 );
376
377 $tagObject = CRM_Core_BAO_Tag::add($params, CRM_Core_DAO::$_nullArray);
378
379 $tagInfo = array(
380 'name' => $tagID,
381 'id' => $tagObject->id,
382 'action' => $action,
383 );
384 $tagID = $tagObject->id;
385 }
386
387 if (!$skipEntityAction && $entityId) {
388 // save this tag to contact
389 $params = array(
390 'entity_table' => $entityTable,
391 'entity_id' => $entityId,
392 'tag_id' => $tagID,
393 );
394
395 CRM_Core_BAO_EntityTag::add($params);
396 }
397 // if action is delete
398 }
399 elseif ($action == 'delete') {
400 if (!is_numeric($tagID)) {
401 $tagID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $tagID, 'id', 'name');
402 }
b3caa3a7 403 if (!$skipEntityAction && $entityId) {
6a488035
TO
404 // delete this tag entry for the entity
405 $params = array(
406 'entity_table' => $entityTable,
407 'entity_id' => $entityId,
408 'tag_id' => $tagID,
409 );
410
411 CRM_Core_BAO_EntityTag::del($params);
412 }
413 $tagInfo = array(
414 'id' => $tagID,
415 'action' => $action,
416 );
417 }
418
419 echo json_encode($tagInfo);
420 CRM_Utils_System::civiExit();
421 }
422
423 function mappingList() {
424 $params = array('mappingID');
425 foreach ($params as $param) {
426 $$param = CRM_Utils_Array::value($param, $_POST);
427 }
428
429 if (!$mappingID) {
430 echo json_encode(array('error_msg' => 'required params missing.'));
431 CRM_Utils_System::civiExit();
432 }
433
434 $selectionOptions = CRM_Core_BAO_ActionSchedule::getSelection1($mappingID);
435 extract($selectionOptions);
436
437 $elements = array();
438 foreach ($sel4 as $id => $name) {
439 $elements[] = array(
440 'name' => $name,
441 'value' => $id,
442 );
443 }
444
445 echo json_encode($elements);
446 CRM_Utils_System::civiExit();
447 }
448
449 function mappingList1() {
450 $params = array('mappingID');
451 foreach ($params as $param) {
452 $$param = CRM_Utils_Array::value($param, $_POST);
453 }
454
455 if (!$mappingID) {
456 echo json_encode(array('error_msg' => 'required params missing.'));
457 CRM_Utils_System::civiExit();
458 }
459
460 $selectionOptions = CRM_Core_BAO_ActionSchedule::getSelection1($mappingID);
461 extract($selectionOptions);
462
463 $elements = array();
464 foreach ($sel5 as $id => $name) {
465 $elements['sel5'][] = array(
466 'name' => $name,
467 'value' => $id,
468 );
469 }
470 $elements['recipientMapping'] = $recipientMapping;
471
472 echo json_encode($elements);
473 CRM_Utils_System::civiExit();
474 }
475
476 static function mergeTags() {
477 $tagAId = CRM_Utils_Type::escape($_POST['fromId'], 'Integer');
478 $tagBId = CRM_Utils_Type::escape($_POST['toId'], 'Integer');
479
480 $result = CRM_Core_BAO_EntityTag::mergeTags($tagAId, $tagBId);
481
482 if (!empty($result['tagB_used_for'])) {
483 $usedFor = CRM_Core_OptionGroup::values('tag_used_for');
484 foreach ($result['tagB_used_for'] as & $val) {
485 $val = $usedFor[$val];
486 }
487 $result['tagB_used_for'] = implode(', ', $result['tagB_used_for']);
488 }
489
490 echo json_encode($result);
491 CRM_Utils_System::civiExit();
492 }
493
494 function recipient() {
495 $params = array('recipient');
496 foreach ($params as $param) {
497 $$param = CRM_Utils_Array::value($param, $_POST);
498 }
499
500 if (!$recipient) {
501 echo json_encode(array('error_msg' => 'required params missing.'));
502 CRM_Utils_System::civiExit();
503 }
504
505 switch ($recipient) {
506 case 'Participant Status':
507 $values = CRM_Event_PseudoConstant::participantStatus();
508 break;
509
40f2fee3 510 case 'participant_role':
6a488035
TO
511 $values = CRM_Event_PseudoConstant::participantRole();
512 break;
513
514 default:
515 exit;
516 }
517
518 $elements = array();
519 foreach ($values as $id => $name) {
520 $elements[] = array(
521 'name' => $name,
522 'value' => $id,
523 );
524 }
525
526 echo json_encode($elements);
527 CRM_Utils_System::civiExit();
528 }
529}
530