Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 715 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
705 delphine 1
<?php
2
/** Exemple lancement:
3
 * /opt/lampp/bin/php -d memory_limit=3500M ~/web/eflore-projets/scripts/cli.php sauvages -a chargerTous
4
*/
5
class Sauvages extends EfloreScript {
6
	private $contenu_fichier = array();
7
	protected $parametres_autorises = array(
8
			'-f' => array(true, true, 'Nom du fichier ou du dossier à traiter'));
9
 
10
	public function executer() {
11
		// Lancement de l'action demandée
12
		try {
13
			$this->initialiserProjet('sauvages');
14
 
15
			$cmd = $this->getParametre('a');
16
			switch ($cmd) {
17
				case 'chargerTous' :
18
					$this->creerXmlTaxons();
19
					$this->creerXmlCriteres();
20
					break;
21
				case 'creerXmlCriteres' :
22
					$this->creerXmlCriteres();
23
					break;
24
				case 'creerXmlTaxons' :
25
					$this->creerXmlTaxons();
26
					break;
27
				default :
28
					throw new Exception("Erreur : la commande '$cmd' n'existe pas!");
29
			}
30
		} catch (Exception $e) {
31
			$this->traiterErreur($e->getMessage());
32
		}
33
	}
34
 
35
	public function creerXmlCriteres() {
36
		$this->recupererContenuFichier();
37
		$criteres_label = $this->contenu_fichier[0];
38
		unset($this->contenu_fichier[0]);
39
		$description_label = $this->contenu_fichier[1];
40
		unset($this->contenu_fichier[1]);
41
		$criteres_valeurs = $this->extraireValeurs();
42
		$xml = $this->$formerXmlCriteres($criteres_label, $description_label, $criteres_valeurs);//print_r($xml);
43
		file_put_contents("./xml_criteres.xml", $xml);
44
	}
45
 
46
 
47
	public function formerXmlCriteres($label, $description, $valeurs) {
48
		$xml='<keys>
49
				<groupe id="1">
50
					<nom>Arbres</nom>
51
					<media>images/pictos/icone_arbre.png</media>
52
					<criteres>';
53
		foreach ($label as $id=>$criteres) {
54
			if ($id > 2) {
55
				$xml .= '<critere id="1.'.$id.'">
56
							<label>'.$criteres.'</label>
57
							<description>'.$description[$id].'</description>
58
							<valeurs>';
59
				foreach ($valeurs[$id] as $id_val=>$valeur) {
60
					if ($valeur != "0") {
61
						$crit = explode(" ", $criteres);
62
						$val = explode(" ", $valeur);
63
						$image = $crit[0]."_".$val[0];
64
						$xml .= '<valeur code="1.'.$id.'.'.$id_val.'" media="images/pictos/'.$image.'.png">'.$valeur.'</valeur>';
65
					}
66
				}
67
				$xml .= '</valeurs></critere>';
68
			}
69
		}
70
		$xml .= "</criteres>
71
			</groupe>
72
			</keys>";
73
		return $xml;
74
	}
75
 
76
	private function extraireValeurs() {
77
		$valeurs = array();
78
		foreach ($this->contenu_fichier as $ligne) {
79
			$i = 0;
80
			foreach ($ligne as $critere) {
81
				if (!isset($valeurs[$i]) || !in_array(trim($critere), $valeurs[$i])) {
82
					$valeurs[$i][] = trim($critere);
83
				}
84
				$i++;
85
			}
86
		}
87
		return $valeurs;
88
	}
89
 
90
 
91
	public function creerXmlTaxons() {
92
		$this->recupererContenuFichier();
93
		$criteres_label = array_flip($this->contenu_fichier[0]);
94
		unset($this->contenu_fichier[0]);
95
		unset($this->contenu_fichier[1]);
96
		$criteres_valeurs = $this->extraireValeurs();
97
		$xml = $this->formerXmlTaxons($criteres_label, $criteres_valeurs); //print_r($xml);
98
		file_put_contents("./xml_taxons.xml", $xml);
99
	}
100
 
101
	public function formerXmlTaxons($criteres_label, $criteres_valeurs) {
102
		$xml='<?xml version="1.0" ?><TAXONS SUBJECT="XML">';
103
		$rest = new RestClient();
104
		foreach ($this->contenu_fichier as $id=>$taxon) {
105
			$infos_taxon = $this->rechercherInfosTaxon($rest, $taxon[$criteres_label["nom scientifique"]]);
106
			$xml .= '<TAXON id="'.$id.'" value="'.
107
				ucfirst($taxon[$criteres_label["nom vernaculaire"]]).'" sciName="'.$taxon[$criteres_label["nom scientifique"]].'" groupe="1">
108
			<DESCRIPTION>'.$infos_taxon["description"].'</DESCRIPTION>
109
			<PICTURES>';
110
			if ($infos_taxon["images"] != array()) {
111
				foreach ($infos_taxon["images"] as $image) {
112
					$xml .= '<PICTURE media="'.$image["binaire.href"].'"><author>'.$image["auteur.libelle"].'</author></PICTURE>';
113
				}
114
			}
115
			$xml .= '</PICTURES><CRITERIAS>';
116
			for ($i=3; $i < count($taxon); $i++) {
117
				if (trim($taxon[$i]) !== "0") {
118
					$xml .= '<VALUE code="1.'.$i.'.'.array_search(trim($taxon[$i]), $criteres_valeurs[$i]).'"/>';
119
				}
120
			}
121
			$xml .= '</CRITERIAS></TAXON>';
122
		}
123
		$xml .= "</TAXONS>";
124
		return $xml;
125
	}
126
 
127
	private function rechercherInfosTaxon($rest, $ns) {
128
		$info = array("description" => "", "images" => array());
129
		$reponse = $this->consulterWebService($rest, "bdtfx", "taxons", "?masque=".urlencode(rtrim($ns)));
130
		if (is_array($reponse["resultat"])) {
131
			$id = key($reponse["resultat"]);
132
		} else {
133
			$reponse = $this->consulterWebService($rest, "bdtfx", "noms", "??retour.champs=nom_retenu.id&masque=".urlencode(rtrim($ns)));
134
			if (is_array($reponse["resultat"])) {
135
				$id = $reponse["resultat"][key($reponse["resultat"])]["nom_retenu.id"];
136
			}
137
		}
138
		$reponse = $this->consulterWebService($rest, "coste", "textes", "/bdtfx.nn:".$id);
139
		if ($reponse['texte'] != ""){
140
			$info["description"] = str_replace("**", "", $reponse['texte']);
141
			$info["description"] = str_replace("//", "", $info["description"]);
142
		} else {
143
			$reponse = $this->consulterWebService($rest, "baseflor", "informations", "/bdtfx.nn:".$id);
144
			$info["description"] = $this->formaterBaseflor($reponse);
145
		}
146
		$reponse = $this->consulterWebService($rest, "cel", "images", "?navigation.limite=5&retour.format=CRS&masque.nn=".$id);
147
		$info["images"] = $reponse["resultats"];
148
		return $info;
149
	}
150
 
151
	private function formaterBaseflor($reponse) {
152
		$description = "En cours de rédaction.";
153
		if ($reponse["idiotaxon"] != "") {
154
			$description = $reponse["idiotaxon"]." – ".
155
							"Forme végétale : ".$reponse["form_vegetale"]."; ".
156
							"Inflorescence : ".$reponse["inflorescence"]."; ".
157
							"Couleur de la fleur : ".$reponse["couleur_fleur"]."; ".
158
							"Sexualité : ".$reponse["sexualite"]."; ".
159
							"Fruit : ".$reponse["fruit"]."; ".
160
							"Pollinisation : ".$reponse["pollinisation"]."; ".
161
							"Dissémination : ".$reponse["dissemination"]."; ".
162
							"Ecologie : ".$reponse["carac_ecolo"]." – ".
163
							"Chorologie : ".$reponse["chorologie"]."; ".
164
							"Floraison :  ".$reponse["floraison"];
165
		}
166
		return $description;
167
	}
168
 
169
	private function consulterWebService($rest, $projet,$resssource,$parametres) {
170
		$url_id = "http://www.tela-botanica.org/service:eflore:0.1/".$projet."/".$resssource.$parametres;
171
		$reponse = $rest->consulter($url_id);
172
		return json_decode($reponse, true);
173
	}
174
 
175
 
176
	private function recupererContenuFichier() {
177
		$nomFichier = Config::get('dossierCsv').Config::get('projet');
178
		if ($nomFichier && file_exists($nomFichier) ){
179
			$extensionFichier = strtolower(strrchr($nomFichier, '.'));
180
			if ($extensionFichier === ".csv"){
181
				$file = new SplFileObject($nomFichier);
182
				$file->setFlags(SplFileObject::SKIP_EMPTY);
183
				$i = 0;
184
				echo "Traitement du fichier : ";
185
				while (!$file->eof()){
186
					$ligne_csv = $file->fgetcsv($delimiter = ';');
187
						$this->contenu_fichier[$i] = $ligne_csv;
188
					echo str_repeat(chr(8), ( strlen( $i ) + 1 ))."\t".$i++;
189
				}
190
				echo "\n";
191
			} else {
192
				$this->traiterErreur("Le fichier : $nomFichier n'est pas au format csv.");
193
			}
194
		} else {
195
			$this->traiterErreur("Le fichier : $nomFichier n'existe pas.");
196
		}
197
	}
198
}
199
?>