1: <?php
2: namespace Opencart\Admin\Controller\Marketplace;
3: /**
4: * Class Cron
5: *
6: * @package Opencart\Admin\Controller\Marketplace
7: */
8: class Cron extends \Opencart\System\Engine\Controller {
9: /**
10: * Index
11: *
12: * @return void
13: */
14: public function index(): void {
15: $this->load->language('marketplace/cron');
16:
17: $this->document->setTitle($this->language->get('heading_title'));
18:
19: $url = '';
20:
21: if (isset($this->request->get['sort'])) {
22: $url .= '&sort=' . $this->request->get['sort'];
23: }
24:
25: if (isset($this->request->get['order'])) {
26: $url .= '&order=' . $this->request->get['order'];
27: }
28:
29: if (isset($this->request->get['page'])) {
30: $url .= '&page=' . $this->request->get['page'];
31: }
32:
33: $data['breadcrumbs'] = [];
34:
35: $data['breadcrumbs'][] = [
36: 'text' => $this->language->get('text_home'),
37: 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
38: ];
39:
40: $data['breadcrumbs'][] = [
41: 'text' => $this->language->get('heading_title'),
42: 'href' => $this->url->link('marketplace/cron', 'user_token=' . $this->session->data['user_token'] . $url)
43: ];
44:
45: $data['delete'] = $this->url->link('marketplace/cron.delete', 'user_token=' . $this->session->data['user_token']);
46:
47: // Example cron URL
48: $data['cron'] = HTTP_CATALOG . 'index.php?route=cron/cron';
49:
50: $data['list'] = $this->getList();
51:
52: $data['user_token'] = $this->session->data['user_token'];
53:
54: $data['header'] = $this->load->controller('common/header');
55: $data['column_left'] = $this->load->controller('common/column_left');
56: $data['footer'] = $this->load->controller('common/footer');
57:
58: $this->response->setOutput($this->load->view('marketplace/cron', $data));
59: }
60:
61: /**
62: * List
63: *
64: * @return void
65: */
66: public function list(): void {
67: $this->load->language('marketplace/cron');
68:
69: $this->response->setOutput($this->getList());
70: }
71:
72: /**
73: * Get List
74: *
75: * @return string
76: */
77: public function getList(): string {
78: if (isset($this->request->get['sort'])) {
79: $sort = (string)$this->request->get['sort'];
80: } else {
81: $sort = 'code';
82: }
83:
84: if (isset($this->request->get['order'])) {
85: $order = (string)$this->request->get['order'];
86: } else {
87: $order = 'ASC';
88: }
89:
90: if (isset($this->request->get['page'])) {
91: $page = (int)$this->request->get['page'];
92: } else {
93: $page = 1;
94: }
95:
96: $url = '';
97:
98: if (isset($this->request->get['sort'])) {
99: $url .= '&sort=' . $this->request->get['sort'];
100: }
101:
102: if (isset($this->request->get['order'])) {
103: $url .= '&order=' . $this->request->get['order'];
104: }
105:
106: if (isset($this->request->get['page'])) {
107: $url .= '&page=' . $this->request->get['page'];
108: }
109:
110: $data['action'] = $this->url->link('marketplace/cron.list', 'user_token=' . $this->session->data['user_token'] . $url);
111:
112: $data['crons'] = [];
113:
114: $filter_data = [
115: 'sort' => $sort,
116: 'order' => $order,
117: 'start' => ($page - 1) * $this->config->get('config_pagination_admin'),
118: 'limit' => $this->config->get('config_pagination_admin')
119: ];
120:
121: $this->load->model('setting/cron');
122:
123: $results = $this->model_setting_cron->getCrons($filter_data);
124:
125: foreach ($results as $result) {
126: $data['crons'][] = [
127: 'cron_id' => $result['cron_id'],
128: 'code' => $result['code'],
129: 'description' => $result['description'],
130: 'cycle' => $this->language->get('text_' . $result['cycle']),
131: 'action' => $result['action'],
132: 'status' => $result['status'],
133: 'date_added' => date($this->language->get('datetime_format'), strtotime($result['date_added'])),
134: 'date_modified' => date($this->language->get('datetime_format'), strtotime($result['date_modified'])),
135: 'run' => $this->url->link('marketplace/cron.run', 'user_token=' . $this->session->data['user_token'] . '&cron_id=' . $result['cron_id']),
136: 'enable' => $this->url->link('marketplace/cron.enable', 'user_token=' . $this->session->data['user_token'] . '&cron_id=' . $result['cron_id']),
137: 'disable' => $this->url->link('marketplace/cron.disable', 'user_token=' . $this->session->data['user_token'] . '&cron_id=' . $result['cron_id'])
138: ];
139: }
140:
141: $url = '';
142:
143: if ($order == 'ASC') {
144: $url .= '&order=DESC';
145: } else {
146: $url .= '&order=ASC';
147: }
148:
149: $data['sort_code'] = $this->url->link('marketplace/cron.list', 'user_token=' . $this->session->data['user_token'] . '&sort=code' . $url);
150: $data['sort_cycle'] = $this->url->link('marketplace/cron.list', 'user_token=' . $this->session->data['user_token'] . '&sort=cycle' . $url);
151: $data['sort_action'] = $this->url->link('marketplace/cron.list', 'user_token=' . $this->session->data['user_token'] . '&sort=action' . $url);
152: $data['sort_date_added'] = $this->url->link('marketplace/cron.list', 'user_token=' . $this->session->data['user_token'] . '&sort=date_added' . $url);
153: $data['sort_date_modified'] = $this->url->link('marketplace/cron.list', 'user_token=' . $this->session->data['user_token'] . '&sort=date_modified' . $url);
154:
155: $url = '';
156:
157: if (isset($this->request->get['sort'])) {
158: $url .= '&sort=' . $this->request->get['sort'];
159: }
160:
161: if (isset($this->request->get['order'])) {
162: $url .= '&order=' . $this->request->get['order'];
163: }
164:
165: $cron_total = $this->model_setting_cron->getTotalCrons();
166:
167: $data['pagination'] = $this->load->controller('common/pagination', [
168: 'total' => $cron_total,
169: 'page' => $page,
170: 'limit' => $this->config->get('config_pagination_admin'),
171: 'url' => $this->url->link('marketplace/cron.list', 'user_token=' . $this->session->data['user_token'] . $url . '&page={page}')
172: ]);
173:
174: $data['results'] = sprintf($this->language->get('text_pagination'), ($cron_total) ? (($page - 1) * $this->config->get('config_pagination_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_pagination_admin')) > ($cron_total - $this->config->get('config_pagination_admin'))) ? $cron_total : ((($page - 1) * $this->config->get('config_pagination_admin')) + $this->config->get('config_pagination_admin')), $cron_total, ceil($cron_total / $this->config->get('config_pagination_admin')));
175:
176: $data['sort'] = $sort;
177: $data['order'] = $order;
178:
179: return $this->load->view('marketplace/cron_list', $data);
180: }
181:
182: /**
183: * Run
184: *
185: * @return void
186: */
187: public function run(): void {
188: $this->load->language('marketplace/cron');
189:
190: $json = [];
191:
192: if (isset($this->request->get['cron_id'])) {
193: $cron_id = (int)$this->request->get['cron_id'];
194: } else {
195: $cron_id = 0;
196: }
197:
198: if (!$this->user->hasPermission('modify', 'marketplace/cron')) {
199: $json['error'] = $this->language->get('error_permission');
200: }
201:
202: if (!$json) {
203: $this->load->model('setting/cron');
204:
205: $cron_info = $this->model_setting_cron->getCron($cron_id);
206:
207: if ($cron_info) {
208: // Create a store instance using loader class to call controllers, models, views, libraries
209: $this->load->model('setting/store');
210:
211: $store = $this->model_setting_store->createStoreInstance(0, $this->config->get('config_language'));
212:
213: $store->load->controller($cron_info['action'], $cron_id, $cron_info['code'], $cron_info['cycle'], $cron_info['date_added'], $cron_info['date_modified']);
214:
215: $store->session->destroy();
216:
217: $this->model_setting_cron->editCron($cron_info['cron_id']);
218: }
219:
220: $json['success'] = $this->language->get('text_success');
221: }
222:
223: $this->response->addHeader('Content-Type: application/json');
224: $this->response->setOutput(json_encode($json));
225: }
226:
227: /**
228: * Enable
229: *
230: * @return void
231: */
232: public function enable(): void {
233: $this->load->language('marketplace/cron');
234:
235: $json = [];
236:
237: if (isset($this->request->get['cron_id'])) {
238: $cron_id = (int)$this->request->get['cron_id'];
239: } else {
240: $cron_id = 0;
241: }
242:
243: if (!$this->user->hasPermission('modify', 'marketplace/cron')) {
244: $json['error'] = $this->language->get('error_permission');
245: }
246:
247: if (!$json) {
248: $this->load->model('setting/cron');
249:
250: $this->model_setting_cron->editStatus($cron_id, true);
251:
252: $json['success'] = $this->language->get('text_success');
253: }
254:
255: $this->response->addHeader('Content-Type: application/json');
256: $this->response->setOutput(json_encode($json));
257: }
258:
259: /**
260: * Disable
261: *
262: * @return void
263: */
264: public function disable(): void {
265: $this->load->language('marketplace/cron');
266:
267: $json = [];
268:
269: if (isset($this->request->get['cron_id'])) {
270: $cron_id = (int)$this->request->get['cron_id'];
271: } else {
272: $cron_id = 0;
273: }
274:
275: if (!$this->user->hasPermission('modify', 'marketplace/cron')) {
276: $json['error'] = $this->language->get('error_permission');
277: }
278:
279: if (!$json) {
280: $this->load->model('setting/cron');
281:
282: $this->model_setting_cron->editStatus($cron_id, false);
283:
284: $json['success'] = $this->language->get('text_success');
285: }
286:
287: $this->response->addHeader('Content-Type: application/json');
288: $this->response->setOutput(json_encode($json));
289: }
290:
291: /**
292: * Delete
293: *
294: * @return void
295: */
296: public function delete(): void {
297: $this->load->language('marketplace/cron');
298:
299: $json = [];
300:
301: if (isset($this->request->post['selected'])) {
302: $selected = (array)$this->request->post['selected'];
303: } else {
304: $selected = [];
305: }
306:
307: if (!$this->user->hasPermission('modify', 'marketplace/event')) {
308: $json['error'] = $this->language->get('error_permission');
309: }
310:
311: if (!$json) {
312: $this->load->model('setting/cron');
313:
314: foreach ($selected as $cron_id) {
315: $this->model_setting_cron->deleteCron($cron_id);
316: }
317:
318: $json['success'] = $this->language->get('text_success');
319: }
320:
321: $this->response->addHeader('Content-Type: application/json');
322: $this->response->setOutput(json_encode($json));
323: }
324: }
325: