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