Valider d3518c83 rédigé par tcharlss@hotmail.fr's avatar tcharlss@hotmail.fr
Parcourir les fichiers

notifications (2/2) ( suite r82135 )

Pipelines :
- post_edition :  on n'en a plus besoin pour envoyer les notifs => à la trappe.
- affiche_gauche : le plugin médias fait déjà le boulot, et elle n'est de toute façon plus utilisée => à la trappe.

Fonctions dans inc/commandes :
- Ajout d'une fonction pour traiter l'envoi des emails de notifications d'une commande.
Je ne touche pas à la fonction commandes_envoyer_notification ( elle semble orpheline et mes tests se sont révélés infructueux, mais dans le doute...).
- creer_commande_encours : pour la création de la commande, il y a déjà action/editer_commande qui fait tout le boulot. On s'en sert donc, notamment pour bénéficier de l'envoi des notifs.
parent dcb11bbd
Chargement en cours
Chargement en cours
Chargement en cours
Chargement en cours
+12 −95
Numéro de ligne d'origine Numéro de ligne de diff Ligne de diff
@@ -3,14 +3,22 @@
// Sécurité
if (!defined('_ECRIRE_INC_VERSION')) return;

// La CSS pour une commande

/**
 * Insertion de la feuille de style CSS sur les pages publiques
 * 
 */
function commandes_insert_head_css($flux){
	$css = find_in_path('css/commandes.css');
	$flux .= "<link rel='stylesheet' type='text/css' media='all' href='$css' />\n";
	return $flux;
}

// Supprimer toutes les commandes en cours qui sont trop vieilles

/**
 * Supprimer toutes les commandes en cours qui sont trop vieilles
 * 
 */
