Guest User

Untitled

a guest
Jul 18th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.06 KB | None | 0 0
  1. <?php
  2.  
  3. class MailAlerts extends Module
  4. {
  5. private $_html = '';
  6. private $_postErrors = array();
  7. private $_postSucess;
  8. private $_data;
  9.  
  10. private $_mails;
  11. private $_alertNewOrder;
  12. private $_alertUpdateQuantity;
  13. private $_files = array();
  14.  
  15. const __MA_MAIL_DELIMITOR__ = ',';
  16.  
  17.  
  18. public function __construct()
  19. {
  20. $this->name = 'mailalerts';
  21. $this->tab = 'Tools';
  22. $this->version = 1.1;
  23.  
  24. $this->_mails = Configuration::get('MA_MAILS');
  25. $this->_alertNewOrder = intval(Configuration::get('MA_NEW_ORDER'));
  26. $this->_alertUpdateQuantity = intval(Configuration::get('MA_UPDATE_QUANTITY'));
  27.  
  28. parent::__construct();
  29.  
  30. /* The parent construct is required for translations */
  31. $this->page = basename(__FILE__, '.php');
  32. $this->displayName = $this->l('Mail alerts');
  33. $this->description = $this->l('Sends e-mails to the merchant for new orders and stock notifications');
  34. $this->confirmUninstall = $this->l('Are you sure you want to delete all templates mails ?');
  35. }
  36.  
  37. public function install()
  38. {
  39. parent::install();
  40. $this->registerHook('NewOrder');
  41. $this->registerHook('UpdateQuantity');
  42.  
  43. Configuration::updateValue('MA_NEW_ORDER', 1);
  44. Configuration::updateValue('MA_UPDATE_QUANTITY', 1);
  45. Configuration::updateValue('MA_MAILS', Configuration::get('PS_SHOP_EMAIL'));
  46. }
  47.  
  48. public function uninstall()
  49. {
  50. Configuration::deleteByName('MA_MAILS');
  51. Configuration::deleteByName('MA_NEW_ORDER');
  52. Configuration::deleteByName('MA_UPDATE_QUANTITY');
  53. parent::uninstall();
  54. }
  55.  
  56. public function hookNewOrder($params)
  57. {
  58. if (!$this->_alertNewOrder OR empty($this->_mails))
  59. return;
  60.  
  61. // Getting differents vars
  62. $id_lang = intval(Configuration::get('PS_LANG_DEFAULT'));
  63. $currency = $params['currency'];
  64. $configuration = Configuration::getMultiple(array('PS_SHOP_EMAIL', 'PS_MAIL_METHOD', 'PS_MAIL_SERVER', 'PS_MAIL_USER', 'PS_MAIL_PASSWD', 'PS_SHOP_NAME'));
  65. $order = $params['order'];
  66. $customer = $params['customer'];
  67. $delivery = new Address(intval($order->id_address_delivery));
  68. //Fix state problem
  69. $delivery_state = $delivery->id_state ? new State(intval($delivery->id_state)) : false;
  70. $invoice_state = $invoice->id_state ? new State(intval($invoice->id_state)) : false;
  71. //End Fix
  72. $invoice = new Address(intval($order->id_address_invoice));
  73. $order_date_text = Tools::displayDate($order->date_add, intval($id_lang));
  74. $carrier = new Carrier(intval($order->id_carrier));
  75. $message = $order->getLastMessage();
  76. if (!$message OR empty($message))
  77. $message = $this->l('No message');
  78.  
  79. $itemsTable = '';
  80. foreach ($params['cart']->getProducts() AS $key => $product)
  81. {
  82. $reduc = 0.0;
  83. $price = Tools::convertPrice(Product::getPriceStatic(intval($product['id_product']), false, ($product['id_product_attribute'] ? intval($product['id_product_attribute']) : NULL), 4), $currency);
  84. $price_wt = Tools::convertPrice(Product::getPriceStatic(intval($product['id_product']), true, ($product['id_product_attribute'] ? intval($product['id_product_attribute']) : NULL), 4), $currency);
  85. if (Tax::excludeTaxeOption())
  86. {
  87. $product['tax'] = 0;
  88. $product['rate'] = 0;
  89. }
  90. else
  91. $tax = Tax::getApplicableTax(intval($product['id_tax']), floatval($product['rate']));
  92. if ($product['quantity'] > 1 AND ($qtyD = QuantityDiscount::getDiscountFromQuantity($product['id_product'], $product['quantity'])))
  93. {
  94. $reduc = QuantityDiscount::getValue($price_wt, $qtyD->id_discount_type, $qtyD->value);
  95. $price -= $reduc / (1 + floatval($tax) / 100);
  96. }
  97. $itemsTable .=
  98. '<tr style="background-color:'.($key % 2 ? '#DDE2E6' : '#EBECEE').';">
  99. <td style="padding:0.6em 0.4em;">'.$product['reference'].'</td>
  100. <td style="padding:0.6em 0.4em;"><strong>'.$product['name'].(isset($product['attributes_small']) ? ' '.$product['attributes_small'] : '').'</strong></td>
  101. <td style="padding:0.6em 0.4em; text-align:right;">'.Tools::displayPrice($price * ($tax + 100) / 100, $currency, false, false).'</td>
  102. <td style="padding:0.6em 0.4em; text-align:center;">'.intval($product['quantity']).'</td>
  103. <td style="padding:0.6em 0.4em; text-align:right;">'.Tools::displayPrice(intval($product['quantity']) * ($price * ($tax + 100) / 100), $currency, false, false).'</td>
  104. </tr>';
  105. }
  106. foreach ($params['cart']->getDiscounts() AS $discount)
  107. {
  108. $itemsTable .=
  109. '<tr style="background-color:#EBECEE;">
  110. <td colspan="4" style="padding:0.6em 0.4em; text-align:right;">'.$this->l('Voucher code:').' '.$objDiscount->name.'</td>
  111. <td style="padding:0.6em 0.4em; text-align:right;">-'.Tools::displayPrice($value, $currency, false, false).'</td>
  112. </tr>';
  113. }
  114.  
  115. // Filling-in vars for mail
  116. $template = 'new_order';
  117. $subject = $this->l('New order');
  118. $templateVars = array(
  119. '{firstname}' => $customer->firstname,
  120. '{lastname}' => $customer->lastname,
  121. '{email}' => $customer->email,
  122. '{delivery_firstname}' => $delivery->firstname,
  123. '{delivery_lastname}' => $delivery->lastname,
  124. '{delivery_address1}' => $delivery->address1,
  125. '{delivery_address2}' => $delivery->address2,
  126. '{delivery_city}' => $delivery->city,
  127. '{delivery_postal_code}' => $delivery->postcode,
  128. '{delivery_country}' => $delivery->country,
  129. '{delivery_state}' => $delivery->id_state ? $delivery_state->name : '',
  130. '{delivery_phone}' => $delivery->phone,
  131. '{invoice_firstname}' => $invoice->firstname,
  132. '{invoice_lastname}' => $invoice->lastname,
  133. '{invoice_address2}' => $invoice->address2,
  134. '{invoice_address1}' => $invoice->address1,
  135. '{invoice_city}' => $invoice->city,
  136. '{invoice_postal_code}' => $invoice->postcode,
  137. '{invoice_country}' => $invoice->country,
  138. '{invoice_state}' => $invoice->id_state ? $invoice_state->name : '',
  139. '{invoice_phone}' => $invoice->phone,
  140. '{order_name}' => sprintf("%06d", $order->id),
  141. '{shop_name}' => Configuration::get('PS_SHOP_NAME'),
  142. '{date}' => $order_date_text,
  143. '{carrier}' => (($carrier->name == '0') ? Configuration::get('PS_SHOP_NAME') : $carrier->name),
  144. '{payment}' => $order->payment,
  145. '{items}' => $itemsTable,
  146. '{total_paid}' => Tools::displayPrice($order->total_paid, $currency),
  147. '{total_products}' => Tools::displayPrice($order->getTotalProductsWithTaxes(), $currency),
  148. '{total_discounts}' => Tools::displayPrice($order->total_discounts, $currency),
  149. '{total_shipping}' => Tools::displayPrice($order->total_shipping, $currency),
  150. '{total_wrapping}' => Tools::displayPrice($order->total_wrapping, $currency),
  151. '{currency}' => $currency->sign,
  152. '{message}' => $message
  153. '{company}' => $delivery->company
  154. );
  155. Mail::Send($id_lang, $template, $subject, $templateVars, split(',', $this->_mails), NULL, $configuration['PS_SHOP_EMAIL'], $configuration['PS_SHOP_NAME'], NULL, NULL, dirname(__FILE__).'/mails/');
  156. }
  157.  
  158. public function hookUpdateQuantity($params)
  159. {
  160. $qty = intval($params['product']['quantity_attribute'] ? $params['product']['quantity_attribute'] : $params['product']['stock_quantity']) - intval($params['product']['quantity']);
  161. if ($qty <= intval(Configuration::get('PS_LAST_QTIES')) AND !(!$this->_alertUpdateQuantity OR empty($this->_mails)))
  162. {
  163. $templateVars = array('{qty}' => $qty,
  164. '{last_qty}' => intval(Configuration::get('PS_LAST_QTIES')),
  165. '{product}' => strval($params['product']['name']));
  166. Mail::Send(intval(Configuration::get('PS_LANG_DEFAULT')), 'productoutofstock', $this->l('Product out of stock'), $templateVars, split(',', $this->_mails), NULL, strval(Configuration::get('PS_SHOP_EMAIL')), strval(Configuration::get('PS_SHOP_NAME')), NULL, NULL, dirname(__FILE__).'/mails/');
  167. }
  168. }
  169.  
  170. public function getContent()
  171. {
  172. $this->_html = '<h2>'.$this->displayName.'</h2><br/>';
  173.  
  174. if (!empty($_POST))
  175. {
  176. $this->_postValidation();
  177. if (!sizeof($this->_postErrors))
  178. $this->_postProcess();
  179. else
  180. foreach ($this->_postErrors AS $err)
  181. $this->_html .= '<div class="errmsg">'.$err.'</div>';
  182. }
  183. $this->_displayForm();
  184. return $this->_html;
  185. }
  186.  
  187. private function _displayForm()
  188. {
  189. if (!isset($_POST['btnSubmit']))
  190. {
  191. if ($this->_mails)
  192. {
  193. $_POST['mails'] = str_replace(self::__MA_MAIL_DELIMITOR__, "\n", $this->_mails);
  194. $_POST['alert_new_order'] = $this->_alertNewOrder;
  195. $_POST['alert_update_quantity'] = $this->_alertUpdateQuantity;
  196. }
  197. }
  198.  
  199. $button_txt = $this->l('Update settings');
  200. $this->_html .=
  201. '<fieldset><legend><img src="'.$this->_path.'logo.gif" alt="" title="" /> '.$this->l('Settings').'</legend>
  202. <form action="'.$_SERVER['REQUEST_URI'].'" method="post">
  203. <table id="form" cellspacing="5">
  204. <tr><td colspan="2"><h4><u>'.$this->l('Receiving alerts').'</u></h4></td></tr>
  205. <tr>
  206. <td>'.$this->l('New order alert?').'</td>
  207. <td><input type="checkbox" value="1" name="alert_new_order" '.( (isset($_POST['alert_new_order']) AND $_POST['alert_new_order'] == '1') ? 'checked' : '').'></td>
  208.  
  209. </tr>
  210. <tr>
  211. <td>'.$this->l('Inventory alert?').'</td>
  212. <td><input type="checkbox" value="1" name="alert_update_quantity" '.( (isset($_POST['alert_update_quantity']) AND $_POST['alert_update_quantity'] == '1') ? 'checked' : '').'></td>
  213. </tr>
  214. <tr><td colspan="2"><h4><u>'.$this->l('E-mail addresses').'</u></h4></td></tr>
  215. <tr><td style="vertical-align: top;">
  216. <span>'.$this->l('One e-mail per line').'<br />'.$this->l('E.g.: bob@example.com').'</span></a></td>
  217. <td>
  218. <textarea name="mails" rows="10" cols="30">'.(isset($_POST['mails']) ? $_POST['mails'] : '').'</textarea>
  219. </td></tr>
  220.  
  221. <tr>
  222. <td colspan="2" align="center"><input name="btnSubmit" class="button" value="'.$button_txt.'" type="submit" /></td>
  223. </tr>
  224. </table>
  225. </form></fieldset>';
  226. }
  227.  
  228. private function _postProcess()
  229. {
  230. $this->_alertNewOrder = isset($_POST['alert_new_order']) ? 1 : 0;
  231. $this->_alertUpdateQuantity = isset($_POST['alert_update_quantity']) ? 1 : 0;
  232.  
  233. Configuration::updateValue('MA_NEW_ORDER', isset($_POST['alert_new_order']) ? 1 : 0);
  234. Configuration::updateValue('MA_UPDATE_QUANTITY', isset($_POST['alert_update_quantity']) ? 1 : 0);
  235.  
  236. $mails = explode("\n", $_POST['mails']);
  237. $this->_mails = '';
  238. foreach ($mails as $mail)
  239. $this->_mails .= trim($mail).self::__MA_MAIL_DELIMITOR__;
  240. $this->_mails = trim($this->_mails, ',');
  241. Configuration::updateValue('MA_MAILS', $this->_mails);
  242. $this->_html .= '<div class="conf">'.$this->l('Settings updated').'</div>';
  243. }
  244.  
  245. private function _postValidation()
  246. {
  247. if (!isset($_POST['mails']) OR empty($_POST['mails']))
  248. $this->_postErrors[] = $this->l('No e-mail addresses specified');
  249. else
  250. {
  251. $mails = explode("\n", $_POST['mails']);
  252. foreach ($mails as $mail)
  253. {
  254. $mail = trim($mail);
  255. if (!empty($mail) AND !Validate::isEmail($mail))
  256. $this->_postErrors[] = $this->l('Invalid e-mail: ').$mail.'.';
  257. }
  258. }
  259. }
  260. }
  261.  
  262. ?>
Add Comment
Please, Sign In to add comment