1: <?php
2: namespace Opencart\Admin\Controller\Customer;
3: /**
4: * Class Custom Field
5: *
6: * @package Opencart\Admin\Controller\Customer
7: */
8: class CustomField extends \Opencart\System\Engine\Controller {
9: /**
10: * Index
11: *
12: * @return void
13: */
14: public function index(): void {
15: $this->load->language('customer/custom_field');
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('customer/custom_field', 'user_token=' . $this->session->data['user_token'] . $url)
43: ];
44:
45: $data['add'] = $this->url->link('customer/custom_field.form', 'user_token=' . $this->session->data['user_token'] . $url);
46: $data['delete'] = $this->url->link('customer/custom_field.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('customer/custom_field', $data));
57: }
58:
59: /**
60: * List
61: *
62: * @return void
63: */
64: public function list(): void {
65: $this->load->language('customer/custom_field');
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 = 'cfd.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('customer/custom_field.list', 'user_token=' . $this->session->data['user_token'] . $url);
109:
110: $data['custom_fields'] = [];
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('customer/custom_field');
120:
121: $results = $this->model_customer_custom_field->getCustomFields($filter_data);
122:
123: foreach ($results as $result) {
124: $type = '';
125:
126: switch ($result['type']) {
127: case 'select':
128: $type = $this->language->get('text_select');
129: break;
130: case 'radio':
131: $type = $this->language->get('text_radio');
132: break;
133: case 'checkbox':
134: $type = $this->language->get('text_checkbox');
135: break;
136: case 'input':
137: $type = $this->language->get('text_input');
138: break;
139: case 'text':
140: $type = $this->language->get('text_text');
141: break;
142: case 'textarea':
143: $type = $this->language->get('text_textarea');
144: break;
145: case 'file':
146: $type = $this->language->get('text_file');
147: break;
148: case 'date':
149: $type = $this->language->get('text_date');
150: break;
151: case 'datetime':
152: $type = $this->language->get('text_datetime');
153: break;
154: case 'time':
155: $type = $this->language->get('text_time');
156: break;
157: }
158:
159: $data['custom_fields'][] = [
160: 'custom_field_id' => $result['custom_field_id'],
161: 'name' => $result['name'],
162: 'location' => $this->language->get('text_' . $result['location']),
163: 'type' => $type,
164: 'status' => $result['status'],
165: 'sort_order' => $result['sort_order'],
166: 'edit' => $this->url->link('customer/custom_field.form', 'user_token=' . $this->session->data['user_token'] . '&custom_field_id=' . $result['custom_field_id'] . $url)
167: ];
168: }
169:
170: $url = '';
171:
172: if ($order == 'ASC') {
173: $url .= '&order=DESC';
174: } else {
175: $url .= '&order=ASC';
176: }
177:
178: $data['sort_name'] = $this->url->link('customer/custom_field.list', 'user_token=' . $this->session->data['user_token'] . '&sort=cfd.name' . $url);
179: $data['sort_location'] = $this->url->link('customer/custom_field.list', 'user_token=' . $this->session->data['user_token'] . '&sort=cf.location' . $url);
180: $data['sort_type'] = $this->url->link('customer/custom_field.list', 'user_token=' . $this->session->data['user_token'] . '&sort=cf.type' . $url);
181: $data['sort_status'] = $this->url->link('customer/custom_field.list', 'user_token=' . $this->session->data['user_token'] . '&sort=cf.status' . $url);
182: $data['sort_sort_order'] = $this->url->link('customer/custom_field.list', 'user_token=' . $this->session->data['user_token'] . '&sort=cf.sort_order' . $url);
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: $custom_field_total = $this->model_customer_custom_field->getTotalCustomFields();
195:
196: $data['pagination'] = $this->load->controller('common/pagination', [
197: 'total' => $custom_field_total,
198: 'page' => $page,
199: 'limit' => $this->config->get('config_pagination_admin'),
200: 'url' => $this->url->link('customer/custom_field.list', 'user_token=' . $this->session->data['user_token'] . $url . '&page={page}')
201: ]);
202:
203: $data['results'] = sprintf($this->language->get('text_pagination'), ($custom_field_total) ? (($page - 1) * $this->config->get('config_pagination_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_pagination_admin')) > ($custom_field_total - $this->config->get('config_pagination_admin'))) ? $custom_field_total : ((($page - 1) * $this->config->get('config_pagination_admin')) + $this->config->get('config_pagination_admin')), $custom_field_total, ceil($custom_field_total / $this->config->get('config_pagination_admin')));
204:
205: $data['sort'] = $sort;
206: $data['order'] = $order;
207:
208: return $this->load->view('customer/custom_field_list', $data);
209: }
210:
211: /**
212: * Form
213: *
214: * @return void
215: */
216: public function form(): void {
217: $this->load->language('customer/custom_field');
218:
219: $this->document->setTitle($this->language->get('heading_title'));
220:
221: $data['text_form'] = !isset($this->request->get['custom_field_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
222:
223: $url = '';
224:
225: if (isset($this->request->get['sort'])) {
226: $url .= '&sort=' . $this->request->get['sort'];
227: }
228:
229: if (isset($this->request->get['order'])) {
230: $url .= '&order=' . $this->request->get['order'];
231: }
232:
233: if (isset($this->request->get['page'])) {
234: $url .= '&page=' . $this->request->get['page'];
235: }
236:
237: $data['breadcrumbs'] = [];
238:
239: $data['breadcrumbs'][] = [
240: 'text' => $this->language->get('text_home'),
241: 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
242: ];
243:
244: $data['breadcrumbs'][] = [
245: 'text' => $this->language->get('heading_title'),
246: 'href' => $this->url->link('customer/custom_field', 'user_token=' . $this->session->data['user_token'] . $url)
247: ];
248:
249: $data['save'] = $this->url->link('customer/custom_field.save', 'user_token=' . $this->session->data['user_token']);
250: $data['back'] = $this->url->link('customer/custom_field', 'user_token=' . $this->session->data['user_token'] . $url);
251:
252: if (isset($this->request->get['custom_field_id'])) {
253: $this->load->model('customer/custom_field');
254:
255: $custom_field_info = $this->model_customer_custom_field->getCustomField($this->request->get['custom_field_id']);
256: }
257:
258: if (isset($this->request->get['custom_field_id'])) {
259: $data['custom_field_id'] = (int)$this->request->get['custom_field_id'];
260: } else {
261: $data['custom_field_id'] = 0;
262: }
263:
264: $this->load->model('localisation/language');
265:
266: $data['languages'] = $this->model_localisation_language->getLanguages();
267:
268: if (isset($this->request->get['custom_field_id'])) {
269: $data['custom_field_description'] = $this->model_customer_custom_field->getDescriptions($this->request->get['custom_field_id']);
270: } else {
271: $data['custom_field_description'] = [];
272: }
273:
274: if (!empty($custom_field_info)) {
275: $data['location'] = $custom_field_info['location'];
276: } else {
277: $data['location'] = '';
278: }
279:
280: if (!empty($custom_field_info)) {
281: $data['type'] = $custom_field_info['type'];
282: } else {
283: $data['type'] = '';
284: }
285:
286: if (!empty($custom_field_info)) {
287: $data['value'] = $custom_field_info['value'];
288: } else {
289: $data['value'] = '';
290: }
291:
292: if (!empty($custom_field_info)) {
293: $data['validation'] = $custom_field_info['validation'];
294: } else {
295: $data['validation'] = '';
296: }
297:
298: if (!empty($custom_field_info)) {
299: $data['status'] = $custom_field_info['status'];
300: } else {
301: $data['status'] = '';
302: }
303:
304: if (!empty($custom_field_info)) {
305: $data['sort_order'] = $custom_field_info['sort_order'];
306: } else {
307: $data['sort_order'] = '';
308: }
309:
310: if (isset($this->request->get['custom_field_id'])) {
311: $custom_field_values = $this->model_customer_custom_field->getValueDescriptions($this->request->get['custom_field_id']);
312: } else {
313: $custom_field_values = [];
314: }
315:
316: $data['custom_field_values'] = [];
317:
318: foreach ($custom_field_values as $custom_field_value) {
319: $data['custom_field_values'][] = [
320: 'custom_field_value_id' => $custom_field_value['custom_field_value_id'],
321: 'custom_field_value_description' => $custom_field_value['custom_field_value_description'],
322: 'sort_order' => $custom_field_value['sort_order']
323: ];
324: }
325:
326: if (isset($this->request->get['custom_field_id'])) {
327: $custom_field_customer_groups = $this->model_customer_custom_field->getCustomerGroups($this->request->get['custom_field_id']);
328: } else {
329: $custom_field_customer_groups = [];
330: }
331:
332: $data['custom_field_customer_group'] = [];
333:
334: foreach ($custom_field_customer_groups as $custom_field_customer_group) {
335: if (isset($custom_field_customer_group['customer_group_id'])) {
336: $data['custom_field_customer_group'][] = $custom_field_customer_group['customer_group_id'];
337: }
338: }
339:
340: $this->load->model('customer/customer_group');
341:
342: $data['customer_groups'] = $this->model_customer_customer_group->getCustomerGroups();
343:
344: $data['custom_field_required'] = [];
345:
346: foreach ($custom_field_customer_groups as $custom_field_customer_group) {
347: if (isset($custom_field_customer_group['required']) && $custom_field_customer_group['required'] && isset($custom_field_customer_group['customer_group_id'])) {
348: $data['custom_field_required'][] = $custom_field_customer_group['customer_group_id'];
349: }
350: }
351:
352: $data['user_token'] = $this->session->data['user_token'];
353:
354: $data['header'] = $this->load->controller('common/header');
355: $data['column_left'] = $this->load->controller('common/column_left');
356: $data['footer'] = $this->load->controller('common/footer');
357:
358: $this->response->setOutput($this->load->view('customer/custom_field_form', $data));
359: }
360:
361: /**
362: * Save
363: *
364: * @return void
365: */
366: public function save(): void {
367: $this->load->language('customer/custom_field');
368:
369: $json = [];
370:
371: if (!$this->user->hasPermission('modify', 'customer/custom_field')) {
372: $json['error']['warning'] = $this->language->get('error_permission');
373: }
374:
375: foreach ($this->request->post['custom_field_description'] as $language_id => $value) {
376: if (!oc_validate_length($value['name'], 1, 128)) {
377: $json['error']['name_' . $language_id] = $this->language->get('error_name');
378: }
379: }
380:
381: if (($this->request->post['type'] == 'select' || $this->request->post['type'] == 'radio' || $this->request->post['type'] == 'checkbox')) {
382: if (!isset($this->request->post['custom_field_value'])) {
383: $json['error']['warning'] = $this->language->get('error_type');
384: }
385:
386: if (isset($this->request->post['custom_field_value'])) {
387: foreach ($this->request->post['custom_field_value'] as $custom_field_value_id => $custom_field_value) {
388: foreach ($custom_field_value['custom_field_value_description'] as $language_id => $custom_field_value_description) {
389: if (!oc_validate_length($custom_field_value_description['name'], 1, 128)) {
390: $json['error']['custom_field_value_' . $custom_field_value_id . '_' . $language_id] = $this->language->get('error_custom_value');
391: }
392: }
393: }
394: }
395: }
396:
397: if ($this->request->post['type'] == 'text' && $this->request->post['validation'] && @preg_match(html_entity_decode($this->request->post['validation'], ENT_QUOTES, 'UTF-8'), '') === false) {
398: $json['error']['validation'] = $this->language->get('error_validation');
399: }
400:
401: if (!$json) {
402: $this->load->model('customer/custom_field');
403:
404: if (!$this->request->post['custom_field_id']) {
405: $json['custom_field_id'] = $this->model_customer_custom_field->addCustomField($this->request->post);
406: } else {
407: $this->model_customer_custom_field->editCustomField($this->request->post['custom_field_id'], $this->request->post);
408: }
409:
410: $json['success'] = $this->language->get('text_success');
411: }
412:
413: $this->response->addHeader('Content-Type: application/json');
414: $this->response->setOutput(json_encode($json));
415: }
416:
417: /**
418: * Delete
419: *
420: * @return void
421: */
422: public function delete(): void {
423: $this->load->language('customer/custom_field');
424:
425: $json = [];
426:
427: if (isset($this->request->post['selected'])) {
428: $selected = $this->request->post['selected'];
429: } else {
430: $selected = [];
431: }
432:
433: if (!$this->user->hasPermission('modify', 'customer/custom_field')) {
434: $json['error'] = $this->language->get('error_permission');
435: }
436:
437: if (!$json) {
438: $this->load->model('customer/custom_field');
439:
440: foreach ($selected as $custom_field_id) {
441: $this->model_customer_custom_field->deleteCustomField($custom_field_id);
442: }
443:
444: $json['success'] = $this->language->get('text_success');
445: }
446:
447: $this->response->addHeader('Content-Type: application/json');
448: $this->response->setOutput(json_encode($json));
449: }
450: }
451: