e06b150c7fb11586ba4a596c64a10c48cacc56a9
[project/gallery.git] / gallery_menu / gallery_menu.module
1 <?php
2 // $Id$
3
4 /**
5 * gallery_menu.module
6 */
7
8 /**
9 * Implementation of hook_menu().
10 */
11 function gallery_menu_menu($may_cache) {
12 $items = array();
13 if (variable_get('gallery_valid', 0)) {
14 if ($may_cache) {
15 $items[] = array(
16 'path' => 'admin/settings/gallery/menu',
17 'title' => t('Menu'),
18 'callback' => 'drupal_get_form',
19 'callback arguments' => '_gallery_menu_settings',
20 'access' => user_access('administer gallery settings'),
21 'type' => MENU_LOCAL_TASK,
22 'weight' => 6
23 );
24 }
25 else {
26 $items = gallery_menu_build_menu();
27 }
28 }
29
30 return $items;
31 }
32
33 /**
34 * Implementation of hook_enable().
35 */
36 function gallery_menu_enable() {
37 variable_set('gallery_page_callback', 'gallery_menu_page');
38 }
39
40 /**
41 * Implementation of hook_disable().
42 */
43 function gallery_menu_disable() {
44 variable_del('gallery_page_callback');
45 }
46
47 /**
48 * Function gallery_menu_page().
49 */
50 function gallery_menu_page() {
51 $result = gallery_page(TRUE);
52 if (isset($result['themeData']['pageUrl']['itemId'])) {
53 $id = $result['themeData']['pageUrl']['itemId'];
54 $item = gallery_item_details($id);
55 if ($item['type'] != 'GalleryAlbumItem') {
56 $id = $item['parent'];
57 }
58 list($ret, $parents) = GalleryCoreApi::fetchParentSequence($id);
59 if ($ret) {
60 gallery_error(t('Error fetching item parents'), $ret);
61 }
62 else {
63 array_shift($parents);
64 $path = 'gallery/'. (count($parents) ? implode('/', $parents) .'/' : '') . $id;
65 menu_set_active_item($path);
66 }
67 }
68
69 return $result['bodyHtml'];
70 }
71
72 /**
73 * Function gallery_menu_album().
74 */
75 function gallery_menu_album($id) {
76 // Redirect to true album url (from the virtual menu path)
77 $url = gallery_generate_url(array('itemId' => $id), FALSE);
78 drupal_goto($url);
79 }
80
81 /**
82 * Function gallery_menu_build_menu().
83 */
84 function gallery_menu_build_menu() {
85 global $user;
86 $items = array();
87 if (!_gallery_init(TRUE)) {
88 return $items;
89 }
90
91 $depth = variable_get('gallery_menu_depth', 0);
92 $tree = _gallery_menu_album_tree(NULL, $depth ? $depth : NULL);
93 $cid = 'gallery_menu:'. $user->uid .':'. md5(serialize($tree));
94 if ($cache = cache_get($cid)) {
95 $items = unserialize($cache->data);
96 }
97 else {
98 _gallery_menu_traverse($tree, $items);
99 cache_set($cid, 'cache', serialize($items), CACHE_TEMPORARY);
100 }
101
102 GalleryEmbed::done();
103 return $items;
104 }
105
106 /**
107 * Function _gallery_menu_album_tree().
108 */
109 function _gallery_menu_album_tree($root = NULL, $depth = NULL, $user = NULL) {
110 list($ret, $tree) = GalleryCoreApi::fetchAlbumTree($root, $depth, $user);
111 if ($ret) {
112 gallery_error(t('Error fetching album tree'), $ret);
113 return FALSE;
114 }
115
116 return $tree;
117 }
118
119 /**
120 * Function _gallery_menu_traverse().
121 */
122 function _gallery_menu_traverse(&$tree, &$items) {
123 static $parents = array();
124 foreach (array_keys($tree) as $id) {
125 $album = gallery_item_details($id);
126 if (variable_get('gallery_menu_show_'. $id, 1)) {
127 $path = 'gallery/'. (count($parents) ? implode('/', $parents) .'/' : '') . $id;
128 $items[] = array(
129 'path' => $path,
130 'title' => htmlspecialchars_decode($album['title']),
131 'callback' => 'gallery_menu_album',
132 'callback arguments' => array($id),
133 'access' => user_access('access gallery'),
134 'type' => MENU_DYNAMIC_ITEM
135 );
136 if (count($tree[$id])) {
137 array_push($parents, $id);
138 _gallery_menu_traverse($tree[$id], $items);
139 array_pop($parents);
140 }
141 }
142 $tree[$id] += $album;
143 }
144 }
145
146 /**
147 * Function _gallery_menu_settings().
148 */
149 function _gallery_menu_settings() {
150 $form['menu'] = array(
151 '#type' => 'fieldset',
152 '#title' => t('Gallery menu settings'),
153 '#collapsible' => FALSE,
154 '#collapsed' => FALSE,
155 );
156 $form['menu']['gallery_menu_depth'] = array(
157 '#type' => 'textfield',
158 '#title' => t('Depth of Gallery albums'),
159 '#default_value' => variable_get('gallery_menu_depth', 0),
160 '#description' => 'Depth of album hierarchy to include (\'0\' for infinite).'
161 );
162
163 // Item visibility settings
164 $form['menu']['items'] = array(
165 '#type' => 'fieldset',
166 '#title' => t('Menu Items'),
167 '#collapsible' => TRUE,
168 '#collapsed' => TRUE
169 );
170 $form['menu']['items'][] = _gallery_menu_settings_table();
171
172 $form['#submit']['_gallery_menu_settings_submit'] = array();
173 $form['#submit']['system_settings_form_submit'] = array();
174 return system_settings_form($form);
175 }
176
177 /**
178 * Function _gallery_menu_settings_submit().
179 */
180 function _gallery_menu_settings_submit($form_id, $form_values) {
181 cache_clear_all('gallery_menu:', 'cache', TRUE);
182 }
183
184 /**
185 * Function _gallery_menu_settings_table().
186 */
187 function _gallery_menu_settings_table() {
188 if (!_gallery_init(TRUE)) {
189 return array();
190 }
191
192 $depth = variable_get('gallery_menu_depth', 0);
193 $tree = _gallery_menu_album_tree(NULL, $depth ? $depth : NULL);
194
195 $form = _gallery_menu_settings_traverse($tree);
196 $form['#theme'] = 'gallery_menu_settings_table';
197
198 GalleryEmbed::done();
199 return $form;
200 }
201
202 /**
203 * Function _gallery_menu_album_traverse().
204 */
205 function _gallery_menu_settings_traverse(&$tree) {
206 static $parents = array();
207 foreach (array_keys($tree) as $id) {
208 $album = gallery_item_details($id);
209 $enabled = variable_get('gallery_menu_show_'. $id, 1);
210 $form[$id]['title'] = array('#value' => implode('', $parents) .' '. $album['title']);
211 $form[$id]['checkbox']['gallery_menu_show_'. $id] = array(
212 '#type' => 'checkbox',
213 '#title' => '',
214 '#default_value' => $enabled
215 );
216 if (count($tree[$id]) && $enabled) {
217 array_push($parents, '-');
218 $form[$id]['children'] = _gallery_menu_settings_traverse($tree[$id]);
219 array_pop($parents);
220 }
221 $tree[$id] += $album;
222 }
223
224 return $form;
225 }
226
227 /**
228 * Theme function : theme_gallery_menu_settings_table().
229 */
230 function theme_gallery_menu_settings_table($form, $rows = array()) {
231 static $depth = 0;
232 foreach (element_children($form) as $key) {
233 if (isset($form[$key]['title'])) {
234 $row = array();
235 $row[] = drupal_render($form[$key]['title']);
236 $row[] = drupal_render($form[$key]['checkbox']);
237 $rows[] = $row;
238 }
239 if (isset($form[$key]['children'])) {
240 $depth++;
241 $rows = theme_gallery_menu_settings_table($form[$key]['children'], $rows);
242 }
243 }
244
245 $depth--;
246 return ($depth < 0) ? theme('table', array(t('Album'), t('Show')), $rows) : $rows;
247 }