Subversion Repositories Applications.papyrus

Rev

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

Rev Author Line No. Line
448 ddelon 1
<?php
2
/*vim: set expandtab tabstop=4 shiftwidth=4: */
3
// +------------------------------------------------------------------------------------------------------+
4
// | PHP version 4.1                                                                                      |
5
// +------------------------------------------------------------------------------------------------------+
6
// | Copyright (C) 2004 Tela Botanica (accueil@tela-botanica.org)                                         |
7
// +------------------------------------------------------------------------------------------------------+
8
// | This library is free software; you can redistribute it and/or                                        |
9
// | modify it under the terms of the GNU General Public                                                  |
10
// | License as published by the Free Software Foundation; either                                         |
11
// | version 2.1 of the License, or (at your option) any later version.                                   |
12
// |                                                                                                      |
13
// | This library is distributed in the hope that it will be useful,                                      |
14
// | but WITHOUT ANY WARRANTY; without even the implied warranty of                                       |
15
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                                    |
16
// | General Public License for more details.                                                             |
17
// |                                                                                                      |
18
// | You should have received a copy of the GNU General Public                                            |
19
// | License along with this library; if not, write to the Free Software                                  |
20
// | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                            |
21
// +------------------------------------------------------------------------------------------------------+
474 alexandre_ 22
// CVS : $Id: document.class.php,v 1.3 2005-09-27 16:38:11 alexandre_tb Exp $
448 ddelon 23
/**
24
* Application projet
25
*
26
* La classe document
27
*
28
*@package projet
29
//Auteur original :
30
*@author        Alexandre Granier <alexandre@tela-botanica.org>
31
//Autres auteurs :
32
*@author        Aucun
33
*@copyright     Tela-Botanica 2000-2004
474 alexandre_ 34
*@version       $Revision: 1.3 $
448 ddelon 35
// +------------------------------------------------------------------------------------------------------+
36
*/
37
 
38
 
39
// +------------------------------------------------------------------------------------------------------+
40
// |                                            ENTETE du PROGRAMME                                       |
41
// +------------------------------------------------------------------------------------------------------+
42
 
43
include_once PROJET_CHEMIN_CLASSES.'fichier.class.php' ;
44
 
45
// +------------------------------------------------------------------------------------------------------+
46
// |                                            ENTETE du PROGRAMME                                       |
47
// +------------------------------------------------------------------------------------------------------+
48
 
49
/**
50
 * class document
51
 * Représente un document associé à un projet. C'est à dire un fichier
52
 * téléchargeable disposant en plus d'une visibilité, d'un nom long, d'une
53
 * description et d'une url.
54
 */
