Removing translation directories
[project/lightbox2.git] / lightbox2.module
1 <?php
2
3
4 /**
5 * Enables the use of lightbox V2 which places images above your current page,
6 * not within.
7 * This frees you from the constraints of the layout, particularly column
8 * widths.
9 *
10 * This module is for Drupal 5.x only.
11 *
12 * Module by: Mark Ashmead
13 * Mailto: bugzie@gmail.com
14 * Co-maintainer: Stella Power (http://drupal.org/user/66894)
15 *
16 * Image Node Support: Steve McKenzie
17 *
18 */
19
20 /**
21 * GLOBAL VARIABLES
22 */
23 $_LIGHTBOX2_INCLUDE = false;
24
25 /**
26 * Implementation of hook_help().
27 *
28 * Displays the information for this module.
29 * Displays the help text for this module.
30 */
31 function lightbox2_help($section) {
32 switch ($section) {
33 case 'admin/modules#description':
34 return t('Enables Lightbox V2 for Drupal');
35
36 case 'admin/help#lightbox2':
37 return t('<h3>Overview</h3>
38 <p align="justify">Lightbox JS V2 is a simple, unobtrusive script used to overlay images on the current page. It\'s a snap to setup and works on all modern browsers. The module comes with a Lightbox2 Lite option which does not use the Scriptaculous/Prototype libraries; it is therefore less likely to conflict with anything else.</p>
39 <p align="justify">Places images above your current page, not within. This frees you from the constraints of the layout, particularly column widths. Keeps users on the same page. Clicking to view an image and then having to click the back button to return to your site is bad for continuity (and no fun!).</p>
40 <p align="justify"><h3>Usage</h3></p>
41 <p>Add rel=&quot;lightbox&quot; attribute to any link tag to activate the lightbox. For example: </p>
42 <p>&lt;a href=&quot;image-1.jpg&quot; rel=&quot;lightbox&quot; title=&quot;my caption&quot;&gt;image #1&lt;/a&gt;</p>
43 <p>Optional: Use the title attribute if you want to show a caption.</p>
44 <p>If you have a set of related images that you would like to group, follow
45 step one but additionally include a group name between square brackets in the
46 rel attribute. For example:</p><p>&lt;a href="images/image-1.jpg"
47 rel="lightbox[roadtrip]"&gt;image #1&lt;/a&gt;<br />
48 &lt;a href="images/image-2.jpg" rel="lightbox[roadtrip]"&gt;image
49 #2&lt;/a&gt;<br />
50 &lt;a href="images/image-3.jpg" rel="lightbox[roadtrip]"&gt;image
51 #3&lt;/a&gt;<br /></p>
52 <p>No limits to the number of image sets per page or how many images are allowed
53 in each set. Go nuts!</p>');
54 break;
55 }
56 }
57
58 /**
59 * Implementation of hook_perm()
60 * Define the permissions this module uses
61 */
62 function lightbox2_perm() {
63 return array('administer lightbox2');
64 }
65
66 /**
67 * Implementation of hook_menu()
68 */
69 function lightbox2_menu($may_cache) {
70 $items = array();
71
72 if ($may_cache) {
73 $items[] = array('path' => 'admin/settings/lightbox2',
74 'title' => t('Lightbox2'),
75 'callback' => 'drupal_get_form',
76 'callback arguments' => array('lightbox2_settings_form'),
77 'access' => user_access('administer lightbox2'),
78 'description' => t('Allows the user to configure the lightbox2 settings'),
79 );
80 }
81
82 return $items;
83 }
84
85 /**
86 * Implementation of hook_nodeapi().
87 */
88 function lightbox2_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
89 global $_LIGHTBOX2_INCLUDE;
90
91 if (lightbox2_exclude_these_paths() != 1) {
92 if ($op == 'view' && !$_LIGHTBOX2_INCLUDE) {
93 lightbox2_add_files();
94 $_LIGHTBOX2_INCLUDE = true;
95 }
96 elseif ($node->type == 'image' && !$_LIGHTBOX2_INCLUDE) {
97 lightbox2_add_files();
98 $_LIGHTBOX2_INCLUDE = true;
99 }
100 }
101 }
102
103 /**
104 * Implementation of hook_settings()
105 */
106 function lightbox2_settings_form() {
107 // Define Lightbox2 Plus form.
108 $form['lightbox2_plus_options'] = array(
109 '#type' => 'fieldset',
110 '#title' => t('Lightbox2 Plus'),
111 '#collapsible' => TRUE,
112 '#collapsed' => FALSE,
113 );
114
115 // Add Checkbox for Lightbox2 Plus.
116 $form['lightbox2_plus_options']['lightbox2_plus'] = array(
117 '#type' => 'checkbox',
118 '#title' => t('Use Lightbox2 Plus'),
119 '#description' => t('Un-checking this box will enable Lightbox2 Lite.'),
120 '#default_value' => variable_get('lightbox2_plus', true),
121 );
122
123 // Define Image Node Options form.
124 $form['image_node_options'] = array(
125 '#type' => 'fieldset',
126 '#title' => t('Lightbox2 Plus Image Node options'),
127 '#collapsible' => TRUE,
128 '#collapsed' => TRUE,
129 );
130
131 // Add Checkbox for Image Node.
132 $form['image_node_options']['lightbox2_image_node'] = array(
133 '#type' => 'checkbox',
134 '#title' => t('Enable for Image Nodes'),
135 '#description' => t('Checking this box will enable automatic URL formatting for Image Nodes.'),
136 '#default_value' => variable_get('lightbox2_image_node', true),
137 );
138
139 // Add Checkbox for Image Node Grouping.
140 $form['image_node_options']['lightbox2_image_group'] = array(
141 '#type' => 'checkbox',
142 '#title' => t('Enable Grouping'),
143 '#description' => t('Checking this box will enable automatic grouping of Image Nodes on a page. Useful for image galleries.'),
144 '#default_value' => variable_get('lightbox2_image_group', true),
145 );
146
147 // Add Checkbox for disabling lightbox for gallery lists
148 $form['image_node_options']['lightbox2_disable_nested_galleries'] = array(
149 '#type' => 'checkbox',
150 '#title' => t('Disable Lightbox for Gallery Lists'),
151 '#description' => t('Checking this box will disable the lightbox for images in gallery lists, but will still use the lightbox when viewing gallery images.'),
152 '#default_value' => variable_get('lightbox2_disable_nested_galleries', true),
153 );
154
155 // Add Checkbox for Gallery2 Image Filter.
156 $form['image_node_options']['lightbox2G2_filter'] = array(
157 '#type' => 'checkbox',
158 '#title' => t('Enable Gallery 2 Filter'),
159 '#description' => t('Checking this box will enable the Gallery 2 filter.'),
160 '#default_value' => variable_get('lightbox2G2_filter', true),
161 );
162
163 // Add Textarea for urls without lightbox
164 $form['image_node_options']['lightbox2_disable_these_urls'] = array(
165 '#type' => 'textarea',
166 '#title' => t('Disable LightBox for these urls'),
167 '#description' => t('List the urls where the lightbox should be disabled, e.g. "node/add/page". Put each url on a separate line. You can use the % character as a wildcard.'),
168 '#default_value' => variable_get('lightbox2_disable_these_urls', ''),
169 );
170
171 return system_settings_form($form);
172 }
173
174 /**
175 * Implementation of hook_filter().
176 */
177 function lightbox2_filter_tips($delta, $format, $long = false) {
178 return t('Image links from G2 are formatted for use with Lightbox.V2');
179 }
180
181 // Check to see if the G2 Filter is Enabled in Settings
182 if (variable_get('lightbox2G2_filter', true)) {
183 function lightbox2_filter($op, $delta = 0, $format = -1, $text = '') {
184 switch ($op) {
185 case 'list':
186 return array(0 => t('Lightbox filter'));
187
188 case 'description':
189 return t('Turns g2_filter links into Lighbox.V2 appropriate links');
190
191 case 'process':
192 $text = ' '. $text .' ';
193 $text = preg_replace('/ShowItem/', 'DownloadItem', $text);
194 $text = preg_replace('/target=""/', 'rel="lightbox"', $text);
195 $text = substr($text, 1, -1);
196 return $text;
197
198 default:
199 return $text;
200 }
201 }
202 }
203
204 /**
205 * Provides a link to the CSS stylesheet associated with this module.
206 * Provides a link to the JS file associated with this module.
207 */
208 function lightbox2_add_files() {
209 // Load required js and css files.
210 $path = drupal_get_path('module', 'lightbox2');
211
212 // Check to see if Lightbox2 Plus is enabled.
213 if (variable_get('lightbox2_plus', true)) {
214 if (function_exists('drupal_add_css')) {
215 drupal_add_css($path .'/lightbox.css');
216 }
217 else {
218 theme('add_style', $path .'/lightbox.css');
219 }
220
221 // Check to see if Libraries are installed correctly.
222 if (file_exists($path .'/js/prototype.js')) {
223 drupal_add_js($path .'/js/prototype.js');
224
225 // Check to see if the Image Node Option is enabled in settings.
226 if (variable_get('lightbox2_image_node', true)) {
227 if (variable_get('lightbox2_image_group', true)) {
228 if (variable_get('lightbox2_disable_nested_galleries', true)) {
229 drupal_add_js($path .'/js/image_nodes_galleries.js');
230 }
231 else {
232 drupal_add_js($path .'/js/image_nodes.js');
233 }
234 }
235 else {
236 if (variable_get('lightbox2_disable_nested_galleries', true)) {
237 drupal_add_js($path .'/js/image_nodes_nogroup_galleries.js');
238 }
239 else {
240 drupal_add_js($path .'/js/image_nodes_nogroup.js');
241 }
242 }
243 }
244 drupal_add_js($path .'/js/scriptaculous.js?load=effects');
245 drupal_add_js($path .'/js/lightbox.js');
246 // Future support for non-images will go here.
247 //drupal_add_js($path .'/js/lightbox_docs.js');
248 }
249
250 // Display warning message if Libraries aren't installed correctly.
251 else {
252 drupal_set_message(t('The script.aculo.us library is in not installed correctly. Please download from <a href="http://www.stellapowerdesign.net/scriptaculous.zip">http://www.stellapowerdesign.net/scriptaculous-drupal.zip</a>, follow the instructions in the Lightbox V.2 README.TXT file to copy the files to their correct locations.'), 'error');
253 }
254 }
255 // Load Lightbox Lite if Plus is not enabled.
256 else {
257 if (function_exists('drupal_add_css')) {
258 drupal_add_css($path .'/lightbox_lite.css');
259 }
260 else {
261 theme('add_style', $path .'/lightbox_lite.css');
262 }
263 drupal_add_js($path .'/js/lightbox_lite.js');
264 }
265 drupal_add_js($path .'/js/lightningload.js');
266 }
267
268 /**
269 * Implementation of hook_field_formatter_info().
270 * Adds certain lightbox+imagecache formatters to CCK image fields if the
271 * imagefield.module and the imagecache.module exist.
272 */
273 function lightbox2_field_formatter_info() {
274 $formatter = array();
275 if (module_exists('imagefield') && module_exists('imagecache')) {
276 $rules = _imagecache_get_presets();
277 foreach ($rules as $ruleid => $rulename) {
278 $formatters['lightbox2]['. $rulename] = array(
279 'label' => 'Lightbox2: '. $rulename,
280 'field types' => array('image'),
281 );
282 }
283 }
284
285 return $formatters;
286 }
287
288 /**
289 * Implementation of hook_field_formatter().
290 */
291 function lightbox2_field_formatter($field, $item, $formatter) {
292 if (module_exists('imagefield') && module_exists('imagecache')) {
293 if (!isset($item['fid'])) {
294 return '';
295 }
296 if (!$_LIGHTBOX2_INCLUDE && lightbox2_exclude_these_paths() != 1) {
297 lightbox2_add_files();
298 $_LIGHTBOX2_INCLUDE = true;
299 }
300 $file = _imagefield_file_load($item['fid']);
301 $item = array_merge($item, $file);
302 if (strpos($formatter, 'lightbox2][') !== false) {
303 list($null, $namespace) = explode('][', $formatter, 2);
304 $rules = _imagecache_get_presets();
305 if (in_array($namespace, (array) $rules)) {
306 return theme('imagefield_image_imagecache_lightbox2', $namespace, $field, $item);
307 }
308 }
309 }
310 }
311
312 /**
313 * Implementation of theme_imagefield_image_imagecache_lightbox2().
314 */
315 function theme_imagefield_image_imagecache_lightbox2($namespace, $field, $item, $attributes = NULL) {
316 $imagecache_path = file_create_url(file_directory_path() .'/imagecache/'. $namespace .'/'. $item['filepath']);
317
318 $rel = 'lightbox';
319 if (variable_get('lightbox2_image_group', TRUE)) {
320 $rel = 'lightbox[cck_image_nodes]';
321 }
322
323 $node_link = '';
324 if (!empty($item['nid'])) {
325 $node_link = '<br /><br />'. l(t('View Image Information'), 'node/'. $item['nid']);
326 }
327
328 $link_attributes = array(
329 'rel' => $rel,
330 'title' => $item['alt'] . $node_link);
331
332 $image = theme('imagecache', $namespace, $item['filepath'], $item['alt'], $item['title'], $attributes);
333 $output = l($image, $item['filepath'], $link_attributes, NULL, NULL, FALSE, TRUE);
334
335 return $output;
336 }
337
338 function lightbox2_exclude_these_paths() {
339 // Use lightbox if this url is not excluded
340 $disabled_urls = variable_get('lightbox2_disable_these_urls', '');
341
342 if (!empty($disabled_urls)) {
343 $urls = explode("\n", $disabled_urls);
344
345 foreach ($urls as $url) {
346 $url = rtrim($url);
347 $url = preg_replace('/%/', '.*?', preg_quote($url, '/'));
348 $pattern = '/^'. $url .'$/';
349 if (preg_match($pattern, $_REQUEST['q'])) {
350 return 1;
351 }
352 }
353 }
354
355 return 0;
356 }
357