| 1 |
<?php
|
| 2 |
// $Id:
|
| 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 |
$arGalleries = variable_get('fg_galleries', array());
|
| 34 |
foreach ($arGalleries as $gallery) {
|
| 35 |
$path = utf8_decode($gallery['fg_path']);
|
| 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 |
$engines = module_invoke_all('fast_gallery_info');
|
| 62 |
|
| 63 |
$file = variable_get('fg_storage_engine', FG_DEFAULT_STORAGE_ENGINE);
|
| 64 |
|
| 65 |
include_once($engines['storageengine'][$file]['path'].'/'.$engines['storageengine'][$file]['file']);
|
| 66 |
switch ($file) {
|
| 67 |
case 'default':
|
| 68 |
print 'hallo welt';
|
| 69 |
return new DefaultStorage();
|
| 70 |
}
|
| 71 |
}
|
| 72 |
|
| 73 |
/**
|
| 74 |
* Process the images (which are right now only a path) and
|
| 75 |
* make nice FGImages out of them so that they can be
|
| 76 |
* passed to the storage engine
|
| 77 |
*
|
| 78 |
* @param ar_files
|
| 79 |
* Array of files, as returned by exploreDir()
|
| 80 |
* @param fGImagesContainer
|
| 81 |
* Array that collects the created FGImages objects
|
| 82 |
*/
|
| 83 |
private function processFiles($ar_files, &$fGImagesContainer) {
|
| 84 |
foreach ($ar_files as $key => $value) {
|
| 85 |
// If we found a folder, recurse through it
|
| 86 |
if (is_array($value)) {
|
| 87 |
$this->processFiles($value, $fGImagesContainer);
|
| 88 |
}
|
| 89 |
else {
|
| 90 |
// Handle files with special characters.
|
| 91 |
$value_slash = str_replace('\\', '/', utf8_encode($value));
|
| 92 |
$fGImagesContainer[] = new FGImage($value_slash);
|
| 93 |
}
|
| 94 |
|
| 95 |
//we don't care about empty arrays
|
| 96 |
if (is_array($value) && count($value) < 1) {
|
| 97 |
continue;
|
| 98 |
}
|
| 99 |
}
|
| 100 |
//return $fgImagesContainer;
|
| 101 |
}
|
| 102 |
|
| 103 |
/**
|
| 104 |
* clearing the db -> removing all entries from the db
|
| 105 |
*
|
| 106 |
*/
|
| 107 |
public function clearDb() {
|
| 108 |
$storage = $this->getStorageEngine();
|
| 109 |
$storage->clearDb();
|
| 110 |
}
|
| 111 |
|
| 112 |
|
| 113 |
/**
|
| 114 |
* Get all images in given directory.
|
| 115 |
*
|
| 116 |
* @param path
|
| 117 |
* Absolute path of the directory
|
| 118 |
* @param recursive
|
| 119 |
* Specify whether to recurse through subdirectories.
|
| 120 |
* @return
|
| 121 |
* Array of arrays of image files. Each array element corresponds to
|
| 122 |
* an image type. If recursive is on, returns an array tree hierarchy.
|
| 123 |
*/
|
| 124 |
private function exploreDir($path = '', $recursive = FALSE) {
|
| 125 |
// List of image extensions
|
| 126 |
$exts = array (
|
| 127 |
'jpg',
|
| 128 |
'jpeg',
|
| 129 |
'png',
|
| 130 |
'gif',
|
| 131 |
'bmp',
|
| 132 |
/*'flv',
|
| 133 |
'mov',
|
| 134 |
'wmv',
|
| 135 |
'asx',
|
| 136 |
'swf',
|
| 137 |
'pdf',*/
|
| 138 |
);
|
| 139 |
// Get all image files of each file type
|
| 140 |
$files = array ();
|
| 141 |
foreach ($exts as $ext) {
|
| 142 |
$pattern = sql_regcase($path . '*.' . $ext);
|
| 143 |
$files[] = glob($pattern);
|
| 144 |
}
|
| 145 |
|
| 146 |
// Recurse through subdirectories if necessary
|
| 147 |
if ($recursive) {
|
| 148 |
$dirs = glob($path . '*', GLOB_MARK | GLOB_ONLYDIR);
|
| 149 |
if (is_array($dirs)) {
|
| 150 |
foreach ($dirs as $dir) {
|
| 151 |
$files[] = $this->exploreDir($dir, TRUE);
|
| 152 |
}
|
| 153 |
}
|
| 154 |
}
|
| 155 |
return $files;
|
| 156 |
}
|
| 157 |
|
| 158 |
/**
|
| 159 |
* Building the array for the breadcrumbs
|
| 160 |
* @return array
|
| 161 |
*/
|
| 162 |
public function buildBreadCrumbs() {
|
| 163 |
$bc = array();
|
| 164 |
$path = '';
|
| 165 |
foreach (arg() as $item) {
|
| 166 |
$path .= $item . '/';
|
| 167 |
$bc[] = l($item, $path);
|
| 168 |
}
|
| 169 |
return $bc;
|
| 170 |
}
|
| 171 |
}
|