From 435f366b624e1e4aa3275740b45b32e85c7a075a Mon Sep 17 00:00:00 2001
From: Fil <fil@rezo.net>
Date: Fri, 25 Apr 2003 08:56:34 +0000
Subject: [PATCH] =?UTF-8?q?*=20le=20tag=20#DATE=20fonctionne=20en-dehors?=
 =?UTF-8?q?=20des=20boucles=20(cas=20des=20dates=20pass=C3=A9es=20dans=20l?=
 =?UTF-8?q?'URL)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* tous les critères {date_...} ou {age_...} sont disponibles en {date_..._redac} ou {age_..._redac} -- mais attention c'est la date_redac relative à la *date* courante, pas à la *date_redac*  (donc ces critères ne sont probablement utilisable qu'avec la date passée en URL ou en contexte inclus) ; pour comparer à la date_redac courante, il faut doubler : {age_relatif_redac_redac}... c'est un peu dingue, mais au moins ça sera logique et mnémotechnique. Sauf si quelqu'un a plus simple ??

* correction de 2-3 anciens petits bugs de date

Exemple :

<b>#DATE</b><br>
<BOUCLE_a(ARTICLES){par date}{inverse}>
<br>#ID_ARTICLE #DATE #DATE_REDAC
<BOUCLE_b(ARTICLES){age_relatif_redac_redac>0}{par date_redac}{inverse}{0,1}>
<br>&nbsp; (age_relatif_redac_redac : #ID_ARTICLE)
</BOUCLE_b>
<BOUCLE_c(ARTICLES){age_relatif_redac>0}{par date_redac}{inverse}{0,1}>
<br>&nbsp; (age_relatif_redac : #ID_ARTICLE)
</BOUCLE_c>
<BOUCLE_d(ARTICLES){age_relatif>0}{par date}{inverse}{0,1}>
<br>&nbsp; (age_relatif : #ID_ARTICLE)
</BOUCLE_d>
</BOUCLE_a>

résultat :

4 2003-04-04 15:23:05
  (age_relatif_redac_redac : 2)
  (age_relatif_redac : 2)
  (age_relatif : 3)
3 2003-03-26 00:00:00 1970-04-11 00:00:00
  (age_relatif_redac_redac : 1)
  (age_relatif_redac : 2)
  (age_relatif : 2)
2 2003-03-00 00:00:00 1980-02-02 00:00:00
  (age_relatif_redac_redac : 3)
  (age_relatif_redac : 2)
  (age_relatif : 2)
1 2003-02-26 17:26:01
  (age_relatif_redac_redac : 2)
  (age_relatif_redac : 2)
---
 ecrire/inc_sites.php3   |  2 +-
 ecrire/inc_version.php3 | 18 ++++++++++++++
 inc-calcul-squel.php3   | 55 ++++++++++++++++++++++-------------------
 inc-calcul.php3         | 14 -----------
 4 files changed, 48 insertions(+), 41 deletions(-)

diff --git a/ecrire/inc_sites.php3 b/ecrire/inc_sites.php3
index bf3b57d8d7..4187832520 100644
--- a/ecrire/inc_sites.php3
+++ b/ecrire/inc_sites.php3
@@ -194,7 +194,7 @@ function syndic_a_jour($now_id_syndic, $statut = 'off') {
 					$la_date = $match[2];
 				else if (ereg("<pubDate>([^<]*)</pubDate>",$item[$i],$match))
 					$la_date = $match[1];
-				if ($la_date) {
+				if ($GLOBALS['flag_strtotime'] AND $la_date) {
 					// http://www.w3.org/TR/NOTE-datetime
 					if (ereg('^([0-9]+-[0-9]+-[0-9]+T[0-9]+:[0-9]+(:[0-9]+)?)(\.[0-9]+)?(Z|([-+][0-9][0-9]):[0-9]+)$', $la_date, $match)) {
 						$la_date = str_replace("T", " ", $match[1])." GMT";
diff --git a/ecrire/inc_version.php3 b/ecrire/inc_version.php3
index f54bcb2738..e6cdf3a615 100644
--- a/ecrire/inc_version.php3
+++ b/ecrire/inc_version.php3
@@ -738,6 +738,24 @@ function email_valide($adresse) {
 	return false;
 }
 
+
+// normaliser la date
+function normaliser_date($date) {
+	if ($date) {
+		if (ereg("^([12][0-9]{3})(-00)?( .*)?$", $date, $regs))
+			$date = $regs[1]."-01-01".$regs[2];
+		else if (ereg("^([12][0-9]{3}[/-][01]?[0-9])(-00)?( .*)?$", $date, $regs))
+			$date = ereg_replace("/","-",$regs[1])."-01".$regs[3];
+		else if ($GLOBALS['flag_strtotime']) {
+			if ($date_str = strtotime($date))
+				$date = date("Y-m-d H:i:s", $date_str);
+		}
+	}
+	return $date;
+}
+
+$date = normaliser_date($date);
+
 //
 // Traduction des textes de SPIP
 //
diff --git a/inc-calcul-squel.php3 b/inc-calcul-squel.php3
index 5b83b37693..dfe76d2ea4 100644
--- a/inc-calcul-squel.php3
+++ b/inc-calcul-squel.php3
@@ -389,48 +389,45 @@ function parser_boucle($texte, $id_parent) {
 						// Cas particulier : id_secteur = id_rubrique pour certaines tables
 						else if (($type == 'breves' OR $type == 'forums') AND $col == 'id_secteur')
 							$col = 'id_rubrique';
+
+						// Cas particulier : expressions de date redac
+						$datecompare='date';
+						if (ereg("^(date|mois|annee|age|age_relatif|jour_relatif|annee_relatif)_redac(_redac)?$", $col, $regs)) {
+							$col_date = 'date_redac';
+							$col = $regs[1];
+							if ($regs[2])
+								$datecompare='date_redac';
+						}
+
 						// Cas particulier : expressions de date
-						else if ($col == 'date')
+						if ($col == 'date')
 							$col = $table.$col_date;
 						else if ($col == 'mois') {
 							$col = "MONTH($table.$col_date)";
 							$col_table = '';
 						}
-						else if ($col == 'mois_redac') {
-							$col = "MONTH($table.date_redac)";
-							$col_table = '';
-						}
 						else if ($col == 'annee') {
 							$col = "YEAR($table.$col_date)";
 							$col_table = '';
 						}
-						else if ($col == 'annee_redac') {
-							$col = "YEAR($table.date_redac)";
-							$col_table = '';
-						}
 						else if ($col == 'age') {
 							$col = "(LEAST((UNIX_TIMESTAMP(now())-UNIX_TIMESTAMP($table.$col_date))/86400, TO_DAYS(now())-TO_DAYS($table.$col_date), DAYOFMONTH(now())-DAYOFMONTH($table.$col_date)+30.4368*(MONTH(now())-MONTH($table.$col_date))+365.2422*(YEAR(now())-YEAR($table.$col_date))))";
 							$col_table = '';
 						}
 						else if ($col == 'age_relatif') {
-							$col = "LEAST((UNIX_TIMESTAMP('(\$date)')-UNIX_TIMESTAMP($table.$col_date))/86400,
-TO_DAYS('(\$date)')-TO_DAYS($table.$col_date), DAYOFMONTH('(\$date)')-DAYOFMONTH($table.$col_date)+30.4368*(MONTH('(\$date)')-MONTH($table.$col_date))+365.2422*(YEAR('(\$date)')-YEAR($table.$col_date)))";
+							$col = "LEAST((UNIX_TIMESTAMP('(\$$datecompare)')-UNIX_TIMESTAMP($table.$col_date))/86400, TO_DAYS('(\$$datecompare)')-TO_DAYS($table.$col_date), DAYOFMONTH('(\$$datecompare)')-DAYOFMONTH($table.$col_date)+30.4368*(MONTH('(\$$datecompare)')-MONTH($table.$col_date))+365.2422*(YEAR('(\$$datecompare)')-YEAR($table.$col_date)))";
 							$col_table = '';
 						}
 						else if ($col == 'jour_relatif') {
-							$col = "LEAST(TO_DAYS('(\$date)')-TO_DAYS($table.$col_date), DAYOFMONTH('(\$date)')-DAYOFMONTH($table.$col_date)+30.4368*(MONTH('(\$date)')-MONTH($table.$col_date))+365.2422*(YEAR('(\$date)')-YEAR($table.$col_date)))";
+							$col = "LEAST(TO_DAYS('(\$$datecompare)')-TO_DAYS($table.$col_date), DAYOFMONTH('(\$$datecompare)')-DAYOFMONTH($table.$col_date)+30.4368*(MONTH('(\$$datecompare)')-MONTH($table.$col_date))+365.2422*(YEAR('(\$$datecompare)')-YEAR($table.$col_date)))";
 							$col_table = '';
 						}
 						else if ($col == 'mois_relatif') {
-							$col = "(MONTH('(\$date)')-MONTH($table.$col_date)+12*(YEAR('(\$date)')-YEAR($table.$col_date)))";
+							$col = "(MONTH('(\$$datecompare)')-MONTH($table.$col_date)+12*(YEAR('(\$$datecompare)')-YEAR($table.$col_date)))";
 							$col_table = '';
 						}
 						else if ($col == 'annee_relatif') {
-							$col = "YEAR('(\$date)')-YEAR($table.$col_date)";
-							$col_table = '';
-						}
-						else if ($col == 'age_redac') {
-							$col = "(LEAST((TO_DAYS(now())-TO_DAYS(date_redac)),(DAYOFMONTH(now())-DAYOFMONTH(date_redac))+30.4368*(MONTH(now())-MONTH(date_redac))+365.2422*(YEAR(now())-YEAR(date_redac))))";
+							$col = "YEAR('(\$$datecompare)')-YEAR($table.$col_date)";
 							$col_table = '';
 						}
 
@@ -1353,6 +1350,10 @@ function calculer_champ($id_champ, $id_boucle, $nom_var)
 		$code = "propre('- ')";
 		break;
 
+	case 'DATE':
+		$code = "\$GLOBALS['date']";	// uniquement hors-boucles, pour la date passee dans l'URL ou le contexte inclusion
+		break;
+
 	case 'DATE_NOUVEAUTES':
 		$milieu = "if (lire_meta('quoi_de_neuf') == 'oui' AND lire_meta('majnouv'))
 			\$$nom_var = date('Y-m-d H:i:s', lire_meta('majnouv'));
@@ -1829,7 +1830,8 @@ function calculer_boucle($id_boucle, $prefix_boucle)
 				$contexte["id_rubrique"] = $row["id_rubrique"];
 				$contexte["id_parent"] = $row["id_parent"];
 				$contexte["id_secteur"] = $row["id_secteur"];
-				$contexte["date"] = $row["date"];
+				$contexte["date"] = normaliser_date($row["date"]);
+				$contexte["date_redac"] = normaliser_date($row["date_redac"]);
 	
 				if ($doublons == "oui") {
 					$id_doublons["rubriques"] .= ",".$row["id_rubrique"];
@@ -1949,7 +1951,8 @@ function calculer_boucle($id_boucle, $prefix_boucle)
 		$contexte["id_article"] = $row["id_article"];
 		$contexte["id_rubrique"] = $row["id_rubrique"];
 		$contexte["id_secteur"] = $row["id_secteur"];
-		$contexte["date"] = $row["date"];
+		$contexte["date"] = normaliser_date($row["date"]);
+		$contexte["date_redac"] = normaliser_date($row["date_redac"]);
 		$contexte["accepter_forum"] = $row["accepter_forum"];
 		if ($instance->doublons == "oui") $id_doublons["articles"] .= ",".$row["id_article"];
 		';
@@ -1960,7 +1963,7 @@ function calculer_boucle($id_boucle, $prefix_boucle)
 		$contexte["id_breve"] = $row["id_breve"];
 		$contexte["id_rubrique"] = $row["id_rubrique"];
 		$contexte["id_secteur"] = $row["id_rubrique"];
-		$contexte["date"] = $row["date_heure"];
+		$contexte["date"] = normaliser_date($row["date_heure"]);
 		if ($instance->doublons == "oui") $id_doublons["breves"] .= ",".$row["id_breve"];
 		';
 		break;
@@ -1971,7 +1974,7 @@ function calculer_boucle($id_boucle, $prefix_boucle)
 		$contexte["id_rubrique"] = $row["id_rubrique"];
 		$contexte["id_secteur"] = $row["id_secteur"];
 		$contexte["url_site"] = $row["url_site"];
-		$contexte["date"] = $row["date"];
+		$contexte["date"] = normaliser_date($row["date"]);
 		if ($instance->doublons == "oui") $id_doublons["syndication"] .= ",".$row["id_syndic"];
 		';
 		break;
@@ -1996,7 +1999,7 @@ function calculer_boucle($id_boucle, $prefix_boucle)
 		$texte .= '
 		$contexte["id_syndic"] = $row["id_syndic"];
 		$contexte["id_syndic_article"] = $row["id_syndic_article"];
-		$contexte["date"] = $row["date"];
+		$contexte["date"] = normaliser_date($row["date"]);
 		if ($instance->doublons == "oui") $id_doublons["syndic_articles"] .= ",".$row["syndic_articles"];
 		';
 		break;
@@ -2006,7 +2009,7 @@ function calculer_boucle($id_boucle, $prefix_boucle)
 		$contexte["id_rubrique"] = $row["id_rubrique"];
 		$contexte["id_parent"] = $row["id_parent"];
 		$contexte["id_secteur"] = $row["id_secteur"];
-		$contexte["date"] = $row["date"];
+		$contexte["date"] = normaliser_date($row["date"]);
 		if ($instance->doublons == "oui") $id_doublons["rubriques"] .= ",".$row["id_rubrique"];
 		$syn_rubrique .= ",".$row["id_rubrique"].",";
 		';
@@ -2019,7 +2022,7 @@ function calculer_boucle($id_boucle, $prefix_boucle)
 		$contexte["id_article"] = $row["id_article"];
 		$contexte["id_breve"] = $row["id_breve"];
 		$contexte["id_parent"] = $row["id_parent"];
-		$contexte["date"] = $row["date_heure"];
+		$contexte["date"] = normaliser_date($row["date_heure"]);
 		if ($instance->doublons == "oui") $id_doublons["forums"] .= ",".$row["id_forum"];
 		';
 		break;
@@ -2034,7 +2037,7 @@ function calculer_boucle($id_boucle, $prefix_boucle)
 	case 'signatures':
 		$texte .= '
 		$contexte["id_signature"] = $row["id_signature"];
-		$contexte["date"] = $row["date_time"];
+		$contexte["date"] = normaliser_date($row["date_time"]);
 		if ($instance->doublons == "oui") $id_doublons["signatures"] .= ",".$row["id_signature"];
 		';
 		break;
diff --git a/inc-calcul.php3 b/inc-calcul.php3
index f666ce151b..41acf92b9a 100644
--- a/inc-calcul.php3
+++ b/inc-calcul.php3
@@ -429,7 +429,6 @@ function calculer_page_globale($fond) {
 	global $contexte;
 	global $fichier_requete;
 	global $id_rubrique_fond;
-	global $date;
 
 	$contexte = '';
 	$contexte_defaut = array('id_parent', 'id_rubrique', 'id_article', 'id_auteur',
@@ -441,19 +440,6 @@ function calculer_page_globale($fond) {
 		}
 	}
 
-	// mettre la date eventuellement passee en globale au format MySQL
-	if ($date) {
-		if (ereg("^[12][0-9]{3}$", $date))
-			$date .= "-01-01";
-		else if (ereg("^[12][0-9]{3}[/-][01]?[0-9]$", $date))
-			$date = ereg_replace("/","-",$date)."-01";
-		else if ($GLOBALS['flag_strtotime']) {
-			if ($date_str = strtotime($date))
-				$date = date("Y-m-d H:i:s", $date_str);
-		}
-		$contexte['date'] = $date;
-	}
-
 	// Calcul de la rubrique associee a la requete
 	// (selection de squelette specifique)
 
-- 
GitLab