Merge pull request #17470 from colemanw/array
[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
121 if (($this->_action & CRM_Core_Action::COPY) && (!empty($this->_id))) {
122 try {
123 $jobResult = civicrm_api3('Job', 'clone', array('id' => $this->_id));
124 if ($jobResult['count'] > 0) {
125 CRM_Core_Session::setStatus($jobResult['values'][$jobResult['id']]['name'], ts('Job copied successfully'), 'success');
126 }
127 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/job', 'reset=1'));
128 }
129 catch (Exception $e) {
130 CRM_Core_Session::setStatus(ts('Failed to copy job'), 'Error');
131 }
132 }
133
134 return parent::run();
135 }
136
137 /**
138 * Browse all jobs.
139 */
140 public function browse() {
141 // check if non-prod mode is enabled.
142 if (CRM_Core_Config::environment() != 'Production') {
143 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));
144 }
145
146 $sj = new CRM_Core_JobManager();
147 $rows = $temp = [];
148 foreach ($sj->jobs as $job) {
149 $action = array_sum(array_keys($this->links()));
150
151 // update enable/disable links.
152 // CRM-9868- remove enable action for jobs that should never be run automatically via execute action or runjobs url
153 if ($job->api_action == 'process_membership_reminder_date' || $job->api_action == 'update_greeting') {
154 $action -= CRM_Core_Action::ENABLE;
155 $action -= CRM_Core_Action::DISABLE;
156 }
157 elseif ($job->is_active) {
158 $action -= CRM_Core_Action::ENABLE;
159 }
160 else {
161 $action -= CRM_Core_Action::DISABLE;
162 }
163
164 $job->action = CRM_Core_Action::formLink(self::links(), $action,
165 array('id' => $job->id),
166 ts('more'),
167 FALSE,
168 'job.manage.action',
169 'Job',
170 $job->id
171 );
172 $rows[] = get_object_vars($job);
173 }
174 $this->assign('rows', $rows);
175 }
176
177 /**
178 * Get name of edit form.
179 *
180 * @return string
181 * Classname of edit form.
182 */
183 public function editForm() {
184 return 'CRM_Admin_Form_Job';
185 }
186
187 /**
188 * Get edit form name.
189 *
190 * @return string
191 * name of this page.
192 */
193 public function editName() {
194 return 'Scheduled Jobs';
195 }
196
197 /**
198 * Get user context.
199 *
200 * @param null $mode
201 *
202 * @return string
203 * user context.
204 */
205 public function userContext($mode = NULL) {
206 return 'civicrm/admin/job';
207 }
208
209 }