comment fixes
[civicrm-core.git] / CRM / Queue / Page / AJAX.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
4c6ce474
EM
27
28/**
29 * Class CRM_Queue_Page_AJAX
30 */
6a488035
TO
31class CRM_Queue_Page_AJAX {
32
33 /**
fe482240 34 * Run the next task and return status information.
6a488035 35 *
4523a2f5
TO
36 * Outputs JSON: array(
37 * is_error => bool,
38 * is_continue => bool,
39 * numberOfItems => int,
40 * exception => htmlString
41 * )
6a488035 42 */
4523a2f5 43 public static function runNext() {
6a488035 44 $errorPolicy = new CRM_Queue_ErrorPolicy();
4523a2f5
TO
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.');
6a488035 51 }
4523a2f5
TO
52 $result = $activeQueueRunner->runNext(TRUE);
53 CRM_Queue_Page_AJAX::_return('runNext', $result);
54 });
6a488035
TO
55 }
56
57 /**
fe482240 58 * Run the next task and return status information.
6a488035 59 *
4523a2f5
TO
60 * Outputs JSON: array(
61 * is_error => bool,
62 * is_continue => bool,
63 * numberOfItems => int,
64 * exception => htmlString
65 * )
6a488035 66 */
4523a2f5 67 public static function skipNext() {
6a488035 68 $errorPolicy = new CRM_Queue_ErrorPolicy();
4523a2f5
TO
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.');
6a488035 75 }
4523a2f5
TO
76 $result = $activeQueueRunner->skipNext(TRUE);
77 CRM_Queue_Page_AJAX::_return('skipNext', $result);
78 });
6a488035
TO
79 }
80
81 /**
fe482240 82 * Run the next task and return status information.
6a488035 83 *
4523a2f5
TO
84 * Outputs JSON: array(
85 * is_error => bool,
86 * is_continue => bool,
87 * numberOfItems => int,
88 * exception => htmlString
89 * )
6a488035 90 */
4523a2f5 91 public static function onEnd() {
6a488035 92 $errorPolicy = new CRM_Queue_ErrorPolicy();
4523a2f5
TO
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');
6a488035 99 }
4523a2f5
TO
100 $result = $activeQueueRunner->handleEnd(FALSE);
101 CRM_Queue_Page_AJAX::_return('onEnd', $result);
102 });
6a488035
TO
103 }
104
105 /**
106 * Performing any view-layer filtering on result and send to client.
ad37ac8e 107 *
108 * @param string $op
109 * @param array $result
6a488035 110 */
4523a2f5
TO
111 public static function _return($op, $result) {
112 if ($result['is_error']) {
6a488035
TO
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']);
4523a2f5 119 }
6a488035
TO
120 else {
121 $result['exception'] = $result['exception']->getMessage();
122 }
4523a2f5
TO
123 }
124 else {
6a488035
TO
125 CRM_Core_Error::debug_var("CRM_Queue_Page_AJAX_{$op}_error", $result);
126 }
127 }
4523a2f5 128 CRM_Utils_JSON::output($result);
6a488035 129 }
96025800 130
4523a2f5 131}