/[drupal]/contributions/modules/spaces/spaces_ui.module
ViewVC logotype

Contents of /contributions/modules/spaces/spaces_ui.module

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


Revision 1.3 - (show annotations) (download) (as text)
Mon Oct 6 21:56:40 2008 UTC (13 months, 2 weeks ago) by yhahn
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +270 -51 lines
File MIME type: text/x-php
Beginning merge of Spaces 2 to DRUPAL-6 branch
1 <?php
2
3 /**
4 * Implementation of hook_init().
5 */
6 function spaces_ui_init() {
7 if (arg(0) .'/'. arg(1) .'/'. arg(2) .'/'. arg(3) == 'admin/build/spaces/features') {
8 include_once(drupal_get_path("module", "context_ui") ."/context_ui_admin.inc");
9 }
10 }
11
12 /**
13 * Implementation of hook_menu().
14 */
15 function spaces_ui_menu($may_cache) {
16 $items = array();
17 if ($may_cache) {
18 $items[] = array(
19 'path' => 'admin/build/spaces/features',
20 'title' => t('Features'),
21 'description' => t('Page listing spaces features.'),
22 'callback' => 'spaces_ui_features',
23 'access' => user_access('administer spaces'),
24 'type' => MENU_LOCAL_TASK,
25 );
26 $items[] = array(
27 'path' => 'admin/build/spaces/features/list',
28 'title' => t('List'),
29 'description' => t('Page listing spaces features.'),
30 'callback' => 'spaces_ui_features',
31 'access' => user_access('administer spaces'),
32 'type' => MENU_DEFAULT_LOCAL_TASK,
33 'weight' => 0,
34 );
35 $items[] = array(
36 'path' => 'admin/build/spaces/features/add',
37 'title' => t('Add'),
38 'description' => t('Add a new feature.'),
39 'callback' => 'drupal_get_form',
40 'callback arguments' => array('context_ui_form', 'add'),
41 'access' => user_access('administer spaces'),
42 'type' => MENU_LOCAL_TASK,
43 'weight' => 1,
44 );
45 }
46 return $items;
47 }
48
49 /**
50 * Implementation of hook_context_define().
51 */
52 function spaces_ui_context_define() {
53 $items = array();
54 $result = db_query('SELECT feature, value FROM {spaces_features_ui}');
55 while ($row = db_fetch_object($result)) {
56 $c = new StdClass();
57 $c->namespace = 'spaces';
58 $c->attribute = 'feature';
59 $c->value = $row->feature;
60
61 if ($c = context_ui_context('load', $c)) {
62 $c->spaces = unserialize($row->value);
63 // A small change in context_ui now allows modules to
64 // set system/status explicitly -- not recommended except for
65 // cases like this.
66 $c->system = 0;
67 $c->status = 1;
68
69 $items[] = (array) $c;
70 }
71 }
72 return $items;
73 }
74
75 /**
76 * Implementation of hook_form_alter()
77 */
78 function spaces_ui_form_alter($form_id, &$form) {
79 switch ($form_id) {
80 case 'context_ui_delete_confirm':
81 $cid = $form['cid']['#value'];
82 $context = context_ui_context('load', $cid);
83 if ($context->namespace == 'spaces' && $context->attribute == 'feature') {
84 $form['spaces_feature'] = array(
85 '#type' => 'value',
86 '#value' => $context->value,
87 );
88 $form['#submit']['spaces_ui_feature_delete'] = array();
89 $form['#submit'] = array_reverse($form['#submit']);
90 }
91 break;
92 case 'context_ui_form':
93 // Determine whether we should treat this context as a spaces feature
94 if ($form['namespace']['#default_value'] == 'spaces' && $form['attribute']['#default_value'] == 'feature') {
95 $is_feature = TRUE;
96 }
97 else {
98 $is_feature = FALSE;
99 }
100
101 // Load feature information: first attempt from DB, then fallback
102 // to code definition.
103 if (!empty($form['value']['#default_value'])) {
104 $id = $form['value']['#default_value'];
105 $row = db_fetch_object(db_query('SELECT * FROM {spaces_features_ui} WHERE feature = "%s"', $id));
106 if ($row) {
107 $feature = unserialize($row->value);
108 }
109 else {
110 $features = spaces_features();
111 if (isset($features[$id])) {
112 $feature = $features[$id]->spaces;
113 }
114 }
115 }
116
117 // Add Feature information to context form
118 $form['feature'] = array(
119 '#type' => 'fieldset',
120 '#title' => t('Spaces feature'),
121 '#weight' => -5,
122 '#tree' => true,
123 );
124 $form['feature']['label'] = array(
125 '#type' => 'textfield',
126 '#title' => t('Label'),
127 '#description' => t('Name of the feature.'),
128 '#required' => true,
129 '#maxlength' => 30,
130 '#default_value' => isset($feature['label']) ? $feature['label'] : '',
131 );
132 $form['feature']['description'] = array(
133 '#type' => 'textfield',
134 '#title' => t('Description'),
135 '#description' => t('A brief description of the feature.'),
136 '#required' => true,
137 '#default_value' => isset($feature['description']) ? $feature['description'] : '',
138 );
139
140 // Space type compatibility
141 $options = array(0 => t('All spaces'));
142 foreach (spaces_types() as $type => $info) {
143 $options[$type] = $info['title'];
144 }
145 $form['feature']['types'] = array(
146 '#type' => 'select',
147 '#title' => t('Compatibility'),
148 '#description' => t('Choose the space type compatible with this feature.'),
149 '#required' => true,
150 '#options' => $options,
151 '#default_value' => isset($feature['types']) ? $feature['types'] : 0,
152 );
153
154 // Spaces feature menu
155 $menu_items = isset($feature['menu']) ? array_values($feature['menu']) : array();
156 $menu_paths = isset($feature['menu']) ? array_keys($feature['menu']) : array();
157 $form['feature']['menu'] = array(
158 '#tree' => true,
159 '#theme' => 'spaces_ui_feature_form_menu',
160 );
161 $form['feature']['menu']['help'] = array(
162 '#title' => t('Feature menu'),
163 '#type' => 'item',
164 '#description' => t('Define a menu for this feature. Items will be arranged into a tree based on path. <strong>Example:</strong> "gallery/browse" will become a child item of "gallery".'),
165 );
166 for ($i = 0; $i < 10; $i++) {
167 $form['feature']['menu'][$i] = array('#tree' => true);
168 $form['feature']['menu'][$i]['path'] =
169 $form['feature']['menu'][$i]['title'] = array(
170 '#type' => 'textfield',
171 '#size' => 30,
172 );
173 $form['feature']['menu'][$i]['path']['#default_value'] = isset($menu_paths[$i]) ? $menu_paths[$i] : '';
174 $form['feature']['menu'][$i]['title']['#default_value'] = isset($menu_items[$i]['title']) ? $menu_items[$i]['title'] : '';
175 }
176
177 // Force key and space to be 'feature', 'spaces' so that the spaces module knows what to do.
178 $form['attribute']['#disabled'] = true;
179 $form['attribute']['#default_value'] = 'feature';
180 $form['namespace']['#disabled'] = true;
181 $form['namespace']['#default_value'] = 'spaces';
182
183 $form['#submit']['spaces_ui_feature_form_submit'] = array();
184 $form['#submit'] = array_reverse($form['#submit']);
185
186 $form['#redirect'] = 'admin/build/spaces/features';
187 break;
188 }
189 }
190
191 /**
192 * Submit handler for spaces context_ui form alterations.
193 */
194 function spaces_ui_feature_form_submit($form_id, $form_values) {
195 $feature = $form_values['value'];
196 $settings = array();
197 $settings['label'] = $form_values['feature']['label'];
198 $settings['description'] = $form_values['feature']['description'];
199
200 // Types
201 if ($form_values['feature']['types'] == 0) {
202 $settings['types'] = array();
203 }
204 else {
205 $settings['types'] = array($form_values['feature']['types']);
206 }
207
208 // Menu
209 $settings['menu'] = array();
210 foreach ($form_values['feature']['menu'] as $item) {
211 if (!empty($item['title']) && !empty($item['path'])) {
212 $settings['menu'][$item['path']] = array(
213 'title' => $item['title']
214 );
215 }
216 }
217 ksort($settings['menu']);
218
219 db_query('DELETE FROM {spaces_features_ui} WHERE feature = "%s"', $feature);
220 db_query('INSERT INTO {spaces_features_ui} (feature, value) VALUES ("%s", "%s")', $feature, serialize($settings));
221 }
222
223 /**
224 * Delete confirmation submit handler that deletes the spaces feature
225 * values.
226 */
227 function spaces_ui_feature_delete($form_id, $form_values) {
228 $feature = $form_values['spaces_feature'];
229 db_query('DELETE FROM {spaces_features_ui} WHERE feature = "%s"', $feature);
230 }
231
232 /**
233 * Menu page callback that shows a listing of spaces features.
234 */
235 function spaces_ui_features() {
236 drupal_set_title(t('Spaces features'));
237 $spaces_types = spaces_types();
238 $spaces_features = spaces_features();
239
240 $output = '';
241 $rows = array();
242
243 foreach ($spaces_features as $id => $feature) {
244 // Load the context object for this feature
245 // @TODO: change context_ui load operation so that it wouldn't
246 // wipe out the spaces extension if loaded.
247 $context = context_ui_context('load', $feature);
248
249 // Basic information about this feature
250 $label = "<strong>". $feature->spaces['label'] ."</strong>";
251 $label .= "<div class='description'>". $feature->spaces['description'] ."</div>";
252
253 // Generate action links for this item
254 // @TODO: currently, override contexts also have a "Delete" link --
255 // it should probably say "Revert". However, there is no easy way
256 // to tell that the context is an override of a code context. This
257 // is probably something that needs to change upstream
258 // (in context_ui).
259 $links = array();
260 if ($context->system == 0) {
261 $links[] = l(t('Edit'), 'admin/build/context/edit/'. $context->cid);
262 $links[] = l(t('Delete'), 'admin/build/context/delete/'. $context->cid, array(), 'destination=admin/build/spaces/features');
263 }
264 else {
265 $links[] = l(t('Override'), 'admin/build/context/clone/'. $context->cid);
266 }
267 $links = implode(' | ', $links);
268
269 // Generate human readable version of spaces types
270 if (isset($feature->spaces['types']) && count($feature->spaces['types'])) {
271 $types = array();
272 foreach ($feature->spaces['types'] as $type) {
273 $types[] = $spaces_types[$type]['title'];
274 }
275 $types = implode(', ', $types);
276 }
277 else {
278 $types = t('All spaces');
279 }
280
281 $rows[] = array($label, $types, $links);
282 }
283
284 $header = array(t('Feature'), t('Usable for'), t('Actions'));
285 $output .= theme('table', $header, $rows);
286
287 return $output;
288 }
289
290 /**
291 * Custom form theming for spaces feature menu.
292 */
293 function theme_spaces_ui_feature_form_menu($form) {
294 drupal_add_js(drupal_get_path('module', 'spaces_ui') .'/spaces_ui.js');
295 drupal_add_css(drupal_get_path('module', 'spaces_ui') .'/spaces.css');
296
297 // Render help markup first
298 $output = drupal_render($form['help']);
299
300 // Render menu items in a table
301 $rows = array();
302 foreach (element_children($form) as $element) {
303 if (is_numeric($element)) {
304 $class = !$form[$element]['title']['#default_value'] ? 'hidden' : '';
305 $rows[] = array(
306 'data' => array(
307 drupal_render($form[$element]['title']),
308 drupal_render($form[$element]['path']),
309 l(t('Remove'), $_GET['q'], array('class' => 'spaces-remove-menu')),
310 ),
311 'class' => $class,
312 );
313 }
314 }
315
316 // Add an ajax button
317 $rows[] = array(
318 array(
319 'data' => l(t('Add menu item'), $_GET['q'], array('class' => 'button spaces-add-menu')),
320 'colspan' => 3,
321 'class' => 'actions',
322 ),
323 );
324
325 $output .= theme('table', array(t('Title'), t('Path'), ''), $rows, array('class' => 'spaces-admin'));
326 return $output;
327 }
328
329 ?>

  ViewVC Help
Powered by ViewVC 1.1.2