phpcs - Fix error, "CONST keyword must be lowercase; expected const but found CONST"
[civicrm-core.git] / CRM / Admin / Page / AJAX.php
... / ...
CommitLineData
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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 * $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 /**
42 * CRM-12337 Output navigation menu as executable javascript
43 * @see smarty_function_crmNavigationMenu
44 */
45 static function getNavigationMenu() {
46 $contactID = CRM_Core_Session::singleton()->get('userID');
47 if ($contactID) {
48 CRM_Core_Page_AJAX::setJsHeaders();
49 print CRM_Core_Smarty::singleton()->fetchWith('CRM/common/navigation.js.tpl', array(
50 'navigation' => CRM_Core_BAO_Navigation::createNavigation($contactID),
51 ));
52 }
53 CRM_Utils_System::civiExit();
54 }
55
56 /**
57 * Return menu tree as json data for editing
58 */
59 static function getNavigationList() {
60 echo CRM_Core_BAO_Navigation::buildNavigation(TRUE, FALSE);
61 CRM_Utils_System::civiExit();
62 }
63
64 /**
65 * Process drag/move action for menu tree
66 */
67 static function menuTree() {
68 CRM_Core_BAO_Navigation::processNavigation($_GET);
69 }
70
71 /**
72 * Build status message while
73 * enabling/ disabling various objects
74 */
75 static function getStatusMsg() {
76 require_once('api/v3/utils.php');
77 $recordID = CRM_Utils_Type::escape($_GET['id'], 'Integer');
78 $entity = CRM_Utils_Type::escape($_GET['entity'], 'String');
79 $ret = array();
80
81 if ($recordID && $entity && $recordBAO = _civicrm_api3_get_BAO($entity)) {
82 switch ($recordBAO) {
83 case 'CRM_Core_BAO_UFGroup':
84 $method = 'getUFJoinRecord';
85 $result = array($recordBAO, $method);
86 $ufJoin = call_user_func_array(($result), array($recordID, TRUE));
87 if (!empty($ufJoin)) {
88 $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?');
89 }
90 else {
91 $ret['content'] = ts('Are you sure you want to disable this profile?');
92 }
93 break;
94
95 case 'CRM_Price_BAO_PriceSet':
96 $usedBy = CRM_Price_BAO_PriceSet::getUsedBy($recordID);
97 $priceSet = CRM_Price_BAO_PriceSet::getTitle($recordID);
98
99 if (!CRM_Utils_System::isNull($usedBy)) {
100 $template = CRM_Core_Smarty::singleton();
101 $template->assign('usedBy', $usedBy);
102 $comps = array(
103 'Event' => 'civicrm_event',
104 'Contribution' => 'civicrm_contribution_page',
105 'EventTemplate' => 'civicrm_event_template'
106 );
107 $contexts = array();
108 foreach ($comps as $name => $table) {
109 if (array_key_exists($table, $usedBy)) {
110 $contexts[] = $name;
111 }
112 }
113 $template->assign('contexts', $contexts);
114
115 $ret['illegal'] = TRUE;
116 $table = $template->fetch('CRM/Price/Page/table.tpl');
117 $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(
118 1 => $priceSet)) . "<br/> $table";
119 }
120 else {
121 $ret['content'] = ts('Are you sure you want to disable \'%1\' Price Set?', array(1 => $priceSet));
122 }
123 break;
124
125 case 'CRM_Event_BAO_Event':
126 $ret['content'] = ts('Are you sure you want to disable this Event?');
127 break;
128
129 case 'CRM_Core_BAO_UFField':
130 $ret['content'] = ts('Are you sure you want to disable this CiviCRM Profile field?');
131 break;
132
133 case 'CRM_Contribute_BAO_ManagePremiums':
134 $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.');
135 break;
136
137 case 'CRM_Contact_BAO_Relationship':
138 $ret['content'] = ts('Are you sure you want to disable this relationship?');
139 break;
140
141 case 'CRM_Contact_BAO_RelationshipType':
142 $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.');
143 break;
144
145 case 'CRM_Financial_BAO_FinancialType':
146 $ret['content'] = ts('Are you sure you want to disable this financial type?');
147 break;
148
149 case 'CRM_Financial_BAO_FinancialAccount':
150 if (!CRM_Financial_BAO_FinancialAccount::getARAccounts($recordID)) {
151 $ret['illegal'] = TRUE;
152 $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).');
153 }
154 else {
155 $ret['content'] = ts('Are you sure you want to disable this financial account?');
156 }
157 break;
158
159 case 'CRM_Financial_BAO_PaymentProcessor':
160 $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.');
161 break;
162
163 case 'CRM_Financial_BAO_PaymentProcessorType':
164 $ret['content'] = ts('Are you sure you want to disable this payment processor type?');
165 break;
166
167 case 'CRM_Core_BAO_LocationType':
168 $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.');
169 break;
170
171 case 'CRM_Event_BAO_ParticipantStatusType':
172 $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.');
173 break;
174
175 case 'CRM_Mailing_BAO_Component':
176 $ret['content'] = ts('Are you sure you want to disable this component?');
177 break;
178
179 case 'CRM_Core_BAO_CustomField':
180 $ret['content'] = ts('Are you sure you want to disable this custom data field?');
181 break;
182
183 case 'CRM_Core_BAO_CustomGroup':
184 $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.');
185 break;
186
187 case 'CRM_Core_BAO_MessageTemplate':
188 $ret['content'] = ts('Are you sure you want to disable this message tempate?');
189 break;
190
191 case 'CRM_ACL_BAO_ACL':
192 $ret['content'] = ts('Are you sure you want to disable this ACL?');
193 break;
194
195 case 'CRM_ACL_BAO_EntityRole':
196 $ret['content'] = ts('Are you sure you want to disable this ACL Role Assignment?');
197 break;
198
199 case 'CRM_Member_BAO_MembershipType':
200 $ret['content'] = ts('Are you sure you want to disable this membership type?');
201 break;
202
203 case 'CRM_Member_BAO_MembershipStatus':
204 $ret['content'] = ts('Are you sure you want to disable this membership status rule?');
205 break;
206
207 case 'CRM_Price_BAO_PriceField':
208 $ret['content'] = ts('Are you sure you want to disable this price field?');
209 break;
210
211 case 'CRM_Contact_BAO_Group':
212 $ret['content'] = ts('Are you sure you want to disable this Group?');
213 break;
214
215 case 'CRM_Core_BAO_OptionGroup':
216 $ret['content'] = ts('Are you sure you want to disable this Option?');
217 break;
218
219 case 'CRM_Contact_BAO_ContactType':
220 $ret['content'] = ts('Are you sure you want to disable this Contact Type?');
221 break;
222
223 case 'CRM_Core_BAO_OptionValue':
224 $label = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $recordID, 'label');
225 $ret['content'] = ts('Are you sure you want to disable the \'%1\' option ?', array(1 => $label));
226 $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.');
227 break;
228
229 case 'CRM_Contribute_BAO_ContributionRecur':
230 $recurDetails = CRM_Contribute_BAO_ContributionRecur::getSubscriptionDetails($recordID);
231 $ret['content'] = ts('Are you sure you want to mark this recurring contribution as cancelled?');
232 $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>';
233 if ($recurDetails->membership_id) {
234 $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>';
235 }
236 break;
237
238 default:
239 $ret['content'] = ts('Are you sure you want to disable this record?');
240 break;
241 }
242 }
243 else {
244 $ret = array('status' => 'error', 'content' => 'Error: Unknown entity type.', 'illegal' => TRUE);
245 }
246 CRM_Core_Page_AJAX::returnJsonResponse($ret);
247 }
248
249 static function mergeTagList() {
250 $name = CRM_Utils_Type::escape($_GET['term'], 'String');
251 $fromId = CRM_Utils_Type::escape($_GET['fromId'], 'Integer');
252 $limit = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'search_autocomplete_count', NULL, 10);
253
254 // build used-for clause to be used in main query
255 $usedForTagA = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $fromId, 'used_for');
256 $usedForClause = array();
257 if ($usedForTagA) {
258 $usedForTagA = explode(",", $usedForTagA);
259 foreach ($usedForTagA as $key => $value) {
260 $usedForClause[] = "t1.used_for LIKE '%{$value}%'";
261 }
262 }
263 $usedForClause = !empty($usedForClause) ? implode(' OR ', $usedForClause) : '1';
264 sort($usedForTagA);
265
266 // query to list mergable tags
267 $query = "
268SELECT t1.name, t1.id, t1.used_for, t2.name as parent
269FROM civicrm_tag t1
270LEFT JOIN civicrm_tag t2 ON t1.parent_id = t2.id
271WHERE t1.id <> {$fromId} AND
272 t1.name LIKE '%{$name}%' AND
273 ({$usedForClause})
274LIMIT $limit";
275 $dao = CRM_Core_DAO::executeQuery($query);
276 $result = array();
277
278 while ($dao->fetch()) {
279 $row = array(
280 'id' => $dao->id,
281 'text' => ($dao->parent ? "{$dao->parent} :: " : '') . $dao->name,
282 );
283 // Add warning about used_for types
284 if (!empty($dao->used_for)) {
285 $usedForTagB = explode(',', $dao->used_for);
286 sort($usedForTagB);
287 $usedForDiff = array_diff($usedForTagA, $usedForTagB);
288 if (!empty($usedForDiff)) {
289 $row['warning'] = TRUE;
290 }
291 }
292 $result[] = $row;
293 }
294 CRM_Utils_JSON::output($result);
295 }
296
297 function mappingList() {
298 if (empty($_GET['mappingID'])) {
299 CRM_Utils_JSON::output(array('status' => 'error', 'error_msg' => 'required params missing.'));
300 }
301
302 $selectionOptions = CRM_Core_BAO_ActionSchedule::getSelection1($_GET['mappingID']);
303
304 $output = array(
305 'sel4' => array(),
306 'sel5' => array(),
307 'recipientMapping' => $selectionOptions['recipientMapping'],
308 );
309 foreach (array(4, 5) as $sel) {
310 foreach ($selectionOptions["sel$sel"] as $id => $name) {
311 $output["sel$sel"][] = array(
312 'value' => $name,
313 'key' => $id,
314 );
315 }
316 }
317
318 CRM_Utils_JSON::output($output);
319 }
320
321 static function mergeTags() {
322 $tagAId = CRM_Utils_Type::escape($_POST['fromId'], 'Integer');
323 $tagBId = CRM_Utils_Type::escape($_POST['toId'], 'Integer');
324
325 $result = CRM_Core_BAO_EntityTag::mergeTags($tagAId, $tagBId);
326
327 if (!empty($result['tagB_used_for'])) {
328 $usedFor = CRM_Core_OptionGroup::values('tag_used_for');
329 foreach ($result['tagB_used_for'] as & $val) {
330 $val = $usedFor[$val];
331 }
332 $result['tagB_used_for'] = implode(', ', $result['tagB_used_for']);
333 }
334
335 $result['message'] = ts('"%1" has been merged with "%2". All records previously tagged "%1" are now tagged "%2".',
336 array(1 => $result['tagA'], 2 => $result['tagB'])
337 );
338
339 CRM_Utils_JSON::output($result);
340 }
341
342}
343