Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 655 | Rev 665 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 655 Rev 660
Line 269... Line 269...
269
 
269
 
270
	public function assemblerLaRequete() {
270
	public function assemblerLaRequete() {
271
		if ( strrpos($this->format_reponse, 'noms/stats/') === false ) {
271
		if ( strrpos($this->format_reponse, 'noms/stats/') === false ) {
272
			$this->mettreAuFormat(); //Ds CommunNomsTaxons.php
272
			$this->mettreAuFormat(); //Ds CommunNomsTaxons.php
273
		}
273
		}
274
		$requete = 	' SELECT '.$this->distinct.' '.$this->requete_champ.
274
		$requete = 'SELECT '.$this->retournerChamps().' '.
275
					' FROM '.$this->table
275
			"FROM {$this->table} ".
276
					.$this->retournerRequeteCondition()
276
			$this->retournerRequeteCondition().' '.
-
 
277
			$this->requete_group_by.' '.
277
					.$this->requete_group_by
278
			$this->retournerOrderBy().' '.
-
 
279
			$this->formerRequeteLimite();
278
					.$this->formerRequeteLimite();
280
//		Debug::printr($requete);
-
 
281
		return $requete;
-
 
282
	}
-
 
283
 
-
 
284
	public function retournerChamps() {
-
 
285
		$sql = '';
-
 
286
		if ($this->distinct) {
-
 
287
			$sql .= $this->distinct.' ';
-
 
288
		}
-
 
289
		if ($this->requete_champ) {
-
 
290
			$sql .= $this->requete_champ.' ';
-
 
291
		}
-
 
292
 
-
 
293
		// Champs "virtuels" pour tier sur l'ensemble des résultats
-
 
294
		if (isset($this->parametres['retour.tri'])) {
-
 
295
			list($champ, $ordre) = $this->decouperParametreRetourTri();
-
 
296
			if ($champ == 'retenu') {
-
 
297
				$sql .= ", IF(num_nom = num_nom_retenu, '0', '1') AS nom_retenu_tri ";
-
 
298
			}
Line -... Line 299...
-
 
299
		}
279
		return $requete;
300
 
Line 280... Line 301...
280
 
301
		return $sql;
281
	}
-
 
282
 
-
 
283
	public function formerRequeteLimite() {
302
	}
284
		if ($this->format_reponse != 'noms' && $this->format_reponse != 'noms/id/relations/synonymie'
-
 
285
			&& $this->format_reponse != 'noms/id/relations/homonymie') {
303
 
286
			$this->requete_limite = '';
-
 
287
		} elseif (($depart = $this->limite_requete['depart']) > ($this->total_resultat = $this->recupererTotalResultat())) {
304
	public function decouperParametreRetourTri() {
288
			$this->limite_requete['depart'] =
305
		$tri = array('', '');
289
				(($this->total_resultat - $this->limite_requete['limite']) < 0) ? 0 : ($this->total_resultat - $this->limite_requete['limite']);
306
		if (isset($this->parametres['retour.tri'])) {
290
			$this->requete_limite = ' LIMIT '.$this->limite_requete['depart'].', '.$this->limite_requete['limite'];
307
			if (preg_match('/^(retenu)(?:,(ASC|DESC)|)$/', $this->parametres['retour.tri'], $match))
291
		} else {
308
			$tri[0] = $match[1];
292
			$this->requete_limite = ' LIMIT '.$this->limite_requete['depart'].', '.$this->limite_requete['limite'];
309
			$tri[1] = isset($match[2]) ? $match[2] : '';
Line 293... Line 310...
293
		}
310
		}
294
		return $this->requete_limite;
311
		return $tri;
295
	}
312
	}
296
 
313
 
297
	public function retournerRequeteCondition() {
314
	public function retournerRequeteCondition() {
298
		$condition = '';
315
		$condition = '';
299
		if ($this->requete_condition) {
316
		if ($this->requete_condition) {
Line -... Line 317...
-
 
317
			$condition = ' WHERE '.implode(' AND ', $this->requete_condition);
-
 
318
		}
-
 
319
		return $condition;
-
 
320
	}
-
 
321
 
-
 
322
	public function retournerOrderBy() {
-
 
323
		$orderBy = array();
-
 
324
 
-
 
325
		// Tri sur l'ensemble des résultats
-
 
326
		if (isset($this->parametres['retour.tri'])) {
-
 
327
			list($champ, $ordre) = $this->decouperParametreRetourTri();
-
 
328
			if ($champ == 'retenu') {
-
 
329
				$orderBy[] = "nom_retenu_tri $ordre";
-
 
330
			}
-
 
331
		}
-
 
332
		// Tri par défaut
-
 
333
		if ($this->format_reponse == 'noms') {
-
 
334
			$orderBy[] = 'nom_sci ASC';
-
 
335
		}
-
 
336
 
-
 
337
		$sql = '';
-
 
338
		if (count($orderBy > 0)) {
-
 
339
			$sql = 'ORDER BY '.implode(', ', $orderBy).' ';
-
 
340
		}
-
 
341
		return $sql;
-
 
342
	}
-
 
343
 
-
 
344
	public function formerRequeteLimite() {
-
 
345
		if ($this->format_reponse != 'noms' && $this->format_reponse != 'noms/id/relations/synonymie'
-
 
346
				&& $this->format_reponse != 'noms/id/relations/homonymie') {
-
 
347
			$this->requete_limite = '';
-
 
348
		} elseif (($depart = $this->limite_requete['depart']) > ($this->total_resultat = $this->recupererTotalResultat())) {
-
 
349
			$this->limite_requete['depart'] =
-
 
350
			(($this->total_resultat - $this->limite_requete['limite']) < 0) ? 0 : ($this->total_resultat - $this->limite_requete['limite']);
-
 
351
			$this->requete_limite = ' LIMIT '.$this->limite_requete['depart'].', '.$this->limite_requete['limite'];
Line 300... Line 352...
300
			$condition = ' WHERE '.implode(' AND ', $this->requete_condition);
352
		} else {
301
		}
353
			$this->requete_limite = ' LIMIT '.$this->limite_requete['depart'].', '.$this->limite_requete['limite'];
302
		return $condition;
354
		}
303
	}
355
		return $this->requete_limite;
Line 564... Line 616...
564
		return $reponse;
616
		return $reponse;
565
	}
