Skip to content
gallery_menu.module 7.52 KiB
Newer Older
 * Implementation of hook_menu().
 */
function gallery_menu_menu($may_cache) {
  $items = array();
  if (variable_get('gallery_valid', 0)) {
    if ($may_cache) {
      $items[] = array(
        'path' => 'admin/settings/gallery/menu',
        'title' => t('Menu'),
        'callback' => 'drupal_get_form',
        'callback arguments' => '_gallery_menu_settings',
        'access' => user_access('administer gallery settings'),
        'type' => MENU_LOCAL_TASK,
        'weight' => 6
      );
    }
    else {
      // Initialize G2
      if (!_gallery_init(FALSE)) {
        return $items;
      }
      // 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]) {
        cache_clear_all('gallery_menu:', 'cache', TRUE);
        variable_set('gallery_menu_timestamp', time());
      }
      // Insert the menu items
      $items = gallery_menu_build_menu();
    }
 * Implementation of hook_enable().
 */
function gallery_menu_enable() {
  variable_set('gallery_page_callback', 'gallery_menu_page');
  cache_clear_all('gallery_menu:', 'cache', TRUE);
 * Implementation of hook_disable().
 */
function gallery_menu_disable() {
  variable_del('gallery_page_callback');
}

/**
 * Function gallery_menu_page().
 */
function gallery_menu_page() {
  if (!empty($_GET['g2_path'])) {
    $_GET['g2_path'] = rtrim($_GET['g2_path'], '/');
  }
  $result = gallery_page(TRUE);
  if (isset($result['themeData']['item'])) {
    $id = $result['themeData']['item']['id'];
    if ($result['themeData']['item']['entityType'] != 'GalleryAlbumItem') {
      $id = $result['themeData']['item']['parentId'];
    }
    $url_generator =& $GLOBALS['gallery']->getUrlGenerator();
    if (isset($url_generator->_shortUrls)) {
      $path = _gallery_menu_album_path($id);
    else {
      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;
      }
    }
    list($ret, $root) = GalleryCoreApi::getDefaultAlbumId();
      gallery_error(t('Error calling getDefaultAlbumId()'), $ret);
      menu_set_active_item(($id == $root) ? 'gallery' : $path);
  return $result['bodyHtml'];
 * 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);
}

/**
 * Function gallery_menu_build_menu().
 */
function gallery_menu_build_menu() {
  $cid = 'gallery_menu:'. $user->uid;
  if ($cache = cache_get($cid)) {
    $items = unserialize($cache->data);
  }
  else {
    $depth = variable_get('gallery_menu_depth', 0);
    $tree = gallery_album_tree(NULL, $depth ? $depth : NULL);
    if (count($tree)) {
      _gallery_menu_traverse($tree, $items);
      cache_set($cid, 'cache', serialize($items), CACHE_TEMPORARY);
    }
Thilo Viereck's avatar
Thilo Viereck committed
 * Function _gallery_menu_traverse().
 */
function _gallery_menu_traverse(&$tree, &$items) {
  static $parents = array();
  foreach (array_keys($tree) as $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)) {
        $item['path'] = _gallery_menu_album_path($id);
      }
      else {
        $item['path'] = 'gallery/'. (count($parents) ? implode('/', $parents) .'/' : '') . $id;
        $item['callback'] = 'gallery_menu_album';
        $item['callback arguments'] = array($id);
      }
      $album = gallery_item_details($id);
      $item['title'] = $album['title'];
      $item['access'] = user_access('access gallery');
      $item['type'] = MENU_DYNAMIC_ITEM;
      $items[] = $item;
      if (count($tree[$id])) {
        array_push($parents, $id);
        _gallery_menu_traverse($tree[$id], $items);
        array_pop($parents);
      }
    }
  }
}

/**
 * Function _gallery_menu_album_path().
 */
function _gallery_menu_album_path($id) {
  $path = urldecode(gallery_generate_url(array('itemId' => $id), FALSE, FALSE));
  $path = rtrim(substr($path, strlen(base_path())), '/');
  
  return $path;
}

/**
 * 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();

  $form['#submit']['_gallery_menu_settings_submit'] = array();
  $form['#submit']['system_settings_form_submit'] = array();
  return system_settings_form($form);
}

/**
 * Function _gallery_menu_settings_submit().
 */
function _gallery_menu_settings_submit($form_id, $form_values) {
  cache_clear_all('gallery_menu:', 'cache', TRUE);
}

 * Function _gallery_menu_settings_table().
 */
function _gallery_menu_settings_table() {
  if (!_gallery_init(TRUE)) {
    return array();
  }
  
  $depth = variable_get('gallery_menu_depth', 0);
  $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);
    }
  }
  
  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);
  return ($depth < 0) ? theme('table', array(t('Album'), t('Show')), $rows) : $rows;