1: | <?php
2: | namespace Opencart\Admin\Controller\Design;
3: | /**
4: | * Class Banner
5: | *
6: | * @package Opencart\Admin\Controller\Design
7: | */
8: | class Banner extends \Opencart\System\Engine\Controller {
9: | /**
10: | * Index
11: | *
12: | * @return void
13: | */
14: | public function index(): void {
15: | $this->load->language('design/banner');
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('design/banner', 'user_token=' . $this->session->data['user_token'] . $url)
43: | ];
44: |
45: | $data['add'] = $this->url->link('design/banner.form', 'user_token=' . $this->session->data['user_token'] . $url);
46: | $data['delete'] = $this->url->link('design/banner.delete', 'user_token=' . $this->session->data['user_token']);
47: |
48: | $data['list'] = $this->getList();
49: |
50: | $data['user_token'] = $this->session->data['user_token'];
51: |
52: | $data['header'] = $this->load->controller('common/header');
53: | $data['column_left'] = $this->load->controller('common/column_left');
54: | $data['footer'] = $this->load->controller('common/footer');
55: |
56: | $this->response->setOutput($this->load->view('design/banner', $data));
57: | }
58: |
59: | /**
60: | * List
61: | *
62: | * @return void
63: | */
64: | public function list(): void {
65: | $this->load->language('design/banner');
66: |
67: | $this->response->setOutput($this->getList());
68: | }
69: |
70: | /**
71: | * Get List
72: | *
73: | * @return string
74: | */
75: | protected function getList(): string {
76: | if (isset($this->request->get['sort'])) {
77: | $sort = (string)$this->request->get['sort'];
78: | } else {
79: | $sort = 'name';
80: | }
81: |
82: | if (isset($this->request->get['order'])) {
83: | $order = (string)$this->request->get['order'];
84: | } else {
85: | $order = 'ASC';
86: | }
87: |
88: | if (isset($this->request->get['page'])) {
89: | $page = (int)$this->request->get['page'];
90: | } else {
91: | $page = 1;
92: | }
93: |
94: | $url = '';
95: |
96: | if (isset($this->request->get['sort'])) {
97: | $url .= '&sort=' . $this->request->get['sort'];
98: | }
99: |
100: | if (isset($this->request->get['order'])) {
101: | $url .= '&order=' . $this->request->get['order'];
102: | }
103: |
104: | if (isset($this->request->get['page'])) {
105: | $url .= '&page=' . $this->request->get['page'];
106: | }
107: |
108: | $data['action'] = $this->url->link('design/banner.list', 'user_token=' . $this->session->data['user_token'] . $url);
109: |
110: | $data['banners'] = [];
111: |
112: | $filter_data = [
113: | 'sort' => $sort,
114: | 'order' => $order,
115: | 'start' => ($page - 1) * $this->config->get('config_pagination_admin'),
116: | 'limit' => $this->config->get('config_pagination_admin')
117: | ];
118: |
119: | $this->load->model('design/banner');
120: |
121: | $results = $this->model_design_banner->getBanners($filter_data);
122: |
123: | foreach ($results as $result) {
124: | $data['banners'][] = [
125: | 'banner_id' => $result['banner_id'],
126: | 'name' => $result['name'],
127: | 'status' => $result['status'],
128: | 'edit' => $this->url->link('design/banner.form', 'user_token=' . $this->session->data['user_token'] . '&banner_id=' . $result['banner_id'] . $url)
129: | ];
130: | }
131: |
132: | $url = '';
133: |
134: | if ($order == 'ASC') {
135: | $url .= '&order=DESC';
136: | } else {
137: | $url .= '&order=ASC';
138: | }
139: |
140: | $data['sort_name'] = $this->url->link('design/banner.list', 'user_token=' . $this->session->data['user_token'] . '&sort=name' . $url);
141: |
142: | $url = '';
143: |
144: | if (isset($this->request->get['sort'])) {
145: | $url .= '&sort=' . $this->request->get['sort'];
146: | }
147: |
148: | if (isset($this->request->get['order'])) {
149: | $url .= '&order=' . $this->request->get['order'];
150: | }
151: |
152: | $banner_total = $this->model_design_banner->getTotalBanners();
153: |
154: | $data['pagination'] = $this->load->controller('common/pagination', [
155: | 'total' => $banner_total,
156: | 'page' => $page,
157: | 'limit' => $this->config->get('config_pagination_admin'),
158: | 'url' => $this->url->link('design/banner.list', 'user_token=' . $this->session->data['user_token'] . $url . '&page={page}')
159: | ]);
160: |
161: | $data['results'] = sprintf($this->language->get('text_pagination'), ($banner_total) ? (($page - 1) * $this->config->get('config_pagination_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_pagination_admin')) > ($banner_total - $this->config->get('config_pagination_admin'))) ? $banner_total : ((($page - 1) * $this->config->get('config_pagination_admin')) + $this->config->get('config_pagination_admin')), $banner_total, ceil($banner_total / $this->config->get('config_pagination_admin')));
162: |
163: | $data['sort'] = $sort;
164: | $data['order'] = $order;
165: |
166: | return $this->load->view('design/banner_list', $data);
167: | }
168: |
169: | /**
170: | * Form
171: | *
172: | * @return void
173: | */
174: | public function form(): void {
175: | $this->load->language('design/banner');
176: |
177: | $this->document->setTitle($this->language->get('heading_title'));
178: |
179: | $data['text_form'] = !isset($this->request->get['banner_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
180: |
181: | $url = '';
182: |
183: | if (isset($this->request->get['sort'])) {
184: | $url .= '&sort=' . $this->request->get['sort'];
185: | }
186: |
187: | if (isset($this->request->get['order'])) {
188: | $url .= '&order=' . $this->request->get['order'];
189: | }
190: |
191: | if (isset($this->request->get['page'])) {
192: | $url .= '&page=' . $this->request->get['page'];
193: | }
194: |
195: | $data['breadcrumbs'] = [];
196: |
197: | $data['breadcrumbs'][] = [
198: | 'text' => $this->language->get('text_home'),
199: | 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
200: | ];
201: |
202: | $data['breadcrumbs'][] = [
203: | 'text' => $this->language->get('heading_title'),
204: | 'href' => $this->url->link('design/banner', 'user_token=' . $this->session->data['user_token'] . $url)
205: | ];
206: |
207: | $data['save'] = $this->url->link('design/banner.save', 'user_token=' . $this->session->data['user_token']);
208: | $data['back'] = $this->url->link('design/banner', 'user_token=' . $this->session->data['user_token'] . $url);
209: |
210: | if (isset($this->request->get['banner_id'])) {
211: | $this->load->model('design/banner');
212: |
213: | $banner_info = $this->model_design_banner->getBanner($this->request->get['banner_id']);
214: | }
215: |
216: | if (isset($this->request->get['banner_id'])) {
217: | $data['banner_id'] = (int)$this->request->get['banner_id'];
218: | } else {
219: | $data['banner_id'] = 0;
220: | }
221: |
222: | if (!empty($banner_info)) {
223: | $data['name'] = $banner_info['name'];
224: | } else {
225: | $data['name'] = '';
226: | }
227: |
228: | if (!empty($banner_info)) {
229: | $data['status'] = $banner_info['status'];
230: | } else {
231: | $data['status'] = true;
232: | }
233: |
234: | $this->load->model('localisation/language');
235: |
236: | $data['languages'] = $this->model_localisation_language->getLanguages();
237: |
238: | $this->load->model('tool/image');
239: |
240: | if (!empty($banner_info)) {
241: | $banner_images = $this->model_design_banner->getImages($this->request->get['banner_id']);
242: | } else {
243: | $banner_images = [];
244: | }
245: |
246: | $data['banner_images'] = [];
247: |
248: | foreach ($banner_images as $language_id => $banner_image) {
249: | foreach ($banner_image as $value) {
250: | if ($value['image'] && is_file(DIR_IMAGE . html_entity_decode($value['image'], ENT_QUOTES, 'UTF-8'))) {
251: | $image = $value['image'];
252: | $thumb = $value['image'];
253: | } else {
254: | $image = '';
255: | $thumb = 'no_image.png';
256: | }
257: |
258: | $data['banner_images'][$language_id][] = [
259: | 'title' => $value['title'],
260: | 'link' => $value['link'],
261: | 'image' => $image,
262: | 'thumb' => $this->model_tool_image->resize($thumb, $this->config->get('config_image_default_width'), $this->config->get('config_image_default_height')),
263: | 'sort_order' => $value['sort_order']
264: | ];
265: | }
266: | }
267: |
268: | $data['placeholder'] = $this->model_tool_image->resize('no_image.png', $this->config->get('config_image_default_width'), $this->config->get('config_image_default_height'));
269: |
270: | $data['user_token'] = $this->session->data['user_token'];
271: |
272: | $data['header'] = $this->load->controller('common/header');
273: | $data['column_left'] = $this->load->controller('common/column_left');
274: | $data['footer'] = $this->load->controller('common/footer');
275: |
276: | $this->response->setOutput($this->load->view('design/banner_form', $data));
277: | }
278: |
279: | /**
280: | * Save
281: | *
282: | * @return void
283: | */
284: | public function save(): void {
285: | $this->load->language('design/banner');
286: |
287: | $json = [];
288: |
289: | if (!$this->user->hasPermission('modify', 'design/banner')) {
290: | $json['error']['warning'] = $this->language->get('error_permission');
291: | }
292: |
293: | if ((oc_strlen($this->request->post['name']) < 3) || (oc_strlen($this->request->post['name']) > 64)) {
294: | $json['error']['name'] = $this->language->get('error_name');
295: | }
296: |
297: | if (isset($this->request->post['banner_image'])) {
298: | foreach ($this->request->post['banner_image'] as $language_id => $banner_image) {
299: | foreach ($banner_image as $key => $value) {
300: | if ((oc_strlen(trim($value['title'])) < 2) || (oc_strlen($value['title']) > 64)) {
301: | $json['error']['image_' . $language_id . '_' . $key . '_title'] = $this->language->get('error_title');
302: | }
303: | }
304: | }
305: | }
306: |
307: | if (!$json) {
308: | $this->load->model('design/banner');
309: |
310: | if (!$this->request->post['banner_id']) {
311: | $json['banner_id'] = $this->model_design_banner->addBanner($this->request->post);
312: | } else {
313: | $this->model_design_banner->editBanner($this->request->post['banner_id'], $this->request->post);
314: | }
315: |
316: | $json['success'] = $this->language->get('text_success');
317: | }
318: |
319: | $this->response->addHeader('Content-Type: application/json');
320: | $this->response->setOutput(json_encode($json));
321: | }
322: |
323: | /**
324: | * Delete
325: | *
326: | * @return void
327: | */
328: | public function delete(): void {
329: | $this->load->language('design/banner');
330: |
331: | $json = [];
332: |
333: | if (isset($this->request->post['selected'])) {
334: | $selected = $this->request->post['selected'];
335: | } else {
336: | $selected = [];
337: | }
338: |
339: | if (!$this->user->hasPermission('modify', 'design/banner')) {
340: | $json['error'] = $this->language->get('error_permission');
341: | }
342: |
343: | if (!$json) {
344: | $this->load->model('design/banner');
345: |
346: | foreach ($selected as $banner_id) {
347: | $this->model_design_banner->deleteBanner($banner_id);
348: | }
349: |
350: | $json['success'] = $this->language->get('text_success');
351: | }
352: |
353: | $this->response->addHeader('Content-Type: application/json');
354: | $this->response->setOutput(json_encode($json));
355: | }
356: | }
357: |

OpenCart API API documentation generated by ApiGen dev-master