Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 11 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
11 jpm 1
<?php
2
/**
3
 * declare(encoding='UTF-8');
4
 * Exemple de lancement du script : :
392 jpm 5
 * /opt/lampp/bin/php cli.php coste/creation_col -a nomSciHtml -v 3
11 jpm 6
 * Classe permettant de créer le nom scientifique au format HTML.
7
 *
8
 * @category	php 5.2
9
 * @package		eflore/scripts/coste
10
 * @author		Jennifer DHÉ <jennifer@tela-botanica.org>
11
 * @author		Jean-Pascal MILCENT <jpm@tela-botanica.org>
12
 * @copyright	Copyright (c) 2011, Tela Botanica (accueil@tela-botanica.org)
13
 * @license		http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
14
 * @license		http://www.gnu.org/licenses/gpl.html Licence GNU-GPL
15
 * @version		$Id$
16
 */
17
class Creationcol extends Script {
18
	protected $bdd = null;
19
	protected $table = null;
20
	protected $num = null;
21
	protected $compo_nom = array();
22
	protected $abbr = array (
23
		'infra-gen.' 	=>	'Infra-Genre',
24
		'sect.'			=> 'Section',
25
		'subsect.'		=> 'Sous-Section',
26
		'ser.'			=> 'Série',
27
		'subser.'		=> 'Sous-Série',
28
		'gr.'			=> 'Groupe',
29
		'agg.' 			=> 'Agrégat',
30
		'sp.' 			=> 'Espèce',
31
		'subsp.'		=> 'Sous-Espèce',
32
		'infra-sp.'		=> 'Infra-Espèce',
33
		'var.'			=> 'Variété',
34
		'subvar.'		=> 'Sous-Variété',
35
		'fa'			=> 'Forme',
36
		'subf.'			=> 'Sous-Forme',
37
		'f. sp.'		=> 'Forma species',
38
		'proles' 		=> 'Race prole'
39
	);
40
	private $nomSciTpl = '<span class=sci>%s</span>';
41
	private $nomSupraGenTpl = '<span class="supra_gen">%s</span>';
42
	private $genTpl = '<span class="gen">%s</span>';
43
	private $infraGenTpl = '<span class="infra-gen">%s</span>';
44
	private $spFHTpl = '<span class="gen">%s</span> <span class="sp">%s</span>';
45
	private $typeEpitheteTpl = '<abbr class="type_epithete" title="%s">%s</abbr>';
46
	private $infraSpFHTpl = '<span class="gen">%s</span> <span class="sp">%s</span> <abbr class="type-epithete" title="%s">%s</abbr> <span class="infra-sp">%s</span>';
47
	private $formuleHybTpl = '<span class="formule-hyb">%s</span>';
48
	protected $table_requete;
49
	protected $limite_req_tuples = 10000;
50
	protected $depart_req_tuples = 0;
51
 
52
	protected $parametres_autorises = array(
53
		'-t' => array(true, true, 'Nom de la table qui doit être traitée'));
54
 
55
	public function executer() {
56
		// Lancement de l'action demandée
57
		$cmd = $this->getParametre('a');
58
	    switch ($cmd) {
59
			case 'nomSciHtml' :
60
				$this->genererNomSciHtml();
61
				break;
62
			default :
63
				$this->traiterErreur('Erreur : la commande "%s" n\'existe pas!', array($cmd));
64
		}
65
    }
66
 
67
	private function genererNomSciHtml() {
68
		$this->initialiser();
69
 
70
		$nbtot = $this->recupererNbTotalTuples();
71
		while ($this->depart_req_tuples < $nbtot) {
72
			$resultat = $this->recupererTuples();
73
			$nb_tot_res = count($resultat);
74
			$compteur = 1;
75
			foreach ($resultat as $tuples => $infos) { //pour chaque tuple
76
				$this->initialiserVariables($infos);
77
 
78
				$nomSciHtml = '';
79
				$nomSciHtml .= $this->ajouterBaliseNomSupraGen();
80
				$nomSciHtml .= $this->verifierHybridite($this->compo_nom['genre'], 'gen');
81
				$nomSciHtml .= $this->ajouterBaliseInfraGen();
82
				$nomSciHtml .= $this->verifierHybridite($this->compo_nom['epithete_sp'], 'sp');
83
				$nomSciHtml .= $this->ajouterBaliseTypeInfraSp();
84
				$nomSciHtml .= $this->verifierHybridite($this->compo_nom['epithete_infra_sp'], 'infra-sp');
85
				$nomSciHtml .= $this->ajouterCultivarGpComm();
86
 
87
				if ($nomSciHtml != '') {
88
					$this->table_requete[$this->num] = sprintf($this->nomSciTpl, trim($nomSciHtml));
89
				}
90
				if (count($this->table_requete) == 1000) {
91
					$this->lancerRequeteModification();
92
					array_splice($this->table_requete, 0);
93
				} elseif ($compteur == $nb_tot_res) {
94
					$this->lancerRequeteModification();
95
				}
96
				$compteur++;
97
				$this->afficherAvancement('Génération des noms scientifique au format HTML en cours');
98
			}
99
		}
100
		echo "\n";
101
	}
102
 
103
	private function initialiser() {
104
		$this->bdd = new Bdd();
105
		$this->table = $this->getParametre('t');
106
		$this->preparerTable();
107
	}
108
 
109
	private function preparerTable() {
110
		$requete = "SHOW COLUMNS FROM {$this->table} LIKE 'nom_sci_html' ";
111
		$resultat = $this->bdd->recuperer($requete);
112
		if ($resultat === false) {
113
			$requete = 	"ALTER TABLE {$this->table} ".
114
						'ADD nom_sci_html VARCHAR( 500 ) '.
115
						'CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ';
116
			$this->bdd->requeter($requete);
117
		}
118
	}
119
 
120
	private function recupererNbTotalTuples(){
121
		$req = "SELECT count(*) AS nb FROM {$this->table} ";
122
		$res = $this->bdd->recuperer($req);
123
		return $res['nb'];
124
	}
125
 
126
	private function recupererTuples() {
127
		$requete = 'SELECT 	num_nom, rang, nom_supra_generique, genre, epithete_infra_generique, '.
128
					'	epithete_sp, type_epithete, epithete_infra_sp,cultivar_groupe, '.
129
					'	nom_commercial, cultivar '.
130
					"FROM {$this->table} ".
131
					"LIMIT {$this->depart_req_tuples},{$this->limite_req_tuples} ";
132
 
133
		$resultat = $this->bdd->recupererTous($requete);
134
		$this->depart_req_tuples += 10000;
135
		return $resultat;
136
	}
137
 
138
	private function initialiserVariables($infos) {
139
		$this->num = $infos['num_nom'];
140
		$this->compo_nom = $infos;
141
	}
142
 
143
	private function ajouterBaliseNomSupraGen() {
144
		$html = '';
145
		if ($this->compo_nom['nom_supra_generique'] != '') {
146
			$html = sprintf($this->nomSupraGenTpl, $this->compo_nom['nom_supra_generique']);
147
		}
148
		return $html;
149
	}
150
 
151
	private function ajouterTypeEpithete($type) {
152
		if (!array_key_exists($type, $this->abbr)) {
153
			$this->abbr[$type] = $type;
154
		}
155
	}
156
 
157
	private function ajouterBaliseInfraGen() {
158
		$html = '';
159
		if ($this->verifierTypeInfraGen()) {
160
			$html .= $this->ajouterBaliseTypeInfraGen();
161
		} else {
162
			if ($this->avoirInfo('epithete_infra_generique')) {
163
				$html .= sprintf($this->infraGenTpl, $this->compo_nom['epithete_infra_generique']);
164
			}
165
		}
166
		return $html;
167
	}
168
 
169
	private function verifierTypeInfraGen() {
170
		$ok = false;
171
		if ($this->compo_nom['type_epithete'] != '' && $this->compo_nom['epithete_infra_generique'] != '') {
172
			$this->ajouterTypeEpithete($this->compo_nom['type_epithete']);
173
			$ok = true;
174
		}
175
		return $ok;
176
	}
177
 
178
	private function ajouterBaliseTypeInfraGen() {
179
		$html = '';
180
		$type = $this->compo_nom['type_epithete'];
181
 
182
		if ($type == 'agg.') { // Ajout de l'infra gen avant le type s'il est égal à agg.
183
			$html .= ' '.$this->ajouterBaliseInfraGen().
184
				' '.sprintf($this->typeEpitheteTpl, $this->abbr[$type], $type);
185
		} else {
186
			$html .= ' '.sprintf($this->typeEpitheteTpl, $this->abbr[$type], $type).
187
				' '.$this->ajouterBaliseInfraGen();
188
		}
189
		return $html;
190
	}
191
 
192
	private function ajouterBaliseTypeInfraSp() {
193
		$html = '';
194
		$type = $this->compo_nom['type_epithete'];
195
		$infraSp = $this->compo_nom['epithete_infra_sp'];
196
 
197
		if ($infraSp != '') {
198
			if ($type != '') {
199
				$this->ajouterTypeEpithete($type);
200
				$html = sprintf($this->typeEpitheteTpl, $this->abbr[$type], $type);
201
			} else {
202
				$message = "Nom #%s contient un épithète infra-spécifique mais son type n'est pas renseigné.";
203
				$this->traiterErreur($message, array($this->num));
204
			}
205
		}
206
		return $html;
207
	}
208
 
209
	private function ajouterCultivarGpComm() {
210
		$html = '';
211
		if ($this->avoirInfo('cultivar_groupe')) {
212
			$html .= ' '.$this->ajouterCultivarGroupe();
213
		}
214
		if ($this->avoirInfo('nom_commercial')) {
215
			$html .= ' <span class="commercial">'.$this->compo_nom['nom_commercial'].'</span>';
216
		}
217
		if ($this->avoirInfo('cultivar')) {
218
			$html .= ' <span class="cultivar">\''.$this->compo_nom['cultivar'].'\'</span>';
219
		}
220
		return $html;
221
	}
222
 
223
	private function avoirInfo($valeur) {
224
		return (isset($this->compo_nom[$valeur]) && $this->compo_nom[$valeur] != '') ? true : false;
225
	}
226
 
227
	/**
228
	 * Permet d'ajouter les groupes de cultivar en fonction de la présence d'un cultivar et de la présence d'un grex
229
	 *
230
	 * L'ensemble des individus obtenu par une fécondation particulière s'appelle un le Grex (que pour les orchidées).
231
	 * Au sein du grex, certains individus peuvent se distinguer par des formes, des coloris ou autres qui font que
232
	 * l'obtenteur du grex va les sélectionner.
233
	 * les noms de groupes de cultivars sont suivis de l'abréviation « Gp » et placés entre parenthèses
234
	 * les noms de grex, s'ils sont utilisés devant une épithète de cultivar, ne se mettent pas entre parenthèses
235
	 *	 ex : Cymbidium Alexanderi gx 'Westonbirt' (cultivar_groupe = Alexanderi gx) ;
236
	 * les noms de groupe se mettent entre parenthèses s'ils sont devant une épithète de cultivar
237
	 *	 ex : Dracaena fragrans (Deremenis Gp) 'Christianne' (cultivar_groupe = Deremenis)
238
	 * Un grex peut contenir des groupes (rédaction d'un exemple de l'ICNCP)
239
	 *	 ex : × Rhyncosophrocattleya Marie Lemon Stick grex Francis Suzuki Group
240
	 *	 ou : × Rhyncosophrocattleya Marie Lemon Stick gx Francis Suzuki Gp
241
	 * @param unknown_type $val
242
	 *
243
	 */
244
	private function ajouterCultivarGroupe() {
245
		$html = '';
246
		if ($this->avoirInfo('cultivar_groupe')) { //si le champ cultivar_groupe n'est pas vide
247
			if ($this->compo_nom['cultivar']) { //si il y a un cultivar, on ajoute des parenthèses au group (mais pas au grex)
248
				if (strrpos($this->compo_nom['cultivar_groupe'], ' gx ') !== false) { //si le grex est composé de groupe
249
					$tab_gp = explode(' gx ', $this->compo_nom['cultivar_groupe']);
250
					$html = ' <span class="gp">'.$tab_gp[0].
251
							' <abbr title="grex">gx </abbr>('.$tab_gp[1].
252
							' <abbr title="groupe">Gp</abbr>)</span>';
253
				} elseif (strrpos($this->compo_nom['cultivar_groupe'], ' gx') !== false) { //si il y a un grex et pas de groupe
254
					$tab_gp = explode(' gx', $this->compo_nom['cultivar_groupe']);
255
					$html = ' <span class="gp">'.$tab_gp[0].' <abbr title="grex">gx</abbr></span>';
256
				} else { //si il n'y a pas de grex mais un groupe
257
					$html = ' <span class="gp">'.$this->compo_nom['cultivar_groupe'].' <abbr title="groupe">Gp</abbr>)</span>';
258
				}
259
			} else { //s'il n'y a pas de cultivar
260
				if (strrpos($this->compo_nom['cultivar_groupe'], ' gx ') !== false) { //si le grex est composé de groupe
261
					$tab_gp = explode(' gx ', $this->compo_nom['cultivar_groupe']);
262
					$html = ' <span class="gp">'.$tab_gp[0].
263
							' <abbr title="grex">gx</abbr>'.$tab_gp[1].
264
							' <abbr title="groupe">Gp</abbr></span>';
265
				} elseif (strrpos($this->compo_nom['cultivar_groupe'], ' gx') !== false) { //si il y a un grex et pas de groupe
266
					$tab_gp = explode(' gx', $this->compo_nom['cultivar_groupe']);
267
					$html = ' <span class="gp">'.$tab_gp[0].' <abbr title="grex">gx</abbr></span>';
268
				} else { //si il n'y a pas de grex mais un groupe
269
					$html = ' <span class="gp">'.$this->compo_nom['cultivar_groupe'].' <abbr title="groupe">Gp</abbr></span>';
270
				}
271
			}
272
		}
273
		return $html;
274
	}
275
 
276
	/**
277
	 *
278
	 * Permet de repérer s'il s'agit d'un hybride (infra-sp, genre, sp) ou d'une chimère.
279
	 * @param unknown_type $val
280
	 * @param unknown_type $type
281
	 */
282
	private function verifierHybridite($epithete, $type) {
283
		$html = '';
284
		if ($epithete != '') {
285
			if (substr($epithete, 0, 2) == 'x ') {
286
				$html = ' <span class=hyb>x <span class="'.$type.'">'.str_replace('x ', '', $epithete).'</span></span>';
287
			} elseif (substr($epithete, 0, 2) == '+ ') {
288
				$html = ' <span class=chimere>+ <span class="'.$type.'">'.str_replace('+ ', '', $epithete).'</span></span>';
289
			} else if (substr_count($epithete, ' x ') > 1) {// Cas d'une formule d'hybridité comprenant des parents hybrides
290
				$html = ' '.$this->insererBaliseFormuleHyb($epithete);
291
			} elseif (substr_count($epithete, ' x ') == 1) {// Cas d'une formule d'hybridité simple
292
				$html = ' '.$this->ajouterFomuleHybridite($epithete, $type);
293
			} else {
294
				$html = ' <span class="'.$type.'">'.$epithete.'</span>';
295
			}
296
		}
297
		return $html;
298
	}
299
 
300
	private function ajouterFomuleHybridite($formule, $type) {
301
		$tab_x = explode(' x ', $formule);
302
		$formule_hyb = array();
303
		switch ($type) {
304
			case 'gen' 		:
305
				foreach ($tab_x as $hyb) {
306
					$formule_hyb[] = sprintf($this->genTpl, $hyb);
307
				}
308
				break;
309
			case 'sp'		:
310
				foreach ($tab_x as $hyb) {
311
					if (substr_count($hyb, ' ') >= 1) {
312
						list($gen, $sp) = explode(' ', $hyb);
313
						$formule_hyb[] = sprintf($this->spFHTpl, $gen, $sp);
314
					} else {
315
						$avertissement = "Nom #%s : la valeur d'hybride '%s' ne contenait pas d'espace .";
316
						$this->traiterAvertissement($avertissement, array($this->num, $hyb));
317
					}
318
				}
319
				break;
320
			case 'infra-sp' :
321
				foreach ($tab_x as $hyb) {
322
					list($gen, $sp, $typeEpithete, $infraSp) = explode (' ', $hyb);
323
					$formule_hyb[] = sprintf($this->infraSpFHTpl, $gen, $sp, $this->abbr[$typeEpithete], $typeEpithete, $infraSp);
324
				}
325
				break;
326
			default : break;
327
		}
328
		return $this->insererBaliseFormuleHyb(implode(' x ', $formule_hyb));
329
	}
330
 
331
	private function insererBaliseFormuleHyb($formule) {
332
		return sprintf($this->formuleHybTpl, $formule);
333
	}
334
 
335
	private function lancerRequeteModification() {
336
		foreach ($this->table_requete as $id => $html) {
337
			$html = $this->bdd->proteger($html);
338
			$requete = "UPDATE {$this->table} ".
339
				"SET nom_sci_html = $html ".
340
				"WHERE num_nom = $id ";
341
			$res = $this->bdd->requeter($requete);
342
        	if ($res === false) {
343
        		$this->traiterErreur("erreur d'insertion pour le tuple %s", array($id));
344
        	}
345
		}
346
	}
347
}
348
?>