/[drupal]/contributions/modules/spritemenu/spritemenu.module
ViewVC logotype

Contents of /contributions/modules/spritemenu/spritemenu.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download) (as text)
Thu Dec 25 21:18:38 2008 UTC (10 months, 4 weeks ago) by hanoii
Branch: MAIN
CVS Tags: DRUPAL-5--1-0, HEAD
Branch point for: DRUPAL-5, DRUPAL-6--1
File MIME type: text/x-php
Initial commit. This module let you upload a sprite image for a menu item and, with the use of CSS, configure different sprites of the image for different states of the link (normal, visited, hover and active).
1 <?php
2 // $Id$
3
4 /**
5 * @file
6 * Add an option to upload an image file for a menu, it can contains both normal
7 * (top, left) and hover, (bottom, left).
8 *
9 */
10
11 function _spritemenu_css_path() {
12 $directory = file_directory_path() .'/'. variable_get('spritemenu_path', 'spritemenu');
13 file_check_directory($directory, FILE_CREATE_DIRECTORY);
14 return $directory;
15
16 }
17
18 function _spritemenu_css_filepath() {
19 return file_create_path(_spritemenu_css_path() .'/spritemenu.css');
20 }
21
22 function _spritemenu_get_mid($path, $pid = null) {
23 static $menu;
24
25 if (!$menu) {
26 $menu = menu_get_menu();
27 }
28
29 if (!$pid) {
30 $mid = $menu['path index'][$path];
31 } else {
32 $items = $menu['items'];
33 foreach ($items as $key => $item) {
34 if ($item['pid'] == $pid && $item['path'] == $path) {
35 $mid = $key;
36 break;
37 }
38 }
39 }
40
41 return $mid;
42 }
43
44 function _spritemenu_build_css() {
45 $css = _spritemenu_css_filepath();
46 $result = db_query("SELECT * FROM {spritemenu}");
47
48 if (db_num_rows($result)) {
49 $f = fopen($css, 'w+');
50
51 while ($spritemenu = db_fetch_object($result)) {
52 $img = basename($spritemenu->filepath);
53 $css = '';
54 //$css .= "a#menu-{$spritemenu->mid} { display:block; height: 25px; width: 100%; background:transparent url($img) no-repeat scroll 0 0; }\n";
55 $position = $spritemenu->link;
56 $css .= "a#menu-{$spritemenu->mid}, a#menu-{$spritemenu->mid}-active { text-decoration: none; width: 100%; background: transparent url($img) no-repeat scroll $position; }\n";
57 $css .= "a#menu-{$spritemenu->mid} span, a#menu-{$spritemenu->mid}-active span { visibility: hidden; }\n";
58 if ($position = $spritemenu->visited) {
59 $css .= "a#menu-{$spritemenu->mid}:visited, a#menu-{$spritemenu->mid}-visited:visited { background-position: $position; }\n";
60 }
61 if ($position = $spritemenu->hover) {
62 $css .= "a#menu-{$spritemenu->mid}:hover, a#menu-{$spritemenu->mid}-active:hover { background-position: $position; }\n";
63 }
64 if ($position = $spritemenu->active) {
65 // $css .= "a#menu-{$spritemenu->mid}[class~=\"active\"] { text-decoration: none; background-position: $position; }\n";
66 $css .= "a#menu-{$spritemenu->mid}-active { text-decoration: none; width: 100%; background: transparent url($img) no-repeat scroll $position; }\n";
67 }
68 fwrite($f, $css);
69 }
70
71 fclose($f);
72 }
73 else {
74 // remove empty css
75 file_delete($css);
76 }
77 }
78
79
80 function spritemenu_menu($may_cache) {
81 $items = array();
82
83 if (!$may_cache) {
84 $css = _spritemenu_css_filepath();
85 if (file_exists($css)) {
86 drupal_add_css($css);
87 }
88 }
89
90 if ($may_cache) {
91 }
92
93 return $items;
94 }
95
96 /**
97 * Implementation of hook_form_alter().
98 */
99 function spritemenu_form_alter($form_id, &$form) {
100 if ($form_id == 'menu_item_delete_form' && ($mid = $form['mid']['#value']) && $form['#post'] && $form['#post']['confirm']) {
101 $spritemenu = db_fetch_object(db_query("SELECT * FROM {spritemenu} WHERE mid = %d", $mid));
102 if ($spritemenu->filepath) {
103 file_delete($spritemenu->filepath);
104 }
105 db_query("DELETE FROM {spritemenu} WHERE mid = %d", $mid);
106 drupal_set_message(t('Sprite Graphic Menu: Menu information deleted.'));
107 _spritemenu_build_css();
108 }
109
110 if ($form_id == 'menu_edit_item_form' && ($mid = $form['mid']['#value'])) {
111 // move submit to the end
112 $form['submit']['#weight'] = 2;
113
114 $form['#attributes'] = array('enctype' => 'multipart/form-data');
115
116 $spritemenu = db_fetch_object(db_query("SELECT * FROM {spritemenu} WHERE mid = %d", $mid));
117
118 $form['spritemenu'] = array(
119 '#type' => 'fieldset',
120 '#title' => t('Sprite Graphic Menu'),
121 '#collapsible' => TRUE,
122 '#collapsed' => !($spritemenu),
123 '#weight' => 1,
124 );
125
126 if ($spritemenu) {
127 $form['spritemenu']['image_display'] = array(
128 '#type' => 'markup',
129 '#value' => theme_image($spritemenu->filepath),
130 );
131 $form['spritemenu']['image_remove'] = array(
132 '#type' => 'checkbox',
133 '#title' => t('Remove'),
134 '#description' => t('Check this box and then submit to remove the image from disk and from this menu entry.'),
135 );
136 }
137
138 $form['spritemenu']['image'] = array(
139 '#type' => 'file',
140 '#title' => t('Image file'),
141 );
142
143 $form['spritemenu']['link'] = array(
144 '#type' => 'textfield',
145 '#title' => t('Link'),
146 '#description' => t('Enter the CSS positioning corresponding to the sprite of the image you want to use.'),
147 '#default_value' => ($spritemenu->link ? $spritemenu->link : 'left top'),
148 '#required' => TRUE,
149 );
150 $form['spritemenu']['visited'] = array(
151 '#type' => 'textfield',
152 '#title' => t('Visited'),
153 '#description' => t('Enter the CSS positioning corresponding to the sprite of the image you want to use.'),
154 '#default_value' => ($spritemenu->visited ? $spritemenu->visited: ''),
155 );
156 $form['spritemenu']['hover'] = array(
157 '#type' => 'textfield',
158 '#title' => t('Hover'),
159 '#description' => t('Enter the CSS positioning corresponding to the sprite of the image you want to use.'),
160 '#default_value' => ($spritemenu->hover ? $spritemenu->hover: 'left bottom'),
161 );
162 $form['spritemenu']['active'] = array(
163 '#type' => 'textfield',
164 '#title' => t('Active'),
165 '#description' => t('Enter the CSS positioning corresponding to the sprite of the image you want to use. This uses the active class of the link, not the CSS :active pseudo class.'),
166 '#default_value' => ($spritemenu->active ? $spritemenu->active: ''),
167 );
168
169 if ($form['#post']) {
170 $file = file_save_upload('image', _spritemenu_css_path());
171 if ($file) {
172 $filepath = $file->filepath;
173 }
174 else {
175 $filepath = $spritemenu->filepath;
176 }
177 if ($file || $spritemenu && ($spritemenu->link != $form['#post']['link'] || $spritemenu->visited != $form['#post']['visited'] || $spritemenu->hover != $form['#post']['hover'] || $spritemenu->active != $form['#post']['active'])) {
178 if (!$spritemenu) {
179 db_query(
180 "INSERT INTO {spritemenu}
181 (mid, path, filepath, link, visited, hover, active)
182 VALUES (%d, '%s', '%s', '%s', '%s', '%s', '%s')",
183 $mid,
184 $form['#post']['path'] ? $form['#post']['path'] : $form['path']['#value'],
185 $filepath,
186 $form['#post']['link'],
187 $form['#post']['visited'],
188 $form['#post']['hover'],
189 $form['#post']['active']
190 );
191 }
192 else {
193 if ($file && $spritemenu->filepath && $spritemenu->filepath != $filepath) {
194 file_delete($spritemenu->filepath);
195 }
196 db_query(
197 "UPDATE {spritemenu} SET
198 path = '%s', filepath = '%s', link = '%s', visited = '%s', hover = '%s', active = '%s'
199 WHERE mid = %d",
200 $form['#post']['path'] ? $form['#post']['path'] : $form['path']['#value'],
201 $filepath,
202 $form['#post']['link'],
203 $form['#post']['visited'],
204 $form['#post']['hover'],
205 $form['#post']['active'],
206 $mid
207 );
208 }
209 $build = TRUE;
210 }
211 else {
212 // check if the user wants to remove the image
213 if ($form['#post']['image_remove']) {
214 db_query("DELETE FROM {spritemenu} WHERE mid = %d", $mid);
215 file_delete($spritemenu->filepath);
216 $build = TRUE;
217 }
218 }
219
220 if ($build) {
221 drupal_set_message(t('Sprite Graphic Menu: Information saved and CSS reubilt. Please Ctrl+Refresh the page to reflect the changes.'));
222 _spritemenu_build_css();
223 }
224 }
225 }
226
227 if ($form_id == 'menu_configure') {
228 $form['spritemenu'] = array(
229 '#type' => 'fieldset',
230 '#title' => t('Sprite Graphic Menu'),
231 '#weight' => 0,
232 );
233
234 $form['spritemenu']['spritemenu_path'] = array(
235 '#type' => 'textfield',
236 '#title' => t('Directory'),
237 '#description' => t('A directory that will reside inside the files directory'),
238 '#default_value' => variable_get('spritemenu_path', 'spritemenu'),
239 );
240 }
241 }
242
243 /**
244 * Generate the HTML representing a given menu item ID.
245 *
246 * @param $item
247 * The menu item to render.
248 * @param $link_item
249 * The menu item which should be used to find the correct path.
250 *
251 * @ingroup themeable
252 */
253 function phptemplate_menu_item_link($item, $link_item) {
254 return theme('spritemenu_menu_item_link', $item, $link_item);
255 }
256
257 /**
258 * Return a themed set of links.
259 *
260 * @param $links
261 * A keyed array of links to be themed.
262 * @param $attributes
263 * A keyed array of attributes
264 * @return
265 * A string containing an unordered list of links.
266 */
267 function phptemplate_links($links, $attributes = array('class' => 'links')) {
268 return theme('spritemenu_links', $links, $attributes);
269 }
270
271 /**
272 * Return a themed set of links.
273 *
274 * @param $links
275 * A keyed array of links to be themed.
276 * @param $attributes
277 * A keyed array of attributes
278 * @return
279 * A string containing an unordered list of links.
280 */
281 function theme_spritemenu_links($links, $attributes = array('class' => 'links')) {
282 $output = '';
283
284 if (count($links) > 0) {
285 $output = '<ul'. drupal_attributes($attributes) .'>';
286
287 $num_links = count($links);
288 $i = 1;
289
290 foreach ($links as $key => $link) {
291 $title = $link['title'];
292 if (substr($key, 0, 4) == 'menu') {
293 $menu = split('-', $key);
294 $mid = _spritemenu_get_mid($link['href'], $menu[3]);
295 if ($mid) {
296 $link['attributes']['id'] = 'menu-'. $mid;
297 if (($item['path'] == $_GET['q']) || ($item['path'] == '<front>' && drupal_is_front_page())) {
298 $link['attributes']['id'] .= '-active';
299 }
300
301 $spritemenu = db_fetch_object(db_query("SELECT * FROM {spritemenu} WHERE mid = %d", $mid));
302 if ($spritemenu->filepath) {
303 $title = '<span>'. $link['title'] .'</span>';
304 $link['html'] = TRUE;
305 }
306 }
307 }
308 $class = $key;
309
310 // Automatically add a class to each link and also to each LI
311 if (isset($link['attributes']) && isset($link['attributes']['class'])) {
312 $link['attributes']['class'] .= ' ' . $key;
313 }
314 else {
315 $link['attributes']['class'] = $key;
316 }
317
318 // Add first and last classes to the list of links to help out themers.
319 $extra_class = '';
320 if ($i == 1) {
321 $extra_class .= 'first ';
322 }
323 if ($i == $num_links) {
324 $extra_class .= 'last ';
325 }
326 $output .= '<li '. drupal_attributes(array('class' => $extra_class . $class)) .'>';
327
328 // Is the title HTML?
329 $html = isset($link['html']) && $link['html'];
330
331 // Initialize fragment and query variables.
332 $link['query'] = isset($link['query']) ? $link['query'] : NULL;
333 $link['fragment'] = isset($link['fragment']) ? $link['fragment'] : NULL;
334
335 if (isset($link['href'])) {
336 $output .= l($title, $link['href'], $link['attributes'], $link['query'], $link['fragment'], FALSE, $html);
337 }
338 else if ($link['title']) {
339 //Some links are actually not links, but we wrap these in <span> for adding title and class attributes
340 if (!$html) {
341 $link['title'] = check_plain($link['title']);
342 }
343 $output .= '<span'. drupal_attributes($link['attributes']) .'>'. $link['title'] .'</span>';
344 }
345
346 $i++;
347 $output .= "</li>\n";
348 }
349
350 $output .= '</ul>';
351 }
352
353 return $output;
354 }
355
356 /**
357 * Generate the HTML representing a given menu item ID.
358 *
359 * @param $item
360 * The menu item to render.
361 * @param $link_item
362 * The menu item which should be used to find the correct path.
363 *
364 * @ingroup themeable
365 */
366 function theme_spritemenu_menu_item_link($item, $link_item) {
367 $title = $item['title'];
368 $attributes = array();
369
370 $mid = _spritemenu_get_mid($item['path'], $item['pid']);
371 if ($mid) {
372 $attributes['id'] = 'menu-'. $mid;
373 if (($item['path'] == $_GET['q']) || ($item['path'] == '<front>' && drupal_is_front_page())) {
374 $attributes['id'] .= '-active';
375 }
376
377 $spritemenu = db_fetch_object(db_query("SELECT * FROM {spritemenu} WHERE mid = %d", $mid));
378 if ($spritemenu->filepath) {
379 $title = '<span>'. $item['title'] .'</span>';
380 }
381 }
382 if (!empty($item['description'])) {
383 $attributes['title'] = $item['description'];
384 }
385
386
387 return l($title, $link_item['path'], $attributes, isset($item['query']) ? $item['query'] : NULL, NULL, FALSE, TRUE);
388 }
389
390 function spritemenu_css() {
391 _spritemenu_build_css();
392 return 'a';
393 }

  ViewVC Help
Powered by ViewVC 1.1.2