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