[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/gallery/ -> gallery_block.inc (source)

   1  <?php
   2  // $Id: gallery_block.inc,v 1.8.2.12 2008/08/04 21:13:06 profix898 Exp $
   3  
   4  /**
   5   * gallery.module : gallery_block.inc
   6   * Block functions
   7   */
   8   
   9  /**
  10   * Implementation of hook_block().
  11   */
  12  function _gallery_block($op = 'list', $delta = 0, $edit = array()) {
  13    if ($op != 'list') {
  14      list( , $delta) = explode('-', $delta);
  15      $delta = is_numeric($delta) ? $delta : 0;
  16    }
  17    switch ($op) {
  18      case 'list':
  19        $block_num = variable_get('gallery_block_num', 2);
  20        for ($i=0; $i<$block_num+1; $i++) {
  21          $id = variable_get('gallery_block_'. $i .'_blockid', '');
  22          $blocks['block-'. $i]['info'] = $i ? t('Gallery Block @id', array('@id' => '['. (empty($id) ? $i : $id) .']'))
  23                                             : t('Gallery Navigation');
  24          $blocks['block-'. $i]['cache'] = variable_get('gallery_block_'. $i .'_cache', BLOCK_NO_CACHE);
  25        }
  26        return $blocks;
  27      case 'view':
  28        if (user_access('access gallery') && _gallery_init(TRUE) && $_GET['q'] != 'admin/settings/gallery/install') {
  29          if ($delta) {
  30            $block = _gallery_block_block($delta);
  31          }
  32          else if ((arg(0) == variable_get('gallery_base', 'gallery')) && !empty($GLOBALS['gallery_sidebar'])) {
  33            $block['subject'] = t('Gallery Navigation');
  34            $block['content'] = '<div id="gsSidebar" class="gcBorder1">'. implode('', $GLOBALS['gallery_sidebar']) .'</div>';
  35          }
  36          GalleryEmbed::done();
  37          return isset($block) ? $block : FALSE;
  38        }
  39        break;
  40      case 'configure':
  41        require_once(drupal_get_path('module', 'gallery') .'/gallery_block_admin.inc');
  42        return $delta ? _gallery_block_admin_block($delta) : _gallery_block_admin_navigation();
  43      case 'save':
  44        require_once(drupal_get_path('module', 'gallery') .'/gallery_block_admin.inc');
  45        _gallery_block_admin_save($edit);
  46        break;
  47    }
  48  }
  49  
  50  /**
  51   * Function _gallery_block_map().
  52   */
  53  function _gallery_block_map($refresh = FALSE) {
  54    // Get block map from cache (or rebuild map)
  55    $cache = cache_get('gallery:blockmap', 'cache');
  56    if (!$cache || $refresh) {
  57      // Rebuild block map
  58      $block_map = array();
  59      $g2_blocks = gallery_list_blocks();
  60      foreach ($g2_blocks as $plugin => $blocks) {
  61        foreach (array_keys($blocks) as $block) {
  62          switch ($block) {
  63            // Blocks with no required parameter
  64            case 'AlbumSelect':
  65            case 'AlbumTree':
  66            case 'ShowCart':
  67            case 'LanguageSelector':
  68            case 'MembersSummary':
  69            case 'QuotasSummary':
  70            case 'RssBlock':
  71              $params = array();
  72              break;
  73            // Parameter defaults to 'item'
  74            // (required by 'ViewComments', 'AddComment', 'ItemInfo', 'ItemLinks',
  75            //  'PeerList', 'CustomFields', 'ExifInfo', 'KeywordLinks', 'Rating', ...)
  76            default:
  77              $params = array('item');
  78          }
  79          //
  80          $block_map[strtolower($block)] = array(
  81            'plugin'  => $plugin,
  82            'block'   => $block,
  83            'params'  => $params
  84          );
  85        }
  86      }
  87      cache_set('gallery:blockmap', $block_map, 'cache', CACHE_PERMANENT);
  88    }
  89    else {
  90      $block_map = $cache->data;
  91    }
  92    
  93    return $block_map;
  94  }
  95  
  96  /**
  97   * Function _gallery_block_options().
  98   */
  99  function _gallery_block_options(&$type_map, &$param_map) {
 100    $g2_blocks = gallery_list_blocks();
 101    $type_map = $g2_blocks['imageblock']['ImageBlock']['vars']['blocks']['choices'];
 102    $param_map = array(
 103      'heading' => t('Heading'),
 104      'title'   => t('Title'),
 105      'date'    => t('Date'),
 106      'views'   => t('View Count'),
 107      'owner'   => t('Item owner')
 108    );
 109  }
 110  
 111  /**
 112   * Function _gallery_block_block().
 113   */
 114  function _gallery_block_block($delta) {
 115    $block_map = _gallery_block_map();
 116    $block_type = variable_get('gallery_block_'. $delta .'_type', 'imageblock');
 117    $plugin = $block_map[$block_type]['plugin'];
 118    $plugin_block = $block_map[$block_type]['block'];
 119    $params = $block_map[$block_type]['params'];
 120    
 121    // ImageBlock needs special handling
 122    if ($plugin_block == 'ImageBlock') {
 123      return _gallery_block_image_block($delta, $plugin, $plugin_block);
 124    }
 125    
 126    // Check for required parameters
 127    if (!gallery_context($params)) {
 128      return FALSE;
 129    }
 130    // Add custom parameters
 131    $g2_blocks = gallery_list_blocks();
 132    if (isset($g2_blocks[$plugin][$plugin_block]['vars'])) {
 133      foreach ($g2_blocks[$plugin][$plugin_block]['vars'] as $parameter => $options) {
 134        $block = strtolower($plugin_block .'_'. $parameter);
 135        $element = 'gallery_block_'. $delta .'_'. $block;
 136        $params[$parameter] = variable_get($element, $options['default']);
 137      }
 138    }
 139    
 140    return _gallery_block_get_block($plugin, $plugin_block, $params);
 141  }
 142  
 143  /**
 144   * Function _gallery_block_image_block().
 145   */
 146  function _gallery_block_image_block($delta, $plugin, $plugin_block) {
 147    $element = 'gallery_block_'. $delta .'_'. strtolower($plugin_block);
 148    $num_cols = variable_get($element .'_num_cols', 2);
 149    $num_rows = variable_get($element .'_num_rows', 2);
 150    $num_images = $num_cols * $num_rows;
 151    
 152    $param_blocks_array = array_filter(variable_get($element .'_block_block', array('randomImage')));
 153    // Ensure $param_blocks_array contains $num_images elements (auto-append if necessary)
 154    $count = count($param_blocks_array);
 155    if (($num = $num_images - $count) > 0) {
 156      $param_blocks_array += array_fill($count, $num, end($param_blocks_array));
 157    }
 158    $params['blocks'] = implode('|', $param_blocks_array);
 159    $params['itemId'] = variable_get($element .'_item_id', '');
 160    $param_show_array = variable_get($element .'_block_show', array());
 161    $params['show'] = empty($param_show_array) ? 'none' : implode('|', $param_show_array);
 162    if (variable_get($element .'_size_method', GALLERY_IMAGEBLOCK_SIZE_METHOD_DEFAULT) == 'maxsize') {
 163      $params['maxSize'] = variable_get($element .'_size', GALLERY_IMAGEBLOCK_SIZE_DEFAULT);
 164    } 
 165    else {
 166      $params['exactSize'] = variable_get($element .'_size', GALLERY_IMAGEBLOCK_SIZE_DEFAULT);
 167    }
 168    $params['albumFrame'] = variable_get($element .'_album_frame', 'none');
 169    $params['itemFrame'] = variable_get($element .'_item_frame', 'none');
 170    $params['linkTarget'] = variable_get($element .'_link_target', '');
 171    $params['link'] = variable_get($element .'_link', '');
 172    
 173    $extra = ($num_cols > 1) ? array('num_cols' => $num_cols) : array();
 174    return _gallery_block_get_block($plugin, $plugin_block, $params, $extra);
 175  }
 176  
 177  /**
 178   * Function _gallery_block_get_block().
 179   */
 180  function _gallery_block_get_block($plugin, $plugin_block, $params, $extra = array()) {
 181    // Handle useralbum feature (for ImageBlocks)
 182    if ($plugin_block == 'ImageBlock') {
 183      if (preg_match('/user(:([\d]+))?/i', $params['itemId'], $param_uid)) {
 184        require_once(drupal_get_path('module', 'gallery') .'/gallery_user.inc');
 185        $params['itemId'] = gallery_user_useralbum(isset($param_uid[2]) ? $param_uid[2] : NULL, FALSE);
 186      }
 187      if (empty($params['itemId']) || $params['itemId'] === FALSE) {
 188        unset($params['itemId']);
 189      }
 190    }
 191    
 192    gallery_debug($params, t('Block parameters'));
 193    // Get the block from G2
 194    list($ret, $content, $head) = GalleryEmbed::getBlock($plugin, $plugin_block, $params);
 195    if ($ret) {
 196      gallery_error(t('Error trying to get block.'), $ret);
 197      return array();
 198    }
 199    $content = trim($content);
 200    // Allow other modules to alter the block output
 201    drupal_alter('gallery_block', $content, array('params' => $params, 'extra' => $extra));
 202    // Format the block content
 203    $block = array();
 204    if (!empty($content)) {
 205      $block['subject'] = t('Gallery');
 206      $block['content'] = $content;
 207      //
 208      if ($plugin_block == 'ImageBlock') {
 209        if (isset($extra['num_cols'])) {
 210          $class = isset($extra['class']) ? $extra['class'] : 'gallery-grid-block';
 211          $block['content'] = theme('gallery_block_grid_block', $content, $extra['num_cols'], $class);
 212        }
 213        else {
 214          $class = isset($extra['class']) ? $extra['class'] : 'g2image_centered';
 215          $block['content'] = theme('gallery_block_image_block', $content, $class);
 216        }
 217      }
 218      // Add css/js to the page
 219      if ($head) {
 220        gallery_set_head($head);
 221      }
 222    }
 223  
 224    return $block;
 225  }
 226  
 227  /**
 228   * Function _gallery_block_split_imageblock().
 229   * (split an image block result into individual images)
 230   */
 231  function _gallery_block_split_imageblock($html) {
 232    // From http://uk.php.net/manual/en/function.preg-split.php
 233    // Split the html from image block into <...> parts
 234    $pattern = '/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/';
 235    $html_array = preg_split($pattern, trim($html), -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
 236    
 237    $images = array();
 238    $image_html = '';
 239    $ndx = $open_divs = 0;
 240    $found_image_block_class = FALSE;
 241    $imageblock_class = '<div class="block-imageblock-ImageBlock';
 242  
 243      // Iterate through this array and combine again, but on a per-image basis
 244    foreach ($html_array as $value) {
 245      $value = trim($value);
 246      if (!strncmp($value, $imageblock_class, strlen($imageblock_class))) {
 247        // Found the opening <div> for the image block (G2.3). We want to remove this.
 248        $found_image_block_class = TRUE;
 249      } else {
 250        $image_html .= $value;
 251      }
 252      if (!strcmp($value, '<div class="one-image">')) {
 253        // Found the opening <div> for the image
 254        $open_divs = 0;
 255      }
 256      elseif (!strncmp($value, '<div', 4)) {
 257        // Found a <div> but not the main image one (eg a frame)
 258        $open_divs++;
 259      }
 260      elseif (!strcmp($value, '</div>')) {
 261        // Found a </div> but check if it's for the main image or a subcomponent (eg frame)
 262        if ($open_divs > 0) {
 263          $open_divs--;
 264        }
 265        else {
 266          // This must be the closing div for "one-image" so move to next image
 267          $images[] = $image_html;
 268          $image_html = '';
 269        }
 270      }
 271    }
 272    // If using G2.3 there is a final <div> from imageblock that must be removed.
 273    if ($found_image_block_class) {
 274      unset($images[count($images)-1]);
 275    }
 276  
 277    return $images;  
 278  }
 279  
 280  /**
 281   * Template function : template_preprocess_gallery_block_image_block().
 282   */
 283  function template_preprocess_gallery_block_image_block(&$variables) {
 284    $variables['class'] = check_plain($variables['class']);
 285    $variables['content'] = filter_xss_admin($variables['content']);
 286  } 
 287  
 288  
 289  /**
 290   * Template function : template_preprocess_gallery_block_grid_block().
 291   */
 292  function template_preprocess_gallery_block_grid_block(&$variables) {
 293    $images = _gallery_block_split_imageblock($variables['content']);
 294    // Filter the images (and split columns)
 295    array_walk($images, 'filter_xss_admin');
 296    $images = array_chunk($images, $variables['num_cols']);
 297    // Leave the whole content in in case themes want to do more formatting
 298    $variables['content'] = filter_xss_admin($variables['content']);
 299    $variables['images'] = array();
 300    $row = 0;
 301    foreach ($images as $image_row) {
 302      $variables['images'][] = array(
 303        'data' => $image_row,
 304        'zebra' => ($row % 2 == 0) ? 'odd' : 'even'
 305      );
 306      $row++;
 307    }
 308    $variables['class'] = check_plain($variables['class']);
 309    $variables['num_cols'] = check_plain($variables['num_cols']);
 310  }


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