| 1 |
<?php
|
| 2 |
/**
|
| 3 |
* $Id: $
|
| 4 |
* @author Bruno Massa http://drupal.org/user/67164
|
| 5 |
* @file slideshow_creator.inc
|
| 6 |
* Create true slideshows using any image over internet with many other features.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Get all images on a given directory
|
| 11 |
*
|
| 12 |
* @param $path
|
| 13 |
* string, the absolute path to start the scanner
|
| 14 |
* @param $path
|
| 15 |
* boolean, if its a recursive scanning
|
| 16 |
* @return
|
| 17 |
* array, all images (all path)
|
| 18 |
*/
|
| 19 |
function slideshow_creator_explore_dir($path = '', $recursive = FALSE) {
|
| 20 |
static $files = array();
|
| 21 |
|
| 22 |
if (empty($files)) {
|
| 23 |
// This code will work on non GNU systems, like Solaris
|
| 24 |
$exts = array('png', 'gif', 'jpeg', 'jpg', 'bmp',
|
| 25 |
'PNG', 'GIF', 'JPEG', 'JPG', 'BMP');
|
| 26 |
$files = array();
|
| 27 |
foreach ($exts as $ext) {
|
| 28 |
$files = array_merge($files, glob("$path*.$ext"));
|
| 29 |
}
|
| 30 |
|
| 31 |
if ($dirs = glob("$path*", GLOB_ONLYDIR) and !empty($recursive)) {
|
| 32 |
foreach ($dirs as $dir) {
|
| 33 |
$files = array_merge($files, slideshow_creator_explore_dir("$dir/"));
|
| 34 |
}
|
| 35 |
}
|
| 36 |
}
|
| 37 |
|
| 38 |
return $files;
|
| 39 |
}
|
| 40 |
|
| 41 |
/**
|
| 42 |
* The main user hook to display a slideshow.
|
| 43 |
*
|
| 44 |
* @param $output
|
| 45 |
* String, All the page text with the slideshow code
|
| 46 |
* @return
|
| 47 |
* String, All the page text with the slideshow HTML/Javascript
|
| 48 |
*/
|
| 49 |
function _slideshow_creator_process_text($output) {
|
| 50 |
if (preg_match_all('/\[slideshow:([^]]+)\]/i', $output, $matches, PREG_SET_ORDER)) {
|
| 51 |
$max_ss = count($matches);
|
| 52 |
foreach ($matches as $match) {
|
| 53 |
$attributes = preg_split('|(?<!\\\),|', $match[1]);
|
| 54 |
if (!empty($attributes)) {
|
| 55 |
$output = preg_replace('/\[slideshow:([^]]+)\]/i',
|
| 56 |
_slideshow_creator_replace($max_ss, $attributes), $output, 1);
|
| 57 |
}
|
| 58 |
}
|
| 59 |
}
|
| 60 |
return $output;
|
| 61 |
}
|
| 62 |
|
| 63 |
/**
|
| 64 |
* The main user hook to display a slideshow.
|
| 65 |
*
|
| 66 |
* @param &$max_ss
|
| 67 |
* number, How many slideshows there are on the page
|
| 68 |
* @param &$attributes
|
| 69 |
* array, Each slideshow tag
|
| 70 |
* @return
|
| 71 |
* String, The HTML code
|
| 72 |
*/
|
| 73 |
function _slideshow_creator_replace(&$max_ss, &$attributes) {
|
| 74 |
// To ensure that all slideshows have a unique
|
| 75 |
// ID, the current_ss variable will hold a automatic
|
| 76 |
// increasing number
|
| 77 |
static $current_ss = 0;
|
| 78 |
++$current_ss;
|
| 79 |
|
| 80 |
$output = '';
|
| 81 |
|
| 82 |
// using version system, its possible to expand the slideshow functionality
|
| 83 |
// and support old versions
|
| 84 |
switch ((int) $attributes[0]) {
|
| 85 |
case 1:
|
| 86 |
case 2:
|
| 87 |
default:
|
| 88 |
// Default settings
|
| 89 |
$js = array(
|
| 90 |
'fx' => 'fade',
|
| 91 |
'pause' => 1,
|
| 92 |
'speed' => 300,
|
| 93 |
);
|
| 94 |
$slides = array(
|
| 95 |
'total' => 0,
|
| 96 |
'layout' => 'default',
|
| 97 |
);
|
| 98 |
|
| 99 |
$attributes_count = count($attributes);
|
| 100 |
foreach ($attributes as $attribute) {
|
| 101 |
$attribute = explode('=', $attribute);
|
| 102 |
|
| 103 |
// If no , skip the validation
|
| 104 |
if (empty($attribute[1])) {
|
| 105 |
continue;
|
| 106 |
}
|
| 107 |
|
| 108 |
if (trim(strtolower($attribute[0])) == 'img') { // img tag: URL|link|title|description|target format
|
| 109 |
$img_attributes = explode('|' , $attribute[1]);
|
| 110 |
if (!empty($img_attributes[1])) {
|
| 111 |
$slides['total']++;
|
| 112 |
$slides[$slides['total']] = array(
|
| 113 |
'src' => _slideshow_creator_url(check_url(trim($img_attributes[1]))),
|
| 114 |
'link' => !empty($img_attributes[2]) ? _slideshow_creator_url(url(trim($img_attributes[2]))) : 'javascript: void(0);',
|
| 115 |
'title' => !empty($img_attributes[3]) ? check_plain(preg_replace('|\\\,|', ',', trim($img_attributes[3]))) : '',
|
| 116 |
'description' => !empty($img_attributes[4]) ? check_plain(preg_replace('|\\\,|', ',', trim($img_attributes[4]))) : '',
|
| 117 |
'target' => !empty($img_attributes[5]) ? check_plain(trim($img_attributes[5])) : '_blank',
|
| 118 |
);
|
| 119 |
}
|
| 120 |
}
|
| 121 |
elseif (trim(strtolower($attribute[0])) == 'dir') { // dir tag: scan all images from a given directory
|
| 122 |
$dir_attributes = explode('|' , $attribute[1]);
|
| 123 |
if (!empty($dir_attributes[1]) and $files = slideshow_creator_explore_dir('./'. $dir_attributes[1], $dir_attributes[2])) {
|
| 124 |
foreach ($files as $file) {
|
| 125 |
$file = preg_replace('/^\.\//', '', $file);
|
| 126 |
$slides['total']++;
|
| 127 |
$slides[$slides['total']] = array(
|
| 128 |
'src' => $file,
|
| 129 |
'link' => !empty($dir_attributes[3]) ? $file : 'javascript: void(0);',
|
| 130 |
'title' => !empty($dir_attributes[4]) ? check_plain(preg_replace('|\\\,|', ',', trim($dir_attributes[4]))) : '',
|
| 131 |
'description' => !empty($dir_attributes[5]) ? check_plain(preg_replace('|\\\,|', ',', trim($dir_attributes[5]))) : '',
|
| 132 |
'target' => !empty($dir_attributes[6]) ? check_plain(trim($dir_attributes[6])) : '_blank',
|
| 133 |
);
|
| 134 |
}
|
| 135 |
}
|
| 136 |
}
|
| 137 |
else { // all other tags
|
| 138 |
// For retro compatibility, we must filter some results
|
| 139 |
// to simulate the previous behaviour
|
| 140 |
if (trim($attribute[0]) == 'rotate') {
|
| 141 |
$js['speed'] = check_plain(trim($attribute[1]));
|
| 142 |
}
|
| 143 |
elseif (trim($attribute[0]) == 'blend') {
|
| 144 |
}
|
| 145 |
else {
|
| 146 |
$js[trim($attribute[0])] = check_plain(trim($attribute[1]));
|
| 147 |
}
|
| 148 |
}
|
| 149 |
}
|
| 150 |
|
| 151 |
// If at least one slide is valid, build the Slideshow
|
| 152 |
if ($slides['total'] > 0) {
|
| 153 |
|
| 154 |
// To build the correct link for NEXT and PREVIOUS
|
| 155 |
// buttons (for non-javascript browsers), we need to
|
| 156 |
// know which slide each slideshow is now.
|
| 157 |
if (!empty($_GET['ssc'])) {
|
| 158 |
$status_sscs = explode(';', $_GET['ssc']);
|
| 159 |
foreach ($status_sscs as $status_ssc) {
|
| 160 |
$temp = explode(',' , $status_ssc);
|
| 161 |
if ($temp[0] == $current_ss) {
|
| 162 |
$sscs['next'][] = $temp[1] + 1 > $slides['total'] ? $current_ss .',1' : $current_ss .','. ($temp[1] + 1);
|
| 163 |
$sscs['previous'][] = $temp[1] - 1 < 1 ? $current_ss .','. $slides['total'] : $current_ss .','. ($temp[1] - 1);
|
| 164 |
$slides['current'] = $temp[1];
|
| 165 |
$toggle = TRUE;
|
| 166 |
}
|
| 167 |
else {
|
| 168 |
$sscs['next'][] = $status_ssc;
|
| 169 |
$sscs['previous'][] = $status_ssc;
|
| 170 |
$slides['current'] = 1;
|
| 171 |
}
|
| 172 |
}
|
| 173 |
}
|
| 174 |
if (empty($toggle)) {
|
| 175 |
$sscs['next'][] = $slides['total'] > 1 ? check_plain($current_ss .',2') : check_plain($current_ss .',1');
|
| 176 |
$sscs['previous'][] = check_plain($current_ss .','. $slides['total']);
|
| 177 |
$slides['current'] = 1;
|
| 178 |
}
|
| 179 |
$links['next'] = check_plain(implode(';', $sscs['next'])) ;
|
| 180 |
$links['previous'] = check_plain(implode(';', $sscs['previous'])) ;
|
| 181 |
|
| 182 |
// The slideshow should be displayed using
|
| 183 |
// a theme function to allow users to change
|
| 184 |
// it's general look
|
| 185 |
$output = theme('slideshow_creator', $current_ss, $links, $slides, $js);
|
| 186 |
}
|
| 187 |
break;
|
| 188 |
}
|
| 189 |
return $output;
|
| 190 |
}
|
| 191 |
|
| 192 |
/**
|
| 193 |
* Repair internal urls using the pathfilter module, if available.
|
| 194 |
*
|
| 195 |
* @param $url
|
| 196 |
* String, The url to complete (if internal)
|
| 197 |
* @return
|
| 198 |
* String, The full url, filtered from internal
|
| 199 |
*/
|
| 200 |
function _slideshow_creator_url($url) {
|
| 201 |
if (module_exists('pathfilter')) {
|
| 202 |
// Wrap url in double quotes, filter and then remove double quotes
|
| 203 |
return preg_replace("(^\"|\"$)", '', pathfilter_filter('process', 0, -1, '"'. $url .'"'));
|
| 204 |
}
|
| 205 |
else {
|
| 206 |
// Return URL as-is
|
| 207 |
return $url;
|
| 208 |
}
|
| 209 |
}
|
| 210 |
|
| 211 |
/**
|
| 212 |
* Format the slideshow
|
| 213 |
* @ingroup themeable
|
| 214 |
*/
|
| 215 |
function theme_slideshow_creator(&$ssid, &$links, $ss, $js) {
|
| 216 |
// Initializing the variables
|
| 217 |
$output_image = $output_previous = $output_status = $output_next = '';
|
| 218 |
|
| 219 |
// Building the slideshow parts: title, name, slideshow weight and height
|
| 220 |
$title = !empty($ss[$ss['current']]['title']) ? $ss[$ss['current']]['title'] : '';
|
| 221 |
$output_name = !empty($ss['name']) ? $ss['name'] : '';
|
| 222 |
$output_title = '<span class="ssc-title" id="ssc-title-'. $ssid .'">'. $title .'</span>';
|
| 223 |
|
| 224 |
// Create each image. The order should be:
|
| 225 |
// 1* the current picture to the end
|
| 226 |
// 2* the first picture to the current
|
| 227 |
for ($i = $ss['current']; $i <= $ss['total']; ++$i) {
|
| 228 |
// $output_title = '<a href="'. $ss[$ss['current']]['link'] .
|
| 229 |
// '" id="ssc-ltitle-'. $ssid .'" >'. $output_title .'</a>';
|
| 230 |
|
| 231 |
// Build the image
|
| 232 |
$image = theme('image', $ss[$i]['src'], check_plain($title),
|
| 233 |
NULL, NULL, FALSE);
|
| 234 |
|
| 235 |
// Unless the height and width was deternimed, we must
|
| 236 |
// extract these values from each and every image and
|
| 237 |
// select the biggest value to fix the image container
|
| 238 |
// to this size
|
| 239 |
if (empty($ss['width']) and empty($ss['height'])) {
|
| 240 |
list($cur_width, $cur_height) = @getimagesize($ss[$i]['src']);
|
| 241 |
$width = $cur_width > $width ? $cur_width : $width;
|
| 242 |
$height = $cur_height > $height ? $cur_height : $height;
|
| 243 |
}
|
| 244 |
|
| 245 |
// Add a new image link
|
| 246 |
$output_image .= l($image, $ss[$ss['current']]['link'], array('html' => TRUE)) ."\n";
|
| 247 |
}
|
| 248 |
for ($i = 1; $i < $ss['current']; ++$i) {
|
| 249 |
// $output_title = '<a href="'. $ss[$ss['current']]['link'] .
|
| 250 |
// '" id="ssc-ltitle-'. $ssid .'" >'. $output_title .'</a>';
|
| 251 |
|
| 252 |
// Build the image
|
| 253 |
$image = theme('image', $ss[$i]['src'], check_plain($title),
|
| 254 |
NULL, NULL, FALSE);
|
| 255 |
|
| 256 |
// Unless the height and width was deternimed, we must
|
| 257 |
// extract these values from each and every image and
|
| 258 |
// select the biggest value to fix the image container
|
| 259 |
// to this size
|
| 260 |
if (empty($ss['width']) and empty($ss['height'])) {
|
| 261 |
list($cur_width, $cur_height) = @getimagesize($ss[$i]['src']);
|
| 262 |
$width = $cur_width > $width ? $cur_width : $width;
|
| 263 |
$height = $cur_height > $height ? $cur_height : $height;
|
| 264 |
}
|
| 265 |
|
| 266 |
// Add a new image link
|
| 267 |
$output_image .= l($image, $ss[$ss['current']]['link'], array('html' => TRUE)) ."\n";
|
| 268 |
}
|
| 269 |
|
| 270 |
// Setting the image height and width
|
| 271 |
$width = (empty($ss['width']) ? $width : $ss['width']) .'px';
|
| 272 |
$height = (empty($ss['height']) ? $height : $ss['height']) .'px';
|
| 273 |
|
| 274 |
// Put all images inside a big SPAN tag
|
| 275 |
// $output_image = "\n<span id='ssc-pic-$ssid' class='ssc-pic' width=\"$width\" height=\"$height\">\n".
|
| 276 |
// $output_image ."</span>\n";
|
| 277 |
$output_image = "\n<span id='ssc-pic-$ssid' class='ssc-pic' style='width:$width;height:$height'>\n".
|
| 278 |
$output_image ."</span>\n";
|
| 279 |
|
| 280 |
|
| 281 |
// Put a PREVIOUS and NEXT buttons if there are more
|
| 282 |
// then one image (generally TRUE for a slideshow)
|
| 283 |
if ($ss['total'] > 1) {
|
| 284 |
$output_previous = l(t('Previous'), $_GET['q'], array('query' => 'ssc='. $links['previous'],
|
| 285 |
'attributes' => array('id' => 'ssc-previous-'. $ssid, 'class' => 'ssc-previous')));
|
| 286 |
$output_status = ' <span class="ssc-index" id="ssc-index-'. $ssid .'">'.
|
| 287 |
t('Image') .' <span class="ssc-current" id="ssc-current-'. $ssid .'">'.
|
| 288 |
$ss['current'] .'</span>/'. $ss['total'] .' </span>';
|
| 289 |
$output_next = l(t('Next'), $_GET['q'], array('query' => 'ssc='. $links['next'],
|
| 290 |
'attributes' => array('id' => 'ssc-next-'. $ssid, 'class' => 'ssc-next')));
|
| 291 |
}
|
| 292 |
|
| 293 |
$output_descript = '<span class="ssc-description" id="ssc-description-'.
|
| 294 |
$ssid .'">'. $ss[$ss['current']]['description'] ."</span>\n";
|
| 295 |
|
| 296 |
// This will put the pieces into a given order,
|
| 297 |
// base on which "layout" the user chose.
|
| 298 |
if (empty($ss['layout']) or $ss['layout'] == 'default') {
|
| 299 |
$output = $output_previous . $output_status . $output_next . $output_title .
|
| 300 |
$output_image . $output_descript;
|
| 301 |
}
|
| 302 |
elseif ($ss['layout'] == 'reverse') {
|
| 303 |
$output = $output_title . $output_image . $output_descript . $output_previous .
|
| 304 |
$output_status . $output_next;
|
| 305 |
}
|
| 306 |
elseif ($ss['layout'] == 'bottom') {
|
| 307 |
$output = $output_image . $output_title . $output_descript . $output_previous .
|
| 308 |
$output_status . $output_next;
|
| 309 |
}
|
| 310 |
elseif ($ss['layout'] == 'top') {
|
| 311 |
$output = $output_title . $output_descript . $output_previous . $output_status .
|
| 312 |
$output_next . $output_image;
|
| 313 |
}
|
| 314 |
|
| 315 |
// Add the CSS file
|
| 316 |
drupal_add_css(drupal_get_path('module', 'slideshow_creator') .'/slideshow_creator.css');
|
| 317 |
|
| 318 |
// Add the main JavaScript, that does all the magic
|
| 319 |
drupal_add_js(drupal_get_path('module', 'slideshow_creator') .'/slideshow_creator.js');
|
| 320 |
drupal_add_js(drupal_get_path('module', 'slideshow_creator') .'/jquery.cycle.all.js');
|
| 321 |
|
| 322 |
// Add specific data for this slideshow
|
| 323 |
drupal_add_js(array('ssc' => array($ss['current'] => $js)), 'setting');
|
| 324 |
|
| 325 |
// Put everything inside a SPAN tag
|
| 326 |
return '<span class="ssc '. $output_name .'" id="ssc-'. $ssid ."\">\n".
|
| 327 |
$output ."\n</span>\n";
|
| 328 |
}
|