1: <?php
2: namespace Opencart\Admin\Controller\Customer;
3: /**
4: * Class Customer Approval
5: *
6: * @package Opencart\Admin\Controller\Customer
7: */
8: class CustomerApproval extends \Opencart\System\Engine\Controller {
9: /**
10: * Index
11: *
12: * @return void
13: */
14: public function index(): void {
15: $this->load->language('customer/customer_approval');
16:
17: $this->document->setTitle($this->language->get('heading_title'));
18:
19: $data['breadcrumbs'] = [];
20:
21: $data['breadcrumbs'][] = [
22: 'text' => $this->language->get('text_home'),
23: 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
24: ];
25:
26: $data['breadcrumbs'][] = [
27: 'text' => $this->language->get('heading_title'),
28: 'href' => $this->url->link('customer/customer_approval', 'user_token=' . $this->session->data['user_token'])
29: ];
30:
31: $data['approve'] = $this->url->link('customer/customer_approval.approve', 'user_token=' . $this->session->data['user_token'], true);
32: $data['deny'] = $this->url->link('customer/customer_approval.deny', 'user_token=' . $this->session->data['user_token'], true);
33:
34: $this->load->model('customer/customer_group');
35:
36: $data['customer_groups'] = $this->model_customer_customer_group->getCustomerGroups();
37:
38: $data['list'] = $this->getList();
39:
40: $data['user_token'] = $this->session->data['user_token'];
41:
42: $data['header'] = $this->load->controller('common/header');
43: $data['column_left'] = $this->load->controller('common/column_left');
44: $data['footer'] = $this->load->controller('common/footer');
45:
46: $this->response->setOutput($this->load->view('customer/customer_approval', $data));
47: }
48:
49: /**
50: * List
51: *
52: * @return void
53: */
54: public function list(): void {
55: $this->load->language('customer/customer_approval');
56:
57: $this->response->setOutput($this->getList());
58: }
59:
60: /**
61: * Get List
62: *
63: * @return string
64: */
65: public function getList(): string {
66: if (isset($this->request->get['filter_customer'])) {
67: $filter_customer = $this->request->get['filter_customer'];
68: } else {
69: $filter_customer = '';
70: }
71:
72: if (isset($this->request->get['filter_email'])) {
73: $filter_email = $this->request->get['filter_email'];
74: } else {
75: $filter_email = '';
76: }
77:
78: if (isset($this->request->get['filter_customer_group_id'])) {
79: $filter_customer_group_id = (int)$this->request->get['filter_customer_group_id'];
80: } else {
81: $filter_customer_group_id = '';
82: }
83:
84: if (isset($this->request->get['filter_type'])) {
85: $filter_type = $this->request->get['filter_type'];
86: } else {
87: $filter_type = '';
88: }
89:
90: if (isset($this->request->get['filter_date_from'])) {
91: $filter_date_from = $this->request->get['filter_date_from'];
92: } else {
93: $filter_date_from = '';
94: }
95:
96: if (isset($this->request->get['filter_date_to'])) {
97: $filter_date_to = $this->request->get['filter_date_to'];
98: } else {
99: $filter_date_to = '';
100: }
101:
102: if (isset($this->request->get['page'])) {
103: $page = (int)$this->request->get['page'];
104: } else {
105: $page = 1;
106: }
107:
108: $url = '';
109:
110: if (isset($this->request->get['filter_customer'])) {
111: $url .= '&filter_customer=' . urlencode(html_entity_decode($this->request->get['filter_customer'], ENT_QUOTES, 'UTF-8'));
112: }
113:
114: if (isset($this->request->get['filter_email'])) {
115: $url .= '&filter_email=' . urlencode(html_entity_decode($this->request->get['filter_email'], ENT_QUOTES, 'UTF-8'));
116: }
117:
118: if (isset($this->request->get['filter_customer_group_id'])) {
119: $url .= '&filter_customer_group_id=' . $this->request->get['filter_customer_group_id'];
120: }
121:
122: if (isset($this->request->get['filter_type'])) {
123: $url .= '&filter_type=' . $this->request->get['filter_type'];
124: }
125:
126: if (isset($this->request->get['filter_date_from'])) {
127: $url .= '&filter_date_from=' . $this->request->get['filter_date_from'];
128: }
129:
130: if (isset($this->request->get['filter_date_to'])) {
131: $url .= '&filter_date_to=' . $this->request->get['filter_date_to'];
132: }
133:
134: if (isset($this->request->get['page'])) {
135: $url .= '&page=' . $this->request->get['page'];
136: }
137:
138: $data['action'] = $this->url->link('customer/customer_approval.list', 'user_token=' . $this->session->data['user_token'] . $url, true);
139:
140: $data['customer_approvals'] = [];
141:
142: $filter_data = [
143: 'filter_customer' => $filter_customer,
144: 'filter_email' => $filter_email,
145: 'filter_customer_group_id' => $filter_customer_group_id,
146: 'filter_type' => $filter_type,
147: 'filter_date_from' => $filter_date_from,
148: 'filter_date_to' => $filter_date_to,
149: 'start' => ($page - 1) * $this->config->get('config_pagination_admin'),
150: 'limit' => $this->config->get('config_pagination_admin')
151: ];
152:
153: $this->load->model('customer/customer_approval');
154:
155: $results = $this->model_customer_customer_approval->getCustomerApprovals($filter_data);
156:
157: foreach ($results as $result) {
158: $data['customer_approvals'][] = [
159: 'customer_approval_id' => $result['customer_approval_id'],
160: 'customer_id' => $result['customer_id'],
161: 'customer' => $result['customer'],
162: 'email' => $result['email'],
163: 'customer_group' => $result['customer_group'],
164: 'type' => $this->language->get('text_' . $result['type']),
165: 'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added'])),
166: 'approve' => $this->url->link('customer/customer_approval.approve', 'user_token=' . $this->session->data['user_token'] . '&customer_approval_id=' . $result['customer_approval_id'], true),
167: 'deny' => $this->url->link('customer/customer_approval.deny', 'user_token=' . $this->session->data['user_token'] . '&customer_approval_id=' . $result['customer_approval_id'], true),
168: 'edit' => $this->url->link('customer/customer.form', 'user_token=' . $this->session->data['user_token'] . '&customer_id=' . $result['customer_id'], true)
169: ];
170: }
171:
172: $url = '';
173:
174: if (isset($this->request->get['filter_customer'])) {
175: $url .= '&filter_customer=' . urlencode(html_entity_decode($this->request->get['filter_customer'], ENT_QUOTES, 'UTF-8'));
176: }
177:
178: if (isset($this->request->get['filter_email'])) {
179: $url .= '&filter_email=' . urlencode(html_entity_decode($this->request->get['filter_email'], ENT_QUOTES, 'UTF-8'));
180: }
181:
182: if (isset($this->request->get['filter_customer_group_id'])) {
183: $url .= '&filter_customer_group_id=' . $this->request->get['filter_customer_group_id'];
184: }
185:
186: if (isset($this->request->get['filter_type'])) {
187: $url .= '&filter_type=' . $this->request->get['filter_type'];
188: }
189:
190: if (isset($this->request->get['filter_date_from'])) {
191: $url .= '&filter_date_from=' . $this->request->get['filter_date_from'];
192: }
193:
194: if (isset($this->request->get['filter_date_to'])) {
195: $url .= '&filter_date_to=' . $this->request->get['filter_date_to'];
196: }
197:
198: $customer_approval_total = $this->model_customer_customer_approval->getTotalCustomerApprovals($filter_data);
199:
200: $data['pagination'] = $this->load->controller('common/pagination', [
201: 'total' => $customer_approval_total,
202: 'page' => $page,
203: 'limit' => $this->config->get('config_pagination_admin'),
204: 'url' => $this->url->link('customer/customer_approval.list', 'user_token=' . $this->session->data['user_token'] . $url . '&page={page}')
205: ]);
206:
207: $data['results'] = sprintf($this->language->get('text_pagination'), ($customer_approval_total) ? (($page - 1) * $this->config->get('config_pagination_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_pagination_admin')) > ($customer_approval_total - $this->config->get('config_pagination_admin'))) ? $customer_approval_total : ((($page - 1) * $this->config->get('config_pagination_admin')) + $this->config->get('config_pagination_admin')), $customer_approval_total, ceil($customer_approval_total / $this->config->get('config_pagination_admin')));
208:
209: return $this->load->view('customer/customer_approval_list', $data);
210: }
211:
212: /**
213: * Approve
214: *
215: * @return void
216: */
217: public function approve(): void {
218: $this->load->language('customer/customer_approval');
219:
220: $json = [];
221:
222: if (!$this->user->hasPermission('modify', 'customer/customer_approval')) {
223: $json['error'] = $this->language->get('error_permission');
224: }
225:
226: if (!$json) {
227: $this->load->model('customer/customer_approval');
228:
229: $approvals = [];
230:
231: if (isset($this->request->post['selected'])) {
232: $approvals = $this->request->post['selected'];
233: }
234:
235: if (isset($this->request->get['customer_approval_id'])) {
236: $approvals[] = (int)$this->request->get['customer_approval_id'];
237: }
238:
239: foreach ($approvals as $customer_approval_id) {
240: $customer_approval_info = $this->model_customer_customer_approval->getCustomerApproval($customer_approval_id);
241:
242: if ($customer_approval_info) {
243: if ($customer_approval_info['type'] == 'customer') {
244: $this->model_customer_customer_approval->approveCustomer($customer_approval_info['customer_id']);
245: }
246:
247: if ($customer_approval_info['type'] == 'affiliate') {
248: $this->model_customer_customer_approval->approveAffiliate($customer_approval_info['customer_id']);
249: }
250: }
251: }
252:
253: $json['success'] = $this->language->get('text_success');
254: }
255:
256: $this->response->addHeader('Content-Type: application/json');
257: $this->response->setOutput(json_encode($json));
258: }
259:
260: /**
261: * Deny
262: *
263: * @return void
264: */
265: public function deny(): void {
266: $this->load->language('customer/customer_approval');
267:
268: $json = [];
269:
270: if (!$this->user->hasPermission('modify', 'customer/customer_approval')) {
271: $json['error'] = $this->language->get('error_permission');
272: }
273:
274: if (!$json) {
275: $this->load->model('customer/customer_approval');
276:
277: $denials = [];
278:
279: if (isset($this->request->post['selected'])) {
280: $denials = $this->request->post['selected'];
281: }
282:
283: if (isset($this->request->get['customer_approval_id'])) {
284: $denials[] = (int)$this->request->get['customer_approval_id'];
285: }
286:
287: foreach ($denials as $customer_approval_id) {
288: $customer_approval_info = $this->model_customer_customer_approval->getCustomerApproval($customer_approval_id);
289:
290: if ($customer_approval_info) {
291: if ($customer_approval_info['type'] == 'customer') {
292: $this->model_customer_customer_approval->denyCustomer($customer_approval_info['customer_id']);
293: }
294:
295: if ($customer_approval_info['type'] == 'affiliate') {
296: $this->model_customer_customer_approval->denyAffiliate($customer_approval_info['customer_id']);
297: }
298: }
299: }
300:
301: $json['success'] = $this->language->get('text_success');
302: }
303:
304: $this->response->addHeader('Content-Type: application/json');
305: $this->response->setOutput(json_encode($json));
306: }
307: }
308: