1: | <?php
2: | namespace Opencart\Catalog\Controller\Checkout;
3: | /**
4: | * Class Voucher
5: | *
6: | * @package Opencart\Catalog\Controller\Checkout
7: | */
8: | class Voucher extends \Opencart\System\Engine\Controller {
9: | /**
10: | * @return void
11: | */
12: | public function index(): void {
13: | $this->load->language('checkout/voucher');
14: |
15: | $this->document->setTitle($this->language->get('heading_title'));
16: |
17: | if (!isset($this->session->data['vouchers'])) {
18: | $this->session->data['vouchers'] = [];
19: | }
20: |
21: | $data['breadcrumbs'] = [];
22: |
23: | $data['breadcrumbs'][] = [
24: | 'text' => $this->language->get('text_home'),
25: | 'href' => $this->url->link('common/home', 'language=' . $this->config->get('config_language'))
26: | ];
27: |
28: | $data['breadcrumbs'][] = [
29: | 'text' => $this->language->get('text_account'),
30: | 'href' => $this->url->link('account/account', 'language=' . $this->config->get('config_language'))
31: | ];
32: |
33: | $data['breadcrumbs'][] = [
34: | 'text' => $this->language->get('text_voucher'),
35: | 'href' => $this->url->link('checkout/voucher', 'language=' . $this->config->get('config_language'))
36: | ];
37: |
38: | $data['help_amount'] = sprintf($this->language->get('help_amount'), $this->currency->format($this->config->get('config_voucher_min'), $this->session->data['currency']), $this->currency->format($this->config->get('config_voucher_max'), $this->session->data['currency']));
39: |
40: | $this->session->data['voucher_token'] = oc_token(26);
41: |
42: | $data['save'] = $this->url->link('checkout/voucher.add', 'language=' . $this->config->get('config_language') . '&voucher_token=' . $this->session->data['voucher_token']);
43: |
44: | if ($this->customer->isLogged()) {
45: | $data['from_name'] = $this->customer->getFirstName() . ' ' . $this->customer->getLastName();
46: | } else {
47: | $data['from_name'] = '';
48: | }
49: |
50: | if ($this->customer->isLogged()) {
51: | $data['from_email'] = $this->customer->getEmail();
52: | } else {
53: | $data['from_email'] = '';
54: | }
55: |
56: | $data['amount'] = $this->currency->format($this->config->get('config_voucher_min'), $this->config->get('config_currency'), 0.0, false);
57: |
58: | $this->load->model('checkout/voucher_theme');
59: |
60: | $data['voucher_themes'] = $this->model_checkout_voucher_theme->getVoucherThemes();
61: |
62: | $data['column_left'] = $this->load->controller('common/column_left');
63: | $data['column_right'] = $this->load->controller('common/column_right');
64: | $data['content_top'] = $this->load->controller('common/content_top');
65: | $data['content_bottom'] = $this->load->controller('common/content_bottom');
66: | $data['footer'] = $this->load->controller('common/footer');
67: | $data['header'] = $this->load->controller('common/header');
68: |
69: | $this->response->setOutput($this->load->view('checkout/voucher', $data));
70: | }
71: |
72: | /**
73: | * Add
74: | *
75: | * @return void
76: | */
77: | public function add(): void {
78: | $this->load->language('checkout/voucher');
79: |
80: | $json = [];
81: |
82: | $keys = [
83: | 'to_name',
84: | 'to_email',
85: | 'from_name',
86: | 'from_email',
87: | 'voucher_theme_id',
88: | 'amount',
89: | 'agree'
90: | ];
91: |
92: | foreach ($keys as $key) {
93: | if (!isset($this->request->post[$key])) {
94: | $this->request->post[$key] = '';
95: | }
96: | }
97: |
98: | if (!isset($this->request->get['voucher_token']) || !isset($this->session->data['voucher_token']) || ($this->session->data['voucher_token'] != $this->request->get['voucher_token'])) {
99: | $json['redirect'] = $this->url->link('checkout/voucher', 'language=' . $this->config->get('config_language'), true);
100: | }
101: |
102: | if ((oc_strlen($this->request->post['to_name']) < 1) || (oc_strlen($this->request->post['to_name']) > 64)) {
103: | $json['error']['to_name'] = $this->language->get('error_to_name');
104: | }
105: |
106: | if ((oc_strlen($this->request->post['to_email']) > 96) || !filter_var($this->request->post['to_email'], FILTER_VALIDATE_EMAIL)) {
107: | $json['error']['to_email'] = $this->language->get('error_email');
108: | }
109: |
110: | if ((oc_strlen($this->request->post['from_name']) < 1) || (oc_strlen($this->request->post['from_name']) > 64)) {
111: | $json['error']['from_name'] = $this->language->get('error_from_name');
112: | }
113: |
114: | if ((oc_strlen($this->request->post['from_email']) > 96) || !filter_var($this->request->post['from_email'], FILTER_VALIDATE_EMAIL)) {
115: | $json['error']['from_email'] = $this->language->get('error_email');
116: | }
117: |
118: | if (!$this->request->post['voucher_theme_id']) {
119: | $json['error']['theme'] = $this->language->get('error_theme');
120: | }
121: |
122: | if (($this->currency->convert((int)$this->request->post['amount'], $this->session->data['currency'], $this->config->get('config_currency')) < $this->config->get('config_voucher_min')) || ($this->currency->convert($this->request->post['amount'], $this->session->data['currency'], $this->config->get('config_currency')) > $this->config->get('config_voucher_max'))) {
123: | $json['error']['amount'] = sprintf($this->language->get('error_amount'), $this->currency->format($this->config->get('config_voucher_min'), $this->session->data['currency']), $this->currency->format($this->config->get('config_voucher_max'), $this->session->data['currency']));
124: | }
125: |
126: | if (!isset($this->request->post['agree'])) {
127: | $json['error']['warning'] = $this->language->get('error_agree');
128: | }
129: |
130: | if (!$json) {
131: | $code = oc_token(10);
132: |
133: | $this->session->data['vouchers'][] = [
134: | 'code' => $code,
135: | 'description' => sprintf($this->language->get('text_for'), $this->currency->format($this->request->post['amount'], $this->session->data['currency'], 1.0), $this->request->post['to_name']),
136: | 'to_name' => $this->request->post['to_name'],
137: | 'to_email' => $this->request->post['to_email'],
138: | 'from_name' => $this->request->post['from_name'],
139: | 'from_email' => $this->request->post['from_email'],
140: | 'voucher_theme_id' => $this->request->post['voucher_theme_id'],
141: | 'message' => $this->request->post['message'],
142: | 'amount' => $this->currency->convert((int)$this->request->post['amount'], $this->session->data['currency'], $this->config->get('config_currency'))
143: | ];
144: |
145: | unset($this->session->data['shipping_method']);
146: | unset($this->session->data['shipping_methods']);
147: | unset($this->session->data['payment_method']);
148: | unset($this->session->data['payment_methods']);
149: | unset($this->session->data['reward']);
150: |
151: | $json['redirect'] = $this->url->link('checkout/voucher.success', 'language=' . $this->config->get('config_language'), true);
152: | }
153: |
154: | $this->response->addHeader('Content-Type: application/json');
155: | $this->response->setOutput(json_encode($json));
156: | }
157: |
158: | /**
159: | * Remove
160: | *
161: | * @return void
162: | */
163: | public function remove(): void {
164: | $this->load->language('checkout/voucher');
165: |
166: | $json = [];
167: |
168: | if (isset($this->request->get['key'])) {
169: | $key = $this->request->get['key'];
170: | } else {
171: | $key = '';
172: | }
173: |
174: | if (!isset($this->session->data['vouchers'][$key])) {
175: | $json['error'] = $this->language->get('error_voucher');
176: | }
177: |
178: | if (!$json) {
179: | if ($this->cart->hasProducts() || !empty($this->session->data['vouchers'])) {
180: | $json['success'] = $this->language->get('text_remove');
181: | } else {
182: | $json['redirect'] = $this->url->link('checkout/cart', 'language=' . $this->config->get('config_language'), true);
183: | }
184: |
185: | unset($this->session->data['vouchers'][$key]);
186: | unset($this->session->data['shipping_method']);
187: | unset($this->session->data['shipping_methods']);
188: | unset($this->session->data['payment_method']);
189: | unset($this->session->data['payment_methods']);
190: | unset($this->session->data['reward']);
191: | }
192: |
193: | $this->response->addHeader('Content-Type: application/json');
194: | $this->response->setOutput(json_encode($json));
195: | }
196: |
197: | /**
198: | * Success
199: | *
200: | * @return void
201: | */
202: | public function success(): void {
203: | $this->load->language('checkout/voucher');
204: |
205: | $this->document->setTitle($this->language->get('heading_title'));
206: |
207: | $data['breadcrumbs'] = [];
208: |
209: | $data['breadcrumbs'][] = [
210: | 'text' => $this->language->get('text_home'),
211: | 'href' => $this->url->link('common/home', 'language=' . $this->config->get('config_language'))
212: | ];
213: |
214: | $data['breadcrumbs'][] = [
215: | 'text' => $this->language->get('heading_title'),
216: | 'href' => $this->url->link('checkout/voucher', 'language=' . $this->config->get('config_language'))
217: | ];
218: |
219: | $data['continue'] = $this->url->link('checkout/cart', 'language=' . $this->config->get('config_language'));
220: |
221: | $data['column_left'] = $this->load->controller('common/column_left');
222: | $data['column_right'] = $this->load->controller('common/column_right');
223: | $data['content_top'] = $this->load->controller('common/content_top');
224: | $data['content_bottom'] = $this->load->controller('common/content_bottom');
225: | $data['footer'] = $this->load->controller('common/footer');
226: | $data['header'] = $this->load->controller('common/header');
227: |
228: | $this->response->setOutput($this->load->view('common/success', $data));
229: | }
230: | }
231: |

OpenCart API API documentation generated by ApiGen dev-master