function commandes_optimiser_base_disparus($flux){
	include_spip('inc/config');
	// On cherche la date depuis quand on a le droit d'avoir fait la commande (par défaut 1h)
@@ -35,31 +43,7 @@ function commandes_optimiser_base_disparus($flux){


/**
 * Ajouter une boite sur la fiche de commande
 *
 * @param string $flux
 * @return string
 */
function commandes_affiche_gauche($flux) {

	if (
		$flux['args']['exec'] == 'commande_edit'
		AND $table = preg_replace(",_edit$,","",$flux['args']['exec'])
		AND $type = objet_type($table)
		AND $id_table_objet = id_table_objet($type)
		AND ($id = intval($flux['args'][$id_table_objet]))
		AND (autoriser('modifier', 'commande', 0))
	) {
		//un test pour todo ajouter un objet (produit,document,article,abonnement,rubrique ...)
		$flux['data'] .= recuperer_fond('prive/objets/editer/colonne_document',array('objet'=>$type,'id_objet'=>$id));
	}

	return $flux;
}


/**
 * formulaires dates sur la fiche d'une commande
 * formulaires de dates sur la fiche d'une commande
 *
 * @param string $flux
 * @return string
@@ -76,7 +60,7 @@ function commandes_affiche_milieu($flux) {
		$texte = recuperer_fond('prive/squelettes/contenu/commande_affiche_milieu',array('id_commande'=>$id_commande));
	}

	if ($texte) {
	if (isset($texte)) {
		if ($p=strpos($flux['data'],"<!--affiche_milieu-->"))
			$flux['data'] = substr_replace($flux['data'],$texte,$p,0);
		else
@@ -87,7 +71,6 @@ function commandes_affiche_milieu($flux) {
}



/**
 * accueil : liste des commandes en attente de validation
 *
@@ -142,70 +125,4 @@ function commandes_affiche_auteurs_interventions($flux) {
}


/**
 * Traitements après l'édition d'une commande
 * Quand on  modifie le satut d'une commande :
 * - Mettre à jour les dates de paiement et d'envoi si par mégarde ça n'a pas été fait manuellement
 * - Envoyer les notifications 
 *
 * @param array $flux
 *     $flux['args'][x] = arguments (table, id_objet, action, statut_ancien, date_ancienne, id_parent_ancien)
 *     $flux['data'][x] = champs modifiés (statut, date_paiement etc.)
 * @return array $flux
**/
function commandes_post_edition($flux){

	if (
		($action = $flux['args']['action']) == 'instituer' // action instituer
		and ($table = $flux['args']['table']) == table_objet_sql('commande') // on institue une commande
		and ($statut_ancien = $flux['args']['statut_ancien']) != ($statut = $flux['data']['statut']) // le statut a été modifié
		and $id_commande = $flux['args']['id_objet'] // on a bien un identifiant pour la commande
	) {

		// Mise à jour des dates de paiement ou d'envoi si ça n'a pas été fait manuellement
		if (
			in_array($statut, array('paye','envoye'))
			and $maj = sql_getfetsel('maj', table_objet_sql('commande'), "id_commande=".sql_quote($id_commande) )
			and $date_paiement = sql_getfetsel('date_paiement', table_objet_sql('commande'), "id_commande=".sql_quote($id_commande) )
			and $date_envoi = sql_getfetsel('date_envoi', table_objet_sql('commande'), "id_commande=".sql_quote($id_commande) )
			and include_spip('action/editer_objet')
		) {
			if (
				$statut == 'paye'
				and intval($date_paiement) == 0
			){
				objet_modifier('commande', $id_commande, array('date_paiement'=>$maj));
			}
			if (
				$statut == 'envoye'
				and intval($date_envoi) == 0
			){
				objet_modifier('commande', $id_commande, array('date_envoi'=>$maj));
			}
		}

		// Envoi des notifications
		if (
			include_spip('inc/config')
			and $config = lire_config('commandes')
			and $quand = $config['quand'] ? $config['quand'] : array()
			and ($config['activer']) // les notifications sont activées
			and (in_array($statut, $quand)) // le nouveau statut est valide pour envoyer une notification
			and ($notifications = charger_fonction('notifications', 'inc', true)) // la fonction est bien chargée
		) {
			// Déterminer l'expediteur
			$options = array();
			if( $config['expediteur'] != "facteur" )
				$options['expediteur'] = $config['expediteur_'.$config['expediteur']];

			// Envoyer au vendeur et optionnellement au client
			$notifications('commande_vendeur', $id_commande, $options);
			if($config['client'])
				$notifications('commande_client', $id_commande, $options);
		}
	}

	return($flux);
}

?>
+59 −32
Numéro de ligne d'origine Numéro de ligne de diff Ligne de diff
@@ -40,39 +40,18 @@ function creer_commande_encours(){

	$champs = array(
		'reference' => $fonction_reference($id_auteur),
		'id_auteur' => $id_auteur,
		'date' => date('Y-m-d H:i:s'),
		'statut' => 'encours'
	);
	
	// Envoyer aux plugins avant insertion
	$champs = pipeline('pre_insertion',
		array(
			'args' => array(
				'table' => 'spip_commandes',
			),
			'data' => $champs
		)
	);
	$id_commande = sql_insertq('spip_commandes', $champs);
	// Envoyer aux plugins après insertion
	pipeline('post_insertion',
		array(
			'args' => array(
				'table' => 'spip_commandes',
				'id_objet' => $id_commande
			),
			'data' => $champs
		)
		'id_auteur' => $id_auteur
	);

	// Création de la commande
	include_spip('action/editer_commande');
	$id_commande = commande_inserer(null,$champs);
	session_set('id_commande', $id_commande);

	return $id_commande;
}



/**
 * Suppression d'une ou plusieurs commandes
 * et de ses données associées
@@ -174,4 +153,52 @@ function commandes_envoyer_notification( $qui, $id_type, $id_commande, $expedite
		}
	}
}


/*
 * Traitement des notifications d'une commande
 * Selon les options de configuration, des emails seront envoyés au(x) vendeur(s) et optionnellement au client
 * 
 * @param int|string $id_commande
 *     identifiant de la commande
 * @return void
 */
function traiter_notifications_commande($id_commande=0){

	if (intval($id_commande)==0) return;

	if (
		include_spip('inc/config')
		and $config = lire_config('commandes')
		and $quand = $config['quand'] ? $config['quand'] : array()
		and $config['activer'] // les notifications sont activées
		and $statut = sql_getfetsel('statut', table_objet_sql('commande'), "id_commande=".intval($id_commande))
		and in_array($statut, $quand) // le nouveau statut est valide pour envoyer une notification
		and $notifications = charger_fonction('notifications', 'inc', true) // la fonction est bien chargée
	) {

		// Sans les plugins Facteur et Notifications avancées, on ne fait rien
		if (!defined('_DIR_PLUGIN_NOTIFAVANCEES')) {
			spip_log("traiter_notifications_commande : notifications impossibles sans le plugins Notifications avancées pour la commande $id_commande",'commandes.' . _LOG_ERREUR);
			return;
		}

		// Déterminer l'expéditeur
		$options = array();
		if( $config['expediteur'] != "facteur" )
			$options['expediteur'] = $config['expediteur_'.$config['expediteur']];

		// Envoyer au vendeur
		spip_log("traiter_notifications_commande : notification vendeur pour la commande $id_commande",'commandes.' . _LOG_INFO);
		$notifications('commande_vendeur', $id_commande, $options);

		// Envoyer optionnellement au client
		if($config['client']) {
			spip_log("traiter_notifications_commande : notification client pour la commande $id_commande",'commandes.' . _LOG_INFO);
			$notifications('commande_client', $id_commande, $options);
		}

	}
}

?>
+1 −1
Numéro de ligne d'origine Numéro de ligne de diff Ligne de diff
@@ -22,12 +22,12 @@
	<necessite nom="prix" compatibilite="[0.1.0;]" />
	<utilise nom="coordonnees" compatibilite="[2.0.0;]" />
	<utilise nom="notifavancees" compatibilite="[0.2.0;]" />
	<utilise nom="facteur" compatibilite="[2.0.0;]" />

	<pipeline nom="affiche_milieu" inclure="commandes_pipelines.php" /><!-- fiche commande : formulaires de dates -->
	<pipeline nom="accueil_encours" inclure="commandes_pipelines.php" /><!-- page d'accueil : liste des commandes en attente validation -->
	<pipeline nom="affiche_auteurs_interventions" inclure="commandes_pipelines.php" /><!-- liste des commandes sur la fiche d'un auteur -->
	<pipeline nom="insert_head_css" inclure="commandes_pipelines.php" />
	<pipeline nom="post_edition" inclure="commandes_pipelines.php" /><!-- changement de statut : envoi de notifications et maj dates paiement et envoi -->

	<pipeline nom="declarer_tables_interfaces" inclure="base/commandes.php" />
	<pipeline nom="declarer_tables_objets_sql" inclure="base/commandes.php" />