name = 'mdtossend';
$this->tab = 'checkout';
$this->version = '1.3.0';
$this->author = 'MDWeb';
$this->module_key = '929fa9fa0938030f19c5002cbcf3f385';
$this->need_instance = 0;
$this->config = Configuration::get('MD_PDF_CGV');
parent::__construct();
$this->displayName = $this->l('Terms of Sale Send');
$this->description = $this->l('Send your Terms of Sale to your customers when they order');
/* Backward compatibility */
if (_PS_VERSION_ < '1.5')
require(_PS_MODULE_DIR_.$this->name.'/backward_compatibility/backward.php');
if (!isset($this->config) || empty($this->config))
$this->warning = $this->l('Please configure '.$this->displayName.' to use it.');
}
public function install()
{
if (parent::install() == false || !$this->installDb())
return false;
//Procedure d'installation pour Prestashop 1.5 et plus
if (version_compare(_PS_VERSION_, 1.5, '>'))
{
if (!$this->registerHook('actionObjectCmsUpdateAfter'))
return false;
if (!$this->registerHook('actionValidateOrder'))
return false;
if (!$this->registerHook('displayPDFInvoice'))
return false;
// Copie du fichier personnalisé Mail en sauvegardant celui d'origine
if (is_file( _PS_ROOT_DIR_.'/override/classes/Mail.php'))
{
if (!copy ( _PS_ROOT_DIR_.'/override/classes/Mail.php', _PS_ROOT_DIR_.'/modules/mdtossend/install/Mail_save.php' ))
return false;
}
else
{
if (!copy ( _PS_ROOT_DIR_.'/modules/mdtossend/install/Mail.php', _PS_ROOT_DIR_.'/override/classes/Mail.php' ))
return false;
}
}
//Procedure d'installation pour Prestashop 1.4
if (version_compare(_PS_VERSION_, 1.4, '>=') && version_compare(_PS_VERSION_, 1.5, '<'))
{
if (!$this->registerHook('newOrder'))
return false;
//Create hook : actionObjectCmsUpdateAfter
$sql = 'INSERT INTO `'._DB_PREFIX_.'hook` (
`id_hook` ,
`name` ,
`title` ,
`description` ,
`position`
)
VALUES (
NULL , \'actionObjectCmsUpdateAfter\', \'actionObjectCmsUpdateAfter\', NULL , 0);';
if (!Db::getInstance()->execute($sql))
{
if (!Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.'hook WHERE name = \'actionObjectCmsUpdateAfter\''))
return false;
if (!Db::getInstance()->execute($sql))
return false;
}
if (!$this->registerHook('actionObjectCmsUpdateAfter'))
return false;
// Copie du fichier personnalisé CMS en sauvegardant celui d'origine
if (is_file( _PS_ROOT_DIR_.'/override/classes/CMS.php'))
{
if (!copy ( _PS_ROOT_DIR_.'/override/classes/CMS.php', _PS_ROOT_DIR_.'/modules/mdtossend/install/CMS_save.php' ))
return false;
}
else
{
if (!copy ( _PS_ROOT_DIR_.'/modules/mdtossend/install/CMS.php', _PS_ROOT_DIR_.'/override/classes/CMS.php' ))
return false;
}
}
return true;
}
private function installDb()
{
$sql = 'CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'mdtossend` (
`filename` varchar(250) NOT NULL,
`id_lang` int(2) NOT NULL,
`datetime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`filename`)
) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8;';
Db::getInstance()->execute($sql);
// Create history for PDF Invoice if Prestashop version is up to 1.5
if (version_compare(_PS_VERSION_, 1.5, '>'))
{
$sql_history = 'CREATE TABLE IF NOT EXISTS '._DB_PREFIX_.'mdtossend_history (
`id_cms` INT NOT NULL ,
`id_lang` INT NOT NULL ,
`date_add` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
`meta_title` VARCHAR(255) NOT NULL ,
`content` LONGTEXT NOT NULL ,
PRIMARY KEY ( `id_cms` , `id_lang` , `date_add` )
) ENGINE = '._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8 ;';
Db::getInstance()->execute($sql_history);
}
return true;
}
public function uninstall()
{
if (!parent::uninstall() || !$this->uninstallDb())
return false;
return true;
}
public function uninstallDb()
{
if (!Configuration::deleteByName('MD_PDF_CGV')
|| !Configuration::deleteByName('MD_CGV_INVOICE')
|| !Configuration::deleteByName('MD_CGV_INVOICE_WITHDRAWAL')
|| !Configuration::deleteByName('MD_CGV_WITHDRAWALFORM'))
return false;
if (!Db::getInstance()->execute('DROP TABLE IF EXISTS '._DB_PREFIX_.'mdtossend'))
return false;
if (version_compare(_PS_VERSION_, 1.5, '>'))
if (!Db::getInstance()->execute('DROP TABLE IF EXISTS '._DB_PREFIX_.'mdtossend_history'))
return false;
return true;
}
public function hookActionValidateOrder($params)
{
// if ((Configuration::get('MD_CGV_INVOICE') == 0) || (version_compare(_PS_VERSION_, 1.4, '>=') && version_compare(_PS_VERSION_, 1.5, '<')))
if (version_compare(_PS_VERSION_, 1.4, '>=') && version_compare(_PS_VERSION_, 1.5, '<'))
{
$iso = Language::getIsoById((int)$params['order']->id_lang);
$mail_iso = $iso;
$pdf = '';
if (version_compare(_PS_VERSION_, 1.5, '>'))
{
$id_shop = $this->context->shop->id;
$pdf = dirname(__FILE__).'/pdf/cgv_'.$iso.'_'.$id_shop.'.pdf';
if (!is_file($pdf))
{
$iso = Language::getIsoById((int)Configuration::get('PS_LANG_DEFAULT'));
$pdf = dirname(__FILE__).'/pdf/cgv_'.$iso.'_'.$id_shop.'.pdf';
}
}
else
{
$pdf = dirname(__FILE__).'/pdf/cgv_'.$iso.'.pdf';
if (!is_file($pdf))
{
$iso = Language::getIsoById((int)Configuration::get('PS_LANG_DEFAULT'));
$pdf = dirname(__FILE__).'/pdf/cgv_'.$iso.'.pdf';
}
}
if (!in_array($mail_iso, array('fr', 'en')))
$mail_iso = Language::getIsoById((int)Configuration::get('PS_LANG_DEFAULT'));
if (!in_array($mail_iso, array('fr', 'en')))
$mail_iso = 'en';
if (is_file($pdf) && $mail_iso)
{
$pdf = array('content' => Tools::file_get_contents($pdf),
'name' => 'cgv.pdf',
'mime' => 'application/pdf');
Mail::Send(Language::getIdByIso($mail_iso),
'conditions',
$this->l('TOS for our shop', false, (int)Language::getIdByIso($mail_iso)),
array(),
pSQL($params['customer']->email),
null,
null,
null,
$pdf,
null,
dirname(__FILE__).'/mails/');
}
if (Configuration::get('MD_CGV_INVOICE_WITHDRAWAL') == 1 && $file = Configuration::get('MD_CGV_WITHDRAWALFORM'))
{
$pdf = dirname(__FILE__).'/form/'.$file;
if (is_file($pdf) && $mail_iso)
{
$pdf = array('content' => Tools::file_get_contents($pdf),
'name' => 'withdrawal_form.pdf',
'mime' => 'application/pdf');
Mail::Send(Language::getIdByIso($mail_iso),
'formulaire',
$this->l('Withdrawal form'),
array(),
pSQL($params['customer']->email),
pSQL($params['customer']->firstname).' '.pSQL($params['customer']->lastname),
null,
null,
$pdf,
null,
dirname(__FILE__).'/mails/');
}
}
}
return true;
}
/*Hook to Add Extra Content into the PDF Invoice
Add in V1.1*/
public function hookDisplayPDFInvoice($params)
{
if ((Configuration::get('MD_CGV_INVOICE') == 1) && (version_compare(_PS_VERSION_, 1.5, '>')))
{
$order_details = Db::getInstance()->getRow('select id_lang,date_add FROM '._DB_PREFIX_.'orders WHERE id_order = '.$params['object']->id_order);
$id_lang = $order_details['id_lang'];
$date_order = $order_details['date_add'];
$cms = Db::getInstance()->getRow('SELECT content,meta_title FROM '._DB_PREFIX_.'mdtossend_history
WHERE id_cms ='.Configuration::get('MD_PDF_CGV').'
AND id_lang = '.$id_lang.' AND \''.$date_order.'\' >= date_add ORDER BY date_add DESC ');
$title_cms = $cms['meta_title'];
$content_cms = $cms['content'];
//Si title_cms ou content_cms ne renvoie rien on ne rajoute rien au PDF. Par exemple si une commande a été passée avant le premier historique.
if ($title_cms || $content_cms)
return '
'.$this->displayName.'| '.$this->l('Filename').' | '.$this->l('Language').' | '.$this->l('Date / Time').' | '.$this->l('Remove').' |
|---|---|---|---|
| '.$file['filename'].' | '; $history .= ''.$lang['name'].' | '.$file['datetime'].' | '; $history .= '