Subversion Repositories eFlore/Applications.eflore-consultation

Rev

Rev 1381 | Rev 1383 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php
// declare(encoding='UTF-8');
/**
 * Classe mère du module Liste.
 *
 * @category    PHP 5.2
 * @package             eflore-consultation
 * @author              Jean-Pascal MILCENT <jpm@tela-botanica.org>
 * @author              Delphine CAUQUIL <delphine@tela-botanica.org>
 * @copyright   2011 Tela-Botanica
 * @license             http://www.gnu.org/licenses/gpl.html Licence GNU-GPL-v3
 * @license             http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL-v2
 * @version             $Id$
 */
class Illustrations extends aControleur {

        private $conteneur = null;
        private $nomCourant = null;
        private $images = null;
        private $appUrls = null;
        private $meta = null;
        private $referentiel = 'bdtfx';
        private $donnees = array();

        public function __construct(Conteneur $conteneur) {
                $this->conteneur = $conteneur;
                $this->nomCourant = $this->conteneur->getNomCourant();
                $this->referentiel = $this->conteneur->getParametre('referentiel');
                $this->images = $this->conteneur->getApiImages();
                $this->appUrls = $this->conteneur->getAppUrls();
                $this->meta = $this->conteneur->getApiMetaDonnees();
        }

        public function obtenirDonnees() {
                $this->donnees['nt'] = $this->nomCourant->getNt();
                $this->donnees['nnr'] = $this->nomCourant->getNnr();
                $this->donnees['referentiel'] = $this->referentiel;
                $this->donnees['nomSciRetenu'] = $this->nomCourant->getNomRetenu()->get('nom_sci_html');
                $infosNomRetenu = $this->nomCourant->getNomRetenu()->getInfos();
                $this->donnees['nomComplet'] = $infosNomRetenu['nom_complet'];
                $this->getPhotos();
                $this->getDessin();
                
                $this->getPhotosCaracteresIdentification();
                $pso = $this->getPhotosSixOrganes();
                
                // Le framework n'avait pas prévu l'inclusion de sous squelettes dans les modules
                // en attendant, on utilise un chemin complet vers le fichier de squelette
                $chemin_sous_squelette = Config::get('chemin_modules').AppControleur::getNomDossierDepuisClasse('Fiche').DS.Config::get('dossier_squelettes').DS;
                $params = array('organes' => $pso, 'referentiel' => $this->referentiel, 
                                                'nnr' => $this->donnees['nnr'], 
                                                'nomComplet' => $this->donnees['nomComplet']);
                $this->donnees['illustrationsOrganes'] = $this->getVue($chemin_sous_squelette.'fiche_illustrations_organes.tpl.html',$params);
                $this->donnees['organes'] = $pso;
                
                // chemins
                $this->donnees['base_url_widget_saisie'] = Config::get('base_url_widget_saisie');
                $this->donnees['base_url_pictoflora'] = Config::get('base_url_pictoflora');
                return $this->donnees;
        }
        
        public function obtenirDonneesExport() {
                $this->donnees['nt'] = $this->nomCourant->getNt();
                $this->donnees['nomSciRetenu'] = $this->nomCourant->getNomRetenu()->get('nom_sci_html');
                $this->donnees['cel'] = $this->getCelExport();
                if ($this->referentiel == 'bdtfx') {
                        $this->donnees['coste'] = $this->getCoste();
                }
                return $this->donnees;
        }

        public function getBloc() {
                $this->donnees = $this->images->getInfosMeilleureImageParPrioriteTags($nnr = $this->nomCourant->getNnr(), array(
                                'fleur', 'feuille', 'fruit', 'ecorce', 'port', 'rameau'));
                if (empty($this->donnees)) {
                        $this->donnees = $this->getPhoto('cel');
                        if (empty($this->donnees)) {
                                $infos_image = $this->getDessin();
                                if (empty($infos_image) == false ) {
                                        $this->donnees['imageUrl'] =  array_shift($infos_image['images']);
                                }
                        }
                } else {
                        // extraction de l'url
                        $this->donnees = array('imageUrl' => $this->donnees['binaire.href']);
                }
                if (empty($this->donnees)) {
                        $this->donnees = $this->getPhoto('photoflora');
                }
                return $this->donnees;
        }
        
