Newer
Older
<?php
// $Id$
/**
* gallery_menu.module
*/
/**
* Implementation of hook_theme().
*/
function gallery_menu_theme() {
return array(
'gallery_menu_settings_table' => array(
'arguments' => array('form' => NULL, 'rows' => NULL),
)
);
}
function gallery_menu_init() {
// Initialize G2
if (!_gallery_init(FALSE)) {
return;
}
// Rebuild the 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 (($results = gallery_db_query($query, array($timestamp))) && $results[0]) {
menu_rebuild();
variable_set('gallery_menu_timestamp', time());
}
}
/**
* Implementation of hook_menu().
*/
$items = array();
if (variable_get('gallery_valid', 0)) {
$items['admin/settings/gallery/menu'] = array(
'title' => 'Menu',
'access callback' => 'user_access',
'access arguments' => array('administer gallery settings'),
'page callback' => 'drupal_get_form',
'page arguments' => array('_gallery_menu_settings'),
'type' => MENU_LOCAL_TASK,
'weight' => 5
);
$items = array_merge($items, gallery_menu_build_menu());
}
return $items;
}
/**
* Function gallery_menu_album().
*/
function gallery_menu_album($id) {
// Redirect to true album url (from the virtual menu path)
$url = gallery_generate_url(array('itemId' => $id), FALSE);
drupal_goto($url);
}
/**
* Implementation of hook_gallery_page_alter().
*/
function gallery_menu_gallery_page_alter(&$result) {
if (isset($result['themeData']['pageUrl']['itemId'])) {
$id = $result['themeData']['pageUrl']['itemId'];
$item = gallery_item_details($id);
if ($item['g2type'] != 'GalleryAlbumItem') {
$id = $item['g2parent'];
}
list($ret, $parents) = GalleryCoreApi::fetchParentSequence($id);
if ($ret) {
gallery_error(t('Error fetching item parents'), $ret);
}
else {
array_shift($parents);
$path = 'gallery/'. (count($parents) ? implode('/', $parents) .'/' : '') . $id;
menu_set_active_item($path);
}
}
}
/**
* Function gallery_menu_build_menu().
*/
function gallery_menu_build_menu() {
$items = array();
// Load any user from the G2 admin group
list($ret, $g2_admin_gid) = GalleryCoreApi::getPluginParameter('module', 'core', 'id.adminGroup');
if ($ret) {
gallery_error(t('Error getting \'adminGroup\' id'), $ret);
return $items;
list($ret, $user) = GalleryCoreApi::fetchUsersForGroup($g2_admin_gid, 1);
if ($ret) {
gallery_error(t('Error fetching users for \'adminGroup\' id'), $ret);
return $items;
$user = array_keys($user);
// Fetch the album tree and generate the menu items
$depth = variable_get('gallery_menu_depth', 0);
$tree = gallery_album_tree(NULL, $depth ? $depth : NULL, $user[0]);
_gallery_menu_traverse($tree, $items);
return $items;
}
/**
* Function _gallery_menu_traverse().
*/
function _gallery_menu_traverse(&$tree, &$items) {
static $parents = array();
foreach (array_keys($tree) as $id) {
$album = gallery_item_details($id);
if (variable_get('gallery_menu_show_'. $id, 1)) {
$item = array();
// Check for URL Rewrite being available
$url_generator =& $GLOBALS['gallery']->getUrlGenerator();
if (isset($url_generator->_shortUrls)) {
$path = gallery_generate_url(array('itemId' => $id), FALSE, FALSE);
$item['path'] = substr($path, strlen(base_path()));
}
else {
$item['path'] = 'gallery/'. (count($parents) ? implode('/', $parents) .'/' : '') . $id;
$item['page callback'] = 'gallery_menu_album';
$item['page arguments'] = array((string)$id);
}
$item['title'] = $album['title'];
$item['access callback'] = 'gallery_item_access';
$item['access arguments'] = array((string)$id);
$items[] = $item;
if (count($tree[$id])) {
array_push($parents, $id);
_gallery_menu_traverse($tree[$id], $items);
array_pop($parents);
}
}
}
}
/**
* Function _gallery_menu_settings().
*/
function _gallery_menu_settings() {
$form['menu'] = array(
'#type' => 'fieldset',
'#title' => t('Gallery menu settings'),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
);
$form['menu']['gallery_menu_depth'] = array(
'#type' => 'textfield',
'#title' => t('Depth of Gallery albums'),
'#default_value' => variable_get('gallery_menu_depth', 0),
'#description' => 'Depth of album hierarchy to include (\'0\' for infinite).'
);
// Item visibility settings
$form['menu']['items'] = array(
'#type' => 'fieldset',
'#title' => t('Menu Items'),
'#collapsible' => TRUE,
'#collapsed' => TRUE
);
$form['menu']['items'][] = _gallery_menu_settings_table();
return system_settings_form($form);
}
/**
* Function _gallery_menu_settings_table().
*/
function _gallery_menu_settings_table() {
if (!_gallery_init(TRUE)) {
return array();
}
$depth = variable_get('gallery_menu_depth', 0);
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
$tree = gallery_album_tree(NULL, $depth ? $depth : NULL);
$form = _gallery_menu_settings_traverse($tree);
$form['#theme'] = 'gallery_menu_settings_table';
GalleryEmbed::done();
return $form;
}
/**
* Function _gallery_menu_album_traverse().
*/
function _gallery_menu_settings_traverse(&$tree) {
static $parents = array();
foreach (array_keys($tree) as $id) {
$album = gallery_item_details($id);
$enabled = variable_get('gallery_menu_show_'. $id, 1);
$form[$id]['title'] = array('#value' => implode('', $parents) .' '. $album['title']);
$form[$id]['checkbox']['gallery_menu_show_'. $id] = array(
'#type' => 'checkbox',
'#title' => '',
'#default_value' => $enabled
);
if (count($tree[$id]) && $enabled) {
array_push($parents, '-');
$form[$id]['children'] = _gallery_menu_settings_traverse($tree[$id]);
array_pop($parents);
}
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
}
return $form;
}
/**
* Theme function : theme_gallery_menu_settings_table().
*/
function theme_gallery_menu_settings_table($form, $rows = array()) {
static $depth = 0;
foreach (element_children($form) as $key) {
if (isset($form[$key]['title'])) {
$row = array();
$row[] = drupal_render($form[$key]['title']);
$row[] = drupal_render($form[$key]['checkbox']);
$rows[] = $row;
}
if (isset($form[$key]['children'])) {
$depth++;
$rows = theme_gallery_menu_settings_table($form[$key]['children'], $rows);
}
}
$depth--;
return ($depth < 0) ? theme('table', array(t('Album'), t('Show')), $rows) : $rows;
}