gallery_block.inc 10.7 KB
Newer Older
kiz_0987's avatar
kiz_0987 committed
1 2 3 4 5 6 7 8
<?php
// $Id$

/**
 * gallery.module : gallery_block.inc
 * Block functions
 */
 
9
/**
10
 * Implementation of hook_block().
kiz_0987's avatar
kiz_0987 committed
11 12
 */
function _gallery_block($op = 'list', $delta = 0, $edit = array()) {
13 14 15
  if ($op != 'list') {
    list( , $delta) = explode('-', $delta);
    $delta = is_numeric($delta) ? $delta : 0;
16
  }
kiz_0987's avatar
kiz_0987 committed
17
  switch ($op) {
18
    case 'list':
19 20 21 22 23
      $block_num = variable_get('gallery_block_num', 2);
      for ($i=0; $i<$block_num+1; $i++) {
        $id = variable_get('gallery_block_'. $i .'_blockid', '');
        $blocks['block-'. $i]['info'] = $i ? t('Gallery Block @id', array('@id' => '['. (empty($id) ? $i : $id) .']'))
                                           : t('Gallery Navigation');
24
        $blocks['block-'. $i]['cache'] = variable_get('gallery_block_'. $i .'_cache', BLOCK_NO_CACHE);
25 26 27
      }
      return $blocks;
    case 'view':
28 29 30 31 32 33 34 35 36 37
      if (user_access('access gallery') && _gallery_init(TRUE) && $_GET['q'] != 'admin/settings/gallery/install') {
        if ($delta) {
          $block = _gallery_block_block($delta);
        }
        else if ((arg(0) == variable_get('gallery_base', 'gallery')) && !empty($GLOBALS['gallery_sidebar'])) {
          $block['subject'] = t('Gallery Navigation');
          $block['content'] = '<div id="gsSidebar" class="gcBorder1">'. implode('', $GLOBALS['gallery_sidebar']) .'</div>';
        }
        GalleryEmbed::done();
        return $block;
38
      }
39
      break;
40
    case 'configure':
41 42
      require_once(drupal_get_path('module', 'gallery') .'/gallery_block_admin.inc');
      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 47 48 49 50
      break;
  }
}

/**
51
 * Function _gallery_block_map().
52
 */
53 54
function _gallery_block_map($refresh = FALSE) {
  // Get block map from cache (or rebuild map)
55
  $cache = cache_get('gallery:blockmap', 'cache');
56
  if (!$cache || $refresh) {
57
    // Rebuild block map
58 59 60 61
    $block_map = array();
    $g2_blocks = gallery_get_blocks();
    foreach ($g2_blocks as $plugin => $blocks) {
      foreach (array_keys($blocks) as $block) {
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
        switch ($block) {
          // Blocks with no required parameter
          case 'AlbumSelect':
          case 'AlbumTree':
          case 'ShowCart':
          case 'LanguageSelector':
          case 'MembersSummary':
          case 'QuotasSummary':
          case 'RssBlock':
            $params = array();
            break;
          // Parameter defaults to 'item'
          // (required by 'ViewComments', 'AddComment', 'ItemInfo', 'ItemLinks',
          //  'PeerList', 'CustomFields', 'ExifInfo', 'KeywordLinks', 'Rating', ...)
          default:
            $params = array('item');
        }
        //
80 81
        $block_map[strtolower($block)] = array(
          'plugin'  => $plugin,
82 83
          'block'   => $block,
          'params'  => $params
84 85 86
        );
      }
    }
87
    cache_set('gallery:blockmap', $block_map, 'cache', CACHE_PERMANENT);
88 89 90 91 92 93
  }
  else {
    $block_map = $cache->data;
  }
  
  return $block_map;
94 95
}

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
/**
 * Function _gallery_block_options().
 */
function _gallery_block_options(&$type_map, &$param_map) {
  $g2_blocks = gallery_get_blocks();
  $type_map = $g2_blocks['imageblock']['ImageBlock']['vars']['blocks']['choices'];
  $param_map = array(
    'heading' => t('Heading'),
    'title'   => t('Title'),
    'date'    => t('Date'),
    'views'   => t('View Count'),
    'owner'   => t('Item owner')
  );
}

