avoid variable name clash
[civicrm-core.git] / CRM / Queue / Page / AJAX.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 * Class CRM_Queue_Page_AJAX
30 */
31 class CRM_Queue_Page_AJAX {
32
33 /**
34 * Run the next task and return status information.
35 *
36 * Outputs JSON: array(
37 * is_error => bool,
38 * is_continue => bool,
39 * numberOfItems => int,
40 * exception => htmlString
41 * )
42 */
43 public static function runNext() {
44 $errorPolicy = new CRM_Queue_ErrorPolicy();
45 $errorPolicy->call(function () {
46 global $activeQueueRunner;
47 $qrid = CRM_Utils_Request::retrieve('qrid', 'String', CRM_Core_DAO::$_nullObject, TRUE, NULL, 'POST');
48 $activeQueueRunner = CRM_Queue_Runner::instance($qrid);
49 if (!is_object($activeQueueRunner)) {
50 throw new Exception('Queue runner must be configured before execution.');
51 }
52 $result = $activeQueueRunner->runNext(TRUE);
53 CRM_Queue_Page_AJAX::_return('runNext', $result);
54 });
55 }
56
57 /**
58 * Run the next task and return status information.
59 *
60 * Outputs JSON: array(
61 * is_error => bool,
62 * is_continue => bool,
63 * numberOfItems => int,
64 * exception => htmlString
65 * )
66 */
67 public static function skipNext() {
68 $errorPolicy = new CRM_Queue_ErrorPolicy();
69 $errorPolicy->call(function () {
70 global $activeQueueRunner;
71 $qrid = CRM_Utils_Request::retrieve('qrid', 'String', CRM_Core_DAO::$_nullObject, TRUE, NULL, 'POST');
72 $activeQueueRunner = CRM_Queue_Runner::instance($qrid);
73 if (!is_object($activeQueueRunner)) {
74 throw new Exception('Queue runner must be configured before execution.');
75 }
76 $result = $activeQueueRunner->skipNext(TRUE);
77 CRM_Queue_Page_AJAX::_return('skipNext', $result);
78 });
79 }
80
81 /**
82 * Run the next task and return status information.
83 *
84 * Outputs JSON: array(
85 * is_error => bool,
86 * is_continue => bool,
87 * numberOfItems => int,
88 * exception => htmlString
89 * )
90 */
91 public static function onEnd() {
92 $errorPolicy = new CRM_Queue_ErrorPolicy();
93 $errorPolicy->call(function () {
94 global $activeQueueRunner;
95 $qrid = CRM_Utils_Request::retrieve('qrid', 'String', CRM_Core_DAO::$_nullObject, TRUE, NULL, 'POST');
96 $activeQueueRunner = CRM_Queue_Runner::instance($qrid);
97 if (!is_object($activeQueueRunner)) {
98 throw new Exception('Queue runner must be configured before execution. - onEnd');
99 }
100 $result = $activeQueueRunner->handleEnd(FALSE);
101 CRM_Queue_Page_AJAX::_return('onEnd', $result);
102 });
103 }
104
105 /**
106 * Performing any view-layer filtering on result and send to client.
107 *
108 * @param string $op
109 * @param array $result
110 */
111 public static function _return($op, $result) {
112 if ($result['is_error']) {
113 if (is_object($result['exception'])) {
114 CRM_Core_Error::debug_var("CRM_Queue_Page_AJAX_{$op}_error", CRM_Core_Error::formatTextException($result['exception']));
115
116 $config = CRM_Core_Config::singleton();
117 if ($config->backtrace || CRM_Core_Config::isUpgradeMode()) {
118 $result['exception'] = CRM_Core_Error::formatHtmlException($result['exception']);
119 }
120 else {
121 $result['exception'] = $result['exception']->getMessage();
122 }
123 }
124 else {
125 CRM_Core_Error::debug_var("CRM_Queue_Page_AJAX_{$op}_error", $result);
126 }
127 }
128 CRM_Utils_JSON::output($result);
129 }
130
131 }