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