1: <?php
2: namespace Opencart\Admin\Controller\Customer;
3: /**
4: * Class Customer Group
5: *
6: * @package Opencart\Admin\Controller\Customer
7: */
8: class CustomerGroup extends \Opencart\System\Engine\Controller {
9: /**
10: * Index
11: *
12: * @return void
13: */
14: public function index(): void {
15: $this->load->language('customer/customer_group');
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/customer_group', 'user_token=' . $this->session->data['user_token'] . $url)
43: ];
44:
45: $data['add'] = $this->url->link('customer/customer_group.form', 'user_token=' . $this->session->data['user_token'] . $url);
46: $data['delete'] = $this->url->link('customer/customer_group.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/customer_group', $data));
57: }
58:
59: /**
60: * List
61: *
62: * @return void
63: */
64: public function list(): void {
65: $this->load->language('customer/customer_group');
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 = 'cgd.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/customer_group.list', 'user_token=' . $this->session->data['user_token'] . $url);
109:
110: $data['customer_groups'] = [];
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/customer_group');
120:
121: $results = $this->model_customer_customer_group->getCustomerGroups($filter_data);
122:
123: foreach ($results as $result) {
124: $data['customer_groups'][] = [
125: 'customer_group_id' => $result['customer_group_id'],
126: 'name' => $result['name'] . (($result['customer_group_id'] == $this->config->get('config_customer_group_id')) ? $this->language->get('text_default') : ''),
127: 'sort_order' => $result['sort_order'],
128: 'edit' => $this->url->link('customer/customer_group.form', 'user_token=' . $this->session->data['user_token'] . '&customer_group_id=' . $result['customer_group_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('customer/customer_group.list', 'user_token=' . $this->session->data['user_token'] . '&sort=cgd.name' . $url);
141: $data['sort_sort_order'] = $this->url->link('customer/customer_group.list', 'user_token=' . $this->session->data['user_token'] . '&sort=cg.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: $customer_group_total = $this->model_customer_customer_group->getTotalCustomerGroups();
154:
155: $data['pagination'] = $this->load->controller('common/pagination', [
156: 'total' => $customer_group_total,
157: 'page' => $page,
158: 'limit' => $this->config->get('config_pagination_admin'),
159: 'url' => $this->url->link('customer/customer_group.list', 'user_token=' . $this->session->data['user_token'] . $url . '&page={page}')
160: ]);
161:
162: $data['results'] = sprintf($this->language->get('text_pagination'), ($customer_group_total) ? (($page - 1) * $this->config->get('config_pagination_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_pagination_admin')) > ($customer_group_total - $this->config->get('config_pagination_admin'))) ? $customer_group_total : ((($page - 1) * $this->config->get('config_pagination_admin')) + $this->config->get('config_pagination_admin')), $customer_group_total, ceil($customer_group_total / $this->config->get('config_pagination_admin')));
163:
164: $data['sort'] = $sort;
165: $data['order'] = $order;
166:
167: return $this->load->view('customer/customer_group_list', $data);
168: }
169:
170: /**
171: * Form
172: *
173: * @return void
174: */
175: public function form(): void {
176: $this->load->language('customer/customer_group');
177:
178: $this->document->setTitle($this->language->get('heading_title'));
179:
180: $data['text_form'] = !isset($this->request->get['customer_group_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('customer/customer_group', 'user_token=' . $this->session->data['user_token'] . $url)
206: ];
207:
208: $data['save'] = $this->url->link('customer/customer_group.save', 'user_token=' . $this->session->data['user_token']);
209: $data['back'] = $this->url->link('customer/customer_group', 'user_token=' . $this->session->data['user_token'] . $url);
210:
211: if (isset($this->request->get['customer_group_id'])) {
212: $this->load->model('customer/customer_group');
213:
214: $customer_group_info = $this->model_customer_customer_group->getCustomerGroup($this->request->get['customer_group_id']);
215: }
216:
217: if (isset($this->request->get['customer_group_id'])) {
218: $data['customer_group_id'] = (int)$this->request->get['customer_group_id'];
219: } else {
220: $data['customer_group_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['customer_group_id'])) {
228: $data['customer_group_description'] = $this->model_customer_customer_group->getDescriptions($this->request->get['customer_group_id']);
229: } else {
230: $data['customer_group_description'] = [];
231: }
232:
233: if (!empty($customer_group_info)) {
234: $data['approval'] = $customer_group_info['approval'];
235: } else {
236: $data['approval'] = '';
237: }
238:
239: if (!empty($customer_group_info)) {
240: $data['sort_order'] = $customer_group_info['sort_order'];
241: } else {
242: $data['sort_order'] = '';
243: }
244:
245: $data['header'] = $this->load->controller('common/header');
246: $data['column_left'] = $this->load->controller('common/column_left');
247: $data['footer'] = $this->load->controller('common/footer');
248:
249: $this->response->setOutput($this->load->view('customer/customer_group_form', $data));
250: }
251:
252: /**
253: * Save
254: *
255: * @return void
256: */
257: public function save(): void {
258: $this->load->language('customer/customer_group');
259:
260: $json = [];
261:
262: if (!$this->user->hasPermission('modify', 'customer/customer_group')) {
263: $json['error']['warning'] = $this->language->get('error_permission');
264: }
265:
266: foreach ($this->request->post['customer_group_description'] as $language_id => $value) {
267: if ((oc_strlen($value['name']) < 3) || (oc_strlen($value['name']) > 32)) {
268: $json['error']['name_' . $language_id] = $this->language->get('error_name');
269: }
270: }
271:
272: if (!$json) {
273: $this->load->model('customer/customer_group');
274:
275: if (!$this->request->post['customer_group_id']) {
276: $json['customer_group_id'] = $this->model_customer_customer_group->addCustomerGroup($this->request->post);
277: } else {
278: $this->model_customer_customer_group->editCustomerGroup($this->request->post['customer_group_id'], $this->request->post);
279: }
280:
281: $json['success'] = $this->language->get('text_success');
282: }
283:
284: $this->response->addHeader('Content-Type: application/json');
285: $this->response->setOutput(json_encode($json));
286: }
287:
288: /**
289: * Delete
290: *
291: * @return void
292: */
293: public function delete(): void {
294: $this->load->language('customer/customer_group');
295:
296: $json = [];
297:
298: if (isset($this->request->post['selected'])) {
299: $selected = $this->request->post['selected'];
300: } else {
301: $selected = [];
302: }
303:
304: if (!$this->user->hasPermission('modify', 'customer/customer_group')) {
305: $json['error'] = $this->language->get('error_permission');
306: }
307:
308: $this->load->model('setting/store');
309: $this->load->model('customer/customer');
310:
311: foreach ($selected as $customer_group_id) {
312: if ($this->config->get('config_customer_group_id') == $customer_group_id) {
313: $json['error'] = $this->language->get('error_default');
314: }
315:
316: $store_total = $this->model_setting_store->getTotalStoresByCustomerGroupId($customer_group_id);
317:
318: if ($store_total) {
319: $json['error'] = sprintf($this->language->get('error_store'), $store_total);
320: }
321:
322: $customer_total = $this->model_customer_customer->getTotalCustomersByCustomerGroupId($customer_group_id);
323:
324: if ($customer_total) {
325: $json['error'] = sprintf($this->language->get('error_customer'), $customer_total);
326: }
327: }
328:
329: if (!$json) {
330: $this->load->model('customer/customer_group');
331:
332: foreach ($selected as $customer_group_id) {
333: $this->model_customer_customer_group->deleteCustomerGroup($customer_group_id);
334: }
335:
336: $json['success'] = $this->language->get('text_success');
337: }
338:
339: $this->response->addHeader('Content-Type: application/json');
340: $this->response->setOutput(json_encode($json));
341: }
342: }
343: