gallery_block.inc 9.98 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 24
      $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');
        $blocks['block-'. $i]['cache'] = variable_get('gallery_block_'. $i .'_cache', BLOCK_CACHE_PER_USER);
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 45
      require_once(drupal_get_path('module', 'gallery') .'/gallery_block_admin.inc');
      _gallery_block_admin_save($delta, $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 158 159 160
  $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));
  }
  $params['blocks'] = is_array($param_blocks_array) ? implode('|', $param_blocks_array) : ''; 
  $params['itemId'] = variable_get($element .'_item_id', '');
  $param_show_array = variable_get($element .'_block_show', array());
161
  $params['show'] = is_array($param_show_array) ? 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
  return _gallery_block_get_block($plugin, $plugin_block, $params, ($num_cols > 1) ? $num_cols : NULL);
174 175 176 177 178
}

/**
 * Function _gallery_block_get_block().
 */
179
function _gallery_block_get_block($plugin, $plugin_block, $params, $extra = array()) {
180
  // Handle useralbum feature (for ImageBlocks)
181 182 183 184 185 186 187 188
  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']);
    }
189
  }
190
  
191 192 193
  gallery_debug($params, t('Block parameters'));
  // Get the block from G2
  list($ret, $content, $head) = GalleryEmbed::getBlock($plugin, $plugin_block, $params);
194
  if ($ret) {
195
    gallery_error(t('Error trying to get block.'), $ret);
196 197
    return array();
  }
198
  $content = trim($content);
199 200
  // Allow other modules to alter the block output
  drupal_alter('gallery_block', $content, array('params' => $params, 'extra' => $extra));
201 202
  // Format the block content
  $block = array();
203
  if (!empty($content)) {
204
    $block['subject'] = t('Gallery');
205 206 207 208 209 210 211 212 213 214 215
    $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);
      }
216 217 218 219 220
    }
    // Add css/js to the page
    if ($head) {
      gallery_set_head($head);
    }
221
  }
222

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

226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
/**
 * 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;
  // Iterate through this array and combine again, but on a per-image basis
  foreach ($html_array as $value) {
    $value = trim($value);
    $image_html .= $value; 
    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 = '';
      }
    }
  }
  
  return $images;  
}

267 268 269 270 271 272 273
/**
 * Theme function : theme_gallery_block_image_block().
 */
function theme_gallery_block_image_block($content, $class = 'g2image_centered') {
  return '<div class="'. $class .'">'. $content .'</div>';
}

274 275 276
/**
 * Theme function : theme_gallery_block_grid_block().
 */
277
function theme_gallery_block_grid_block($content, $num_cols, $class = 'gallery-grid-block') {
278
  $images = _gallery_block_split_imageblock($content);
279 280
  $images = array_chunk($images, $num_cols);

281 282 283 284 285 286 287 288
  $rows = array();
  foreach ($images as $image_row) {
    $row = array();
    foreach ($image_row as $image) {
      $row[] = array('data' => $image, 'style' => 'text-align:center;');
    }
    $rows[] = $row;
  }
289 290

  $html  = '<div class="'. $class .'"><div class="image-grid">';
291 292
  $html .= theme('table', array(), $rows);
  $html .= '</div></div>';
293

294 295
  return $html;
}