PageRenderTime 57ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/controller/sale/contact.php

https://bitbucket.org/jjasko/opencart_serbian
PHP | 328 lines | 262 code | 66 blank | 0 comment | 42 complexity | 48f48923e001326516eb2e85da6d7137 MD5 | raw file
  1. <?php
  2. class ControllerSaleContact extends Controller {
  3. private $error = array();
  4. public function index() {
  5. $this->load->language('sale/contact');
  6. $this->document->setTitle($this->language->get('heading_title'));
  7. $this->load->model('sale/customer');
  8. $this->load->model('sale/customer_group');
  9. $this->load->model('sale/affiliate');
  10. if (($this->request->server['REQUEST_METHOD'] == 'POST') && ($this->validate())) {
  11. $this->load->model('setting/store');
  12. $store_info = $this->model_setting_store->getStore($this->request->post['store_id']);
  13. if ($store_info) {
  14. $store_name = $store_info['name'];
  15. } else {
  16. $store_name = $this->config->get('config_name');
  17. }
  18. $emails = array();
  19. switch ($this->request->post['to']) {
  20. case 'newsletter':
  21. $results = $this->model_sale_customer->getCustomersByNewsletter();
  22. foreach ($results as $result) {
  23. $emails[] = $result['email'];
  24. }
  25. break;
  26. case 'customer_all':
  27. $results = $this->model_sale_customer->getCustomers();
  28. foreach ($results as $result) {
  29. $emails[] = $result['email'];
  30. }
  31. break;
  32. case 'customer_group':
  33. $results = $this->model_sale_customer->getCustomersByCustomerGroupId($this->request->post['customer_group_id']);
  34. foreach ($results as $result) {
  35. $emails[$result['customer_id']] = $result['email'];
  36. }
  37. break;
  38. case 'customer':
  39. if (isset($this->request->post['customer'])) {
  40. foreach ($this->request->post['customer'] as $customer_id) {
  41. $customer_info = $this->model_sale_customer->getCustomer($customer_id);
  42. if ($customer_info) {
  43. $emails[] = $customer_info['email'];
  44. }
  45. }
  46. }
  47. break;
  48. case 'affiliate_all':
  49. $results = $this->model_sale_affiliate->getAffiliates();
  50. foreach ($results as $result) {
  51. $emails[] = $result['email'];
  52. }
  53. break;
  54. case 'affiliate':
  55. if (isset($this->request->post['affiliate'])) {
  56. foreach ($this->request->post['affiliate'] as $affiliate_id) {
  57. $affiliate_info = $this->model_sale_affiliate->getAffiliate($affiliate_id);
  58. if ($affiliate_info) {
  59. $emails[] = $affiliate_info['email'];
  60. }
  61. }
  62. }
  63. break;
  64. case 'product':
  65. if (isset($this->request->post['product'])) {
  66. foreach ($this->request->post['product'] as $product_id) {
  67. $results = $this->model_sale_customer->getCustomersByProduct($product_id);
  68. foreach ($results as $result) {
  69. $emails[] = $result['email'];
  70. }
  71. }
  72. }
  73. break;
  74. }
  75. $emails = array_unique($emails);
  76. if ($emails) {
  77. $message = '<html dir="ltr" lang="en">' . "\n";
  78. $message .= '<head>' . "\n";
  79. $message .= '<title>' . $this->request->post['subject'] . '</title>' . "\n";
  80. $message .= '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">' . "\n";
  81. $message .= '</head>' . "\n";
  82. $message .= '<body>' . html_entity_decode($this->request->post['message'], ENT_QUOTES, 'UTF-8') . '</body>' . "\n";
  83. $message .= '</html>' . "\n";
  84. $attachments = array();
  85. if (preg_match_all('#(src="([^"]*)")#mis', $message, $matches)) {
  86. foreach ($matches[2] as $key => $value) {
  87. $filename = md5(basename($value)) . strrchr($value, '.');
  88. $path = rtrim($this->request->server['DOCUMENT_ROOT'], '/') . parse_url($value, PHP_URL_PATH);
  89. $attachments[] = array(
  90. 'filename' => $filename,
  91. 'path' => $path
  92. );
  93. $message = str_replace($value, 'cid:' . $filename, $message);
  94. }
  95. }
  96. foreach ($emails as $email) {
  97. $mail = new Mail();
  98. $mail->protocol = $this->config->get('config_mail_protocol');
  99. $mail->parameter = $this->config->get('config_mail_parameter');
  100. $mail->hostname = $this->config->get('config_smtp_host');
  101. $mail->username = $this->config->get('config_smtp_username');
  102. $mail->password = $this->config->get('config_smtp_password');
  103. $mail->port = $this->config->get('config_smtp_port');
  104. $mail->timeout = $this->config->get('config_smtp_timeout');
  105. $mail->setTo($email);
  106. $mail->setFrom($this->config->get('config_email'));
  107. $mail->setSender($store_name);
  108. $mail->setSubject($this->request->post['subject']);
  109. foreach ($attachments as $attachment) {
  110. $mail->addAttachment($attachment['path'], $attachment['filename']);
  111. }
  112. $mail->setHtml($message);
  113. $mail->send();
  114. }
  115. }
  116. $this->session->data['success'] = $this->language->get('text_success');
  117. }
  118. $this->data['heading_title'] = $this->language->get('heading_title');
  119. $this->data['text_default'] = $this->language->get('text_default');
  120. $this->data['text_newsletter'] = $this->language->get('text_newsletter');
  121. $this->data['text_customer_all'] = $this->language->get('text_customer_all');
  122. $this->data['text_customer'] = $this->language->get('text_customer');
  123. $this->data['text_customer_group'] = $this->language->get('text_customer_group');
  124. $this->data['text_affiliate_all'] = $this->language->get('text_affiliate_all');
  125. $this->data['text_affiliate'] = $this->language->get('text_affiliate');
  126. $this->data['text_product'] = $this->language->get('text_product');
  127. $this->data['entry_store'] = $this->language->get('entry_store');
  128. $this->data['entry_to'] = $this->language->get('entry_to');
  129. $this->data['entry_customer_group'] = $this->language->get('entry_customer_group');
  130. $this->data['entry_customer'] = $this->language->get('entry_customer');
  131. $this->data['entry_affiliate'] = $this->language->get('entry_affiliate');
  132. $this->data['entry_product'] = $this->language->get('entry_product');
  133. $this->data['entry_subject'] = $this->language->get('entry_subject');
  134. $this->data['entry_message'] = $this->language->get('entry_message');
  135. $this->data['button_send'] = $this->language->get('button_send');
  136. $this->data['button_cancel'] = $this->language->get('button_cancel');
  137. $this->data['tab_general'] = $this->language->get('tab_general');
  138. $this->data['token'] = $this->session->data['token'];
  139. if (isset($this->error['warning'])) {
  140. $this->data['error_warning'] = $this->error['warning'];
  141. } else {
  142. $this->data['error_warning'] = '';
  143. }
  144. if (isset($this->error['subject'])) {
  145. $this->data['error_subject'] = $this->error['subject'];
  146. } else {
  147. $this->data['error_subject'] = '';
  148. }
  149. if (isset($this->error['message'])) {
  150. $this->data['error_message'] = $this->error['message'];
  151. } else {
  152. $this->data['error_message'] = '';
  153. }
  154. $this->data['breadcrumbs'] = array();
  155. $this->data['breadcrumbs'][] = array(
  156. 'text' => $this->language->get('text_home'),
  157. 'href' => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
  158. 'separator' => false
  159. );
  160. $this->data['breadcrumbs'][] = array(
  161. 'text' => $this->language->get('heading_title'),
  162. 'href' => $this->url->link('sale/contact', 'token=' . $this->session->data['token'], 'SSL'),
  163. 'separator' => ' :: '
  164. );
  165. if (isset($this->session->data['success'])) {
  166. $this->data['success'] = $this->session->data['success'];
  167. unset($this->session->data['success']);
  168. } else {
  169. $this->data['success'] = '';
  170. }
  171. $this->data['action'] = $this->url->link('sale/contact', 'token=' . $this->session->data['token'], 'SSL');
  172. $this->data['cancel'] = $this->url->link('sale/contact', 'token=' . $this->session->data['token'], 'SSL');
  173. if (isset($this->request->post['store_id'])) {
  174. $this->data['store_id'] = $this->request->post['store_id'];
  175. } else {
  176. $this->data['store_id'] = '';
  177. }
  178. $this->load->model('setting/store');
  179. $this->data['stores'] = $this->model_setting_store->getStores();
  180. if (isset($this->request->post['to'])) {
  181. $this->data['to'] = $this->request->post['to'];
  182. } else {
  183. $this->data['to'] = '';
  184. }
  185. if (isset($this->request->post['customer_group_id'])) {
  186. $this->data['customer_group_id'] = $this->request->post['customer_group_id'];
  187. } else {
  188. $this->data['customer_group_id'] = '';
  189. }
  190. $this->data['customer_groups'] = $this->model_sale_customer_group->getCustomerGroups(0);
  191. $this->data['customers'] = array();
  192. if (isset($this->request->post['customer'])) {
  193. foreach ($this->request->post['customer'] as $customer_id) {
  194. $customer_info = $this->model_sale_customer->getCustomer($customer_id);
  195. if ($customer_info) {
  196. $this->data['customers'][] = array(
  197. 'customer_id' => $customer_info['customer_id'],
  198. 'name' => $customer_info['firstname'] . ' ' . $customer_info['lastname']
  199. );
  200. }
  201. }
  202. }
  203. $this->data['affiliates'] = array();
  204. if (isset($this->request->post['affiliate'])) {
  205. foreach ($this->request->post['affiliate'] as $affiliate_id) {
  206. $affiliate_info = $this->model_sale_affiliate->getAffiliate($affiliate_id);
  207. if ($affiliate_info) {
  208. $this->data['affiliates'][] = array(
  209. 'affiliate_id' => $affiliate_info['affiliate_id'],
  210. 'name' => $affiliate_info['firstname'] . ' ' . $affiliate_info['lastname']
  211. );
  212. }
  213. }
  214. }
  215. $this->load->model('catalog/product');
  216. $this->data['products'] = array();
  217. if (isset($this->request->post['product'])) {
  218. foreach ($this->request->post['product'] as $product_id) {
  219. $product_info = $this->model_catalog_product->getProduct($product_id);
  220. if ($product_info) {
  221. $this->data['products'][] = array(
  222. 'product_id' => $product_info['product_id'],
  223. 'name' => $product_info['name']
  224. );
  225. }
  226. }
  227. }
  228. if (isset($this->request->post['subject'])) {
  229. $this->data['subject'] = $this->request->post['subject'];
  230. } else {
  231. $this->data['subject'] = '';
  232. }
  233. if (isset($this->request->post['message'])) {
  234. $this->data['message'] = $this->request->post['message'];
  235. } else {
  236. $this->data['message'] = '';
  237. }
  238. $this->template = 'sale/contact.tpl';
  239. $this->children = array(
  240. 'common/header',
  241. 'common/footer'
  242. );
  243. $this->response->setOutput($this->render());
  244. }
  245. private function validate() {
  246. if (!$this->user->hasPermission('modify', 'sale/contact')) {
  247. $this->error['warning'] = $this->language->get('error_permission');
  248. }
  249. if (!$this->request->post['subject']) {
  250. $this->error['subject'] = $this->language->get('error_subject');
  251. }
  252. if (!$this->request->post['message']) {
  253. $this->error['message'] = $this->language->get('error_message');
  254. }
  255. if (!$this->error) {
  256. return true;
  257. } else {
  258. return false;
  259. }
  260. }
  261. }
  262. ?>