5 * gallery.module : gallery_filter.inc
6 * Gallery Filter functions (originally by MichelleC and Waldemar)
9 // ***************** The Filter in Action ***********************
11 define('GALLERY__FILTER_WORD', 1);
12 define('GALLERY_FILTER_INTEGER', 2);
13 define('GALLERY_FILTER_STRING', 3);
15 function gallery_filter_attr_value($text, $value_type = GALLERY_FILTER_WORD
) {
16 // Strip off initial and final quotes.
17 $first = substr($text, 0, 1);
18 if ($first == "\"" || $first == "\'") {
19 if (substr($text, -1, 1) == $first) {
20 $text = substr($text, 1, -1);
23 switch ($value_type) {
24 case GALLERY_FILTER_WORD
:
25 return preg_replace("/\W/", '', $text);
26 case GALLERY_FILTER_INTEGER
:
27 return preg_replace("/\D/", '', $text);
29 return check_plain($text);
33 // Execute filter on given text.
34 function gallery_filter_process($text) {
35 // Find all the image codes and loop over them, replacing each with the Gallery2 image block
36 $prefix = variable_get('gallery_filter_prefix', 'G2');
38 $matchetxt = "/\[".
trim($prefix).
":(\d+)(\s*,)?\s*(.*?)\]/i";
39 preg_match_all($matchetxt, $text, $matches, PREG_SET_ORDER
);
40 // If we have at least one match, set everything up
41 if (count($matches) > 0) {
42 // Set the default and path variables based on module settings
43 $default_size = variable_get('gallery_filter_default_size', 150);
44 $default_div_class = variable_get('gallery_filter_default_div_class', 'nowrap');
45 $default_album_frame = variable_get('gallery_filter_default_album_frame', '');
46 $default_item_frame = variable_get('gallery_filter_default_item_frame', '');
47 $default_block_type = variable_get('gallery_filter_default_block_type', 'recentImage');
48 $default_n_images = variable_get('gallery_filter_n_images', 1);
49 $default_show = variable_get('gallery_filter_default_show', 'none');
50 $default_link_target = variable_get('gallery_filter_default_link_target', '');
52 if ($default_album_frame == 'none') {
53 $default_album_frame = '';
55 if ($default_item_frame == 'none') {
56 $default_item_frame = '';
59 // This will hold the list of frames used for images so we can add the CSS link(s) at the end
60 $frame_list = array ();
62 // This sets up the embedding
63 list ($success, $ret) = _gallery_init(true
);
65 gallery_error(t('Unable to initialize embedded Gallery'), $ret);
69 foreach ($matches as
$match) {
70 // Pull out the arguments into the $args array
72 preg_match_all("/(\w+)\=(\"[^\"]*\"|\S*)/", $match[3], $a, PREG_SET_ORDER
);
74 foreach ($a as
$arg) {
75 $args[strtolower($arg[1])] = $arg[2];
78 // Set number of images to show
79 $n_images = gallery_filter_attr_value($args['n'], GALLERY_FILTER_INTEGER
);
81 // No size specified; use the default
82 $n_images = $default_n_images;
86 $block_type = gallery_filter_attr_value($args['type'], GALLERY_FILTER_WORD
);
87 if (empty($block_type)) {
88 // No block type specified; use the default
89 $block_type = $default_block_type;
92 $block_type = 'specificItem'; //so it shows something if n=1 and an album is selected
94 // Set the size of the thumbnail
95 $size = gallery_filter_attr_value($args['size'], GALLERY_FILTER_INTEGER
);
97 // No size specified; use the default
98 $size = $default_size;
101 // Set the class of the div
102 $div_class = gallery_filter_attr_value($args['class'], GALLERY_FILTER_WORD
);
103 if (empty ($div_class)) {
104 // No class specified; use the default
105 $div_class = $default_div_class;
107 // Switch the class to g2image versions (adds consistency)
108 switch ($div_class) {
110 $div_class = "g2image_float_left";
113 $div_class = "g2image_float_right";
117 $div_class = "g2image_centered";
120 $div_class = "g2image_normal";
123 // Set the overriding, album, and item frames
124 $frame = gallery_filter_attr_value($args['frame'], GALLERY_FILTER_WORD
);
125 $album_frame = gallery_filter_attr_value($args['aframe'], GALLERY_FILTER_WORD
);
126 $item_frame = gallery_filter_attr_value($args['iframe'], GALLERY_FILTER_WORD
);
128 if (empty ($frame)) {
129 // No overriding frame given; check for album_frame and item_frame
130 if (empty ($album_frame)) {
131 // No album frame specified; use the default one
132 $album_frame = $default_album_frame;
135 if (empty ($item_frame)) {
136 // No item frame specified; use the default one
137 $item_frame = $default_item_frame;
141 // Overriding frame given; use it
142 $album_frame = $frame;
143 $item_frame = $frame;
146 // Add the requested frames to the array so we can get the CSS later. Don't worry about
147 // dupes at this point; they will be filtered out later.
148 array_push($frame_list, $frame);
149 array_push($frame_list, $album_frame);
150 array_push($frame_list, $item_frame);
152 // This part actually fetches the image block. It uses the same paramaters as the code
153 // found under "Image Block" in site admin in Gallery2.
155 $show = $default_show;
157 // Not customized yet:
158 $link_target = $default_link_target;
160 $param_blocks_array = array_fill(0, $n_images, $block_type);
161 $params['itemId'] = $match[1];
162 $params['blocks'] = is_array($param_blocks_array) ?
implode('|', $param_blocks_array) : "";
163 $param_show_array = $show;
164 $params['show'] = is_array($param_show_array) ?
implode('|', $param_show_array) : "";
165 $params['maxSize'] = $size;
166 // Add frames and link target using g2_filter code from MichelleC
167 $params['albumFrame'] = $album_frame;
168 $params['itemFrame'] = $item_frame;
169 $params['linkTarget'] = $link_target;
173 list ($ret, $content, $head) = GalleryEmbed
::getImageBlock($params);
175 gallery_error(t('Unable to get Gallery image block'), $ret);
179 // Add a div around the table for styling
180 if ($div_class != 'none') {
181 $content = '<div class ="giImageBlock '.
$div_class.
'">'.
$content.
'</div>';
183 // This puts the image block HTML back into the rest of the text
184 $text = str_replace($match[0], $content, $text);
190 } // end of for loop through matches
191 // If we had at least one match, finish up by adding the css. Unfotunately if there are multiple
192 // images on a page this will get added multiple times.
193 if (count($matches) > 0) {
194 GalleryEmbed
:: done();
196 gallery_set_html_head(implode("\n", array_unique($g2_head)));
198 drupal_add_css(drupal_get_path('module', 'gallery') .
'/gallery_filter.css', 'module', 'all');
200 return $text .
"<br class=\"giImageBlock-clear-both\" />";;