1: | <?php
2: | namespace Opencart\Admin\Controller\Catalog;
3: | /**
4: | * Class Manufacturer
5: | *
6: | * @package Opencart\Admin\Controller\Catalog
7: | */
8: | class Manufacturer extends \Opencart\System\Engine\Controller {
9: | /**
10: | * Index
11: | *
12: | * @return void
13: | */
14: | public function index(): void {
15: | $this->load->language('catalog/manufacturer');
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/manufacturer', 'user_token=' . $this->session->data['user_token'] . $url)
43: | ];
44: |
45: | $data['add'] = $this->url->link('catalog/manufacturer.form', 'user_token=' . $this->session->data['user_token'] . $url);
46: | $data['delete'] = $this->url->link('catalog/manufacturer.delete', 'user_token=' . $this->session->data['user_token']);
47: |
48: | $data['list'] = $this->controller_catalog_manufacturer->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/manufacturer', $data));
57: | }
58: |
59: | /**
60: | * List
61: | *
62: | * @return void
63: | */
64: | public function list(): void {
65: | $this->load->language('catalog/manufacturer');
66: |
67: | $this->response->setOutput($this->controller_catalog_manufacturer->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('catalog/manufacturer.list', 'user_token=' . $this->session->data['user_token'] . $url);
109: |
110: | $data['manufacturers'] = [];
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/manufacturer');
120: |
121: | $results = $this->model_catalog_manufacturer->getManufacturers($filter_data);
122: |
123: | foreach ($results as $result) {
124: | $data['manufacturers'][] = [
125: | 'manufacturer_id' => $result['manufacturer_id'],
126: | 'name' => $result['name'],
127: | 'sort_order' => $result['sort_order'],
128: | 'edit' => $this->url->link('catalog/manufacturer.form', 'user_token=' . $this->session->data['user_token'] . '&manufacturer_id=' . $result['manufacturer_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('catalog/manufacturer.list', 'user_token=' . $this->session->data['user_token'] . '&sort=name' . $url);
141: | $data['sort_sort_order'] = $this->url->link('catalog/manufacturer.list', 'user_token=' . $this->session->data['user_token'] . '&sort=sort_order' . $url);
142: |
143: | $url = '';
144: |
145: | if (isset($this->request->get['sort'])) {
146: | $url .= '&sort=' . $this->request->get['sort'];
147: | }
148: |
149: | if (isset($this->request->get['order'])) {
150: | $url .= '&order=' . $this->request->get['order'];
151: | }
152: |
153: | $manufacturer_total = $this->model_catalog_manufacturer->getTotalManufacturers();
154: |
155: | $data['pagination'] = $this->load->controller('common/pagination', [
156: | 'total' => $manufacturer_total,
157: | 'page' => $page,
158: | 'limit' => $this->config->get('config_pagination_admin'),
159: | 'url' => $this->url->link('catalog/manufacturer.list', 'user_token=' . $this->session->data['user_token'] . $url . '&page={page}')
160: | ]);
161: |
162: | $data['results'] = sprintf($this->language->get('text_pagination'), ($manufacturer_total) ? (($page - 1) * $this->config->get('config_pagination_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_pagination_admin')) > ($manufacturer_total - $this->config->get('config_pagination_admin'))) ? $manufacturer_total : ((($page - 1) * $this->config->get('config_pagination_admin')) + $this->config->get('config_pagination_admin')), $manufacturer_total, ceil($manufacturer_total / $this->config->get('config_pagination_admin')));
163: |
164: | $data['sort'] = $sort;
165: | $data['order'] = $order;
166: |
167: | return $this->load->view('catalog/manufacturer_list', $data);
168: | }
169: |
170: | /**
171: | * Form
172: | *
173: | * @return void
174: | */
175: | public function form(): void {
176: | $this->load->language('catalog/manufacturer');
177: |
178: | $this->document->setTitle($this->language->get('heading_title'));
179: |
180: | $data['text_form'] = !isset($this->request->get['manufacturer_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
181: |
182: | $url = '';
183: |
184: | if (isset($this->request->get['sort'])) {
185: | $url .= '&sort=' . $this->request->get['sort'];
186: | }
187: |
188: | if (isset($this->request->get['order'])) {
189: | $url .= '&order=' . $this->request->get['order'];
190: | }
191: |
192: | if (isset($this->request->get['page'])) {
193: | $url .= '&page=' . $this->request->get['page'];
194: | }
195: |
196: | $data['breadcrumbs'] = [];
197: |
198: | $data['breadcrumbs'][] = [
199: | 'text' => $this->language->get('text_home'),
200: | 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
201: | ];
202: |
203: | $data['breadcrumbs'][] = [
204: | 'text' => $this->language->get('heading_title'),
205: | 'href' => $this->url->link('catalog/manufacturer', 'user_token=' . $this->session->data['user_token'] . $url)
206: | ];
207: |
208: | $data['save'] = $this->url->link('catalog/manufacturer.save', 'user_token=' . $this->session->data['user_token']);
209: | $data['back'] = $this->url->link('catalog/manufacturer', 'user_token=' . $this->session->data['user_token'] . $url);
210: |
211: | if (isset($this->request->get['manufacturer_id'])) {
212: | $this->load->model('catalog/manufacturer');
213: |
214: | $manufacturer_info = $this->model_catalog_manufacturer->getManufacturer($this->request->get['manufacturer_id']);
215: | }
216: |
217: | if (isset($this->request->get['manufacturer_id'])) {
218: | $data['manufacturer_id'] = (int)$this->request->get['manufacturer_id'];
219: | } else {
220: | $data['manufacturer_id'] = 0;
221: | }
222: |
223: | if (!empty($manufacturer_info)) {
224: | $data['name'] = $manufacturer_info['name'];
225: | } else {
226: | $data['name'] = '';
227: | }
228: |
229: | $data['stores'] = [];
230: |
231: | $data['stores'][] = [
232: | 'store_id' => 0,
233: | 'name' => $this->language->get('text_default')
234: | ];
235: |
236: | $this->load->model('setting/store');
237: |
238: | $stores = $this->model_setting_store->getStores();
239: |
240: | foreach ($stores as $store) {
241: | $data['stores'][] = [
242: | 'store_id' => $store['store_id'],
243: | 'name' => $store['name']
244: | ];
245: | }
246: |
247: | if (isset($this->request->get['manufacturer_id'])) {
248: | $data['manufacturer_store'] = $this->model_catalog_manufacturer->getStores($this->request->get['manufacturer_id']);
249: | } else {
250: | $data['manufacturer_store'] = [0];
251: | }
252: |
253: | if (!empty($manufacturer_info)) {
254: | $data['image'] = $manufacturer_info['image'];
255: | } else {
256: | $data['image'] = '';
257: | }
258: |
259: | $this->load->model('tool/image');
260: |
261: | $data['placeholder'] = $this->model_tool_image->resize('no_image.png', $this->config->get('config_image_default_width'), $this->config->get('config_image_default_height'));
262: |
263: | if ($data['image'] && is_file(DIR_IMAGE . html_entity_decode($data['image'], ENT_QUOTES, 'UTF-8'))) {
264: | $data['thumb'] = $this->model_tool_image->resize($data['image'], $this->config->get('config_image_default_width'), $this->config->get('config_image_default_height'));
265: | } else {
266: | $data['thumb'] = $data['placeholder'];
267: | }
268: |
269: | if (!empty($manufacturer_info)) {
270: | $data['sort_order'] = $manufacturer_info['sort_order'];
271: | } else {
272: | $data['sort_order'] = '';
273: | }
274: |
275: | $this->load->model('localisation/language');
276: |
277: | $data['languages'] = $this->model_localisation_language->getLanguages();
278: |
279: | if (isset($this->request->get['manufacturer_id'])) {
280: | $this->load->model('design/seo_url');
281: |
282: | $data['manufacturer_seo_url'] = $this->model_design_seo_url->getSeoUrlsByKeyValue('manufacturer_id', $this->request->get['manufacturer_id']);
283: | } else {
284: | $data['manufacturer_seo_url'] = [];
285: | }
286: |
287: | $this->load->model('design/layout');
288: |
289: | $data['layouts'] = $this->model_design_layout->getLayouts();
290: |
291: | if (isset($this->request->get['manufacturer_id'])) {
292: | $data['manufacturer_layout'] = $this->model_catalog_manufacturer->getLayouts($this->request->get['manufacturer_id']);
293: | } else {
294: | $data['manufacturer_layout'] = [];
295: | }
296: |
297: | $data['user_token'] = $this->session->data['user_token'];
298: |
299: | $data['header'] = $this->load->controller('common/header');
300: | $data['column_left'] = $this->load->controller('common/column_left');
301: | $data['footer'] = $this->load->controller('common/footer');
302: |
303: | $this->response->setOutput($this->load->view('catalog/manufacturer_form', $data));
304: | }
305: |
306: | /**
307: | * @return void
308: | */
309: | public function save(): void {
310: | $this->load->language('catalog/manufacturer');
311: |
312: | $json = [];
313: |
314: | if (!$this->user->hasPermission('modify', 'catalog/manufacturer')) {
315: | $json['error']['warning'] = $this->language->get('error_permission');
316: | }
317: |
318: | if (!oc_validate_length($this->request->post['name'], 1, 64)) {
319: | $json['error']['name'] = $this->language->get('error_name');
320: | }
321: |
322: | if ($this->request->post['manufacturer_seo_url']) {
323: | $this->load->model('design/seo_url');
324: |
325: | foreach ($this->request->post['manufacturer_seo_url'] as $store_id => $language) {
326: | foreach ($language as $language_id => $keyword) {
327: | if (!oc_validate_length($keyword, 1, 64)) {
328: | $json['error']['keyword_' . $store_id . '_' . $language_id] = $this->language->get('error_keyword');
329: | }
330: |
331: | if (!oc_validate_seo_url($keyword)) {
332: | $json['error']['keyword_' . $store_id . '_' . $language_id] = $this->language->get('error_keyword_character');
333: | }
334: |
335: | $seo_url_info = $this->model_design_seo_url->getSeoUrlByKeyword($keyword, $store_id);
336: |
337: | if ($seo_url_info && ($seo_url_info['key'] != 'manufacturer_id' || !isset($this->request->post['manufacturer_id']) || $seo_url_info['value'] != (int)$this->request->post['manufacturer_id'])) {
338: | $json['error']['keyword_' . $store_id . '_' . $language_id] = $this->language->get('error_keyword_exists');
339: | }
340: | }
341: | }
342: | }
343: |
344: | if (isset($json['error']) && !isset($json['error']['warning'])) {
345: | $json['error']['warning'] = $this->language->get('error_warning');
346: | }
347: |
348: | if (!$json) {
349: | $this->load->model('catalog/manufacturer');
350: |
351: | if (!$this->request->post['manufacturer_id']) {
352: | $json['manufacturer_id'] = $this->model_catalog_manufacturer->addManufacturer($this->request->post);
353: | } else {
354: | $this->model_catalog_manufacturer->editManufacturer($this->request->post['manufacturer_id'], $this->request->post);
355: | }
356: |
357: | $json['success'] = $this->language->get('text_success');
358: | }
359: |
360: | $this->response->addHeader('Content-Type: application/json');
361: | $this->response->setOutput(json_encode($json));
362: | }
363: |
364: | /**
365: | * Delete
366: | *
367: | * @return void
368: | */
369: | public function delete(): void {
370: | $this->load->language('catalog/manufacturer');
371: |
372: | $json = [];
373: |
374: | if (isset($this->request->post['selected'])) {
375: | $selected = $this->request->post['selected'];
376: | } else {
377: | $selected = [];
378: | }
379: |
380: | if (!$this->user->hasPermission('modify', 'catalog/manufacturer')) {
381: | $json['error'] = $this->language->get('error_permission');
382: | }
383: |
384: | $this->load->model('catalog/product');
385: |
386: | foreach ($selected as $manufacturer_id) {
387: | $product_total = $this->model_catalog_product->getTotalProductsByManufacturerId($manufacturer_id);
388: |
389: | if ($product_total) {
390: | $json['error'] = sprintf($this->language->get('error_product'), $product_total);
391: | }
392: | }
393: |
394: | if (!$json) {
395: | $this->load->model('catalog/manufacturer');
396: |
397: | foreach ($selected as $manufacturer_id) {
398: | $this->model_catalog_manufacturer->deleteManufacturer($manufacturer_id);
399: | }
400: |
401: | $json['success'] = $this->language->get('text_success');
402: | }
403: |
404: | $this->response->addHeader('Content-Type: application/json');
405: | $this->response->setOutput(json_encode($json));
406: | }
407: |
408: | /**
409: | * Autocomplete
410: | *
411: | * @return void
412: | */
413: | public function autocomplete(): void {
414: | $json = [];
415: |
416: | if (isset($this->request->get['filter_name'])) {
417: | $this->load->model('catalog/manufacturer');
418: |
419: | $filter_data = [
420: | 'filter_name' => $this->request->get['filter_name'],
421: | 'start' => 0,
422: | 'limit' => 5
423: | ];
424: |
425: | $results = $this->model_catalog_manufacturer->getManufacturers($filter_data);
426: |
427: | foreach ($results as $result) {
428: | $json[] = [
429: | 'manufacturer_id' => $result['manufacturer_id'],
430: | 'name' => strip_tags(html_entity_decode($result['name'], ENT_QUOTES, 'UTF-8'))
431: | ];
432: | }
433: | }
434: |
435: | $sort_order = [];
436: |
437: | foreach ($json as $key => $value) {
438: | $sort_order[$key] = $value['name'];
439: | }
440: |
441: | array_multisort($sort_order, SORT_ASC, $json);
442: |
443: | $this->response->addHeader('Content-Type: application/json');
444: | $this->response->setOutput(json_encode($json));
445: | }
446: | }
447: |

OpenCart API API documentation generated by ApiGen dev-master