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

OpenCart API API documentation generated by ApiGen dev-master