added a try {} on the addLoadEvent and also updated the css div name for the
[project/lightbox2.git] / lightbox2.module
CommitLineData
89cbe47f 1<?php
dfd69821 2// $Id $
89cbe47f
M
3/**
4 * Enables the use of lightbox V2 which places images above your current page, not within.
5 * This frees you from the constraints of the layout, particularly column widths.
6 *
dfd69821 7 * This module is for Drupal 5.x only.
89cbe47f
M
8 *
9 * Module by: Mark Ashmead
10 * Mailto: bugzie@gmail.com
dfd69821 11 * Co-maintainer: Stella Power (http://drupal.org/user/66894)
19258fd7
M
12 *
13 * Image Node Support: Steve McKenzie
14 *
89cbe47f
M
15*/
16
17/**
c7cc1c1d
M
18 * GLOBAL VARIABLES
19 */
20$LIGHTBOX2_INCLUDE = false;
21
22/**
89cbe47f
M
23 * Displays the information for this module.
24 * Displays the help text for this module.
25 *
26 */
8d908419
M
27/**
28 * Implementation of hook_help().
29 */
89cbe47f
M
30function lightbox2_help($section) {
31 switch ($section) {
32 case 'admin/modules#description':
8d908419 33 return t('Enables Lightbox V2 for Drupal');
89cbe47f
M
34 case 'admin/help#lightbox2':
35 return t('<h3>Overview</h3>
a08f9415 36 <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>
89cbe47f 37 <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>
839ab564 38 <p align="justify"><h3>Usage</h3></p>
89cbe47f
M
39 <p>Add rel=&quot;lightbox&quot; attribute to any link tag to activate the lightbox. For example: </p>
40 <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>
41 <p>Optional: Use the title attribute if you want to show a caption.</p>
42 <p>If you have a set of related images that you would like to group, follow step one but additionally include a group name between square brackets in the rel attribute. For example:</p><p>&lt;a href="images/image-1.jpg" rel="lightbox[roadtrip]"&gt;image #1&lt;/a&gt;<br>
43&lt;a href="images/image-2.jpg" rel="lightbox[roadtrip]"&gt;image #2&lt;/a&gt;<br>
44&lt;a href="images/image-3.jpg" rel="lightbox[roadtrip]"&gt;image #3&lt;/a&gt;<br></p>
420c430f 45<p>No limits to the number of image sets per page or how many images are allowed in each set. Go nuts!</p>
839ab564
M
46 <p align="justify"><h3>Known Issues</h3></p>
47 <p>Image Issues - An issue has been identified with the loading of certain images when using the module. (closelabel.gif, expand.gif, loading.gif)</p>
420c430f
M
48 <p>If your installation of Drupal exists in the root of your domain, i.e., www.yourinstallation.com then you shouldn\'t have any problems. The issue only occurs when Drupal is installed in a subdirectory, i.e., www.yourinstallation.com/subdirectory.</p>
49 <p>If this is the case, you will need to edit the lightbox.js on lines 63, 64 and 65 to reflect the fully qualified URL of your images. In the above case, this would be as follows;</p>
50 <p>var fileLoadingImage = &quot;/modules/lightbox2/images/loading.gif&quot;;<br>
51 var fileBottomNavCloseImage = &quot;/modules/lightbox2/images/closelabel.gif&quot;;<br>
52 var fileBottomNavZoomImage = &quot;/modules/lightbox2/images/expand.gif&quot;; //Update to 2.02+</p>
53 <p>should be changed to</p>
54 <p>var fileLoadingImage = &quot;/subdirectory/modules/lightbox2/images/loading.gif&quot;;<br>
55 var fileBottomNavCloseImage = &quot;/subdirectory/modules/lightbox2/images/closelabel.gif&quot;;<br>
56 var fileBottomNavZoomImage = &quot;/subdirectory/modules/lightbox2/images/expand.gif&quot;; //Update to 2.02+</p>
a08f9415 57 <p>There may be other methods that can be used to achieve this, but this should be the simplest for those with little or no programming experience. If you choose to use Lightbox2 Lite option, then you will need to edit the lightbox_lite.js file in a similar manner on lines 39 and 40.</p>');
89cbe47f
M
58 break;
59 }
60}
89cbe47f 61
8d908419 62/**
dfd69821
SP
63* Implementation of hook_perm()
64* Define the permissions this module uses
65*/
66function lightbox2_perm() {
67 return array('administer lightbox2');
68}
69
70
71/**
72* Implementation of hook_access()
73*/
74function lightbox2_access($op, $node) {
75 return user_access('administer lightbox2');
76}
77
78/**
79* Implementation of hook_menu()
80*/
81function lightbox2_menu($may_cache) {
82 $items = array();
83
84 if ($may_cache) {
85 $items[] = array('path' => 'admin/settings/lightbox2',
86 'title' => t('Lightbox2'),
87 'callback' => 'drupal_get_form',
88 'callback arguments' => array('lightbox2_settings_form'),
89 'access' => user_access('administer lightbox2'),
90 'description' => t('Allows the user to configure the lightbox2 settings.'),
91 );
92 }
93 else {
94 }
95
96 return $items;
97}
98
99
100/**
c7cc1c1d
M
101 * Implementation of hook_nodeapi().
102 */
103function lightbox2_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
104 global $LIGHTBOX2_INCLUDE;
105
106 if ($op == "view" && !$LIGHTBOX2_INCLUDE) {
107 lightbox2_add_files();
108 $LIGHTBOX2_INCLUDE = true;
109 }
a08f9415
M
110 elseif ($node->type == "image" && !$LIGHTBOX2_INCLUDE) {
111 lightbox2_add_files();
112 $LIGHTBOX2_INCLUDE = true;
113 }
c7cc1c1d
M
114}
115
116/**
117 * Implementation of hook_settings().
118 */
dfd69821
SP
119function lightbox2_settings_form() {
120
744d4f60
M
121 // Define Lightbox2 Plus form.
122 $form["lightbox2_plus_options"] = array(
123 "#type" => "fieldset",
839ab564 124 "#title" => t("Lightbox2 Plus"),
aed433ff 125 "#collapsible" => TRUE,
dfd69821 126 "#collapsed" => FALSE,
744d4f60 127 );
839ab564 128 // Add Checkbox for Lightbox2 Plus.
744d4f60
M
129 $form["lightbox2_plus_options"]["lightbox2_plus"] = array(
130 "#type" => "checkbox",
131 "#title" => t("Use Lightbox2 Plus"),
839ab564 132 "#description" => t("Un-checking this box will enable Lightbox2 Lite."),
744d4f60 133 "#default_value" => variable_get("lightbox2_plus", true),
744d4f60
M
134 );
135 // Define Image Node Options form.
c7cc1c1d
M
136 $form["image_node_options"] = array(
137 "#type" => "fieldset",
839ab564 138 "#title" => t("Lightbox2 Plus Image Node options"),
aed433ff 139 "#collapsible" => TRUE,
dfd69821 140 "#collapsed" => TRUE,
c7cc1c1d 141 );
839ab564 142 // Add Checkbox for Image Node.
c7cc1c1d
M
143 $form["image_node_options"]["lightbox2_image_node"] = array(
144 "#type" => "checkbox",
145 "#title" => t("Enable for Image Nodes"),
839ab564 146 "#description" => t("Checking this box will enable automatic URL formatting for Image Nodes."),
c7cc1c1d 147 "#default_value" => variable_get("lightbox2_image_node", true),
c7cc1c1d 148 );
839ab564
M
149 // Add Checkbox for Image Node Grouping.
150 $form["image_node_options"]["lightbox2_image_group"] = array(
151 "#type" => "checkbox",
152 "#title" => t("Enable Grouping"),
a08f9415 153 "#description" => t("Checking this box will enable automatic grouping of Image Nodes on a page. Useful for image galleries."),
839ab564 154 "#default_value" => variable_get("lightbox2_image_group", true),
839ab564
M
155 );
156 // Add Checkbox for Gallery2 Image Filter.
744d4f60 157 $form["image_node_options"]["lightbox2G2_filter"] = array(
c7cc1c1d
M
158 "#type" => "checkbox",
159 "#title" => t("Enable Gallery 2 Filter"),
839ab564 160 "#description" => t("Checking this box will enable the Gallery 2 filter."),
744d4f60 161 "#default_value" => variable_get("lightbox2G2_filter", true),
c7cc1c1d 162 );
dfd69821
SP
163
164 $form["update"] = array(
165 '#type' => 'submit',
166 '#value' => t('Update'),
167 '#weight' => 3,
168 );
169
c7cc1c1d
M
170
171 return $form;
172}
dfd69821
SP
173
174function lightbox2_settings_form_submit($form_id, $form_values) {
175 if ($form_values['op'] == t('Update')) {
176 variable_set("lightbox2_plus", $form_values["lightbox2_plus"]);
177 variable_set("lightbox2G2_filter", $form_values["lightbox2G2_filter"]);
178 variable_set("lightbox2_image_node", $form_values["lightbox2_image_node"]);
179 variable_set("lightbox2_image_group", $form_values["lightbox2_image_group"]);
180 }
181}
182
c7cc1c1d
M
183/**
184 *Implementation of hook_filter().
185 */
186function lightbox2_filter_tips($delta, $format, $long = false) {
187 return t('Image links from G2 are formatted for use with Lightbox.V2');
188}
dfd69821 189
c7cc1c1d 190// Check to see if the G2 Filter is Enabled in Settings
744d4f60 191if (variable_get("lightbox2G2_filter", true)) {
c7cc1c1d
M
192function lightbox2_filter($op, $delta = 0, $format = -1, $text = '') {
193 switch ($op) {
194 case 'list':
195 return array(0 => t('Lightbox filter'));
196 case 'description':
197 return t('Turns g2_filter links into Lighbox.V2 appropriate links');
198 case 'process':
199 $text = ' ' . $text . ' ';
200 $text = preg_replace('/ShowItem/','DownloadItem',$text);
201 $text = preg_replace('/target=""/','rel="lightbox"',$text);
202 $text = substr($text, 1, -1);
203 return $text;
204 default:
205 return $text;
206 }
207 }
208}
209/**
8d908419
M
210 * Provides a link to the CSS stylesheet associated with this module.
211 * Provides a link to the JS file associated with this module.
212 */
213
214function lightbox2_add_files() {
215 // Load required js and css files.
216 $path = drupal_get_path('module', 'lightbox2');
9f3cb09e 217 // Check to see if Lightbox2 Plus is enabled.
744d4f60 218 if (variable_get("lightbox2_plus", true)) {
dfd69821
SP
219 if (function_exists('drupal_add_css')) {
220 drupal_add_css($path .'/lightbox.css');
221 }
222 else {
744d4f60 223 theme("add_style", $path . '/lightbox.css');
dfd69821 224 }
9f3cb09e 225 // Check to see if Libraries are installed correctly.
744d4f60 226 if (file_exists($path .'/js/prototype.js')) {
9f3cb09e
M
227 drupal_add_js($path . '/js/prototype.js');
228 // Check to see if the Image Node Option is enabled in settings.
839ab564 229 if (variable_get("lightbox2_image_node", true) && variable_get("lightbox2_image_group", true)) {
744d4f60
M
230 drupal_add_js($path . "/js/image_nodes.js");
231 }
839ab564
M
232 elseif (variable_get("lightbox2_image_node", true)) {
233 drupal_add_js($path . "/js/image_nodes_nogroup.js");
234 }
dbebedff 235 drupal_add_js($path . '/js/scriptaculous.js?load=effects');
9f3cb09e 236 drupal_add_js($path . '/js/lightbox.js');
d0e28b2f
M
237 // Future support for non-images will go here.
238 //drupal_add_js($path . '/js/lightbox_docs.js');
744d4f60 239 }
9f3cb09e
M
240 // Display warning message if Libraries aren't installed correctly.
241 else {
744d4f60
M
242 drupal_set_message(t('The script.aculo.us library is in not installed correctly. Please download from <a href="http://script.aculo.us/downloads">http://script.aculo.us/downloads</a>, follow the instructions in the Lightbox V.2 README.TXT file to copy the files to their correct locations.'), 'error');
243 }
244 }
9f3cb09e 245 // Load Lightbox Lite if Plus is not enabled.
744d4f60 246 else {
dfd69821
SP
247 if (function_exists('drupal_add_css')) {
248 drupal_add_css($path .'/lightbox_lite.css');
249 }
250 else {
251 theme("add_style", $path . '/lightbox_lite.css');
252 }
744d4f60 253 drupal_add_js($path . '/js/lightbox_lite.js');
8d056336 254 }
8d908419 255}