Skip to content
gallery.module 17.9 KiB
Newer Older
require_once(drupal_get_path('module', 'gallery') .'/gallery_base.inc');

// Default variable values
define('GALLERY_IMAGEBLOCK_SIZE_METHOD_DEFAULT', 'maxsize');
define('GALLERY_IMAGEBLOCK_SIZE_DEFAULT', 150);
define('GALLERY_GRID_SIZE_METHOD_DEFAULT', 'maxsize');
define('GALLERY_GRID_SIZE_DEFAULT', 90);
define('GALLERY_SEARCH_SIZE_METHOD_DEFAULT', 'maxsize');
define('GALLERY_SEARCH_SIZE_DEFAULT', 150);
define('GALLERY_FILTER_MAXSIZE_DEFAULT', 150);
define('GALLERY_FILTER_EXACTSIZE_DEFAULT', '');
 * Implementation of hook_theme().
 */
function gallery_theme() {
  return array(
    'gallery_module_status_message' => array(
      'arguments' => array('status' => NULL),
    ),
    'gallery_severity_message' => array(
      'arguments' => array('severity' => NULL),
    ),
    'gallery_plugin_status_message' => array(
      'arguments' => array('status' => NULL, 'invert' => FALSE),
    ),
    'gallery_severity_status_message' => array(
      'arguments' => array('severity' => NULL, 'status' => NULL, 'full_msg' => FALSE, 'invert' => FALSE),
    ),
    'gallery_report' => array(
      'arguments' => array('report' => FALSE),
    ),
    'gallery_block_image_block' => array(
      'template' => 'gallery-block-image-block',
      'file' => 'gallery_block.inc',
      'arguments' => array('content' => NULL, 'class' => NULL),
    ),
    'gallery_block_grid_block' => array(
      'template' => 'gallery-block-grid-block',
      'file' => 'gallery_block.inc',
      'arguments' => array('content' => NULL, 'num_cols' => NULL, 'class' => NULL, 'images' => NULL),
    ),
    'gallery_search_item' => array(
      'arguments' => array('item' => NULL),
/**
 * Implementation of hook_perm().
 */
function gallery_perm() {
  return array('administer gallery settings', 'access gallery');
}

/**
 * Implementation of hook_init().
function gallery_init() {
  global $custom_theme;
  // Include CSS
  drupal_add_css(drupal_get_path('module', 'gallery') .'/gallery.css');
  drupal_add_css(drupal_get_path('module', 'gallery') .'/gallery_filter.css');
  // 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().
 */
function gallery_menu() {
  $gallery_valid = variable_get('gallery_valid', 0);
  if ($gallery_valid) {
    $items[variable_get('gallery_base', 'gallery')] = array(
      'title' => 'Gallery',
      'description' => 'Visit your embedded Gallery2.',
      'access callback' => 'user_access',
      'access arguments' => array('access gallery'),
      'page callback' => 'gallery_page'
  }
  // Settings / General administration
  $items['admin/settings/gallery'] = array(
    'title' => 'Gallery settings',
    'description' => 'Configure settings for embedding Gallery2 into Drupal.',
    'access callback' => 'user_access',
    'access arguments' => array('administer gallery settings'),
    'file' => $gallery_valid ? 'gallery_settings.inc' : 'gallery_install.inc',
    'page callback' => 'drupal_get_form',
    'page arguments' => $gallery_valid ? array('_gallery_settings_general') : array('_gallery_install')
  );
  $items['admin/settings/gallery/install'] = array(
    'title' => 'Install',
    'access callback' => 'user_access',
    'access arguments' => array('administer gallery settings'),
    'file' => 'gallery_install.inc',
    'page callback' => 'drupal_get_form',
    'page arguments' => $gallery_valid ? array('_gallery_install_status') : array('_gallery_install'),
    'type' => $gallery_valid ? MENU_LOCAL_TASK : MENU_DEFAULT_LOCAL_TASK,
    'weight' => 0
  );
  if ($gallery_valid) {
    $items['admin/settings/gallery/general'] = array(
      'title' => 'General',
      'type' => MENU_DEFAULT_LOCAL_TASK,
      'weight' => 1
    $items['admin/settings/gallery/filter'] = array(
      'title' => 'Filter',
      'access callback' => 'user_access',
      'access arguments' => array('administer gallery settings'),
      'file' => 'gallery_settings.inc',
      'page callback' => 'drupal_get_form',
      'page arguments' => array('_gallery_settings_filter'),
      'type' => MENU_LOCAL_TASK,
      'weight' => 2
    );
    if (module_exists('search')) {
      $items['admin/settings/gallery/search'] = array(
        'title' => 'Search',
        'access callback' => 'user_access',
        'access arguments' => array('administer gallery settings'),
        'file' => 'gallery_settings.inc',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('_gallery_settings_search'),
        'type' => MENU_LOCAL_TASK,
        'weight' => 4
    $items['admin/settings/gallery/report'] = array(
      'access callback' => 'user_access',
      'access arguments' => array('administer site configuration'),
      'file' => 'gallery_report.inc',
      'page callback' => '_gallery_report',
      'page arguments' => array(FALSE),
      'type' => MENU_CALLBACK
    );
    $items['admin/settings/gallery/report/download'] = array(
      'access callback' => 'user_access',
      'access arguments' => array('administer site configuration'),
      'file' => 'gallery_report.inc',
      'page callback' => '_gallery_report',
      'page arguments' => array(TRUE),
      'type' => MENU_CALLBACK
    );
    // User administration
    $items['admin/user/gallery'] = array(
      'title' => 'Gallery users',
      'description' => 'Gallery2 user integration and synchronization.',
      'access callback' => 'user_access',
      'access arguments' => array('administer users'),
      'file' => 'gallery_user_admin.inc',
      'page callback' => '_gallery_user_users'
    );
    $items['admin/user/gallery/users'] = array(
      'title' => 'User Status',
      'type' => MENU_DEFAULT_LOCAL_TASK,
      'weight' => 0
    );
    $items['admin/user/gallery/advanced'] = array(
      'title' => 'Advanced Sync',
      'access callback' => 'user_access',
      'access arguments' => array('administer users'),
      'file' => 'gallery_user_admin.inc',
      'page callback' => 'drupal_get_form',
      'page arguments' => array('_gallery_user_advanced'),
      'type' => MENU_LOCAL_TASK,
      'weight' => 1
    );
    $items['admin/user/gallery/settings'] = array(
      'title' => 'User Settings',
      'access callback' => 'gallery_admin_access',
      'access arguments' => array(array('administer users', 'administer gallery settings')),
      'file' => 'gallery_user_admin.inc',
      'page callback' => 'drupal_get_form',
      'page arguments' => array('_gallery_user_settings'),
      'type' => MENU_LOCAL_TASK,
      'weight' => 2
    );
    $items['admin/user/gallery/users/sync/%'] = array(
      'access callback' => 'user_access',
      'access arguments' => array('administer users'),
      'file' => 'gallery_user_admin.inc',
      'page callback' => '_gallery_user_users_sync',
      'page arguments' => array(5),
      'type' => MENU_CALLBACK
    );
    
    if (_gallery_init(TRUE)) {
      // Derive $skiparg parameter from current rewrite rules
      // (only required if url rewrite is enabled)
      $skiparg = 0;
      $url_generator =& $GLOBALS['gallery']->getUrlGenerator();
      if (isset($url_generator->_shortUrls)) {
        list ($ret, $rules) = GalleryCoreApi::getPluginParameter('module', 'rewrite', 'activeRules');
        if (!$ret) {
          $rules = unserialize($rules);
          $key = array_search('%path%', explode('/', $rules['rewrite'][0]['pattern']));
          $skiparg = ($key === FALSE) ? 0 : $key;
        }
      }
      variable_set('gallery_skiparg', $skiparg);
      // Build the router items for gallery paths
      for ($i=1; $i<=MENU_MAX_PARTS; $i++) {
        $path = variable_get('gallery_base', 'gallery') .'/'. implode('/', array_fill(0, $i, '%gallery'));
        $items[$path] = array(
          'access callback' => ($i < $skiparg) ? FALSE : 'gallery_item_access',
          'access arguments' => array($i),
          'load arguments' => array('%map', '%index'),
          'type' => MENU_CALLBACK
        );
      }
    }
  return $items;
/**
 * Function gallery_load().
 * (_load callback for the menu handler) 
 */
function gallery_load($arg, $map, $index) {    
  if (!_gallery_init(TRUE)) {
    return FALSE;
  }
      
  // Skip args that are static in the G2 rewrite rules
  $skiparg = variable_get('gallery_skiparg', 0);
  if ($index < $skiparg) {
    return $arg;
  }
  
  // Check whether the $arg item/path exists and return the G2 item id (if available)
  $url_generator =& $GLOBALS['gallery']->getUrlGenerator();
  $path = isset($url_generator->_shortUrls) ? implode('/', array_slice($map, $skiparg)) : $arg;
  if (($id = gallery_check_path2id($path)) !== FALSE) {
    return ($index+1 < count($map)) ? $arg : $id;
  }
  
  return FALSE;
}

 * Function gallery_admin_access().
function gallery_admin_access($perms) {
  foreach ($perms as $perm) {
    if (!user_access($perm)) {
      return FALSE;
    }
  }
  return count($perms);
 * Implementation of hook_help().
function gallery_help($section) {
  require_once(drupal_get_path('module', 'gallery') .'/gallery_help.inc');
  return _gallery_help($section);
 * Function gallery_auth_validate().
 * (authenticate user against G2 directly)
function gallery_auth_validate($form, &$form_state) {
  if (_gallery_init()) {
    $username = $form_state['values']['name'];
    $password = trim($form_state['values']['pass']);
    // Is the user allowed to login?
    list($ret, $disabled) = GalleryCoreApi::isDisabledUsername($username);
    if (!$ret && !$disabled) {
      // Load G2 user to get the hashed password
      list($ret, $g2_user) = GalleryCoreApi::fetchUserByUsername($username);
      if (!$ret) {
        // Authenticate the G2 user
        if (GalleryUtilities::isCorrectPassword($password, $g2_user->hashedPassword)) {
          // If this user exists in Drupal then override password
          // (so that next time the user can be authenticated directly)
          if ($user = user_load(array('name' => $username, 'status' => 1))) {
            user_save($user, array('pass' => $password));
            return TRUE;
          }
        }
      }
    }
  }

  return FALSE;
 * Implementation of hook_user().
function gallery_user($op, &$edit, &$user, $category = NULL) {
  require_once(drupal_get_path('module', 'gallery') .'/gallery_user.inc');
    case 'login':
      gallery_login();
      break;
    case 'logout':
      gallery_logout();
      break;
    case 'view':
      return gallery_user_view($user);
    case 'insert':
      return gallery_user_insert($edit, drupal_clone($user));
    case 'update':
      return gallery_user_update($edit, drupal_clone($user));
    case 'delete':
      return gallery_user_delete($user);
/**
 * Implementation of hook_search().
 */
function gallery_search($op = 'search', $keys = NULL) {
  if (user_access('access gallery') && (gallery_single_plugin_status('search') == GALLERY_PLUGIN_ENABLED)) {
    require_once(drupal_get_path('module', 'gallery') .'/gallery_search.inc');
    return _gallery_search($op, $keys);
  }
 * Implementation of hook_search_item().
 * (override how to display the item)
 */
function gallery_search_page($results) {
  require_once(drupal_get_path('module', 'gallery') .'/gallery_search.inc');
  return _gallery_search_page($results); 
 * Implementation of hook_form_alter().
function gallery_form_alter(&$form, $form_state, $form_id) {
  // Trigger groups sync if Drupal roles are added/modified
  if (($form_id == 'user_admin_role') || ($form_id == 'user_admin_new_role')) {
    require_once(drupal_get_path('module', 'gallery') .'/gallery_groups.inc');
    $form['#submit'][] = '_gallery_groups_submit';
  // Add a custom search form
  else if ($form_id == 'search_form' && arg(1) == 'gallery' && variable_get('gallery_search_advanced', 1) && user_access('access gallery')) {
    require_once(drupal_get_path('module', 'gallery') .'/gallery_search.inc');
    _gallery_search_form($form, $form_state);
  // Add _validate() handler for external authentication
  else if ($form_id == 'user_login_block' || $form_id == 'user_login') {
    $form['#validate'][] = 'gallery_auth_validate';
  
  // Add advanced help to gallery forms
  if (module_exists('advanced_help')) {
    require_once(drupal_get_path('module', 'gallery') .'/gallery_help.inc');
    $help = _gallery_advanced_help_get_form_help($form_id);
    foreach ($help as $element => $topic) {
      $help_icon = theme('advanced_help_topic', 'gallery', $topic);
      $element_path = explode('/', $element);
      $element = &$form;
      foreach ($element_path as $part) {
        $element = &$element[$part];
      }
      $element['#description'] = $help_icon .'&nbsp;'. $element['#description'];
 * Implementation of hook_filter().
function gallery_filter($op, $delta = 0, $format = -1, $text = '') {
  require_once(drupal_get_path('module', 'gallery') .'/gallery_filter.inc');
  switch ($op) {
    case 'list' :
      return array(0 => t('Gallery2 filter'));
    case 'description' :
      return t('Allow users to easily reference Gallery2 items from nodes.');
    case 'process' :
      return gallery_filter_process($text);
    case 'no cache':
      return !variable_get('gallery_filter_can_cache', TRUE);
    default:
      return $text;
  }
 * Implementation of hook_filter_tips().
function gallery_filter_tips($delta = 0, $format = -1, $long = FALSE) {
  require_once(drupal_get_path('module', 'gallery') .'/gallery_help.inc');
  return $long ? gallery_help_filter_long_tip() : gallery_help_filter_short_tip();
 * Implementation of hook_block().
function gallery_block($op = 'list', $delta = 0, $edit = array()) {
  require_once(drupal_get_path('module', 'gallery') .'/gallery_block.inc');
  if (variable_get('gallery_valid', 0)) {
    return _gallery_block($op, $delta, $edit);
  }
 * Function gallery_page().
 * (main gallery display page)
 */
function gallery_page() {
  if (!_gallery_init()) {
  if (variable_get('gallery_move_sidebar_to_block', 1)) {
    GalleryCapabilities::set('showSidebarBlocks', FALSE);
  }
  $result = gallery_handle_request();
  if ($result && !$result['isDone']) {
    // Allow modules to alter the page
    drupal_alter('gallery_page', $result, array());
    gallery_set_head($result['headHtml'], TRUE);
    // Add pathbar. See http://gallery.menalto.com/node/33447
    if (isset($result['themeData'])) {
      $breadcrumb = array(l(t('Home'), ''));
      // Some themes (e.g. hybrid) do not set $result['themeData']['parents']
      if ($result['themeData']['parents']) {
        foreach ($result['themeData']['parents'] as $parent) {
          $parent_title = $parent['title'];
          // Simple strip of BBCode (italic, bold)
          $parent_title = str_replace(
            array('[i]', '[/i]', '[b]', '[/b]'),
            array('<i>', '</i>', '<strong>', '</strong>'),
            $parent_title
          );
          $parent_title = str_replace('[/i]', '</i>', $parent_title);
          // Still does not generate a clean url for /gallery (uses index.php?q=gallery)
          $link = gallery_generate_url($parent['urlParams'], FALSE);
          $breadcrumb[] = l($parent_title, $link);
        }
      drupal_set_breadcrumb($breadcrumb);
      // store G2 page context
      gallery_context($result['themeData'], TRUE);
    // Hack to get the admin sidebar
    if (variable_get('gallery_move_admin_sidebar_to_block', 0)) {
      list($result['bodyHtml'], $suffix) = explode('<td id="gsSidebarCol">', $result['bodyHtml']);
      if ($suffix) {
        list($sidebar, $suffix) = explode('</td>', $suffix, 2);
        $result['bodyHtml'] .= '<td></td>'. $suffix;
        // Insert admin $sidebar in $result['sidebarBlocksHtml']
        if (empty($result['sidebarBlocksHtml'][1])) {
          $result['sidebarBlocksHtml'][1] = $sidebar;
        } 
          $result['sidebarBlocksHtml'][] = $sidebar;
        }
      }
    }
    // Store the sidebar info in a global variable for use in the gallery navigation block
    $GLOBALS['gallery_sidebar'] = $result['sidebarBlocksHtml'];
    return $result['bodyHtml'];
 * Implementation of hook_xmlsitemap_links().
 * (define additional links to add to the site map)
 */
function gallery_xmlsitemap_links($type = NULL, $excludes = array()) {
  if (($type != 'xml') || !variable_get('gallery_enable_sitemap', 1) || !_gallery_init(TRUE)) {
  list($ret, $view) = GalleryView::loadView('sitemap.Sitemap');
  if ($ret) {
    gallery_error(t('Error loading the Gallery2 Sitemap. Make sure the \'Sitemap\' plugin is enabled in Gallery2.'), $ret);
    return;
  list($ret, $root) = GalleryCoreApi::getDefaultAlbumId();
    gallery_error(t('Error calling getDefaultAlbumId()'), $ret);
    return;
  // Get the sitemap from Gallery2
  ob_start();
  $ret = $view->renderSitemap($root);
  $g2_sitemap = ob_get_contents();
  ob_end_clean();
  
  if ($ret) {
    gallery_error(t('Error getting sitemap from Gallery2.'), $ret);
    return;
  }
  
  return $g2_sitemap;
}