modified code so it no longer clashes with jQuery which is included with drupal
[project/lightbox2.git] / lightbox2.module
1 <?php
2 // $Id $
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 *
7 * This module is for Drupal 5.x only.
8 *
9 * Module by: Mark Ashmead
10 * Mailto: bugzie@gmail.com
11 * Co-maintainer: Stella Power (http://drupal.org/user/66894)
12 *
13 * Image Node Support: Steve McKenzie
14 *
15 */
16
17 /**
18 * GLOBAL VARIABLES
19 */
20 $LIGHTBOX2_INCLUDE = false;
21
22 /**
23 * Displays the information for this module.
24 * Displays the help text for this module.
25 *
26 */
27 /**
28 * Implementation of hook_help().
29 */
30 function lightbox2_help($section) {
31 switch ($section) {
32 case 'admin/modules#description':
33 return t('Enables Lightbox V2 for Drupal');
34 case 'admin/help#lightbox2':
35 return t('<h3>Overview</h3>
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>
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>
38 <p align="justify"><h3>Usage</h3></p>
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>
45 <p>No limits to the number of image sets per page or how many images are allowed in each set. Go nuts!</p>
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>
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>
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>');
58 break;
59 }
60 }
61
62 /**
63 * Implementation of hook_perm()
64 * Define the permissions this module uses
65 */
66 function lightbox2_perm() {
67 return array('administer lightbox2');
68 }
69
70
71 /**
72 * Implementation of hook_access()
73 */
74 function lightbox2_access($op, $node) {
75 return user_access('administer lightbox2');
76 }
77
78 /**
79 * Implementation of hook_menu()
80 */
81 function 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 /**
101 * Implementation of hook_nodeapi().
102 */
103 function 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 }
110 elseif ($node->type == "image" && !$LIGHTBOX2_INCLUDE) {
111 lightbox2_add_files();
112 $LIGHTBOX2_INCLUDE = true;
113 }
114 }
115
116 /**
117 * Implementation of hook_settings().
118 */
119 function lightbox2_settings_form() {
120
121 // Define Lightbox2 Plus form.
122 $form["lightbox2_plus_options"] = array(
123 "#type" => "fieldset",
124 "#title" => t("Lightbox2 Plus"),
125 "#collapsible" => TRUE,
126 "#collapsed" => FALSE,
127 );
128 // Add Checkbox for Lightbox2 Plus.
129 $form["lightbox2_plus_options"]["lightbox2_plus"] = array(
130 "#type" => "checkbox",
131 "#title" => t("Use Lightbox2 Plus"),
132 "#description" => t("Un-checking this box will enable Lightbox2 Lite."),
133 "#default_value" => variable_get("lightbox2_plus", true),
134 );
135 // Define Image Node Options form.
136 $form["image_node_options"] = array(
137 "#type" => "fieldset",
138 "#title" => t("Lightbox2 Plus Image Node options"),
139 "#collapsible" => TRUE,
140 "#collapsed" => TRUE,
141 );
142 // Add Checkbox for Image Node.
143 $form["image_node_options"]["lightbox2_image_node"] = array(
144 "#type" => "checkbox",
145 "#title" => t("Enable for Image Nodes"),
146 "#description" => t("Checking this box will enable automatic URL formatting for Image Nodes."),
147 "#default_value" => variable_get("lightbox2_image_node", true),
148 );
149 // Add Checkbox for Image Node Grouping.
150 $form["image_node_options"]["lightbox2_image_group"] = array(
151 "#type" => "checkbox",
152 "#title" => t("Enable Grouping"),
153 "#description" => t("Checking this box will enable automatic grouping of Image Nodes on a page. Useful for image galleries."),
154 "#default_value" => variable_get("lightbox2_image_group", true),
155 );
156 // Add Checkbox for Gallery2 Image Filter.
157 $form["image_node_options"]["lightbox2G2_filter"] = array(
158 "#type" => "checkbox",
159 "#title" => t("Enable Gallery 2 Filter"),
160 "#description" => t("Checking this box will enable the Gallery 2 filter."),
161 "#default_value" => variable_get("lightbox2G2_filter", true),
162 );
163
164 $form["update"] = array(
165 '#type' => 'submit',
166 '#value' => t('Update'),
167 '#weight' => 3,
168 );
169
170
171 return $form;
172 }
173
174 function 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
183 /**
184 *Implementation of hook_filter().
185 */
186 function lightbox2_filter_tips($delta, $format, $long = false) {
187 return t('Image links from G2 are formatted for use with Lightbox.V2');
188 }
189
190 // Check to see if the G2 Filter is Enabled in Settings
191 if (variable_get("lightbox2G2_filter", true)) {
192 function 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 /**
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
214 function lightbox2_add_files() {
215 // Load required js and css files.
216 $path = drupal_get_path('module', 'lightbox2');
217 // Check to see if Lightbox2 Plus is enabled.
218 if (variable_get("lightbox2_plus", true)) {
219 if (function_exists('drupal_add_css')) {
220 drupal_add_css($path .'/lightbox.css');
221 }
222 else {
223 theme("add_style", $path . '/lightbox.css');
224 }
225 // Check to see if Libraries are installed correctly.
226 if (file_exists($path .'/js/prototype.js')) {
227 drupal_add_js($path . '/js/prototype.js');
228 // Check to see if the Image Node Option is enabled in settings.
229 if (variable_get("lightbox2_image_node", true) && variable_get("lightbox2_image_group", true)) {
230 drupal_add_js($path . "/js/image_nodes.js");
231 }
232 elseif (variable_get("lightbox2_image_node", true)) {
233 drupal_add_js($path . "/js/image_nodes_nogroup.js");
234 }
235 drupal_add_js($path . '/js/scriptaculous.js?load=effects');
236 drupal_add_js($path . '/js/lightbox.js');
237 // Future support for non-images will go here.
238 //drupal_add_js($path . '/js/lightbox_docs.js');
239 }
240 // Display warning message if Libraries aren't installed correctly.
241 else {
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 }
245 // Load Lightbox Lite if Plus is not enabled.
246 else {
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 }
253 drupal_add_js($path . '/js/lightbox_lite.js');
254 }
255 }