Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2013-12-09-17-00-10
[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() {
86 $recordID = CRM_Utils_Type::escape($_POST['recordID'], 'Integer');
87 $recordBAO = CRM_Utils_Type::escape($_POST['recordBAO'], 'String');
88 $op = CRM_Utils_Type::escape($_POST['op'], 'String');
89 $show = NULL;
90
91 if ($op == 'disable-enable') {
92 $status = ts('Are you sure you want to enable this record?');
93 }
94 else {
95 switch ($recordBAO) {
96 case 'CRM_Core_BAO_UFGroup':
97 require_once (str_replace('_', DIRECTORY_SEPARATOR, $recordBAO) . '.php');
98 $method = 'getUFJoinRecord';
99 $result = array($recordBAO, $method);
100 $ufJoin = call_user_func_array(($result), array($recordID, TRUE));
101 if (!empty($ufJoin)) {
102 $status = 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?');
103 }
104 else {
105 $status = ts('Are you sure you want to disable this profile?');
106 }
107 break;
108
9da8dc8c 109 case 'CRM_Price_BAO_PriceSet':
6a488035 110 require_once (str_replace('_', DIRECTORY_SEPARATOR, $recordBAO) . '.php');
9da8dc8c 111 $usedBy = CRM_Price_BAO_PriceSet::getUsedBy($recordID);
112 $priceSet = CRM_Price_BAO_PriceSet::getTitle($recordID);
6a488035
TO
113
114 if (!CRM_Utils_System::isNull($usedBy)) {
115 $template = CRM_Core_Smarty::singleton();
116 $template->assign('usedBy', $usedBy);
117 $comps = array(
118 'Event' => 'civicrm_event',
119 'Contribution' => 'civicrm_contribution_page',
c34e4bb4 120 'EventTemplate' => 'civicrm_event_template'
6a488035
TO
121 );
122 $contexts = array();
123 foreach ($comps as $name => $table) {
124 if (array_key_exists($table, $usedBy)) {
125 $contexts[] = $name;
126 }
127 }
128 $template->assign('contexts', $contexts);
129
130 $show = 'noButton';
131 $table = $template->fetch('CRM/Price/Page/table.tpl');
132 $status = ts('Unable to disable the \'%1\' price set - it is currently in use by one or more active events, contribution pages or contributions.', array(
133 1 => $priceSet)) . "<br/> $table";
134 }
135 else {
136 $status = ts('Are you sure you want to disable \'%1\' Price Set?', array(1 => $priceSet));
137 }
138 break;
139
140 case 'CRM_Event_BAO_Event':
141 $status = ts('Are you sure you want to disable this Event?');
142 break;
143
144 case 'CRM_Core_BAO_UFField':
145 $status = ts('Are you sure you want to disable this CiviCRM Profile field?');
146 break;
147
148 case 'CRM_Contribute_BAO_ManagePremiums':
149 $status = 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.');
150 break;
151
152 case 'CRM_Contact_BAO_RelationshipType':
153 $status = 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.');
154 break;
155
7b3622bf 156 case 'CRM_Financial_BAO_FinancialType':
6a488035
TO
157 $status = ts('Are you sure you want to disable this financial type?');
158 break;
8ef12e64 159
7b3622bf
PN
160 case 'CRM_Financial_BAO_FinancialAccount':
161 if (!CRM_Financial_BAO_FinancialAccount::getARAccounts($recordID)) {
162 $show = 'noButton';
fd2f43f7 163 $status = 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
164 }
165 else {
166 $status = ts('Are you sure you want to disable this financial account?');
167 }
168 break;
6a488035 169
8ef12e64 170 case 'CRM_Financial_BAO_PaymentProcessor':
6a488035
TO
171 $status = 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.');
172 break;
173
174 case 'CRM_Financial_BAO_PaymentProcessorType':
175 $status = ts('Are you sure you want to disable this payment processor type?');
176 break;
177
178 case 'CRM_Core_BAO_LocationType':
179 $status = 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.');
180 break;
181
182 case 'CRM_Event_BAO_ParticipantStatusType':
183 $status = 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.');
184 break;
185
186 case 'CRM_Mailing_BAO_Component':
187 $status = ts('Are you sure you want to disable this component?');
188 break;
189
190 case 'CRM_Core_BAO_CustomField':
191 $status = ts('Are you sure you want to disable this custom data field?');
192 break;
193
194 case 'CRM_Core_BAO_CustomGroup':
195 $status = 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.');
196 break;
197
c6327d7d 198 case 'CRM_Core_BAO_MessageTemplate':
6a488035
TO
199 $status = ts('Are you sure you want to disable this message tempate?');
200 break;
201
202 case 'CRM_ACL_BAO_ACL':
203 $status = ts('Are you sure you want to disable this ACL?');
204 break;
205
206 case 'CRM_ACL_BAO_EntityRole':
207 $status = ts('Are you sure you want to disable this ACL Role Assignment?');
208 break;
209
210 case 'CRM_Member_BAO_MembershipType':
211 $status = ts('Are you sure you want to disable this membership type?');
212 break;
213
214 case 'CRM_Member_BAO_MembershipStatus':
215 $status = ts('Are you sure you want to disable this membership status rule?');
216 break;
217
9da8dc8c 218 case 'CRM_Price_BAO_PriceField':
6a488035
TO
219 $status = ts('Are you sure you want to disable this price field?');
220 break;
221
222 case 'CRM_Contact_BAO_Group':
223 $status = ts('Are you sure you want to disable this Group?');
224 break;
225
226 case 'CRM_Core_BAO_OptionGroup':
227 $status = ts('Are you sure you want to disable this Option?');
228 break;
229
230 case 'CRM_Contact_BAO_ContactType':
231 $status = ts('Are you sure you want to disable this Contact Type?');
232 break;
233
234 case 'CRM_Core_BAO_OptionValue':
235 require_once (str_replace('_', DIRECTORY_SEPARATOR, $recordBAO) . '.php');
236 $label = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $recordID, 'label');
237 $status = ts('Are you sure you want to disable the \'%1\' option ?', array(1 => $label));
238 $status .= '<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.');
239 break;
240
241 case 'CRM_Contribute_BAO_ContributionRecur':
242 $recurDetails = CRM_Contribute_BAO_ContributionRecur::getSubscriptionDetails($recordID);
243 $status = ts('Are you sure you want to mark this recurring contribution as cancelled?');
244 $status .= '<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>';
245 if ($recurDetails->membership_id) {
246 $status .= '<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>';
247 }
248 break;
8ef12e64 249
6a488035
TO
250 case 'CRM_Batch_BAO_Batch':
251 if ($op == 'close') {
252 $status = ts('Are you sure you want to close this batch?');
253 }
254 elseif ($op == 'open') {
255 $status = ts('Are you sure you want to reopen this batch?');
256 }
257 elseif ($op == 'delete') {
258 $status = ts('Are you sure you want to delete this batch?');
259 }
260 elseif ($op == 'remove') {
261 $status = ts('Are you sure you want to remove this financial transaction?');
262 }
263 elseif ($op == 'export') {
264 $status = ts('Are you sure you want to close and export this batch?');
265 }
266 else {
267 $status = ts('Are you sure you want to assign this financial transaction to the batch?');
268 }
269 break;
8ef12e64 270
6a488035
TO
271 default:
272 $status = ts('Are you sure you want to disable this record?');
273 break;
274 }
275 }
276 $statusMessage['status'] = $status;
277 $statusMessage['show'] = $show;
278
279 echo json_encode($statusMessage);
280 CRM_Utils_System::civiExit();
281 }
282
283 static function getTagList() {
284 $name = CRM_Utils_Type::escape($_GET['name'], 'String');
285 $parentId = CRM_Utils_Type::escape($_GET['parentId'], 'Integer');
286
287 $isSearch = NULL;
288 if (isset($_GET['search'])) {
289 $isSearch = CRM_Utils_Type::escape($_GET['search'], 'Integer');
290 }
291
292 $tags = array();
293
294 // always add current search term as possible tag
295 // here we append :::value to determine if existing / new tag should be created
296 if (!$isSearch) {
297 $tags[] = array(
298 'name' => $name,
299 'id' => $name . ":::value",
300 );
301 }
302
303 $query = "SELECT id, name FROM civicrm_tag WHERE parent_id = {$parentId} and name LIKE '%{$name}%'";
304 $dao = CRM_Core_DAO::executeQuery($query);
305
306 while ($dao->fetch()) {
307 // make sure we return tag name entered by user only if it does not exists in db
308 if ($name == $dao->name) {
309 $tags = array();
310 }
311 // escape double quotes, which break results js
312 $tags[] = array('name' => addcslashes($dao->name, '"'),
313 'id' => $dao->id,
314 );
315 }
316
317 echo json_encode($tags);
318 CRM_Utils_System::civiExit();
319 }
320
321 static function mergeTagList() {
322 $name = CRM_Utils_Type::escape($_GET['s'], 'String');
323 $fromId = CRM_Utils_Type::escape($_GET['fromId'], 'Integer');
324 $limit = CRM_Utils_Type::escape($_GET['limit'], 'Integer');
325
326 // build used-for clause to be used in main query
327 $usedForTagA = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $fromId, 'used_for');
328 $usedForClause = array();
329 if ($usedForTagA) {
330 $usedForTagA = explode(",", $usedForTagA);
331 foreach ($usedForTagA as $key => $value) {
332 $usedForClause[] = "t1.used_for LIKE '%{$value}%'";
333 }
334 }
335 $usedForClause = !empty($usedForClause) ? implode(' OR ', $usedForClause) : '1';
336 sort($usedForTagA);
337
338 // query to list mergable tags
339 $query = "
340SELECT t1.name, t1.id, t1.used_for, t2.name as parent
8ef12e64 341FROM civicrm_tag t1
6a488035 342LEFT JOIN civicrm_tag t2 ON t1.parent_id = t2.id
8ef12e64 343WHERE t1.id <> {$fromId} AND
6a488035 344 t1.name LIKE '%{$name}%' AND
8ef12e64 345 ({$usedForClause})
6a488035
TO
346LIMIT $limit";
347 $dao = CRM_Core_DAO::executeQuery($query);
348
349 while ($dao->fetch()) {
350 $warning = 0;
351 if (!empty($dao->used_for)) {
352 $usedForTagB = explode(',', $dao->used_for);
353 sort($usedForTagB);
354 $usedForDiff = array_diff($usedForTagA, $usedForTagB);
355 if (!empty($usedForDiff)) {
356 $warning = 1;
357 }
358 }
359 $tag = addcslashes($dao->name, '"') . "|{$dao->id}|{$warning}\n";
360 echo $tag = $dao->parent ? (addcslashes($dao->parent, '"') . ' :: ' . $tag) : $tag;
361 }
362 CRM_Utils_System::civiExit();
363 }
364
365 static function processTags() {
366 $skipTagCreate = $skipEntityAction = $entityId = NULL;
367 $action = CRM_Utils_Type::escape($_POST['action'], 'String');
368 $parentId = CRM_Utils_Type::escape($_POST['parentId'], 'Integer');
369 if ($_POST['entityId']) {
370 $entityId = CRM_Utils_Type::escape($_POST['entityId'], 'Integer');
371 }
372
373 $entityTable = CRM_Utils_Type::escape($_POST['entityTable'], 'String');
374
375 if ($_POST['skipTagCreate']) {
376 $skipTagCreate = CRM_Utils_Type::escape($_POST['skipTagCreate'], 'Integer');
377 }
378
379 if ($_POST['skipEntityAction']) {
380 $skipEntityAction = CRM_Utils_Type::escape($_POST['skipEntityAction'], 'Integer');
381 }
382
383 // check if user has selected existing tag or is creating new tag
384 // this is done to allow numeric tags etc.
385 $tagValue = explode(':::', $_POST['tagID']);
386
387 $createNewTag = FALSE;
388 $tagID = $tagValue[0];
389 if (isset($tagValue[1]) && $tagValue[1] == 'value') {
390 $createNewTag = TRUE;
391 }
392
393 $tagInfo = array();
394 // if action is select
395 if ($action == 'select') {
396 // check the value of tagID
397 // if numeric that means existing tag
398 // else create new tag
399 if (!$skipTagCreate && $createNewTag) {
400 $params = array(
401 'name' => $tagID,
402 'parent_id' => $parentId,
403 );
404
405 $tagObject = CRM_Core_BAO_Tag::add($params, CRM_Core_DAO::$_nullArray);
406
407 $tagInfo = array(
408 'name' => $tagID,
409 'id' => $tagObject->id,
410 'action' => $action,
411 );
412 $tagID = $tagObject->id;
413 }
414
415 if (!$skipEntityAction && $entityId) {
416 // save this tag to contact
417 $params = array(
418 'entity_table' => $entityTable,
419 'entity_id' => $entityId,
420 'tag_id' => $tagID,
421 );
422
423 CRM_Core_BAO_EntityTag::add($params);
424 }
425 // if action is delete
426 }
427 elseif ($action == 'delete') {
428 if (!is_numeric($tagID)) {
429 $tagID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $tagID, 'id', 'name');
430 }
431 if ($entityId) {
432 // delete this tag entry for the entity
433 $params = array(
434 'entity_table' => $entityTable,
435 'entity_id' => $entityId,
436 'tag_id' => $tagID,
437 );
438
439 CRM_Core_BAO_EntityTag::del($params);
440 }
441 $tagInfo = array(
442 'id' => $tagID,
443 'action' => $action,
444 );
445 }
446
447 echo json_encode($tagInfo);
448 CRM_Utils_System::civiExit();
449 }
450
451 function mappingList() {
452 $params = array('mappingID');
453 foreach ($params as $param) {
454 $$param = CRM_Utils_Array::value($param, $_POST);
455 }
456
457 if (!$mappingID) {
458 echo json_encode(array('error_msg' => 'required params missing.'));
459 CRM_Utils_System::civiExit();
460 }
461
462 $selectionOptions = CRM_Core_BAO_ActionSchedule::getSelection1($mappingID);
463 extract($selectionOptions);
464
465 $elements = array();
466 foreach ($sel4 as $id => $name) {
467 $elements[] = array(
468 'name' => $name,
469 'value' => $id,
470 );
471 }
472
473 echo json_encode($elements);
474 CRM_Utils_System::civiExit();
475 }
476
477 function mappingList1() {
478 $params = array('mappingID');
479 foreach ($params as $param) {
480 $$param = CRM_Utils_Array::value($param, $_POST);
481 }
482
483 if (!$mappingID) {
484 echo json_encode(array('error_msg' => 'required params missing.'));
485 CRM_Utils_System::civiExit();
486 }
487
488 $selectionOptions = CRM_Core_BAO_ActionSchedule::getSelection1($mappingID);
489 extract($selectionOptions);
490
491 $elements = array();
492 foreach ($sel5 as $id => $name) {
493 $elements['sel5'][] = array(
494 'name' => $name,
495 'value' => $id,
496 );
497 }
498 $elements['recipientMapping'] = $recipientMapping;
499
500 echo json_encode($elements);
501 CRM_Utils_System::civiExit();
502 }
503
504 static function mergeTags() {
505 $tagAId = CRM_Utils_Type::escape($_POST['fromId'], 'Integer');
506 $tagBId = CRM_Utils_Type::escape($_POST['toId'], 'Integer');
507
508 $result = CRM_Core_BAO_EntityTag::mergeTags($tagAId, $tagBId);
509
510 if (!empty($result['tagB_used_for'])) {
511 $usedFor = CRM_Core_OptionGroup::values('tag_used_for');
512 foreach ($result['tagB_used_for'] as & $val) {
513 $val = $usedFor[$val];
514 }
515 $result['tagB_used_for'] = implode(', ', $result['tagB_used_for']);
516 }
517
518 echo json_encode($result);
519 CRM_Utils_System::civiExit();
520 }
521
522 function recipient() {
523 $params = array('recipient');
524 foreach ($params as $param) {
525 $$param = CRM_Utils_Array::value($param, $_POST);
526 }
527
528 if (!$recipient) {
529 echo json_encode(array('error_msg' => 'required params missing.'));
530 CRM_Utils_System::civiExit();
531 }
532
533 switch ($recipient) {
534 case 'Participant Status':
535 $values = CRM_Event_PseudoConstant::participantStatus();
536 break;
537
40f2fee3 538 case 'participant_role':
6a488035
TO
539 $values = CRM_Event_PseudoConstant::participantRole();
540 break;
541
542 default:
543 exit;
544 }
545
546 $elements = array();
547 foreach ($values as $id => $name) {
548 $elements[] = array(
549 'name' => $name,
550 'value' => $id,
551 );
552 }
553
554 echo json_encode($elements);
555 CRM_Utils_System::civiExit();
556 }
557}
558