* @copyright 2007-2011 PrestaShop SA * @version Release: $Revision: 7456 $ * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) * International Registered Trademark & Property of PrestaShop SA */ if (!defined('_CAN_LOAD_FILES_')) exit; class Tm4b extends Module { private $_html = ''; private $_postErrors = array(); private $_postSucess; private $_data; private $_password; private $_user; private $_originator; private $_route; private $_simulation; private $_new_order_numbers; private $_alert_new_order_active; private $_alert_update_quantity_active; private $_daily_report_active; public $token; const __TM4B_LOWBALANCE__ = '20'; const __TM4B_NUMBER_DELIMITOR__ = ','; static private $_tpl_sms_files = array( 'name' => array( 'new_orders' => 'sms_new_order', 'out_of_stock' => 'sms_out_of_stock' ), 'ext' => array( 'new_orders' => '.txt', 'out_of_stock' => '.txt' ) ); public function __construct() { $this->name = 'tm4b'; $this->displayName = 'SMS Tm4b'; $this->description = $this->l('Sends an SMS for each new order'); $this->tab = 'administration'; $this->version = 1.1; $this->author = 'PrestaShop'; $this->need_instance = 0; parent::__construct(); if ($this->id) { $this->_data = array('shopname' => Configuration::get('PS_SHOP_NAME')); /* Get config vars */ $this->_password = Configuration::get('TM4B_PASSWORD'); $this->_user = Configuration::get('TM4B_USER'); $this->_originator = Configuration::get('TM4B_ORIGINATOR'); $this->_route = Configuration::get('TM4B_ROUTE'); $this->_simulation = Configuration::get('TM4B_SIM'); $this->_new_order_numbers = Configuration::get('TM4B_NEW_ORDER_NUMBERS'); $this->_alert_new_order_active = Configuration::get('TM4B_ALERT_NO_ACTIVE'); $this->_alert_update_quantity_active = Configuration::get('TM4B_ALERT_UQ_ACTIVE'); $this->_daily_report_active = Configuration::get('TM4B_DAILY_REPORT_ACTIVE'); } $this->displayName = 'SMS Tm4b'; $this->description = $this->l('Sends an SMS for each new order'); $this->confirmUninstall = $this->l('Are you sure you want to delete your info?'); $this->token = md5('tm4b'._COOKIE_KEY_); } public function install() { if (!parent::install() OR !$this->registerHook('newOrder') OR !$this->registerHook('updateQuantity')) return false; Configuration::updateValue('TM4B_SIM', 1); Configuration::updateValue('TM4B_ALERT_NO_ACTIVE', 1); Configuration::updateValue('TM4B_ALERT_UQ_ACTIVE', 1); Configuration::updateValue('TM4B_DAILY_REPORT_ACTIVE', 0); return true; } public function uninstall() { Configuration::deleteByName('TM4B_PASSWORD'); Configuration::deleteByName('TM4B_USER'); Configuration::deleteByName('TM4B_ORIGINATOR'); Configuration::deleteByName('TM4B_ROUTE'); Configuration::deleteByName('TM4B_SIM'); Configuration::deleteByName('TM4B_NEW_ORDER_NUMBERS'); Configuration::deleteByName('TM4B_ALERT_NO_ACTIVE'); Configuration::deleteByName('TM4B_ALERT_UQ_ACTIVE'); Configuration::deleteByName('TM4B_DAILY_REPORT_ACTIVE'); Configuration::deleteByName('TM4B_LAST_REPORT'); return parent::uninstall(); } private function _getTplBody($tpl_file, $vars = array()) { $iso = Language::getIsoById((int)(Configuration::get('PS_LANG_DEFAULT'))); $file = dirname(__FILE__).'/mails/'.$iso.'/'.$tpl_file; if (!file_exists($file)) die($file); $tpl = file($file); $template = str_replace(array_keys($vars), array_values($vars), $tpl); return (implode("\n", $template)); } public function hookNewOrder($params) { require_once (dirname(__FILE__).'/classes/Tm4bSms.php'); if ( !(int)($this->_alert_new_order_active) OR empty($this->_user) OR empty($this->_password) OR empty($this->_new_order_numbers)) return ; $order = $params['order']; $customer = $params['customer']; $currency = $params['currency']; $templateVars = array( '{firstname}' => utf8_decode($customer->firstname), '{lastname}' => utf8_decode($customer->lastname), '{order_name}' => sprintf("%06d", $order->id), '{shop_name}' => Configuration::get('PS_SHOP_NAME'), '{payment}' => $order->payment, '{total_paid}' => $order->total_paid, '{currency}' => $currency->sign); $body = $this->_getTplBody(self::$_tpl_sms_files['name']['new_orders'].self::$_tpl_sms_files['ext']['new_orders'], $templateVars); $sms = new Tm4bSms($this->_user, $this->_password, $this->_route, $this->_originator); $sms->msg = $body; $numbers = explode(self::__TM4B_NUMBER_DELIMITOR__, $this->_new_order_numbers); foreach ($numbers as $number) if ($number != '') $sms->addRecipient($number); $sms->Send($this->_simulation); } public function hookUpdateQuantity($params) { require_once (dirname(__FILE__).'/classes/Tm4bSms.php'); if (!(int)($this->_alert_update_quantity_active) OR empty($this->_new_order_numbers)) return ; if (is_object($params['product'])) $params['product'] = get_object_vars($params['product']); $product = $params['product']; $qty = (int)($params['product']['quantity_attribute'] ? $params['product']['quantity_attribute'] : $params['product']['stock_quantity']) - (int)($params['product']['quantity']); if ($qty <= (int)(Configuration::get('PS_LAST_QTIES'))) { $templateVars = array( '{last_qty}' => (int)(Configuration::get('PS_LAST_QTIES')), '{qty}' => $qty, '{product}' => strval($params['product']['name'])); $body = $this->_getTplBody(self::$_tpl_sms_files['name']['out_of_stock'].self::$_tpl_sms_files['ext']['out_of_stock'], $templateVars); } if (!empty($body)) { $sms = new Tm4bSms($this->_user, $this->_password, $this->_route, $this->_originator); $sms->msg = $body; $numbers = explode(self::__TM4B_NUMBER_DELIMITOR__, $this->_new_order_numbers); foreach ($numbers as $number) if ($number != '') $sms->addRecipient($number); $sms->Send($this->_simulation); } } public function getContent() { require_once (dirname(__FILE__).'/classes/Tm4bSms.php'); $this->_html = '