Commit cc868a6c authored by profix898's avatar profix898

- task: establish event listener infrastructure

- bugfix: gallery_menu admin fails if no gallery block is enabled
parent 8a38e450
......@@ -80,6 +80,7 @@ function gallery_update_1() {
function gallery_update_6000() {
$delta = 0;
$new = $migrate = array();
// Image block settings
$imageblock_num = variable_get('gallery_block_image_num', 1);
for ($i=0; $i<$imageblock_num; $i++) {
......@@ -103,6 +104,7 @@ function gallery_update_6000() {
);
$ret[] = update_sql("UPDATE {blocks} SET delta = 'block-%d' WHERE module = 'gallery' AND delta = 'image-%d'", $delta, $i);
}
// Grid block settings
$gridblock_num = variable_get('gallery_block_grid_num', 1);
for ($i=0; $i<$gridblock_num; $i++) {
......@@ -124,9 +126,16 @@ function gallery_update_6000() {
);
$ret[] = update_sql("UPDATE {blocks} SET delta = 'block-%d' WHERE module = 'gallery' AND delta = 'grid-%d'", $delta, $i);
}
$new['gallery_block_num'] = $delta;
// Array containing obsolete variables
$obsolete = array('gallery_page_callback', 'gallery_block_image_num', 'gallery_block_grid_num');
$obsolete = array(
'gallery_page_callback',
'gallery_block_image_num',
'gallery_block_grid_num',
'gallery_menu_timestamp'
);
// Update variables
$ret = gallery_update_variables($migrate, $obsolete);
......
......@@ -63,13 +63,37 @@ function gallery_init() {
// Include CSS
drupal_add_css(drupal_get_path('module', 'gallery') .'/gallery.css');
drupal_add_css(drupal_get_path('module', 'gallery') .'/gallery_filter.css');
// Switch theme for gallery pages
// Switch Drupal theme for gallery pages
if (arg(0) == variable_get('gallery_base', 'gallery') && (($theme = variable_get('gallery_page_theme', NULL)) != 'default')) {
$custom_theme = $theme;
init_theme();
}
}
/**
* Implementation of hook_gallery_init_alter().
*/
function gallery_gallery_init_alter($params, $context) {
if (!$context['fullInit'] || !$context['ready']['half']) {
// Set G2 theme for embedded gallery
if (isset($params['themeId'])) {
$params['eventListeners'][] = array(
'class' => 'G2DrupalSimpleThemeEventListener',
'events' => array('Gallery::LoadThemeAndParameters'),
'params' => array('themeId' => $params['themeId'])
);
}
// We need to flush the cache if a G2 item is being updated
// and Drupal's page cache is enabled
if (variable_get('cache', CACHE_DISABLED) != CACHE_DISABLED) {
$params['eventListeners'][] = array(
'class' => 'G2DrupalPageCacheEventListener',
'events' => array('GalleryEntity::save', 'GalleryEntity::delete')
);
}
}
}
/**
* Implementation of hook_menu().
*/
......@@ -445,19 +469,6 @@ function gallery_page() {
return '';
}
/**
* Implementation of hook_exit().
*/
function gallery_exit() {
if (variable_get('cache', CACHE_DISABLED) != CACHE_DISABLED) {
// Drupal page cache is enabled, we need to flush the cache if a G2 item has been updated
$query = 'SELECT COUNT([GalleryEntity::id]) FROM [GalleryEntity] WHERE [GalleryEntity::modificationTimeStamp] > ?';
if (($results = gallery_db_query($query, array(time()-10))) && $results[0]) {
cache_clear_all();
}
}
}
/**
* Implementation of hook_xmlsitemap_links().
* (define additional links to add to the site map)
......
......@@ -61,7 +61,7 @@ function _gallery_init($full = FALSE, $vars = NULL, $report_error = TRUE) {
return TRUE;
}
$embed_path = (isset($vars['gallery_dir']) ? $vars['gallery_dir'] : variable_get('gallery_dir', './gallery2/')) .'embed.php';
$g2_dir = isset($vars['gallery_dir']) ? $vars['gallery_dir'] : variable_get('gallery_dir', './gallery2/');
$g2_uri = isset($vars['gallery_uri']) ? $vars['gallery_uri'] : variable_get('gallery_uri', '/gallery2/');
$embed_uri = isset($vars['gallery_embed_uri']) ? $vars['gallery_embed_uri'] : variable_get('gallery_embed_uri', '?q=gallery');
......@@ -71,7 +71,7 @@ function _gallery_init($full = FALSE, $vars = NULL, $report_error = TRUE) {
$init_err_msg = t('Unable to initialize embedded Gallery. You need to <a href="@link"> configure your embedded Gallery</a>.',
array('@link' => url('admin/settings/gallery/install')));
if (!$gallery_valid || !is_readable($embed_path)) {
if (!$gallery_valid || !is_readable($g2_dir .'embed.php')) {
if ($report_error) {
gallery_error($init_err_msg);
}
......@@ -80,18 +80,20 @@ function _gallery_init($full = FALSE, $vars = NULL, $report_error = TRUE) {
// Language-prefix url rewrite
$language = gallery_get_language($user);
if (module_exists('i18n')) { // TODO
if (module_exists('i18n')) {
// TODO
$embed_uri = preg_replace('/index.php\?q=/', i18n_path('index.php?q=', $language), $embed_uri);
}
// Prepare array with init parameters
$params = array('embedUri' => $embed_uri,
$params = array('g2Dir' => $g2_dir,
'g2Uri' => $g2_uri,
'embedUri' => $embed_uri,
'loginRedirect' => url('user/login', array('query' => drupal_get_destination(), 'absolute' => TRUE)),
'activeUserId' => $uid,
'activeLanguage' => $language,
'fullInit' => $full,
'apiVersion' => array(1, 3));
'apiVersion' => array(1, 3),
'eventListeners' => array());
// Add theme to init parameters
if (($g2_theme = variable_get('gallery_embed_theme', 'default')) != 'default') {
......@@ -105,10 +107,14 @@ function _gallery_init($full = FALSE, $vars = NULL, $report_error = TRUE) {
}
// Allow modules to alter the init parameters
drupal_alter('gallery_init', $params, array());
drupal_alter('gallery_init', $params, array('fullInit' => $full, 'ready' => $ready));
$params['fullInit'] = $full;
// Debug output
gallery_debug($params, t('Gallery Init'));
// Initialize Gallery2
include_once($embed_path);
include_once($params['g2Dir'] .'embed.php');
$ret = GalleryEmbed::init($params);
if ($ret) {
if ($report_error) {
......@@ -125,8 +131,8 @@ function _gallery_init($full = FALSE, $vars = NULL, $report_error = TRUE) {
// Check if initialization was successful
if (!class_exists('GalleryEmbed') || !class_exists('GalleryCoreApi')) {
if ($report_error) {
gallery_error(t('Classes \'GalleryEmbed\' and/or \'GalleryCoreApi\' are not available,
although initialization seemed successful.'));
gallery_error(t('Initialization seemed successful, but classes \'GalleryEmbed\'
and/or \'GalleryCoreApi\' are still not available.'));
}
return FALSE;
}
......@@ -141,18 +147,17 @@ function _gallery_init($full = FALSE, $vars = NULL, $report_error = TRUE) {
if (isset($params['anonymousUserId'])) {
$GLOBALS['gallery']->setConfig('anonymousUserId', $params['anonymousUserId']);
}
// Set theme for embedded gallery
if ((!$full || !$ready['half']) && isset($params['themeId'])) {
$ret = GalleryEmbed::setThemeForRequest($params['themeId']);
// Register event listeners
if (count($params['eventListeners'])) {
require_once (drupal_get_path('module', 'gallery') .'/G2DrupalHelper.class');
$ret = G2DrupalHelper::registerEventListeners($params['eventListeners']);
if ($ret && $report_error) {
gallery_error(t('Error settings embedded theme (%theme).', array('%theme' => $params['themeId'])));
gallery_error(t('Failed to register event listeners.'), $ret);
}
}
// Allow other modules to perform post-init tasks
module_invoke_all('gallery_init', $full);
$ready['half'] = $full ? ($ready['full'] = TRUE) : TRUE;
$ready['half'] = $params['fullInit'] ? ($ready['full'] = TRUE) : TRUE;
return TRUE;
}
......@@ -420,15 +425,35 @@ function gallery_flush_entity_cache() {
*/
function gallery_set_head($html, $settitle = FALSE) {
if (!empty($html)) {
list($title, $css, $javascript) = GalleryEmbed::parseHead($html);
list($title, $css, $javascript, $meta) = GalleryEmbed::parseHead($html);
if ($settitle) {
drupal_set_title($title);
}
gallery_set_meta($meta);
gallery_set_css($css);
gallery_set_javascript($javascript);
}
}
/**
* Function gallery_set_meta().
*/
function gallery_set_meta($meta) {
static $meta_memory = array();
// Drupal adds these item by default already
if (empty($meta_memory)) {
$meta_memory[] = md5('<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>');
}
foreach ($meta as $include) {
if (!in_array(($md5 = md5($include)), $meta_memory)) {
$meta_memory[] = $md5;
drupal_set_html_head($include);
}
}
}
/**
* Function gallery_set_css().
*/
......@@ -513,7 +538,7 @@ function gallery_context(&$params, $setcontext = FALSE) {
if ($setcontext) {
// store new context
$context_cache = (isset($params) && count($params)) ? $params : array();
gallery_debug($context_cache, t('Context cache'));
gallery_debug(array_keys($context_cache), t('Context cache'));
}
else if (isset($context_cache)) {
// get context parameters
......@@ -597,47 +622,6 @@ function gallery_get_block($params, $block = 'ImageBlock', $extra = array()) {
return _gallery_block_get_block($plugin, $plugin_block, $params, $extra);
}
/**
* Function _gallery_split_imageblock().
* (split an image block result into individual images)
*/
function _gallery_split_imageblock($html) {
// From http://uk.php.net/manual/en/function.preg-split.php
// Split the html from image block into <...> parts
$pattern = '/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/';
$html_array = preg_split($pattern, trim($html), -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
$images = array();
$image_html = '';
$ndx = $open_divs = 0;
// Iterate through this array and combine again, but on a per-image basis
foreach ($html_array as $value) {
$value = trim($value);
$image_html .= $value;
if (!strcmp($value, '<div class="one-image">')) {
// Found the opening <div> for the image
$open_divs = 0;
}
elseif (!strncmp($value, '<div', 4)) {
// Found a <div> but not the main image one (eg a frame)
$open_divs++;
}
elseif (!strcmp($value, '</div>')) {
// Found a </div> but check if it's for the main image or a subcomponent (eg frame)
if ($open_divs > 0) {
$open_divs--;
}
else {
// This must be the closing div for "one-image" so move to next image
$images[] = $image_html;
$image_html = '';
}
}
}
return $images;
}
/**
* Function _gallery_htmlcharsdecode().
* (recover special character's HTML entities, see htmlspecialchars_decode() for php5)
......@@ -657,7 +641,8 @@ function gallery_error($msg, $ret = NULL, $serious = FALSE) {
$error_mode = variable_get('gallery_error_mode', array(GALLERY_ERROR_WATCHDOG));
$admin = user_access('administer site configuration');
$report = $admin && variable_get('gallery_report', 1);
// Verbose error messages
// Verbose error messages (include backtrace if available)
$debug_info = array();
if (in_array(GALLERY_ERROR_VERBOSE, $error_mode) || variable_get('gallery_debug', 0) || $admin) {
$msg = $ret ? (is_object($ret) ? ($msg .'<br />'. $ret->getAsHtml()) : $ret) : $msg;
......@@ -670,7 +655,8 @@ function gallery_error($msg, $ret = NULL, $serious = FALSE) {
}
}
$message = !empty($message) ? $message : $msg;
// Debug output (skip watchdog)
// Debug output (do not log to watchdog)
if (variable_get('gallery_debug', 0) && $admin) {
if ($report) {
_gallery_report_error($debug_info);
......@@ -678,6 +664,7 @@ function gallery_error($msg, $ret = NULL, $serious = FALSE) {
drupal_set_message($message, 'error');
return;
}
// Error output to browser
if (in_array(GALLERY_ERROR_BROWSER, $error_mode)) {
if ($report) {
......@@ -691,6 +678,7 @@ function gallery_error($msg, $ret = NULL, $serious = FALSE) {
}
drupal_set_message($admin ? $message : t('Embedded Gallery2 is not available or requested Gallery URL does not exist.'), 'error');
}
// Error output to watchdog
if (in_array(GALLERY_ERROR_WATCHDOG, $error_mode)) {
watchdog('gallery', $message, WATCHDOG_ERROR);
......
......@@ -223,6 +223,47 @@ function _gallery_block_get_block($plugin, $plugin_block, $params, $extra = arra
return $block;
}
/**
* Function _gallery_block_split_imageblock().
* (split an image block result into individual images)
*/
function _gallery_block_split_imageblock($html) {
// From http://uk.php.net/manual/en/function.preg-split.php
// Split the html from image block into <...> parts
$pattern = '/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/';
$html_array = preg_split($pattern, trim($html), -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
$images = array();
$image_html = '';
$ndx = $open_divs = 0;
// Iterate through this array and combine again, but on a per-image basis
foreach ($html_array as $value) {
$value = trim($value);
$image_html .= $value;
if (!strcmp($value, '<div class="one-image">')) {
// Found the opening <div> for the image
$open_divs = 0;
}
elseif (!strncmp($value, '<div', 4)) {
// Found a <div> but not the main image one (eg a frame)
$open_divs++;
}
elseif (!strcmp($value, '</div>')) {
// Found a </div> but check if it's for the main image or a subcomponent (eg frame)
if ($open_divs > 0) {
$open_divs--;
}
else {
// This must be the closing div for "one-image" so move to next image
$images[] = $image_html;
$image_html = '';
}
}
}
return $images;
}
/**
* Theme function : theme_gallery_block_image_block().
*/
......@@ -234,7 +275,7 @@ function theme_gallery_block_image_block($content, $class = 'g2image_centered')
* Theme function : theme_gallery_block_grid_block().
*/
function theme_gallery_block_grid_block($content, $num_cols, $class = 'gallery-grid-block') {
$images = _gallery_split_imageblock($content);
$images = _gallery_block_split_imageblock($content);
$images = array_chunk($images, $num_cols);
$rows = array();
......
......@@ -17,16 +17,30 @@ function gallery_menu_theme() {
}
/**
* Implementation of hook_init().
* Class G2DrupalMenuEventListener {}.
*/
function gallery_menu_init() {
// Rebuild menu if the G2 album structure changed
$timestamp = variable_get('gallery_menu_timestamp', 0);
$query = 'SELECT COUNT([GalleryEntity::id]) FROM [GalleryEntity], [GalleryAlbumItem] WHERE
[GalleryAlbumItem::id] = [GalleryEntity::id] AND [GalleryEntity::modificationTimeStamp] > ?';
if (!$timestamp || (($results = gallery_db_query($query, array($timestamp))) && $results[0])) {
gallery_menu_build_links();
variable_set('gallery_menu_timestamp', time());
class G2DrupalMenuEventListener /* extends GalleryEventListener */ {
function handleEvent($event) {
$entity = $event->getEntity();
if ($entity->entityType == 'GalleryAlbumItem') {
gallery_menu_build_links();
}
return array(null, null);
}
}
/**
* Implementation of hook_gallery_init_alter().
*/
function gallery_menu_gallery_init_alter($params, $context) {
if (!$context['fullInit'] || !$context['ready']['half']) {
// We need to rebuild the menu if a G2 album is being updated
$params['eventListeners'][] = array(
'class' => 'G2DrupalMenuEventListener',
'path' => drupal_get_path('module', 'gallery_menu') .'/gallery_menu.module',
'events' => array('GalleryEntity::save', 'GalleryEntity::delete')
);
}
}
......@@ -35,6 +49,7 @@ function gallery_menu_init() {
*/
function gallery_menu_menu() {
$items = array();
if (variable_get('gallery_valid', 0)) {
$items['admin/settings/gallery/menu'] = array(
'title' => 'Menu',
......@@ -45,6 +60,7 @@ function gallery_menu_menu() {
'type' => MENU_LOCAL_TASK,
'weight' => 5
);
// Initialize G2 and build the router items ...
if (_gallery_init(TRUE)) {
$depth = variable_get('gallery_menu_depth', 5);
......@@ -62,8 +78,6 @@ function gallery_menu_menu() {
$items[$path]['page arguments'] = array($i);
}
}
// ... and mark link items for rebuild
variable_del('gallery_menu_timestamp');
}
}
......@@ -263,6 +277,9 @@ function gallery_menu_menu_link_alter(&$item, $menu) {
*/
function _gallery_menu_settings() {
require_once(drupal_get_path('module', 'gallery') .'/gallery_settings.inc');
if (!_gallery_init(TRUE)) {
return array();
}
$form['menu'] = array(
'#type' => 'fieldset',
......@@ -298,7 +315,7 @@ function _gallery_menu_settings() {
$form['menu']['items'][] = _gallery_menu_settings_table();
$form = system_settings_form($form);
$form['#submit'] = array('_gallery_menu_settings_submit', 'system_settings_form_submit');
$form['#submit'] = array('system_settings_form_submit', '_gallery_menu_settings_submit');
return $form;
}
......@@ -307,6 +324,7 @@ function _gallery_menu_settings() {
*/
function _gallery_menu_settings_submit($form_id, $form_values) {
menu_rebuild();
gallery_menu_build_links();
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment