From 1d2db8debea55d279236c42039802a41db14a43b Mon Sep 17 00:00:00 2001
From: "Committo,Ergo:sum" <esj@rezo.net>
Date: Wed, 22 Aug 2007 06:23:23 +0000
Subject: [PATCH] =?UTF-8?q?#209:=20l'=C3=A9limination=20d'apostrophes=20(e?=
 =?UTF-8?q?xig=C3=A9e=20par=20PG)=20autour=20d'une=20constante=20num=C3=A9?=
 =?UTF-8?q?rque=20dans=20un=20crit=C3=A8re=20''col=3Dvaleur''=20=C3=A9tait?=
 =?UTF-8?q?=20mal=20calcul=C3=A9=20par=20[9859]=20car=20cette=20colonne=20?=
 =?UTF-8?q?n'est=20pas=20forc=C3=A9ment=20dans=20la=20table=20de=20la=20bo?=
 =?UTF-8?q?ucle,=20elle=20peut-etre=20dans=20une=20table=20de=20jointure.?=
 =?UTF-8?q?=20Le=20code=20est=20donc=20report=C3=A9=20dans=20la=20fonction?=
 =?UTF-8?q?=20d=C3=A9tectant=20le=20besoin=20de=20jointure=20pour=20r?=
 =?UTF-8?q?=C3=A9cup=C3=A9rer=20la=20description=20de=20la=20bonne=20table?=
 =?UTF-8?q?.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 ecrire/public/compiler.php |  1 -
 ecrire/public/criteres.php | 27 ++++++++++++++-------------
 2 files changed, 14 insertions(+), 14 deletions(-)

diff --git a/ecrire/public/compiler.php b/ecrire/public/compiler.php
index aeed166489..29691cd3e0 100644
--- a/ecrire/public/compiler.php
+++ b/ecrire/public/compiler.php
@@ -713,7 +713,6 @@ function public_compiler_dist($squelette, $nom, $gram, $sourcefile) {
 				if ((!$boucles[$id]->jointures)
 				AND (is_array($x = $tables_jointures[$nom_table])))
 					$boucles[$id]->jointures = $x;
-				spip_log("j $nom_table  $x $table");
 				if (($type == 'documents') && $boucle->doublons)
 					{ $descr['documents'] = true;  }
 			}
diff --git a/ecrire/public/criteres.php b/ecrire/public/criteres.php
index 87e81a25b2..440f6fbc7e 100644
--- a/ecrire/public/criteres.php
+++ b/ecrire/public/criteres.php
@@ -723,9 +723,12 @@ function calculer_critere_infixe($idb, &$boucles, $crit) {
 
 	global $table_des_tables, $tables_principales, $table_date;
 	global $exceptions_des_jointures, $exceptions_des_tables;
+	global $table_criteres_infixes;
 	$boucle = &$boucles[$idb];
 	$type = $boucle->type_requete;
 	$table = $boucle->id_table;
+	$nom = $table_des_tables[$type];
+	list(, $desc) = trouver_def_table($nom ? $nom : $type, $boucle);
 
 	list($fct, $col, $op, $val, $args_sql) =
 	  calculer_critere_infixe_ops($idb, $boucles, $crit);
@@ -763,8 +766,6 @@ function calculer_critere_infixe($idb, &$boucles, $crit) {
 		$val[0] = str_replace('image', 'vignette', $val[0]);
 
 	else  {
-		$nom = $table_des_tables[$type];
-		list($nom, $desc) = trouver_def_table($nom ? $nom : $type, $boucle);
 		if (@!array_key_exists($col, $desc['field'])) {
 	  	$calculer_critere_externe = 'calculer_critere_externe_init';
 			// gestion par les plugins des jointures tordues pas automatiques mais necessaires
@@ -775,11 +776,19 @@ function calculer_critere_infixe($idb, &$boucles, $crit) {
 					list($t, $col) = $exceptions_des_jointures[$table][$col];
 			}
 			else if (isset($exceptions_des_jointures[$col]))
-			  // on ignore la table, quel luxe!
 				list($t, $col) = $exceptions_des_jointures[$col];
-			else $t ='';
+			else $t =''; // jointure non declaree. La trouver.
 			$table = $calculer_critere_externe($boucle, $boucle->jointures, $col, $desc, ($crit->cond OR $op !='='), $t);
-	  }
+			list($nom, $desc) = trouver_champ_exterieur($col, $boucle->jointures, $boucle);
+		}
+	}
+	// En fonction du type de la colonne SQL
+	// la valeur comparee doit etre munie ou non d'apostrophes
+	if ($op == '=' OR in_array($op, $table_criteres_infixes)) {
+		if (strpos($val[0], '_q(') === 0
+		AND test_sql_int($desc['field'][$col]))
+				$val[0] = 'intval' . substr($val[0],2);
+		spip_log("typer $nom $col $idb $d $val[0]");
 	}
 	// tag du critere pour permettre aux boucles de modifier leurs requetes par defaut en fonction de ca
 	$boucles[$idb]->modificateur['criteres'][$col] = true;
@@ -1069,7 +1078,6 @@ function trouver_champ_exterieur($cle, $joints, &$boucle, $checkarrivee = false)
 // http://doc.spip.org/@calculer_critere_infixe_ops
 function calculer_critere_infixe_ops($idb, &$boucles, $crit)
 {
-	global $table_criteres_infixes;
 	// cas d'une valeur comparee a elle-meme ou son referent
 	if (count($crit->param) == 0)
 	  { $op = '=';
@@ -1135,13 +1143,6 @@ function calculer_critere_infixe_ops($idb, &$boucles, $crit)
 	  $args_sql .= $a[2];;
 	}
 
-	if ($op == '=' OR in_array($op, $table_criteres_infixes)) {
-		list($nom, $desc) = trouver_def_table($boucles[$idb]->id_table, $boucles[$idb]);
-		$type = $desc['field'][$col];
-		if (strpos($val[0], '_q(') === 0 AND test_sql_int($type))
-			$val[0] = 'intval' . substr($val[0],2);
-	}
-
 	return array($fct, $col, $op, $val, $args_sql);
 }
 
-- 
GitLab