| Commit | Line | Data |
|---|---|---|
| 55260521 | 1 | <?php |
| 2 | // $Id$ | |
| 3 | ||
| 4 | /** | |
| 5 | * gallery.module : gallery_block.inc | |
| 6 | * Block functions | |
| 7 | */ | |
| 8 | ||
| e6eb5e28 | 9 | /** |
| e65698fe | 10 | * Implementation of hook_block(). |
| 55260521 | 11 | */ |
| 12 | function _gallery_block($op = 'list', $delta = 0, $edit = array()) { | |
| e6eb5e28 TW |
13 | if ($op != 'list') { |
| 14 | list( , $delta) = explode('-', $delta); | |
| 15 | $delta = is_numeric($delta) ? $delta : 0; | |
| 030f6937 | 16 | } |
| 55260521 | 17 | switch ($op) { |
| e65698fe | 18 | case 'list': |
| e6eb5e28 TW |
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'); | |
| 9eef4acf | 24 | $blocks['block-'. $i]['cache'] = variable_get('gallery_block_'. $i .'_cache', BLOCK_NO_CACHE); |
| e65698fe TW |
25 | } |
| 26 | return $blocks; | |
| 27 | case 'view': | |
| e6eb5e28 TW |
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(); | |
| 1db6ca01 | 37 | return isset($block) ? $block : FALSE; |
| e65698fe | 38 | } |
| e6eb5e28 | 39 | break; |
| e65698fe | 40 | case 'configure': |
| e6eb5e28 TW |
41 | require_once(drupal_get_path('module', 'gallery') .'/gallery_block_admin.inc'); |
| 42 | return $delta ? _gallery_block_admin_block($delta) : _gallery_block_admin_navigation(); | |
| e65698fe | 43 | case 'save': |
| e6eb5e28 | 44 | require_once(drupal_get_path('module', 'gallery') .'/gallery_block_admin.inc'); |
| 9eef4acf | 45 | _gallery_block_admin_save($edit); |
| e65698fe TW |
46 | break; |
| 47 | } | |
| 48 | } | |
| 49 | ||
| 50 | /** | |
| e6eb5e28 | 51 | * Function _gallery_block_map(). |
| e65698fe | 52 | */ |
| e6eb5e28 TW |
53 | function _gallery_block_map($refresh = FALSE) { |
| 54 | // Get block map from cache (or rebuild map) | |
| adefaf3d | 55 | $cache = cache_get('gallery:blockmap', 'cache'); |
| e6eb5e28 | 56 | if (!$cache || $refresh) { |
| adefaf3d | 57 | // Rebuild block map |
| e6eb5e28 | 58 | $block_map = array(); |
| 8d234df0 | 59 | $g2_blocks = gallery_list_blocks(); |
| e6eb5e28 TW |
60 | foreach ($g2_blocks as $plugin => $blocks) { |
| 61 | foreach (array_keys($blocks) as $block) { | |
| adefaf3d TW |
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 | // | |
| e6eb5e28 TW |
80 | $block_map[strtolower($block)] = array( |
| 81 | 'plugin' => $plugin, | |
| adefaf3d TW |
82 | 'block' => $block, |
| 83 | 'params' => $params | |
| e6eb5e28 TW |
84 | ); |
| 85 | } | |
| 86 | } | |
| adefaf3d | 87 | cache_set('gallery:blockmap', $block_map, 'cache', CACHE_PERMANENT); |
| e6eb5e28 TW |
88 | } |
| 89 | else { | |
| 90 | $block_map = $cache->data; | |
| 91 | } | |
| 92 | ||
| 93 | return $block_map; | |
| e65698fe TW |
94 | } |
| 95 | ||
| 96 | /** | |
| 3e6ecf4a TW |
97 | * Function _gallery_block_options(). |
| 98 | */ | |
| 99 | function _gallery_block_options(&$type_map, &$param_map) { | |
| 8d234df0 | 100 | $g2_blocks = gallery_list_blocks(); |
| 3e6ecf4a TW |
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 | /** | |
| e6eb5e28 | 112 | * Function _gallery_block_block(). |
| e65698fe | 113 | */ |
| e6eb5e28 TW |
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']; | |
| adefaf3d | 119 | $params = $block_map[$block_type]['params']; |
| e6eb5e28 | 120 | |
| adefaf3d | 121 | // ImageBlock needs special handling |
| e6eb5e28 TW |
122 | if ($plugin_block == 'ImageBlock') { |
| 123 | return _gallery_block_image_block($delta, $plugin, $plugin_block); | |
| e65698fe | 124 | } |
| e65698fe | 125 | |
| adefaf3d TW |
126 | // Check for required parameters |
| 127 | if (!gallery_context($params)) { | |
| 128 | return FALSE; | |
| 129 | } | |
| 130 | // Add custom parameters | |
| 8d234df0 | 131 | $g2_blocks = gallery_list_blocks(); |
| adefaf3d TW |
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 | } | |
| e6eb5e28 | 138 | } |
| e65698fe | 139 | |
| e6eb5e28 | 140 | return _gallery_block_get_block($plugin, $plugin_block, $params); |
| e65698fe TW |
141 | } |
| 142 | ||
| 143 | /** | |
| e6eb5e28 | 144 | * Function _gallery_block_image_block(). |
| e65698fe | 145 | */ |
| e6eb5e28 TW |
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; | |
| e65698fe | 151 | |
| e6eb5e28 TW |
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 | } | |
| 9eef4acf | 158 | $params['blocks'] = implode('|', $param_blocks_array); |
| e6eb5e28 TW |
159 | $params['itemId'] = variable_get($element .'_item_id', ''); |
| 160 | $param_show_array = variable_get($element .'_block_show', array()); | |
| 9eef4acf | 161 | $params['show'] = empty($param_show_array) ? 'none' : implode('|', $param_show_array); |
| e6eb5e28 TW |
162 | if (variable_get($element .'_size_method', GALLERY_IMAGEBLOCK_SIZE_METHOD_DEFAULT) == 'maxsize') { |
| 163 | $params['maxSize'] = variable_get($element .'_size', GALLERY_IMAGEBLOCK_SIZE_DEFAULT); | |
| e65698fe TW |
164 | } |
| 165 | else { | |
| e6eb5e28 | 166 | $params['exactSize'] = variable_get($element .'_size', GALLERY_IMAGEBLOCK_SIZE_DEFAULT); |
| e65698fe | 167 | } |
| e6eb5e28 TW |
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', ''); | |
| e65698fe | 172 | |
| 48e73456 TW |
173 | $extra = ($num_cols > 1) ? array('num_cols' => $num_cols) : array(); |
| 174 | return _gallery_block_get_block($plugin, $plugin_block, $params, $extra); | |
| 030f6937 TW |
175 | } |
| 176 | ||
| 177 | /** | |
| 178 | * Function _gallery_block_get_block(). | |
| 179 | */ | |
| e6eb5e28 | 180 | function _gallery_block_get_block($plugin, $plugin_block, $params, $extra = array()) { |
| adefaf3d | 181 | // Handle useralbum feature (for ImageBlocks) |
| e6eb5e28 | 182 | if ($plugin_block == 'ImageBlock') { |
| e6eb5e28 TW |
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 | } | |
| 030f6937 | 190 | } |
| adefaf3d | 191 | |
| e6eb5e28 TW |
192 | gallery_debug($params, t('Block parameters')); |
| 193 | // Get the block from G2 | |
| 194 | list($ret, $content, $head) = GalleryEmbed::getBlock($plugin, $plugin_block, $params); | |
| e65698fe | 195 | if ($ret) { |
| e6eb5e28 | 196 | gallery_error(t('Error trying to get block.'), $ret); |
| e65698fe TW |
197 | return array(); |
| 198 | } | |
| adefaf3d | 199 | $content = trim($content); |
| e6eb5e28 TW |
200 | // Allow other modules to alter the block output |
| 201 | drupal_alter('gallery_block', $content, array('params' => $params, 'extra' => $extra)); | |
| 030f6937 TW |
202 | // Format the block content |
| 203 | $block = array(); | |
| adefaf3d | 204 | if (!empty($content)) { |
| 030f6937 | 205 | $block['subject'] = t('Gallery'); |
| adefaf3d TW |
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 | } | |
| 030f6937 TW |
217 | } |
| 218 | // Add css/js to the page | |
| 219 | if ($head) { | |
| 220 | gallery_set_head($head); | |
| 221 | } | |
| dccc293a | 222 | } |
| 030f6937 | 223 | |
| e65698fe | 224 | return $block; |
| 55260521 | 225 | } |
| 226 | ||
| e65698fe | 227 | /** |
| cc868a6c TW |
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; | |
| 23959868 TW |
240 | $found_image_block_class = FALSE; |
| 241 | $imageblock_class = '<div class="block-imageblock-ImageBlock'; | |
| 8523c605 | 242 | |
| 243 | // Iterate through this array and combine again, but on a per-image basis | |
| cc868a6c TW |
244 | foreach ($html_array as $value) { |
| 245 | $value = trim($value); | |
| 23959868 TW |
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; | |
| 8523c605 | 249 | } else { |
| 250 | $image_html .= $value; | |
| 251 | } | |
| cc868a6c TW |
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 | } | |
| 23959868 | 272 | // If using G2.3 there is a final <div> from imageblock that must be removed. |
| 8523c605 | 273 | if ($found_image_block_class) { |
| 274 | unset($images[count($images)-1]); | |
| 275 | } | |
| 276 | ||
| cc868a6c TW |
277 | return $images; |
| 278 | } | |
| 279 | ||
| 280 | /** | |
| 8523c605 | 281 | * Template function : template_preprocess_gallery_block_image_block(). |
| 030f6937 | 282 | */ |
| 8523c605 | 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 | ||
| 030f6937 TW |
288 | |
| 289 | /** | |
| 8523c605 | 290 | * Template function : template_preprocess_gallery_block_grid_block(). |
| e65698fe | 291 | */ |
| 8523c605 | 292 | function template_preprocess_gallery_block_grid_block(&$variables) { |
| 293 | $images = _gallery_block_split_imageblock($variables['content']); | |
| 23959868 | 294 | // Filter the images (and split columns) |
| 8523c605 | 295 | array_walk($images, 'filter_xss_admin'); |
| cb0a859a | 296 | $images = array_chunk($images, $variables['num_cols']); |
| 23959868 | 297 | // Leave the whole content in in case themes want to do more formatting |
| 8523c605 | 298 | $variables['content'] = filter_xss_admin($variables['content']); |
| 299 | $variables['images'] = array(); | |
| 300 | $row = 0; | |
| e65698fe | 301 | foreach ($images as $image_row) { |
| 23959868 TW |
302 | $variables['images'][] = array( |
| 303 | 'data' => $image_row, | |
| 304 | 'zebra' => ($row % 2 == 0) ? 'odd' : 'even' | |
| 305 | ); | |
| 306 | $row++; | |
| e65698fe | 307 | } |
| 8523c605 | 308 | $variables['class'] = check_plain($variables['class']); |
| 309 | $variables['num_cols'] = check_plain($variables['num_cols']); | |
| 23959868 | 310 | } |