        public function obtenirPhotoCelTpl() {
                $images = $this->initialiserPhotos('cel');
                $this->formaterListePhotos('cel', $images);
                $this->formaterMetaPhotos('cel');
                $chemin_sous_squelette = Config::get('chemin_modules').AppControleur::getNomDossierDepuisClasse('Fiche').DS.Config::get('dossier_squelettes').DS; 
                $donnees = array('cel' => array('images' => $this->donnees['cel']['images'], 'meta' => $this->donnees['cel']['meta'] ));
                $html_tpl = $this->getVue($chemin_sous_squelette.'fiche_illustrations_cel.tpl.html', $donnees);
                return $html_tpl;
        }
        
        //TODO: factoriser les deux fonctions ci dessous et ci dessus
        public function obtenirTranchesCelTpl($projet) {
                // On ne prend que la tranche des images (10)
                $images = $this->initialiserTranchePhotos($projet);
                $this->formaterListePhotos($projet, $images);
                $this->formaterMetaPhotos($projet);
                $chemin_sous_squelette = Config::get('chemin_modules').AppControleur::getNomDossierDepuisClasse('Fiche').DS.Config::get('dossier_squelettes').DS;
                $donnees = array('cel' => array('images' => $this->donnees['cel']['images'], 'meta' => $this->donnees[$projet]['meta'] ));
                $html_tpl = $this->getVue($chemin_sous_squelette.'fiche_illustrations_cel.tpl.html', $donnees);
                $this->donnees[$projet]['illustrationsCel'] = $html_tpl;
                // Ensuite on fait juste un lien qui permettra de les charger
                $this->donnees[$projet]['ajax_url'] = $this->formaterUrlAjaxPhotoCel();
        }
        
        public function getPhoto($source) {
                $donnees = array();
                $this->images->setProjet($source);
                $nnr = $this->nomCourant->getNnr();
                $infos_image = $this->images->setApi(Eflore::API_EFLORE)->getInfosPremiereImageParIdsNoms(array($nnr));
                if ($infos_image != array()) {
                        $image = array_shift($infos_image);
                        $donnees['imageUrl'] =  $image['binaire.href'];
                }
                return $donnees;
        }

        public function getCoste() {
                $coste = array();
                $tax = $this->nomCourant->getNomRetenu()->get('num_taxonomique');
                $this->images->setProjet('coste');
                $this->images->setNnTaxon($tax);
                $costeImg = $this->images->setApi(Eflore::API_EFLORE)->getInfosImagesTaxons();
                foreach ($costeImg as $infos) {
                        $coste['images'][] = $infos['binaire.href'];
                }
                $this->meta->setProjet('coste');
                $meta = $this->meta->getMetaDonnees();
                $coste['meta'] = $meta[0];
                $coste['meta']['url'] = $this->appUrls->obtenirUrlMetaDonnees('coste');
                return $coste;
        }

        public function getDessin() {
                $projet = Config::get($this->referentiel.'.baseDessins');
                if ($projet != "") {
                        $dessin = array();
                        $tax = $this->nomCourant->getNomRetenu()->get('num_taxonomique');
                        $this->images->setProjet($projet);
                        $this->images->setNnTaxon($tax);
                        $costeImg = $this->images->setApi(Eflore::API_EFLORE)->getInfosImagesTaxons();
                                foreach ($costeImg as $infos) {
                                        $dessin['images'][] = $infos['binaire.href'];
                                }
                        $this->meta->setProjet($projet);
                        $meta = $this->meta->getMetaDonnees();
                        $dessin['meta'] = $meta[0];
                        $dessin['meta']['url'] = $this->appUrls->obtenirUrlMetaDonnees($projet);
                        $this->donnees[$projet] = $dessin;
                }
        }
        
        public function getPhotos() {
                $projets[] = Config::get($this->referentiel.'.baseImages');
                $projets[] = Config::get($this->referentiel.'.baseImagesSupp');
                foreach ($projets as $projet) {
                        if ($projet != "") {
                                if ($projet == "cel") {
                                        // Chargement des 10 premieres images + un lien pour en afficher plus
                                        $this->obtenirTranchesCelTpl($projet);
                                } else {
                                        $this->images->setApi(Eflore::API_EFLORE);
                                        $images = $this->initialiserPhotos($projet);
                                        $this->formaterListePhotos($projet, $images);
                                        $this->formaterMetaPhotos($projet);
                                }
                        }
                }
        }
        
