brilliant_gallery.inc in Brilliant Gallery 7.2
File
brilliant_gallery.incView source
<?php
/**
* Recursively read all gallery images and repopulate table brilliant_gallery_sources.
*/
function brilliant_gallery_getbgfiles() {
// @TODO Run here a function to clean expired records (those files were probably deleted).
$bg_folder_abs = 'public://' . variable_get('brilliant_gallery_folder', '');
//$files_to_add = file_scan_directory($dir, $mask, array('key' => $key, 'min_depth' => $min_depth));
$mask = '/\\.(png|jpe?g)$/i';
$files_to_add = file_scan_directory($bg_folder_abs, $mask, array(
'recurse' => TRUE,
));
//dpm($files_to_add);
//$results = array();
$request_time = REQUEST_TIME;
// We need to use the same time for all updated records, so let's put it into a variable here.
foreach ($files_to_add as $object) {
// Get just the relative path of the album
$hidden = (int) brilliant_gallery_image_is_hidden($object->uri);
// Both "'/' . $object->filename" and "$object->filename" must be eliminated from path; this is because there may be images also in the root of the albums folder.
//if (mt_rand(1,5)==3) dpm($object); dpm(filemtime($object->uri));
$album_path = str_replace(array(
$bg_folder_abs . '/',
'/' . $object->filename,
$object->filename,
), '', $object->uri);
//$results[] = array($album_path, $object->filename, $hidden);
//'" . md5($album_path.$object->filename.filemtime($object->uri)) . "',
// Update rows with the same path and filename (their file timestamp or visibility or caption may have changed), or insert them if they do not exist. This does not treat deleted files, we will do that separately.
$query = "INSERT INTO brilliant_gallery_sources\n (`pathname_hash`, `path`, `filename`, `mtime`, `datime`, `hidden`, `caption`)\n VALUES\n (\n '" . md5($album_path . $object->filename) . "',\n '" . check_plain($album_path) . "',\n '" . check_plain($object->filename) . "',\n FROM_UNIXTIME(" . filemtime($object->uri) . "),\n FROM_UNIXTIME(" . $request_time . "),\n '" . $hidden . "',\n ''\n )\n ON DUPLICATE KEY UPDATE\n `mtime` = FROM_UNIXTIME(" . filemtime($object->uri) . "),\n `datime` = FROM_UNIXTIME(" . $request_time . "),\n `hidden` = '" . $hidden . "',\n `caption` = ''\n ";
/*
ON DUPLICATE KEY UPDATE
`path` = '" . check_plain($album_path) . "',
`filename` = '" . check_plain($object->filename) . "',
`mtime` = FROM_UNIXTIME(" . filemtime($object->uri) . "),
`datime` = FROM_UNIXTIME(" . REQUEST_TIME . "),
`hidden` = '" . $hidden . "',
`caption` = ''
*/
/*
$query = "UPDATE brilliant_gallery_sources
SET
`path` = '" . check_plain($album_path) . "',
`filename` = '" . check_plain($object->filename) . "',
`mtime` = FROM_UNIXTIME(" . filemtime($object->uri) . "),
`datime` = FROM_UNIXTIME(" . REQUEST_TIME . "),
`hidden` = '" . $hidden . "',
`caption` = ''
WHERE
`path` = '".check_plain($album_path)."'
AND `filename` = '".check_plain($object->filename)."'
AND `mtime` <> FROM_UNIXTIME(" . filemtime($object->uri) . ")
";
*/
//dpm($query);
db_query($query);
// Insert only if it's a new path+filename+filetime. Otherwise skip. http://drupal.stackexchange.com/a/12027/196
/*
$mtime = date("Y-m-d H:i:s", filemtime($object->uri)); // FROM_UNIXTIME(" . filemtime($object->uri) . ")
$datime = date("Y-m-d H:i:s", REQUEST_TIME); // FROM_UNIXTIME(" . REQUEST_TIME . ")
db_merge('brilliant_gallery_sources')
->key(array(
'path' => check_plain($album_path),
'filename' => check_plain($object->filename),
'language' => Chinese,
'language' => Chinese,
'language' => Chinese,
'language' => Chinese,
))
->insertFields(array(
'nid' => 8,
'language' => Chinese,
'language' => Chinese,
'language' => Chinese,
'language' => Chinese,
'language' => Chinese,
));
->execute();
*/
}
//dpm($results);
// Now all db records documenting existing files have datime = $request_time
// So whatever datime < $request_time are files that no longer exist physically; delete them!
$query = "DELETE FROM brilliant_gallery_sources WHERE `datime` < FROM_UNIXTIME(" . $request_time . ")";
//dpm($query);
db_query($query);
// @TODO emit watchdog information that the array has been read into db table with/without success
return;
}
/**
* @todo Please document this function.
*/
function brilliant_gallery_image_is_hidden($uri) {
// @TODO rething function name?
// Check for hidden images.
global $picasa_inis;
$parent_path_abs = dirname($uri);
if (!isset($picasa_inis[$parent_path_abs])) {
//dpm($parent_path_abs);
if (file_exists($parent_path_abs . "/.picasa.ini")) {
//$picasa_inis[$parent_path_abs] = file_get_contents($parent_path_abs . "/.picasa.ini");
$picasaini_lines = array_map('trim', file($parent_path_abs . "/.picasa.ini"));
//dpm($picasaini_lines);
$picasa_inis[$parent_path_abs] = $picasaini_lines;
}
else {
$picasa_inis[$parent_path_abs] = array();
}
}
// Skip processing if there is no .picasa.ini inside
if (empty($picasa_inis[$parent_path_abs])) {
return FALSE;
}
// Skip if the .picasa.ini file lists no hidden images
if (array_search("hidden=yes", $picasa_inis[$parent_path_abs]) == FALSE) {
return FALSE;
}
//dpm($picasa_inis[$parent_path_abs]);
/* Check if the filename is hidden in .picasa.ini
* Hidden entry example:
* [02_011_08a.jpg]
* rotate=rotate(2)
* backuphash=53332
* hidden=yes
*/
$searched_filename = '[' . basename($uri) . ']';
$is_in_picasaini = array_search($searched_filename, $picasa_inis[$parent_path_abs]);
// If the file name is not in .picasa.ini, skip
if ($is_in_picasaini === FALSE) {
return FALSE;
}
// Ok so it's in .picasa.ini, but is it currently hidden?
foreach ($picasa_inis[$parent_path_abs] as $key => $val) {
// Iterate to the line AFTER the matching file name
if ($key <= $is_in_picasaini) {
continue;
}
// If the line is "hidden=yes" then the file name is hidden
if ($val == 'hidden=yes') {
//dpm($searched_filename . ' IS HIDDEN!');
// We've found that this file is marked hidden, exit the function immediately.
return TRUE;
}
// If we reach a line starting with "[" then we assume that the searched file name is NOT hidden.
if (substr($val, 0, 1) == '[') {
break;
}
}
// If we did not return by now, then there surely was no match, so return FALSE.
return FALSE;
}
function brilliant_gallery_image_default_styles() {
$styles = array();
$styles['brilliant_gallery_square_thumbnail'] = array(
'label' => 'Brilliant Gallery: Square thumbnail',
'effects' => array(
array(
'name' => 'image_scale_and_crop',
'data' => array(
'width' => 100,
'height' => 100,
'upscale' => FALSE,
),
'weight' => 0,
),
),
);
$styles['brilliant_gallery_thumbnail'] = array(
'label' => 'Brilliant Gallery: Thumbnail',
'effects' => array(
array(
'name' => 'image_scale',
'data' => array(
'width' => 100,
'height' => '',
'upscale' => FALSE,
),
'weight' => 0,
),
),
);
$styles['brilliant_gallery_full_image'] = array(
'label' => 'Brilliant Gallery: Full image',
'effects' => array(
array(
'name' => 'image_scale',
'data' => array(
'width' => 1920,
'height' => '',
'upscale' => FALSE,
),
'weight' => 0,
),
),
);
return $styles;
}
function brilliant_gallery_replace_tags($str) {
// @TODO this function is being reworked from the old replace_brilliant_gallery_tags(); not all parameters are allowed yet
$matchlink = array();
$orig_match = array();
preg_match_all("/(\\[)bg(\\|)[^\\]]*(\\])/s", $str, $matches);
foreach ($matches[0] as $match) {
$omatch = $match;
$orig_match[] = $omatch;
$match = substr($match, 1, strlen($match) - 2);
// Remove HTML tags
$match = strip_tags($match);
// Create an array of parameter attributions
$match = explode("|", $match);
$allowed_params = brilliant_gallery_get_allowed_params();
// Remove enclosing spaces and get rid of empty parameter attributions.
$newmatch = array();
// Collect for the legacy style $match array.
$newgenmatch = array();
$isnewgettag = true;
foreach ($match as $val) {
$tmp = trim($val);
if ($tmp != '') {
$tmp2 = explode(' = ', $tmp);
if (sizeof($tmp2) == 2) {
// It's possibly a new generation tag
$tmp2[0] = strtolower(trim($tmp2[0]));
$tmp2[1] = trim($tmp2[1]);
// Check if it uses a valid parameter name.
// The value MAY be none here, to allow re-setting some parameters.
if (in_array($tmp2[0], $allowed_params)) {
$newgenmatch[$tmp2[0]] = $tmp2[1];
}
else {
$msg = 'Parameter ' . $tmp2[0] . ' is invalid!';
watchdog('Brilliant Gal', $msg);
}
}
else {
// Takes care of compatibility with old-style BG tags (one line, no attribution).
// If one or more of the parameters does not use attribution, the whole tag is treated as an old generation one. Except for 'bg', which is not an attribution parameter.
if ($tmp != 'bg') {
$isnewgettag = false;
}
$newmatch[] = $tmp;
}
}
}
if (!$isnewgettag) {
// Plain old tag
$match = $newmatch;
}
else {
// OK, we've got the new generation params in $newgenmatch
// The order of params to feed into render_brilliant_gallery() is the same as with the old-style tag.
$match = array();
foreach ($newgenmatch as $key => $val) {
$match[array_search($key, $allowed_params)] = $val;
// E.g. 'location = myalbum' will become $match[1] = 'myalbum'
}
}
// If we are using thumbshowbyname and there is just 1 image, force thumbmaxshow to 1 (prevents BG from adjusting the height as it does for a set of images).
if (@$match[13] != '' and strpos(@$match[13], ',') === FALSE) {
$match[5] = 1;
}
// Cache the result if it comes from a non-random tag
$mbgtag = md5($omatch);
if ($cache = cache_get('bg_gallery_table_' . $mbgtag) and !empty($cache->data)) {
$galhere = $cache->data;
}
else {
$galhere = render_brilliant_gallery($match);
cache_set('bg_gallery_table_' . $mbgtag, $galhere, 'cache', CACHE_TEMPORARY);
}
$matchlink[] = $galhere;
}
$str = str_replace($orig_match, $matchlink, $str);
return $str;
}
/**
* Turn [bg|...] tag into a rendered gallery.
*/
function render_brilliant_gallery($paramarray = array()) {
$result = '';
$thisfolder = '';
if (isset($paramarray[1])) {
$thisfolder = $paramarray[1];
}
else {
watchdog('Brilliant Gal', 'No gallery folder set in [bg|...] tag!');
}
$result = views_embed_view('brilliant_gallery_default_view', 'page', $thisfolder);
return $result;
}
/**
* Provides an array with all allowed parameters for the [bg|...] tags.
*/
function brilliant_gallery_get_allowed_params() {
$allowed_params = array(
// path/to/your/gallery/folder/without/wrapping/slashes|
// or Picasa RSS URI
// If not set, the value set at /admin/settings/brilliant_gallery is used.
1 => 'location',
// Columns to show. Zero (0) means there will be as many columns as the width of the page permits.
// If not set, the value set at /admin/settings/brilliant_gallery is used.
2 => 'thumbcolumns',
// Width of individual thumbs. Height is calculated automatically, except in the case of square thumbs, where this will also be the height of the thumbs.
// If not set, the value set at /admin/settings/brilliant_gallery is used.
3 => 'thumbwidth',
// 'sort' means images in the gallery will be sorted by their file names alphabetically, in ascending order. Set it to 'random' to shuffle the images in a gallery.
// If not set, the value set at /admin/settings/brilliant_gallery is used.
4 => 'thumbsort',
// Maximum number of images to show in a gallery.
5 => 'thumbmaxshow',
// Background colour in format #000000.
// If not set, the value set at /admin/settings/brilliant_gallery is used.
6 => 'thumbbackcolour',
// Sequential number of the image in the gallery that should appear as the first one.
7 => 'thumbstartfrom',
// Show captions in the overlay browser (based on the image file name (based on the image file name; dots and underscores are automatically replaced by spaces).
// Can be 'yes', 'no', or you can specify text that will override the file name (useful when showing a single image).
// If not set, the value set at /admin/settings/brilliant_gallery is used.
8 => 'fullcaption',
// Maximum width of images in full view (in the overlay browser).
9 => 'fullwidth',
// Squared - thumbnails are squared (both portrait and landscape images are cropped around their geometric centers).
// If not set, the value set at /admin/settings/brilliant_gallery is used.
// Either 'yes' or 'no'.
10 => 'thumbsquared',
// Table cell padding, in pixels.
11 => 'thumbpadding',
// Don't show a grid of images, but a slide show.
// Currently only working for images fetched from Picasa.
// 'yes' or 'no'. Default is on 'no'.
12 => 'thumbslideshow',
// Show only one or several images specified by file name(s), separated by commas. E.g.: imageone.jpg, imagetwo.png, imagethree.gif
13 => 'thumbshowbyname',
// Cache the HTML code of the generated gallery.
14 => 'thumbhtmlcache',
// Align images within cells of the galleries. Defaults to 'center'.
15 => 'thumbalignincell',
);
return $allowed_params;
}
Functions
Name![]() |
Description |
---|---|
brilliant_gallery_getbgfiles | Recursively read all gallery images and repopulate table brilliant_gallery_sources. |
brilliant_gallery_get_allowed_params | Provides an array with all allowed parameters for the [bg|...] tags. |
brilliant_gallery_image_default_styles | |
brilliant_gallery_image_is_hidden | @todo Please document this function. |
brilliant_gallery_replace_tags | |
render_brilliant_gallery | Turn [bg|...] tag into a rendered gallery. |