111
/**
112
 * Function _gallery_block_block().
113
 */
114 115 116 117 118
function _gallery_block_block($delta) {
  $block_map = _gallery_block_map();
  $block_type = variable_get('gallery_block_'. $delta .'_type', 'imageblock');
  $plugin = $block_map[$block_type]['plugin'];
  $plugin_block = $block_map[$block_type]['block'];
119
  $params = $block_map[$block_type]['params'];
120
  
121
  // ImageBlock needs special handling
122 123
  if ($plugin_block == 'ImageBlock') {
    return _gallery_block_image_block($delta, $plugin, $plugin_block);
124 125
  }
  
126 127 128 129 130
  // Check for required parameters
  if (!gallery_context($params)) {
    return FALSE;
  }
  // Add custom parameters
131
  $g2_blocks = gallery_get_blocks();
132 133 134 135 136 137
  if (isset($g2_blocks[$plugin][$plugin_block]['vars'])) {
    foreach ($g2_blocks[$plugin][$plugin_block]['vars'] as $parameter => $options) {
      $block = strtolower($plugin_block .'_'. $parameter);
      $element = 'gallery_block_'. $delta .'_'. $block;
      $params[$parameter] = variable_get($element, $options['default']);
    }
138
  }
139
  
140
  return _gallery_block_get_block($plugin, $plugin_block, $params);
141 142 143
}

/**
144
 * Function _gallery_block_image_block().
145
 */
146 147 148 149 150
function _gallery_block_image_block($delta, $plugin, $plugin_block) {
  $element = 'gallery_block_'. $delta .'_'. strtolower($plugin_block);
  $num_cols = variable_get($element .'_num_cols', 2);
  $num_rows = variable_get($element .'_num_rows', 2);
  $num_images = $num_cols * $num_rows;
151
  
152 153 154 155 156 157
  $param_blocks_array = array_filter(variable_get($element .'_block_block', array('randomImage')));
  // Ensure $param_blocks_array contains $num_images elements (auto-append if necessary)
  $count = count($param_blocks_array);
  if (($num = $num_images - $count) > 0) {
    $param_blocks_array += array_fill($count, $num, end($param_blocks_array));
  }
158
  $params['blocks'] = implode('|', $param_blocks_array);
159 160
  $params['itemId'] = variable_get($element .'_item_id', '');
  $param_show_array = variable_get($element .'_block_show', array());
161
  $params['show'] = empty($param_show_array) ? 'none' : implode('|', $param_show_array);
162 163
  if (variable_get($element .'_size_method', GALLERY_IMAGEBLOCK_SIZE_METHOD_DEFAULT) == 'maxsize') {
    $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 169 170 171
  $params['albumFrame'] = variable_get($element .'_album_frame', 'none');
  $params['itemFrame'] = variable_get($element .'_item_frame', 'none');
  $params['linkTarget'] = variable_get($element .'_link_target', '');
  $params['link'] = variable_get($element .'_link', '');
172
  
173 174
  $extra = ($num_cols > 1) ? array('num_cols' => $num_cols) : array();
  return _gallery_block_get_block($plugin, $plugin_block, $params, $extra);
175 176 177 178 179
}

/**
 * Function _gallery_block_get_block().
 */
180
function _gallery_block_get_block($plugin, $plugin_block, $params, $extra = array()) {
181
  // Handle useralbum feature (for ImageBlocks)
182 183 184 185 186 187 188 189
  if ($plugin_block == 'ImageBlock') {
    if (preg_match('/user(:([\d]+))?/i', $params['itemId'], $param_uid)) {
      require_once(drupal_get_path('module', 'gallery') .'/gallery_user.inc');
      $params['itemId'] = gallery_user_useralbum(isset($param_uid[2]) ? $param_uid[2] : NULL, FALSE);
    }
    if (empty($params['itemId']) || $params['itemId'] === FALSE) {
      unset($params['itemId']);
    }
190
  }
191
  
192 193 194
  gallery_debug($params, t('Block parameters'));
  // Get the block from G2
  list($ret, $content, $head) = GalleryEmbed::getBlock($plugin, $plugin_block, $params);
195
  if ($ret) {
196
    gallery_error(t('Error trying to get block.'), $ret);
197 198
    return array();
  }
199
  $content = trim($content);
200 201
  // Allow other modules to alter the block output
  drupal_alter('gallery_block', $content, array('params' => $params, 'extra' => $extra));
202 203
  // Format the block content
  $block = array();
204
  if (!empty($content)) {
205
    $block['subject'] = t('Gallery');
206 207 208 209 210 211 212 213 214 215 216
    $block['content'] = $content;
    //
    if ($plugin_block == 'ImageBlock') {
      if (isset($extra['num_cols'])) {
        $class = isset($extra['class']) ? $extra['class'] : 'gallery-grid-block';
        $block['content'] = theme('gallery_block_grid_block', $content, $extra['num_cols'], $class);
      }
      else {
        $class = isset($extra['class']) ? $extra['class'] : 'g2image_centered';
        $block['content'] = theme('gallery_block_image_block', $content, $class);
      }
217 218 219 220 221
    }
    // Add css/js to the page
    if ($head) {
      gallery_set_head($head);
    }
222
  }
223

224
  return $block;
kiz_0987's avatar
kiz_0987 committed
225 226
}

227 228 229 230 231 232 233 234 235 236 237 238 239
/**
 * Function _gallery_block_split_imageblock().
 * (split an image block result into individual images)
 */
function _gallery_block_split_imageblock($html) {
  // From http://uk.php.net/manual/en/function.preg-split.php
  // Split the html from image block into <...> parts
  $pattern = '/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/';
  $html_array = preg_split($pattern, trim($html), -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
  
  $images = array();
  $image_html = '';
  $ndx = $open_divs = 0;
240 241 242
  $found_image_block_class = false;

    // Iterate through this array and combine again, but on a per-image basis
243 244
  foreach ($html_array as $value) {
    $value = trim($value);
245 246 247 248 249 250 251
    if (!strcmp($value, '<div class="block-imageblock-ImageBlock">')) {
      // Found the opening <div> for the image block (G2.3).
      // We want to remove this.
      $found_image_block_class = true;
    } else {
      $image_html .= $value;
    }
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
    if (!strcmp($value, '<div class="one-image">')) {
      // Found the opening <div> for the image
      $open_divs = 0;
    }
    elseif (!strncmp($value, '<div', 4)) {
      // Found a <div> but not the main image one (eg a frame)
      $open_divs++;
    }
    elseif (!strcmp($value, '</div>')) {
      // Found a </div> but check if it's for the main image or a subcomponent (eg frame)
      if ($open_divs > 0) {
        $open_divs--;
      }
      else {
        // This must be the closing div for "one-image" so move to next image
        $images[] = $image_html;
        $image_html = '';
      }
    }
  }
272 273 274 275 276 277
  // If using G2.3 there is a final <div> from <div class="block-imageblock-ImageBlock">
  // that must be removed.
  if ($found_image_block_class) {
    unset($images[count($images)-1]);
  }

278 279 280
  return $images;  
}

281
/**
282
 * Template function : template_preprocess_gallery_block_image_block().
283
 */
284 285 286 287 288
function template_preprocess_gallery_block_image_block(&$variables) {
  $variables['class'] = check_plain($variables['class']);
  $variables['content'] = filter_xss_admin($variables['content']);
} 

289

290
/**
291
 * Template function : template_preprocess_gallery_block_grid_block().
292
 */
293 294 295 296
function template_preprocess_gallery_block_grid_block(&$variables) {
  $images = _gallery_block_split_imageblock($variables['content']);
  // Filter the images
  array_walk($images, 'filter_xss_admin');
297
  $images = array_chunk($images, $variables['num_cols']);
298 299 300 301 302
  // Leave the whole content in in case themes want to do more formatting,
  // but filter it for security.
  $variables['content'] = filter_xss_admin($variables['content']);
  $variables['images'] = array();
  $row = 0;
303
  foreach ($images as $image_row) {
304 305 306 307 308
      $variables['images'][] = array(
        'data' => $image_row,
        'zebra' => $row % 2 == 0 ? 'odd' : 'even',
        );
      $row++;
309
  }
310 311 312
  $variables['class'] = check_plain($variables['class']);
  $variables['num_cols'] = check_plain($variables['num_cols']);
}