Merge pull request #15875 from civicrm/5.20
[civicrm-core.git] / CRM / Queue / Page / AJAX.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
4c6ce474
EM
11
12/**
13 * Class CRM_Queue_Page_AJAX
14 */
6a488035
TO
15class CRM_Queue_Page_AJAX {
16
17 /**
fe482240 18 * Run the next task and return status information.
6a488035 19 *
4523a2f5
TO
20 * Outputs JSON: array(
21 * is_error => bool,
22 * is_continue => bool,
23 * numberOfItems => int,
24 * exception => htmlString
25 * )
6a488035 26 */
4523a2f5 27 public static function runNext() {
6a488035 28 $errorPolicy = new CRM_Queue_ErrorPolicy();
4523a2f5
TO
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.');
6a488035 35 }
4523a2f5
TO
36 $result = $activeQueueRunner->runNext(TRUE);
37 CRM_Queue_Page_AJAX::_return('runNext', $result);
38 });
6a488035
TO
39 }
40
41 /**
fe482240 42 * Run the next task and return status information.
6a488035 43 *
4523a2f5
TO
44 * Outputs JSON: array(
45 * is_error => bool,
46 * is_continue => bool,
47 * numberOfItems => int,
48 * exception => htmlString
49 * )
6a488035 50 */
4523a2f5 51 public static function skipNext() {
6a488035 52 $errorPolicy = new CRM_Queue_ErrorPolicy();
4523a2f5
TO
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.');
6a488035 59 }
4523a2f5
TO
60 $result = $activeQueueRunner->skipNext(TRUE);
61 CRM_Queue_Page_AJAX::_return('skipNext', $result);
62 });
6a488035
TO
63 }
64
65 /**
fe482240 66 * Run the next task and return status information.
6a488035 67 *
4523a2f5
TO
68 * Outputs JSON: array(
69 * is_error => bool,
70 * is_continue => bool,
71 * numberOfItems => int,
72 * exception => htmlString
73 * )
6a488035 74 */
4523a2f5 75 public static function onEnd() {
6a488035 76 $errorPolicy = new CRM_Queue_ErrorPolicy();
4523a2f5
TO
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');
6a488035 83 }
4523a2f5
TO
84 $result = $activeQueueRunner->handleEnd(FALSE);
85 CRM_Queue_Page_AJAX::_return('onEnd', $result);
86 });
6a488035
TO
87 }
88
89 /**
90 * Performing any view-layer filtering on result and send to client.
ad37ac8e 91 *
92 * @param string $op
93 * @param array $result
6a488035 94 */
4523a2f5
TO
95 public static function _return($op, $result) {
96 if ($result['is_error']) {
6a488035
TO
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']);
4523a2f5 103 }
6a488035
TO
104 else {
105 $result['exception'] = $result['exception']->getMessage();
106 }
4523a2f5
TO
107 }
108 else {
6a488035
TO
109 CRM_Core_Error::debug_var("CRM_Queue_Page_AJAX_{$op}_error", $result);
110 }
111 }
4523a2f5 112 CRM_Utils_JSON::output($result);
6a488035 113 }
96025800 114
4523a2f5 115}