1: | <?php
2: | namespace Opencart\Admin\Controller\Catalog;
3: | /**
4: | * Class Filter
5: | *
6: | * @package Opencart\Admin\Controller\Catalog
7: | */
8: | class Filter extends \Opencart\System\Engine\Controller {
9: | /**
10: | * Index
11: | *
12: | * @return void
13: | */
14: | public function index(): void {
15: | $this->load->language('catalog/filter');
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('catalog/filter', 'user_token=' . $this->session->data['user_token'] . $url)
43: | ];
44: |
45: | $data['add'] = $this->url->link('catalog/filter.form', 'user_token=' . $this->session->data['user_token'] . $url);
46: | $data['delete'] = $this->url->link('catalog/filter.delete', 'user_token=' . $this->session->data['user_token']);
47: |
48: | $data['user_token'] = $this->session->data['user_token'];
49: |
50: | $data['list'] = $this->controller_catalog_filter->getList();
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('catalog/filter', $data));
57: | }
58: |
59: | /**
60: | * List
61: | *
62: | * @return void
63: | */
64: | public function list(): void {
65: | $this->load->language('catalog/filter');
66: |
67: | $this->response->setOutput($this->controller_catalog_filter->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 = 'fgd.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('catalog/filter.list', 'user_token=' . $this->session->data['user_token'] . $url);
109: |
110: | $data['filters'] = [];
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('catalog/filter');
120: |
121: | $results = $this->model_catalog_filter->getFilters($filter_data);
122: |
123: | foreach ($results as $result) {
124: | $data['filters'][] = [
125: | 'filter_id' => $result['filter_id'],
126: | 'name' => $result['name'],
127: | 'filter_group' => $result['filter_group'],
128: | 'sort_order' => $result['sort_order'],
129: | 'edit' => $this->url->link('catalog/filter.form', 'user_token=' . $this->session->data['user_token'] . '&filter_id=' . $result['filter_id'] . $url)
130: | ];
131: | }
132: |
133: | $url = '';
134: |
135: | if ($order == 'ASC') {
136: | $url .= '&order=DESC';
137: | } else {
138: | $url .= '&order=ASC';
139: | }
140: |
141: | if (isset($this->request->get['page'])) {
142: | $url .= '&page=' . $this->request->get['page'];
143: | }
144: |
145: | $data['sort_name'] = $this->url->link('catalog/filter.list', 'user_token=' . $this->session->data['user_token'] . '&sort=fd.name' . $url);
146: | $data['sort_filter_group'] = $this->url->link('catalog/filter.list', 'user_token=' . $this->session->data['user_token'] . '&sort=filter_group' . $url);
147: | $data['sort_sort_order'] = $this->url->link('catalog/filter.list', 'user_token=' . $this->session->data['user_token'] . '&sort=f.sort_order' . $url);
148: |
149: | $url = '';
150: |
151: | if (isset($this->request->get['sort'])) {
152: | $url .= '&sort=' . $this->request->get['sort'];
153: | }
154: |
155: | if (isset($this->request->get['order'])) {
156: | $url .= '&order=' . $this->request->get['order'];
157: | }
158: |
159: | $filter_total = $this->model_catalog_filter->getTotalFilters();
160: |
161: | $data['pagination'] = $this->load->controller('common/pagination', [
162: | 'total' => $filter_total,
163: | 'page' => $page,
164: | 'limit' => $this->config->get('config_pagination_admin'),
165: | 'url' => $this->url->link('catalog/filter.list', 'user_token=' . $this->session->data['user_token'] . $url . '&page={page}')
166: | ]);
167: |
168: | $data['results'] = sprintf($this->language->get('text_pagination'), ($filter_total) ? (($page - 1) * $this->config->get('config_pagination_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_pagination_admin')) > ($filter_total - $this->config->get('config_pagination_admin'))) ? $filter_total : ((($page - 1) * $this->config->get('config_pagination_admin')) + $this->config->get('config_pagination_admin')), $filter_total, ceil($filter_total / $this->config->get('config_pagination_admin')));
169: |
170: | $data['sort'] = $sort;
171: | $data['order'] = $order;
172: |
173: | return $this->load->view('catalog/filter_list', $data);
174: | }
175: |
176: | /**
177: | * Form
178: | *
179: | * @return void
180: | */
181: | public function form(): void {
182: | $this->load->language('catalog/filter');
183: |
184: | $this->document->setTitle($this->language->get('heading_title'));
185: |
186: | $data['text_form'] = !isset($this->request->get['filter_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
187: |
188: | $url = '';
189: |
190: | if (isset($this->request->get['sort'])) {
191: | $url .= '&sort=' . $this->request->get['sort'];
192: | }
193: |
194: | if (isset($this->request->get['order'])) {
195: | $url .= '&order=' . $this->request->get['order'];
196: | }
197: |
198: | if (isset($this->request->get['page'])) {
199: | $url .= '&page=' . $this->request->get['page'];
200: | }
201: |
202: | $data['breadcrumbs'] = [];
203: |
204: | $data['breadcrumbs'][] = [
205: | 'text' => $this->language->get('text_home'),
206: | 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
207: | ];
208: |
209: | $data['breadcrumbs'][] = [
210: | 'text' => $this->language->get('heading_title'),
211: | 'href' => $this->url->link('catalog/filter', 'user_token=' . $this->session->data['user_token'] . $url)
212: | ];
213: |
214: | $data['save'] = $this->url->link('catalog/filter.save', 'user_token=' . $this->session->data['user_token']);
215: | $data['back'] = $this->url->link('catalog/filter', 'user_token=' . $this->session->data['user_token'] . $url);
216: |
217: | if (isset($this->request->get['filter_id'])) {
218: | $this->load->model('catalog/filter');
219: |
220: | $filter_info = $this->model_catalog_filter->getFilter($this->request->get['filter_id']);
221: | }
222: |
223: | if (isset($this->request->get['filter_id'])) {
224: | $data['filter_id'] = (int)$this->request->get['filter_id'];
225: | } else {
226: | $data['filter_id'] = 0;
227: | }
228: |
229: | $this->load->model('localisation/language');
230: |
231: | $data['languages'] = $this->model_localisation_language->getLanguages();
232: |
233: | if (isset($this->request->get['filter_id'])) {
234: | $data['filter_description'] = $this->model_catalog_filter->getDescriptions($this->request->get['filter_id']);
235: | } else {
236: | $data['filter_description'] = [];
237: | }
238: |
239: | if (!empty($filter_info)) {
240: | $data['sort_order'] = $filter_info['sort_order'];
241: | } else {
242: | $data['sort_order'] = '';
243: | }
244: |
245: | $this->load->model('catalog/filter_group');
246: |
247: | $data['filter_groups'] = $this->model_catalog_filter_group->getFilterGroups();
248: |
249: | if (!empty($filter_info)) {
250: | $data['filter_group_id'] = $filter_info['filter_group_id'];
251: | } else {
252: | $data['filter_group_id'] = 0;
253: | }
254: |
255: | $data['header'] = $this->load->controller('common/header');
256: | $data['column_left'] = $this->load->controller('common/column_left');
257: | $data['footer'] = $this->load->controller('common/footer');
258: |
259: | $this->response->setOutput($this->load->view('catalog/filter_form', $data));
260: | }
261: |
262: | /**
263: | * Save
264: | *
265: | * @return void
266: | */
267: | public function save(): void {
268: | $this->load->language('catalog/filter');
269: |
270: | $json = [];
271: |
272: | if (!$this->user->hasPermission('modify', 'catalog/filter')) {
273: | $json['error']['warning'] = $this->language->get('error_permission');
274: | }
275: |
276: | foreach ($this->request->post['filter_description'] as $language_id => $value) {
277: | if (!oc_validate_length($value['name'], 1, 64)) {
278: | $json['error']['name_' . $language_id] = $this->language->get('error_name');
279: | }
280: | }
281: |
282: | if (empty($this->request->post['filter_group_id'])) {
283: | $json['error']['filter_group'] = $this->language->get('error_filter_group');
284: | }
285: |
286: | if (isset($json['error']) && !isset($json['error']['warning'])) {
287: | $json['error']['warning'] = $this->language->get('error_warning');
288: | }
289: |
290: | if (!$json) {
291: | $this->load->model('catalog/filter');
292: |
293: | if (!$this->request->post['filter_id']) {
294: | $json['filter_id'] = $this->model_catalog_filter->addFilter($this->request->post);
295: | } else {
296: | $this->model_catalog_filter->editFilter($this->request->post['filter_id'], $this->request->post);
297: | }
298: |
299: | $json['success'] = $this->language->get('text_success');
300: | }
301: |
302: | $this->response->addHeader('Content-Type: application/json');
303: | $this->response->setOutput(json_encode($json));
304: | }
305: |
306: | /**
307: | * Delete
308: | *
309: | * @return void
310: | */
311: | public function delete(): void {
312: | $this->load->language('catalog/filter');
313: |
314: | $json = [];
315: |
316: | if (isset($this->request->post['selected'])) {
317: | $selected = $this->request->post['selected'];
318: | } else {
319: | $selected = [];
320: | }
321: |
322: | if (!$this->user->hasPermission('modify', 'catalog/filter')) {
323: | $json['error'] = $this->language->get('error_permission');
324: | }
325: |
326: | if (!$json) {
327: | $this->load->model('catalog/filter');
328: |
329: | foreach ($selected as $filter_id) {
330: | $this->model_catalog_filter->deleteFilter($filter_id);
331: | }
332: |
333: | $json['success'] = $this->language->get('text_success');
334: | }
335: |
336: | $this->response->addHeader('Content-Type: application/json');
337: | $this->response->setOutput(json_encode($json));
338: | }
339: |
340: | /**
341: | * Autocomplete
342: | *
343: | * @return void
344: | */
345: | public function autocomplete(): void {
346: | $json = [];
347: |
348: | if (isset($this->request->get['filter_name'])) {
349: | $this->load->model('catalog/filter');
350: |
351: | $filter_data = [
352: | 'filter_name' => $this->request->get['filter_name'],
353: | 'start' => 0,
354: | 'limit' => 5
355: | ];
356: |
357: | $filters = $this->model_catalog_filter->getFilters($filter_data);
358: |
359: | foreach ($filters as $filter) {
360: | $json[] = [
361: | 'filter_id' => $filter['filter_id'],
362: | 'name' => strip_tags(html_entity_decode($filter['filter_group'] . ' &gt; ' . $filter['name'], ENT_QUOTES, 'UTF-8'))
363: | ];
364: | }
365: | }
366: |
367: | $sort_order = [];
368: |
369: | foreach ($json as $key => $value) {
370: | $sort_order[$key] = $value['name'];
371: | }
372: |
373: | array_multisort($sort_order, SORT_ASC, $json);
374: |
375: | $this->response->addHeader('Content-Type: application/json');
376: | $this->response->setOutput(json_encode($json));
377: | }
378: | }
379: |

OpenCart API API documentation generated by ApiGen dev-master