fix #31412 - now allow multiple images in the image block
[project/gallery.git] / contrib / g2_filter / g2_filter.module
CommitLineData
06e78013
JW
1<?php\r
2// $Id$\r
3\r
4/**\r
5 * @file\r
6 * Allows adding gallery blocks into nodes using a filter --> [G2:item_id]\r
7 *\r
8 */\r
9\r
10/******************************* Help sections ************************************/\r
11\r
12function g2_filter_short_tip_translated() {\r
13 return t('You may link to G2 items on this site <a href="%explanation-url">using a special syntax</a>',\r
14 array('%explanation-url' => url('filter/tips', NULL, 'filter-g2_filter-0')));\r
15}\r
16\r
17function g2_filter_long_tip_translated() {\r
18 $prefix = variable_get("g2_filter_prefix", "G2");\r
19\r
20$output = "";\r
21$output .= '<strong>G2 Filter:</strong><p>You can link to items in your ';\r
22$output .= 'embedded Gallery 2 using a special code. ';\r
23$output .= 'This code will be replaced by a thumbnail image that is ';\r
24$output .= 'linked to the actual item in your Gallery. ';\r
25$output .= '<em>Syntax:</em><br />';\r
26$output .= '<blockquote><code>';\r
27$output .= '['.$prefix.':<em>item_id</em> n=<em>number</em> type=<em>type</em> size=<em>number</em> class=<em>name</em> frame=<em>name</em> album_frame=<em>name</em> item_frame=<em>name</em>]';\r
28$output .= '</code></blockquote>';\r
29\r
30$output .= '<ul><li>item_id (required): This is the item ID from G2. ';\r
31$output .= 'If you look at the URL of the item, this is the last number. '\r
32 .'<em>Note that if the item_id is a single photo, n must be 1</em></li> ';\r
33\r
34$output .= '<li>n (suggested): This is the number of photos you want the block to show. ';\r
35$output .= 'It will override whatever is set in the defaults (initially 1). '\r
36 .' Note: this will change past instances where you did not set n -- the reason for its suggested use.</li> ';\r
37\r
38$output .= '<li>type: The default type of gallery block. Any of the following may be used: ';\r
39$output .= '<em>randomImage, recentImage, viewedImage, randomAlbum, recentAlbum, viewedAlbum, dailyImage, ' \r
40 .'weeklyImage, monthlyImage, dailyAlbum, weeklyAlbum, monthlyAlbum, specificItem . </em> '\r
41 .'Note that for n=1, selectedItem is automatically chosen regardless of this parameter.</li> ';\r
42\r
43$output .= '<li>class: The block that G2 returns is wrapped in a DIV so additional styling can be done. ';\r
44$output .= 'The classes for this DIV are located in g2_filter.css. Included with the module ';\r
45$output .= 'are "left", "right", and "nowrap". These position the image block to the left or ';\r
46$output .= 'right or on a line all its own with the text not wrapping. You can also add your '; \r
47$output .= 'own class(es) to the CSS file and they will automatically be available.</li> ';\r
48\r
49$output .= '<li>size: The length of the longest side for the thumbnail. ';\r
50$output .= 'The other side is determined automatically to keep the same aspect ratio.</li> ';\r
51\r
52$output .= '<li>frame/album_frame/item_frame: You can use just "frame" to assign a frame ';\r
53$output .= 'to the thumbnail regardless of whether it\'s for an album or a single item. ';\r
54$output .= 'Using aframe will only affect albums and iframe will only affect single items. ';\r
55$output .= 'Frames included with the default Gallery 2 install are: ';\r
56$output .= 'bamboo, book, brand , dots, flicking, gold, gold2, polaroid, polaroids, shadow, ';\r
57$output .= 'shells, slide, solid, notebook, wood.</li>';\r
58\r
59return t($output);\r
60\r
61}\r
62\r
63function g2_filter_help($section = 'admin/help#image_filter') {\r
64 $output = '';\r
65 switch ($section) {\r
66 case 'admin/help#g2_filter':\r
67 $output = t('<p>Used to add image blocks from your embedded Gallery 2 to a node \r
68 like a blog entry or a story. To enable this feature and learn the proper syntax, \r
69 visit the <a href="%filters">filters configuration screen</a>.</p>', \r
70 array('%filters' => url('admin/filters')));\r
71 \r
72 break;\r
73 case 'admin/modules#description':\r
74 $output = t("Allow users to reference G2 items from nodes.");\r
75 break;\r
76 case 'filter#short-tip':\r
77 return g2_filter_short_tip_translated();\r
78 case 'filter#long-tip':\r
79 return g2_filter_long_tip_translated();\r
80 }\r
81\r
82 return $output;\r
83}\r
84\r
85\r
86/******************************* Configuration ************************************/\r
87\r
88function g2_filter_get_settings_form() {\r
89 $typeMap = array('randomImage' => t('Random image'),\r
90 'recentImage' => t('Recent image'),\r
91 'viewedImage' => t('Viewed image'),\r
92 'randomAlbum' => t('Random album'),\r
93 'recentAlbum' => t('Recent album'),\r
94 'viewedAlbum' => t('Viewed album'),\r
95 'dailyImage' => t('Daily image'),\r
96 'weeklyImage' => t('Weekly image'),\r
97 'monthlyImage' => t('Monthly image'),\r
98 'dailyAlbum' => t('Daily album'),\r
99 'weeklyAlbum' => t('Weekly album'),\r
100 'monthlyAlbum' => t('Monthly album')); \r
101//we don't include specificItem, since it can be selected automatically if n=1\r
102 \r
103 $output .= form_textfield(t("Filter prefix"), "g2_filter_prefix",\r
104 variable_get("g2_filter_prefix", "G2"), 10, 10,\r
105 t("Prefix to use with filter. Example: 'G2' means you use [G2: 999].")); \r
106 \r
107 $output .= form_select(t('Image Block Type'), 'g2_filter_default_block_type',\r
108 variable_get('g2_filter_default_block_type', 'viewedImage'), $typeMap,\r
109 t('Pick default type of image block you\'d like to use, default is Recent image. Viewed image is by most clicks.')); \r
110 \r
111 $output .= form_textfield(t("Default Number of Images"), "g2_filter_n_images",\r
112 variable_get("g2_filter_n_images", 1), 3, 3,\r
113 t("How many images you want the default block to show. Best to keep at 1 and use the n parameter.")); \r
114//////*********in fact, should we remove this option all together? because the way it is now, if you change this to n>1\r
115//then instances where n was not specified and item_id is a specific item, the block breaks and nothing is shown.\r
116\r
117 $output .= form_checkboxes(\r
118 t('Default Image Block Settings'),\r
119 'g2_filter_default_show',\r
120 variable_get('g2_filter_default_show', 'none'),\r
121 array('title' => 'Title (the caption)',\r
122 'date' => 'Date',\r
123 'views' => 'View Count',\r
124 'owner' => 'Item owner',\r
125 'heading' => 'Heading',\r
126 'fullSize' => 'Full Size'),\r
127 t('Choose the item metadata you\'d like to display by default. This will change all instances where show parameter was not specified.'));\r
128\r
129 \r
130 $output .= form_textfield(t("Default thumbnail size"), "g2_filter_default_size",\r
131 variable_get("g2_filter_default_size", "150"), 3, 3,\r
132 t('If no size is specified when calling the filter, this size will be used. '\r
133 .'Note that the display size will be limited by the size of the item_id in Gallery.')); \r
134\r
135 $output .= form_textfield(t("Default class"), "g2_filter_default_div_class",\r
136 variable_get("g2_filter_default_div_class", "nowrap"), 20, 20,\r
137 t("left, right, or nowrap. (See g2_filter.css to add more or modify these.)")); \r
138\r
139 $output .= form_textfield(t("Default album frame"), "g2_filter_default_album_frame",\r
140 variable_get("g2_filter_default_album_frame", "none"), 20, 20,\r
141 t("Enter a frame name like notebook, polaroid, shadow, slide, wood, etc. See your G2 install for a complete list. Use 'none' or blank for no frame. ")); \r
142\r
143 $output .= form_textfield(t("Default item frame"), "g2_filter_default_item_frame",\r
144 variable_get("g2_filter_default_item_frame", "none"), 20, 20,\r
145 t("Enter a frame name like notebook, polaroid, shadow, slide, wood, etc. See your G2 install for a complete list. Use 'none' or blank for no frame. ")); \r
146\r
147 return $output;\r
148}\r
149\r
150// Hook which handles filtering.\r
151function g2_filter_filter($op, $delta = 0, $format = -1, $text = '') {\r
152 switch ($op) {\r
153 case 'list':\r
154 return array(0 => t('G2 filter'));\r
155 case 'description':\r
156 return t('Allow users to easily reference G2 items from nodes.');\r
157 case 'process':\r
158 return g2_filter_process($text);\r
159 case 'settings':\r
160 $group = g2_filter_get_settings_form();\r
161 return form_group(t("Gallery 2 Filter"), $group);\r
162 default:\r
163 return $text;\r
164 }\r
165}\r
166\r
167function g2_filter_filter_tips($delta = 0, $format = -1, $long = false) {\r
168 if ($long) {\r
169 return g2_filter_long_tip_translated();\r
170 }\r
171 else {\r
172 return g2_filter_short_tip_translated();\r
173 }\r
174}\r
175\r
176// ***************** The Filter in Action ***********************\r
177\r
178define("G2_FILTER_WORD", 1);\r
179define("G2_FILTER_INTEGER", 2);\r
180define("G2_FILTER_STRING", 3);\r
181\r
182function g2_filter_attr_value($text, $value_type = G2_FILTER_WORD) {\r
183 // Strip off initial and final quotes.\r
184 $first = substr($text, 0, 1);\r
185 if ($first == "\"" || $first == "\'") {\r
186 if (substr($text, -1, 1) == $first) {\r
187 $text = substr($text, 1, -1);\r
188 }\r
189 }\r
190 switch ($value_type) {\r
191 case G2_FILTER_WORD:\r
192 return preg_replace("/\W/", '', $text);\r
193 case G2_FILTER_INTEGER:\r
194 return preg_replace("/\D/", '', $text);\r
195 default:\r
196 return check_plain($text);\r
197 }\r
198}\r
199\r
200// Execute filter on given text.\r
201function g2_filter_process($text) {\r
202\r
203 // Find all the image codes and loop over them, replacing each with the G2 image block\r
204 $prefix = variable_get("g2_filter_prefix", "G2");\r
205\r
206 $matchetxt = "/\[".trim($prefix).":(\d+)(\s*,)?\s*(.*?)\]/i";\r
207 preg_match_all($matchetxt, $text, $matches, PREG_SET_ORDER);\r
208\r
209 // If we have at least one match, set everything up\r
210 if (count($matches) > 0) { \r
211 // Set the default and path variables based on module settings\r
212 $default_size = variable_get("g2_filter_default_size", 150);\r
213 $default_div_class = variable_get("g2_filter_default_div_class", "nowrap");\r
214 $default_album_frame = variable_get("g2_filter_default_album_frame", "");\r
215 $default_item_frame = variable_get("g2_filter_default_item_frame", "");\r
216 $default_block_type = variable_get('g2_filter_default_block_type', 'recentImage');\r
217 $default_n_images = variable_get("g2_filter_n_images", 1);\r
218 $default_show = variable_get('g2_filter_default_show', 'none');\r
219 \r
220 if ($default_album_frame == 'none') {\r
221 $default_album_frame = '';\r
222 }\r
223 if ($default_item_frame == 'none') {\r
224 $default_item_frame = '';\r
225 } \r
226\r
227 // This will hold the list of frames used for images so we can add the CSS link(s) at the end\r
228 $frame_list = array() ;\r
229 \r
230/* this is all done by _gallery_init(true) function\r
231 // Set up the pathing\r
232 $relative_G2_Path = variable_get("gallery_dir", "gallery2");\r
233 $embed_uri = url('gallery');\r
234 $embed_php_path = $relative_G2_Path . 'embed.php';\r
235 //the above line needs work, when the user sets the relative G2 path without a trailing / this module breaks\r
236 \r
237 // The tolerant base_url patch in http://drupal.org/node/32389 seems to work well, but not all of it is needed.\r
238 // The http or https part is not required.\r
239 // BUT, does this work for all cases (Apache, IIS?)\r
240 if ($dir = trim(dirname($_SERVER['SCRIPT_NAME']), '\,/')) {\r
241 $embedPath = "/$dir";\r
242 } else {\r
243 $embedPath = '/';\r
244 }\r
245\r
246 // Link to the embedding code\r
247 require_once($embed_php_path);\r
248\r
249 // Grab the user information from Drupal\r
250 global $user;\r
251*/\r
252 // This sets up the embedding\r
253 list ($success, $ret) = _gallery_init(true); \r
254 //_gallery_init() is from gallery.module, any reason not to use it? \r
255 //just remember if they change that function this module breaks\r
256 if (!$success) {\r
257 gallery_error(t('Unable to initialize embedded Gallery'), $ret);\r
258 return;\r
259 } /* since i commented this out, and use galler_init, i can probably get rid of some of the init code above\r
260 $ret = GalleryEmbed::init(array('embedUri' => $embed_uri,\r
261 'embedPath' => $embed_path,\r
262 'relativeG2Path' => $relative_G2_Path,\r
263 'loginRedirect' => '',\r
264 'fullInit' => true,\r
265 'activeUserId' => $user->uid)); */\r
266 }\r
267\r
268 foreach ($matches as $match) {\r
269\r
270 // Pull out the arguments into the $args array\r
271 $args = array();\r
272 preg_match_all("/(\w+)\=(\"[^\"]*\"|\S*)/", $match[3], $a, PREG_SET_ORDER);\r
273\r
274 foreach ($a as $arg) {\r
275 $args[strtolower($arg[1])] = $arg[2];\r
276 }\r
277\r
278 // Set number of images to show\r
279 $n_images = g2_filter_attr_value($args['n'], G2_FILTER_INTEGER);\r
280 if ($n_images == 0) {\r
281 // No size specified; use the default\r
282 $n_images = $default_n_images;\r
283 }\r
284\r
285 // Set the block type\r
286 $block_type = g2_filter_attr_value($args['type'], G2_FILTER_WORD);\r
287 if (empty($block_type)) {\r
288 // No size specified; use the default\r
289 $block_type = $default_block_type;\r
290 }\r
291 if ($n_images <= 1) $block_type = 'specificItem'; //so it shows something if n=1 and an album is selected\r
292 \r
293 // Set the size of the thumbnail\r
294 $size = g2_filter_attr_value($args['size'], G2_FILTER_INTEGER);\r
295 if ($size == 0) {\r
296 // No size specified; use the default\r
297 $size = $default_size;\r
298 }\r
299\r
300 // Set the class of the div\r
301 $div_class = g2_filter_attr_value($args['class'], G2_FILTER_WORD);\r
302 if (empty($div_class)) {\r
303 // No class specified; use the default\r
304 $div_class = $default_div_class;\r
305 }\r
306\r
307 // Set the overriding, album, and item frames\r
308 $frame = g2_filter_attr_value($args['frame'], G2_FILTER_WORD);\r
309 $album_frame = g2_filter_attr_value($args['aframe'], G2_FILTER_WORD);\r
310 $item_frame = g2_filter_attr_value($args['iframe'], G2_FILTER_WORD);\r
311\r
312 if (empty($frame)) {\r
313 // No overriding frame given; check for album_frame and item_frame\r
314 if (empty($album_frame)) {\r
315 // No album frame specified; use the default one\r
316 $album_frame = $default_album_frame;\r
317 }\r
318 \r
319 if (empty($item_frame)) {\r
320 // No item frame specified; use the default one\r
321 $item_frame = $default_item_frame;\r
322 }\r
323 \r
324 } else {\r
325 // Overriding frame given; use it\r
326 $album_frame = $frame;\r
327 $item_frame = $frame;\r
328 }\r
329\r
330 // Add the requested frames to the array so we can get the CSS later. Don't worry about\r
331 // dupes at this point; they will be filtered out later.\r
332 array_push($frame_list,$frame);\r
333 array_push($frame_list,$album_frame);\r
334 array_push($frame_list,$item_frame);\r
335\r
336 // This part actually fetches the image block. It uses the same paramaters as the code\r
337 // found under "Image Block" in site admin in G2. Copied and slightly modified here for reference.\r
338\r
339 // blocks Pipe(|) separate list chosen from: randomImage, recentImage, \r
340 // viewedImage, randomAlbum, recentAlbum, viewedAlbum, dailyImage, \r
341 // weeklyImage, monthlyImage, dailyAlbum, weeklyAlbum, monthlyAlbum, \r
342 // specificItem; default is randomImage\r
343 // show Pipe(|) separated list chosen from: title, date, views, owner, heading, \r
344 // fullSize; the value can also be: none\r
345 // NOTE: If you want your size to be bigger than the thumbnail size for that image as\r
346 // defined in your G2, you must use 'show' => 'fullSize'\r
347 // itemId Limit the item selection to the subtree of the gallery under the \r
348 // album with the given id; or the id of the item to display when used with \r
349 // specificItem block type\r
350 // maxSize Scale images to this maximum size\r
351 // linkTarget Add a link target (for example, to open links in a new browser window)\r
352 // itemFrame Image frame to use around images\r
353 // albumFrame Image frame to use around albums\r
354\r
355\r
356//$blocks = 'specificItem'; //not used \r
357$show = $default_show;\r
358\r
359// Not customized yet:\r
360$link_target = ''; \r
361\r
362 \r
363///////keizo\r
364 // Allow for multiple image types\r
365 $param_blocks_array = array_fill(0,$n_images,$block_type); \r
366 $params['itemId'] = $match[1];\r
367 $params['blocks'] = is_array($param_blocks_array) ? implode('|', $param_blocks_array) : ""; \r
368 $param_show_array = $show;\r
369 $params['show'] = is_array($param_show_array) ? implode('|', $param_show_array) : ""; \r
370 $params['maxSize'] = $size;\r
371 // Add frames and link target using g2_filter code from MichelleC\r
372 $params['albumFrame'] = $album_frame;\r
373 $params['itemFrame'] = $item_frame;\r
374 $params['linkTarget'] = $link_target;\r
375\r
376 $block = array();\r
377 list($ret, $imageBlockHtml) = GalleryEmbed::getImageBlock($params);\r
378 if ($ret->isError()) {\r
379 gallery_error(t('Unable to get Gallery image block'), $ret);\r
380 return;\r
381 } else {\r
382 //if ($imageBlockHtml) { this line is redundant isn't it?\r
383 // Add a div around the table for styling\r
384 if ($div_class != 'none') {\r
385 $imageBlockHtml = '<div class ="giImageBlock ' . $div_class . '">' .\r
386 $imageBlockHtml . '</div>';\r
387 } \r
388 // This puts the image block HTML back into the rest of the text\r
389 $text = str_replace($match[0], $imageBlockHtml, $text);\r
390 //}\r
391 } \r
392 //end keizo\r
393/* list ($ret, $imageBlockHtml) = \r
394 GalleryEmbed::getImageBlock(array(\r
395 'blocks' => $blocks, \r
396 'show' => $show , \r
397 'itemId' => $match[1], \r
398 'maxSize' => $size, \r
399 'linkTarget' => $link_target,\r
400 'albumFrame' => $album_frame, \r
401 'itemFrame' => $item_frame));\r
402\r
403 // Add a div around the table for styling\r
404 $imageBlockHtml = '<div class ="' . $div_class . '">' . $imageBlockHtml . '</div>';\r
405\r
406 // This puts the image block HTML back into the rest of the text\r
407 $text = str_replace($match[0], $imageBlockHtml, $text);\r
408*/\r
409\r
410\r
411 } // end of for loop through matches\r
412\r
413 // If we had at least one match, finish up\r
414 if (count($matches) > 0) { \r
415\r
416 GalleryEmbed::done();\r
417\r
418 // Add the CSS link(s)\r
419 $css_links = '';\r
420\r
421 // In order to use the album/image frames, we need to link to the style sheet(s)\r
422 $frame_list = array_unique($frame_list);\r
423 foreach ($frame_list as $frame) {\r
424 $css_links .= '<link rel="stylesheet" href="' . $relative_G2_Path . \r
425 '/main.php?g2_controller=imageblock.ExternalCSS&amp;g2_frames=' .$frame.'"/> ' ;\r
426 }\r
427\r
428 // Link to the CSS that controls the styling of the wrapping div\r
429 $div_css = drupal_get_path('module', 'g2_filter') . "/g2_filter.css";\r
430 $css_links .= '<link rel="stylesheet" href="' . $div_css . '"/> ' ;\r
431\r
432 $text = $css_links .= $text ;\r
433\r
434 }\r
435\r
436 return $text;\r
437}\r
438\r
439\r
440?>\r