Advertisement
Guest User

Untitled

a guest
Feb 13th, 2018
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.65 KB | None | 0 0
  1. <?php
  2. require_once(realpath(dirname(__FILE__) . "/../table/Config.php"));
  3. require_once(realpath(dirname(__FILE__) . "/../table/ProductOrder.php"));
  4. require_once(realpath(dirname(__FILE__) . "/../table/ProductOrderDetail.php"));
  5. require_once(realpath(dirname(__FILE__) . "/../conf.php"));
  6. require_once(realpath(dirname(__FILE__) . "/rest.php"));
  7.  
  8. require_once("mailer/PHPMailer.php");
  9. require_once("mailer/Exception.php");
  10. require_once("mailer/SMTP.php");
  11. use PHPMailer\PHPMailer\PHPMailer;
  12.  
  13. class Mail extends REST {
  14.  
  15. private $db = NULL;
  16. private $config = NULL;
  17. private $config_arr = NULL;
  18. private $product_order = NULL;
  19. private $product_order_detail = NULL;
  20. private $conf = NULL;
  21.  
  22. public function __construct($db) {
  23. parent::__construct();
  24. $this->db = $db;
  25. $this->config = new Config($this->db);
  26. $this->product_order = new ProductOrder($this->db);
  27. $this->product_order_detail = new ProductOrderDetail($this->db);
  28. $this->conf = new CONF(); // Create config class
  29. }
  30.  
  31. public function restEmail() {
  32. if ($this->get_request_method() != "GET") $this->response('', 406);
  33. if (!isset($this->_request['id'])) $this->responseInvalidParam();
  34. if (!isset($this->_request['type'])) $this->responseInvalidParam();
  35. $id = (int)$this->_request['id'];
  36. $type = $this->_request['type'];
  37. $debug = false;
  38. if (isset($this->_request['debug'])) {
  39. $d = (int)$this->_request['debug'];
  40. $debug = ($d == 1);
  41. }
  42.  
  43. // filter with config
  44. $this->config_arr = $this->config->findAllArr();
  45. if ($type == "NEW_ORDER") {
  46. $notif_on_order = $this->getValue($this->config_arr, 'EMAIL_NOTIF_ON_ORDER');
  47. if ($notif_on_order == null or $notif_on_order == "FALSE") return;
  48.  
  49. } else if ($type == "ORDER_PROCESS") {
  50. $notif_on_order_process = $this->getValue($this->config_arr, 'EMAIL_NOTIF_ON_ORDER_PROCESS');
  51. if ($notif_on_order_process == null or $notif_on_order_process == "FALSE") return;
  52. } else if (!$type == "ORDER_UPDATE") {
  53. return;
  54. }
  55.  
  56. $this->sendOrderEmail($id, $this->config_arr, $type, $debug);
  57. }
  58.  
  59. private function sendOrderEmail($order_id, $config_arr, $type, $debug) {
  60. // find object order order_details
  61. $order = $this->product_order->findOnePlain($order_id);
  62. $order_details = $this->product_order_detail->findAllByOrderIdPlain($order_id);
  63.  
  64. $email_reply_to = $this->getValue($this->config_arr, 'EMAIL_REPLY_TO');
  65. $email_receiver_arr = json_decode($this->getValue($this->config_arr, 'EMAIL_BCC_RECEIVER'), true);
  66. if (sizeof($email_receiver_arr) == 0) return;
  67.  
  68. try {
  69. $mailer = new PHPMailer();
  70. $mailer->IsSMTP();
  71. $mailer->SMTPAuth = true;
  72. // SMTP connection will not close after each email sent, reduces SMTP overhead
  73. $mailer->SMTPKeepAlive = true;
  74.  
  75. $mailer->Host = $this->conf->SMTP_HOST;
  76. $mailer->Port = $this->conf->SMTP_PORT;
  77. $mailer->Username = $this->conf->SMTP_EMAIL;
  78. $mailer->Password = $this->conf->SMTP_PASSWORD;
  79.  
  80. $subject_title = "";
  81. if ($type == "NEW_ORDER") {
  82. $subject_title = $this->conf->SUBJECT_EMAIL_NEW_ORDER;
  83. foreach ($email_receiver_arr as $row) {
  84. $mailer->addBCC($row, $row);
  85. }
  86. } else if ($type == "ORDER_PROCESS") {
  87. $subject_title = $this->conf->SUBJECT_EMAIL_ORDER_PROCESSED;
  88. } else if ($type == "ORDER_UPDATE") {
  89. $subject_title = $this->conf->SUBJECT_EMAIL_ORDER_UPDATED;
  90. }
  91.  
  92. $subject = '[' . $order['code'] . '] ' . $subject_title;
  93. $mailer->addCustomHeader('X-Entity-Ref-ID', $order['code']);
  94. $mailer->Subject = $subject;
  95.  
  96. $mailer->SetFrom($this->conf->SMTP_EMAIL);
  97. $mailer->addReplyTo($email_reply_to);
  98. $mailer->addAddress($order['email'], $order['email']);
  99. $template = $this->getEmailOrderTemplate($order, $order_details, $config_arr, $type);
  100. $mailer->msgHTML($template);
  101.  
  102. $error = 'Message sent!';
  103. if (!$mailer->Send()) {
  104. $error = 'Mail error: ' . $mailer->ErrorInfo;
  105. }
  106. if ($debug) echo $error;
  107.  
  108. } catch (Exception $e) {
  109.  
  110. }
  111. }
  112.  
  113. private function getEmailOrderTemplate($order, $order_details, $config_arr, $type) {
  114. $currency = $this->getValue($config_arr, 'CURRENCY');
  115. $order_item_row = "";
  116.  
  117. // calculate total
  118. $price_total = 0;
  119. $amount_total = 0;
  120. $index = 1;
  121.  
  122. foreach ($order_details as $od) {
  123. $price_total = 0;
  124. $item_row = file_get_contents(realpath(dirname(__FILE__) . "/template/order_item_row.html"));
  125. $price_total += $od['price_item'] * $od['amount'];
  126. $amount_total += $price_total;
  127. $od['index'] = $index;
  128. $od['price_total'] = number_format($price_total, 2, '.', '');
  129. foreach ($od as $key => $value) {
  130. $tagToReplace = "[@$key]";
  131. $item_row = str_replace($tagToReplace, $value, $item_row);
  132. }
  133. $order_item_row = $order_item_row . $item_row;
  134. $index++;
  135. }
  136.  
  137. $price_tax = ($order['tax'] / 100) * $amount_total;
  138. $price_tax_formatted = number_format($price_tax, 2, '.', '');
  139. $price_total_formatted = number_format($amount_total, 2, '.', '');
  140. $price_after_tax = number_format(($amount_total + $price_tax), 2, '.', '');
  141.  
  142. // binding data
  143. $order_template = file_get_contents(realpath(dirname(__FILE__) . "/template/order_template.html"));
  144. $order['date_ship'] = date("d M y", floatval($order['date_ship']) / 1000);
  145. $order['created_at'] = date("d M y", floatval($order['created_at']) / 1000);
  146. $order['last_update'] = date("d M y", floatval($order['last_update']) / 1000);
  147. foreach ($order as $key => $value) {
  148. $tagToReplace = "[@$key]";
  149. $order_template = str_replace($tagToReplace, $value, $order_template);
  150. }
  151.  
  152. // put row view into $order_template
  153. $title = "";
  154. if ($type == "NEW_ORDER") {
  155. $title = $this->conf->TITLE_REPORT_NEW_ORDER;
  156. } else if ($type == "ORDER_PROCESS") {
  157. $title = $this->conf->TITLE_REPORT_ORDER_PROCESSED;
  158. } else if ($type == "ORDER_UPDATE") {
  159. $title = $this->conf->TITLE_REPORT_ORDER_UPDATED;
  160. }
  161. $order_template = str_replace('[@report_title]', $title, $order_template);
  162. $order_template = str_replace('[@order_item_row]', $order_item_row, $order_template);
  163.  
  164. $order_template = str_replace('[@conf_currency]', $currency, $order_template);
  165. $order_template = str_replace('[@price_tax_formatted]', $price_tax_formatted, $order_template);
  166. $order_template = str_replace('[@price_total_formatted]', $price_total_formatted, $order_template);
  167. $order_template = str_replace('[@price_after_tax]', $price_after_tax, $order_template);
  168.  
  169. return $order_template;
  170. }
  171.  
  172. private function getOrderProcessedTemplate($order, $config_arr, $type) {
  173. $order_template = "";
  174. return $order_template;
  175. }
  176.  
  177. private function getValue($data, $code) {
  178. foreach ($data as $d) {
  179. if ($d['code'] == $code) {
  180. return $d['value'];
  181. }
  182. }
  183. }
  184.  
  185. }
  186.  
  187. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement