name = 'ant_alert'; $this->tab = 'administration'; $this->author = 'Antadis'; $this->version = '1.0'; $this->need_instance = 0; parent::__construct(); $this->displayName = $this->l('Alert for message'); $this->description = $this->l('Alert for message of technical error'); } 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'), ); foreach($hooks as $k => $v) { if(count(Db::getInstance()->ExecuteS(' SELECT `id_hook` FROM `'._DB_PREFIX_.'hook` WHERE `name` = "'.$k.'" LIMIT 1 ')) == 0) { Db::getInstance()->ExecuteS(' INSERT INTO `'._DB_PREFIX_.'hook` VALUES (DEFAULT, "'.$k.'", "'.$v[0].'", "'.$v[1].'", 0, 0) '); } } if(!parent::install() || !$this->registerHook('ant_alert')) { return FALSE; } # 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')); return true; } public function uninstall() { if(parent::uninstall() == false) { return false; } Configuration::deleteByName('ANT_ALTER_LIMIT'); Configuration::deleteByName('ANT_ALTER_HOURS'); Configuration::deleteByName('ANT_ALTER_DATESEND'); return true; } public function hookAnt_Alert($params) { global $cookie; $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)$alert->hours) { // prod $open_messages = Db::getInstance()->getRow(' SELECT COUNT(*) as total, MIN(ct.`date_add`) as `min_date` FROM '._DB_PREFIX_.'customer_thread ct WHERE ct.status = "open" AND ct.date_add >= "'.pSQL($alert->date_last_check).'" GROUP BY ct.id_contact HAVING COUNT(*) > 0 AND ct.id_contact ='.(int)$alert->id_contact ); $date_min = new Datetime($open_messages['min_date']); $minDiff = $date_min->diff($now); $hourLimitMin = (int)$alert->hours; $hourLimitMax = (int)$alert->hours+1; if ( (int)$open_messages['total'] >= (int)$alert->limit && (int)$minDiff->format("%H") >= $hourLimitMin && (int)$minDiff->format("%H") <= $hourLimitMax ) { // 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, $email)) { $alert->date_last_sent = date('Y-m-d H:i:s'); $alert->date_last_check = date('Y-m-d H:i:s'); $alert->save(); } } } elseif((int)$open_messages['total'] >= (int)$alert->limit && (int)$minDiff->format("%H") > $hourLimitMax) { $alert->date_last_check = date('Y-m-d H:i:s'); $alert->save(); } } } } }