1: <?php
2: namespace Opencart\Admin\Controller\Localisation;
3: /**
4: * Class Weight Class
5: *
6: * @package Opencart\Admin\Controller\Localisation
7: */
8: class WeightClass extends \Opencart\System\Engine\Controller {
9: /**
10: * Index
11: *
12: * @return void
13: */
14: public function index(): void {
15: $this->load->language('localisation/weight_class');
16:
17: $url = '';
18:
19: if (isset($this->request->get['sort'])) {
20: $url .= '&sort=' . $this->request->get['sort'];
21: }
22:
23: if (isset($this->request->get['order'])) {
24: $url .= '&order=' . $this->request->get['order'];
25: }
26:
27: if (isset($this->request->get['page'])) {
28: $url .= '&page=' . $this->request->get['page'];
29: }
30:
31: $data['breadcrumbs'] = [];
32:
33: $data['breadcrumbs'][] = [
34: 'text' => $this->language->get('text_home'),
35: 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
36: ];
37:
38: $data['breadcrumbs'][] = [
39: 'text' => $this->language->get('heading_title'),
40: 'href' => $this->url->link('localisation/weight_class', 'user_token=' . $this->session->data['user_token'] . $url)
41: ];
42:
43: $data['add'] = $this->url->link('localisation/weight_class.form', 'user_token=' . $this->session->data['user_token'] . $url);
44: $data['delete'] = $this->url->link('localisation/weight_class.delete', 'user_token=' . $this->session->data['user_token']);
45:
46: $data['list'] = $this->getList();
47:
48: $data['user_token'] = $this->session->data['user_token'];
49:
50: $data['header'] = $this->load->controller('common/header');
51: $data['column_left'] = $this->load->controller('common/column_left');
52: $data['footer'] = $this->load->controller('common/footer');
53:
54: $this->response->setOutput($this->load->view('localisation/weight_class', $data));
55: }
56:
57: /**
58: * List
59: *
60: * @return void
61: */
62: public function list(): void {
63: $this->load->language('localisation/weight_class');
64:
65: $this->response->setOutput($this->getList());
66: }
67:
68: /**
69: * Get List
70: *
71: * @return string
72: */
73: protected function getList(): string {
74: if (isset($this->request->get['sort'])) {
75: $sort = (string)$this->request->get['sort'];
76: } else {
77: $sort = 'title';
78: }
79:
80: if (isset($this->request->get['order'])) {
81: $order = (string)$this->request->get['order'];
82: } else {
83: $order = 'ASC';
84: }
85:
86: if (isset($this->request->get['page'])) {
87: $page = (int)$this->request->get['page'];
88: } else {
89: $page = 1;
90: }
91:
92: $url = '';
93:
94: if (isset($this->request->get['sort'])) {
95: $url .= '&sort=' . $this->request->get['sort'];
96: }
97:
98: if (isset($this->request->get['order'])) {
99: $url .= '&order=' . $this->request->get['order'];
100: }
101:
102: if (isset($this->request->get['page'])) {
103: $url .= '&page=' . $this->request->get['page'];
104: }
105:
106: $data['action'] = $this->url->link('localisation/weight_class.list', 'user_token=' . $this->session->data['user_token'] . $url);
107:
108: $data['weight_classes'] = [];
109:
110: $filter_data = [
111: 'sort' => $sort,
112: 'order' => $order,
113: 'start' => ($page - 1) * $this->config->get('config_pagination_admin'),
114: 'limit' => $this->config->get('config_pagination_admin')
115: ];
116:
117: $this->load->model('localisation/weight_class');
118:
119: $results = $this->model_localisation_weight_class->getWeightClasses($filter_data);
120:
121: foreach ($results as $result) {
122: $data['weight_classes'][] = [
123: 'weight_class_id' => $result['weight_class_id'],
124: 'title' => $result['title'] . (($result['weight_class_id'] == $this->config->get('config_weight_class_id')) ? $this->language->get('text_default') : ''),
125: 'unit' => $result['unit'],
126: 'value' => $result['value'],
127: 'edit' => $this->url->link('localisation/weight_class.form', 'user_token=' . $this->session->data['user_token'] . '&weight_class_id=' . $result['weight_class_id'] . $url)
128: ];
129: }
130:
131: $url = '';
132:
133: if ($order == 'ASC') {
134: $url .= '&order=DESC';
135: } else {
136: $url .= '&order=ASC';
137: }
138:
139: $data['sort_title'] = $this->url->link('localisation/weight_class.list', 'user_token=' . $this->session->data['user_token'] . '&sort=title' . $url);
140: $data['sort_unit'] = $this->url->link('localisation/weight_class.list', 'user_token=' . $this->session->data['user_token'] . '&sort=unit' . $url);
141: $data['sort_value'] = $this->url->link('localisation/weight_class.list', 'user_token=' . $this->session->data['user_token'] . '&sort=value' . $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: $weight_class_total = $this->model_localisation_weight_class->getTotalWeightClasses();
154:
155: $data['pagination'] = $this->load->controller('common/pagination', [
156: 'total' => $weight_class_total,
157: 'page' => $page,
158: 'limit' => $this->config->get('config_pagination_admin'),
159: 'url' => $this->url->link('localisation/weight_class.list', 'user_token=' . $this->session->data['user_token'] . $url . '&page={page}')
160: ]);
161:
162: $data['results'] = sprintf($this->language->get('text_pagination'), ($weight_class_total) ? (($page - 1) * $this->config->get('config_pagination_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_pagination_admin')) > ($weight_class_total - $this->config->get('config_pagination_admin'))) ? $weight_class_total : ((($page - 1) * $this->config->get('config_pagination_admin')) + $this->config->get('config_pagination_admin')), $weight_class_total, ceil($weight_class_total / $this->config->get('config_pagination_admin')));
163:
164: $data['sort'] = $sort;
165: $data['order'] = $order;
166:
167: return $this->load->view('localisation/weight_class_list', $data);
168: }
169:
170: /**
171: * Form
172: *
173: * @return void
174: */
175: public function form(): void {
176: $this->load->language('localisation/weight_class');
177:
178: $this->document->setTitle($this->language->get('heading_title'));
179:
180: $data['text_form'] = !isset($this->request->get['weight_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/weight_class', 'user_token=' . $this->session->data['user_token'] . $url)
206: ];
207:
208: $data['save'] = $this->url->link('localisation/weight_class.save', 'user_token=' . $this->session->data['user_token']);
209: $data['back'] = $this->url->link('localisation/weight_class', 'user_token=' . $this->session->data['user_token'] . $url);
210:
211: if (isset($this->request->get['weight_class_id'])) {
212: $this->load->model('localisation/weight_class');
213:
214: $weight_class_info = $this->model_localisation_weight_class->getWeightClass($this->request->get['weight_class_id']);
215: }
216:
217: if (isset($this->request->get['weight_class_id'])) {
218: $data['weight_class_id'] = (int)$this->request->get['weight_class_id'];
219: } else {
220: $data['weight_class_id'] = 0;
221: }
222:
223: $this->load->model('localisation/language');
224:
225: $data['languages'] = $this->model_localisation_language->getLanguages();
226:
227: if (isset($this->request->get['weight_class_id'])) {
228: $data['weight_class_description'] = $this->model_localisation_weight_class->getDescriptions($this->request->get['weight_class_id']);
229: } else {
230: $data['weight_class_description'] = [];
231: }
232:
233: if (!empty($weight_class_info)) {
234: $data['value'] = $weight_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/weight_class_form', $data));
244: }
245:
246: /**
247: * Save
248: *
249: * @return void
250: */
251: public function save(): void {
252: $this->load->language('localisation/weight_class');
253:
254: $json = [];
255:
256: if (!$this->user->hasPermission('modify', 'localisation/weight_class')) {
257: $json['error']['warning'] = $this->language->get('error_permission');
258: }
259:
260: foreach ($this->request->post['weight_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/weight_class');
272:
273: if (!$this->request->post['weight_class_id']) {
274: $json['weight_class_id'] = $this->model_localisation_weight_class->addWeightClass($this->request->post);
275: } else {
276: $this->model_localisation_weight_class->editWeightClass($this->request->post['weight_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/weight_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/weight_class')) {
303: $json['error'] = $this->language->get('error_permission');
304: }
305:
306: $this->load->model('catalog/product');
307:
308: foreach ($selected as $weight_class_id) {
309: if ($this->config->get('config_weight_class_id') == $weight_class_id) {
310: $json['error'] = $this->language->get('error_default');
311: }
312:
313: $product_total = $this->model_catalog_product->getTotalProductsByWeightClassId($weight_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/weight_class');
322:
323: foreach ($selected as $weight_class_id) {
324: $this->model_localisation_weight_class->deleteWeightClass($weight_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: