Rev 11 | Rev 97 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
<?php/*vim: set expandtab tabstop=4 shiftwidth=4: */// +------------------------------------------------------------------------------------------------------+// | PHP version 4.1 |// +------------------------------------------------------------------------------------------------------+// | Copyright (C) 2004 Tela Botanica (accueil@tela-botanica.org) |// +------------------------------------------------------------------------------------------------------+// | This library is free software; you can redistribute it and/or |// | modify it under the terms of the GNU General Public |// | License as published by the Free Software Foundation; either |// | version 2.1 of the License, or (at your option) any later version. |// | |// | This library is distributed in the hope that it will be useful, |// | but WITHOUT ANY WARRANTY; without even the implied warranty of |// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |// | General Public License for more details. |// | |// | You should have received a copy of the GNU General Public |// | License along with this library; if not, write to the Free Software |// | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |// +------------------------------------------------------------------------------------------------------+// CVS : $Id: document.class.php,v 1.4 2005-09-28 16:29:39 ddelon Exp $/*** Application projet** La classe document**@package projet//Auteur original :*@author Alexandre Granier <alexandre@tela-botanica.org>//Autres auteurs :*@author Aucun*@copyright Tela-Botanica 2000-2004*@version $Revision: 1.4 $// +------------------------------------------------------------------------------------------------------+*/// +------------------------------------------------------------------------------------------------------+// | ENTETE du PROGRAMME |// +------------------------------------------------------------------------------------------------------+include_once PROJET_CHEMIN_CLASSES.'fichier.class.php' ;// +------------------------------------------------------------------------------------------------------+// | ENTETE du PROGRAMME |// +------------------------------------------------------------------------------------------------------+/*** class document* Représente un document associé à un projet. C'est à dire un fichier* téléchargeable disposant en plus d'une visibilité, d'un nom long, d'une* description et d'une url.*/class document extends fichier{/*** Attributes: ***//*** Description qui apparaitra à l'écran.* @access private*/var $_description;/*** Date de dernière mise à jour des attributs du fichier.* @access private*/var $_date_mise_a_jour;/*** Soit public, soit privé.* @access private*/var $_visibilite;/*** Le nom du fichier tel qu'il apparaitra à l'écran.* @access private*/var $_nom_long;/*** Un objet PEAR:DB* @access private*/var $_db;/*** L'identifiant du document dans la table projet_document.* @access private*/var $_id;/**** @access private*/var $_chemin_icone;/*** L'identifiant du père, peut être à NULL* @access private*/var $_id_pere;/*** L'identifiant du propriétaire. Provient d'un annuaire. Peut être à NULL.* @access private*/var $_id_proprietaire;/*** L'identifiant du projet auquel appartient le document. Peut être à NULL.* @access private*/var $_id_projet;/*** Le chemin du fichier, depuis le répertoire du projet**/var $_pd_lien ;/***** @param int id_document L'identifiant du document dans la base.* @param int objetDB un objet PEAR:DB* @return void* @access public*/function document( $id_document = "", &$objetDB, $chemin = '', $chemin_icones = ''){$this->_db = $objetDB ;$this->_chemin_icone = $chemin_icones ;if ($id_document != "") {$requete = "select * from projet_documents where pd_id=".$id_document ;$resultat = $this->_db->query ($requete) ;if (DB::isError($resultat)) {die ("Echec de la requete<br />".$resultat->getMessage()."<br />".$resultat->getDebugInfo()) ;}$ligne = $resultat->fetchRow (DB_FETCHMODE_OBJECT) ;if ($resultat->numRows()>0) {fichier::fichier($chemin.$ligne->pd_lien, $this->_db) ;$this->_id = $ligne->pd_id ;if (is_object ($this->_type_mime)) $this->_type_mime->setCheminIcone ($chemin_icones) ;$this->_id_proprietaire = $ligne->pd_ce_utilisateur ;$this->_nom_long = $ligne->pd_nom ;$this->_visibilite = $ligne->pd_visibilite ;$this->_date_mise_a_jour = $ligne->pd_date_de_mise_a_jour ;$this->_description = $ligne->pd_description ;$this->_pd_lien = $ligne->pd_lien;if ($this->_isRacine($ligne->pd_pere)) {$this->_id_pere = 0 ;} else {$this->_id_pere = $ligne->pd_pere ;}}}} // end of member function document/***** @param int id_document L'identifiant du document dans la base.* @param int objetDB Un objet PEAR:DB* @return document* @access public*/function __construct( $id_document = "", &$objetDB, $chemin = '', $chemin_icones = '' ){$this->document($id_document, $objetDB, $chemin, $chemin_icones);} // end of member function __construct/*** Renvoie le nom long du fichier.** @return string* @access public*/function getNomLong( ){return $this->_nom_long ;} // end of member function getNomLong/*** Renvoie la visibilité du document, soit "public" soit "prive"** @return visibilite* @access public*/function getVisibilite( ){return $this->_visibilite ;} // end of member function getVisibilite/*** Renvoie la description du document, sous forme de chaine.** @return string* @access public*/function getDescription( ){return $this->_description ;} // end of member function getDescription/*** Renvoie la date de création ou de mise à jour du fichier.** @return date* @access public*/function getDateMiseAJour( ){return $this->_date_mise_a_jour ;} // end of member function getDateMiseAJour/*** Renvoie l'identifiant d'un document.** @return int* @access public*/function getIdDocument( ){return $this->_id ;} // end of member function getIdDocument/*** Renvoie le chemin de l'icone du fichier. Fait un appel à type_fichier_mime.** @return string* @access public*/function getCheminIcone(){if ($this->isRepertoire()) {return $this->_chemin_icone."repertoire.gif" ;} else {return $this->_type_mime->getCheminIcone() ;}} // end of member function getCheminIcone/*** Permet de récupérer le nom du répertoire racine associé à un projet.** @param int id_projet L'identifiant du projet dont on veux récupérer le répertoire racine.* @param DB objetDB Un objet PEAR:DB* @return string* @static* @access public*/function getNomRepertoireProjet( $id_projet , &$objetDB){// Dans la table projet_documents, pour les répertoires racines, pd_pere = null$requete = "select pd_nom from projet_documents where pd_ce_projet=$id_projet and pd_pere is null" ;$resultat = $objetDB->query ($requete) ;if (DB::isError($resultat)) {die ("Echec de la requete<br />".$resultat->getMessage()."<br />".$resultat->getDebugInfo()) ;}$ligne = $resultat->fetchRow (DB_FETCHMODE_OBJECT) ;return $ligne->pd_nom ;} // end of member function getNomRepertoireProjet/*** Renvoie true si le document passé en paramètre est le répertoire racine.** @param int id_document L'identifiant du document dont on veut savoir si c'est la racine.* @return bool* @access public*/function _isRacine( $id_document ){if ($id_document) {$requete = "select pd_pere from projet_documents where pd_id=".$id_document ;$resultat = $this->_db->query ($requete) ;if (DB::isError($resultat)) {die ("Echec de la requete<br />".$resultat->getMessage()."<br />".$resultat->getDebugInfo()) ;}$ligne = $resultat->fetchRow(DB_FETCHMODE_OBJECT) ;if ($resultat->numRows() >0) {if ($ligne->pd_pere == null){return true ;}}}return false ;} // end of member function _isRacine/*** Renvoie le chemin d'un fichier ou d'un répertoire. Contrairement à la classe* mère, cette méthode renvoie une URL projet et non pas un fichier.** @return string* @access public*/function getChemin( ){if ($this->isRepertoire()) {return $this->_id ;} else {return $this->_chemin ;}} // end of member function getChemin/*** Renvoie pour le répertoire courant, les identifiants et les noms de tous les* répertoires père jusqu'à la racine. 0 => ['id'], ['nom'] 1 => [id'], ['nom'] etc.* En commençant par la racine et en descendant. Pour la racine id vaut "" et nom* vaut "".** @param int id_repertoire L'identifiant d'un répertoire.* @return Array* @access public*/function getCheminIdRepertoire( $id_repertoire, &$objetDB ){if ($id_repertoire == "") $id_repertoire = 0;// on commence par rechercher le répertoire père, dans la base de donnée$requete = "select pd_pere, pd_nom, pd_id from projet_documents where pd_id=$id_repertoire" ;$resultat = $objetDB->query ($requete) ;if (DB::isError($resultat)) {die ("Echec de la requete<br />".$resultat->getMessage()."<br />".$resultat->getDebugInfo()) ;}$ligne = $resultat->fetchRow (DB_FETCHMODE_OBJECT) ;$chemin_rep_id_nom = array() ;if ($resultat->numRows()>0) {if ($ligne->pd_pere == 0) {$tab = array ($ligne->pd_id, $ligne->pd_nom) ;return $tab ;}else {$requete_pere = "select pd_id, pd_nom from projet_documents where pd_id=$ligne->pd_pere" ;$resultat_pere = $objetDB->query ($requete_pere) ;if (DB::isError($resultat_pere)) {die ("Echec de la requete<br />".$resultat_pere->getMessage()."<br />".$resultat_pere->getDebugInfo()) ;}$ligne_pere = $resultat_pere->fetchRow (DB_FETCHMODE_OBJECT) ;array_push ($chemin_rep_id_nom, $ligne->pd_id,$ligne->pd_nom) ;$tab = document::getCheminIdRepertoire($ligne_pere->pd_id, $objetDB) ;$chemin_rep_id_nom = array_merge ($chemin_rep_id_nom, $tab) ;}}$tabl_resultat = array() ;for ($i = 0; $i < count ($chemin_rep_id_nom); $i++) {$val1 = array_pop ($chemin_rep_id_nom) ;$val2 = array_pop ($chemin_rep_id_nom) ;array_push ($tabl_resultat, $val2, $val1) ;}return $tabl_resultat ;} // end of member function getCheminIdRepertoire/*** Calcule le chemin vers le fichier ou le répertoire uploadé* renvoie un chaine de la forme dir1/dir2/fichier.ext* En prenant comme racine le répertoire du projet, exclu.* @return string Le chemin*/function calculeCheminUploaded ($radical) {// On recherche le chemin vers le fichier, en fonction du répertoire// ici on renomme le fichier à partir du dernier ID de la table gen_voiraussi$requete_document = "select pd_id from projet_documents order by pd_id desc limit 1,1" ;$resultat_document = $this->_db->query($requete_document) ;$ligne_document = $resultat_document->fetchRow(DB_FETCHMODE_OBJECT) ;$nouveau_nom = $ligne_document->pd_id+ 1 ;$extension = preg_replace("/([^\.]+)\.([a-zA-Z0-9]+$)/", "\\2", $_FILES['fichier']['name']) ;$nouveau_nom = $radical."_".$nouveau_nom.".".$extension ;if ($this->_id_pere != '') {// On appelle la méthode getCheminIdRepertoire qui renvoie un tableau avec la liste// des répertoires jusqu'à la racine, on enlève la racine ($i = 0) et on concatène// toutes les entrées pour obtenir le chemin jusqu'au répertoire courant$chemin_repertoire_entre_racine_et_repertoire_a_cree = '' ;$tableau_navigation = $this->getCheminIdRepertoire($this->_id_pere, $this->_db) ;for ($i = 0; $i < count ($tableau_navigation); $i+=2) $chemin_repertoire_entre_racine_et_repertoire_a_cree.= $tableau_navigation[$i]."/";$chemin = $chemin_repertoire_entre_racine_et_repertoire_a_cree.$nouveau_nom ;} else {// Si l'on est à la racine du projet, le chemin est le nom du fichierreturn $nouveau_nom ;}return $chemin ;}/*** Enregistre une ligne dans la table projet_document* Le tableau de valeur doit contenir les éléments suivants 'document_nom','document_description','document_visibilite','fichier'** @param Array tableau_de_valeur Le tableau de valeur a insérer dans la base avec pour clé les noms des éléments* @access public* @return void*/function enregistrerSQL ($valeur, $chemin) {// On teste si on a affaire à un répertoire ou un fichierif (isset ($_FILES['fichier']['name'])) {// On tente de déterminer le type du fichier à partir de son nom dans $valeur['$fichier']$tableau_nom = explode (".", $_FILES['fichier']['name']) ;// On prend le dernier élément du tableau, si c'est un tableauif (is_array($tableau_nom)) {$extension = array_pop($tableau_nom) ;$type = type_fichier_mime::factory($extension, $this->_db) ;$id_extension = $type->getIdType() ;} else {$id_extension = 12 ;}$pd_lien = $chemin ;} else { // Le cas ou on a affaire à un répertoire$id_extension = 0 ;// Le nom du répertoire est son identifiant avec un slash à la fin$pd_lien = $chemin."/" ;if ($this->_id_pere != '') {// On appelle la méthode getCheminIdRepertoire qui renvoie un tableau avec la liste// des répertoires jusqu'à la racine, on enlève la racine ($i = 0) et on concatène// toutes les entrées pour obtenir le chemin jusqu'au répertoire courant$chemin_repertoire_entre_racine_et_repertoire_a_cree = '' ;$tableau_navigation = $this->getCheminIdRepertoire($this->_id_pere, $this->_db) ;for ($i = 0; $i < count ($tableau_navigation); $i+=2) $chemin_repertoire_entre_racine_et_repertoire_a_cree.= $tableau_navigation[$i]."/";$pd_lien .= $chemin_repertoire_entre_racine_et_repertoire_a_cree ;}$pd_lien .= SQL_obtenirNouveauId($this->_db, 'projet_documents', 'pd_id')."/" ;}$id = SQL_obtenirNouveauId($this->_db, 'projet_documents', 'pd_id') ;$requete = "insert into projet_documents set pd_id=".$id ;$requete .= ", pd_nom=\"".$valeur['document_nom']."\", pd_description=\"".$valeur['document_description']."\"".", pd_visibilite=\"".$valeur['document_visibilite']."\", pd_date_de_mise_a_jour=NOW(),"."pd_ce_projet=\"".$this->_id_projet."\", pd_ce_utilisateur=\"".$this->_id_proprietaire."\"".", pd_pere=\"$this->_id_pere\", pd_ce_type=\"$id_extension\", pd_lien=\"$pd_lien\"" ;$resultat = $this->_db->query ($requete) ;if (DB::isError($resultat)) {die ("Echec de la requete<br />".$resultat->getMessage()."<br />".$resultat->getDebugInfo()) ;}$this->_id = $id ;return $pd_lien;}/*** Met à jour une ligne dans la table projet_document* Le tableau de valeur doit contenir les éléments suivants 'document_nom','document_description','document_visibilite','fichier'** @param Array tableau_de_valeur Le tableau de valeur a insérer dans la base avec pour clé les noms des éléments* @access public* @return void*/function majSQL ($valeur) {$requete = "update projet_documents set pd_nom=\"".$valeur['document_nom']."\", pd_description=\"".$valeur['document_description']."\"".", pd_visibilite=\"".$valeur['document_visibilite']."\", pd_date_de_mise_a_jour=NOW()"." where pd_id=".$this->_id;$resultat = $this->_db->query ($requete) ;if (DB::isError($resultat)) {die ("Echec de la requete<br />".$resultat->getMessage()."<br />".$resultat->getDebugInfo()) ;}return ;}/** Supprime les donnéexs du document dans la table projet_documents*** @return true en cas de succès*/function suppressionSQL () {$requete = 'delete from projet_documents where pd_id='.$this->_id ;$resultat = $this->_db->query ($requete) ;if ($this->_db->affectedRows()) return true ;return false ;}/*** Déplace un document au sein d'un même projet** @param int repertoire_destination L'identifiant du répertoire destination.* @return bool* @access public*/function deplace( $repertoire_destination, $repertoire_projet ){// On récupère les informations du répertoire cibleif ($repertoire_destination != 0) {$repertoire_cible = new document ($repertoire_destination, $this->_db) ;$rep = $repertoire_cible->_pd_lien ;} else {$rep = $repertoire_projet.'/' ;}// On récupère le nom du fichier$decoupe = explode ('/', $this->_pd_lien) ;$nom_fichier = $decoupe[count($decoupe)-1] ;$requete = 'update projet_documents set pd_lien="'.$rep.$nom_fichier.'", pd_pere='.$repertoire_destination.' where pd_id='.$this->_id ;$resultat = $this->_db->query ($requete) ;if (DB::isError($resultat)) {echo ("Echec de la requete<br />".$resultat->getMessage()."<br />".$resultat->getDebugInfo()) ;return false ;}return fichier::deplace ($this->_chemin, PROJET_CHEMIN_FICHIER.$rep.$nom_fichier) ;} // end of member function deplace/*** Pour modifier l'identifiant du projet auquel appartient un document.** @param int id_projet L'identifiant du projet.* @return void* @access public*/function setIdProjet( $id_projet ){$this->_id_projet = $id_projet ;} // end of member function setIdProjet/*** Permet de modifier l'identifiant du propietaire d'un projet.** @param int id_proprietaire L'identifiant du proprietaire d'un document.* @return void* @access public*/function setIdProprietaire( $id_proprietaire ){$this->_id_proprietaire = $id_proprietaire ;} // end of member function setIdProprietaire/*** Permet de modifier l'identifiant du répertoire d'un document.** @param int id_repertoire L'identifiant du repertoire d'un document.* @return void* @access public*/function setIdRepertoire( $id_repertoire ){$this->_id_pere = $id_repertoire;} // end of member function setIdRepertoire/*** initAttributes sets all document attributes to its default value make* sure to call this method within your class constructor*/function initAttributes( ){$this->_visibilite = "public";}/*** Renvoie les derniers documents de l'ensemble des projets.** @param int nombre Le nombre de document à renvoyer* @return Array* @static* @access public*/function getDocumentsRecents( $nombre = 10, &$objetDB, $chemin, $chemin_icones, $id_projet = '' ){// on recherche les documents, hors répertoire$requete = 'select pd_id from projet_documents where pd_ce_type<>0 ';if ($id_projet != '') $requete .= ' and pd_ce_projet='.$id_projet.' ';$requete .= 'order by pd_date_de_mise_a_jour desc limit 0,'.$nombre ;$resultat = $objetDB->query ($requete) ;if (DB::isError ($resultat)) {die ('Echec de la requete : '.$requete.'<br />'.$resultat->getMessage()) ;}$tableau_document = array() ;while ($ligne = $resultat->fetchRow (DB_FETCHMODE_OBJECT)) {array_push ($tableau_document, new document ($ligne->pd_id, $objetDB, $chemin, $chemin_icones)) ;}return $tableau_document ;} // end of member function getDocumentsRecents} // end of document?>