Merge pull request #22484 from civicrm/5.46
[civicrm-core.git] / CRM / Admin / Page / Job.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
ce064e4f 19 * Page for displaying list of jobs.
6a488035
TO
20 */
21class CRM_Admin_Page_Job extends CRM_Core_Page_Basic {
22
23 /**
eceb18cc 24 * The action links that we need to display for the browse screen.
6a488035
TO
25 *
26 * @var array
6a488035 27 */
62d3ee27 28 public static $_links = NULL;
6a488035
TO
29
30 /**
eceb18cc 31 * Get BAO Name.
6a488035 32 *
a6c01b45
CW
33 * @return string
34 * Classname of BAO.
6a488035 35 */
00be9182 36 public function getBAOName() {
6a488035
TO
37 return 'CRM_Core_BAO_Job';
38 }
39
40 /**
eceb18cc 41 * Get action Links.
6a488035 42 *
a6c01b45
CW
43 * @return array
44 * (reference) of action links
6a488035 45 */
00be9182 46 public function &links() {
6a488035
TO
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 ),
a098caf2 61 CRM_Core_Action::VIEW => array(
6a488035
TO
62 'name' => ts('Execute Now'),
63 'url' => 'civicrm/admin/job',
a098caf2 64 'qs' => 'action=view&id=%%id%%&reset=1',
6a488035
TO
65 'title' => ts('Execute Scheduled Job Now'),
66 ),
67 CRM_Core_Action::DISABLE => array(
68 'name' => ts('Disable'),
4d17a233 69 'ref' => 'crm-enable-disable',
6a488035
TO
70 'title' => ts('Disable Scheduled Job'),
71 ),
72 CRM_Core_Action::ENABLE => array(
73 'name' => ts('Enable'),
4d17a233 74 'ref' => 'crm-enable-disable',
6a488035
TO
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 ),
7a6059c2
MW
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 ),
6a488035
TO
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.
6a488035 100 */
00be9182 101 public function run() {
6a488035
TO
102 // set title and breadcrumb
103 CRM_Utils_System::setTitle(ts('Settings - Scheduled Jobs'));
353ffa53
TO
104 $breadCrumb = array(
105 array(
106 'title' => ts('Scheduled Jobs'),
6a488035
TO
107 'url' => CRM_Utils_System::url('civicrm/admin',
108 'reset=1'
109 ),
bed98343 110 ),
353ffa53 111 );
6a488035
TO
112 CRM_Utils_System::appendBreadCrumb($breadCrumb);
113
d71d4aae
SL
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 );
ed2ee15a
MW
120 $this->_context = CRM_Utils_Request::retrieve('context', 'String',
121 $this, FALSE, 0
122 );
d71d4aae 123
7a6059c2
MW
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
6a488035
TO
137 return parent::run();
138 }
139
140 /**
141 * Browse all jobs.
6a488035 142 */
41b74a9c 143 public function browse() {
f008885c
E
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 }
23af1818
CW
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 }
6a488035 159
6a488035 160 $sj = new CRM_Core_JobManager();
7b8d6e77 161 $rows = [];
6a488035
TO
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
7b8d6e77 167 if ($job->api_action === 'process_membership_reminder_date' || $job->api_action === 'update_greeting') {
6a488035
TO
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
7b8d6e77
EM
178 $job->action = CRM_Core_Action::formLink($this->links(), $action,
179 ['id' => $job->id],
87dab4a4
AH
180 ts('more'),
181 FALSE,
182 'job.manage.action',
183 'Job',
184 $job->id
6a488035
TO
185 );
186 $rows[] = get_object_vars($job);
187 }
188 $this->assign('rows', $rows);
189 }
190
191 /**
eceb18cc 192 * Get name of edit form.
6a488035 193 *
a6c01b45
CW
194 * @return string
195 * Classname of edit form.
6a488035 196 */
00be9182 197 public function editForm() {
6a488035
TO
198 return 'CRM_Admin_Form_Job';
199 }
200
201 /**
eceb18cc 202 * Get edit form name.
6a488035 203 *
a6c01b45
CW
204 * @return string
205 * name of this page.
6a488035 206 */
00be9182 207 public function editName() {
6a488035
TO
208 return 'Scheduled Jobs';
209 }
210
211 /**
212 * Get user context.
213 *
77b97be7
EM
214 * @param null $mode
215 *
a6c01b45
CW
216 * @return string
217 * user context.
6a488035 218 */
00be9182 219 public function userContext($mode = NULL) {
6a488035
TO
220 return 'civicrm/admin/job';
221 }
96025800 222
6a488035 223}