bebeboutik/modules/privatesale_mail_delay/AdminPrivateSaleMailDelay.php
2017-03-30 12:10:32 +02:00

287 lines
14 KiB
PHP
Executable File

<?php
if(!defined('_PS_VERSION_')) {
exit;
}
include_once(_PS_ROOT_DIR_.'/modules/privatesales/Sale.php');
class AdminPrivateSaleMailDelay extends AdminTab {
public $_html = '';
public function postProcess() {
global $cookie;
if(Tools::isSubmit('submitEnvoi') || Tools::isSubmit('confirmationEnvoi') ) {
$id_category = Tools::getValue('category', 0);
$type_envoi = Tools::getValue('type_envoi', 0);
$contenu_mail = Tools::getValue('mail_ps_delay');
$montant_remise = Tools::getValue('montant_remise', 0);
$objet_mail = Tools::getValue('objet_mail');
$envoi_remise = Tools::getValue('envoi_remise', 0);
$prefix_remise = Tools::getValue('prefix_remise');
$status_pending = Tools::getValue('status_pending', 0);
if($id_category == 0 || !Validate::isInt($montant_remise)) {
echo '<div class="error">Vérifier le formulaire</div>';
} else {
$sale = Sale::getSaleFromCategory((int) $id_category);
$category = new Category($id_category, $cookie->id_lang);
$id_status = $status_pending? array(19):NULL;
$orders_single = $sale->getOrdersFromSale($id_status, TRUE);
$orders_multiple = $sale->getOrdersFromSale($id_status, FALSE);
if($type_envoi == 0) {
$orders = array_merge($orders_multiple, $orders_single);
} else if($type_envoi == 1) {
$orders = $orders_multiple;
} else if($type_envoi == 2) {
$orders = $orders_single;
}
$orders = array_unique($orders);
if(Tools::isSubmit('submitEnvoi')) {
$this->displayConfirmationForm($category, $type_envoi, $contenu_mail, count($orders), $sale, $objet_mail, $envoi_remise, $montant_remise, $prefix_remise, $status_pending);
} else if(Tools::isSubmit('confirmationEnvoi')) {
$contenu_mail = str_replace("%marque%", $category->name , $contenu_mail);
$mail = 0;
$mails_errors = array();
foreach ($orders as $key => $order) {
$data = new Order($order);
if($envoi_remise == 1){
$params = array(
'id_customer' => $data->id_customer,
'currency' => 1,
'description' => Configuration::get('MAIL_PS_DESC'),
'value' => (int) $montant_remise,
'prefix' => $prefix_remise
);
$discount = $this->_register_discount($params);
$contenu_mail = str_replace("%montant%", (int)$montant_remise , $contenu_mail);
$contenu_mail = str_replace("%discount%", $discount->name , $contenu_mail);
}
$customer = new Customer($discount->id_customer);
$send = Mail::Send(
$cookie->id_lang,
'ps_delay',
$objet_mail,
array(
'{message}' => $contenu_mail
),
$customer->email,
$customer->firstname.' '.$customer->lastname,
strval(Configuration::get('PS_SHOP_EMAIL')),
strval(Configuration::get('PS_SHOP_NAME')),
NULL,
NULL,
dirname(__FILE__).'/mails/'
);
$send = true;
if(!$send){
$mails_errors[] = $order->id;
}
$mail++;
}
if( count($mails_errors) > 0) {
echo '<div class="error">Soucis sur les commandes ' . implode(',', $mails_errors) .'</div>';
}
echo '<div class="conf">Envoi réussi : '. $mail.' mails(s) envoyés</div>';
}
}
}
}
private function _register_discount($params) {
$discount = new Discount();
$discount->id_customer = $params['id_customer'];
$discount->id_discount_type = 2;
$discount->cumulable = 1;
$discount->cumulable_reduction = 1;
$discount->minimal = 0;
$discount->include_tax = 0;
$discount->cart_display = 1;
$discount->active = 1;
$discount->quantity = 1;
$discount->quantity_per_user = 1;
$discount->date_from = date('Y-m-d H:i:s');
$discount->date_to = date('Y-m-d H:i:s', time() + 365 * 86400);
$name = $params['prefix'] . '-' . Tools::passwdGen(8);
if(Discount::discountExists($name)) {
// It should not happen, but who knows...
$name = $params['prefix'] . '-' . Tools::passwdGen(8);
}
$discount->name = $name;
$title[2] = $params['description'];
$discount->description = $title;
if(isset($params['value']) && isset($params['currency'])) {
$discount->id_currency = $params['currency'];
$discount->value = $params['value'];
}
$discount->add(true, false, array(1));
// assoc only accueil catégorie
return $discount;
}
public function displayConfirmationForm($category, $type_envoi, $contenu_mail, $nb_envoi, $sale, $objet_mail, $envoi_remise, $montant_remise = 0, $prefix_remise, $status_pending=0) {
if($type_envoi == 0) {
$type_envoi_text = "Toutes";
} else if($type_envoi == 1) {
$type_envoi_text = "Multi";
} else if($type_envoi == 2) {
$type_envoi_text = "Simple";
}
$output .= '<form action="'.Tools::safeOutput($_SERVER['REQUEST_URI']).'" method="post" enctype="multipart/form-data">
<fieldset><legend><img src="/modules/privatesale_mail_delay/logo.gif" alt="" title="" />Confirmation envoi</legend>';
$output.= '<input type="hidden" name="category" value="'.$category->id.'"/>';
$output.= '<input type="hidden" name="type_envoi" value="'.$type_envoi.'"/>';
$output.= '<input type="hidden" name="mail_ps_delay" value="'.$contenu_mail.'"/>';
$output.= '<input type="hidden" name="objet_mail" value="'.$objet_mail.'"/>';
$output.= '<input type="hidden" name="envoi_remise" value="'.(int)$envoi_remise.'"/>';
$output.= '<input type="hidden" name="montant_remise" value="'.(int)$montant_remise.'"/>';
$output.= '<input type="hidden" name="prefix_remise" value="'. (string)$prefix_remise.'"/>';
$output.= '<input type="hidden" name="status_pending" value="'. (string)$status_pending.'"/>';
$output .= '<p class="conf">Vous allez envoyer '. $nb_envoi .' mails pour un retard concernant la vente '. $category->name .'
<br /> Segmentation des commandes : '. $type_envoi_text .' '.($status_pending?'(en attente)':'').'</p>
<center>
<input type="submit" value="Confirmer l\'envoi" name="confirmationEnvoi" class="button">
</center>
</fieldset>
</form> <br /><br />';
echo $output;
}
public function display(){
global $cookie;
$mail_delay_sql = 'SELECT value FROM ps_configuration WHERE name = "MAIL_PS_DELAY"';
$mail_delay = DB::getInstance()->getValue($mail_delay_sql);
$montant = Configuration::get('REMISE_PS_DELAY');
$output .= '
<form action="'.Tools::safeOutput($_SERVER['REQUEST_URI']).'" method="post" enctype="multipart/form-data">
<fieldset><legend><img src="/modules/privatesale_mail_delay/logo.gif" alt="" title="" />'.$this->l('Alerte mail').'</legend>
<label>'.$this->l('Choisir une vente :').'</label>
<div class="margin-form">
<select class="chosen-select" id="category" name="category">
<option value=""></option>
';
foreach(Db::getInstance()->ExecuteS('
SELECT c.`id_category`, l.`name`
FROM `'._DB_PREFIX_.'category_lang` l
LEFT JOIN `'._DB_PREFIX_.'category` c
ON c.`id_category` = l.`id_category`
WHERE l.`id_lang` = '.(int) $cookie->id_lang.'
AND c.`id_parent` = 1
ORDER BY id_category DESC
') as $row) {
$output .= '<option value="'.$row['id_category'].'">'.$row['id_category'].' - '.$row['name'].'</option>';
}
$output .= '</select>
</div>
<label>Type de commande</label>
<div class="margin-form">
<input type="radio" id="all" value="0" name="type_envoi" checked /> <label for="all" style="float:none">Toutes</label>
<input type="radio" id="multi" value="1" name="type_envoi" /> <label for="multi" style="float:none">Multi</label>
<input type="radio" id="simple" value="2" name="type_envoi" /> <label for="simple" style="float:none">Simple</label>
</div>';
$output .= '<label>Uniquement en attente</label>
<div class="margin-form">
<input type="radio" id="status_pending_no" value="0" name="status_pending" checked /> <label for="status_pending_no" style="float:none">Non</label>
<input type="radio" id="status_pending_yes" value="1" name="status_pending" /> <label for="status_pending_yes" style="float:none">Oui</label>
</div>';
$output .= '
<hr >
<label>Ajoutez une remise ?</label>
<div class="margin-form">
<input type="radio" id="remise_non" value="0" name="envoi_remise" checked /> <label for="remise_non" style="float:none">Non</label>
<input type="radio" id="remise_oui" value="1" name="envoi_remise" /> <label for="remise_oui" style="float:none">Oui</label>
<p style="color:red" class="info">Si vous cochez non, la variable %montant% et %value% ne doit pas être présente dans le mail ci-dessous</p>
</div>
<label>Si oui, montant de la remise</label>
<div class="margin-form">
<input type="text" id="montant" name="montant_remise" value="'. $montant .'" size="5"> €
</div>
<label>Prefix bon</label>
<div class="margin-form">
<input type="text" id="prefix_remise" name="prefix_remise" />
<p class="info">Exemple : TEST pour avoir un bon sous la forme TEST-AAAAA </p>
</div>
<hr >
<label>Objet du mail</label>
<div class="margin-form">
<input type="text" id="objet_mail" value="" placeholder="Remplir le sujet du mail" size="60" name="objet_mail" />
</div>
<label>Mail</label>
<div class="margin-form">
<textarea class="rte" cols="30" rows="10" name="mail_ps_delay">'. $mail_delay .'</textarea>
<p>
Les données suivantes seront remplacés avant l\'envoi du mail :
<ol>
<li>%montant% : Montant de la remise</li>
<li>%discount% : Code de réduction</li>
<li>%marque% : Nom de la vente</li>
</ol>
</p>
</div>
<center>
<input type="submit" name="submitEnvoi" value="'.$this->l('Envoyer le mail').'" class="button" />
<br />
<p style="float:right; font-size:12px; color:#812143; font-weight:bold; ">Nb : Un récapitulatif de l\'envoi sera disponible avant le définitif</p>
</center>
</fieldset>
</form>';
// TinyMCE
global $cookie;
$iso = Language::getIsoById((int)($cookie->id_lang));
$isoTinyMCE = (file_exists(_PS_ROOT_DIR_.'/js/tiny_mce/langs/'.$iso.'.js') ? $iso : 'en');
$ad = dirname($_SERVER["PHP_SELF"]);
$output.= '
<script type="text/javascript">
var iso = \''.$isoTinyMCE.'\' ;
var pathCSS = \''._THEME_CSS_DIR_.'\' ;
var ad = \''.$ad.'\' ;
</script>
<script type="text/javascript" src="'.__PS_BASE_URI__.'js/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript" src="'.__PS_BASE_URI__.'js/tinymce.inc.js"></script>';
$output.= '<link type="text/css" rel="stylesheet" href="'._MODULE_DIR_.'bulkupdate/chosen.min.css" />';
$output.= '<script type="text/javascript" src="'._MODULE_DIR_.'bulkupdate/chosen.jquery.min.js"></script>';
$output.= '<script type="text/javascript">
$(function() {
$(".chosen-select").chosen(
{
allow_single_deselect:true,
placeholder_text_single : "Choisir une vente",
no_results_text : "Aucun résultat",
enable_split_word_search : true,
search_contains : true,
}
);
});
</script>';
echo $output;
}
}