| 1 |
<?php
|
| 2 |
// $Id: fast_gallery.class.php,v 1.26 2009/07/17 20:23:10 rapsli Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* This is a helper class doing most of the database calls.
|
| 7 |
*
|
| 8 |
* @author Raphael Sch�r - www.schaerwebdesign.ch
|
| 9 |
*/
|
| 10 |
|
| 11 |
include_once('FGImage.class.php');
|
| 12 |
|
| 13 |
class FastGallery {
|
| 14 |
private static $instance = null;
|
| 15 |
|
| 16 |
/**
|
| 17 |
* We are implementing a singleton pattern
|
| 18 |
*/
|
| 19 |
private function __construct() {
|
| 20 |
}
|
| 21 |
|
| 22 |
public function getInstance() {
|
| 23 |
if (is_null(self :: $instance)) {
|
| 24 |
self :: $instance = new self;
|
| 25 |
}
|
| 26 |
return self :: $instance;
|
| 27 |
}
|
| 28 |
|
| 29 |
/**
|
| 30 |
* Checks for gallery additions/deletions only. Leaves existing files alone.
|
| 31 |
*/
|
| 32 |
public function rescanGallery() {
|
| 33 |
$path = variable_get('fast_gallery_path', 'sites/default/files/');
|
| 34 |
$path = utf8_decode($path);
|
| 35 |
|
| 36 |
if (!is_dir($path)) {
|
| 37 |
watchdog('fast_gallery', 'No gallery path specified! Do it at !link', array('!link' => l('admin/config/fast_gallery', 'admin/config/fast_gallery')));
|
| 38 |
}
|
| 39 |
// Get current file list hierarchy
|
| 40 |
$files = $this->exploreDir($path, TRUE);
|
| 41 |
|
| 42 |
// Delete non-existent files
|
| 43 |
//$this->deletePics();
|
| 44 |
|
| 45 |
// Process the files
|
| 46 |
$FgImgContainer = array();
|
| 47 |
$this->processFiles($files, $FgImgContainer);
|
| 48 |
|
| 49 |
$storage = $this->getStorageEngine();
|
| 50 |
$storage->storeImages($FgImgContainer);
|
| 51 |
|
| 52 |
// Check for orphan images/folders and fix it
|
| 53 |
//$this->checkHierarchy();
|
| 54 |
}
|
| 55 |
|
| 56 |
/**
|
| 57 |
* Return the storage engine
|
| 58 |
* @return Istorage
|
| 59 |
*/
|
| 60 |
public function getStorageEngine() {
|
| 61 |
$file = variable_get('fg_storage_engine',FG_DEFAULT_STORAGE_ENGINE);
|
| 62 |
include_once(drupal_get_path('module', 'fast_gallery') . '/storageengine/' . $file);
|
| 63 |
switch ($file) {
|
| 64 |
case 'default.storage.inc':
|
| 65 |
return new DefaultStorage();
|
| 66 |
}
|
| 67 |
}
|
| 68 |
|
| 69 |
/**
|
| 70 |
* Process the images (which are right now only a path) and
|
| 71 |
* make nice FGImages out of them so that they can be
|
| 72 |
* passed to the storage engine
|
| 73 |
*
|
| 74 |
* @param ar_files
|
| 75 |
* Array of files, as returned by exploreDir()
|
| 76 |
* @param fGImagesContainer
|
| 77 |
* Array that collects the created FGImages objects
|
| 78 |
*/
|
| 79 |
private function processFiles($ar_files, &$fGImagesContainer) {
|
| 80 |
foreach ($ar_files as $key => $value) {
|
| 81 |
// If we found a folder, recurse through it
|
| 82 |
if (is_array($value)) {
|
| 83 |
$this->processFiles($value, $fGImagesContainer);
|
| 84 |
}
|
| 85 |
else {
|
| 86 |
// Handle files with special characters.
|
| 87 |
$value_slash = str_replace('\\', '/', utf8_encode($value));
|
| 88 |
$fGImagesContainer[] = new FGImage($value_slash);
|
| 89 |
}
|
| 90 |
|
| 91 |
//we don't care about empty arrays
|
| 92 |
if (is_array($value) && count($value) < 1) {
|
| 93 |
continue;
|
| 94 |
}
|
| 95 |
}
|
| 96 |
//return $fgImagesContainer;
|
| 97 |
}
|
| 98 |
|
| 99 |
/**
|
| 100 |
* clearing the db -> removing all entries from the db
|
| 101 |
*
|
| 102 |
*/
|
| 103 |
public function clearDb() {
|
| 104 |
$storage = $this->getStorageEngine();
|
| 105 |
$storage->clearDb();
|
| 106 |
}
|
| 107 |
|
| 108 |
|
| 109 |
/**
|
| 110 |
* Get all images in given directory.
|
| 111 |
*
|
| 112 |
* @param path
|
| 113 |
* Absolute path of the directory
|
| 114 |
* @param recursive
|
| 115 |
* Specify whether to recurse through subdirectories.
|
| 116 |
* @return
|
| 117 |
* Array of arrays of image files. Each array element corresponds to
|
| 118 |
* an image type. If recursive is on, returns an array tree hierarchy.
|
| 119 |
*/
|
| 120 |
private function exploreDir($path = '', $recursive = FALSE) {
|
| 121 |
// List of image extensions
|
| 122 |
$exts = array (
|
| 123 |
'jpg',
|
| 124 |
'jpeg',
|
| 125 |
'png',
|
| 126 |
'gif',
|
| 127 |
'bmp',
|
| 128 |
/*'flv',
|
| 129 |
'mov',
|
| 130 |
'wmv',
|
| 131 |
'asx',
|
| 132 |
'swf',
|
| 133 |
'pdf',*/
|
| 134 |
);
|
| 135 |
// Get all image files of each file type
|
| 136 |
$files = array ();
|
| 137 |
foreach ($exts as $ext) {
|
| 138 |
$pattern = sql_regcase($path . '*.' . $ext);
|
| 139 |
$files[] = glob($pattern);
|
| 140 |
}
|
| 141 |
|
| 142 |
// Recurse through subdirectories if necessary
|
| 143 |
if ($recursive) {
|
| 144 |
$dirs = glob($path . '*', GLOB_MARK | GLOB_ONLYDIR);
|
| 145 |
if (is_array($dirs)) {
|
| 146 |
foreach ($dirs as $dir) {
|
| 147 |
$files[] = $this->exploreDir($dir, TRUE);
|
| 148 |
}
|
| 149 |
}
|
| 150 |
}
|
| 151 |
return $files;
|
| 152 |
}
|
| 153 |
|
| 154 |
/**
|
| 155 |
* Building the array for the breadcrumbs
|
| 156 |
* @return array
|
| 157 |
*/
|
| 158 |
public function buildBreadCrumbs() {
|
| 159 |
$bc = array();
|
| 160 |
$path = '';
|
| 161 |
foreach (arg() as $item) {
|
| 162 |
$path .= $item . '/';
|
| 163 |
$bc[] = l($item, $path);
|
| 164 |
}
|
| 165 |
return $bc;
|
| 166 |
}
|
| 167 |
}
|