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