1: | <?php
2: | namespace Opencart\Admin\Controller\Catalog;
3: | /**
4: | * Class Attribute
5: | *
6: | * @package Opencart\Admin\Controller\Catalog
7: | */
8: | class Attribute extends \Opencart\System\Engine\Controller {
9: | /**
10: | * Index
11: | *
12: | * @return void
13: | */
14: | public function index(): void {
15: | $this->load->language('catalog/attribute');
16: |
17: | $this->document->setTitle($this->language->get('heading_title'));
18: |
19: | $url = '';
20: |
21: | if (isset($this->request->get['sort'])) {
22: | $url .= '&sort=' . (string)$this->request->get['sort'];
23: | }
24: |
25: | if (isset($this->request->get['order'])) {
26: | $url .= '&order=' . (string)$this->request->get['order'];
27: | }
28: |
29: | if (isset($this->request->get['page'])) {
30: | $url .= '&page=' . (int)$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/attribute', 'user_token=' . $this->session->data['user_token'] . $url)
43: | ];
44: |
45: | $data['add'] = $this->url->link('catalog/attribute.form', 'user_token=' . $this->session->data['user_token'] . $url);
46: | $data['delete'] = $this->url->link('catalog/attribute.delete', 'user_token=' . $this->session->data['user_token']);
47: |
48: | $data['list'] = $this->controller_catalog_attribute->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('catalog/attribute', $data));
57: | }
58: |
59: | /**
60: | * List
61: | *
62: | * @return void
63: | */
64: | public function list(): void {
65: | $this->load->language('catalog/attribute');
66: |
67: | $this->response->setOutput($this->controller_catalog_attribute->getList());
68: | }
69: |
70: | /**
71: | * Get List
72: | *
73: | * @return string
74: | */
75: | public function getList(): string {
76: | if (isset($this->request->get['sort'])) {
77: | $sort = (string)$this->request->get['sort'];
78: | } else {
79: | $sort = 'ad.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/attribute.list', 'user_token=' . $this->session->data['user_token'] . $url);
109: |
110: | $data['attributes'] = [];
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/attribute');
120: |
121: | $results = $this->model_catalog_attribute->getAttributes($filter_data);
122: |
123: | foreach ($results as $result) {
124: | $data['attributes'][] = [
125: | 'attribute_id' => $result['attribute_id'],
126: | 'name' => $result['name'],
127: | 'attribute_group' => $result['attribute_group'],
128: | 'sort_order' => $result['sort_order'],
129: | 'edit' => $this->url->link('catalog/attribute.form', 'user_token=' . $this->session->data['user_token'] . '&attribute_id=' . $result['attribute_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: | $data['sort_name'] = $this->url->link('catalog/attribute.list', 'user_token=' . $this->session->data['user_token'] . '&sort=ad.name' . $url);
142: | $data['sort_attribute_group'] = $this->url->link('catalog/attribute.list', 'user_token=' . $this->session->data['user_token'] . '&sort=attribute_group' . $url);
143: | $data['sort_sort_order'] = $this->url->link('catalog/attribute.list', 'user_token=' . $this->session->data['user_token'] . '&sort=a.sort_order' . $url);
144: |
145: | $url = '';
146: |
147: | if ($sort) {
148: | $url .= '&sort=' . $sort;
149: | }
150: |
151: | if (isset($this->request->get['order'])) {
152: | $url .= '&order=' . $this->request->get['order'];
153: | }
154: |
155: | $attribute_total = $this->model_catalog_attribute->getTotalAttributes();
156: |
157: | $data['pagination'] = $this->load->controller('common/pagination', [
158: | 'total' => $attribute_total,
159: | 'page' => $page,
160: | 'limit' => $this->config->get('config_pagination_admin'),
161: | 'url' => $this->url->link('catalog/attribute.list', 'user_token=' . $this->session->data['user_token'] . $url . '&page={page}')
162: | ]);
163: |
164: | $data['results'] = sprintf($this->language->get('text_pagination'), ($attribute_total) ? (($page - 1) * $this->config->get('config_pagination_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_pagination_admin')) > ($attribute_total - $this->config->get('config_pagination_admin'))) ? $attribute_total : ((($page - 1) * $this->config->get('config_pagination_admin')) + $this->config->get('config_pagination_admin')), $attribute_total, ceil($attribute_total / $this->config->get('config_pagination_admin')));
165: |
166: | $data['sort'] = $sort;
167: | $data['order'] = $order;
168: |
169: | return $this->load->view('catalog/attribute_list', $data);
170: | }
171: |
172: | /**
173: | * Form
174: | *
175: | * @return void
176: | */
177: | public function form(): void {
178: | $this->load->language('catalog/attribute');
179: |
180: | $this->document->setTitle($this->language->get('heading_title'));
181: |
182: | $data['text_form'] = !isset($this->request->get['attribute_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
183: |
184: | $url = '';
185: |
186: | if (isset($this->request->get['sort'])) {
187: | $url .= '&sort=' . $this->request->get['sort'];
188: | }
189: |
190: | if (isset($this->request->get['order'])) {
191: | $url .= '&order=' . $this->request->get['order'];
192: | }
193: |
194: | if (isset($this->request->get['page'])) {
195: | $url .= '&page=' . $this->request->get['page'];
196: | }
197: |
198: | $data['breadcrumbs'] = [];
199: |
200: | $data['breadcrumbs'][] = [
201: | 'text' => $this->language->get('text_home'),
202: | 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
203: | ];
204: |
205: | $data['breadcrumbs'][] = [
206: | 'text' => $this->language->get('heading_title'),
207: | 'href' => $this->url->link('catalog/attribute', 'user_token=' . $this->session->data['user_token'] . $url)
208: | ];
209: |
210: | $data['save'] = $this->url->link('catalog/attribute.save', 'user_token=' . $this->session->data['user_token']);
211: | $data['back'] = $this->url->link('catalog/attribute', 'user_token=' . $this->session->data['user_token'] . $url);
212: |
213: | if (isset($this->request->get['attribute_id'])) {
214: | $this->load->model('catalog/attribute');
215: |
216: | $attribute_info = $this->model_catalog_attribute->getAttribute($this->request->get['attribute_id']);
217: | }
218: |
219: | if (isset($this->request->get['attribute_id'])) {
220: | $data['attribute_id'] = (int)$this->request->get['attribute_id'];
221: | } else {
222: | $data['attribute_id'] = 0;
223: | }
224: |
225: | $this->load->model('localisation/language');
226: |
227: | $data['languages'] = $this->model_localisation_language->getLanguages();
228: |
229: | if (isset($this->request->get['attribute_id'])) {
230: | $data['attribute_description'] = $this->model_catalog_attribute->getDescriptions($this->request->get['attribute_id']);
231: | } else {
232: | $data['attribute_description'] = [];
233: | }
234: |
235: | $this->load->model('catalog/attribute_group');
236: |
237: | $data['attribute_groups'] = $this->model_catalog_attribute_group->getAttributeGroups();
238: |
239: | if (!empty($attribute_info)) {
240: | $data['attribute_group_id'] = $attribute_info['attribute_group_id'];
241: | } else {
242: | $data['attribute_group_id'] = 0;
243: | }
244: |
245: | if (!empty($attribute_info)) {
246: | $data['sort_order'] = $attribute_info['sort_order'];
247: | } else {
248: | $data['sort_order'] = 0;
249: | }
250: |
251: | $data['user_token'] = $this->session->data['user_token'];
252: |
253: | $data['header'] = $this->load->controller('common/header');
254: | $data['column_left'] = $this->load->controller('common/column_left');
255: | $data['footer'] = $this->load->controller('common/footer');
256: |
257: | $this->response->setOutput($this->load->view('catalog/attribute_form', $data));
258: | }
259: |
260: | /**
261: | * Save
262: | *
263: | * @return void
264: | */
265: | public function save(): void {
266: | $this->load->language('catalog/attribute');
267: |
268: | $json = [];
269: |
270: | if (!$this->user->hasPermission('modify', 'catalog/attribute')) {
271: | $json['error']['warning'] = $this->language->get('error_permission');
272: | }
273: |
274: | if (!$this->request->post['attribute_group_id']) {
275: | $json['error']['attribute_group'] = $this->language->get('error_attribute_group');
276: | }
277: |
278: | foreach ($this->request->post['attribute_description'] as $language_id => $value) {
279: | if (!oc_validate_length($value['name'], 1, 64)) {
280: | $json['error']['name_' . $language_id] = $this->language->get('error_name');
281: | }
282: | }
283: |
284: | if (isset($json['error']) && !isset($json['error']['warning'])) {
285: | $json['error']['warning'] = $this->language->get('error_warning');
286: | }
287: |
288: | if (!$json) {
289: | $this->load->model('catalog/attribute');
290: |
291: | if (!$this->request->post['attribute_id']) {
292: | $json['attribute_id'] = $this->model_catalog_attribute->addAttribute($this->request->post);
293: | } else {
294: | $this->model_catalog_attribute->editAttribute($this->request->post['attribute_id'], $this->request->post);
295: | }
296: |
297: | $json['success'] = $this->language->get('text_success');
298: | }
299: |
300: | $this->response->addHeader('Content-Type: application/json');
301: | $this->response->setOutput(json_encode($json));
302: | }
303: |
304: | /**
305: | * Delete
306: | *
307: | * @return void
308: | */
309: | public function delete(): void {
310: | $this->load->language('catalog/attribute');
311: |
312: | $json = [];
313: |
314: | if (isset($this->request->post['selected'])) {
315: | $selected = $this->request->post['selected'];
316: | } else {
317: | $selected = [];
318: | }
319: |
320: | if (!$this->user->hasPermission('modify', 'catalog/attribute')) {
321: | $json['error'] = $this->language->get('error_permission');
322: | }
323: |
324: | $this->load->model('catalog/product');
325: |
326: | foreach ($selected as $attribute_id) {
327: | $product_total = $this->model_catalog_product->getTotalAttributesByAttributeId($attribute_id);
328: |
329: | if ($product_total) {
330: | $json['error'] = sprintf($this->language->get('error_product'), $product_total);
331: | }
332: | }
333: |
334: | if (!$json) {
335: | $this->load->model('catalog/attribute');
336: |
337: | foreach ($selected as $attribute_id) {
338: | $this->model_catalog_attribute->deleteAttribute($attribute_id);
339: | }
340: |
341: | $json['success'] = $this->language->get('text_success');
342: | }
343: |
344: | $this->response->addHeader('Content-Type: application/json');
345: | $this->response->setOutput(json_encode($json));
346: | }
347: |
348: | /**
349: | * Autocomplete
350: | *
351: | * @return void
352: | */
353: | public function autocomplete(): void {
354: | $json = [];
355: |
356: | if (isset($this->request->get['filter_name'])) {
357: | $this->load->model('catalog/attribute');
358: |
359: | $filter_data = [
360: | 'filter_name' => $this->request->get['filter_name'],
361: | 'start' => 0,
362: | 'limit' => 5
363: | ];
364: |
365: | $results = $this->model_catalog_attribute->getAttributes($filter_data);
366: |
367: | foreach ($results as $result) {
368: | $json[] = [
369: | 'attribute_id' => $result['attribute_id'],
370: | 'name' => strip_tags(html_entity_decode($result['name'], ENT_QUOTES, 'UTF-8')),
371: | 'attribute_group' => $result['attribute_group']
372: | ];
373: | }
374: | }
375: |
376: | $sort_order = [];
377: |
378: | foreach ($json as $key => $value) {
379: | $sort_order[$key] = $value['name'];
380: | }
381: |
382: | array_multisort($sort_order, SORT_ASC, $json);
383: |
384: | $this->response->addHeader('Content-Type: application/json');
385: | $this->response->setOutput(json_encode($json));
386: | }
387: | }
388: |

OpenCart API API documentation generated by ApiGen dev-master