New file |
0,0 → 1,347 |
<?php |
// declare(encoding='UTF-8'); |
/** |
* Widget fournissant des interfaces de saisies simplifiée pour différent projets. |
* Encodage en entrée : utf8 |
* Encodage en sortie : utf8 |
* |
* Cas d'utilisation et documentation : |
* @link http://www.tela-botanica.org/wikini/eflore/wakka.php?wiki=AideCELWidgetSaisie |
* |
* Paramètres : |
* ===> projet = chaine [par défaut : Biodiversite34] |
* Indique quel projet nous voulons charger |
* |
* @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 Copyright (c) 2010, Tela Botanica (accueil@tela-botanica.org) |
*/ |
class Saisie extends WidgetCommun { |
|
const DS = DIRECTORY_SEPARATOR; |
const PROJET_DEFAUT = 'defaut'; |
const WS_SAISIE = "CelWidgetSaisie"; |
const WS_NOM = "noms"; |
private $NS_PROJET_VERSION = "1.01"; |
const EFLORE_API_VERSION = "0.1"; |
private $NS_PROJET = "bdtfx"; |
|
private $projetsVersions = array(); |
private $projet = null; |
private $configProjet = null; |
|
/** |
* Méthode appelée par défaut pour charger ce widget. |
*/ |
public function executer() { |
$retour = null; |
extract($this->parametres); |
|
$this->projet = isset($projet) ? $projet : self::PROJET_DEFAUT; |
$this->chargerConfigProjet(); |
$this->chargerProjetsVersion(); |
|
$service = isset($service) ? $service : 'widget'; |
$methode = $this->traiterNomMethodeExecuter($service); |
if (method_exists($this, $methode)) { |
$retour = $this->$methode(); |
} else { |
$this->messages[] = "Ce type de service '$methode' n'est pas disponible."; |
} |
|
$contenu = null; |
$mime = null; |
if (is_array($retour) && array_key_exists('squelette', $retour)) { |
$ext = (isset($retour['squelette_ext'])) ? $retour['squelette_ext'] : '.tpl.html'; |
if($this->projetASquelette()) { |
$squelette = dirname(__FILE__).self::DS.'squelettes'.self::DS.$this->projet.self::DS.$retour['squelette'].$ext; |
} else { |
$squelette = dirname(__FILE__).self::DS.'squelettes'.self::DS.'defaut'.self::DS.'defaut'.$ext; |
} |
$contenu = $this->traiterSquelettePhp($squelette, $retour['donnees']); |
$mime = isset($retour['mime']) ? $retour['mime'] : null; |
} else { |
if (count($this->messages) == 0) { |
$this->messages[] = "La méthode du sous-service ne renvoie pas les données dans le bon format."; |
} |
$contenu = 'Un problème est survenu : '.print_r($this->messages, true); |
} |
|
$this->envoyer($contenu, $mime); |
} |
|
private function chargerConfigProjet() { |
$fichier_config = dirname(__FILE__).self::DS.'configurations'.self::DS.$this->projet.'.ini'; |
if (file_exists($fichier_config)) { |
if (!$this->configProjet = parse_ini_file($fichier_config)) { |
$this->messages[] = "Le fichier ini '$fichier_config' du projet n'a pu être chargé."; |
} |
} else { |
$this->debug[] = "Le fichier ini '$fichier_config' du projet n'existe pas."; |
} |
} |
|
private function chargerProjetsVersion() { |
if (isset($this->configProjet)) { |
foreach ($this->configProjet as $config => $valeur) { |
if(strstr($config,'.version')) { |
$this->projetsVersions[str_replace('.version', '', $config)] = $valeur; |
} |
} |
} |
} |
|
public function executerWidget() { |
$referentiel_impose = false; |
if (isset($_GET['referentiel']) && $_GET['referentiel'] != '') { |
$this->NS_PROJET = isset($_GET['referentiel']) && $_GET['referentiel'] != '' ? $_GET['referentiel'] : $this->NS_PROJET; |
$this->NS_PROJET_VERSION = $this->projetsVersions[$this->NS_PROJET]; |
$referentiel_impose = true; |
} |
|
$widget['squelette'] = $this->projet; |
$widget['donnees'] = array(); |
$widget['donnees']['url_base'] = sprintf($this->config['chemins']['baseURLAbsoluDyn'], ''); |
$widget['donnees']['url_ws_saisie'] = sprintf($this->config['chemins']['baseURLServicesCelTpl'], self::WS_SAISIE); |
$widget['donnees']['url_ws_annuaire'] = sprintf($this->config['chemins']['baseURLServicesAnnuaireTpl'], 'utilisateur/identite-par-courriel/'); |
|
|
$widget['donnees']['logo'] = isset($_GET['logo']) ? $_GET['logo'] : 'defaut'; |
$widget['donnees']['titre'] = isset($_GET['titre']) ? $_GET['titre'] : 'defaut'; |
$widget['donnees']['titre'] = ($widget['donnees']['titre'] == '0') ? '' : $widget['donnees']['titre']; |
|
// cas du projet par défaut ou bien d'un projet n'ayant pas de squelette spécifique |
if ($this->projet == 'defaut' || $this->projet == 'florileges' || !$this->projetASquelette()) { |
$urlWsNsTpl = $this->config['chemins']['baseURLServicesEfloreTpl']; |
$urlWsNs = sprintf($urlWsNsTpl, self::EFLORE_API_VERSION, $this->NS_PROJET, self::WS_NOM); |
$urlWsNsSansRef = sprintf($urlWsNsTpl, self::EFLORE_API_VERSION, '{referentiel}', self::WS_NOM); |
$widget['donnees']['url_ws_autocompletion_ns'] = $urlWsNs; |
$widget['donnees']['url_ws_autocompletion_ns_tpl'] = $urlWsNsSansRef; |
$widget['donnees']['ns_referentiel'] = $this->NS_PROJET.':'.$this->NS_PROJET_VERSION; |
$widget['donnees']['ns_projet'] = $this->NS_PROJET; |
$widget['donnees']['ns_version'] = $this->NS_PROJET_VERSION; |
$widget['donnees']['referentiel_impose'] = $referentiel_impose; |
$widget['donnees']['projets_versions'] = $this->projetsVersions; |
$widget['donnees']['espece_imposee'] = false; |
$widget['donnees']['nn_espece_defaut'] = ''; |
$widget['donnees']['nom_sci_espece_defaut'] = ''; |
$widget['donnees']['infos_espece'] = '{}'; |
|
if ($this->especeEstImposee()) { |
$nom = $this->executerChargementInfosTaxon($_GET['num_nom']); |
$widget['donnees']['espece_imposee'] = true; |
$widget['donnees']['nn_espece_defaut'] = $_GET['num_nom']; |
$widget['donnees']['nom_sci_espece_defaut'] = $nom['nom_sci']; |
$widget['donnees']['infos_espece'] = $this->array2js($nom, true); |
} |
} |
if ($this->projet != 'defaut' || $this->projetASquelette()) { |
$widget['donnees']['taxons'] = $this->recupererListeTaxon(); |
$widget['donnees']['milieux'] = $this->parserMilieux(); |
} |
return $widget; |
} |
|
private function projetASquelette() { |
// fonction très simple qui ne teste que si le dossier du projet courant |
// existe, mais elle suffit pour le moment. |
return file_exists(dirname(__FILE__).self::DS.'squelettes'.self::DS.$this->projet); |
} |
|
public function executerTaxons() { |
$widget['squelette'] = $this->projet.'_taxons'; |
$widget['squelette_ext'] = '.tpl.js'; |
$widget['donnees'] = array(); |
$taxons = $this->recupererListeTaxon(); |
$taxons_tries = array(); |
foreach ($taxons as $taxon) { |
$taxons_tries[$taxon['num_nom_sel']] = $taxon; |
} |
$widget['donnees']['taxons'] = json_encode($taxons_tries); |
return $widget; |
} |
|
private function recupererListeTaxon() { |
$taxons = null; |
$fichier_tsv = dirname(__FILE__).self::DS.'configurations'.self::DS.$this->projet.'_taxons.tsv'; |
if (file_exists($fichier_tsv) && is_readable($fichier_tsv)) { |
$taxons = $this->decomposerFichierTsv($fichier_tsv); |
$taxons = self::trierTableauMd($taxons, array('nom_fr' => SORT_ASC)); |
} else { |
$this->debug[] = "Impossible d'ouvrir le fichier '$fichier_tsv'."; |
} |
return $taxons; |
} |
|
private function decomposerFichierTsv($fichier, $delimiter = "\t"){ |
$header = null; |
$data = array(); |
if (($handle = fopen($fichier, 'r')) !== FALSE) { |
while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE) { |
if (!$header) { |
$header = $row; |
} else { |
$data[] = array_combine($header, $row); |
} |
} |
fclose($handle); |
} |
return $data; |
} |
|
private function parserMilieux() { |
$infosMilieux = array(); |
if (isset($this->configProjet['milieux'])) { |
$milieux = explode('|', $this->configProjet['milieux']); |
foreach ($milieux as $milieu) { |
$details = explode(';', $milieu); |
if (isset($details[1])) { |
$infosMilieux[$details[0]] = $details[1]; |
} else { |
$infosMilieux[$details[0]] = ''; |
} |
} |
ksort($infosMilieux); |
} |
return $infosMilieux; |
} |
|
private function especeEstImposee() { |
return (isset($_GET['num_nom']) && $_GET['num_nom'] != ''); |
} |
|
private function executerChargementInfosTaxon($num_nom) { |
$url_service_infos = sprintf($this->config['chemins']['infosTaxonUrl'], $this->NS_PROJET, $num_nom); |
$infos = json_decode(file_get_contents($url_service_infos)); |
$resultat = array(); |
if (isset($infos) && !empty($infos)) { |
$infos = (array)$infos; |
$resultat = (isset($infos['nom_sci']) && $infos['nom_sci'] != '') ? $infos : array(); |
} |
return $resultat; |
} |
|
public function executerUploadImage() { |
$retour = array( |
'squelette' => $this->projet.'_image', |
'squelette_ext' => '.tpl.xml', |
'mime' => 'text/xml', |
'donnees' => array( |
'urlMiniature' => '', |
'imageNom' => '', |
'message' => '', |
'debogage' => '')); |
$message = ''; |
$debogage = ''; |
if ($_FILES['fichier']['error'] == UPLOAD_ERR_OK) { |
if (is_uploaded_file($_FILES['fichier']['tmp_name'])) { |
if ($this->verifierFormatJpeg($_FILES['fichier']['tmp_name'])) { |
$dossierStockage = $this->config['chemins']['imagesTempDossier']; |
|
$nomFichierOriginal = preg_replace('/[.](jpeg|jpg)$/i', '.jpg', strtolower($_FILES['fichier']['name'])); |
$originalChemin = $dossierStockage.$nomFichierOriginal; |
$deplacementOk = move_uploaded_file($_FILES['fichier']['tmp_name'], $originalChemin); |
|
if ($deplacementOk === true) { |
$miniatureFichier = str_replace('.jpg', '_min.jpg', $nomFichierOriginal); |
$miniatureChemin = $dossierStockage.$miniatureFichier; |
|
// Parametres |
$largeurIdeale = 100; |
$hauteurIdeale = 100; |
$qualite = 85; |
|
// Calcul de la hauteur et de la largeur optimale de la miniature |
$taillesImgOriginale = getimagesize($originalChemin); |
$largeurOrigine = $taillesImgOriginale[0]; |
$hauteurOrigine = $taillesImgOriginale[1]; |
|
$largeurMin = $largeurIdeale; |
$hauteurMin = (int) ($hauteurOrigine * ($largeurIdeale / $largeurOrigine)); |
if ($hauteurMin > $hauteurIdeale) { |
$hauteurMin = $hauteurIdeale; |
$largeurMin = (int)($largeurOrigine * ($hauteurMin / $hauteurOrigine)); |
} |
|
// Création de la miniature |
$imageOriginale = imagecreatefromjpeg($originalChemin); |
$imageMiniature = imagecreatetruecolor($largeurMin, $hauteurMin); |
$couleurFond = imagecolorallocate($imageMiniature, 255, 255, 255); |
imagefill($imageMiniature, 0, 0, $couleurFond); |
imagecopyresized($imageMiniature, $imageOriginale, 0, 0, 0, 0, $largeurMin, $hauteurMin, $largeurOrigine, $hauteurOrigine); |
imagejpeg($imageMiniature, $miniatureChemin, $qualite); |
imagedestroy($imageMiniature); |
imagedestroy($imageOriginale); |
|
// Retour des infos |
$retour['donnees']['urlMiniature'] = sprintf($this->config['chemins']['imagesTempUrlTpl'], $miniatureFichier); |
$retour['donnees']['imageNom'] = $nomFichierOriginal; |
} else { |
$message = "L'image n'a pu être déplacé sur le serveur."; |
} |
} else { |
$message = "L'image n'est pas au format JPEG."; |
} |
} else { |
$message = "L'image n'a pu être téléversée."; |
$debogage = $message.print_r($_FILES, true); |
} |
} else { |
if ($_FILES['fichier']['error'] == UPLOAD_ERR_FORM_SIZE) { |
$message = "L'image téléversée excède la taille maximum autorisée.". |
"Veuillez modifier votre image avant de la téléverser à nouveau."; |
} else { |
$message = "Une erreur de transfert a eu lieu (téléversement interrompu)."; |
} |
$debogage = "Code erreur : {$_FILES['fichier']['error']}. ". |
"Voir : http://php.net/manual/fr/features.file-upload.errors.php"; |
} |
// Retour des infos |
$retour['donnees']['message'] = $message; |
$retour['donnees']['debogage'] = $debogage; |
return $retour; |
} |
|
// Il ne faut pas utiliser l'index type du tableau files pour tester |
// si une image est en jpeg car le type renvoyé par les navigateurs |
// peut varier (ex. sous ie qui renvoie image/pjpeg |
private function verifierFormatJpeg($chemin) { |
// get imagesize renvoie un résultat consistant par contre |
$infos = getimagesize($chemin, $infos); |
return (isset($infos['mime']) && $infos['mime'] == 'image/jpeg'); |
} |
|
private function array2js($array,$show_keys) { |
$tableauJs = '{}'; |
if (!empty($array)) { |
$total = count($array) - 1; |
$i = 0; |
$dimensions = array(); |
foreach ($array as $key => $value) { |
if (is_array($value)) { |
$dimensions[$i] = array2js($value,$show_keys); |
if ($show_keys) { |
$dimensions[$i] = '"'.$key.'":'.$dimensions[$i]; |
} |
} else { |
$dimensions[$i] = '"'.addslashes($value).'"'; |
if ($show_keys) { |
$dimensions[$i] = '"'.$key.'":'.$dimensions[$i]; |
} |
} |
if ($i == 0) { |
$dimensions[$i] = '{'.$dimensions[$i]; |
} |
if ($i == $total) { |
$dimensions[$i].= '}'; |
} |
$i++; |
} |
$tableauJs = implode(',', $dimensions); |
} |
return $tableauJs; |
} |
} |
?> |