#105811 - Fixed t('') calls with '%' in them. Hopefully all of them!
[project/gallery.git] / gallery_filter.inc
1 <?php
2 // $Id$
3
4 /**
5 * gallery.module : gallery_filter.inc
6 * Gallery Filter functions (originally by MichelleC and Waldemar)
7 */
8
9 // ***************** The Filter in Action ***********************
10
11 define('GALLERY__FILTER_WORD', 1);
12 define('GALLERY_FILTER_INTEGER', 2);
13 define('GALLERY_FILTER_STRING', 3);
14
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);
21 }
22 }
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);
28 default :
29 return check_plain($text);
30 }
31 }
32
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');
37
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', '');
51
52 if ($default_album_frame == 'none') {
53 $default_album_frame = '';
54 }
55 if ($default_item_frame == 'none') {
56 $default_item_frame = '';
57 }
58
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 ();
61
62 // This sets up the embedding
63 list ($success, $ret) = _gallery_init(true);
64 if (!$success) {
65 gallery_error(t('Unable to initialize embedded Gallery'), $ret);
66 return;
67 }
68 }
69 foreach ($matches as $match) {
70 // Pull out the arguments into the $args array
71 $args = array ();
72 preg_match_all("/(\w+)\=(\"[^\"]*\"|\S*)/", $match[3], $a, PREG_SET_ORDER);
73
74 foreach ($a as $arg) {
75 $args[strtolower($arg[1])] = $arg[2];
76 }
77
78 // Set number of images to show
79 $n_images = gallery_filter_attr_value($args['n'], GALLERY_FILTER_INTEGER);
80 if ($n_images == 0) {
81 // No size specified; use the default
82 $n_images = $default_n_images;
83 }
84
85 // Set the block type
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;
90 }
91 if ($n_images <= 1)
92 $block_type = 'specificItem'; //so it shows something if n=1 and an album is selected
93
94 // Set the size of the thumbnail
95 $size = gallery_filter_attr_value($args['size'], GALLERY_FILTER_INTEGER);
96 if ($size == 0) {
97 // No size specified; use the default
98 $size = $default_size;
99 }
100
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;
106 }
107 // Switch the class to g2image versions (adds consistency)
108 switch ($div_class) {
109 case 'left':
110 $div_class = "g2image_float_left";
111 break;
112 case 'right':
113 $div_class = "g2image_float_right";
114 break;
115 case 'center':
116 case 'centre':
117 $div_class = "g2image_centered";
118 break;
119 case 'normal':
120 $div_class = "g2image_normal";
121 break;
122 }
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);
127
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;
133 }
134
135 if (empty ($item_frame)) {
136 // No item frame specified; use the default one
137 $item_frame = $default_item_frame;
138 }
139
140 } else {
141 // Overriding frame given; use it
142 $album_frame = $frame;
143 $item_frame = $frame;
144 }
145
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);
151
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.
154
155 $show = $default_show;
156
157 // Not customized yet:
158 $link_target = $default_link_target;
159
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;
170
171 $g2_head = array();
172 $block = array ();
173 list ($ret, $content, $head) = GalleryEmbed::getImageBlock($params);
174 if ($ret) {
175 gallery_error(t('Unable to get Gallery image block'), $ret);
176 return;
177 } else {
178 if ($content) {
179 // Add a div around the table for styling
180 if ($div_class != 'none') {
181 $content = '<div class ="giImageBlock '.$div_class.'">'.$content.'</div>';
182 }
183 // This puts the image block HTML back into the rest of the text
184 $text = str_replace($match[0], $content, $text);
185 }
186 if ($head) {
187 $g2_head[] = $head;
188 }
189 }
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();
195 if ($g2_head) {
196 gallery_set_html_head(implode("\n", array_unique($g2_head)));
197 }
198 drupal_add_css(drupal_get_path('module', 'gallery') .'/gallery_filter.css', 'module', 'all');
199 }
200 return $text . "<br class=\"giImageBlock-clear-both\" />";;
201 }
202 ?>