        private function formaterUrlAjaxPhotoCel() {
                $infos = array('referentiel' => $this->referentiel,
                                                'module' => 'fiche',
                                                'action' => 'templateAjax',
                                                'sous_action' => 'photoCelTpl',
                                                'num_nom' => $this->nomCourant->getNnr(),
                                                'onglet' => 'illustrations');
                return Config::get('base_url_application_onglets').'?'.http_build_query($infos);
        }
        
        public function getPhotosSixOrganes() {
                $organes = $this->getOrganes();
                $nnr = $this->nomCourant->getNnr();
                $infosOrganes = $this->images->getInfosMeilleuresImagesParTag($nnr, array_keys($organes), Eflore::PROTOCOLE_CAPITALISATION, 6, 'CRX2S');
                
                $vide = true;
                foreach ($infosOrganes as $tag => $organe) {
                        foreach ($organe as $id_image => &$image) {
                                $image['urlMauvaiseIdentification'] =  $this->appUrls->obtenirUrlMauvaiseIdentification($image['observation']['id_observation']);
                        }
                        $organes[$tag] = $organe;
                        $nbImages = count($organe);
                        if ($nbImages != 0) {
                                $vide = false;
                                for ($i = 6; $i > $nbImages; $i--) {
                                        $organes[$tag][] = null;
                                }
                        }
                }
                if ($vide) {
                        $organes = array();
                }
                
                return $organes;
        }
        
        public function getPhotosOrganes() {
                $organes = $this->getOrganes();
                $nnr = $this->nomCourant->getNnr();
                $infosOrganes = $this->images->getInfosMeilleuresImagesParTag($nnr, array_keys($organes), Eflore::PROTOCOLE_CAPITALISATION, 1);
                $vide = true;
                foreach ($infosOrganes as $tag => $organe) {
                        $nbImages = count($organe);
                        // une image par organe seulement
                        $organes[$tag] = array_pop($organe);
                        if ($nbImages != 0) {
                                $vide = false;
                        }
                }
                if ($vide) {
                        $organes = array();
                }
                $this->donnees['organes'] = $organes;
        }
        
        public function getPhotosCaracteresIdentification() {
                $caracteres = array();
                $nnr = $this->nomCourant->getNnr();
                // pas de tag: on cherche les mieux votées pour ce protocole sans tenir compte des tags
                $infosCaracteres = $this->images->getInfosMeilleuresImagesParTag($nnr, '', Eflore::PROTOCOLE_CARACTERES, 3);
                if (! empty($infosCaracteres) && ! empty($infosCaracteres[''])) { // le tag est vide, ici
                        $caracteres = $infosCaracteres[''];
                }
                foreach($caracteres as $id_image => &$caractere) {
                        $caractere['urlDetailImg'] = $this->appUrls->obtenirUrlPopUpIllustrations($id_image);
                        $caractere['urlImgGrandFormat'] = $this->obtenirUrlImageGrandFormatCel($id_image);
                        $caractere['urlContact'] = $this->appUrls->obtenirUrlPopUpContact($caractere['auteur.id'], $id_image);
                        $caractere['urlProfil'] = $this->appUrls->obtenirUrlProfilAnnuaire($caractere['auteur.id']);
                        $caractere['urlMauvaiseIdentification'] =  $this->appUrls->obtenirUrlMauvaiseIdentification($caractere['observation']['id_observation']);
                }
                $this->donnees['caracteres'] = $caracteres;
        }
        
        private function getOrganes() {
                return array(
                                'fleur' => array(),
                                'feuille' => array(),
                                'fruit' => array(),
                                'ecorce' => array(),
                                'port' => array(),
                                'rameau' => array());
        }

        // XXX: webservice:
        // /service:eflore:0.1/cel/images?masque.nn=XXX&referentiel=bdtfx&retour.format=CS&navigation.limite=801
        private function initialiserPhotos($projet) {
                $this->images->setProjet($projet);
                $nnr = $this->nomCourant->getNnr();
                $ret = $this->images->getInfosImagesParIdsNoms(array($nnr));
                return $ret;
        }
        
        private function initialiserTranchePhotos($projet) {
                $this->images->setProjet($projet);
                $nnr = $this->nomCourant->getNnr();
                $ret = $this->images->getInfosTrancheImagesParIdsNoms(array($nnr));
                return $ret;
        }
        