55
class document extends fichier
56
{
57
    /*** Attributes: ***/
58
 
59
    /**
60
     * Description qui apparaitra à l'écran.
61
     * @access private
62
     */
63
    var $_description;
64
    /**
65
     * Date de dernière mise à jour des attributs du fichier.
66
     * @access private
67
     */
68
    var $_date_mise_a_jour;
69
    /**
70
     * Soit public, soit privé.
71
     * @access private
72
     */
73
    var $_visibilite;
74
    /**
75
     * Le nom du fichier tel qu'il apparaitra à l'écran.
76
     * @access private
77
     */
78
    var $_nom_long;
79
    /**
80
     * Un objet PEAR:DB
81
     * @access private
82
     */
83
    var $_db;
84
 
85
    /**
86
     * L'identifiant du document dans la table projet_document.
87
     * @access private
88
     */
89
    var $_id;
90
 
91
    /**
92
     *
93
     * @access private
94
     */
95
    var $_chemin_icone;
96
 
97
    /**
98
     * L'identifiant du père, peut être à NULL
99
     * @access private
100
     */
101
    var $_id_pere;
102
 
103
    /**
104
     * L'identifiant du propriétaire. Provient d'un annuaire. Peut être à NULL.
105
     * @access private
106
     */
107
    var $_id_proprietaire;
108
    /**
109
     * L'identifiant du projet auquel appartient le document. Peut être à NULL.
110
     * @access private
111
     */
112
    var $_id_projet;
113
 
114
    /**
115
    *   Le chemin du fichier, depuis le répertoire du projet
116
    *
117
    */
118
    var $_pd_lien ;
119
    /**
120
     *
121
     *
122
     * @param int id_document L'identifiant du document dans la base.
123
     * @param int objetDB un objet PEAR:DB
124
     * @return void
125
     * @access public
126
     */
127
    function document( $id_document = "",  &$objetDB, $chemin = '', $chemin_icones = '')
128
    {
129
        $this->_db = $objetDB ;
130
        $this->_chemin_icone = $chemin_icones ;
131
        if ($id_document != "") {
132
            $requete = "select * from projet_documents where pd_id=".$id_document ;
133
            $resultat = $this->_db->query ($requete) ;
134
            if (DB::isError($resultat)) {
135
                die ("Echec de la requete<br />".$resultat->getMessage()."<br />".$resultat->getDebugInfo()) ;
136
            }
137
            $ligne = $resultat->fetchRow (DB_FETCHMODE_OBJECT) ;
474 alexandre_ 138
 
139
            fichier::fichier($chemin.$ligne->pd_lien, $this->_db) ;
140
            $this->_id = $ligne->pd_id ;
141
            if (is_object ($this->_type_mime)) $this->_type_mime->setCheminIcone ($chemin_icones) ;
142
            $this->_id_proprietaire = $ligne->pd_ce_utilisateur ;
143
            $this->_nom_long = $ligne->pd_nom ;
144
            $this->_visibilite = $ligne->pd_visibilite ;
145
            $this->_date_mise_a_jour = $ligne->pd_date_de_mise_a_jour ;
146
            $this->_description = $ligne->pd_description ;
147
            $this->_pd_lien = $ligne->pd_lien;
148
            if ($this->_isRacine($ligne->pd_pere)) {
149
                $this->_id_pere = 0 ;
150
            } else {
151
                $this->_id_pere = $ligne->pd_pere ;
152
            }
448 ddelon 153
        }
154
 
155
    } // end of member function document
156
 
157
    /**
158
     *
159
     *
160
     * @param int id_document L'identifiant du document dans la base.
161
     * @param int objetDB Un objet PEAR:DB
162
     * @return document
163
     * @access public
164
     */
474 alexandre_ 165
    function __construct( $id_document,  &$objetDB )
448 ddelon 166
    {
167
 
168
    } // end of member function __construct
169
 
170
 
171
    /**
172
     * Renvoie le nom long du fichier.
173
     *
174
     * @return string
175
     * @access public
176
     */
177
    function getNomLong( )
178
    {
179
        return $this->_nom_long ;
180
    } // end of member function getNomLong
181
 
182
    /**
183
     * Renvoie la visibilité du document, soit "public" soit "prive"
184
     *
185
     * @return visibilite
186
     * @access public
187
     */
188
    function getVisibilite( )
189
    {
190
        return $this->_visibilite ;
191
    } // end of member function getVisibilite
192
 
193
    /**
194
     * Renvoie la description du document, sous forme de chaine.
195
     *
196
     * @return string
197
     * @access public
198
     */
199
    function getDescription( )
200
    {
201
        return $this->_description ;
202
    } // end of member function getDescription
203
 
204
    /**
205
     * Renvoie la date de création ou de mise à jour du fichier.
206
     *
207
     * @return date
208
     * @access public
209
     */
210
    function getDateMiseAJour( )
211
    {
212
        return $this->_date_mise_a_jour ;
213
    } // end of member function getDateMiseAJour
214
 
215
    /**
216
     * Renvoie l'identifiant d'un document.
217
     *
218
     * @return int
219
     * @access public
220
     */
221
    function getIdDocument( )
222
    {
223
        return $this->_id ;
224
    } // end of member function getIdDocument
225
 
226
    /**
227
     * Renvoie le chemin de l'icone du fichier. Fait un appel à type_fichier_mime.
228
     *
229
     * @return string
230
     * @access public
231
     */
232
    function getCheminIcone()
233
    {
234
        if ($this->isRepertoire()) {
235
            return $this->_chemin_icone."repertoire.gif" ;
236
        } else {
237
            return $this->_type_mime->getCheminIcone() ;
238
        }
239
    } // end of member function getCheminIcone
240
 
241
    /**
242
     * Permet de récupérer le nom du répertoire racine associé à un projet.
243
     *
244
     * @param int id_projet L'identifiant du projet dont on veux récupérer le répertoire racine.
245
     * @param DB objetDB Un objet PEAR:DB
246
     * @return string
247
     * @static
248
     * @access public
249
     */
250
    function getNomRepertoireProjet( $id_projet , &$objetDB)
251
    {
252
        // Dans la table projet_documents, pour les répertoires racines, pd_pere = null
253
        $requete = "select pd_nom from projet_documents where pd_ce_projet=$id_projet and pd_pere is null" ;
254
        $resultat = $objetDB->query ($requete) ;
255
        if (DB::isError($resultat)) {
256
            die ("Echec de la requete<br />".$resultat->getMessage()."<br />".$resultat->getDebugInfo()) ;
257
        }
258
        $ligne = $resultat->fetchRow (DB_FETCHMODE_OBJECT) ;
259
        return $ligne->pd_nom ;
260
    } // end of member function getNomRepertoireProjet
261
 
262
 
263
    /**
264
     * Renvoie true si le document passé en paramètre est le répertoire racine.
265
     *
266
     * @param int id_document L'identifiant du document dont on veut savoir si c'est la racine.
267
     * @return bool
268
     * @access public
269
     */
270
    function _isRacine( $id_document )
271
    {
474 alexandre_ 272
        $requete = "select pd_pere from projet_documents where pd_id=$id_document" ;
273
        $resultat = $this->_db->query ($requete) ;
274
        if (DB::isError($resultat)) {
275
            die ("Echec de la requete<br />".$resultat->getMessage()."<br />".$resultat->getDebugInfo()) ;
276
        }
277
        $ligne = $resultat->fetchRow(DB_FETCHMODE_OBJECT) ;
278
        if ($ligne->pd_pere == null){
279
            return true ;
280
        }
448 ddelon 281
        return false ;
282
    } // end of member function _isRacine
283
 
284
    /**
285
     * Renvoie le chemin d'un fichier ou d'un répertoire. Contrairement à la classe
286
     * mère, cette méthode renvoie une URL projet et non pas un fichier.
287
     *
288
     * @return string
289
     * @access public
290
     */
291
    function getChemin( )
292
    {
293
        if ($this->isRepertoire()) {
294
            return $this->_id ;
295
        } else {
296
            return $this->_chemin ;
297
        }
298
    } // end of member function getChemin
299
 
300
 
301
    /**
302
     * Renvoie pour le répertoire courant, les identifiants et les noms de tous les
303
     * répertoires père jusqu'à la racine. 0 => ['id'], ['nom'] 1 => [id'], ['nom'] etc.
304
     * En commençant par la racine et en descendant.  Pour la racine id vaut "" et nom
305
     * vaut "".
306
     *
307
     * @param int id_repertoire L'identifiant d'un répertoire.
308
     * @return Array
309
     * @access public
310
     */
311
    function getCheminIdRepertoire( $id_repertoire, &$objetDB )
312
    {
313
        if ($id_repertoire == "") $id_repertoire = 0;
314
        // on commence par rechercher le répertoire père, dans la base de donnée
315
        $requete = "select pd_pere, pd_nom, pd_id from projet_documents where pd_id=$id_repertoire" ;
316
        $resultat = $objetDB->query ($requete) ;
317
        if (DB::isError($resultat)) {
318
            die ("Echec de la requete<br />".$resultat->getMessage()."<br />".$resultat->getDebugInfo()) ;
319
        }
320
        $ligne = $resultat->fetchRow (DB_FETCHMODE_OBJECT) ;
321
        $chemin_rep_id_nom = array() ;
474 alexandre_ 322
        if ($ligne->pd_pere == 0) {
323
            $tab = array ($ligne->pd_id, $ligne->pd_nom) ;
448 ddelon 324
            return $tab ;
474 alexandre_ 325
        } else {
326
            $requete_pere = "select pd_id, pd_nom from projet_documents where pd_id=$ligne->pd_pere" ;
327
            $resultat_pere = $objetDB->query ($requete_pere) ;
328
            if (DB::isError($resultat_pere)) {
329
                die ("Echec de la requete<br />".$resultat_pere->getMessage()."<br />".$resultat_pere->getDebugInfo()) ;
330
            }
331
            $ligne_pere = $resultat_pere->fetchRow (DB_FETCHMODE_OBJECT) ;
332
            array_push ($chemin_rep_id_nom, $ligne->pd_id,$ligne->pd_nom) ;
333
            $tab = document::getCheminIdRepertoire($ligne_pere->pd_id, $objetDB) ;
334
            $chemin_rep_id_nom = array_merge ($chemin_rep_id_nom, $tab) ;
448 ddelon 335
        }
336
        $tabl_resultat = array() ;
337
        for ($i = 0; $i < count ($chemin_rep_id_nom); $i++) {
338
            $val1 = array_pop ($chemin_rep_id_nom) ;
339
            $val2 = array_pop ($chemin_rep_id_nom) ;
340
            array_push ($tabl_resultat, $val2, $val1) ;
341
        }
342
        return $tabl_resultat ;
343
    } // end of member function getCheminIdRepertoire
344
 
345
    /**
346
     *  Calcule le chemin vers le fichier ou le répertoire uploadé
347
     *  renvoie un chaine de la forme dir1/dir2/fichier.ext
348
     *  En prenant comme racine le répertoire du projet, exclu.
349
     * @return  string  Le chemin
350
     */
351
 
352
    function calculeCheminUploaded ($radical) {
353
        // On recherche le chemin vers le fichier, en fonction du répertoire
354
        // ici on renomme le fichier à partir du dernier ID de la table gen_voiraussi
355
        $requete_document = "select pd_id from projet_documents order by pd_id desc limit 1,1" ;
356
        $resultat_document = $this->_db->query($requete_document) ;
357
        $ligne_document = $resultat_document->fetchRow(DB_FETCHMODE_OBJECT) ;
358
        $nouveau_nom = $ligne_document->pd_id+ 1 ;
359
 
360
        $extension = preg_replace("/([^\.]+)\.([a-zA-Z0-9]+$)/", "\\2", $_FILES['fichier']['name']) ;
361
 
362
        $nouveau_nom = $radical."_".$nouveau_nom.".".$extension ;
363
        if ($this->_id_pere != '') {
364
            // On appelle la méthode getCheminIdRepertoire qui renvoie un tableau avec la liste
365
            // des répertoires jusqu'à la racine, on enlève la racine ($i = 0) et on concatène
366
            // toutes les entrées pour obtenir le chemin jusqu'au répertoire courant
367
            $chemin_repertoire_entre_racine_et_repertoire_a_cree = '' ;
368
            $tableau_navigation = $this->getCheminIdRepertoire($this->_id_pere, $this->_db) ;
369
            for ($i = 0; $i < count ($tableau_navigation); $i+=2) $chemin_repertoire_entre_racine_et_repertoire_a_cree.= $tableau_navigation[$i]."/";
370
            $chemin = $chemin_repertoire_entre_racine_et_repertoire_a_cree.$nouveau_nom ;
371
        } else {
372
            // Si l'on est à la racine du projet, le chemin est le nom du fichier
373
            return $nouveau_nom ;
374
        }
375
        return $chemin ;
376
    }
377
 
378
    /**
379
     * Enregistre une ligne dans la table projet_document
380
     * Le tableau de valeur doit contenir les éléments suivants 'document_nom','document_description','document_visibilite','fichier'
381
     *
382
     * @param Array tableau_de_valeur Le tableau de valeur a insérer dans la base avec pour clé les noms des éléments
383
     * @access  public
384
     * @return void
385
     */
386
 
387
    function enregistrerSQL ($valeur, $chemin) {
388
 
389
        // On teste si on a affaire à un répertoire ou un fichier
390
        if (isset ($_FILES['fichier']['name'])) {
391
            // On tente de déterminer le type du fichier à partir de son nom dans $valeur['$fichier']
392
            $tableau_nom = explode (".", $_FILES['fichier']['name']) ;
393
 
394
            // On prend le dernier élément du tableau, si c'est un tableau
395
            if (is_array($tableau_nom)) {
396
                $extension = array_pop($tableau_nom) ;
397
                $type = type_fichier_mime::factory($extension, $this->_db) ;
398
                $id_extension = $type->getIdType() ;
399
            } else {
400
                $id_extension = 12 ;
401
            }
402
            $pd_lien = $chemin ;
403
        } else {    // Le cas ou on a affaire à un répertoire
404
            $id_extension = 0 ;
405
            // Le nom du répertoire est son identifiant avec un slash à la fin
406
            $pd_lien = $chemin."/" ;
407
            if ($this->_id_pere != '') {
408
                // On appelle la méthode getCheminIdRepertoire qui renvoie un tableau avec la liste
409
                // des répertoires jusqu'à la racine, on enlève la racine ($i = 0) et on concatène
410
                // toutes les entrées pour obtenir le chemin jusqu'au répertoire courant
411
                $chemin_repertoire_entre_racine_et_repertoire_a_cree = '' ;
412
                $tableau_navigation = $this->getCheminIdRepertoire($this->_id_pere, $this->_db) ;
413
                for ($i = 0; $i < count ($tableau_navigation); $i+=2) $chemin_repertoire_entre_racine_et_repertoire_a_cree.= $tableau_navigation[$i]."/";
414
                $pd_lien .= $chemin_repertoire_entre_racine_et_repertoire_a_cree ;
415
            }
416
            $pd_lien .= SQL_obtenirNouveauId($this->_db, 'projet_documents', 'pd_id')."/" ;
417
        }
418
        $id = SQL_obtenirNouveauId($this->_db, 'projet_documents', 'pd_id') ;
419
 
420
        $requete = "insert into projet_documents set pd_id=".$id ;
421
        $requete .= ", pd_nom=\"".$valeur['document_nom']."\", pd_description=\"".$valeur['document_description']."\"".
422
                    ", pd_visibilite=\"".$valeur['document_visibilite']."\", pd_date_de_mise_a_jour=NOW(),".
423
                    "pd_ce_projet=\"".$this->_id_projet."\", pd_ce_utilisateur=\"".$this->_id_proprietaire."\"".
424
                    ", pd_pere=\"$this->_id_pere\", pd_ce_type=\"$id_extension\", pd_lien=\"$pd_lien\"" ;
425
 
426
        $resultat = $this->_db->query ($requete) ;
427
        if (DB::isError($resultat)) {
428
            die ("Echec de la requete<br />".$resultat->getMessage()."<br />".$resultat->getDebugInfo()) ;
429
        }
430
        $this->_id = $id ;
431
        return $pd_lien;
432
    }
433
 
434
 
435
    /**
436
     * Met à jour une ligne dans la table projet_document
437
     * Le tableau de valeur doit contenir les éléments suivants 'document_nom','document_description','document_visibilite','fichier'
438
     *
439
     * @param Array tableau_de_valeur Le tableau de valeur a insérer dans la base avec pour clé les noms des éléments
440
     * @access  public
441
     * @return void
442
     */
443
 
444
    function majSQL ($valeur) {
445
        $requete = "update projet_documents set pd_nom=\"".$valeur['document_nom']."\", pd_description=\"".$valeur['document_description']."\"".
446
                    ", pd_visibilite=\"".$valeur['document_visibilite']."\", pd_date_de_mise_a_jour=NOW()".
447
                    " where pd_id=".$this->_id;
448
 
449
        $resultat = $this->_db->query ($requete) ;
450
        if (DB::isError($resultat)) {
451
            die ("Echec de la requete<br />".$resultat->getMessage()."<br />".$resultat->getDebugInfo()) ;
452
        }
453
        return ;
454
    }
455
 
456
    /** Supprime les donnéexs du document dans la table projet_documents
457
     *
458
     *
459
     * @return true en cas de succès
460
     */
461
 
462
    function suppressionSQL () {
463
        $requete = 'delete from projet_documents where pd_id='.$this->_id ;
464
        $resultat = $this->_db->query ($requete) ;
465
        if ($this->_db->affectedRows()) return true ;
466
        return false ;
467
    }
468
 
469
    /**
470
     * Déplace un document au sein d'un même projet
471
     *
472
     * @param int repertoire_destination L'identifiant du répertoire destination.
473
     * @return bool
474
     * @access public
475
     */
476
    function deplace( $repertoire_destination, $repertoire_projet )
477
    {
478
        // On récupère les informations du répertoire cible
479
        if ($repertoire_destination != 0) {
480
            $repertoire_cible = new document ($repertoire_destination, $this->_db) ;
481
            $rep = $repertoire_cible->_pd_lien ;
482
        } else {
483
            $rep = $repertoire_projet.'/' ;
484
        }
485
 
486
        // On récupère le nom du fichier
487
        $decoupe = explode ('/', $this->_pd_lien) ;
488
        $nom_fichier = $decoupe[count($decoupe)-1] ;
489
        $requete = 'update projet_documents set pd_lien="'.$rep.$nom_fichier.'", pd_pere='.$repertoire_destination.' where pd_id='.$this->_id ;
490
        $resultat = $this->_db->query ($requete) ;
491
        if (DB::isError($resultat)) {
492
            echo ("Echec de la requete<br />".$resultat->getMessage()."<br />".$resultat->getDebugInfo()) ;
493
            return false ;
494
        }
495
        return fichier::deplace ($this->_chemin, PROJET_CHEMIN_FICHIER.$rep.$nom_fichier) ;
496
 
497
    } // end of member function deplace
498
 
499
 
500
    /**
501
     * Pour modifier l'identifiant du projet auquel appartient un document.
502
     *
503
     * @param int id_projet L'identifiant du projet.
504
     * @return void
505
     * @access public
506
     */
507
    function setIdProjet( $id_projet )
508
    {
509
        $this->_id_projet = $id_projet ;
510
    } // end of member function setIdProjet
511
 
512
    /**
513
     * Permet de modifier l'identifiant du propietaire d'un projet.
514
     *
515
     * @param int id_proprietaire L'identifiant du proprietaire d'un document.
516
     * @return void
517
     * @access public
518
     */
519
    function setIdProprietaire( $id_proprietaire )
520
    {
521
        $this->_id_proprietaire = $id_proprietaire ;
522
    } // end of member function setIdProprietaire
523
 
524
    /**
525
     * Permet de modifier l'identifiant du répertoire d'un document.
526
     *
527
     * @param int id_repertoire L'identifiant du repertoire d'un document.
528
     * @return void
529
     * @access public
530
     */
531
    function setIdRepertoire( $id_repertoire )
532
    {
533
        $this->_id_pere = $id_repertoire;
534
    } // end of member function setIdRepertoire
535
 
536
    /**
537
     * initAttributes sets all document attributes to its default                    value make
538
     * sure to call this method within your class constructor
539
     */
540
    function initAttributes( )
541
    {
542
        $this->_visibilite = "public";
543
    }
544
 
545
    /**
546
     * Renvoie les derniers documents de l'ensemble des projets.
547
     *
548
     * @param int nombre Le nombre de document à renvoyer
549
     * @return Array
550
     * @static
551
     * @access public
552
     */
553
    function getDocumentsRecents( $nombre = 10, &$objetDB, $chemin, $chemin_icones, $id_projet = '' )
554
    {
555
        // on recherche les documents, hors répertoire
556
        $requete = 'select pd_id from projet_documents where pd_ce_type<>0 ';
557
        if ($id_projet != '') $requete .= ' and pd_ce_projet='.$id_projet.' ';
558
        $requete .= 'order by pd_date_de_mise_a_jour desc limit 0,'.$nombre ;
559
        $resultat = $objetDB->query ($requete) ;
560
        if (DB::isError ($resultat)) {
561
            die ('Echec de la requete : '.$requete.'<br />'.$resultat->getMessage()) ;
562
        }
563
        $tableau_document = array() ;
564
 
565
        while ($ligne = $resultat->fetchRow (DB_FETCHMODE_OBJECT)) {
566
            array_push ($tableau_document, new document ($ligne->pd_id, $objetDB, $chemin, $chemin_icones)) ;
567
        }
568
        return $tableau_document ;
569
    } // end of member function getDocumentsRecents
570
 
571
 
572
} // end of document
573
 
574
 
575
?>