1: <?php
2: namespace Opencart\Catalog\Controller\Account;
3: /**
4: * Class Affiliate
5: *
6: * @package Opencart\Catalog\Controller\Account
7: */
8: class Affiliate extends \Opencart\System\Engine\Controller {
9: /**
10: * @return void
11: */
12: public function index(): void {
13: $this->load->language('account/affiliate');
14:
15: if (!$this->customer->isLogged() || (!isset($this->request->get['customer_token']) || !isset($this->session->data['customer_token']) || ($this->request->get['customer_token'] != $this->session->data['customer_token']))) {
16: $this->customer->logout();
17:
18: $this->session->data['redirect'] = $this->url->link('account/affiliate', 'language=' . $this->config->get('config_language'));
19:
20: $this->response->redirect($this->url->link('account/login', 'language=' . $this->config->get('config_language'), true));
21: }
22:
23: $this->document->setTitle($this->language->get('heading_title'));
24:
25: $data['error_upload_size'] = sprintf($this->language->get('error_upload_size'), $this->config->get('config_file_max_size'));
26:
27: $data['config_file_max_size'] = ((int)$this->config->get('config_file_max_size') * 1024 * 1024);
28:
29: $data['breadcrumbs'] = [];
30:
31: $data['breadcrumbs'][] = [
32: 'text' => $this->language->get('text_home'),
33: 'href' => $this->url->link('common/home', 'language=' . $this->config->get('config_language'))
34: ];
35:
36: $data['breadcrumbs'][] = [
37: 'text' => $this->language->get('text_account'),
38: 'href' => $this->url->link('account/account', 'language=' . $this->config->get('config_language') . '&customer_token=' . $this->session->data['customer_token'])
39: ];
40:
41: $data['breadcrumbs'][] = [
42: 'text' => $this->language->get('text_affiliate'),
43: 'href' => $this->url->link('account/affiliate', 'language=' . $this->config->get('config_language') . '&customer_token=' . $this->session->data['customer_token'])
44: ];
45:
46: $data['save'] = $this->url->link('account/affiliate.save', 'language=' . $this->config->get('config_language') . '&customer_token=' . $this->session->data['customer_token']);
47:
48: $this->session->data['upload_token'] = oc_token(32);
49:
50: $data['upload'] = $this->url->link('tool/upload', 'language=' . $this->config->get('config_language') . '&upload_token=' . $this->session->data['upload_token']);
51:
52: $this->load->model('account/affiliate');
53:
54: $affiliate_info = $this->model_account_affiliate->getAffiliate($this->customer->getId());
55:
56: if (!empty($affiliate_info)) {
57: $data['company'] = $affiliate_info['company'];
58: } else {
59: $data['company'] = '';
60: }
61:
62: if (!empty($affiliate_info)) {
63: $data['website'] = $affiliate_info['website'];
64: } else {
65: $data['website'] = '';
66: }
67:
68: if (!empty($affiliate_info)) {
69: $data['tax'] = $affiliate_info['tax'];
70: } else {
71: $data['tax'] = '';
72: }
73:
74: if (!empty($affiliate_info)) {
75: $data['payment_method'] = $affiliate_info['payment_method'];
76: } else {
77: $data['payment_method'] = 'cheque';
78: }
79:
80: if (!empty($affiliate_info)) {
81: $data['cheque'] = $affiliate_info['cheque'];
82: } else {
83: $data['cheque'] = '';
84: }
85:
86: if (!empty($affiliate_info)) {
87: $data['paypal'] = $affiliate_info['paypal'];
88: } else {
89: $data['paypal'] = '';
90: }
91:
92: if (!empty($affiliate_info)) {
93: $data['bank_name'] = $affiliate_info['bank_name'];
94: } else {
95: $data['bank_name'] = '';
96: }
97:
98: if (!empty($affiliate_info)) {
99: $data['bank_branch_number'] = $affiliate_info['bank_branch_number'];
100: } else {
101: $data['bank_branch_number'] = '';
102: }
103:
104: if (!empty($affiliate_info)) {
105: $data['bank_swift_code'] = $affiliate_info['bank_swift_code'];
106: } else {
107: $data['bank_swift_code'] = '';
108: }
109:
110: if (!empty($affiliate_info)) {
111: $data['bank_account_name'] = $affiliate_info['bank_account_name'];
112: } else {
113: $data['bank_account_name'] = '';
114: }
115:
116: if (!empty($affiliate_info)) {
117: $data['bank_account_number'] = $affiliate_info['bank_account_number'];
118: } else {
119: $data['bank_account_number'] = '';
120: }
121:
122: // Custom Fields
123: $this->load->model('account/custom_field');
124:
125: $custom_fields = $this->model_account_custom_field->getCustomFields((int)$this->config->get('config_customer_group_id'));
126:
127: foreach ($custom_fields as $custom_field) {
128: if ($custom_field['location'] == 'affiliate') {
129: $data['custom_fields'][] = $custom_field;
130: }
131: }
132:
133: if (!empty($affiliate_info)) {
134: $data['affiliate_custom_field'] = $affiliate_info['custom_field'];
135: } else {
136: $data['affiliate_custom_field'] = [];
137: }
138:
139: $affiliate_info = $this->model_account_affiliate->getAffiliate($this->customer->getId());
140:
141: if (!$affiliate_info && $this->config->get('config_affiliate_id')) {
142: $this->load->model('catalog/information');
143:
144: $information_info = $this->model_catalog_information->getInformation($this->config->get('config_affiliate_id'));
145:
146: if ($information_info) {
147: $data['text_agree'] = sprintf($this->language->get('text_agree'), $this->url->link('information/information.info', 'language=' . $this->config->get('config_language') . '&information_id=' . $this->config->get('config_affiliate_id')), $information_info['title']);
148: } else {
149: $data['text_agree'] = '';
150: }
151: } else {
152: $data['text_agree'] = '';
153: }
154:
155: $data['back'] = $this->url->link('account/account', 'language=' . $this->config->get('config_language') . '&customer_token=' . $this->session->data['customer_token']);
156:
157: $data['language'] = $this->config->get('config_language');
158:
159: $data['column_left'] = $this->load->controller('common/column_left');
160: $data['column_right'] = $this->load->controller('common/column_right');
161: $data['content_top'] = $this->load->controller('common/content_top');
162: $data['content_bottom'] = $this->load->controller('common/content_bottom');
163: $data['footer'] = $this->load->controller('common/footer');
164: $data['header'] = $this->load->controller('common/header');
165:
166: $this->response->setOutput($this->load->view('account/affiliate', $data));
167: }
168:
169: /**
170: * Save
171: *
172: * @return void
173: */
174: public function save(): void {
175: $this->load->language('account/affiliate');
176:
177: $json = [];
178:
179: if (!$this->customer->isLogged() || (!isset($this->request->get['customer_token']) || !isset($this->session->data['customer_token']) || ($this->request->get['customer_token'] != $this->session->data['customer_token']))) {
180: $this->session->data['redirect'] = $this->url->link('account/affiliate', 'language=' . $this->config->get('config_language'));
181:
182: $json['redirect'] = $this->url->link('account/login', 'language=' . $this->config->get('config_language'), true);
183: }
184:
185: if (!$this->config->get('config_affiliate_status')) {
186: $json['redirect'] = $this->url->link('account/account', 'language=' . $this->config->get('config_language') . '&customer_token=' . $this->session->data['customer_token'], true);
187: }
188:
189: $keys = [
190: 'payment_method',
191: 'cheque',
192: 'paypal',
193: 'bank_account_name',
194: 'bank_account_number',
195: 'agree'
196: ];
197:
198: foreach ($keys as $key) {
199: if (!isset($this->request->post[$key])) {
200: $this->request->post[$key] = '';
201: }
202: }
203:
204: if (!$json) {
205: // Payment validation
206: if (empty($this->request->post['payment_method'])) {
207: $json['error']['payment_method'] = $this->language->get('error_payment_method');
208: }
209:
210: if ($this->request->post['payment_method'] == 'cheque' && !$this->request->post['cheque']) {
211: $json['error']['cheque'] = $this->language->get('error_cheque');
212: } elseif ($this->request->post['payment_method'] == 'paypal' && ((oc_strlen($this->request->post['paypal']) > 96) || !filter_var($this->request->post['paypal'], FILTER_VALIDATE_EMAIL))) {
213: $json['error']['paypal'] = $this->language->get('error_paypal');
214: } elseif ($this->request->post['payment_method'] == 'bank') {
215: if ($this->request->post['bank_account_name'] == '') {
216: $json['error']['bank_account_name'] = $this->language->get('error_bank_account_name');
217: }
218:
219: if ($this->request->post['bank_account_number'] == '') {
220: $json['error']['bank_account_number'] = $this->language->get('error_bank_account_number');
221: }
222: }
223:
224: // Custom field validation
225: $this->load->model('account/custom_field');
226:
227: $custom_fields = $this->model_account_custom_field->getCustomFields((int)$this->config->get('config_customer_group_id'));
228:
229: foreach ($custom_fields as $custom_field) {
230: if ($custom_field['location'] == 'affiliate') {
231: if ($custom_field['required'] && empty($this->request->post['custom_field'][$custom_field['custom_field_id']])) {
232: $json['error']['custom_field_' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);
233: } elseif (($custom_field['type'] == 'text') && !empty($custom_field['validation']) && !preg_match(html_entity_decode($custom_field['validation'], ENT_QUOTES, 'UTF-8'), $this->request->post['custom_field'][$custom_field['custom_field_id']])) {
234: $json['error']['custom_field_' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_regex'), $custom_field['name']);
235: }
236: }
237: }
238:
239: // Validate agree only if customer not already an affiliate
240: $this->load->model('account/affiliate');
241:
242: $affiliate_info = $this->model_account_affiliate->getAffiliate($this->customer->getId());
243:
244: if (!$affiliate_info) {
245: $this->load->model('catalog/information');
246:
247: $information_info = $this->model_catalog_information->getInformation($this->config->get('config_affiliate_id'));
248:
249: if ($information_info && !$this->request->post['agree']) {
250: $json['error']['warning'] = sprintf($this->language->get('error_agree'), $information_info['title']);
251: }
252: }
253: }
254:
255: if (!$json) {
256: if (!$affiliate_info) {
257: $this->model_account_affiliate->addAffiliate($this->customer->getId(), $this->request->post);
258: } else {
259: $this->model_account_affiliate->editAffiliate($this->customer->getId(), $this->request->post);
260: }
261:
262: $this->session->data['success'] = $this->language->get('text_success');
263:
264: $json['redirect'] = $this->url->link('account/account', 'language=' . $this->config->get('config_language') . '&customer_token=' . $this->session->data['customer_token'], true);
265: }
266:
267: $this->response->addHeader('Content-Type: application/json');
268: $this->response->setOutput(json_encode($json));
269: }
270: }
271: