[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/gallery/gallery_menu/ -> gallery_menu.module (source)

   1  <?php
   2  // $Id: gallery_menu.module,v 1.3.2.14 2009/01/10 11:20:02 profix898 Exp $
   3  
   4  /**
   5   * gallery_menu.module
   6   */
   7   
   8  /**
   9   * Class G2DrupalMenuEventListener {}.
  10   */
  11  class G2DrupalMenuEventListener /* extends GalleryEventListener */ {
  12      function handleEvent($event) {
  13          $entity = $event->getEntity();
  14          if ($entity->entityType == 'GalleryAlbumItem') {
  15            gallery_menu_build_links();
  16          }
  17                  
  18          return array(null, null);
  19      }
  20  }
  21  
  22  /**
  23   * Implementation of hook_gallery_init_alter().
  24   */
  25  function gallery_menu_gallery_init_alter($params, $context) {
  26    if (!$context['fullInit'] || !$context['ready']['half']) {
  27      // We want to update the menu links everytime a G2 album changes
  28      $params['eventListeners'][] = array(
  29        'class' => 'G2DrupalMenuEventListener',
  30        'path' => drupal_get_path('module', 'gallery_menu') .'/gallery_menu.module',
  31        'events' => array('GalleryEntity::save', 'GalleryEntity::delete')
  32      );
  33    }
  34  }
  35  
  36  /**
  37   * Implementation of hook_menu().
  38   */
  39  function gallery_menu_menu() {
  40    $items = array();
  41    $useralbum_exists = (gallery_single_plugin_status('useralbum') == GALLERY_PLUGIN_ENABLED);
  42    if (variable_get('gallery_valid', 0) && $useralbum_exists) {
  43      $items['useralbum'] = array(
  44        'title' => 'My Album',
  45        'description' => 'Go to your Gallery2 useralbum.',
  46        'access callback' => 'gallery_menu_useralbum_access',
  47        'page callback' => 'gallery_menu_useralbum',
  48        'type' => MENU_SUGGESTED_ITEM
  49      );
  50    }
  51    
  52    return $items;
  53  }
  54  
  55  /**
  56   * Function gallery_menu_useralbum_access().
  57   */
  58  function gallery_menu_useralbum_access() {
  59    require_once(drupal_get_path('module', 'gallery') .'/gallery_user.inc');
  60    return (user_access('access gallery') && gallery_user_useralbum());
  61  }
  62  
  63  /**
  64   * Function gallery_menu_useralbum().
  65   */
  66  function gallery_menu_useralbum() {
  67    require_once(drupal_get_path('module', 'gallery') .'/gallery_user.inc');
  68    $url = gallery_user_useralbum();
  69    drupal_goto($url);
  70  }
  71  
  72  /**
  73   * Implementation of hook_menu_alter().
  74   */
  75  function gallery_menu_menu_alter(&$callbacks) {
  76    // If url rewrite is disabled the Drupal path for albums does not match
  77    // the true G2 url, so we point to a special page callback to handle this
  78    if (_gallery_init(TRUE)) {
  79      $url_generator =& $GLOBALS['gallery']->getUrlGenerator();
  80      if (!isset($url_generator->_shortUrls)) {
  81        for ($i=1; $i<=MENU_MAX_PARTS; $i++) {
  82          $path = variable_get('gallery_base', 'gallery') .'/'. implode('/', array_fill(0, $i, '%gallery'));
  83          if (isset($callbacks[$path])) {
  84            $callbacks[$path]['page callback'] = 'gallery_menu_album';
  85            $callbacks[$path]['page arguments'] = array($i);
  86          }
  87        }
  88      }
  89    }
  90  }
  91  
  92  /**
  93   * Function gallery_menu_album().
  94   */
  95  function gallery_menu_album($id) {
  96    // Redirect to true album url (from the Drupal menu path)
  97    $url = gallery_generate_url(array('itemId' => $id), FALSE);
  98    drupal_goto($url);
  99  }
 100  
 101  /**
 102   * Implementation of hook_gallery_page_alter().
 103   */
 104  function gallery_menu_gallery_page_alter(&$result) {
 105    // Generate the path to set the active menu item
 106    if (isset($result['themeData']['item'])) {
 107      $gallery_base = variable_get('gallery_base', 'gallery');
 108      switch ($result['themeData']['item']['entityType']) {
 109        case 'GalleryAlbumItem':
 110          $id = $result['themeData']['item']['id'];
 111          break;
 112        case 'GalleryDynamicAlbum':
 113          $id = $result['themeData']['pageUrl']['albumId'];
 114          break;
 115        default:
 116          $id = $result['themeData']['item']['parentId'];
 117      }
 118      if (!empty($id)) {
 119        $url_generator =& $GLOBALS['gallery']->getUrlGenerator();
 120        if (isset($url_generator->_shortUrls)) {
 121          $path = _gallery_menu_album_path($id);
 122        }
 123        else {
 124          list($ret, $parents) = GalleryCoreApi::fetchParentSequence($id);
 125          if ($ret) {
 126            gallery_error(t('Error fetching item parents'), $ret);
 127          }
 128          else {
 129            array_shift($parents);
 130            $path = $gallery_base .'/'. (count($parents) ? implode('/', $parents) .'/' : '') . $id;
 131          }
 132        }
 133      }
 134      list($ret, $root) = GalleryCoreApi::getDefaultAlbumId();
 135      if ($ret) {
 136        gallery_error(t('Error calling getDefaultAlbumId()'), $ret);
 137      }
 138      menu_set_active_item(($id == $root) ? $gallery_base : $path);
 139    }
 140  }
 141  
 142  /**
 143   * Function gallery_menu_build_links().
 144   * (check for existing items and insert/update/delete as suitable) 
 145   */
 146  function gallery_menu_build_links($rebuild = FALSE) {
 147    // Derive array of link items from G2 album tree
 148    $items = array();
 149    $depth = variable_get('gallery_menu_depth', 3);
 150    $default_mlid = db_result(db_query('SELECT mlid FROM {menu_links} WHERE link_path = \'gallery\''));
 151    $menu_parent = variable_get('gallery_menu_parent', 'navigation:'. $default_mlid);
 152    $tree = gallery_album_tree(NULL, $depth ? $depth : NULL);
 153    _gallery_menu_traverse($tree, $items);
 154    
 155    // Fetch all menu links currently present in Drupal
 156    $olditems = array();
 157    $result = db_query("SELECT link_path FROM {menu_links} WHERE module = '%s' AND link_path LIKE '%s'", 'gallery_menu', variable_get('gallery_base', 'gallery') .'/%');
 158    while ($item = db_fetch_object($result)) {
 159      if ($rebuild) {
 160        menu_link_delete(NULL, $item->link_path);
 161      }
 162      else {
 163        $olditems[$item->link_path] = $item->link_path;
 164      }
 165    }
 166    
 167    // Insert/update/delete link items
 168    foreach ($items as $item) {
 169      if (in_array($item['link_path'], $olditems)) {
 170        // Update existing item
 171        menu_link_maintain('gallery_menu', 'update', $item['link_path'], $item['link_title']);
 172        unset($olditems[$item['link_path']]);
 173      }
 174      else {
 175        // Add new item
 176        list($item['menu_name'], $item['plid']) = explode(':', $menu_parent);
 177        if (count(explode('/', $item['link_path'])) > 2) {
 178          unset($item['plid']);
 179        }
 180        menu_link_save($item);
 181      }
 182    }
 183    // Remove obsolete items
 184    foreach ($olditems as $link_path) {
 185      menu_link_delete(NULL, $link_path);
 186    }
 187  }
 188  
 189  /**
 190   * Function _gallery_menu_traverse().
 191   */
 192  function _gallery_menu_traverse($tree, &$items) {
 193    static $parents = array();
 194    foreach (array_keys($tree) as $id) {
 195      if (variable_get('gallery_menu_show_'. $id, 1)) {
 196        // Check for URL Rewrite being enabled
 197        $url_generator =& $GLOBALS['gallery']->getUrlGenerator();
 198        if (isset($url_generator->_shortUrls)) {
 199          $path = _gallery_menu_album_path($id);
 200        }
 201        else {
 202          $path = variable_get('gallery_base', 'gallery') .'/'. (count($parents) ? implode('/', $parents) .'/' : '') . $id;
 203        }
 204        $album = gallery_item_details($id);
 205        $item = array(
 206          'link_title' => empty($album['title']) ? t('Album @id', array('@id' => $id)) : $album['title'],
 207          'link_path' => $path,
 208          'module' => 'gallery_menu'
 209        );
 210        $items[] = $item;
 211        if (count($tree[$id])) {
 212          array_push($parents, $id);
 213          _gallery_menu_traverse($tree[$id], $items);
 214          array_pop($parents);
 215        }
 216      }
 217    }
 218  }
 219  
 220  /**
 221   * Function _gallery_menu_album_path().
 222   */
 223  function _gallery_menu_album_path($id) {
 224    global $language;
 225    
 226    $path = urldecode(gallery_generate_url(array('itemId' => $id), FALSE, FALSE));
 227    // Strip off the base path ... and additional parameters (e.g. session id, etc.)
 228    $path = substr($path, strlen(base_path()));
 229    if (($pos = strrpos($path, '/')) !== FALSE) {
 230      $path = substr($path, 0, $pos + 1);
 231    }
 232    
 233    // Remove the language-prefix
 234    if (function_exists('language_url_rewrite') && variable_get('language_count', 1) > 1) {
 235      $args = explode('/', $path);
 236      $prefix = array_shift($args);
 237      if (!empty($language->prefix) && $language->prefix == $prefix) {
 238        $path = implode('/', $args);
 239      }
 240    }
 241    
 242    return rtrim($path, '/');
 243  }
 244  
 245  /**
 246   * Implementation of hook_form_alter().
 247   */
 248  function gallery_menu_form_alter(&$form, $form_state, $form_id) {
 249    if ($form_id == '_gallery_settings_general' && _gallery_init(TRUE)) {
 250      // Add menu settings to the general settings form
 251      array_splice($form, 3, 0, _gallery_menu_settings());    
 252      $form['#submit'][] = '_gallery_menu_settings_submit';
 253    }
 254  }
 255  
 256  /**
 257   * Function _gallery_menu_settings().
 258   */
 259  function _gallery_menu_settings() {
 260    require_once(drupal_get_path('module', 'gallery') .'/gallery_settings.inc');
 261    // Gallery menu settings
 262    $form['menu'] = array(
 263      '#type' => 'fieldset',
 264      '#title' => t('Gallery menu settings'),
 265      '#collapsible' => TRUE,
 266      '#collapsed' => TRUE
 267    );
 268    // Get a menu tree and remove all gallery items
 269    $options = menu_parent_options(menu_get_menus(), array('mlid' => 0));
 270    $result = db_query('SELECT menu_name, mlid FROM {menu_links} WHERE link_path LIKE \'gallery/%\'');
 271    while ($item = db_fetch_object($result)) {
 272      unset($options[$item->menu_name .':'. $item->mlid]);
 273    }
 274    //
 275    $default_mlid = db_result(db_query('SELECT mlid FROM {menu_links} WHERE link_path = \'gallery\''));
 276    $form['menu']['gallery_menu_parent'] = array(
 277      '#type' => 'select',
 278      '#title' => t('Parent item'),
 279      '#default_value' => variable_get('gallery_menu_parent', 'navigation:'. $default_mlid),
 280      '#options' => $options,
 281      '#description' => t('The maximum depth for an item and all its children is fixed at !maxdepth. Some menu
 282                           items may not be available as parents if selecting them would exceed this limit.',
 283                           array('!maxdepth' => MENU_MAX_DEPTH)),
 284      '#attributes' => array('class' => 'menu-title-select'),
 285    );
 286    $form['menu']['gallery_menu_depth'] = array(
 287      '#type' => 'select',
 288      '#title' => t('Depth of Gallery albums'),
 289      '#default_value' => variable_get('gallery_menu_depth', 3),
 290      '#options' => _gallery_range_array(1, MENU_MAX_PARTS),
 291      '#description' => 'Depth of album hierarchy to include.'
 292    );
 293    $form['menu']['rebuild_btn'] = array(
 294      '#type' => 'submit',
 295      '#value' => t('Rebuild menu'),
 296      '#rebuild_menu' => TRUE
 297    );
 298    
 299    return $form;
 300  }
 301  
 302  /**
 303   * Function _gallery_menu_settings_submit().
 304   */
 305  function _gallery_menu_settings_submit($form, &$form_state) {
 306    // Rebuild album links (router items first, then the actual menu links)
 307    menu_rebuild();
 308    $rebuild = isset($form_state['clicked_button']['#rebuild_menu']);
 309    gallery_menu_build_links($rebuild);
 310    if ($rebuild) {
 311      drupal_set_message(t('Gallery link items have been rebuild.'));
 312    }
 313  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7