Merge pull request #11241 from mattwire/CRM-21392_find_groups_viewcomponents
[civicrm-core.git] / CRM / Admin / Page / Job.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2018
32 */
33
34 /**
35 * Page for displaying list of jobs.
36 */
37 class CRM_Admin_Page_Job extends CRM_Core_Page_Basic {
38
39 /**
40 * The action links that we need to display for the browse screen.
41 *
42 * @var array
43 */
44 static $_links = NULL;
45
46 /**
47 * Get BAO Name.
48 *
49 * @return string
50 * Classname of BAO.
51 */
52 public function getBAOName() {
53 return 'CRM_Core_BAO_Job';
54 }
55
56 /**
57 * Get action Links.
58 *
59 * @return array
60 * (reference) of action links
61 */
62 public function &links() {
63 if (!(self::$_links)) {
64 self::$_links = array(
65 CRM_Core_Action::FOLLOWUP => array(
66 'name' => ts('View Job Log'),
67 'url' => 'civicrm/admin/joblog',
68 'qs' => 'jid=%%id%%&reset=1',
69 'title' => ts('See log entries for this Scheduled Job'),
70 ),
71 CRM_Core_Action::UPDATE => array(
72 'name' => ts('Edit'),
73 'url' => 'civicrm/admin/job',
74 'qs' => 'action=update&id=%%id%%&reset=1',
75 'title' => ts('Edit Scheduled Job'),
76 ),
77 CRM_Core_Action::EXPORT => array(
78 'name' => ts('Execute Now'),
79 'url' => 'civicrm/admin/job',
80 'qs' => 'action=export&id=%%id%%&reset=1',
81 'title' => ts('Execute Scheduled Job Now'),
82 ),
83 CRM_Core_Action::DISABLE => array(
84 'name' => ts('Disable'),
85 'ref' => 'crm-enable-disable',
86 'title' => ts('Disable Scheduled Job'),
87 ),
88 CRM_Core_Action::ENABLE => array(
89 'name' => ts('Enable'),
90 'ref' => 'crm-enable-disable',
91 'title' => ts('Enable Scheduled Job'),
92 ),
93 CRM_Core_Action::DELETE => array(
94 'name' => ts('Delete'),
95 'url' => 'civicrm/admin/job',
96 'qs' => 'action=delete&id=%%id%%',
97 'title' => ts('Delete Scheduled Job'),
98 ),
99 CRM_Core_Action::COPY => array(
100 'name' => ts('Copy'),
101 'url' => 'civicrm/admin/job',
102 'qs' => 'action=copy&id=%%id%%',
103 'title' => ts('Copy Scheduled Job'),
104 ),
105 );
106 }
107 return self::$_links;
108 }
109
110 /**
111 * Run the page.
112 *
113 * This method is called after the page is created. It checks for the
114 * type of action and executes that action.
115 * Finally it calls the parent's run method.
116 */
117 public function run() {
118 // set title and breadcrumb
119 CRM_Utils_System::setTitle(ts('Settings - Scheduled Jobs'));
120 $breadCrumb = array(
121 array(
122 'title' => ts('Scheduled Jobs'),
123 'url' => CRM_Utils_System::url('civicrm/admin',
124 'reset=1'
125 ),
126 ),
127 );
128 CRM_Utils_System::appendBreadCrumb($breadCrumb);
129
130 $this->_id = CRM_Utils_Request::retrieve('id', 'String',
131 $this, FALSE, 0
132 );
133 $this->_action = CRM_Utils_Request::retrieve('action', 'String',
134 $this, FALSE, 0
135 );
136
137 // FIXME: Why are we comparing an integer with a string here?
138 if ($this->_action == 'export') {
139 $session = CRM_Core_Session::singleton();
140 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/job', 'reset=1'));
141 }
142
143 if (($this->_action & CRM_Core_Action::COPY) && (!empty($this->_id))) {
144 try {
145 $jobResult = civicrm_api3('Job', 'clone', array('id' => $this->_id));
146 if ($jobResult['count'] > 0) {
147 CRM_Core_Session::setStatus($jobResult['values'][$jobResult['id']]['name'], ts('Job copied successfully'), 'success');
148 }
149 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/job', 'reset=1'));
150 }
151 catch (Exception $e) {
152 CRM_Core_Session::setStatus(ts('Failed to copy job'), 'Error');
153 }
154 }
155
156 return parent::run();
157 }
158
159 /**
160 * Browse all jobs.
161 *
162 * @param null $action
163 */
164 public function browse($action = NULL) {
165 // check if non-prod mode is enabled.
166 if (CRM_Core_Config::environment() != 'Production') {
167 CRM_Core_Session::setStatus(ts('Execution of scheduled jobs has been turned off by default since this is a non-production environment. You can override this for particular jobs by adding runInNonProductionEnvironment=TRUE as a parameter.'), ts("Non-production Environment"), "warning", array('expires' => 0));
168 }
169
170 // using Export action for Execute. Doh.
171 if ($this->_action & CRM_Core_Action::EXPORT) {
172 $jm = new CRM_Core_JobManager();
173 $jm->executeJobById($this->_id);
174
175 CRM_Core_Session::setStatus(ts('Selected Scheduled Job has been executed. See the log for details.'), ts("Executed"), "success");
176 }
177
178 $sj = new CRM_Core_JobManager();
179 $rows = $temp = array();
180 foreach ($sj->jobs as $job) {
181 $action = array_sum(array_keys($this->links()));
182
183 // update enable/disable links.
184 // CRM-9868- remove enable action for jobs that should never be run automatically via execute action or runjobs url
185 if ($job->api_action == 'process_membership_reminder_date' || $job->api_action == 'update_greeting') {
186 $action -= CRM_Core_Action::ENABLE;
187 $action -= CRM_Core_Action::DISABLE;
188 }
189 elseif ($job->is_active) {
190 $action -= CRM_Core_Action::ENABLE;
191 }
192 else {
193 $action -= CRM_Core_Action::DISABLE;
194 }
195
196 $job->action = CRM_Core_Action::formLink(self::links(), $action,
197 array('id' => $job->id),
198 ts('more'),
199 FALSE,
200 'job.manage.action',
201 'Job',
202 $job->id
203 );
204 $rows[] = get_object_vars($job);
205 }
206 $this->assign('rows', $rows);
207 }
208
209 /**
210 * Get name of edit form.
211 *
212 * @return string
213 * Classname of edit form.
214 */
215 public function editForm() {
216 return 'CRM_Admin_Form_Job';
217 }
218
219 /**
220 * Get edit form name.
221 *
222 * @return string
223 * name of this page.
224 */
225 public function editName() {
226 return 'Scheduled Jobs';
227 }
228
229 /**
230 * Get user context.
231 *
232 * @param null $mode
233 *
234 * @return string
235 * user context.
236 */
237 public function userContext($mode = NULL) {
238 return 'civicrm/admin/job';
239 }
240
241 }