1: | <?php
2: | namespace Opencart\Admin\Controller\Marketing;
3: | /**
4: | * Class Marketing
5: | *
6: | * @package Opencart\Admin\Controller\Marketing
7: | */
8: | class Marketing extends \Opencart\System\Engine\Controller {
9: | /**
10: | * Index
11: | *
12: | * @return void
13: | */
14: | public function index(): void {
15: | $this->load->language('marketing/marketing');
16: |
17: | $this->document->setTitle($this->language->get('heading_title'));
18: |
19: | if (isset($this->request->get['filter_name'])) {
20: | $filter_name = $this->request->get['filter_name'];
21: | } else {
22: | $filter_name = '';
23: | }
24: |
25: | if (isset($this->request->get['filter_code'])) {
26: | $filter_code = $this->request->get['filter_code'];
27: | } else {
28: | $filter_code = '';
29: | }
30: |
31: | if (isset($this->request->get['filter_date_from'])) {
32: | $filter_date_from = $this->request->get['filter_date_from'];
33: | } else {
34: | $filter_date_from = '';
35: | }
36: |
37: | if (isset($this->request->get['filter_date_to'])) {
38: | $filter_date_to = $this->request->get['filter_date_to'];
39: | } else {
40: | $filter_date_to = '';
41: | }
42: |
43: | $url = '';
44: |
45: | if (isset($this->request->get['filter_name'])) {
46: | $url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
47: | }
48: |
49: | if (isset($this->request->get['filter_code'])) {
50: | $url .= '&filter_code=' . $this->request->get['filter_code'];
51: | }
52: |
53: | if (isset($this->request->get['filter_date_from'])) {
54: | $url .= '&filter_date_from=' . $this->request->get['filter_date_from'];
55: | }
56: |
57: | if (isset($this->request->get['filter_date_to'])) {
58: | $url .= '&filter_date_to=' . $this->request->get['filter_date_to'];
59: | }
60: |
61: | if (isset($this->request->get['sort'])) {
62: | $url .= '&sort=' . $this->request->get['sort'];
63: | }
64: |
65: | if (isset($this->request->get['order'])) {
66: | $url .= '&order=' . $this->request->get['order'];
67: | }
68: |
69: | if (isset($this->request->get['page'])) {
70: | $url .= '&page=' . $this->request->get['page'];
71: | }
72: |
73: | $data['breadcrumbs'] = [];
74: |
75: | $data['breadcrumbs'][] = [
76: | 'text' => $this->language->get('text_home'),
77: | 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
78: | ];
79: |
80: | $data['breadcrumbs'][] = [
81: | 'text' => $this->language->get('heading_title'),
82: | 'href' => $this->url->link('marketing/marketing', 'user_token=' . $this->session->data['user_token'] . $url)
83: | ];
84: |
85: | $data['add'] = $this->url->link('marketing/marketing.form', 'user_token=' . $this->session->data['user_token'] . $url);
86: | $data['delete'] = $this->url->link('marketing/marketing.delete', 'user_token=' . $this->session->data['user_token']);
87: |
88: | $data['list'] = $this->getList();
89: |
90: | $data['filter_name'] = $filter_name;
91: | $data['filter_code'] = $filter_code;
92: | $data['filter_date_from'] = $filter_date_from;
93: | $data['filter_date_to'] = $filter_date_to;
94: |
95: | $data['user_token'] = $this->session->data['user_token'];
96: |
97: | $data['header'] = $this->load->controller('common/header');
98: | $data['column_left'] = $this->load->controller('common/column_left');
99: | $data['footer'] = $this->load->controller('common/footer');
100: |
101: | $this->response->setOutput($this->load->view('marketing/marketing', $data));
102: | }
103: |
104: | /**
105: | * List
106: | *
107: | * @return void
108: | */
109: | public function list(): void {
110: | $this->load->language('marketing/marketing');
111: |
112: | $this->response->setOutput($this->getList());
113: | }
114: |
115: | /**
116: | * Get List
117: | *
118: | * @return string
119: | */
120: | protected function getList(): string {
121: | if (isset($this->request->get['filter_name'])) {
122: | $filter_name = $this->request->get['filter_name'];
123: | } else {
124: | $filter_name = '';
125: | }
126: |
127: | if (isset($this->request->get['filter_code'])) {
128: | $filter_code = $this->request->get['filter_code'];
129: | } else {
130: | $filter_code = '';
131: | }
132: |
133: | if (isset($this->request->get['filter_date_from'])) {
134: | $filter_date_from = (string)$this->request->get['filter_date_from'];
135: | } else {
136: | $filter_date_from = '';
137: | }
138: |
139: | if (isset($this->request->get['filter_date_to'])) {
140: | $filter_date_to = (string)$this->request->get['filter_date_to'];
141: | } else {
142: | $filter_date_to = '';
143: | }
144: |
145: | if (isset($this->request->get['sort'])) {
146: | $sort = (string)$this->request->get['sort'];
147: | } else {
148: | $sort = 'm.name';
149: | }
150: |
151: | if (isset($this->request->get['order'])) {
152: | $order = (string)$this->request->get['order'];
153: | } else {
154: | $order = 'ASC';
155: | }
156: |
157: | if (isset($this->request->get['page'])) {
158: | $page = (int)$this->request->get['page'];
159: | } else {
160: | $page = 1;
161: | }
162: |
163: | $url = '';
164: |
165: | if (isset($this->request->get['filter_name'])) {
166: | $url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
167: | }
168: |
169: | if (isset($this->request->get['filter_code'])) {
170: | $url .= '&filter_code=' . $this->request->get['filter_code'];
171: | }
172: |
173: | if (isset($this->request->get['filter_date_from'])) {
174: | $url .= '&filter_date_from=' . $this->request->get['filter_date_from'];
175: | }
176: |
177: | if (isset($this->request->get['filter_date_to'])) {
178: | $url .= '&filter_date_to=' . $this->request->get['filter_date_to'];
179: | }
180: |
181: | if (isset($this->request->get['sort'])) {
182: | $url .= '&sort=' . $this->request->get['sort'];
183: | }
184: |
185: | if (isset($this->request->get['order'])) {
186: | $url .= '&order=' . $this->request->get['order'];
187: | }
188: |
189: | if (isset($this->request->get['page'])) {
190: | $url .= '&page=' . $this->request->get['page'];
191: | }
192: |
193: | $data['action'] = $this->url->link('marketing/marketing.list', 'user_token=' . $this->session->data['user_token'] . $url);
194: |
195: | $data['marketings'] = [];
196: |
197: | $filter_data = [
198: | 'filter_name' => $filter_name,
199: | 'filter_code' => $filter_code,
200: | 'filter_date_from' => $filter_date_from,
201: | 'filter_date_to' => $filter_date_to,
202: | 'sort' => $sort,
203: | 'order' => $order,
204: | 'start' => ($page - 1) * $this->config->get('config_pagination_admin'),
205: | 'limit' => $this->config->get('config_pagination_admin')
206: | ];
207: |
208: | $this->load->model('marketing/marketing');
209: |
210: | $results = $this->model_marketing_marketing->getMarketings($filter_data);
211: |
212: | foreach ($results as $result) {
213: | $data['marketings'][] = [
214: | 'marketing_id' => $result['marketing_id'],
215: | 'name' => $result['name'],
216: | 'code' => $result['code'],
217: | 'clicks' => $result['clicks'],
218: | 'orders' => $result['orders'],
219: | 'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added'])),
220: | 'edit' => $this->url->link('marketing/marketing.form', 'user_token=' . $this->session->data['user_token'] . '&marketing_id=' . $result['marketing_id'] . $url)
221: | ];
222: | }
223: |
224: | $url = '';
225: |
226: | if (isset($this->request->get['filter_name'])) {
227: | $url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
228: | }
229: |
230: | if (isset($this->request->get['filter_code'])) {
231: | $url .= '&filter_code=' . $this->request->get['filter_code'];
232: | }
233: |
234: | if (isset($this->request->get['filter_date_from'])) {
235: | $url .= '&filter_date_from=' . $this->request->get['filter_date_from'];
236: | }
237: |
238: | if (isset($this->request->get['filter_date_to'])) {
239: | $url .= '&filter_date_to=' . $this->request->get['filter_date_to'];
240: | }
241: |
242: | if ($order == 'ASC') {
243: | $url .= '&order=DESC';
244: | } else {
245: | $url .= '&order=ASC';
246: | }
247: |
248: | $data['sort_name'] = $this->url->link('marketing/marketing.list', 'user_token=' . $this->session->data['user_token'] . '&sort=m.name' . $url);
249: | $data['sort_code'] = $this->url->link('marketing/marketing.list', 'user_token=' . $this->session->data['user_token'] . '&sort=m.code' . $url);
250: | $data['sort_date_added'] = $this->url->link('marketing/marketing.list', 'user_token=' . $this->session->data['user_token'] . '&sort=m.date_added' . $url);
251: |
252: | $url = '';
253: |
254: | if (isset($this->request->get['filter_name'])) {
255: | $url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
256: | }
257: |
258: | if (isset($this->request->get['filter_code'])) {
259: | $url .= '&filter_code=' . $this->request->get['filter_code'];
260: | }
261: |
262: | if (isset($this->request->get['filter_date_from'])) {
263: | $url .= '&filter_date_from=' . $this->request->get['filter_date_from'];
264: | }
265: |
266: | if (isset($this->request->get['filter_date_to'])) {
267: | $url .= '&filter_date_to=' . $this->request->get['filter_date_to'];
268: | }
269: |
270: | if (isset($this->request->get['sort'])) {
271: | $url .= '&sort=' . $this->request->get['sort'];
272: | }
273: |
274: | if (isset($this->request->get['order'])) {
275: | $url .= '&order=' . $this->request->get['order'];
276: | }
277: |
278: | $marketing_total = $this->model_marketing_marketing->getTotalMarketings($filter_data);
279: |
280: | $data['pagination'] = $this->load->controller('common/pagination', [
281: | 'total' => $marketing_total,
282: | 'page' => $page,
283: | 'limit' => $this->config->get('config_pagination_admin'),
284: | 'url' => $this->url->link('marketing/marketing.list', 'user_token=' . $this->session->data['user_token'] . $url . '&page={page}')
285: | ]);
286: |
287: | $data['results'] = sprintf($this->language->get('text_pagination'), ($marketing_total) ? (($page - 1) * $this->config->get('config_pagination_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_pagination_admin')) > ($marketing_total - $this->config->get('config_pagination_admin'))) ? $marketing_total : ((($page - 1) * $this->config->get('config_pagination_admin')) + $this->config->get('config_pagination_admin')), $marketing_total, ceil($marketing_total / $this->config->get('config_pagination_admin')));
288: |
289: | $data['sort'] = $sort;
290: | $data['order'] = $order;
291: |
292: | return $this->load->view('marketing/marketing_list', $data);
293: | }
294: |
295: | /**
296: | * Form
297: | *
298: | * @return void
299: | */
300: | public function form(): void {
301: | $this->load->language('marketing/marketing');
302: |
303: | $this->document->setTitle($this->language->get('heading_title'));
304: |
305: | $data['text_form'] = !isset($this->request->get['marketing_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
306: |
307: | $url = '';
308: |
309: | if (isset($this->request->get['filter_name'])) {
310: | $url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
311: | }
312: |
313: | if (isset($this->request->get['filter_code'])) {
314: | $url .= '&filter_code=' . $this->request->get['filter_code'];
315: | }
316: |
317: | if (isset($this->request->get['filter_date_from'])) {
318: | $url .= '&filter_date_from=' . $this->request->get['filter_date_from'];
319: | }
320: |
321: | if (isset($this->request->get['filter_date_to'])) {
322: | $url .= '&filter_date_to=' . $this->request->get['filter_date_to'];
323: | }
324: |
325: | if (isset($this->request->get['sort'])) {
326: | $url .= '&sort=' . $this->request->get['sort'];
327: | }
328: |
329: | if (isset($this->request->get['order'])) {
330: | $url .= '&order=' . $this->request->get['order'];
331: | }
332: |
333: | if (isset($this->request->get['page'])) {
334: | $url .= '&page=' . $this->request->get['page'];
335: | }
336: |
337: | $data['breadcrumbs'] = [];
338: |
339: | $data['breadcrumbs'][] = [
340: | 'text' => $this->language->get('text_home'),
341: | 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
342: | ];
343: |
344: | $data['breadcrumbs'][] = [
345: | 'text' => $this->language->get('heading_title'),
346: | 'href' => $this->url->link('marketing/marketing', 'user_token=' . $this->session->data['user_token'] . $url)
347: | ];
348: |
349: | $data['save'] = $this->url->link('marketing/marketing.save', 'user_token=' . $this->session->data['user_token']);
350: | $data['back'] = $this->url->link('marketing/marketing', 'user_token=' . $this->session->data['user_token'] . $url);
351: |
352: | if (isset($this->request->get['marketing_id'])) {
353: | $this->load->model('marketing/marketing');
354: |
355: | $marketing_info = $this->model_marketing_marketing->getMarketing($this->request->get['marketing_id']);
356: | }
357: |
358: | if (isset($this->request->get['marketing_id'])) {
359: | $data['marketing_id'] = (int)$this->request->get['marketing_id'];
360: | } else {
361: | $data['marketing_id'] = 0;
362: | }
363: |
364: | $data['store'] = HTTP_CATALOG;
365: |
366: | if (!empty($marketing_info)) {
367: | $data['name'] = $marketing_info['name'];
368: | } else {
369: | $data['name'] = '';
370: | }
371: |
372: | if (!empty($marketing_info)) {
373: | $data['description'] = $marketing_info['description'];
374: | } else {
375: | $data['description'] = '';
376: | }
377: |
378: | if (!empty($marketing_info)) {
379: | $data['code'] = $marketing_info['code'];
380: | } else {
381: | $data['code'] = uniqid();
382: | }
383: |
384: | $data['report'] = $this->getReport();
385: |
386: | $data['user_token'] = $this->session->data['user_token'];
387: |
388: | $data['header'] = $this->load->controller('common/header');
389: | $data['column_left'] = $this->load->controller('common/column_left');
390: | $data['footer'] = $this->load->controller('common/footer');
391: |
392: | $this->response->setOutput($this->load->view('marketing/marketing_form', $data));
393: | }
394: |
395: | /**
396: | * Save
397: | *
398: | * @return void
399: | */
400: | public function save(): void {
401: | $this->load->language('marketing/marketing');
402: |
403: | $json = [];
404: |
405: | if (!$this->user->hasPermission('modify', 'marketing/marketing')) {
406: | $json['error']['warning'] = $this->language->get('error_permission');
407: | }
408: |
409: | if (!oc_validate_length($this->request->post['name'], 1, 32)) {
410: | $json['error']['name'] = $this->language->get('error_name');
411: | }
412: |
413: | if (!$this->request->post['code']) {
414: | $json['error']['code'] = $this->language->get('error_code');
415: | }
416: |
417: | $this->load->model('marketing/marketing');
418: |
419: | $marketing_info = $this->model_marketing_marketing->getMarketingByCode($this->request->post['code']);
420: |
421: | if ($marketing_info && (!isset($this->request->post['marketing_id']) || ($this->request->post['marketing_id'] != $marketing_info['marketing_id']))) {
422: | $json['error']['code'] = $this->language->get('error_exists');
423: | }
424: |
425: | if (!$json) {
426: | if (!$this->request->post['marketing_id']) {
427: | $json['marketing_id'] = $this->model_marketing_marketing->addMarketing($this->request->post);
428: | } else {
429: | $this->model_marketing_marketing->editMarketing($this->request->post['marketing_id'], $this->request->post);
430: | }
431: |
432: | $json['success'] = $this->language->get('text_success');
433: | }
434: |
435: | $this->response->addHeader('Content-Type: application/json');
436: | $this->response->setOutput(json_encode($json));
437: | }
438: |
439: | /**
440: | * Delete
441: | *
442: | * @return void
443: | */
444: | public function delete(): void {
445: | $this->load->language('marketing/marketing');
446: |
447: | $json = [];
448: |
449: | if (isset($this->request->post['selected'])) {
450: | $selected = $this->request->post['selected'];
451: | } else {
452: | $selected = [];
453: | }
454: |
455: | if (!$this->user->hasPermission('modify', 'marketing/marketing')) {
456: | $json['error'] = $this->language->get('error_permission');
457: | }
458: |
459: | if (!$json) {
460: | $this->load->model('marketing/marketing');
461: |
462: | foreach ($selected as $marketing_id) {
463: | $this->model_marketing_marketing->deleteMarketing($marketing_id);
464: | }
465: |
466: | $json['success'] = $this->language->get('text_success');
467: | }
468: |
469: | $this->response->addHeader('Content-Type: application/json');
470: | $this->response->setOutput(json_encode($json));
471: | }
472: |
473: | /**
474: | * Report
475: | *
476: | * @return void
477: | */
478: | public function report(): void {
479: | $this->load->language('marketing/marketing');
480: |
481: | $this->response->setOutput($this->getReport());
482: | }
483: |
484: | /**
485: | * Get Report
486: | *
487: | * @return string
488: | */
489: | public function getReport(): string {
490: | if (isset($this->request->get['marketing_id'])) {
491: | $marketing_id = (int)$this->request->get['marketing_id'];
492: | } else {
493: | $marketing_id = 0;
494: | }
495: |
496: | if (isset($this->request->get['page']) && $this->request->get['route'] == 'marketing/marketing.report') {
497: | $page = (int)$this->request->get['page'];
498: | } else {
499: | $page = 1;
500: | }
501: |
502: | $limit = 10;
503: |
504: | $data['reports'] = [];
505: |
506: | $this->load->model('marketing/marketing');
507: | $this->load->model('customer/customer');
508: | $this->load->model('setting/store');
509: |
510: | $results = $this->model_marketing_marketing->getReports($marketing_id, ($page - 1) * $limit, $limit);
511: |
512: | foreach ($results as $result) {
513: | $store_info = $this->model_setting_store->getStore($result['store_id']);
514: |
515: | if ($store_info) {
516: | $store = $store_info['name'];
517: | } elseif (!$result['store_id']) {
518: | $store = $this->config->get('config_name');
519: | } else {
520: | $store = '';
521: | }
522: |
523: | $data['reports'][] = [
524: | 'ip' => $result['ip'],
525: | 'account' => $this->model_customer_customer->getTotalCustomersByIp($result['ip']),
526: | 'store' => $store,
527: | 'country' => $result['country'],
528: | 'date_added' => date($this->language->get('datetime_format'), strtotime($result['date_added'])),
529: | 'filter_ip' => $this->url->link('customer/customer', 'user_token=' . $this->session->data['user_token'] . '&filter_ip=' . $result['ip'])
530: | ];
531: | }
532: |
533: | $report_total = $this->model_marketing_marketing->getTotalReports($marketing_id);
534: |
535: | $data['pagination'] = $this->load->controller('common/pagination', [
536: | 'total' => $report_total,
537: | 'page' => $page,
538: | 'limit' => $limit,
539: | 'url' => $this->url->link('marketing/marketing.report', 'user_token=' . $this->session->data['user_token'] . '&marketing_id=' . $marketing_id . '&page={page}')
540: | ]);
541: |
542: | $data['results'] = sprintf($this->language->get('text_pagination'), ($report_total) ? (($page - 1) * $limit) + 1 : 0, ((($page - 1) * $limit) > ($report_total - $limit)) ? $report_total : ((($page - 1) * $limit) + $limit), $report_total, ceil($report_total / $limit));
543: |
544: | return $this->load->view('marketing/marketing_report', $data);
545: | }
546: |
547: | /**
548: | * Autocomplete
549: | *
550: | * @return void
551: | */
552: | public function autocomplete(): void {
553: | $json = [];
554: |
555: | if (isset($this->request->get['filter_name'])) {
556: | $filter_name = $this->request->get['filter_name'];
557: | } else {
558: | $filter_name = '';
559: | }
560: |
561: | if (isset($this->request->get['filter_code'])) {
562: | $filter_code = $this->request->get['filter_code'];
563: | } else {
564: | $filter_code = '';
565: | }
566: |
567: | $filter_data = [
568: | 'filter_name' => $filter_name,
569: | 'filter_code' => $filter_code,
570: | 'start' => 0,
571: | 'limit' => 5
572: | ];
573: |
574: | $this->load->model('marketing/marketing');
575: |
576: | $results = $this->model_marketing_marketing->getMarketings($filter_data);
577: |
578: | foreach ($results as $result) {
579: | $json[] = [
580: | 'marketing_id' => $result['marketing_id'],
581: | 'name' => strip_tags(html_entity_decode($result['name'], ENT_QUOTES, 'UTF-8')),
582: | 'code' => $result['code']
583: | ];
584: | }
585: |
586: | $sort_order = [];
587: |
588: | foreach ($json as $key => $value) {
589: | $sort_order[$key] = $value['name'];
590: | }
591: |
592: | array_multisort($sort_order, SORT_ASC, $json);
593: |
594: | $this->response->addHeader('Content-Type: application/json');
595: | $this->response->setOutput(json_encode($json));
596: | }
597: | }
598: |

OpenCart API API documentation generated by ApiGen dev-master