525 lines
5.8 KiB
PHP
525 lines
5.8 KiB
PHP
<?php
|
||
|
||
|
||
|
||
// é
|
||
|
||
|
||
|
||
class Cadeau extends PluginsClassiques {
|
||
|
||
|
||
|
||
public $id;
|
||
|
||
public $image;
|
||
|
||
public $etat;
|
||
|
||
public $commande;
|
||
|
||
|
||
|
||
public $contenu = 29; // Les images devront obligatoirement être associées à ce contenu
|
||
|
||
public $montant = 129;
|
||
|
||
|
||
|
||
public $bddvars = array( "id", "image", "etat", "commande" );
|
||
|
||
public $table = "cadeau";
|
||
|
||
|
||
|
||
|
||
|
||
public function __construct() {
|
||
|
||
|
||
|
||
parent::__construct( "cadeau" );
|
||
|
||
}
|
||
|
||
|
||
|
||
public function charger( $id ) {
|
||
|
||
|
||
|
||
$sql = "
|
||
|
||
SELECT
|
||
|
||
*
|
||
|
||
FROM
|
||
|
||
`" . $this->table . "`
|
||
|
||
WHERE
|
||
|
||
id = " . intval( $id ) . "
|
||
|
||
";
|
||
|
||
|
||
|
||
return $this->getVars( $sql );
|
||
|
||
}
|
||
|
||
|
||
|
||
public function chargerAvecCommande( $idCommande ) {
|
||
|
||
|
||
|
||
$sql = "
|
||
|
||
SELECT
|
||
|
||
*
|
||
|
||
FROM
|
||
|
||
`" . $this->table . "`
|
||
|
||
WHERE
|
||
|
||
commande = " . intval( $idCommande ) . "
|
||
|
||
";
|
||
|
||
|
||
|
||
return $this->getVars( $sql );
|
||
|
||
}
|
||
|
||
|
||
|
||
public function chargerAvecRefCommande( $refCommande ) {
|
||
|
||
|
||
|
||
$commande = new Commande();
|
||
|
||
|
||
|
||
$sql = "
|
||
|
||
SELECT
|
||
|
||
cadeau.id as id,
|
||
|
||
cadeau.image as image,
|
||
|
||
cadeau.etat as etat,
|
||
|
||
cadeau.commande as commande
|
||
|
||
FROM
|
||
|
||
`" . $this->table . "` as cadeau
|
||
|
||
INNER JOIN
|
||
|
||
`" . $commande->table . "` as commande
|
||
|
||
ON
|
||
|
||
cadeau.commande = commande.id
|
||
|
||
WHERE
|
||
|
||
commande.ref = \"" . $refCommande . "\"
|
||
|
||
";
|
||
|
||
|
||
|
||
return $this->getVars( $sql );
|
||
|
||
}
|
||
|
||
|
||
|
||
public function init() {
|
||
|
||
|
||
|
||
$sql = "
|
||
|
||
CREATE TABLE IF NOT EXISTS `cadeau` (
|
||
|
||
`id` int(11) NOT NULL auto_increment,
|
||
|
||
`image` int(11) NOT NULL default '0',
|
||
|
||
`etat` int(11) NOT NULL default '0',
|
||
|
||
`commande` int(11) NOT NULL default '0',
|
||
|
||
PRIMARY KEY (`id`)
|
||
|
||
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
|
||
|
||
";
|
||
|
||
|
||
|
||
$this->query( $sql, true );
|
||
|
||
}
|
||
|
||
|
||
|
||
// Appelée lorsqu'une action est transmise (toujours donc)
|
||
|
||
public function action() {
|
||
|
||
|
||
|
||
$envAction = lireParam( 'action' , 'string' );
|
||
|
||
|
||
|
||
// Si l'action correspond au choix d'un cadeau, on enregistre le choix dans une session
|
||
|
||
if ( $envAction == 'choix_cadeau' ) {
|
||
|
||
|
||
|
||
$envCadeau = lireParam( 'cadeau' , 'int' );
|
||
|
||
|
||
|
||
$image = new Image( $envCadeau );
|
||
|
||
if ( $image->contenu == $this->contenu ) {
|
||
|
||
|
||
|
||
$_SESSION['thelia_choix_du_cadeau'] = intval( $envCadeau );
|
||
|
||
}
|
||
|
||
else {
|
||
|
||
|
||
|
||
if ( isset( $_SESSION['thelia_choix_du_cadeau'] ) ) {
|
||
|
||
|
||
|
||
unset( $_SESSION['thelia_choix_du_cadeau'] );
|
||
|
||
}
|
||
|
||
}
|
||
|
||
}
|
||
|
||
}
|
||
|
||
|
||
|
||
// Appelée lors de la création d’une commande
|
||
|
||
public function aprescommande( $commande ) {
|
||
|
||
|
||
|
||
$total = $commande->total( false, true );
|
||
|
||
|
||
|
||
// Si le montant de la commande est suffisant ( total TTC, sans frais de port avec application remise)
|
||
|
||
if ( $total >= $this->montant ) {
|
||
|
||
|
||
|
||
$imageChoisie = 0;
|
||
|
||
if ( isset( $_SESSION['thelia_choix_du_cadeau'] ) ) {
|
||
|
||
|
||
|
||
$imageChoisie = intval( $_SESSION['thelia_choix_du_cadeau'] );
|
||
|
||
}
|
||
|
||
|
||
|
||
$cadeau = new Cadeau();
|
||
|
||
|
||
|
||
$cadeau->image = $imageChoisie;
|
||
|
||
$cadeau->etat = 0;
|
||
|
||
$cadeau->commande = $commande->id;
|
||
|
||
|
||
|
||
$cadeau->add();
|
||
|
||
}
|
||
|
||
}
|
||
|
||
|
||
|
||
// Appelée lors d'un changement de statut d'une commande
|
||
|
||
public function statut( $commande, $ancienStatut ) {
|
||
|
||
|
||
|
||
// On envoi un mail pour prise en compte cadeau si la commande est payée
|
||
|
||
if ( $commande->statut == 2 ) {
|
||
|
||
|
||
|
||
$cadeau = new Cadeau();
|
||
|
||
|
||
|
||
// Un cadeau doit être associé à la commande
|
||
|
||
if ( $cadeau->chargerAvecCommande( $commande->id ) ) {
|
||
|
||
|
||
|
||
if ( $cadeau->etat == 0 ) {
|
||
|
||
|
||
|
||
$sujet = "Un cadeau est associé à la commande";
|
||
|
||
$messagehtml = "<h3>Cadeau Offert</h3>
|
||
|
||
<p>
|
||
|
||
La commande " . $commande->ref . " nécessite l'envoi d'un cadeau.
|
||
|
||
</p>";
|
||
|
||
|
||
|
||
Mail::envoyer(
|
||
|
||
Variable::lire("nomsite"), Variable::lire("emailfrom"),
|
||
|
||
Variable::lire("nomsite"), Variable::lire("emailfrom"),
|
||
|
||
$sujet,
|
||
|
||
$messagehtml, "");
|
||
|
||
|
||
|
||
$cadeau->etat = 1;
|
||
|
||
$cadeau->maj();
|
||
|
||
}
|
||
|
||
}
|
||
|
||
}
|
||
|
||
elseif( $commande->statut == 5 ) {
|
||
|
||
|
||
|
||
$cadeau = new Cadeau();
|
||
|
||
|
||
|
||
// Un cadeau doit être associé à la commande
|
||
|
||
if ( $cadeau->chargerAvecCommande( $commande->id ) ) {
|
||
|
||
|
||
|
||
if ( $cadeau->etat <= 1 ) {
|
||
|
||
|
||
|
||
$cadeau->etat = 3;
|
||
|
||
$cadeau->maj();
|
||
|
||
}
|
||
|
||
}
|
||
|
||
}
|
||
|
||
}
|
||
|
||
|
||
|
||
public function boucle( $texte, $args ) {
|
||
|
||
|
||
|
||
$commande = lireTag( $args, "commande", "int" );
|
||
|
||
$etat = lireTag( $args, "etat", "int" );
|
||
|
||
|
||
|
||
$retourner = "";
|
||
|
||
|
||
|
||
$sqlIdCommande = "";
|
||
|
||
if ( !empty( $commande ) ) {
|
||
|
||
|
||
|
||
$sqlIdCommande = "
|
||
|
||
AND
|
||
|
||
commande = " . $commande . "
|
||
|
||
";
|
||
|
||
}
|
||
|
||
|
||
|
||
$sqlEtat = "";
|
||
|
||
if ( !empty( $etat ) ) {
|
||
|
||
|
||
|
||
$sqlIdCommande = "
|
||
|
||
AND
|
||
|
||
etat = " . $etat . "
|
||
|
||
";
|
||
|
||
}
|
||
|
||
|
||
|
||
$sql = "
|
||
|
||
SELECT
|
||
|
||
*
|
||
|
||
FROM
|
||
|
||
`" . $this->table . "`
|
||
|
||
WHERE
|
||
|
||
1
|
||
|
||
" . $sqlIdCommande . "
|
||
|
||
" . $sqlEtat . "
|
||
|
||
";
|
||
|
||
|
||
|
||
foreach ( $this->query_liste( $sql ) as $ligne ) {
|
||
|
||
|
||
|
||
$temp = str_replace( "#ID", $ligne->id, $texte );
|
||
|
||
$temp = str_replace( "#IMAGE", $ligne->image, $temp );
|
||
|
||
$temp = str_replace( "#ETAT", $ligne->etat, $temp );
|
||
|
||
$temp = str_replace( "#COMMANDE", $ligne->commande, $temp );
|
||
|
||
|
||
|
||
$retourner .= $temp;
|
||
|
||
}
|
||
|
||
|
||
|
||
return $retourner;
|
||
|
||
}
|
||
|
||
|
||
|
||
public function traduireEtat( $etat, $langue = 1 ) {
|
||
|
||
|
||
|
||
$etats = array(
|
||
|
||
'0' => '' ,
|
||
|
||
'1' => '' ,
|
||
|
||
'2' => '' ,
|
||
|
||
'3' => '' ,
|
||
|
||
);
|
||
|
||
|
||
|
||
if ( $langue == 1 ) {
|
||
|
||
|
||
|
||
$etats = array(
|
||
|
||
'0' => 'attente paiement commande' ,
|
||
|
||
'1' => 'attente envoi' ,
|
||
|
||
'2' => 'envoyé' ,
|
||
|
||
'3' => 'annulé' ,
|
||
|
||
);
|
||
|
||
}
|
||
|
||
|
||
|
||
$libelle = "";
|
||
|
||
if ( isset( $etats[$etat] ) ) {
|
||
|
||
|
||
|
||
$libelle = $etats[$etat];
|
||
|
||
}
|
||
|
||
|
||
|
||
return $libelle;
|
||
|
||
}
|
||
|
||
}
|
||
|
||
|
||
|