        private function formaterListePhotos($projet, $images) {
                foreach ($images as $idImg => $img) {
                        $infosImg = array();
                        $infosImg['src'] = $img['binaire.href'];
                        $infosImg['nomSci'] = $img['determination.nom_sci'];
                        $infosImg['station'] = $img['station.libelle'];
                        $infosImg['date'] = $this->formaterDateImg($img['date']);
                        $infosImg['auteur'] = $img['auteur.libelle'];
                        $infosImg['auteur.id'] = $img['auteur.id'];
                        if ($projet == "cel") {
                                $infosImg['urlDetailImg'] = $this->appUrls->obtenirUrlPopUpIllustrations($idImg);
                                $infosImg['urlImgGrandFormat'] = $this->obtenirUrlImageGrandFormatCel($idImg);
                                $infosImg['urlContact'] = $this->appUrls->obtenirUrlPopUpContact($img['auteur.id'], $idImg);
                                $infosImg['urlProfil'] = $this->appUrls->obtenirUrlProfilAnnuaire($img['auteur.id']);
                                $infosImg['urlMauvaiseIdentification'] =  $this->appUrls->obtenirUrlMauvaiseIdentification($img['observation.id']);
                        }
                        $this->donnees[$projet]['images'][$idImg] = $infosImg;
                }
        }
        
        public static function obtenirUrlImageGrandFormatCel($id, $format = "O") {
                $tpl = Config::get('baseUrlImageCelTpl');
                return sprintf($tpl, str_pad($id, 9, '0', STR_PAD_LEFT).$format.'.jpg');
        }
        
        // XXX: public static pour être injecté dans le squelette d'entete
        public static function obtenirTemplateUrlImageGrandFormatOrganes($format = "O") {
                return $tpl = sprintf(Config::get('baseUrlImageCelTpl').$format.'.jpg', '{id_image}');
        }
        
        private function formaterMetaPhotos($projet) {
                $this->meta->setProjet($projet);
                $meta = $this->meta->getMetaDonnees();
                $titreMeta = $meta[0];
                $this->donnees[$projet]['meta'] = $titreMeta;
                $this->donnees[$projet]['meta']['url'] = $this->appUrls->obtenirUrlMetaDonnees($projet);
        }
        
        
        
        public function getCelExport() {
                $cel = array();
                $this->images->setProjet('cel');
                $nnr = $this->nomCourant->getNnr();
                $img = $this->images->getUrlPremiereImageParIdsNoms(array($nnr));
                if($img) {
                        $img = array_values($img);
                        $cel['images']['src'] = $img[0]['binaire.href'];
                        $cel['images']['nomSci'] = $img[0]['determination.nom_sci'];
                        $cel['images']['station'] = $img[0]['station.libelle'];
                        $cel['images']['date'] = $this->formaterDateImg($img[0]['date']);
                        $cel['images']['auteur'] = $img[0]['auteur.libelle'];
                        $this->meta->setProjet('cel');
                        $meta = $this->meta->getMetaDonnees();
                        $cel['meta'] = $meta[0];
                        $cel['meta']['url'] = $this->appUrls->obtenirUrlMetaDonnees('cel');
                }
                return $cel;
        }

        private function formaterDateImg($date) {
                $dateFmt = $date;
                if ($date == '' || $date == null || strpos($date, '0000-00-00') !== false || $date == '1970-01-01 01:01:01') {
                        return 'inconnue';
                }
                $time = strtotime($date);
                if(!$time) {
                        /* TODO: php -r "echo strtotime('1891-00-00 00:00:00');"
                           TODO: fixer le template pour affiche "en l'année X", plutôt que "le X"
                           si FALSE pour avant 1901: problème php
                           On retourne alors simplement l'année.
                           si correction, ne pas oublier modules/popup_illustrations/PopupIllustrations.php */
                        $dateFmt = explode('-', $date);
                        return $dateFmt[0];
                }
                return strftime('%e %B %Y', strtotime($date));
        }

        private function formaterNomSci($nom) {
                $nomFmt = $nom;
                if (is_null($nom) || $nom == '') {
                        $nomFmt = 'inconnu';
                }
                return $nomFmt;
        }
}
?>