Exemple #1
0
function critere_parinverse($idb, &$boucles, $crit, $sens = '')
{
    global $exceptions_des_jointures;
    $boucle =& $boucles[$idb];
    if ($crit->not) {
        $sens = $sens ? "" : " . ' DESC'";
    }
    $collecte = isset($boucle->modificateur['collecte']) ? " . " . $boucle->modificateur['collecte'] : "";
    foreach ($crit->param as $tri) {
        $order = $fct = "";
        // en cas de fonction SQL
        // tris specifies dynamiquement
        if ($tri[0]->type != 'texte') {
            // calculer le order dynamique qui verifie les champs
            $order = calculer_critere_arg_dynamique($idb, $boucles, $tri, $sens);
            // et si ce n'est fait, ajouter un champ 'hasard'
            // pour supporter 'hasard' comme tri dynamique
            $par = "rand()";
            $parha = $par . " AS hasard";
            if (!in_array($parha, $boucle->select)) {
                $boucle->select[] = $parha;
            }
        } else {
            $par = array_shift($tri);
            $par = $par->texte;
            // par multi champ
            if (preg_match(",^multi[\\s]*(.*)\$,", $par, $m)) {
                $champ = trim($m[1]);
                // par multi L1.champ
                if (strpos($champ, '.')) {
                    $cle = '';
                    // par multi champ (champ sur une autre table)
                } elseif (!array_key_exists($champ, $boucle->show['field'])) {
                    $cle = trouver_jointure_champ($champ, $boucle);
                    // par multi champ (champ dans la table en cours)
                } else {
                    $cle = $boucle->id_table;
                }
                if ($cle) {
                    $cle .= '.';
                }
                $texte = $cle . $champ;
                $boucle->select[] = "\".sql_multi('" . $texte . "', \$GLOBALS['spip_lang']).\"";
                $order = "'multi'";
                // par num champ(, suite)
            } else {
                if (preg_match(",^num (.*)\$,m", $par, $m)) {
                    $champ = trim($m[1]);
                    // par num L1.champ
                    if (strpos($champ, '.')) {
                        $cle = '';
                        // par num champ (champ sur une autre table)
                    } elseif (!array_key_exists($champ, $boucle->show['field'])) {
                        $cle = trouver_jointure_champ($champ, $boucle);
                        // par num champ (champ dans la table en cours)
                    } else {
                        $cle = $boucle->id_table;
                    }
                    if ($cle) {
                        $cle .= '.';
                    }
                    $texte = '0+' . $cle . $champ;
                    $suite = calculer_liste($tri, array(), $boucles, $boucle->id_parent);
                    if ($suite !== "''") {
                        $texte = "\" . ((\$x = {$suite}) ? ('{$texte}' . \$x) : '0')" . " . \"";
                    }
                    $as = 'num' . ($boucle->order ? count($boucle->order) : "");
                    $boucle->select[] = $texte . " AS {$as}";
                    $order = "'{$as}'";
                } else {
                    if (!preg_match(",^" . CHAMP_SQL_PLUS_FONC . '$,is', $par, $match)) {
                        return array('zbug_critere_inconnu', array('critere' => $crit->op . " {$par}"));
                    } else {
                        if (count($match) > 2) {
                            $par = substr($match[2], 1, -1);
                            $fct = $match[1];
                        }
                        // par hasard
                        if ($par == 'hasard') {
                            $par = "rand()";
                            $boucle->select[] = $par . " AS alea";
                            $order = "'alea'";
                        } else {
                            if (isset($exceptions_des_jointures[$par])) {
                                list($table, $champ) = $exceptions_des_jointures[$par];
                                $order = critere_par_joint($table, $champ, $boucle, $idb);
                                if (!$order) {
                                    return array('zbug_critere_inconnu', array('critere' => $crit->op . " {$par}"));
                                }
                            } else {
                                if ($par == 'date' and $desc = $boucle->show and $desc['date']) {
                                    $m = $desc['date'];
                                    $order = "'" . $boucle->id_table . "." . $m . "'";
                                } elseif (preg_match("/^([^,]*)\\.(.*)\$/", $par, $r)) {
                                    // cas du tri sur champ de jointure explicite
                                    $t = array_search($r[1], $boucle->from);
                                    if (!$t) {
                                        $t = trouver_jointure_champ($r[2], $boucle, array($r[1]));
                                    }
                                    if (!$t) {
                                        return array('zbug_critere_inconnu', array('critere' => $crit->op . " {$par}"));
                                    } else {
                                        $order = "'" . $t . '.' . $r[2] . "'";
                                    }
                                } else {
                                    $desc = $boucle->show;
                                    if (isset($desc['field'][$par])) {
                                        $par = $boucle->id_table . "." . $par;
                                    }
                                    // sinon tant pis, ca doit etre un champ synthetise (cf points)
                                    $order = "'{$par}'";
                                }
                            }
                        }
                    }
                }
            }
        }
        if (preg_match('/^\'([^"]*)\'$/', $order, $m)) {
            $t = $m[1];
            if (strpos($t, '.') and !in_array($t, $boucle->select)) {
                $boucle->select[] = $t;
            }
        } else {
            $sens = '';
        }
        if ($fct) {
            if (preg_match("/^\\s*'(.*)'\\s*\$/", $order, $r)) {
                $order = "'{$fct}(" . $r[1] . ")'";
            } else {
                $order = "'{$fct}(' . {$order} . ')'";
            }
        }
        $t = $order . $collecte . $sens;
        if (preg_match("/^(.*)'\\s*\\.\\s*'([^']*')\$/", $t, $r)) {
            $t = $r[1] . $r[2];
        }
        $boucle->order[] = $t;
    }
}
Exemple #2
0
function critere_parinverse($idb, &$boucles, $crit, $sens='') {
	global $exceptions_des_jointures;
	$boucle = &$boucles[$idb];
	if ($crit->not) $sens = $sens ? "" : " . ' DESC'";
	$collecte = (isset($boucle->modificateur['collecte']))?" . ".$boucle->modificateur['collecte']:"";

	foreach ($crit->param as $tri) {

	  $order = $fct = ""; // en cas de fonction SQL
	// tris specifies dynamiquement
	  if ($tri[0]->type != 'texte') {
	  	// calculer le order dynamique qui verifie les champs
			$order = calculer_critere_arg_dynamique($idb, $boucles, $tri, $sens);
			// et si ce n'est fait, ajouter un champ 'hasard' 
			// pour supporter 'hasard' comme tri dynamique
			$par = "rand()";
			$parha = $par . " AS hasard";
			if (!in_array($parha, $boucle->select))
				$boucle->select[]= $parha;
	  } else {
	      $par = array_shift($tri);
	      $par = $par->texte;
    // par multi champ
	      if (preg_match(",^multi[\s]*(.*)$,",$par, $m)) {
		  $texte = $boucle->id_table . '.' . trim($m[1]);
		  $boucle->select[] =  "\".sql_multi('".$texte."', \$GLOBALS['spip_lang']).\"" ;
		  $order = "'multi'";
	// par num champ(, suite)
	      }	else if (preg_match(",^num (.*)$,m",$par, $m)) {
		  $texte = '0+' . $boucle->id_table . '.' . trim($m[1]);
		  $suite = calculer_liste($tri, array(), $boucles, $boucle->id_parent);
		  if ($suite !== "''")
		    $texte = "\" . ((\$x = $suite) ? ('$texte' . \$x) : '0')" . " . \"";
		  $as = 'num' .($boucle->order ? count($boucle->order) : "");
		  $boucle->select[] = $texte . " AS $as";
		  $order = "'$as'";
	      } else {
		if (!preg_match(",^" . CHAMP_SQL_PLUS_FONC . '$,is', $par, $match)) {
			return (array('zbug_critere_inconnu', array('critere' => $crit->op . " $par")));
		} else {
		if (count($match)>2) { $par = substr($match[2],1,-1); $fct = $match[1]; }
	// par hasard
		if ($par == 'hasard') {
			$par = "rand()";
			$boucle->select[]= $par . " AS alea";
			$order = "'alea'";
		}
	// par titre_mot ou type_mot voire d'autres
		else if (isset($exceptions_des_jointures[$par])) {
			list($table, $champ) =  $exceptions_des_jointures[$par];
			$order = critere_par_joint($table, $champ, $boucle, $idb);
			if (!$order)
				return (array('zbug_critere_inconnu', array('critere' => $crit->op . " $par")));
		}
		else if ($par == 'date'
		AND isset($GLOBALS['table_date'][$boucle->type_requete])) {
			$m = $GLOBALS['table_date'][$boucle->type_requete];
			$order = "'".$boucle->id_table ."." . $m . "'";
		}
		// par champ. Verifier qu'ils sont presents.
		elseif (preg_match("/^([^,]*)\.(.*)$/", $par, $r)) {
		  // cas du tri sur champ de jointure explicite
			$t = array_search($r[1], $boucle->from);
			if (!$t) {
				$t = trouver_champ_exterieur($r[2], array($r[1]), $boucle);
				$t = array_search(@$t[0], $boucle->from);
			}
			if (!$t) {
				return (array('zbug_critere_inconnu', array('critere' => $crit->op . " $par")));
			} else 	$order = "'" . $t . '.' . $r[2] . "'";
		} else {
			$desc = $boucle->show;
			if ($desc['field'][$par])
				$par = $boucle->id_table.".".$par;
		  // sinon tant pis, ca doit etre un champ synthetise (cf points)
			$order = "'$par'";
		}
	      }
	      }
	  }
	  if (preg_match('/^\'([^"]*)\'$/', $order, $m)) {
	      $t = $m[1];
	      if (strpos($t,'.') AND !in_array($t, $boucle->select)) {
		$boucle->select[] = $t;
	      }
	  } else $sens ='';

	  if ($fct) {
	    if (preg_match("/^\s*'(.*)'\s*$/", $order, $r))
	      $order = "'$fct(" . $r[1] . ")'";
	    else $order = "'$fct(' . $order . ')'";
	  }
	  $t = $order . $collecte . $sens;
	  if (preg_match("/^(.*)'\s*\.\s*'([^']*')$/", $t, $r))
	      $t = $r[1] . $r[2];
	  $boucle->order[] = $t;
	}
}