Update version numbers to 4.4 and lint php
[civicrm-core.git] / CRM / Admin / Page / AJAX.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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 * Function to build menu tree
43 */
44 static function getNavigationList() {
45 echo CRM_Core_BAO_Navigation::buildNavigation(TRUE, FALSE);
46 CRM_Utils_System::civiExit();
47 }
48
49 /**
50 * Function to process drag/move action for menu tree
51 */
52 static function menuTree() {
53 echo CRM_Core_BAO_Navigation::processNavigation($_GET);
54 CRM_Utils_System::civiExit();
55 }
56
57 /**
58 * Function to build status message while
59 * enabling/ disabling various objects
60 */
61 static function getStatusMsg() {
62 $recordID = CRM_Utils_Type::escape($_POST['recordID'], 'Integer');
63 $recordBAO = CRM_Utils_Type::escape($_POST['recordBAO'], 'String');
64 $op = CRM_Utils_Type::escape($_POST['op'], 'String');
65 $show = NULL;
66
67 if ($op == 'disable-enable') {
68 $status = ts('Are you sure you want to enable this record?');
69 }
70 else {
71 switch ($recordBAO) {
72 case 'CRM_Core_BAO_UFGroup':
73 require_once (str_replace('_', DIRECTORY_SEPARATOR, $recordBAO) . '.php');
74 $method = 'getUFJoinRecord';
75 $result = array($recordBAO, $method);
76 $ufJoin = call_user_func_array(($result), array($recordID, TRUE));
77 if (!empty($ufJoin)) {
78 $status = 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?');
79 }
80 else {
81 $status = ts('Are you sure you want to disable this profile?');
82 }
83 break;
84
85 case 'CRM_Price_BAO_PriceSet':
86 require_once (str_replace('_', DIRECTORY_SEPARATOR, $recordBAO) . '.php');
87 $usedBy = CRM_Price_BAO_PriceSet::getUsedBy($recordID);
88 $priceSet = CRM_Price_BAO_PriceSet::getTitle($recordID);
89
90 if (!CRM_Utils_System::isNull($usedBy)) {
91 $template = CRM_Core_Smarty::singleton();
92 $template->assign('usedBy', $usedBy);
93 $comps = array(
94 'Event' => 'civicrm_event',
95 'Contribution' => 'civicrm_contribution_page',
96 'EventTemplate' => 'civicrm_event_template'
97 );
98 $contexts = array();
99 foreach ($comps as $name => $table) {
100 if (array_key_exists($table, $usedBy)) {
101 $contexts[] = $name;
102 }
103 }
104 $template->assign('contexts', $contexts);
105
106 $show = 'noButton';
107 $table = $template->fetch('CRM/Price/Page/table.tpl');
108 $status = ts('Unable to disable the \'%1\' price set - it is currently in use by one or more active events, contribution pages or contributions.', array(
109 1 => $priceSet)) . "<br/> $table";
110 }
111 else {
112 $status = ts('Are you sure you want to disable \'%1\' Price Set?', array(1 => $priceSet));
113 }
114 break;
115
116 case 'CRM_Event_BAO_Event':
117 $status = ts('Are you sure you want to disable this Event?');
118 break;
119
120 case 'CRM_Core_BAO_UFField':
121 $status = ts('Are you sure you want to disable this CiviCRM Profile field?');
122 break;
123
124 case 'CRM_Contribute_BAO_ManagePremiums':
125 $status = 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.');
126 break;
127
128 case 'CRM_Contact_BAO_RelationshipType':
129 $status = 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.');
130 break;
131
132 case 'CRM_Financial_BAO_FinancialType':
133 $status = ts('Are you sure you want to disable this financial type?');
134 break;
135
136 case 'CRM_Financial_BAO_FinancialAccount':
137 if (!CRM_Financial_BAO_FinancialAccount::getARAccounts($recordID)) {
138 $show = 'noButton';
139 $status = 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).');
140 }
141 else {
142 $status = ts('Are you sure you want to disable this financial account?');
143 }
144 break;
145
146 case 'CRM_Financial_BAO_PaymentProcessor':
147 $status = 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.');
148 break;
149
150 case 'CRM_Financial_BAO_PaymentProcessorType':
151 $status = ts('Are you sure you want to disable this payment processor type?');
152 break;
153
154 case 'CRM_Core_BAO_LocationType':
155 $status = 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.');
156 break;
157
158 case 'CRM_Event_BAO_ParticipantStatusType':
159 $status = 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.');
160 break;
161
162 case 'CRM_Mailing_BAO_Component':
163 $status = ts('Are you sure you want to disable this component?');
164 break;
165
166 case 'CRM_Core_BAO_CustomField':
167 $status = ts('Are you sure you want to disable this custom data field?');
168 break;
169
170 case 'CRM_Core_BAO_CustomGroup':
171 $status = 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.');
172 break;
173
174 case 'CRM_Core_BAO_MessageTemplates':
175 $status = ts('Are you sure you want to disable this message tempate?');
176 break;
177
178 case 'CRM_ACL_BAO_ACL':
179 $status = ts('Are you sure you want to disable this ACL?');
180 break;
181
182 case 'CRM_ACL_BAO_EntityRole':
183 $status = ts('Are you sure you want to disable this ACL Role Assignment?');
184 break;
185
186 case 'CRM_Member_BAO_MembershipType':
187 $status = ts('Are you sure you want to disable this membership type?');
188 break;
189
190 case 'CRM_Member_BAO_MembershipStatus':
191 $status = ts('Are you sure you want to disable this membership status rule?');
192 break;
193
194 case 'CRM_Price_BAO_PriceField':
195 $status = ts('Are you sure you want to disable this price field?');
196 break;
197
198 case 'CRM_Contact_BAO_Group':
199 $status = ts('Are you sure you want to disable this Group?');
200 break;
201
202 case 'CRM_Core_BAO_OptionGroup':
203 $status = ts('Are you sure you want to disable this Option?');
204 break;
205
206 case 'CRM_Contact_BAO_ContactType':
207 $status = ts('Are you sure you want to disable this Contact Type?');
208 break;
209
210 case 'CRM_Core_BAO_OptionValue':
211 require_once (str_replace('_', DIRECTORY_SEPARATOR, $recordBAO) . '.php');
212 $label = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $recordID, 'label');
213 $status = ts('Are you sure you want to disable the \'%1\' option ?', array(1 => $label));
214 $status .= '<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.');
215 break;
216
217 case 'CRM_Contribute_BAO_ContributionRecur':
218 $recurDetails = CRM_Contribute_BAO_ContributionRecur::getSubscriptionDetails($recordID);
219 $status = ts('Are you sure you want to mark this recurring contribution as cancelled?');
220 $status .= '<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>';
221 if ($recurDetails->membership_id) {
222 $status .= '<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>';
223 }
224 break;
225
226 case 'CRM_Batch_BAO_Batch':
227 if ($op == 'close') {
228 $status = ts('Are you sure you want to close this batch?');
229 }
230 elseif ($op == 'open') {
231 $status = ts('Are you sure you want to reopen this batch?');
232 }
233 elseif ($op == 'delete') {
234 $status = ts('Are you sure you want to delete this batch?');
235 }
236 elseif ($op == 'remove') {
237 $status = ts('Are you sure you want to remove this financial transaction?');
238 }
239 elseif ($op == 'export') {
240 $status = ts('Are you sure you want to close and export this batch?');
241 }
242 else {
243 $status = ts('Are you sure you want to assign this financial transaction to the batch?');
244 }
245 break;
246
247 default:
248 $status = ts('Are you sure you want to disable this record?');
249 break;
250 }
251 }
252 $statusMessage['status'] = $status;
253 $statusMessage['show'] = $show;
254
255 echo json_encode($statusMessage);
256 CRM_Utils_System::civiExit();
257 }
258
259 static function getTagList() {
260 $name = CRM_Utils_Type::escape($_GET['name'], 'String');
261 $parentId = CRM_Utils_Type::escape($_GET['parentId'], 'Integer');
262
263 $isSearch = NULL;
264 if (isset($_GET['search'])) {
265 $isSearch = CRM_Utils_Type::escape($_GET['search'], 'Integer');
266 }
267
268 $tags = array();
269
270 // always add current search term as possible tag
271 // here we append :::value to determine if existing / new tag should be created
272 if (!$isSearch) {
273 $tags[] = array(
274 'name' => $name,
275 'id' => $name . ":::value",
276 );
277 }
278
279 $query = "SELECT id, name FROM civicrm_tag WHERE parent_id = {$parentId} and name LIKE '%{$name}%'";
280 $dao = CRM_Core_DAO::executeQuery($query);
281
282 while ($dao->fetch()) {
283 // make sure we return tag name entered by user only if it does not exists in db
284 if ($name == $dao->name) {
285 $tags = array();
286 }
287 // escape double quotes, which break results js
288 $tags[] = array('name' => addcslashes($dao->name, '"'),
289 'id' => $dao->id,
290 );
291 }
292
293 echo json_encode($tags);
294 CRM_Utils_System::civiExit();
295 }
296
297 static function mergeTagList() {
298 $name = CRM_Utils_Type::escape($_GET['s'], 'String');
299 $fromId = CRM_Utils_Type::escape($_GET['fromId'], 'Integer');
300 $limit = CRM_Utils_Type::escape($_GET['limit'], 'Integer');
301
302 // build used-for clause to be used in main query
303 $usedForTagA = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $fromId, 'used_for');
304 $usedForClause = array();
305 if ($usedForTagA) {
306 $usedForTagA = explode(",", $usedForTagA);
307 foreach ($usedForTagA as $key => $value) {
308 $usedForClause[] = "t1.used_for LIKE '%{$value}%'";
309 }
310 }
311 $usedForClause = !empty($usedForClause) ? implode(' OR ', $usedForClause) : '1';
312 sort($usedForTagA);
313
314 // query to list mergable tags
315 $query = "
316 SELECT t1.name, t1.id, t1.used_for, t2.name as parent
317 FROM civicrm_tag t1
318 LEFT JOIN civicrm_tag t2 ON t1.parent_id = t2.id
319 WHERE t1.id <> {$fromId} AND
320 t1.name LIKE '%{$name}%' AND
321 ({$usedForClause})
322 LIMIT $limit";
323 $dao = CRM_Core_DAO::executeQuery($query);
324
325 while ($dao->fetch()) {
326 $warning = 0;
327 if (!empty($dao->used_for)) {
328 $usedForTagB = explode(',', $dao->used_for);
329 sort($usedForTagB);
330 $usedForDiff = array_diff($usedForTagA, $usedForTagB);
331 if (!empty($usedForDiff)) {
332 $warning = 1;
333 }
334 }
335 $tag = addcslashes($dao->name, '"') . "|{$dao->id}|{$warning}\n";
336 echo $tag = $dao->parent ? (addcslashes($dao->parent, '"') . ' :: ' . $tag) : $tag;
337 }
338 CRM_Utils_System::civiExit();
339 }
340
341 static function processTags() {
342 $skipTagCreate = $skipEntityAction = $entityId = NULL;
343 $action = CRM_Utils_Type::escape($_POST['action'], 'String');
344 $parentId = CRM_Utils_Type::escape($_POST['parentId'], 'Integer');
345 if ($_POST['entityId']) {
346 $entityId = CRM_Utils_Type::escape($_POST['entityId'], 'Integer');
347 }
348
349 $entityTable = CRM_Utils_Type::escape($_POST['entityTable'], 'String');
350
351 if ($_POST['skipTagCreate']) {
352 $skipTagCreate = CRM_Utils_Type::escape($_POST['skipTagCreate'], 'Integer');
353 }
354
355 if ($_POST['skipEntityAction']) {
356 $skipEntityAction = CRM_Utils_Type::escape($_POST['skipEntityAction'], 'Integer');
357 }
358
359 // check if user has selected existing tag or is creating new tag
360 // this is done to allow numeric tags etc.
361 $tagValue = explode(':::', $_POST['tagID']);
362
363 $createNewTag = FALSE;
364 $tagID = $tagValue[0];
365 if (isset($tagValue[1]) && $tagValue[1] == 'value') {
366 $createNewTag = TRUE;
367 }
368
369 $tagInfo = array();
370 // if action is select
371 if ($action == 'select') {
372 // check the value of tagID
373 // if numeric that means existing tag
374 // else create new tag
375 if (!$skipTagCreate && $createNewTag) {
376 $params = array(
377 'name' => $tagID,
378 'parent_id' => $parentId,
379 );
380
381 $tagObject = CRM_Core_BAO_Tag::add($params, CRM_Core_DAO::$_nullArray);
382
383 $tagInfo = array(
384 'name' => $tagID,
385 'id' => $tagObject->id,
386 'action' => $action,
387 );
388 $tagID = $tagObject->id;
389 }
390
391 if (!$skipEntityAction && $entityId) {
392 // save this tag to contact
393 $params = array(
394 'entity_table' => $entityTable,
395 'entity_id' => $entityId,
396 'tag_id' => $tagID,
397 );
398
399 CRM_Core_BAO_EntityTag::add($params);
400 }
401 // if action is delete
402 }
403 elseif ($action == 'delete') {
404 if (!is_numeric($tagID)) {
405 $tagID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $tagID, 'id', 'name');
406 }
407 if ($entityId) {
408 // delete this tag entry for the entity
409 $params = array(
410 'entity_table' => $entityTable,
411 'entity_id' => $entityId,
412 'tag_id' => $tagID,
413 );
414
415 CRM_Core_BAO_EntityTag::del($params);
416 }
417 $tagInfo = array(
418 'id' => $tagID,
419 'action' => $action,
420 );
421 }
422
423 echo json_encode($tagInfo);
424 CRM_Utils_System::civiExit();
425 }
426
427 function mappingList() {
428 $params = array('mappingID');
429 foreach ($params as $param) {
430 $$param = CRM_Utils_Array::value($param, $_POST);
431 }
432
433 if (!$mappingID) {
434 echo json_encode(array('error_msg' => 'required params missing.'));
435 CRM_Utils_System::civiExit();
436 }
437
438 $selectionOptions = CRM_Core_BAO_ActionSchedule::getSelection1($mappingID);
439 extract($selectionOptions);
440
441 $elements = array();
442 foreach ($sel4 as $id => $name) {
443 $elements[] = array(
444 'name' => $name,
445 'value' => $id,
446 );
447 }
448
449 echo json_encode($elements);
450 CRM_Utils_System::civiExit();
451 }
452
453 function mappingList1() {
454 $params = array('mappingID');
455 foreach ($params as $param) {
456 $$param = CRM_Utils_Array::value($param, $_POST);
457 }
458
459 if (!$mappingID) {
460 echo json_encode(array('error_msg' => 'required params missing.'));
461 CRM_Utils_System::civiExit();
462 }
463
464 $selectionOptions = CRM_Core_BAO_ActionSchedule::getSelection1($mappingID);
465 extract($selectionOptions);
466
467 $elements = array();
468 foreach ($sel5 as $id => $name) {
469 $elements['sel5'][] = array(
470 'name' => $name,
471 'value' => $id,
472 );
473 }
474 $elements['recipientMapping'] = $recipientMapping;
475
476 echo json_encode($elements);
477 CRM_Utils_System::civiExit();
478 }
479
480 static function mergeTags() {
481 $tagAId = CRM_Utils_Type::escape($_POST['fromId'], 'Integer');
482 $tagBId = CRM_Utils_Type::escape($_POST['toId'], 'Integer');
483
484 $result = CRM_Core_BAO_EntityTag::mergeTags($tagAId, $tagBId);
485
486 if (!empty($result['tagB_used_for'])) {
487 $usedFor = CRM_Core_OptionGroup::values('tag_used_for');
488 foreach ($result['tagB_used_for'] as & $val) {
489 $val = $usedFor[$val];
490 }
491 $result['tagB_used_for'] = implode(', ', $result['tagB_used_for']);
492 }
493
494 echo json_encode($result);
495 CRM_Utils_System::civiExit();
496 }
497
498 function recipient() {
499 $params = array('recipient');
500 foreach ($params as $param) {
501 $$param = CRM_Utils_Array::value($param, $_POST);
502 }
503
504 if (!$recipient) {
505 echo json_encode(array('error_msg' => 'required params missing.'));
506 CRM_Utils_System::civiExit();
507 }
508
509 switch ($recipient) {
510 case 'Participant Status':
511 $values = CRM_Event_PseudoConstant::participantStatus();
512 break;
513
514 case 'participant_role':
515 $values = CRM_Event_PseudoConstant::participantRole();
516 break;
517
518 default:
519 exit;
520 }
521
522 $elements = array();
523 foreach ($values as $id => $name) {
524 $elements[] = array(
525 'name' => $name,
526 'value' => $id,
527 );
528 }
529
530 echo json_encode($elements);
531 CRM_Utils_System::civiExit();
532 }
533 }
534