Merge pull request #3942 from colemanw/chainSelect
[civicrm-core.git] / CRM / Admin / Page / AJAX.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
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() {
3d527838 46 $contactID = CRM_Core_Session::singleton()->get('userID');
73538697 47 if ($contactID) {
19f7e35e
CW
48 // Set headers to encourage browsers to cache for a long time
49 $year = 60*60*24*364;
50 header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + $year));
51 header('Content-Type: application/javascript');
52 header("Cache-Control: max-age=$year, public");
53
9b7526a8 54 print CRM_Core_Smarty::singleton()->fetchWith('CRM/common/navigation.js.tpl', array(
3d527838
CW
55 'navigation' => CRM_Core_BAO_Navigation::createNavigation($contactID),
56 ));
73538697 57 }
0a15daa2 58 CRM_Utils_System::civiExit();
73538697
CW
59 }
60
61 /**
62 * Return menu tree as json data for editing
6a488035
TO
63 */
64 static function getNavigationList() {
65 echo CRM_Core_BAO_Navigation::buildNavigation(TRUE, FALSE);
66 CRM_Utils_System::civiExit();
67 }
68
69 /**
70 * Function to process drag/move action for menu tree
71 */
72 static function menuTree() {
73538697 73 CRM_Core_BAO_Navigation::processNavigation($_GET);
6a488035
TO
74 }
75
76 /**
77 * Function to build status message while
78 * enabling/ disabling various objects
79 */
80 static function getStatusMsg() {
12798ddc
CW
81 require_once('api/v3/utils.php');
82 $recordID = CRM_Utils_Type::escape($_GET['id'], 'Integer');
83 $entity = CRM_Utils_Type::escape($_GET['entity'], 'String');
4d17a233 84 $ret = array();
6a488035 85
4d17a233 86 if ($recordID && $entity && $recordBAO = _civicrm_api3_get_BAO($entity)) {
6a488035
TO
87 switch ($recordBAO) {
88 case 'CRM_Core_BAO_UFGroup':
6a488035
TO
89 $method = 'getUFJoinRecord';
90 $result = array($recordBAO, $method);
91 $ufJoin = call_user_func_array(($result), array($recordID, TRUE));
92 if (!empty($ufJoin)) {
4d17a233 93 $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
94 }
95 else {
4d17a233 96 $ret['content'] = ts('Are you sure you want to disable this profile?');
6a488035
TO
97 }
98 break;
99
9da8dc8c 100 case 'CRM_Price_BAO_PriceSet':
9da8dc8c 101 $usedBy = CRM_Price_BAO_PriceSet::getUsedBy($recordID);
102 $priceSet = CRM_Price_BAO_PriceSet::getTitle($recordID);
6a488035
TO
103
104 if (!CRM_Utils_System::isNull($usedBy)) {
105 $template = CRM_Core_Smarty::singleton();
106 $template->assign('usedBy', $usedBy);
107 $comps = array(
108 'Event' => 'civicrm_event',
109 'Contribution' => 'civicrm_contribution_page',
c34e4bb4 110 'EventTemplate' => 'civicrm_event_template'
6a488035
TO
111 );
112 $contexts = array();
113 foreach ($comps as $name => $table) {
114 if (array_key_exists($table, $usedBy)) {
115 $contexts[] = $name;
116 }
117 }
118 $template->assign('contexts', $contexts);
119
4d17a233 120 $ret['illegal'] = TRUE;
6a488035 121 $table = $template->fetch('CRM/Price/Page/table.tpl');
4d17a233 122 $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
123 1 => $priceSet)) . "<br/> $table";
124 }
125 else {
4d17a233 126 $ret['content'] = ts('Are you sure you want to disable \'%1\' Price Set?', array(1 => $priceSet));
6a488035
TO
127 }
128 break;
129
130 case 'CRM_Event_BAO_Event':
4d17a233 131 $ret['content'] = ts('Are you sure you want to disable this Event?');
6a488035
TO
132 break;
133
134 case 'CRM_Core_BAO_UFField':
4d17a233 135 $ret['content'] = ts('Are you sure you want to disable this CiviCRM Profile field?');
6a488035
TO
136 break;
137
138 case 'CRM_Contribute_BAO_ManagePremiums':
4d17a233 139 $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
140 break;
141
142 case 'CRM_Contact_BAO_RelationshipType':
4d17a233 143 $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
144 break;
145
7b3622bf 146 case 'CRM_Financial_BAO_FinancialType':
4d17a233 147 $ret['content'] = ts('Are you sure you want to disable this financial type?');
6a488035 148 break;
8ef12e64 149
7b3622bf
PN
150 case 'CRM_Financial_BAO_FinancialAccount':
151 if (!CRM_Financial_BAO_FinancialAccount::getARAccounts($recordID)) {
4d17a233
CW
152 $ret['illegal'] = TRUE;
153 $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
154 }
155 else {
4d17a233 156 $ret['content'] = ts('Are you sure you want to disable this financial account?');
7b3622bf
PN
157 }
158 break;
6a488035 159
8ef12e64 160 case 'CRM_Financial_BAO_PaymentProcessor':
4d17a233 161 $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
162 break;
163
164 case 'CRM_Financial_BAO_PaymentProcessorType':
4d17a233 165 $ret['content'] = ts('Are you sure you want to disable this payment processor type?');
6a488035
TO
166 break;
167
168 case 'CRM_Core_BAO_LocationType':
4d17a233 169 $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
170 break;
171
172 case 'CRM_Event_BAO_ParticipantStatusType':
4d17a233 173 $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
174 break;
175
176 case 'CRM_Mailing_BAO_Component':
4d17a233 177 $ret['content'] = ts('Are you sure you want to disable this component?');
6a488035
TO
178 break;
179
180 case 'CRM_Core_BAO_CustomField':
4d17a233 181 $ret['content'] = ts('Are you sure you want to disable this custom data field?');
6a488035
TO
182 break;
183
184 case 'CRM_Core_BAO_CustomGroup':
4d17a233 185 $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
186 break;
187
c6327d7d 188 case 'CRM_Core_BAO_MessageTemplate':
4d17a233 189 $ret['content'] = ts('Are you sure you want to disable this message tempate?');
6a488035
TO
190 break;
191
192 case 'CRM_ACL_BAO_ACL':
4d17a233 193 $ret['content'] = ts('Are you sure you want to disable this ACL?');
6a488035
TO
194 break;
195
196 case 'CRM_ACL_BAO_EntityRole':
4d17a233 197 $ret['content'] = ts('Are you sure you want to disable this ACL Role Assignment?');
6a488035
TO
198 break;
199
200 case 'CRM_Member_BAO_MembershipType':
4d17a233 201 $ret['content'] = ts('Are you sure you want to disable this membership type?');
6a488035
TO
202 break;
203
204 case 'CRM_Member_BAO_MembershipStatus':
4d17a233 205 $ret['content'] = ts('Are you sure you want to disable this membership status rule?');
6a488035
TO
206 break;
207
9da8dc8c 208 case 'CRM_Price_BAO_PriceField':
4d17a233 209 $ret['content'] = ts('Are you sure you want to disable this price field?');
6a488035
TO
210 break;
211
212 case 'CRM_Contact_BAO_Group':
4d17a233 213 $ret['content'] = ts('Are you sure you want to disable this Group?');
6a488035
TO
214 break;
215
216 case 'CRM_Core_BAO_OptionGroup':
4d17a233 217 $ret['content'] = ts('Are you sure you want to disable this Option?');
6a488035
TO
218 break;
219
220 case 'CRM_Contact_BAO_ContactType':
4d17a233 221 $ret['content'] = ts('Are you sure you want to disable this Contact Type?');
6a488035
TO
222 break;
223
224 case 'CRM_Core_BAO_OptionValue':
6a488035 225 $label = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $recordID, 'label');
4d17a233
CW
226 $ret['content'] = ts('Are you sure you want to disable the \'%1\' option ?', array(1 => $label));
227 $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
228 break;
229
230 case 'CRM_Contribute_BAO_ContributionRecur':
231 $recurDetails = CRM_Contribute_BAO_ContributionRecur::getSubscriptionDetails($recordID);
4d17a233
CW
232 $ret['content'] = ts('Are you sure you want to mark this recurring contribution as cancelled?');
233 $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 234 if ($recurDetails->membership_id) {
4d17a233 235 $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
236 }
237 break;
8ef12e64 238
6a488035 239 default:
4d17a233 240 $ret['content'] = ts('Are you sure you want to disable this record?');
6a488035
TO
241 break;
242 }
243 }
4d17a233
CW
244 else {
245 $ret = array('status' => 'error', 'content' => 'Error: Unknown entity type.', 'illegal' => TRUE);
246 }
247 CRM_Core_Page_AJAX::returnJsonResponse($ret);
6a488035
TO
248 }
249
6a488035 250 static function mergeTagList() {
2e02f487 251 $name = CRM_Utils_Type::escape($_GET['term'], 'String');
6a488035 252 $fromId = CRM_Utils_Type::escape($_GET['fromId'], 'Integer');
ebb5fc58 253 $limit = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'search_autocomplete_count', NULL, 10);
6a488035
TO
254
255 // build used-for clause to be used in main query
256 $usedForTagA = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $fromId, 'used_for');
257 $usedForClause = array();
258 if ($usedForTagA) {
259 $usedForTagA = explode(",", $usedForTagA);
260 foreach ($usedForTagA as $key => $value) {
261 $usedForClause[] = "t1.used_for LIKE '%{$value}%'";
262 }
263 }
264 $usedForClause = !empty($usedForClause) ? implode(' OR ', $usedForClause) : '1';
265 sort($usedForTagA);
266
267 // query to list mergable tags
268 $query = "
269SELECT t1.name, t1.id, t1.used_for, t2.name as parent
8ef12e64 270FROM civicrm_tag t1
6a488035 271LEFT JOIN civicrm_tag t2 ON t1.parent_id = t2.id
8ef12e64 272WHERE t1.id <> {$fromId} AND
6a488035 273 t1.name LIKE '%{$name}%' AND
8ef12e64 274 ({$usedForClause})
6a488035
TO
275LIMIT $limit";
276 $dao = CRM_Core_DAO::executeQuery($query);
ebb5fc58 277 $result = array();
6a488035
TO
278
279 while ($dao->fetch()) {
ebb5fc58
CW
280 $row = array(
281 'id' => $dao->id,
282 'text' => ($dao->parent ? "{$dao->parent} :: " : '') . $dao->name,
283 );
284 // Add warning about used_for types
6a488035
TO
285 if (!empty($dao->used_for)) {
286 $usedForTagB = explode(',', $dao->used_for);
287 sort($usedForTagB);
288 $usedForDiff = array_diff($usedForTagA, $usedForTagB);
289 if (!empty($usedForDiff)) {
ebb5fc58 290 $row['warning'] = TRUE;
6a488035
TO
291 }
292 }
ebb5fc58 293 $result[] = $row;
6a488035 294 }
ebb5fc58 295 print json_encode($result);
6a488035
TO
296 CRM_Utils_System::civiExit();
297 }
298
6a488035
TO
299 function mappingList() {
300 $params = array('mappingID');
301 foreach ($params as $param) {
302 $$param = CRM_Utils_Array::value($param, $_POST);
303 }
304
305 if (!$mappingID) {
306 echo json_encode(array('error_msg' => 'required params missing.'));
307 CRM_Utils_System::civiExit();
308 }
309
310 $selectionOptions = CRM_Core_BAO_ActionSchedule::getSelection1($mappingID);
311 extract($selectionOptions);
312
313 $elements = array();
314 foreach ($sel4 as $id => $name) {
315 $elements[] = array(
316 'name' => $name,
317 'value' => $id,
318 );
319 }
320
321 echo json_encode($elements);
322 CRM_Utils_System::civiExit();
323 }
324
325 function mappingList1() {
326 $params = array('mappingID');
327 foreach ($params as $param) {
328 $$param = CRM_Utils_Array::value($param, $_POST);
329 }
330
331 if (!$mappingID) {
332 echo json_encode(array('error_msg' => 'required params missing.'));
333 CRM_Utils_System::civiExit();
334 }
335
336 $selectionOptions = CRM_Core_BAO_ActionSchedule::getSelection1($mappingID);
337 extract($selectionOptions);
338
339 $elements = array();
340 foreach ($sel5 as $id => $name) {
341 $elements['sel5'][] = array(
342 'name' => $name,
343 'value' => $id,
344 );
345 }
346 $elements['recipientMapping'] = $recipientMapping;
347
348 echo json_encode($elements);
349 CRM_Utils_System::civiExit();
350 }
351
352 static function mergeTags() {
353 $tagAId = CRM_Utils_Type::escape($_POST['fromId'], 'Integer');
354 $tagBId = CRM_Utils_Type::escape($_POST['toId'], 'Integer');
355
356 $result = CRM_Core_BAO_EntityTag::mergeTags($tagAId, $tagBId);
357
358 if (!empty($result['tagB_used_for'])) {
359 $usedFor = CRM_Core_OptionGroup::values('tag_used_for');
360 foreach ($result['tagB_used_for'] as & $val) {
361 $val = $usedFor[$val];
362 }
363 $result['tagB_used_for'] = implode(', ', $result['tagB_used_for']);
364 }
365
ebb5fc58
CW
366 $result['message'] = ts('"%1" has been merged with "%2". All records previously tagged "%1" are now tagged "%2".',
367 array(1 => $result['tagA'], 2 => $result['tagB'])
368 );
369
6a488035
TO
370 echo json_encode($result);
371 CRM_Utils_System::civiExit();
372 }
373
374 function recipient() {
375 $params = array('recipient');
376 foreach ($params as $param) {
377 $$param = CRM_Utils_Array::value($param, $_POST);
378 }
379
380 if (!$recipient) {
381 echo json_encode(array('error_msg' => 'required params missing.'));
382 CRM_Utils_System::civiExit();
383 }
384
385 switch ($recipient) {
386 case 'Participant Status':
387 $values = CRM_Event_PseudoConstant::participantStatus();
388 break;
389
40f2fee3 390 case 'participant_role':
6a488035
TO
391 $values = CRM_Event_PseudoConstant::participantRole();
392 break;
393
394 default:
395 exit;
396 }
397
398 $elements = array();
399 foreach ($values as $id => $name) {
400 $elements[] = array(
401 'name' => $name,
402 'value' => $id,
403 );
404 }
405
406 echo json_encode($elements);
407 CRM_Utils_System::civiExit();
408 }
409}
410