1: <?php
2: namespace Opencart\Admin\Controller\Marketing;
3: /**
4: * Class Contact
5: *
6: * @package Opencart\Admin\Controller\Marketing
7: */
8: class Contact extends \Opencart\System\Engine\Controller {
9: /**
10: * Index
11: *
12: * @return void
13: */
14: public function index(): void {
15: $this->load->language('marketing/contact');
16:
17: $this->document->setTitle($this->language->get('heading_title'));
18:
19: $this->document->addScript('view/javascript/ckeditor/ckeditor.js');
20: $this->document->addScript('view/javascript/ckeditor/adapters/jquery.js');
21:
22: $data['breadcrumbs'] = [];
23:
24: $data['breadcrumbs'][] = [
25: 'text' => $this->language->get('text_home'),
26: 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
27: ];
28:
29: $data['breadcrumbs'][] = [
30: 'text' => $this->language->get('heading_title'),
31: 'href' => $this->url->link('marketing/contact', 'user_token=' . $this->session->data['user_token'])
32: ];
33:
34: $this->load->model('setting/store');
35:
36: $data['stores'] = $this->model_setting_store->getStores();
37:
38: $this->load->model('customer/customer_group');
39:
40: $data['customer_groups'] = $this->model_customer_customer_group->getCustomerGroups();
41:
42: $data['user_token'] = $this->session->data['user_token'];
43:
44: $data['header'] = $this->load->controller('common/header');
45: $data['column_left'] = $this->load->controller('common/column_left');
46: $data['footer'] = $this->load->controller('common/footer');
47:
48: $this->response->setOutput($this->load->view('marketing/contact', $data));
49: }
50:
51: /**
52: * Send
53: *
54: * @throws \Exception
55: *
56: * @return void
57: */
58: public function send(): void {
59: $this->load->language('marketing/contact');
60:
61: $json = [];
62:
63: if (!$this->user->hasPermission('modify', 'marketing/contact')) {
64: $json['error']['warning'] = $this->language->get('error_permission');
65: }
66:
67: if (!$this->request->post['subject']) {
68: $json['error']['subject'] = $this->language->get('error_subject');
69: }
70:
71: if (!$this->request->post['message']) {
72: $json['error']['message'] = $this->language->get('error_message');
73: }
74:
75: if (!$json) {
76: $this->load->model('setting/store');
77: $this->load->model('setting/setting');
78: $this->load->model('customer/customer');
79: $this->load->model('marketing/affiliate');
80: $this->load->model('sale/order');
81:
82: $store_info = $this->model_setting_store->getStore($this->request->post['store_id']);
83:
84: if ($store_info) {
85: $store_name = $store_info['name'];
86: } else {
87: $store_name = $this->config->get('config_name');
88: }
89:
90: $setting = $this->model_setting_setting->getSetting('config', $this->request->post['store_id']);
91:
92: $store_email = $setting['config_email'] ?? $this->config->get('config_email');
93:
94: if (isset($this->request->get['page'])) {
95: $page = (int)$this->request->get['page'];
96: } else {
97: $page = 1;
98: }
99:
100: $limit = 10;
101:
102: $email_total = 0;
103:
104: $emails = [];
105:
106: switch ($this->request->post['to']) {
107: case 'newsletter':
108: $customer_data = [
109: 'filter_newsletter' => 1,
110: 'start' => ($page - 1) * $limit,
111: 'limit' => $limit
112: ];
113:
114: $email_total = $this->model_customer_customer->getTotalCustomers($customer_data);
115:
116: $results = $this->model_customer_customer->getCustomers($customer_data);
117:
118: foreach ($results as $result) {
119: $emails[] = $result['email'];
120: }
121: break;
122: case 'customer_all':
123: $customer_data = [
124: 'start' => ($page - 1) * $limit,
125: 'limit' => $limit
126: ];
127:
128: $email_total = $this->model_customer_customer->getTotalCustomers($customer_data);
129:
130: $results = $this->model_customer_customer->getCustomers($customer_data);
131:
132: foreach ($results as $result) {
133: $emails[] = $result['email'];
134: }
135: break;
136: case 'customer_group':
137: $customer_data = [
138: 'filter_customer_group_id' => $this->request->post['customer_group_id'],
139: 'start' => ($page - 1) * $limit,
140: 'limit' => $limit
141: ];
142:
143: $email_total = $this->model_customer_customer->getTotalCustomers($customer_data);
144:
145: $results = $this->model_customer_customer->getCustomers($customer_data);
146:
147: foreach ($results as $result) {
148: $emails[$result['customer_id']] = $result['email'];
149: }
150: break;
151: case 'customer':
152: if (!empty($this->request->post['customer'])) {
153: $email_total = count($this->request->post['customer']);
154:
155: $customers = array_slice($this->request->post['customer'], ($page - 1) * $limit, $limit);
156:
157: foreach ($customers as $customer_id) {
158: $customer_info = $this->model_customer_customer->getCustomer($customer_id);
159:
160: if ($customer_info) {
161: $emails[] = $customer_info['email'];
162: }
163: }
164: }
165: break;
166: case 'affiliate_all':
167: $affiliate_data = [
168: 'start' => ($page - 1) * $limit,
169: 'limit' => $limit
170: ];
171:
172: $email_total = $this->model_marketing_affiliate->getTotalAffiliates($affiliate_data);
173:
174: $results = $this->model_marketing_affiliate->getAffiliates($affiliate_data);
175:
176: foreach ($results as $result) {
177: $emails[] = $result['email'];
178: }
179: break;
180: case 'affiliate':
181: if (!empty($this->request->post['affiliate'])) {
182: $affiliates = array_slice($this->request->post['affiliate'], ($page - 1) * $limit, $limit);
183:
184: foreach ($affiliates as $affiliate_id) {
185: $affiliate_info = $this->model_marketing_affiliate->getAffiliate($affiliate_id);
186:
187: if ($affiliate_info) {
188: $emails[] = $affiliate_info['email'];
189: }
190: }
191:
192: $email_total = count($this->request->post['affiliate']);
193: }
194: break;
195: case 'product':
196: if (isset($this->request->post['product'])) {
197: $email_total = $this->model_sale_order->getTotalEmailsByProductsOrdered($this->request->post['product']);
198:
199: $results = $this->model_sale_order->getEmailsByProductsOrdered($this->request->post['product'], ($page - 1) * $limit, $limit);
200:
201: foreach ($results as $result) {
202: $emails[] = $result['email'];
203: }
204: }
205: break;
206: }
207:
208: if ($emails) {
209: $start = ($page - 1) * $limit;
210: $end = $start > ($email_total - $limit) ? $email_total : ($start + $limit);
211:
212: if ($end < $email_total) {
213: $json['text'] = sprintf($this->language->get('text_sent'), $start ?: 1, $end, $email_total);
214:
215: $json['next'] = $this->url->link('marketing/contact.send', 'user_token=' . $this->session->data['user_token'] . '&page=' . ($page + 1), true);
216: } else {
217: $json['success'] = $this->language->get('text_success');
218:
219: $json['next'] = '';
220: }
221:
222: $message = '<html dir="ltr" lang="' . $this->language->get('code') . '">' . "\n";
223: $message .= ' <head>' . "\n";
224: $message .= ' <title>' . $this->request->post['subject'] . '</title>' . "\n";
225: $message .= ' <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">' . "\n";
226: $message .= ' </head>' . "\n";
227: $message .= ' <body>' . html_entity_decode($this->request->post['message'], ENT_QUOTES, 'UTF-8') . '</body>' . "\n";
228: $message .= '</html>' . "\n";
229:
230: if ($this->config->get('config_mail_engine')) {
231: $mail_option = [
232: 'parameter' => $this->config->get('config_mail_parameter'),
233: 'smtp_hostname' => $this->config->get('config_mail_smtp_hostname'),
234: 'smtp_username' => $this->config->get('config_mail_smtp_username'),
235: 'smtp_password' => html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8'),
236: 'smtp_port' => $this->config->get('config_mail_smtp_port'),
237: 'smtp_timeout' => $this->config->get('config_mail_smtp_timeout')
238: ];
239:
240: $mail = new \Opencart\System\Library\Mail($this->config->get('config_mail_engine'), $mail_option);
241:
242: foreach ($emails as $email) {
243: if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
244: $mail->setTo(trim($email));
245: $mail->setFrom($store_email);
246: $mail->setSender(html_entity_decode($store_name, ENT_QUOTES, 'UTF-8'));
247: $mail->setSubject(html_entity_decode($this->request->post['subject'], ENT_QUOTES, 'UTF-8'));
248: $mail->setHtml($message);
249: $mail->send();
250: }
251: }
252: }
253: } else {
254: $json['error']['warning'] = $this->language->get('error_email');
255: }
256: }
257:
258: $this->response->addHeader('Content-Type: application/json');
259: $this->response->setOutput(json_encode($json));
260: }
261: }
262: