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