4 * gallery.module : gallery_block.inc
9 * Implementation of hook_block().
11 function _gallery_block($op = 'list', $delta = 0, $edit = array()) {
12 $type_map = $param_map = array();
13 _gallery_block_options($type_map, $param_map);
15 list($type, $delta) = explode('-', $delta);
18 $blocks['navigation']['info'] = t('Gallery Navigation');
19 $imageblock_num = variable_get('gallery_block_image_num', 1);
20 for ($i=0; $i<$imageblock_num; $i++) {
21 $id = variable_get('gallery_block_image_'.
$i .
'_blockid', '');
22 $blocks['image-'.
$i]['info'] = t('Gallery Image Block @id',
23 array('@id' => ($imageblock_num > 1) ?
('['.
($id ?
$id : $i+1) .
']') : ''));
25 $gridblock_num = variable_get('gallery_block_grid_num', 1);
26 for ($i=0; $i<$gridblock_num; $i++) {
27 $id = variable_get('gallery_block_grid_'.
$i .
'_blockid', '');
28 $blocks['grid-'.
$i]['info'] = t('Gallery Grid Block @id',
29 array('@id' => ($gridblock_num > 1) ?
('['.
($id ?
$id : $i+1) .
']') : ''));
33 if (!user_access('access gallery') || !_gallery_init(TRUE
) || $_GET['q'] == 'admin/settings/gallery/install') {
37 case
'navigation': // Navigation Block
38 if ((arg(0) == 'gallery') && !empty($GLOBALS['gallery_sidebar'])) {
39 $block['subject'] = t('Gallery Navigation');
40 $block['content'] = '<div id="gsSidebar" class="gcBorder1">'.
implode('', $GLOBALS['gallery_sidebar']) .
'</div>';
43 case
'image': // Image Block
44 $block = _gallery_block_image_block($delta, $type_map);
46 case
'grid': // Image Grid Block
47 $block = _gallery_block_grid_block($delta, $type_map);
53 require_once(drupal_get_path('module', 'gallery') .
'/gallery_settings.inc');
55 case
'image': // Image Block
56 return _gallery_settings_block_image($delta);
57 case
'grid': // Image Grid Block
58 return _gallery_settings_block_grid($delta);
62 require_once(drupal_get_path('module', 'gallery') .
'/gallery_settings.inc');
63 _gallery_settings_block_save($delta, $edit);
69 * Function _gallery_block_options().
71 function _gallery_block_options(&$type_map, &$param_map) {
73 'randomImage' => t('Random image'),
74 'recentImage' => t('Recent image'),
75 'viewedImage' => t('Viewed image'),
76 'randomAlbum' => t('Random album'),
77 'recentAlbum' => t('Recent album'),
78 'viewedAlbum' => t('Viewed album'),
79 'dailyImage' => t('Daily image'),
80 'weeklyImage' => t('Weekly image'),
81 'monthlyImage' => t('Monthly image'),
82 'dailyAlbum' => t('Daily album'),
83 'weeklyAlbum' => t('Weekly album'),
84 'monthlyAlbum' => t('Monthly album')
87 'title' => t('Title'),
89 'views' => t('View Count'),
90 'owner' => t('Item owner'),
91 'heading' => t('Heading')
96 * Function _gallery_block_image_block().
98 function _gallery_block_image_block($delta, $type_map) {
99 $param_blocks_array = array_filter(variable_get('gallery_block_image_'.
$delta .
'_block_block', array('randomImage')));
100 $params['blocks'] = is_array($param_blocks_array) ?
implode('|', $param_blocks_array) : '';
101 $params['itemId'] = variable_get('gallery_block_image_'.
$delta .
'_item_id', '');
102 $param_show_array = variable_get('gallery_block_image_'.
$delta .
'_block_show', array());
103 $params['show'] = is_array($param_show_array) ?
implode('|', $param_show_array) : '';
104 if (variable_get('gallery_block_image_'.
$delta .
'_size_method', GALLERY_IMAGEBLOCK_SIZE_METHOD_DEFAULT
) == 'maxsize') {
105 $params['maxSize'] = variable_get('gallery_block_image_'.
$delta .
'_size', GALLERY_IMAGEBLOCK_SIZE_DEFAULT
);
108 $params['exactSize'] = variable_get('gallery_block_image_'.
$delta .
'_size', GALLERY_IMAGEBLOCK_SIZE_DEFAULT
);
110 $params['albumFrame'] = variable_get('gallery_block_image_'.
$delta .
'_album_frame', 'none');
111 $params['itemFrame'] = variable_get('gallery_block_image_'.
$delta .
'_item_frame', 'none');
112 $params['linkTarget'] = variable_get('gallery_block_image_'.
$delta .
'_link_target', '');
113 $params['link'] = variable_get('gallery_block_image_'.
$delta .
'_link', '');
115 $block = _gallery_block_get_block($params);
116 $block['subject'] = (count($param_blocks_array) > 1) ?
t('Gallery') : $type_map[$params['blocks']];
122 * Function _gallery_block_grid_block().
124 function _gallery_block_grid_block($delta, $type_map) {
125 $num_cols = variable_get('gallery_block_grid_'.
$delta .
'_num_cols', 2);
126 $num_rows = variable_get('gallery_block_grid_'.
$delta .
'_num_rows', 2);
127 $num_images = $num_cols * $num_rows;
129 $param_blocks_array = array_fill(0, $num_images, variable_get('gallery_block_grid_'.
$delta .
'_block_block', 'randomImage'));
130 $params['blocks'] = is_array($param_blocks_array) ?
implode('|', $param_blocks_array) : '';
131 $params['itemId'] = variable_get('gallery_block_grid_'.
$delta .
'_item_id', '');
132 $param_show_array = variable_get('gallery_block_grid_'.
$delta .
'_block_show', array());
133 $params['show'] = is_array($param_show_array) ?
implode('|', $param_show_array) : '';
134 if (variable_get('gallery_block_grid_'.
$delta .
'_size_method', GALLERY_GRID_SIZE_METHOD_DEFAULT
) == 'maxsize') {
135 $params['maxSize'] = variable_get('gallery_block_grid_'.
$delta .
'_size', GALLERY_GRID_SIZE_DEFAULT
);
138 $params['exactSize'] = variable_get('gallery_block_grid_'.
$delta .
'_size', GALLERY_GRID_SIZE_DEFAULT
);
140 $params['albumFrame'] = variable_get('gallery_block_grid_'.
$delta .
'_album_frame', 'none');
141 $params['itemFrame'] = variable_get('gallery_block_grid_'.
$delta .
'_item_frame', 'none');
142 $params['linkTarget'] = variable_get('gallery_block_grid_'.
$delta .
'_link_target', '');
143 $params['link'] = variable_get('gallery_block_grid_'.
$delta .
'_link', '');
145 return _gallery_block_get_block($params, array('num_cols' => $num_cols));
149 * Function _gallery_block_get_block().
151 function _gallery_block_get_block($params, $extra = array()) {
152 // Handle useralbum feature
153 if (preg_match('/user(:([\d]+))?/i', $params['itemId'], $param_uid)) {
154 require_once(drupal_get_path('module', 'gallery') .
'/gallery_user.inc');
155 $params['itemId'] = gallery_user_useralbum(isset($param_uid[2]) ?
$param_uid[2] : NULL
, FALSE
);
157 if (empty($params['itemId']) || $params['itemId'] === FALSE
) {
158 unset($params['itemId']);
160 gallery_debug($params, t('Block parameters'));
161 // Get the image(s) from G2
162 list($ret, $content, $head) = GalleryEmbed
::getImageBlock($params);
164 gallery_error(t('Error trying to get image block.'), $ret);
167 // Format the block content
170 $block['subject'] = t('Gallery');
171 if (isset($extra['num_cols'])) {
172 $class = isset($extra['class']) ?
$extra['class'] : 'gallery-grid-block';
173 $block['content'] = theme('gallery_block_grid_block', $content, $extra['num_cols'], $class);
176 $class = isset($extra['class']) ?
$extra['class'] : 'g2image_centered';
177 $block['content'] = theme('gallery_block_image_block', $content, $class);
179 // Add css/js to the page
181 gallery_set_head($head);
189 * Function _gallery_block_split_imageblock().
190 * (split an image block result into individual images)
192 function _gallery_block_split_imageblock($html) {
193 // From http://uk.php.net/manual/en/function.preg-split.php
194 // Split the html from image block into <...> parts
195 $pattern = '/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/';
196 $html_array = preg_split($pattern, trim($html), -1, PREG_SPLIT_DELIM_CAPTURE
| PREG_SPLIT_NO_EMPTY
);
201 // Iterate through this array and combine again, but on a per-image basis
202 foreach ($html_array as
$value) {
203 $value = trim($value);
204 $image_html .
= $value;
205 if (!strcmp($value, '<div class="one-image">')) {
206 // Found the opening <div> for the image
209 elseif (!strncmp($value, '<div', 4)) {
210 // Found a <div> but not the main image one (eg a frame)
213 elseif (!strcmp($value, '</div>')) {
214 // Found a </div> but check if it's for the main image or a subcomponent (eg frame)
215 if ($open_divs > 0) {
219 // This must be the closing div for "one-image" so move to next image
220 $images[] = $image_html;
230 * Theme function : theme_gallery_block_image_block().
232 function theme_gallery_block_image_block($content, $class = 'g2image_centered') {
233 return '<div class="'.
$class .
'">'.
$content .
'</div>';
237 * Theme function : theme_gallery_block_grid_block().
239 function theme_gallery_block_grid_block($content, $num_cols, $class = 'gallery-grid-block') {
240 $images = _gallery_block_split_imageblock($content);
241 $images = array_chunk($images, $num_cols);
244 foreach ($images as
$image_row) {
246 foreach ($image_row as
$image) {
247 $row[] = array('data' => $image, 'style' => 'text-align:center;');
252 $html = '<div class="'.
$class .
'"><div class="image-grid">';
253 $html .
= theme('table', array(), $rows);
254 $html .
= '</div></div>';