Luxbum.net - Script de galerie photo

Changeset 260

Show
Ignore:
Timestamp:
02/11/08 21:14:11 (9 months ago)
Author:
nicolas
Message:

- optimisation de l'algo de recherche de l'image par défaut
- standards de code

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/trunk-newurl/api/process/luxbumgallery.php

    r256 r260  
    1717    /**-----------------------------------------------------------------------**/ 
    1818    var $dir; 
    19     
     19 
    2020    var $sortPosition = ''; 
    2121    var $private = false; 
     
    4444 
    4545        $this->addAllFiles($fileType); 
    46         $this->_completeInfos (); 
     46        $this->_completeInfos(); 
    4747        $this->_loadPrivate(); 
    4848    } 
     
    5757     * @access private 
    5858     */ 
    59     function _completeInfos () { 
    60         if ($fd = dir ($this->dirPath)) { 
    61             while ($current_file = $fd->read ()) { 
    62                 if (files::isPhotoFile($this->dir, $current_file)) { 
    63                     $theFile = luxbum::getFilePath($this->dir, $current_file); 
    64                     $this->imageSize += filesize ($theFile); 
    65                     $this->imageCount++;  
    66                 } 
    67                 else if (files::isFlvFile($this->dir, $current_file)) { 
    68                     $theFile = luxbum::getFilePath($this->dir, $current_file); 
    69                     $this->flvSize += filesize ($theFile); 
    70                     $this->flvCount++;  
     59    function _completeInfos() { 
     60        if ($fd = dir($this->dirPath)) { 
     61            while ($currentFile = $fd->read()) { 
     62                if (files::isPhotoFile($this->dir, $currentFile)) { 
     63                    $theFile = luxbum::getFilePath($this->dir, $currentFile); 
     64                    $this->imageSize += filesize($theFile); 
     65                    $this->imageCount++; 
     66                } 
     67                else if (files::isFlvFile($this->dir, $currentFile)) { 
     68                    $theFile = luxbum::getFilePath($this->dir, $currentFile); 
     69                    $this->flvSize += filesize($theFile); 
     70                    $this->flvCount++; 
    7171                } 
    7272            } 
     
    9292 
    9393    /** 
    94      *  
     94     * 
    9595     */ 
    9696    function setDir($dir) { 
     
    102102     * @return String Chemin de la galerie 
    103103     */ 
    104     function getDir () { 
     104    function getDir() { 
    105105        return $this->dir; 
    106106    } 
     
    123123 
    124124    /** 
    125      *  
     125     * 
    126126     */ 
    127127    function isUnlocked() { 
     
    129129        return $privateManager->isUnlocked($this->dir); 
    130130    } 
    131     
     131 
    132132    function isPrivateAndLocked($current = true) { 
    133133        return $this->isPrivate() && !$this->isUnlocked(); 
     
    140140     * Lit le fichier des dates/descriptions et retourne un tableau 
    141141     * correspondant par rapport au nom de fichier. 
    142      *  
     142     * 
    143143     * @return array clᅵ: nomPhoto / valeur: date|description 
    144144     */ 
    145     function getDescriptions () { 
    146         $desc = array (); 
    147  
    148         if (file_exists($this->dirPath.DESCRIPTION_FILE)) {  
    149             $fd = @fopen ($this->dirPath.DESCRIPTION_FILE, 'r'); 
     145    function getDescriptions() { 
     146        $desc = array(); 
     147 
     148        if (file_exists($this->dirPath.DESCRIPTION_FILE)) { 
     149            $fd = @fopen($this->dirPath.DESCRIPTION_FILE, 'r'); 
    150150            if ($fd === false) { 
    151151                throw new Pluf_HTTP_FileSystemException(__("Unable to open file: ").$this->dirPath.DESCRIPTION_FILE); 
    152152            } 
    153153            while ($line = trim(fgets($fd)))  { 
    154                 if ( ereg ('^.*\|.*\|.*$', $line)) { 
    155                     list ($imgName, $imgDescription) = explode ('|', $line, 2); 
     154                if ( ereg('^.*\|.*\|.*$', $line)) { 
     155                    list($imgName, $imgDescription) = explode('|', $line, 2); 
    156156                    $desc [$imgName] = $imgDescription; 
    157157                } 
    158158            } 
    159             fclose ($fd); 
     159            fclose($fd); 
    160160        } 
    161161        return $desc; 
    162162    } 
    163     
     163 
    164164    /** 
    165165     * Crᅵe ou met ᅵ jour le fichier des descriptions des photos 
    166      *  
    167      */ 
    168     function createOrMajDescriptionFile () { 
    169         $desc = array (); 
     166     * 
     167     */ 
     168    function createOrMajDescriptionFile() { 
     169        $desc = array(); 
    170170 
    171171        // Search images which are in the description file 
    172172        if (file_exists($this->dirPath . DESCRIPTION_FILE)) { 
    173             if (!($fd = @fopen ($this->dirPath . DESCRIPTION_FILE, 'r')))  { 
     173            if (!($fd = @fopen($this->dirPath . DESCRIPTION_FILE, 'r')))  { 
    174174                throw new Pluf_HTTP_FileSystemException(__("Unable to read file: ").$this->dirPath.DESCRIPTION_FILE); 
    175             }  
     175            } 
    176176            else { 
    177                 while ($line = fgets ($fd)) { 
    178                     if (ereg ('^.*\|.*\|.*$', $line)) { 
    179                         list($desc[]) = explode ('|', $line, 2); 
     177                while ($line = fgets($fd)) { 
     178                    if (ereg('^.*\|.*\|.*$', $line)) { 
     179                        list($desc[]) = explode('|', $line, 2); 
    180180                    } 
    181181                } 
     
    185185 
    186186        // On ajoute les images non présentes dans le fichier de description 
    187         if (!($fd = @fopen ($this->dirPath . DESCRIPTION_FILE, 'a+'))) {  
     187        if (!($fd = @fopen($this->dirPath . DESCRIPTION_FILE, 'a+'))) { 
    188188            throw new Pluf_HTTP_FileSystemException(__("Unable to write file: ").$this->dirPath.DESCRIPTION_FILE); 
    189         }  
     189        } 
    190190        else { 
    191191            reset($this->arrayList); 
    192             while (list (,$img) = each ($this->arrayList)) { 
     192            while (list(,$img) = each($this->arrayList)) { 
    193193                $name = $img->getFile(); 
    194                 if (!in_array ($name, $desc)) { 
    195                     fputs ($fd, "$name||\n"); 
    196                 } 
    197             } 
    198             fclose ($fd); 
     194                if (!in_array($name, $desc)) { 
     195                    fputs($fd, "$name||\n"); 
     196                } 
     197            } 
     198            fclose($fd); 
    199199        } 
    200200    } 
     
    203203     * Update the description file 
    204204     */ 
    205     function updateDescriptionFile () { 
     205    function updateDescriptionFile() { 
    206206        if (!files::deleteFile($this->dirPath . DESCRIPTION_FILE)) { 
    207207            throw new Pluf_HTTP_FileSystemException(__("Unable to delete file: ").$this->dirPath.DESCRIPTION_FILE); 
    208208        } 
    209        
     209 
    210210        if (!($fd = fopen ($this->dirPath . DESCRIPTION_FILE, 'a'))) { 
    211211            throw new Pluf_HTTP_FileSystemException(__("Unable to delete file: ").$this->dirPath.DESCRIPTION_FILE); 
    212212        } 
    213213        else { 
    214             for ($i = 0 ; $i < $this->getTotalCount () ; $i++) { 
     214            for ($i = 0 ; $i < $this->getTotalCount() ; $i++) { 
    215215                $img = $this->list[$i]; 
    216                 $name = $img->getFile (); 
    217                 $description = $img->getDescription (); 
    218                 $date = $img->getDate (); 
    219                 fputs ($fd, "$name|$date|$description\n"); 
     216                $name = $img->getFile(); 
     217                $description = $img->getDescription(); 
     218                $date = $img->getDate(); 
     219                fputs($fd, "$name|$date|$description\n"); 
    220220            } 
    221221            fclose ($fd); 
    222222        } 
    223223    } 
    224     
     224 
    225225    /**-----------------------------------------------------------------------**/ 
    226226    /** Fonctions des sous galeries */ 
     
    230230     * 
    231231     */ 
    232     function isSubGallery ($current_file) { 
    233         if (!is_dir($this->dirPath.$current_file)) { 
     232    function isSubGallery($currentFile) { 
     233        if (!is_dir($this->dirPath.$currentFile)) { 
    234234            return false; 
    235235        } 
    236         if ($current_file[0] == '.') { 
     236        if ($currentFile[0] == '.') { 
    237237            return false; 
    238238        } 
    239         if ($current_file == files::removeTailSlash(THUMB_DIR) 
    240             || $current_file == files::removeTailSlash(PREVIEW_DIR)) { 
     239        if ($currentFile == files::removeTailSlash(THUMB_DIR) 
     240            || $currentFile == files::removeTailSlash(PREVIEW_DIR)) { 
    241241            return false; 
    242242        } 
    243         if (files::isPhotoFile($this->dir, $current_file)) { 
     243        if (files::isPhotoFile($this->dir, $currentFile)) { 
    244244            return false; 
    245245        } 
     
    253253        return count($this->listSubGallery) > 0; 
    254254    } 
    255     
     255 
    256256    /** 
    257257     * 
     
    262262 
    263263            // Parcours des photos du dossiers 
    264             while ($current_file = readdir ($dir_fd)) { 
    265                 if ($this->isSubGallery($current_file)) { 
    266                     $this->listSubGallery[] = $this->dir.$current_file; 
     264            while ($currentFile = readdir($dir_fd)) { 
     265                if ($this->isSubGallery($currentFile)) { 
     266                    $this->listSubGallery[] = $this->dir.$currentFile; 
    267267                } 
    268268            } 
     
    280280     */ 
    281281    private function addAllFiles($fileType) { 
    282        
     282 
    283283        if ($fileType == '') { 
    284284            $fileType = TYPE_IMAGE_FILE | TYPE_FLV_FILE; 
     
    286286 
    287287        // Ouverture du dossier des photos 
    288         if ($dir_fd = opendir ($this->dirPath)) { 
    289          
     288        if ($dir_fd = opendir($this->dirPath)) { 
     289 
    290290            // Fetch all images, their descriptions and their orders 
    291             $desc = $this->getDescriptions (); 
     291            $desc = $this->getDescriptions(); 
    292292            $this->_loadSort(); 
    293      
     293 
    294294            // Parcours des photos du dossiers 
    295             while ($current_file = readdir ($dir_fd)) { 
    296              
     295            while ($currentFile = readdir($dir_fd)) { 
     296 
    297297                // Add an image file 
    298                 if (files::isPhotoFile ($this->dir, $current_file) && ($fileType & TYPE_IMAGE_FILE)) { 
    299                     $imageTemp = new luxBumImage ($this->dir, $current_file); 
    300                  
     298                if (files::isPhotoFile($this->dir, $currentFile) &&($fileType & TYPE_IMAGE_FILE)) { 
     299                    $imageTemp = new luxBumImage($this->dir, $currentFile); 
     300 
    301301                    // On affecte les dates / descriptions 
    302                     if (array_key_exists ($current_file, $desc)) { 
    303                         list ($imgDate, $imgDescription) = explode ("|", $desc[$current_file], 2); 
    304                         $imageTemp->setAllDescription ($imgDescription, $imgDate); 
     302                    if (array_key_exists($currentFile, $desc)) { 
     303                        list($imgDate, $imgDescription) = explode("|", $desc[$currentFile], 2); 
     304                        $imageTemp->setAllDescription($imgDescription, $imgDate); 
    305305                    } 
    306                  
     306 
    307307                    // On affecte l'ordre 
    308                     if (array_key_exists ($current_file, $this->sortList)) { 
    309                         $imageTemp->setSortPosition($this->sortList[$current_file]); 
     308                    if (array_key_exists($currentFile, $this->sortList)) { 
     309                        $imageTemp->setSortPosition($this->sortList[$currentFile]); 
    310310                    } 
    311311                    $this->addToList($imageTemp); 
    312312                } 
    313              
     313 
    314314                // Add a flash video file 
    315                 else if (files::isFlvFile($this->dir, $current_file) && ($fileType & TYPE_FLV_FILE)) { 
    316                     $object = new LuxbumFlv($this->dir, $current_file); 
     315                else if (files::isFlvFile($this->dir, $currentFile) &&($fileType & TYPE_FLV_FILE)) { 
     316                    $object = new LuxbumFlv($this->dir, $currentFile); 
    317317                    $this->addToList($object); 
    318318                } 
    319319            } 
    320             closedir ($dir_fd); 
    321         } 
    322        
    323         if (count ($this->arrayList) > 0) { 
     320            closedir($dir_fd); 
     321        } 
     322 
     323        if (count($this->arrayList) > 0) { 
    324324            $this->arrayList = $this->sortRecordset($this->arrayList, $this->sortType, $this->sortOrder); 
    325325        } 
    326326    } 
    327     
    328     
     327 
     328 
    329329    /**-----------------------------------------------------------------------**/ 
    330330    /** Fonctions de tri */ 
     
    378378     * @access private 
    379379     */ 
    380     function _completeDefaultImage () { 
    381         $default = ''; 
    382  
    383         // On est dans une sous galerie sans images 
     380    protected function _completeDefaultImage() { 
     381 
     382        // The gallery has no image 
    384383        if ($this->getTotalCount() == 0) { 
    385384            $this->preview = ''; 
    386         } 
    387        
     385            return; 
     386        } 
     387 
    388388        if ($this->getImageCount() == 0 && $this->getFlvCount() > 0) { 
    389           
     389 
    390390        } 
    391391 
     
    394394            $fd = fopen(luxbum::getFilePath($this->dir, DEFAULT_INDEX_FILE), 'r'); 
    395395            $line = fgets($fd); 
    396           
     396 
    397397            if (files::isFile($this->dir, $line)) { 
    398                 $default = $line; 
     398                $this->preview = $line; 
     399                fclose($fd); 
     400                return; 
    399401            } 
    400402            fclose($fd); 
     
    402404 
    403405        // Search the first image of the gallery 
    404         if ($default == '') { 
    405             $trouve = false; 
    406             $apercu_fd = opendir ($this->dirPath); 
    407  
    408             while (!$trouve && $current_file = readdir ($apercu_fd)) { 
    409                 if ($current_file[0] != '.'  
    410                     && !is_dir (luxbum::getImage ($this->dir, $current_file))  
    411                     && eregi ('^.*(' . ALLOWED_FORMAT . ')$', $current_file)) { 
    412                     $default = $current_file; 
    413                     $trouve = true; 
    414                 } 
    415             } 
    416             closedir($apercu_fd); 
    417         } 
    418  
    419         $this->preview = $default; 
     406        $fd = opendir($this->dirPath); 
     407 
     408        while ($currentFile = readdir($fd)) { 
     409            if ($currentFile[0] != '.' 
     410                && !is_dir(luxbum::getImage($this->dir, $currentFile)) 
     411                && eregi('^.*(' . ALLOWED_FORMAT . ')$', $currentFile)) { 
     412                $this->preview = $currentFile; 
     413                closedir($fd); 
     414                return; 
     415            } 
     416        } 
     417        closedir($fd); 
    420418    } 
    421419 
     
    423421     * Set an user defined image 
    424422     * @param String $filename Filename of the user defined image 
    425      * @return boolean  
    426      */ 
    427     function setNewDefaultImage ($filename) { 
     423     * @return boolean 
     424     */ 
     425    function setNewDefaultImage($filename) { 
    428426        $theFile = $this->dirPath . $filename; 
    429         if (!is_file ($theFile)) { 
     427        if (!is_file($theFile)) { 
    430428            return false; 
    431429        } 
    432         files::writeFile ($this->dirPath . DEFAULT_INDEX_FILE, $filename); 
     430        files::writeFile($this->dirPath . DEFAULT_INDEX_FILE, $filename); 
    433431        return true; 
    434432    } 
    435     
    436     
     433 
     434 
    437435    /**-----------------------------------------------------------------------**/ 
    438436    /** Rename / delete functions */ 
     
    443441     * @return boolean Rename success (true) or failed (false) 
    444442     */ 
    445     function rename ($newName) { 
    446         if (files::renameDir ($this->dirPath, luxbum::getFsPath ($newName))) { 
    447             commentaire::renameGalerie ($this->dir, $newName); 
     443    function rename($newName) { 
     444        if (files::renameDir($this->dirPath, luxbum::getFsPath($newName))) { 
     445            commentaire::renameGalerie($this->dir, $newName); 
    448446            $this->setName($newName); 
    449447            return true; 
     
    451449        return false; 
    452450    } 
    453     
    454     /** 
    455      * Delete a gallery  
    456      * @static  
     451 
     452    /** 
     453     * Delete a gallery 
     454     * @static 
    457455     * @param String $galleryName Gallery name to delete 
    458456     */ 
    459     function delete ($galleryName) { 
    460         files::deltree (luxbum::getFsPath ($galleryName)); 
    461         commentaire::deleteGalerie ($galleryName); 
    462     } 
    463     
     457    function delete($galleryName) { 
     458        files::deltree(luxbum::getFsPath($galleryName)); 
     459        commentaire::deleteGalerie($galleryName); 
     460    } 
     461 
    464462    /** 
    465463     * Delete the gallery cache. 
    466464     */ 
    467     function clearCache () { 
     465    function clearCache() { 
    468466        $this->reset(); 
    469         while (list (,$img) = each ($this->arrayList)) { 
    470             $img->clearCache (); 
    471         } 
    472     } 
    473  
    474     
    475 //    /** 
    476 //     * @return string the link url to select all images in the current gallery 
    477 //     */ 
    478 //    public function getLinkSelectAll() { 
    479 //       return link::selectall($this->dir); 
    480 //    } 
    481     
    482 //    /** 
    483 //     * @return string the link url to select all images in the current gallery 
    484 //     */ 
    485 //    public function getLinkUnselectAll() { 
    486 //       return link::unselectall($this->dir); 
    487 //    } 
    488     
     467        while (list(,$img) = each($this->arrayList)) { 
     468            $img->clearCache(); 
     469        } 
     470    } 
     471 
     472    /** 
     473     * Empty implementation of the __toString() method 
     474     */ 
    489475    function __toString() { 
    490476        echo "luxbumgallery::__toString();";