Merge pull request #13044 from giovannidalmas1967/giovannidalmas1967-patch-1
[civicrm-core.git] / CRM / Admin / Page / AJAX.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
8c9251b3 6 | Copyright CiviCRM LLC (c) 2004-2018 |
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
8c9251b3 31 * @copyright CiviCRM LLC (c) 2004-2018
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();
7958c588 48 $smarty = CRM_Core_Smarty::singleton();
4235341b 49 $smarty->assign('quicksearchOptions', self::getSearchOptions());
7958c588 50 print $smarty->fetchWith('CRM/common/navigation.js.tpl', array(
2e42cb18 51 'navigation' => CRM_Core_BAO_Navigation::createNavigation(),
3d527838 52 ));
73538697 53 }
0a15daa2 54 CRM_Utils_System::civiExit();
73538697
CW
55 }
56
4235341b
CW
57 public static function getSearchOptions() {
58 $searchOptions = Civi::settings()->get('quicksearch_options');
59 $searchOptions[] = 'sort_name';
60 $searchOptions = array_intersect_key(CRM_Core_SelectValues::quicksearchOptions(), array_flip($searchOptions));
61 foreach ($searchOptions as $key => $label) {
62 if (strpos($key, 'custom_') === 0) {
63 unset($searchOptions[$key]);
64 $id = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', substr($key, 7), 'id', 'name');
65 $searchOptions["custom_$id"] = $label;
66 }
67 }
68 return $searchOptions;
69 }
70
6a488035 71 /**
ce064e4f 72 * Process drag/move action for menu tree.
6a488035 73 */
00be9182 74 public static function menuTree() {
73538697 75 CRM_Core_BAO_Navigation::processNavigation($_GET);
6a488035
TO
76 }
77
78 /**
ce064e4f 79 * Build status message while enabling/ disabling various objects.
6a488035 80 */
00be9182 81 public static function getStatusMsg() {
02fc859b 82 require_once 'api/v3/utils.php';
353ffa53 83 $recordID = CRM_Utils_Type::escape($_GET['id'], 'Integer');
12798ddc 84 $entity = CRM_Utils_Type::escape($_GET['entity'], 'String');
4d17a233 85 $ret = array();
6a488035 86
4d17a233 87 if ($recordID && $entity && $recordBAO = _civicrm_api3_get_BAO($entity)) {
6a488035
TO
88 switch ($recordBAO) {
89 case 'CRM_Core_BAO_UFGroup':
6a488035
TO
90 $method = 'getUFJoinRecord';
91 $result = array($recordBAO, $method);
92 $ufJoin = call_user_func_array(($result), array($recordID, TRUE));
93 if (!empty($ufJoin)) {
4d17a233 94 $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
95 }
96 else {
4d17a233 97 $ret['content'] = ts('Are you sure you want to disable this profile?');
6a488035
TO
98 }
99 break;
100
9da8dc8c 101 case 'CRM_Price_BAO_PriceSet':
9da8dc8c 102 $usedBy = CRM_Price_BAO_PriceSet::getUsedBy($recordID);
103 $priceSet = CRM_Price_BAO_PriceSet::getTitle($recordID);
6a488035
TO
104
105 if (!CRM_Utils_System::isNull($usedBy)) {
106 $template = CRM_Core_Smarty::singleton();
107 $template->assign('usedBy', $usedBy);
108 $comps = array(
109 'Event' => 'civicrm_event',
110 'Contribution' => 'civicrm_contribution_page',
21dfd5f5 111 'EventTemplate' => 'civicrm_event_template',
6a488035
TO
112 );
113 $contexts = array();
114 foreach ($comps as $name => $table) {
115 if (array_key_exists($table, $usedBy)) {
116 $contexts[] = $name;
117 }
118 }
119 $template->assign('contexts', $contexts);
120
4d17a233 121 $ret['illegal'] = TRUE;
353ffa53 122 $table = $template->fetch('CRM/Price/Page/table.tpl');
4d17a233 123 $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 124 1 => $priceSet,
353ffa53 125 )) . "<br/> $table";
6a488035
TO
126 }
127 else {
4d17a233 128 $ret['content'] = ts('Are you sure you want to disable \'%1\' Price Set?', array(1 => $priceSet));
6a488035
TO
129 }
130 break;
131
132 case 'CRM_Event_BAO_Event':
4d17a233 133 $ret['content'] = ts('Are you sure you want to disable this Event?');
6a488035
TO
134 break;
135
136 case 'CRM_Core_BAO_UFField':
4d17a233 137 $ret['content'] = ts('Are you sure you want to disable this CiviCRM Profile field?');
6a488035
TO
138 break;
139
37828d4f 140 case 'CRM_Contribute_BAO_Product':
4d17a233 141 $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
142 break;
143
6e4cdf9c
CW
144 case 'CRM_Contact_BAO_Relationship':
145 $ret['content'] = ts('Are you sure you want to disable this relationship?');
146 break;
147
6a488035 148 case 'CRM_Contact_BAO_RelationshipType':
4d17a233 149 $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
150 break;
151
7b3622bf 152 case 'CRM_Financial_BAO_FinancialType':
4d17a233 153 $ret['content'] = ts('Are you sure you want to disable this financial type?');
6a488035 154 break;
8ef12e64 155
7b3622bf
PN
156 case 'CRM_Financial_BAO_FinancialAccount':
157 if (!CRM_Financial_BAO_FinancialAccount::getARAccounts($recordID)) {
4d17a233
CW
158 $ret['illegal'] = TRUE;
159 $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
160 }
161 else {
4d17a233 162 $ret['content'] = ts('Are you sure you want to disable this financial account?');
7b3622bf
PN
163 }
164 break;
6a488035 165
8ef12e64 166 case 'CRM_Financial_BAO_PaymentProcessor':
4d17a233 167 $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
168 break;
169
170 case 'CRM_Financial_BAO_PaymentProcessorType':
4d17a233 171 $ret['content'] = ts('Are you sure you want to disable this payment processor type?');
6a488035
TO
172 break;
173
174 case 'CRM_Core_BAO_LocationType':
4d17a233 175 $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
176 break;
177
178 case 'CRM_Event_BAO_ParticipantStatusType':
4d17a233 179 $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
180 break;
181
4825de4a 182 case 'CRM_Mailing_BAO_MailingComponent':
4d17a233 183 $ret['content'] = ts('Are you sure you want to disable this component?');
6a488035
TO
184 break;
185
186 case 'CRM_Core_BAO_CustomField':
4d17a233 187 $ret['content'] = ts('Are you sure you want to disable this custom data field?');
6a488035
TO
188 break;
189
190 case 'CRM_Core_BAO_CustomGroup':
4d17a233 191 $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
192 break;
193
c6327d7d 194 case 'CRM_Core_BAO_MessageTemplate':
4d17a233 195 $ret['content'] = ts('Are you sure you want to disable this message tempate?');
6a488035
TO
196 break;
197
198 case 'CRM_ACL_BAO_ACL':
4d17a233 199 $ret['content'] = ts('Are you sure you want to disable this ACL?');
6a488035
TO
200 break;
201
202 case 'CRM_ACL_BAO_EntityRole':
4d17a233 203 $ret['content'] = ts('Are you sure you want to disable this ACL Role Assignment?');
6a488035
TO
204 break;
205
206 case 'CRM_Member_BAO_MembershipType':
4d17a233 207 $ret['content'] = ts('Are you sure you want to disable this membership type?');
6a488035
TO
208 break;
209
210 case 'CRM_Member_BAO_MembershipStatus':
4d17a233 211 $ret['content'] = ts('Are you sure you want to disable this membership status rule?');
6a488035
TO
212 break;
213
9da8dc8c 214 case 'CRM_Price_BAO_PriceField':
4d17a233 215 $ret['content'] = ts('Are you sure you want to disable this price field?');
6a488035
TO
216 break;
217
218 case 'CRM_Contact_BAO_Group':
4d17a233 219 $ret['content'] = ts('Are you sure you want to disable this Group?');
240c0ebd 220 $ret['content'] .= '<br /><br /><strong>' . ts('WARNING - Disabling this group will disable all the child groups associated if any.') . '</strong>';
6a488035
TO
221 break;
222
223 case 'CRM_Core_BAO_OptionGroup':
4d17a233 224 $ret['content'] = ts('Are you sure you want to disable this Option?');
6a488035
TO
225 break;
226
227 case 'CRM_Contact_BAO_ContactType':
4d17a233 228 $ret['content'] = ts('Are you sure you want to disable this Contact Type?');
6a488035
TO
229 break;
230
231 case 'CRM_Core_BAO_OptionValue':
6a488035 232 $label = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $recordID, 'label');
4d17a233
CW
233 $ret['content'] = ts('Are you sure you want to disable the \'%1\' option ?', array(1 => $label));
234 $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
235 break;
236
237 case 'CRM_Contribute_BAO_ContributionRecur':
238 $recurDetails = CRM_Contribute_BAO_ContributionRecur::getSubscriptionDetails($recordID);
4d17a233
CW
239 $ret['content'] = ts('Are you sure you want to mark this recurring contribution as cancelled?');
240 $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 241 if ($recurDetails->membership_id) {
4d17a233 242 $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
243 }
244 break;
8ef12e64 245
6a488035 246 default:
4d17a233 247 $ret['content'] = ts('Are you sure you want to disable this record?');
6a488035
TO
248 break;
249 }
250 }
4d17a233
CW
251 else {
252 $ret = array('status' => 'error', 'content' => 'Error: Unknown entity type.', 'illegal' => TRUE);
253 }
254 CRM_Core_Page_AJAX::returnJsonResponse($ret);
6a488035
TO
255 }
256
f8b84800 257 /**
258 * Get a list of mappings.
259 *
260 * This appears to be only used by scheduled reminders.
261 */
262 static public function mappingList() {
581c7be2
CW
263 if (empty($_GET['mappingID'])) {
264 CRM_Utils_JSON::output(array('status' => 'error', 'error_msg' => 'required params missing.'));
6a488035
TO
265 }
266
0905ee5d
TO
267 $mapping = CRM_Core_BAO_ActionSchedule::getMapping($_GET['mappingID']);
268 $dateFieldLabels = $mapping ? $mapping->getDateFields() : array();
9d97a648
TO
269
270 // The UX here is quirky -- for "Activity" types, there's a simple drop "Recipients"
271 // dropdown which is always displayed. For other types, the "Recipients" drop down is
272 // conditional upon the weird isLimit ('Limit To / Also Include / Neither') dropdown.
273 $noThanksJustKidding = !$_GET['isLimit'];
274 if ($mapping instanceof CRM_Activity_ActionMapping || !$noThanksJustKidding) {
275 $entityRecipientLabels = $mapping ? ($mapping->getRecipientTypes() + CRM_Core_BAO_ActionSchedule::getAdditionalRecipients()) : array();
276 }
277 else {
278 $entityRecipientLabels = CRM_Core_BAO_ActionSchedule::getAdditionalRecipients();
279 }
0905ee5d 280 $recipientMapping = array_combine(array_keys($entityRecipientLabels), array_keys($entityRecipientLabels));
6a488035 281
581c7be2 282 $output = array(
dde482e0
CW
283 'sel4' => CRM_Utils_Array::makeNonAssociative($dateFieldLabels),
284 'sel5' => CRM_Utils_Array::makeNonAssociative($entityRecipientLabels),
0905ee5d 285 'recipientMapping' => $recipientMapping,
581c7be2 286 );
6a488035 287
581c7be2 288 CRM_Utils_JSON::output($output);
6a488035
TO
289 }
290
bf2ce887
TO
291 /**
292 * (Scheduled Reminders) Get the list of possible recipient filters.
293 *
294 * Ex: GET /civicrm/ajax/recipientListing?mappingID=contribpage&recipientType=
295 */
296 public static function recipientListing() {
297 $mappingID = filter_input(INPUT_GET, 'mappingID', FILTER_VALIDATE_REGEXP, array(
298 'options' => array(
299 'regexp' => '/^[a-zA-Z0-9_\-]+$/',
300 ),
301 ));
302 $recipientType = filter_input(INPUT_GET, 'recipientType', FILTER_VALIDATE_REGEXP, array(
303 'options' => array(
304 'regexp' => '/^[a-zA-Z0-9_\-]+$/',
305 ),
306 ));
307
308 CRM_Utils_JSON::output(array(
dde482e0 309 'recipients' => CRM_Utils_Array::makeNonAssociative(CRM_Core_BAO_ActionSchedule::getRecipientListing($mappingID, $recipientType)),
bf2ce887
TO
310 ));
311 }
312
d3cbd0a5
CW
313 /**
314 * Outputs one branch in the tag tree
315 *
316 * Used by jstree to incrementally load tags
317 */
318 public static function getTagTree() {
319 $parent = CRM_Utils_Type::escape(CRM_Utils_Array::value('parent_id', $_GET, 0), 'Integer');
6e80ee11 320 $substring = CRM_Utils_Type::escape(CRM_Utils_Array::value('str', $_GET), 'String');
d3cbd0a5
CW
321 $result = array();
322
97dcf1d8 323 $whereClauses = array('is_tagset <> 1');
324 $orderColumn = 'name';
dfe61fcf 325
326 // fetch all child tags in Array('parent_tag' => array('child_tag_1', 'child_tag_2', ...)) format
6e80ee11 327 $childTagIDs = CRM_Core_BAO_Tag::getChildTags($substring);
328 $parentIDs = array_keys($childTagIDs);
329
97dcf1d8 330 if ($parent) {
331 $whereClauses[] = "parent_id = $parent";
332 }
333 elseif ($substring) {
6e80ee11 334 $whereClauses['substring'] = " name LIKE '%$substring%' ";
335 if (!empty($parentIDs)) {
97dcf1d8 336 $whereClauses['substring'] = sprintf(" %s OR id IN (%s) ", $whereClauses['substring'], implode(',', $parentIDs));
6e80ee11 337 }
97dcf1d8 338 $orderColumn = 'id';
339 }
340 else {
341 $whereClauses[] = "parent_id IS NULL";
6e80ee11 342 }
343
344 $dao = CRM_Utils_SQL_Select::from('civicrm_tag')
345 ->where($whereClauses)
346 ->groupBy('id')
97dcf1d8 347 ->orderBy($orderColumn)
6e80ee11 348 ->execute();
dfe61fcf 349
d3cbd0a5 350 while ($dao->fetch()) {
6e80ee11 351 if (!empty($substring)) {
352 $result[] = $dao->id;
353 if (!empty($childTagIDs[$dao->id])) {
354 $result = array_merge($result, $childTagIDs[$dao->id]);
355 }
356 }
357 else {
6e80ee11 358 $hasChildTags = empty($childTagIDs[$dao->id]) ? FALSE : TRUE;
359 $usedFor = (array) explode(',', $dao->used_for);
28dd94bd 360 $tag = [
6e80ee11 361 'id' => $dao->id,
362 'text' => $dao->name,
28dd94bd 363 'a_attr' => [
6e80ee11 364 'class' => 'crm-tag-item',
28dd94bd 365 ],
6e80ee11 366 'children' => $hasChildTags,
28dd94bd 367 'data' => [
6e80ee11 368 'description' => (string) $dao->description,
369 'is_selectable' => (bool) $dao->is_selectable,
370 'is_reserved' => (bool) $dao->is_reserved,
371 'used_for' => $usedFor,
372 'color' => $dao->color ? $dao->color : '#ffffff',
28dd94bd
CW
373 'usages' => civicrm_api3('EntityTag', 'getcount', [
374 'entity_table' => ['IN' => $usedFor],
6e80ee11 375 'tag_id' => $dao->id,
28dd94bd
CW
376 ]),
377 ],
378 ];
379 if ($dao->description || $dao->is_reserved) {
380 $tag['li_attr']['title'] = ((string) $dao->description) . ($dao->is_reserved ? ' (*' . ts('Reserved') . ')' : '');
381 }
382 if ($dao->is_reserved) {
383 $tag['li_attr']['class'] = 'is-reserved';
384 }
385 if ($dao->color) {
386 $tag['a_attr']['style'] = "background-color: {$dao->color}; color: " . CRM_Utils_Color::getContrast($dao->color);
387 }
388 $result[] = $tag;
d3cbd0a5 389 }
d3cbd0a5
CW
390 }
391
97dcf1d8 392 if ($substring) {
393 $result = array_values(array_unique($result));
394 }
395
dfe61fcf 396 if (!empty($_REQUEST['is_unit_test'])) {
397 return $result;
398 }
399
d3cbd0a5
CW
400 CRM_Utils_JSON::output($result);
401 }
402
6a488035 403}