From ad9fefd033f34543a61b99b7148cd1cc741f7527 Mon Sep 17 00:00:00 2001 From: "rastapopoulos@spip.org" <> Date: Sat, 14 Mar 2015 01:54:33 +0000 Subject: [PATCH] =?UTF-8?q?Deux=20ajouts=20notoires=20:=20-=20Quand=20on?= =?UTF-8?q?=20modife=20les=20dates=20d'un=20abonnement,=20=C3=A7a=20v?= =?UTF-8?q?=C3=A9rifie=20si=20aujourd'hui=20on=20est=20dedans=20ou=20dehor?= =?UTF-8?q?s,=20et=20=C3=A7a=20active=20ou=20d=C3=A9sactive=20l'abonnement?= =?UTF-8?q?=20automatiquement.=20Il=20est=20toujours=20possible=20de=20cha?= =?UTF-8?q?nger=20le=20statut=20manuellement=20pour=20mettre=20inactif=20m?= =?UTF-8?q?=C3=AAme=20si=20on=20est=20dans=20les=20bonnes=20dates,=20ou=20?= =?UTF-8?q?inversement.=20-=20Surtout,=20le=20plugin=20est=20d=C3=A9sormai?= =?UTF-8?q?s=20copain=20avec=20Commandes=20:=20quand=20un=20utilisateur=20?= =?UTF-8?q?commande=20des=20offres=20d'abonnements,=20alors=20d=C3=A8s=20q?= =?UTF-8?q?u'il=20a=20enfin=20pay=C3=A9,=20=C3=A7a=20le=20d=C3=A9tecte=20e?= =?UTF-8?q?t=20=C3=A7a=20cr=C3=A9e=20un=20nouvel=20abonnement=20OU=20?= =?UTF-8?q?=C3=A7a=20renouvelle=20un=20existant=20si=20on=20en=20avait=20d?= =?UTF-8?q?=C3=A9j=C3=A0=20un=20avec=20cette=20offre.=20Au=20passage,=20on?= =?UTF-8?q?=20ajoute=20donc=20une=20action=20pratique=20"creer=5Fou=20reno?= =?UTF-8?q?uveler=5Fabonnement"=20qui=20sait=20bien=20faire=20les=20choses?= =?UTF-8?q?=20en=20lui=20donnant=20un=20auteur=20et=20une=20offre.=20-=20P?= =?UTF-8?q?our=20que=20tout=20marche,=20on=20d=C3=A9place=20"inactif"=20en?= =?UTF-8?q?=20statut=20par=20d=C3=A9faut=20(premier),=20pour=20que=20?= =?UTF-8?q?=C3=A7a=20le=20change=20ensuite,=20et=20que=20"abonnements=20?= =?UTF-8?q?=C3=A0=20des=20zones"=20d=C3=A9tecte=20bien=20une=20activation?= =?UTF-8?q?=20lors=20de=20la=20toute=20premi=C3=A8re=20cr=C3=A9ation.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitattributes | 1 + abonnements_pipelines.php | 55 +++++++++++++++++++-- action/creer_ou_renouveler_abonnement.php | 60 +++++++++++++++++++++++ base/abonnements.php | 4 +- formulaires/editer_abonnement.php | 38 +------------- paquet.xml | 2 +- 6 files changed, 116 insertions(+), 44 deletions(-) create mode 100644 action/creer_ou_renouveler_abonnement.php diff --git a/.gitattributes b/.gitattributes index c86d9b1..0523a53 100644 --- a/.gitattributes +++ b/.gitattributes @@ -2,6 +2,7 @@ /abonnements_administrations.php -text /abonnements_autorisations.php -text /abonnements_pipelines.php -text +action/creer_ou_renouveler_abonnement.php -text action/modifier_echeance_abonnement.php -text action/renouveler_abonnement.php -text base/abonnements.php -text diff --git a/abonnements_pipelines.php b/abonnements_pipelines.php index 5437ef2..cfa6ed2 100644 --- a/abonnements_pipelines.php +++ b/abonnements_pipelines.php @@ -41,8 +41,8 @@ function abonnements_optimiser_base_disparus($flux){ function abonnements_post_edition($flux){ // Si on modifie un abonnement if ($flux['args']['table'] == 'spip_abonnements') { - $abonnement = sql_fetsel('*', 'spip_abonnements', 'id_abonnement = '.$flux['args']['id_objet']); - $offre = sql_fetsel('*', 'spip_abonnements_offres', 'id_abonnements_offre = '.$abonnement['id_abonnements_offre']); + $abonnement = sql_fetsel('*', 'spip_abonnements', 'id_abonnement = '.intval($flux['args']['id_objet'])); + $offre = sql_fetsel('*', 'spip_abonnements_offres', 'id_abonnements_offre = '.intval($abonnement['id_abonnements_offre'])); $modifs = array(); @@ -66,13 +66,13 @@ function abonnements_post_edition($flux){ } // Calcul de la date de fin - $modifs['date_fin'] = date('Y-m-d H:i:s', strtotime($abonnement['date_debut'].$ajout)); + $modifs['date_fin'] = $abonnement['date_fin'] = date('Y-m-d H:i:s', strtotime($abonnement['date_debut'].$ajout)); } // S'il le statut est "prepa" c'est une création et on doit changer ça // car pour l'instant SPIP ne permet pas de déclarer le statut par défaut ! if ($abonnement['statut'] == 'prepa') { - $modifs['statut'] = 'actif'; + $modifs['statut'] = $abonnement['statut'] = 'actif'; } // Si on a mis l'abonnement à la poubelle, on doit enlever les tâches liées elseif ($abonnement['statut'] == 'poubelle') { @@ -85,6 +85,26 @@ function abonnements_post_edition($flux){ } } + // Seulement si personne n'a modifié le statut manuellement, alors on check les dates pour statufier + if (!$flux['data']['statut']) { + $jourdhui = date('Y-m-d H:i:s'); + // Si aujourd'hui est dans les dates, on active + if ( + $abonnement['statut'] == 'inactif' + and $jourdhui >= $abonnement['date_debut'] + and $jourdhui <= $abonnement['date_fin'] + ) { + $modifs['statut'] = 'actif'; + } + // Si aujourd'hui est en dehors des dates, on désactive + elseif ( + $abonnement['statut'] == 'actif' + and ($jourdhui < $abonnement['date_debut'] or $jourdhui > $abonnement['date_fin']) + ) { + $modifs['statut'] = 'inactif'; + } + } + // S'il y a des modifs à faire on appelle l'API de modif if (!empty($modifs)){ include_spip('action/editer_objet'); @@ -97,6 +117,33 @@ function abonnements_post_edition($flux){ abonnements_programmer_desactivation($flux['args']['id_objet'], $flux['data']['date_fin']); } } + // Détection magique du plugin Commandes et d'une commande d'offre d'abonnement + elseif ( + // Si on institue une commande + $flux['args']['table'] == 'spip_commandes' + and $id_commande = intval($flux['args']['id_objet']) + and $flux['args']['action'] == 'instituer' + // Et qu'on passe en statut "paye" depuis autre chose + and $flux['data']['statut'] == 'paye' + and $flux['args']['statut_ancien'] != 'paye' + // Et que la commande existe bien + and $commande = sql_fetsel('*', 'spip_commandes', 'id_commande = '.$id_commande) + // Et que cette commande a un utilisateur correct + and ($id_auteur = $commande['id_auteur']) > 0 + // Et qu'on a des détails dans cette commande + and $details = sql_allfetsel('*', 'spip_commandes_details', 'id_commande = '.$id_commande) + and is_array($details) + ) { + // On cherche si on a des offres d'abonnements dans les détails de la commande + foreach ($details as $detail) { + // Si on trouve une offre d'abonnement + if ($detail['objet'] = 'abonnements_offre' and ($id_abonnements_offre = $detail['id_objet']) > 0) { + // On crée ou renouvelle + $action = charger_fonction('creer_ou_renouveler_abonnement', 'action/'); + $action($id_auteur.'/'.$id_abonnements_offre); + } + } + } return $flux; } diff --git a/action/creer_ou_renouveler_abonnement.php b/action/creer_ou_renouveler_abonnement.php new file mode 100644 index 0000000..f59eafa --- /dev/null +++ b/action/creer_ou_renouveler_abonnement.php @@ -0,0 +1,60 @@ + 0 + and ($id_abonnements_offre = intval($id_abonnements_offre)) > 0 + ) { + // Si on trouve un abonnement (le dernier en date) + if ($id_abonnement = intval(sql_getfetsel( + 'id_abonnement', + 'spip_abonnements', + array('id_auteur = '.$id_auteur, 'id_abonnements_offre = '.$id_abonnements_offre, 'statut != "poubelle"'), + '', + 'date_fin desc', + '0,1' + ))) { + // On le renouvelle ! + $renouveler = charger_fonction('renouveler_abonnement', 'action/'); + return $renouveler($id_abonnement); + } + // Sinon on en crée un nouveau + else { + include_spip('action/editer_objet'); + if ($id_abonnement = objet_inserer('abonnement')) { + $erreur = objet_modifier( + 'abonnement', $id_abonnement, + array( + 'id_auteur' => $id_auteur, + 'id_abonnements_offre' => $id_abonnements_offre, + ) + ); + return array($id_abonnement, $erreur); + } + } + } + + return false; +} + diff --git a/base/abonnements.php b/base/abonnements.php index 4952816..f5d32da 100644 --- a/base/abonnements.php +++ b/base/abonnements.php @@ -102,14 +102,14 @@ function abonnements_declarer_tables_objets_sql($tables) { 'rechercher_champs' => array(), 'tables_jointures' => array(), 'statut_textes_instituer' => array( - 'actif' => 'abonnement:statut_actif', 'inactif' => 'abonnement:statut_inactif', + 'actif' => 'abonnement:statut_actif', 'poubelle' => 'texte_statut_poubelle', ), 'statut_images' => array( 'prepa' => 'puce-preparer-8.png', - 'actif' => 'puce-publier-8.png', 'inactif' => 'puce-refuser-8.png', + 'actif' => 'puce-publier-8.png', 'poubelle' => 'puce-supprimer-8.png', ), 'statut'=> array( diff --git a/formulaires/editer_abonnement.php b/formulaires/editer_abonnement.php index d31d27f..0b02993 100644 --- a/formulaires/editer_abonnement.php +++ b/formulaires/editer_abonnement.php @@ -58,8 +58,7 @@ function formulaires_editer_abonnement_saisies_dist($id_abonnement='new', $retou ), ); } - // Sinon si c'est une demande de modification, on ne peut modifier que l'échéance - // Et uniquement en ajoutant avec durée et période + // Sinon si c'est une demande de modification, on ne peut modifier que les dates elseif ($id_abonnement > 0) { $saisies = array( array( @@ -93,41 +92,6 @@ function formulaires_editer_abonnement_saisies_dist($id_abonnement='new', $retou ), ), ); -# $saisies = array( -# array( -# 'saisie' => 'fieldset', -# 'options' => array( -# 'nom' => 'date_fin_allonger', -# 'label' => _T('abonnement:champ_date_fin_allonger_label'), -# ), -# 'saisies' => array( -# array( -# 'saisie' => 'input', -# 'options' => array( -# 'nom' => 'duree', -# 'label' => _T('abonnementsoffre:champ_duree_label'), -# 'defaut' => 0, -# ), -# 'verifier' => array( -# 'type' => 'entier', -# ), -# ), -# array( -# 'saisie' => 'selection', -# 'options' => array( -# 'nom' => 'periode', -# 'label' => _T('abonnementsoffre:champ_periode_label'), -# 'cacher_option_intro' => 'oui', -# 'datas' => array( -# 'mois' => _T('abonnementsoffre:champ_periode_choix_mois'), -# 'jours' => _T('abonnementsoffre:champ_periode_choix_jours'), -# 'heures' => _T('abonnementsoffre:champ_periode_choix_heures'), -# ), -# ), -# ), -# ), -# ), -# ); } return $saisies; diff --git a/paquet.xml b/paquet.xml index 74de186..1153556 100644 --- a/paquet.xml +++ b/paquet.xml @@ -1,7 +1,7 @@