CRM-13422 - Consolidate extra rules for contact reminders
[civicrm-core.git] / CRM / Admin / Page / AJAX.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 */
33
34/**
ce064e4f 35 * This class contains all the function that are called using AJAX.
6a488035
TO
36 */
37class CRM_Admin_Page_AJAX {
38
39 /**
ce064e4f 40 * CRM-12337 Output navigation menu as executable javascript.
41 *
73538697
CW
42 * @see smarty_function_crmNavigationMenu
43 */
00be9182 44 public static function getNavigationMenu() {
3d527838 45 $contactID = CRM_Core_Session::singleton()->get('userID');
73538697 46 if ($contactID) {
4cc9b813 47 CRM_Core_Page_AJAX::setJsHeaders();
9b7526a8 48 print CRM_Core_Smarty::singleton()->fetchWith('CRM/common/navigation.js.tpl', array(
3d527838
CW
49 'navigation' => CRM_Core_BAO_Navigation::createNavigation($contactID),
50 ));
73538697 51 }
0a15daa2 52 CRM_Utils_System::civiExit();
73538697
CW
53 }
54
55 /**
eceb18cc 56 * Return menu tree as json data for editing.
6a488035 57 */
00be9182 58 public static function getNavigationList() {
6a488035
TO
59 echo CRM_Core_BAO_Navigation::buildNavigation(TRUE, FALSE);
60 CRM_Utils_System::civiExit();
61 }
62
63 /**
ce064e4f 64 * Process drag/move action for menu tree.
6a488035 65 */
00be9182 66 public static function menuTree() {
73538697 67 CRM_Core_BAO_Navigation::processNavigation($_GET);
6a488035
TO
68 }
69
70 /**
ce064e4f 71 * Build status message while enabling/ disabling various objects.
6a488035 72 */
00be9182 73 public static function getStatusMsg() {
02fc859b 74 require_once 'api/v3/utils.php';
353ffa53 75 $recordID = CRM_Utils_Type::escape($_GET['id'], 'Integer');
12798ddc 76 $entity = CRM_Utils_Type::escape($_GET['entity'], 'String');
4d17a233 77 $ret = array();
6a488035 78
4d17a233 79 if ($recordID && $entity && $recordBAO = _civicrm_api3_get_BAO($entity)) {
6a488035
TO
80 switch ($recordBAO) {
81 case 'CRM_Core_BAO_UFGroup':
6a488035
TO
82 $method = 'getUFJoinRecord';
83 $result = array($recordBAO, $method);
84 $ufJoin = call_user_func_array(($result), array($recordID, TRUE));
85 if (!empty($ufJoin)) {
4d17a233 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?');
6a488035
TO
87 }
88 else {
4d17a233 89 $ret['content'] = ts('Are you sure you want to disable this profile?');
6a488035
TO
90 }
91 break;
92
9da8dc8c 93 case 'CRM_Price_BAO_PriceSet':
9da8dc8c 94 $usedBy = CRM_Price_BAO_PriceSet::getUsedBy($recordID);
95 $priceSet = CRM_Price_BAO_PriceSet::getTitle($recordID);
6a488035
TO
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',
21dfd5f5 103 'EventTemplate' => 'civicrm_event_template',
6a488035
TO
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
4d17a233 113 $ret['illegal'] = TRUE;
353ffa53 114 $table = $template->fetch('CRM/Price/Page/table.tpl');
4d17a233 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(
7c550ca0 116 1 => $priceSet,
353ffa53 117 )) . "<br/> $table";
6a488035
TO
118 }
119 else {
4d17a233 120 $ret['content'] = ts('Are you sure you want to disable \'%1\' Price Set?', array(1 => $priceSet));
6a488035
TO
121 }
122 break;
123
124 case 'CRM_Event_BAO_Event':
4d17a233 125 $ret['content'] = ts('Are you sure you want to disable this Event?');
6a488035
TO
126 break;
127
128 case 'CRM_Core_BAO_UFField':
4d17a233 129 $ret['content'] = ts('Are you sure you want to disable this CiviCRM Profile field?');
6a488035
TO
130 break;
131
132 case 'CRM_Contribute_BAO_ManagePremiums':
4d17a233 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.');
6a488035
TO
134 break;
135
6e4cdf9c
CW
136 case 'CRM_Contact_BAO_Relationship':
137 $ret['content'] = ts('Are you sure you want to disable this relationship?');
138 break;
139
6a488035 140 case 'CRM_Contact_BAO_RelationshipType':
4d17a233 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.');
6a488035
TO
142 break;
143
7b3622bf 144 case 'CRM_Financial_BAO_FinancialType':
4d17a233 145 $ret['content'] = ts('Are you sure you want to disable this financial type?');
6a488035 146 break;
8ef12e64 147
7b3622bf
PN
148 case 'CRM_Financial_BAO_FinancialAccount':
149 if (!CRM_Financial_BAO_FinancialAccount::getARAccounts($recordID)) {
4d17a233
CW
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).');
7b3622bf
PN
152 }
153 else {
4d17a233 154 $ret['content'] = ts('Are you sure you want to disable this financial account?');
7b3622bf
PN
155 }
156 break;
6a488035 157
8ef12e64 158 case 'CRM_Financial_BAO_PaymentProcessor':
4d17a233 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.');
6a488035
TO
160 break;
161
162 case 'CRM_Financial_BAO_PaymentProcessorType':
4d17a233 163 $ret['content'] = ts('Are you sure you want to disable this payment processor type?');
6a488035
TO
164 break;
165
166 case 'CRM_Core_BAO_LocationType':
4d17a233 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.');
6a488035
TO
168 break;
169
170 case 'CRM_Event_BAO_ParticipantStatusType':
4d17a233 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.');
6a488035
TO
172 break;
173
174 case 'CRM_Mailing_BAO_Component':
4d17a233 175 $ret['content'] = ts('Are you sure you want to disable this component?');
6a488035
TO
176 break;
177
178 case 'CRM_Core_BAO_CustomField':
4d17a233 179 $ret['content'] = ts('Are you sure you want to disable this custom data field?');
6a488035
TO
180 break;
181
182 case 'CRM_Core_BAO_CustomGroup':
4d17a233 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.');
6a488035
TO
184 break;
185
c6327d7d 186 case 'CRM_Core_BAO_MessageTemplate':
4d17a233 187 $ret['content'] = ts('Are you sure you want to disable this message tempate?');
6a488035
TO
188 break;
189
190 case 'CRM_ACL_BAO_ACL':
4d17a233 191 $ret['content'] = ts('Are you sure you want to disable this ACL?');
6a488035
TO
192 break;
193
194 case 'CRM_ACL_BAO_EntityRole':
4d17a233 195 $ret['content'] = ts('Are you sure you want to disable this ACL Role Assignment?');
6a488035
TO
196 break;
197
198 case 'CRM_Member_BAO_MembershipType':
4d17a233 199 $ret['content'] = ts('Are you sure you want to disable this membership type?');
6a488035
TO
200 break;
201
202 case 'CRM_Member_BAO_MembershipStatus':
4d17a233 203 $ret['content'] = ts('Are you sure you want to disable this membership status rule?');
6a488035
TO
204 break;
205
9da8dc8c 206 case 'CRM_Price_BAO_PriceField':
4d17a233 207 $ret['content'] = ts('Are you sure you want to disable this price field?');
6a488035
TO
208 break;
209
210 case 'CRM_Contact_BAO_Group':
4d17a233 211 $ret['content'] = ts('Are you sure you want to disable this Group?');
6a488035
TO
212 break;
213
214 case 'CRM_Core_BAO_OptionGroup':
4d17a233 215 $ret['content'] = ts('Are you sure you want to disable this Option?');
6a488035
TO
216 break;
217
218 case 'CRM_Contact_BAO_ContactType':
4d17a233 219 $ret['content'] = ts('Are you sure you want to disable this Contact Type?');
6a488035
TO
220 break;
221
222 case 'CRM_Core_BAO_OptionValue':
6a488035 223 $label = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $recordID, 'label');
4d17a233
CW
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.');
6a488035
TO
226 break;
227
228 case 'CRM_Contribute_BAO_ContributionRecur':
229 $recurDetails = CRM_Contribute_BAO_ContributionRecur::getSubscriptionDetails($recordID);
4d17a233
CW
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>';
6a488035 232 if ($recurDetails->membership_id) {
4d17a233 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>';
6a488035
TO
234 }
235 break;
8ef12e64 236
6a488035 237 default:
4d17a233 238 $ret['content'] = ts('Are you sure you want to disable this record?');
6a488035
TO
239 break;
240 }
241 }
4d17a233
CW
242 else {
243 $ret = array('status' => 'error', 'content' => 'Error: Unknown entity type.', 'illegal' => TRUE);
244 }
245 CRM_Core_Page_AJAX::returnJsonResponse($ret);
6a488035
TO
246 }
247
00be9182 248 public static function mergeTagList() {
2e02f487 249 $name = CRM_Utils_Type::escape($_GET['term'], 'String');
6a488035 250 $fromId = CRM_Utils_Type::escape($_GET['fromId'], 'Integer');
89595c92 251 $limit = Civi::settings()->get('search_autocomplete_count');
6a488035
TO
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
8ef12e64 268FROM civicrm_tag t1
6a488035 269LEFT JOIN civicrm_tag t2 ON t1.parent_id = t2.id
8ef12e64 270WHERE t1.id <> {$fromId} AND
6a488035 271 t1.name LIKE '%{$name}%' AND
8ef12e64 272 ({$usedForClause})
6a488035
TO
273LIMIT $limit";
274 $dao = CRM_Core_DAO::executeQuery($query);
ebb5fc58 275 $result = array();
6a488035
TO
276
277 while ($dao->fetch()) {
ebb5fc58
CW
278 $row = array(
279 'id' => $dao->id,
280 'text' => ($dao->parent ? "{$dao->parent} :: " : '') . $dao->name,
281 );
282 // Add warning about used_for types
6a488035
TO
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)) {
ebb5fc58 288 $row['warning'] = TRUE;
6a488035
TO
289 }
290 }
ebb5fc58 291 $result[] = $row;
6a488035 292 }
ecdef330 293 CRM_Utils_JSON::output($result);
6a488035
TO
294 }
295
f8b84800 296 /**
297 * Get a list of mappings.
298 *
299 * This appears to be only used by scheduled reminders.
300 */
301 static public function mappingList() {
581c7be2
CW
302 if (empty($_GET['mappingID'])) {
303 CRM_Utils_JSON::output(array('status' => 'error', 'error_msg' => 'required params missing.'));
6a488035
TO
304 }
305
dc3c81b1 306 $selectionOptions = CRM_Core_BAO_ActionSchedule::getSelection1($_GET['mappingID'], $_GET['isLimit']);
6a488035 307
581c7be2
CW
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 }
6a488035 320 }
6a488035 321
581c7be2 322 CRM_Utils_JSON::output($output);
6a488035
TO
323 }
324
00be9182 325 public static function mergeTags() {
6a488035
TO
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
ebb5fc58
CW
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
ecdef330 343 CRM_Utils_JSON::output($result);
6a488035
TO
344 }
345
6a488035 346}