1: <?php
2: namespace Opencart\Admin\Controller\Localisation;
3: /**
4: * Class Length Class
5: *
6: * @package Opencart\Admin\Controller\Localisation
7: */
8: class LengthClass extends \Opencart\System\Engine\Controller {
9: /**
10: * Index
11: *
12: * @return void
13: */
14: public function index(): void {
15: $this->load->language('localisation/length_class');
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('localisation/length_class', 'user_token=' . $this->session->data['user_token'] . $url)
43: ];
44:
45: $data['add'] = $this->url->link('localisation/length_class.form', 'user_token=' . $this->session->data['user_token'] . $url);
46: $data['delete'] = $this->url->link('localisation/length_class.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('localisation/length_class', $data));
57: }
58:
59: /**
60: * List
61: *
62: * @return void
63: */
64: public function list(): void {
65: $this->load->language('localisation/length_class');
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 = 'title';
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('localisation/length_class.list', 'user_token=' . $this->session->data['user_token'] . $url);
109:
110: $data['length_classes'] = [];
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('localisation/length_class');
120:
121: $results = $this->model_localisation_length_class->getLengthClasses($filter_data);
122:
123: foreach ($results as $result) {
124: $data['length_classes'][] = [
125: 'length_class_id' => $result['length_class_id'],
126: 'title' => $result['title'] . (($result['length_class_id'] == $this->config->get('config_length_class_id')) ? $this->language->get('text_default') : ''),
127: 'unit' => $result['unit'],
128: 'value' => $result['value'],
129: 'edit' => $this->url->link('localisation/length_class.form', 'user_token=' . $this->session->data['user_token'] . '&length_class_id=' . $result['length_class_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_title'] = $this->url->link('localisation/length_class.list', 'user_token=' . $this->session->data['user_token'] . '&sort=title' . $url);
142: $data['sort_unit'] = $this->url->link('localisation/length_class.list', 'user_token=' . $this->session->data['user_token'] . '&sort=unit' . $url);
143: $data['sort_value'] = $this->url->link('localisation/length_class.list', 'user_token=' . $this->session->data['user_token'] . '&sort=value' . $url);
144:
145: $url = '';
146:
147: if (isset($this->request->get['sort'])) {
148: $url .= '&sort=' . $this->request->get['sort'];
149: }
150:
151: if (isset($this->request->get['order'])) {
152: $url .= '&order=' . $this->request->get['order'];
153: }
154:
155: $length_class_total = $this->model_localisation_length_class->getTotalLengthClasses();
156:
157: $data['pagination'] = $this->load->controller('common/pagination', [
158: 'total' => $length_class_total,
159: 'page' => $page,
160: 'limit' => $this->config->get('config_pagination_admin'),
161: 'url' => $this->url->link('localisation/length_class.list', 'user_token=' . $this->session->data['user_token'] . $url . '&page={page}')
162: ]);
163:
164: $data['results'] = sprintf($this->language->get('text_pagination'), ($length_class_total) ? (($page - 1) * $this->config->get('config_pagination_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_pagination_admin')) > ($length_class_total - $this->config->get('config_pagination_admin'))) ? $length_class_total : ((($page - 1) * $this->config->get('config_pagination_admin')) + $this->config->get('config_pagination_admin')), $length_class_total, ceil($length_class_total / $this->config->get('config_pagination_admin')));
165:
166: $data['sort'] = $sort;
167: $data['order'] = $order;
168:
169: return $this->load->view('localisation/length_class_list', $data);
170: }
171:
172: /**
173: * @return void
174: */
175: public function form(): void {
176: $this->load->language('localisation/length_class');
177:
178: $this->document->setTitle($this->language->get('heading_title'));
179:
180: $data['text_form'] = !isset($this->request->get['length_class_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('localisation/length_class', 'user_token=' . $this->session->data['user_token'] . $url)
206: ];
207:
208: $data['save'] = $this->url->link('localisation/length_class.save', 'user_token=' . $this->session->data['user_token']);
209: $data['back'] = $this->url->link('localisation/length_class', 'user_token=' . $this->session->data['user_token'] . $url);
210:
211: if (isset($this->request->get['length_class_id'])) {
212: $this->load->model('localisation/length_class');
213:
214: $length_class_info = $this->model_localisation_length_class->getLengthClass($this->request->get['length_class_id']);
215: }
216:
217: if (isset($this->request->get['length_class_id'])) {
218: $data['length_class_id'] = (int)$this->request->get['length_class_id'];
219: } else {
220: $data['length_class_id'] = 0;
221: }
222:
223: $this->load->model('localisation/language');
224:
225: $data['languages'] = $this->model_localisation_language->getLanguages();
226:
227: if (!empty($length_class_info)) {
228: $data['length_class_description'] = $this->model_localisation_length_class->getDescriptions($this->request->get['length_class_id']);
229: } else {
230: $data['length_class_description'] = [];
231: }
232:
233: if (!empty($length_class_info)) {
234: $data['value'] = $length_class_info['value'];
235: } else {
236: $data['value'] = '';
237: }
238:
239: $data['header'] = $this->load->controller('common/header');
240: $data['column_left'] = $this->load->controller('common/column_left');
241: $data['footer'] = $this->load->controller('common/footer');
242:
243: $this->response->setOutput($this->load->view('localisation/length_class_form', $data));
244: }
245:
246: /**
247: * Save
248: *
249: * @return void
250: */
251: public function save(): void {
252: $this->load->language('localisation/length_class');
253:
254: $json = [];
255:
256: if (!$this->user->hasPermission('modify', 'localisation/length_class')) {
257: $json['error']['warning'] = $this->language->get('error_permission');
258: }
259:
260: foreach ($this->request->post['length_class_description'] as $language_id => $value) {
261: if ((oc_strlen($value['title']) < 3) || (oc_strlen($value['title']) > 32)) {
262: $json['error']['title_' . $language_id] = $this->language->get('error_title');
263: }
264:
265: if (!$value['unit'] || (oc_strlen($value['unit']) > 4)) {
266: $json['error']['unit_' . $language_id] = $this->language->get('error_unit');
267: }
268: }
269:
270: if (!$json) {
271: $this->load->model('localisation/length_class');
272:
273: if (!$this->request->post['length_class_id']) {
274: $json['length_class_id'] = $this->model_localisation_length_class->addLengthClass($this->request->post);
275: } else {
276: $this->model_localisation_length_class->editLengthClass($this->request->post['length_class_id'], $this->request->post);
277: }
278:
279: $json['success'] = $this->language->get('text_success');
280: }
281:
282: $this->response->addHeader('Content-Type: application/json');
283: $this->response->setOutput(json_encode($json));
284: }
285:
286: /**
287: * Delete
288: *
289: * @return void
290: */
291: public function delete(): void {
292: $this->load->language('localisation/length_class');
293:
294: $json = [];
295:
296: if (isset($this->request->post['selected'])) {
297: $selected = $this->request->post['selected'];
298: } else {
299: $selected = [];
300: }
301:
302: if (!$this->user->hasPermission('modify', 'localisation/length_class')) {
303: $json['error'] = $this->language->get('error_permission');
304: }
305:
306: $this->load->model('catalog/product');
307:
308: foreach ($selected as $length_class_id) {
309: if ($this->config->get('config_length_class_id') == $length_class_id) {
310: $json['error'] = $this->language->get('error_default');
311: }
312:
313: $product_total = $this->model_catalog_product->getTotalProductsByLengthClassId($length_class_id);
314:
315: if ($product_total) {
316: $json['error'] = sprintf($this->language->get('error_product'), $product_total);
317: }
318: }
319:
320: if (!$json) {
321: $this->load->model('localisation/length_class');
322:
323: foreach ($selected as $length_class_id) {
324: $this->model_localisation_length_class->deleteLengthClass($length_class_id);
325: }
326:
327: $json['success'] = $this->language->get('text_success');
328: }
329:
330: $this->response->addHeader('Content-Type: application/json');
331: $this->response->setOutput(json_encode($json));
332: }
333: }
334: