Removing translation directories
[project/gallery.git] / gallery_block.inc
1 <?php
2
3 /**
4 * gallery.module : gallery_block.inc
5 * Block functions
6 */
7
8 /**
9 * Implementation of hook_block().
10 */
11 function _gallery_block($op = 'list', $delta = 0, $edit = array()) {
12 $type_map = $param_map = array();
13 _gallery_block_options($type_map, $param_map);
14
15 list($type, $delta) = explode('-', $delta);
16 switch ($op) {
17 case 'list':
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) .']') : ''));
24 }
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) .']') : ''));
30 }
31 return $blocks;
32 case 'view':
33 if (!user_access('access gallery') || !_gallery_init(TRUE) || $_GET['q'] == 'admin/settings/gallery/install') {
34 return;
35 }
36 switch ($type) {
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>';
41 }
42 break;
43 case 'image': // Image Block
44 $block = _gallery_block_image_block($delta, $type_map);
45 break;
46 case 'grid': // Image Grid Block
47 $block = _gallery_block_grid_block($delta, $type_map);
48 break;
49 }
50 GalleryEmbed::done();
51 return $block;
52 case 'configure':
53 require_once(drupal_get_path('module', 'gallery') .'/gallery_settings.inc');
54 switch ($type) {
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);
59 }
60 break;
61 case 'save':
62 require_once(drupal_get_path('module', 'gallery') .'/gallery_settings.inc');
63 _gallery_settings_block_save($delta, $edit);
64 break;
65 }
66 }
67
68 /**
69 * Function _gallery_block_options().
70 */
71 function _gallery_block_options(&$type_map, &$param_map) {
72 $type_map = array(
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')
85 );
86 $param_map = array(
87 'title' => t('Title'),
88 'date' => t('Date'),
89 'views' => t('View Count'),
90 'owner' => t('Item owner'),
91 'heading' => t('Heading')
92 );
93 }
94
95 /**
96 * Function _gallery_block_image_block().
97 */
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);
106 }
107 else {
108 $params['exactSize'] = variable_get('gallery_block_image_'. $delta .'_size', GALLERY_IMAGEBLOCK_SIZE_DEFAULT);
109 }
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', '');
114
115 $block = _gallery_block_get_block($params);
116 $block['subject'] = (count($param_blocks_array) > 1) ? t('Gallery') : $type_map[$params['blocks']];
117
118 return $block;
119 }
120
121 /**
122 * Function _gallery_block_grid_block().
123 */
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;
128
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);
136 }
137 else {
138 $params['exactSize'] = variable_get('gallery_block_grid_'. $delta .'_size', GALLERY_GRID_SIZE_DEFAULT);
139 }
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', '');
144
145 return _gallery_block_get_block($params, array('num_cols' => $num_cols));
146 }
147
148 /**
149 * Function _gallery_block_get_block().
150 */
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);
156 }
157 if (empty($params['itemId']) || $params['itemId'] === FALSE) {
158 unset($params['itemId']);
159 }
160 gallery_debug($params, t('Block parameters'));
161 // Get the image(s) from G2
162 list($ret, $content, $head) = GalleryEmbed::getImageBlock($params);
163 if ($ret) {
164 gallery_error(t('Error trying to get image block.'), $ret);
165 return array();
166 }
167 // Format the block content
168 $block = array();
169 if ($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);
174 }
175 else {
176 $class = isset($extra['class']) ? $extra['class'] : 'g2image_centered';
177 $block['content'] = theme('gallery_block_image_block', $content, $class);
178 }
179 // Add css/js to the page
180 if ($head) {
181 gallery_set_head($head);
182 }
183 }
184
185 return $block;
186 }
187
188 /**
189 * Function _gallery_block_split_imageblock().
190 * (split an image block result into individual images)
191 */
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);
197
198 $ndx = 0;
199 $images = array();
200 $image_html = '';
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
207 $open_divs = 0;
208 }
209 elseif (!strncmp($value, '<div', 4)) {
210 // Found a <div> but not the main image one (eg a frame)
211 $open_divs++;
212 }
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) {
216 $open_divs--;
217 }
218 else {
219 // This must be the closing div for "one-image" so move to next image
220 $images[] = $image_html;
221 $image_html = '';
222 }
223 }
224 }
225
226 return $images;
227 }
228
229 /**
230 * Theme function : theme_gallery_block_image_block().
231 */
232 function theme_gallery_block_image_block($content, $class = 'g2image_centered') {
233 return '<div class="'. $class .'">'. $content .'</div>';
234 }
235
236 /**
237 * Theme function : theme_gallery_block_grid_block().
238 */
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);
242
243 $rows = array();
244 foreach ($images as $image_row) {
245 $row = array();
246 foreach ($image_row as $image) {
247 $row[] = array('data' => $image, 'style' => 'text-align:center;');
248 }
249 $rows[] = $row;
250 }
251
252 $html = '<div class="'. $class .'"><div class="image-grid">';
253 $html .= theme('table', array(), $rows);
254 $html .= '</div></div>';
255
256 return $html;
257 }