Subversion Repositories eFlore/Applications.coel

Compare Revisions

Ignore whitespace Rev 1697 → Rev 1698

/trunk/jrest/services/CoelRecherche.php
2,14 → 2,12
/**
* Service fournissant des informations sur les collections et les structures répondant aux critères de recherche
* fournis en paramêtre.
* Encodage en entrée : utf8
* Encodage en sortie : utf8
*
*
* @author Raphaël Droz <raphael@tela-botanica.org>
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
* @version $Id$
* @copyright 2009
* @copyright 2009, 2013 Tela-Botanica
*/
class CoelRecherche extends Coel {
 
41,7 → 39,7
* Appelée avec les paramêtres d'url suivant :
* /CoelRecherche/ParDefaut/_
* ou les _ représentent dans l'ordre : mots
* Si un des paramêtres est abscent, il prendre la valeur *
* Si un des paramêtres est absent, il prendre la valeur *
*/
public function getElementParDefaut($param) {
// Initialisation des variables
49,13 → 47,26
// Pré traitement des paramêtres
$p = $this->pretraiterParametresUrl($param);
 
$fromClause = $whereClause = $joinClause = array();
self::construireFromEtWhere($p, $fromClause, $joinClause, $whereClause).
// Construction de la requête
$requete = (($this->distinct) ? 'SELECT DISTINCT' : 'SELECT').' cs_id_structure, cs_ville, cs_nom, '.
'cs_code_postal, cs_latitude, cs_longitude, cc_id_collection, cc_nom '.
$this->construireFromEtWhere($p).
'ORDER BY '.((!is_null($this->orderby)) ? $this->orderby : 'cs_ville ASC, cs_nom ASC, cc_nom ASC').' '.
"LIMIT $this->start, $this->limit ";
$requete = sprintf(
'SELECT %s cs_id_structure, cs_ville, cs_nom, cs_code_postal, cs_latitude, cs_longitude, cc_id_collection, cc_nom'
. ' FROM %s %s'
. ' WHERE %s ORDER BY %s LIMIT %d, %d -- %s:%d',
 
$this->distinct ? 'DISTINCT' : '',
 
implode(',', $fromClause),
implode(' ', $joinClause),
 
$whereClause ? implode(" AND ", $whereClause) : TRUE,
is_null($this->orderby) ? 'cs_ville ASC, cs_nom ASC, cc_nom ASC' : $this->orderby,
$this->start, $this->limit,
__FILE__, __LINE__);
 
// Récupération des résultats
try {
76,7 → 87,7
* Appelée avec les paramêtres d'url suivant :
* /CoelRecherche/ParDefaut/_
* ou les _ représentent dans l'ordre : mots
* Si un des paramêtres est abscent, il prendre la valeur *
* Si un des paramêtres est absent, il prendre la valeur *
*/
public function getElementNombre($param) {
// Initialisation des variables
85,12 → 96,27
// Pré traitement des paramêtres
$p = $this->pretraiterParametresUrl($param);
 
$fromClause = $whereClause = $joinClause = array();
self::construireFromEtWhere($p, $fromClause, $joinClause, $whereClause).
 
// Construction de la requête
// Il est important de compter le nombre d'association structure-collection différentes pour obtenir le bon nombre
$requete = (($this->distinct) ? 'SELECT DISTINCT' : 'SELECT').' COUNT(cs_id_structure) AS nbre, cc_id_collection '.
$this->construireFromEtWhere($p).
'ORDER BY '.((!is_null($this->orderby)) ? $this->orderby : 'cs_ville ASC, cs_nom ASC').' ';
$requete = sprintf(
'SELECT %s COUNT(cs_id_structure) AS nbre, cc_id_collection '
. ' FROM %s %s'
. ' WHERE %s ORDER BY %s LIMIT %d, %d -- %s:%d',
 
$this->distinct ? 'DISTINCT' : '',
 
implode(',', $fromClause),
implode(' ', $joinClause),
 
$whereClause ? implode(" AND ", $whereClause) : TRUE,
is_null($this->orderby) ? 'cs_ville ASC, cs_nom ASC' : $this->orderby,
$this->start, $this->limit,
__FILE__, __LINE__);
 
 
// Récupération des résultats
try {
$donnees = $this->bdd->query($requete)->fetch(PDO::FETCH_ASSOC);
169,108 → 195,97
return $p;
}
private function construireFromEtWhere($p) {
// Initialisation de variables
$from_et_where = '';
$from = 'FROM coel_collection '.
' LEFT JOIN coel_structure ON (cc_ce_structure = cs_id_structure) ';
$where = 'WHERE ';
static function construireFromEtWhere($p, &$from, &$join, &$where) {
$from = array('coel_collection');
$join = array('LEFT JOIN coel_structure ON (cc_ce_structure = cs_id_structure)');
$where = array();
 
// Gestion du from en fonction des paramêtres
if (isset($p['str-d'])) {// ATTENTION : Remplace $from, doit être situé en première position!
$from = array('coel_structure');
$join = array('LEFT JOIN coel_collection ON (cs_id_structure = cc_ce_structure)');
}
// Construire from et where en fonction des paramêtres
if (isset($p['mots'])) {
$where .= 'AND ('.
" cc_nom LIKE {$p['mots']} ".
" OR cc_truk_nom_alternatif LIKE {$p['mots']} ".
" OR cc_truk_code LIKE {$p['mots']} ".
" OR cc_description LIKE {$p['mots']} ".
" OR cc_description_specialiste LIKE {$p['mots']} ".
" OR cc_historique LIKE {$p['mots']} ".
" OR cs_nom LIKE {$p['mots']} ".
" OR cs_truk_nom_alternatif LIKE {$p['mots']} ".
" OR cs_description LIKE {$p['mots']} ".
" OR cs_adresse_01 LIKE {$p['mots']} ".
" OR cs_adresse_02 LIKE {$p['mots']} ".
" OR cs_ville LIKE {$p['mots']} ".
" OR cs_truk_identifiant_alternatif LIKE {$p['mots']} ".
" OR cs_condition_acces LIKE {$p['mots']} ".
" OR cs_condition_usage LIKE {$p['mots']} ".
" OR cs_truk_telephone LIKE {$p['mots']} ".
" OR cs_courriel LIKE {$p['mots']} ".
" OR cs_truk_url LIKE {$p['mots']} ".
') ';
$where[] = '(' . implode(' OR ', array(
"cc_nom LIKE {$p['mots']}",
"cc_truk_nom_alternatif LIKE {$p['mots']}",
"cc_truk_code LIKE {$p['mots']}",
"cc_description LIKE {$p['mots']}",
"cc_description_specialiste LIKE {$p['mots']}",
"cc_historique LIKE {$p['mots']}",
"cs_nom LIKE {$p['mots']}",
"cs_truk_nom_alternatif LIKE {$p['mots']}",
"cs_description LIKE {$p['mots']}",
"cs_adresse_01 LIKE {$p['mots']}",
"cs_adresse_02 LIKE {$p['mots']}",
"cs_ville LIKE {$p['mots']}",
"cs_truk_identifiant_alternatif LIKE {$p['mots']}",
"cs_condition_acces LIKE {$p['mots']}",
"cs_condition_usage LIKE {$p['mots']}",
"cs_truk_telephone LIKE {$p['mots']}",
"cs_courriel LIKE {$p['mots']}",
"cs_truk_url LIKE {$p['mots']}") . ')');
}
 
if (isset($p['sci'])) {
if ($p['sci'] === true) {
$where .= 'AND csv_mark_visite_avec_motif = 1 ';
$where[] = 'csv_mark_visite_avec_motif = 1';
} else if ($p['sci'] === false) {
$where .= 'AND csv_mark_acces_ss_motif = 1 ';
$where[] = 'csv_mark_acces_ss_motif = 1';
}
}
 
if (isset($p['bot'])) {
$where .= "AND ccb_ce_truk_type IN ({$p['bot']}) ";
$where[] = "ccb_ce_truk_type IN ({$p['bot']})";
}
if (isset($p['zg'])) {
$where .= "AND cc_truk_couverture_lieu LIKE {$p['zg']} ";
$where[] = "cc_truk_couverture_lieu LIKE {$p['zg']}";
}
if (isset($p['p'])) {
$where .= "AND cp_fmt_nom_complet LIKE {$p['p']} ";
$where[] = "cp_fmt_nom_complet LIKE {$p['p']}";
}
if (isset($p['pr'])) {
$where .= "AND ccap_id_role IN ({$p['pr']}) ";
$where[] = "ccap_id_role IN ({$p['pr']})";
}
if (isset($p['str-d'])) {
$where .= 'AND cs_ce_truk_pays = 2654 '.
"AND cs_code_postal LIKE '{$p['str-d']}%' ";
$where[] = 'cs_ce_truk_pays = 2654';
$where[] = "cs_code_postal LIKE '{$p['str-d']}%'";
}
 
if (isset($p['veg'])) {
$veg = explode(',', $p['veg']);
$veg_nbre = count($veg);
if ($veg_nbre == 1) {
$where .= "AND ccb_truk_nature LIKE '%{$p['veg']}%' ";
$where[] = "ccb_truk_nature LIKE '%{$p['veg']}%'";
} else {
$recherche = array();
foreach ($veg as $id) {
$recherche[] = "ccb_truk_nature LIKE '%$id%'";
}
$where .= 'AND ('.implode(' OR ', $recherche).') ';
$where[] = '('.implode(' OR ', $recherche).') ';
}
}
if (isset($p['projets'])) {
$where .= "AND cc_ce_projet IN ({$p['projets']}) ".
"AND cs_ce_projet IN ({$p['projets']}) ";
$where[] = "cc_ce_projet IN ({$p['projets']})";
$where[] = "cs_ce_projet IN ({$p['projets']})";
}
$where = str_replace('WHERE AND', 'WHERE', $where);
// Gestion du from en fonction des paramêtres
if (isset($p['str-d'])) {// ATTENTION : Remplace $from, doit être situé en première position!
$from = 'FROM coel_structure '.
' LEFT JOIN coel_collection ON (cs_id_structure = cc_ce_structure) ';
}
 
if (isset($p['sci'])) {
$from .= ' LEFT JOIN coel_structure_valorisation ON (cs_id_structure = csv_id_structure) ';
$join[] = 'LEFT JOIN coel_structure_valorisation ON (cs_id_structure = csv_id_structure)';
}
if (isset($p['bot']) || isset($p['veg'])) {
$from .= ' LEFT JOIN coel_collection_botanique ON (cc_id_collection = ccb_id_collection) ';
$join[] = 'LEFT JOIN coel_collection_botanique ON (cc_id_collection = ccb_id_collection)';
}
if (isset($p['p']) || isset($p['pr'])) {
$from .= ' LEFT JOIN coel_collection_a_personne ON (cc_id_collection = ccap_id_collection) ';
$join[] = 'LEFT JOIN coel_collection_a_personne ON (cc_id_collection = ccap_id_collection)';
}
if (isset($p['p'])) {
$from .= ' LEFT JOIN coel_personne ON (ccap_id_personne = cp_id_personne) ';
$join[] = 'LEFT JOIN coel_personne ON (ccap_id_personne = cp_id_personne)';
}
// Retour du From et Where associé
if (count($p) == 0) {
$from_et_where = $from;
} else {
$from_et_where = $from.$where;
}
return $from_et_where;
}
}
}
?>