diff --git a/mails/fr/ant_alert.html b/mails/fr/ant_alert.html index f3b72e9e..84472eae 100644 --- a/mails/fr/ant_alert.html +++ b/mails/fr/ant_alert.html @@ -2,7 +2,7 @@
-Vous avez reçu {limit} messages ou plus concernant des problèmes techniques en {hours}H. | +Vous avez reçu {limit} messages ou plus concernant {contact} en {hours}H. | ||||||||||
'.$this->l('ID').' | +'.$this->l('Contact').' | +'.$this->l('Limite par heures').' | +'.$this->l('Dernier envoi').' | +'.$this->l('Active').' | +'.$this->l('Action').' | +
---|---|---|---|---|---|
#'.$alert['id_alert'].' | +'.$current_contact['name'].' | +'.$alert['limit'].' / '.$alert['hours'].'h | +'.date('d/m/Y H:i',strtotime($alert['date_last_sent'])).' | +'.((int)$alert['enabled']?'':'').' | ++ + | +
La limite des messages est par heures et par type de contact.
+Ici un mail d\'alerte se déclenche après 1h si 50 messages ou plus, concernant les Retour/Echange, ont été envoyés.
+Une seule alerte par type de contact possible !
+ ' + ), + ), + 'actions' => array(), + 'actions-class' => 'text-right', + ) + ); + + + if (isset($alert)) { + $this->helperForm->_forms[0]['sections'][0]['title'] = "Editer une alerte"; + $this->helperForm->_forms[0]['sections'][0]['inputs'][] = array( + 'type' => 'hidden', + 'name' => 'id_alert', + 'value' => $alert->id, + 'class' => 'large' + ); + $this->helperForm->_forms[0]['sections'][0]['actions'] = array( + array( + 'type' => 'submit', + 'class' => 'btn-default', + 'name' => 'newAlert', + 'value' => $this->l('Nouvelle Alerte') + ), + array( + 'type' => 'submit', + 'class' => 'btn-primary', + 'name' => 'editAlert', + 'value' => $this->l('Editer l\'alerte') + ), + ); + } else { + $this->helperForm->_forms[0]['sections'][0]['title'] = "Ajouter une alerte"; + $this->helperForm->_forms[0]['sections'][0]['actions'] = array( + array( + 'type' => 'submit', + 'class' => 'btn-primary', + 'name' => 'addAlert', + 'value' => $this->l('Ajouter l\'alerte') + ), + ); + } + + $this->helperForm->renderForm(); + } + + public function _search($array, $key, $value) + { + $results = array(); + + if (is_array($array)) { + if (isset($array[$key]) && $array[$key] == $value) { + return $array; + } + foreach ($array as $subarray) { + $results = array_merge($results, $this->_search($subarray, $key, $value)); + } + } + return $results; + } + } \ No newline at end of file diff --git a/modules/ant_alert/ant_alert.php b/modules/ant_alert/ant_alert.php index efac6bdf..0891d61b 100644 --- a/modules/ant_alert/ant_alert.php +++ b/modules/ant_alert/ant_alert.php @@ -2,7 +2,9 @@ if (!defined('_PS_VERSION_')) exit; -class Ant_Alert extends Module +include_once(_PS_MODULE_DIR_.'/ant_alert/models/AntAlert.php'); + +class Ant_Alert extends Module { public function __construct() @@ -19,9 +21,25 @@ class Ant_Alert extends Module $this->description = $this->l('Alert for message of technical error'); } - public function install() + public function install() { - + # Add tables + $query = ' + CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'ant_alert` ( + `id_alert` INTEGER NOT NULL AUTO_INCREMENT, + `enabled` BOOL NOT NULL, + `id_contact` INTEGER NOT NULL, + `limit` INTEGER NOT NULL, + `hours` INTEGER NOT NULL, + `date_add` DATETIME NOT NULL, + `date_upd` DATETIME NOT NULL, + `date_last_sent` DATETIME NOT NULL, + PRIMARY KEY(`id_alert`,`id_contact`) + ) ENGINE=MyIsam DEFAULT CHARSET=utf8 + '; + if(!Db::getInstance()->Execute($query)) { + return false; + } $hooks = array( 'ant_alert' => array('Ant Alert', 'Called when a message of technical error is sent'), @@ -46,9 +64,9 @@ class Ant_Alert extends Module } # Set default configuration values - Configuration::updateValue('ANT_ALTER_LIMIT', 10); - Configuration::updateValue('ANT_ALTER_HOURS', 24); - Configuration::updateValue('ANT_ALTER_DATESEND', date('Y-m-d H:i:s')); + // Configuration::updateValue('ANT_ALTER_LIMIT', 10); + // Configuration::updateValue('ANT_ALTER_HOURS', 24); + // Configuration::updateValue('ANT_ALTER_DATESEND', date('Y-m-d H:i:s')); return true; } @@ -69,38 +87,44 @@ class Ant_Alert extends Module public function hookAnt_Alert($params) { global $cookie; - $date = new Datetime(Configuration::get('ANT_ALTER_DATESEND')); - $now = new Datetime; + $alert = AntAlert::getAlertByContact((int)$params['id_contact']); + if($alert && (int)$alert->enabled == 1) { + $date = new Datetime($alert->date_last_sent); + $now = new Datetime; - $dteDiff = $date->diff($now); - if ((int)$dteDiff->format("%H") >= (int)Configuration::get('ANT_ALTER_HOURS')) { // prod - $open_messages = Db::getInstance()->getRow(' - SELECT COUNT(*) as total - FROM '._DB_PREFIX_.'customer_thread ct - WHERE ct.status = "open" - GROUP BY ct.id_contact HAVING COUNT(*) > 0 AND ct.id_contact = 4' - ); - if ((int)$open_messages['total'] >= (int)Configuration::get('ANT_ALTER_LIMIT')) { - // dev - // $to = array('marion@antadis.com'); - // prod - $to = array( - 'frederic@bebeboutik.com', - 'jacques@bebeboutik.com', - 'valentin@bebeboutik.com', + $dteDiff = $date->diff($now); + if ((int)$dteDiff->format("%H") >= (int)$alert->hours) { // prod + + $open_messages = Db::getInstance()->getRow(' + SELECT COUNT(*) as total + FROM '._DB_PREFIX_.'customer_thread ct + WHERE ct.status = "open" + GROUP BY ct.id_contact HAVING COUNT(*) > 0 AND ct.id_contact ='.(int)$alert->id_contact ); - $data = array( - '{limit}' => (int)Configuration::get('ANT_ALTER_LIMIT'), - '{hours}' => (int)Configuration::get('ANT_ALTER_HOURS'), - ); - foreach ($to as $email) { - if(Mail::Send((int)$cookie->id_lang, 'ant_alert', 'Alert error', $data, $to)) { - Configuration::updateValue('ANT_ALTER_DATESEND', date('Y-m-d H:i:s')); + if ((int)$open_messages['total'] >= (int)$alert->limit) { + // dev + $to = array('marion@antadis.com'); + // prod + // $to = array( + // 'frederic@bebeboutik.com', + // 'jacques@bebeboutik.com', + // 'valentin@bebeboutik.com', + // ); + $contact = new Contact((int)$alert->id_contact,2); + $data = array( + '{limit}' => (int)$alert->limit, + '{hours}' => (int)$alert->hours, + '{contact}' => $contact->name, + ); + foreach ($to as $email) { + if(Mail::Send(2, 'ant_alert', 'Alert error', $data, $to)) { + $alert->date_last_sent = date('Y-m-d H:i:s'); + $alert->save(); + } } } } } - } } diff --git a/modules/ant_alert/models/AntAlert.php b/modules/ant_alert/models/AntAlert.php new file mode 100644 index 00000000..d356afd9 --- /dev/null +++ b/modules/ant_alert/models/AntAlert.php @@ -0,0 +1,119 @@ + 'isUnsignedId', + 'enabled' => 'isBool', + 'id_contact' => 'isUnsignedId', + 'limit' => 'isInt', + 'hours' => 'isInt', + 'date_add' => 'isDate', + 'date_upd' => 'isDate', + 'date_last_sent' => 'isDate', + ); + + protected $table = 'ant_alert'; + protected $identifier = 'id_alert'; + + + public function getFields() + { + parent::validateFields(); + + $fields['id_alert'] = (int)$this->id_alert; + $fields['enabled'] = (int)$this->enabled; + $fields['id_contact'] = (int)$this->id_contact; + $fields['limit'] = (int)$this->limit; + $fields['hours'] = (int)$this->hours; + $fields['date_add'] = pSQL($this->date_add); + $fields['date_upd'] = pSQL($this->date_upd); + $fields['date_last_sent'] = pSQL($this->date_last_sent); + + return $fields; + } + + public function delete() + { + return true; + } + + /** + * Get demands + * @param $states array get only demands in specific states + * @return Array Groups + */ + public static function getAlerts($enabled = true, $where = false) + { + if($where){ + return Db::getInstance()->executeS(' + SELECT * + FROM `'._DB_PREFIX_.'ant_alert` at + WHERE '.$where.' + '.($enabled ? ' AND at.`enabled` = 1' : '').' + '); + } else { + return Db::getInstance()->executeS(' + SELECT * + FROM `'._DB_PREFIX_.'ant_alert` at + WHERE 1 + '.($enabled ? ' AND at.`enabled` = 1' : '').' + '); + } + } + + public function save($nullValues = false, $autodate = true) + { + if (parent::save($nullValues,$autodate)) { + return true; + } + } + + public static function getAlertByContact($id_contact) + { + $id = Db::getInstance()->getValue(" + SELECT `id_alert` + FROM `"._DB_PREFIX_."ant_alert` + WHERE `id_contact` = " .(int)$id_contact + ); + if($id) { + return new AntAlert((int)$id); + } + return false; + } + + public static function getAlreadyUsedContacts() + { + $id_contacts = array(); + foreach(Db::getInstance()->executeS(" + SELECT DISTINCT `id_contact` + FROM `"._DB_PREFIX_."ant_alert`" + ) as $row){ + $id_contacts[(int)$row['id_contact']] = (int)$row['id_contact']; + } + + return $id_contacts; + } + + public static function isExistAndEnabled($id_contact) + { + return Db::getInstance()->getValue(" + SELECT `id_alert` + FROM `"._DB_PREFIX_."ant_alert` + WHERE `id_contact` = " .(int)$id_contact." + AND `enabled` = 1" + ); + } +} + diff --git a/modules/ant_support_form/support.php b/modules/ant_support_form/support.php index 792c53b3..a28fdbfa 100644 --- a/modules/ant_support_form/support.php +++ b/modules/ant_support_form/support.php @@ -239,7 +239,7 @@ if (Tools::isSubmit('submitMessage')) { else $errors[] = Tools::displayError('An error occurred while sending message.'); } - + Module::hookExec('ant_alert', array('id_contact' => (int)($id_contact))); Module::hookExec('ant_alerthack', array( 'email' => $from, 'id_customer' => (isset($customer->id)?$customer->id:false), diff --git a/override/controllers/ContactController.php b/override/controllers/ContactController.php index 2a6d26db..2810ff41 100755 --- a/override/controllers/ContactController.php +++ b/override/controllers/ContactController.php @@ -202,7 +202,7 @@ class ContactController extends ContactControllerCore { $this->errors[] = Tools::displayError('An error occurred while sending message.'); } - Module::hookExec('ant_alert', array()); + Module::hookExec('ant_alert', array('id_contact' => (int)($id_contact))); Module::hookExec('ant_alerthack', array( 'email' => $from, 'id_customer' => (isset($customer->id)?$customer->id:false),