617
	}
Line 566... Line 618...
566
 
618
 
567
	public function formaterEnJsonMax($resultat) {
619
	public function formaterEnJsonMax($resultat) {
-
 
620
		$this->recupererTableSignification('correspondance_champs,champs_api,champs_comp');
-
 
621
 
-
 
622
		// TODO : améliorer le trie des résultats
568
		$this->recupererTableSignification('correspondance_champs,champs_api,champs_comp');
623
		// ATTENTION : ce comportement est étrange
569
		$masque = $this->recupererMasquePrincipal();
624
		$masque = $this->recupererMasquePrincipal();
570
		if (isset($masque)) {
625
		if (isset($masque)) {
571
			$resultat = $this->trierRechercheFloue($this->parametres[$masque[0]], $resultat, $masque[1]);
626
			$resultat = $this->trierRechercheFloue($this->parametres[$masque[0]], $resultat, $masque[1]);
-
 
627
		}
572
		}
628
 
573
		$table_retour_json['entete'] = $this->remplirJsonEntete();
629
		$table_retour_json['entete'] = $this->remplirJsonEntete();
574
		$table_retour_json['resultat'] = $this->remplirJsonResultat($resultat);
630
		$table_retour_json['resultat'] = $this->remplirJsonResultat($resultat);
575
		return $table_retour_json;
631
		return $table_retour_json;
Line 576... Line 632...
576
	}
632
	}
577
 
633
 
578
	public function remplirJsonResultat($resultat) {
634
	public function remplirJsonResultat($resultat) {
579
		$champs = null;
635
		$champs = null;
580
		if (array_key_exists('retour.champs', $this->parametres)) {
636
		if (array_key_exists('retour.champs', $this->parametres)) {
-
 
637
			$champs = explode(',', $this->parametres['retour.champs']);
581
			$champs = explode(',', $this->parametres['retour.champs']);
638
		}
582
		}
-
 
583
		$noms = array();
639
 
584
		$nomsRetenus = array();
640
		$noms = array();
585
		foreach ($resultat as $tab) {
641
		foreach ($resultat as $tab) {
586
			$this->table_retour = array();
642
			$this->table_retour = array();
587
			$num = $tab['num_nom'];
643
			$num = $tab['num_nom'];
Line 592... Line 648...
592
				$reponse_id = $this->formaterId($tab);
648
				$reponse_id = $this->formaterId($tab);
593
				$this->table_retour = array();
649
				$this->table_retour = array();
594
				$this->ajouterChampsPersonnalises($champs, $reponse_id);
650
				$this->ajouterChampsPersonnalises($champs, $reponse_id);
595
				$retour = array_merge($retour, $this->table_retour);
651
				$retour = array_merge($retour, $this->table_retour);
596
			}
652
			}
597
			// Sépare les noms retenus des autres noms
-
 
598
			if (isset($retour['retenu']) && $retour['retenu'] == 'true') {
-
 
599
				$nomsRetenus[$num] = $retour;
-
 
600
			} else {
-
 
601
				$noms[$num] = $retour;
653
			$noms[$num] = $retour;
602
			}
-
 
603
		}
654
		}
604
		// Trie par ordre alphabétique de nom_sci
-
 
605
		$nomsRetenus = Tableau::trierMD($nomsRetenus, array('nom_sci' => SORT_ASC));
-
 
606
		$noms = Tableau::trierMD($noms, array('nom_sci' => SORT_ASC));
-
 
607
 
-
 
608
		// Rassemble la liste des noms avec les noms retenus en premier
-
 
609
		$nomsRetour = array();
-
 
610
		Tableau::etendre($nomsRetour, $nomsRetenus);
-
 
611
		Tableau::etendre($nomsRetour, $noms);
-
 
Line 612... Line 655...
612
 
655
 
613
		return  $nomsRetour;
656
		return  $noms;
Line 614... Line 657...
614
	}
657
	}
615
 
658
 
616
	public function remplirJsonEntete() {
659
	public function remplirJsonEntete() {