Merge pull request #18794 from eileenmcnaughton/need_less
[civicrm-core.git] / CRM / Admin / Page / Job.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * Page for displaying list of jobs.
20 */
21 class CRM_Admin_Page_Job extends CRM_Core_Page_Basic {
22
23 /**
24 * The action links that we need to display for the browse screen.
25 *
26 * @var array
27 */
28 public static $_links = NULL;
29
30 /**
31 * Get BAO Name.
32 *
33 * @return string
34 * Classname of BAO.
35 */
36 public function getBAOName() {
37 return 'CRM_Core_BAO_Job';
38 }
39
40 /**
41 * Get action Links.
42 *
43 * @return array
44 * (reference) of action links
45 */
46 public function &links() {
47 if (!(self::$_links)) {
48 self::$_links = array(
49 CRM_Core_Action::FOLLOWUP => array(
50 'name' => ts('View Job Log'),
51 'url' => 'civicrm/admin/joblog',
52 'qs' => 'jid=%%id%%&reset=1',
53 'title' => ts('See log entries for this Scheduled Job'),
54 ),
55 CRM_Core_Action::UPDATE => array(
56 'name' => ts('Edit'),
57 'url' => 'civicrm/admin/job',
58 'qs' => 'action=update&id=%%id%%&reset=1',
59 'title' => ts('Edit Scheduled Job'),
60 ),
61 CRM_Core_Action::VIEW => array(
62 'name' => ts('Execute Now'),
63 'url' => 'civicrm/admin/job',
64 'qs' => 'action=view&id=%%id%%&reset=1',
65 'title' => ts('Execute Scheduled Job Now'),
66 ),
67 CRM_Core_Action::DISABLE => array(
68 'name' => ts('Disable'),
69 'ref' => 'crm-enable-disable',
70 'title' => ts('Disable Scheduled Job'),
71 ),
72 CRM_Core_Action::ENABLE => array(
73 'name' => ts('Enable'),
74 'ref' => 'crm-enable-disable',
75 'title' => ts('Enable Scheduled Job'),
76 ),
77 CRM_Core_Action::DELETE => array(
78 'name' => ts('Delete'),
79 'url' => 'civicrm/admin/job',
80 'qs' => 'action=delete&id=%%id%%',
81 'title' => ts('Delete Scheduled Job'),
82 ),
83 CRM_Core_Action::COPY => array(
84 'name' => ts('Copy'),
85 'url' => 'civicrm/admin/job',
86 'qs' => 'action=copy&id=%%id%%',
87 'title' => ts('Copy Scheduled Job'),
88 ),
89 );
90 }
91 return self::$_links;
92 }
93
94 /**
95 * Run the page.
96 *
97 * This method is called after the page is created. It checks for the
98 * type of action and executes that action.
99 * Finally it calls the parent's run method.
100 */
101 public function run() {
102 // set title and breadcrumb
103 CRM_Utils_System::setTitle(ts('Settings - Scheduled Jobs'));
104 $breadCrumb = array(
105 array(
106 'title' => ts('Scheduled Jobs'),
107 'url' => CRM_Utils_System::url('civicrm/admin',
108 'reset=1'
109 ),
110 ),
111 );
112 CRM_Utils_System::appendBreadCrumb($breadCrumb);
113
114 $this->_id = CRM_Utils_Request::retrieve('id', 'String',
115 $this, FALSE, 0
116 );
117 $this->_action = CRM_Utils_Request::retrieve('action', 'String',
118 $this, FALSE, 0
119 );
120 $this->_context = CRM_Utils_Request::retrieve('context', 'String',
121 $this, FALSE, 0
122 );
123
124 if (($this->_action & CRM_Core_Action::COPY) && (!empty($this->_id))) {
125 try {
126 $jobResult = civicrm_api3('Job', 'clone', array('id' => $this->_id));
127 if ($jobResult['count'] > 0) {
128 CRM_Core_Session::setStatus($jobResult['values'][$jobResult['id']]['name'], ts('Job copied successfully'), 'success');
129 }
130 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/job', 'reset=1'));
131 }
132 catch (Exception $e) {
133 CRM_Core_Session::setStatus(ts('Failed to copy job'), 'Error');
134 }
135 }
136
137 return parent::run();
138 }
139
140 /**
141 * Browse all jobs.
142 */
143 public function browse() {
144 // check if non-prod mode is enabled.
145 if (CRM_Core_Config::environment() != 'Production') {
146 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));
147 }
148 else {
149 $cronError = Civi\Api4\System::check(FALSE)
150 ->addWhere('name', '=', 'checkLastCron')
151 ->addWhere('severity_id', '>', 1)
152 ->setIncludeDisabled(TRUE)
153 ->execute()
154 ->first();
155 if ($cronError) {
156 CRM_Core_Session::setStatus($cronError['message'], $cronError['title'], 'alert', ['expires' => 0]);
157 }
158 }
159
160 $sj = new CRM_Core_JobManager();
161 $rows = $temp = [];
162 foreach ($sj->jobs as $job) {
163 $action = array_sum(array_keys($this->links()));
164
165 // update enable/disable links.
166 // CRM-9868- remove enable action for jobs that should never be run automatically via execute action or runjobs url
167 if ($job->api_action == 'process_membership_reminder_date' || $job->api_action == 'update_greeting') {
168 $action -= CRM_Core_Action::ENABLE;
169 $action -= CRM_Core_Action::DISABLE;
170 }
171 elseif ($job->is_active) {
172 $action -= CRM_Core_Action::ENABLE;
173 }
174 else {
175 $action -= CRM_Core_Action::DISABLE;
176 }
177
178 $job->action = CRM_Core_Action::formLink(self::links(), $action,
179 array('id' => $job->id),
180 ts('more'),
181 FALSE,
182 'job.manage.action',
183 'Job',
184 $job->id
185 );
186 $rows[] = get_object_vars($job);
187 }
188 $this->assign('rows', $rows);
189 }
190
191 /**
192 * Get name of edit form.
193 *
194 * @return string
195 * Classname of edit form.
196 */
197 public function editForm() {
198 return 'CRM_Admin_Form_Job';
199 }
200
201 /**
202 * Get edit form name.
203 *
204 * @return string
205 * name of this page.
206 */
207 public function editName() {
208 return 'Scheduled Jobs';
209 }
210
211 /**
212 * Get user context.
213 *
214 * @param null $mode
215 *
216 * @return string
217 * user context.
218 */
219 public function userContext($mode = NULL) {
220 return 'civicrm/admin/job';
221 }
222
223 }