Merge pull request #15785 from eileenmcnaughton/contribution_url_params
[civicrm-core.git] / CRM / Queue / Page / AJAX.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Class CRM_Queue_Page_AJAX
14 */
15 class CRM_Queue_Page_AJAX {
16
17 /**
18 * Run the next task and return status information.
19 *
20 * Outputs JSON: array(
21 * is_error => bool,
22 * is_continue => bool,
23 * numberOfItems => int,
24 * exception => htmlString
25 * )
26 */
27 public static function runNext() {
28 $errorPolicy = new CRM_Queue_ErrorPolicy();
29 $errorPolicy->call(function () {
30 global $activeQueueRunner;
31 $qrid = CRM_Utils_Request::retrieve('qrid', 'String', CRM_Core_DAO::$_nullObject, TRUE, NULL, 'POST');
32 $activeQueueRunner = CRM_Queue_Runner::instance($qrid);
33 if (!is_object($activeQueueRunner)) {
34 throw new Exception('Queue runner must be configured before execution.');
35 }
36 $result = $activeQueueRunner->runNext(TRUE);
37 CRM_Queue_Page_AJAX::_return('runNext', $result);
38 });
39 }
40
41 /**
42 * Run the next task and return status information.
43 *
44 * Outputs JSON: array(
45 * is_error => bool,
46 * is_continue => bool,
47 * numberOfItems => int,
48 * exception => htmlString
49 * )
50 */
51 public static function skipNext() {
52 $errorPolicy = new CRM_Queue_ErrorPolicy();
53 $errorPolicy->call(function () {
54 global $activeQueueRunner;
55 $qrid = CRM_Utils_Request::retrieve('qrid', 'String', CRM_Core_DAO::$_nullObject, TRUE, NULL, 'POST');
56 $activeQueueRunner = CRM_Queue_Runner::instance($qrid);
57 if (!is_object($activeQueueRunner)) {
58 throw new Exception('Queue runner must be configured before execution.');
59 }
60 $result = $activeQueueRunner->skipNext(TRUE);
61 CRM_Queue_Page_AJAX::_return('skipNext', $result);
62 });
63 }
64
65 /**
66 * Run the next task and return status information.
67 *
68 * Outputs JSON: array(
69 * is_error => bool,
70 * is_continue => bool,
71 * numberOfItems => int,
72 * exception => htmlString
73 * )
74 */
75 public static function onEnd() {
76 $errorPolicy = new CRM_Queue_ErrorPolicy();
77 $errorPolicy->call(function () {
78 global $activeQueueRunner;
79 $qrid = CRM_Utils_Request::retrieve('qrid', 'String', CRM_Core_DAO::$_nullObject, TRUE, NULL, 'POST');
80 $activeQueueRunner = CRM_Queue_Runner::instance($qrid);
81 if (!is_object($activeQueueRunner)) {
82 throw new Exception('Queue runner must be configured before execution. - onEnd');
83 }
84 $result = $activeQueueRunner->handleEnd(FALSE);
85 CRM_Queue_Page_AJAX::_return('onEnd', $result);
86 });
87 }
88
89 /**
90 * Performing any view-layer filtering on result and send to client.
91 *
92 * @param string $op
93 * @param array $result
94 */
95 public static function _return($op, $result) {
96 if ($result['is_error']) {
97 if (is_object($result['exception'])) {
98 CRM_Core_Error::debug_var("CRM_Queue_Page_AJAX_{$op}_error", CRM_Core_Error::formatTextException($result['exception']));
99
100 $config = CRM_Core_Config::singleton();
101 if ($config->backtrace || CRM_Core_Config::isUpgradeMode()) {
102 $result['exception'] = CRM_Core_Error::formatHtmlException($result['exception']);
103 }
104 else {
105 $result['exception'] = $result['exception']->getMessage();
106 }
107 }
108 else {
109 CRM_Core_Error::debug_var("CRM_Queue_Page_AJAX_{$op}_error", $result);
110 }
111 }
112 CRM_Utils_JSON::output($result);
113 }
114
115 }