/[drupal]/contributions/modules/spaces/spaces_admin.inc
ViewVC logotype

Contents of /contributions/modules/spaces/spaces_admin.inc

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, 3 weeks ago) by yhahn
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +731 -219 lines
File MIME type: text/x-php
Beginning merge of Spaces 2 to DRUPAL-6 branch
1 <?php
2
3 /**
4 * Page callback for generating a list of spaces presets.
5 * (admin/build/spaces)
6 */
7 function spaces_preset_default_form(&$form_state) {
8 $default_presets = variable_get('spaces_default_presets', array());
9
10 $form = array(
11 '#theme' => 'spaces_preset_default_form',
12 'types' => array('#tree' => TRUE),
13 );
14
15 foreach (spaces_types() as $type => $info) {
16 $form['types'][$type] = array(
17 '#tree' => TRUE,
18 '#title' => $info['title'],
19 );
20 $presets = spaces_presets($type, TRUE);
21 if (count($presets)) {
22 $form['types'][$type]['default'] = array(
23 '#type' => 'radios',
24 '#options' => array(),
25 );
26 foreach ($presets as $id => $preset) {
27 // Add radio for use when choosing default
28 if (!$preset['disabled']) {
29 $form['types'][$type]['default']['#options'][$id] = $preset['name'];
30
31 // Set as default if it is
32 if ($id == $default_presets[$type]) {
33 $form['types'][$type]['default']['#default_value'] = $id;
34 }
35 }
36
37 // Build links for each preset
38 $links = array();
39 if (isset($preset['module'])) {
40 $links[] = $preset['disabled'] ? l(t('Enable'), 'admin/build/spaces/presets/enable/'. $type .'/'. $id) : l(t('Disable'), 'admin/build/spaces/presets/disable/'. $type .'/'. $id);
41 }
42 else {
43 $links[] = l(t('Edit'), 'admin/build/spaces/presets/edit/'. $type .'/'. $id);
44 $links[] = $preset['disabled'] ? l(t('Enable'), 'admin/build/spaces/presets/enable/'. $type .'/'. $id) : l(t('Disable'), 'admin/build/spaces/presets/disable/'. $type .'/'. $id);
45 $links[] = l(t('Delete'), 'admin/build/spaces/presets/delete/'. $type .'/'. $id);
46 }
47 $links = implode(' | ', $links);
48
49 // Store preset information to be passed to theme function
50 $form['types'][$type]['info'][$id] = array(
51 '#type' => 'value',
52 '#value' => array(
53 'name' => $preset['name'],
54 'description' => $preset['description'],
55 'links' => $links,
56 'disabled' => $preset['disabled'],
57 ),
58 );
59 }
60 }
61 }
62
63 $form['submit'] = array(
64 '#type' => 'submit',
65 '#value' => t('Save defaults'),
66 '#submit' => array('spaces_preset_default_form_submit'),
67 );
68 $form['reset'] = array(
69 '#type' => 'submit',
70 '#value' => t('Clear defaults'),
71 '#submit' => array('spaces_preset_default_form_reset'),
72 );
73
74 return $form;
75 }
76
77 /**
78 * Submit handler for spaces_preset_default_form().
79 */
80 function spaces_preset_default_form_submit($form, &$form_state) {
81 $default_presets = variable_get('spaces_default_presets', array());
82
83 foreach (spaces_types() as $type => $dummy) {
84 if (isset($form_state['values']['types'][$type]['default']) && !empty($form_state['values']['types'][$type]['default'])) {
85 $default_presets[$type] = $form_state['values']['types'][$type]['default'];
86 }
87 else {
88 unset($default_presets[$type]);
89 }
90 }
91 variable_set('spaces_default_presets', $default_presets);
92 drupal_set_message(t('Spaces preset defaults were saved successfully.'));
93 }
94
95 /**
96 * Submit handler for spaces_preset_default_form().
97 */
98 function spaces_preset_default_form_reset($form, &$form_state) {
99 variable_del('spaces_default_presets');
100 drupal_set_message(t('Spaces preset defaults were cleared successfully.'));
101 }
102
103 /**
104 * Theme function for spaces_preset_default_form().
105 */
106 function theme_spaces_preset_default_form($form) {
107 drupal_add_css(drupal_get_path('module', 'spaces') .'/spaces.css');
108 $output = '';
109 foreach (element_children($form['types']) as $type) {
110 // Build table rows
111 $rows = array();
112 foreach (element_children($form['types'][$type]['info']) as $preset) {
113 // Determine if this preset is enabled
114 if ($form['types'][$type]['info'][$preset]['#value']['disabled']) {
115 $preset_option = $form['types'][$type]['info'][$preset]['#value']['name'];
116 $disabled = TRUE;
117 }
118 else {
119 $preset_option = drupal_render($form['types'][$type]['default'][$preset]);
120 $disabled = FALSE;
121 }
122
123 // Build table row
124 $row = array(
125 'data' => array(
126 $preset_option,
127 $preset,
128 $form['types'][$type]['info'][$preset]['#value']['description'],
129 $form['types'][$type]['info'][$preset]['#value']['links'],
130 ),
131 'class' => $disabled ? 'disabled' : '',
132 );
133
134 $rows[] = $row;
135 }
136
137 // Add type heading and preset table to output
138 $output .= "<h3>". $form['types'][$type]['#title'] ."</h3>";
139 $output .= theme('table', array(t('Default'), t('ID'), t('Description'), ''), $rows, array('class' => 'spaces-admin'));
140 }
141 $output .= "<div class='buttons'>";
142 $output .= drupal_render($form['submit']) . drupal_render($form['reset']);
143 $output .= "</div>";
144 $output .= drupal_render($form);
145 return $output;
146 }
147
148 /**
149 * Form for adding or editing a spaces preset.
150 *
151 * @param $op
152 * A string for the operation to perform. Either 'add' or 'edit'.
153 * @param $type
154 * A space type string.
155 * @param $preset_id
156 * The preset identifier for edit forms.
157 *
158 * @return
159 * A FormAPI array.
160 */
161 function spaces_preset_form($op = 'add', $type, $preset_id = NULL) {
162 $form = array();
163 $space = spaces_load($type);
164
165 switch ($op) {
166 case 'add':
167 drupal_set_title(t('Add spaces preset'));
168 break;
169 case 'edit':
170 if ($type && $preset_id) {
171 drupal_set_title(t('Edit preset: !presetname', array('!presetname' => $preset_id)));
172
173 // Enforce preset settings on space object
174 $space->preset = $preset_id;
175 spaces_preset_enforce($space);
176
177 // Load preset for form info
178 $presets = spaces_presets($type);
179 $preset = $presets[$preset_id];
180 }
181 break;
182 }
183
184 // Preset fields
185 $form['preset'] = array(
186 '#tree' => true,
187 );
188 $form['preset']['id'] = array(
189 '#type' => 'textfield',
190 '#title' => t('Preset id'),
191 '#required' => true,
192 '#maxlength' => 64,
193 '#description' => t('Enter an id for your preset. It may only contain lowercase letters, numbers, and underscores or dashes. <strong>Example:</strong> private_space'),
194 );
195 $form['preset']['name'] = array(
196 '#type' => 'textfield',
197 '#title' => t('Preset name'),
198 '#required' => true,
199 '#maxlength' => 255,
200 '#description' => t('Enter a name for your preset. It will be displayed to users in forms and messages.'),
201 );
202 $form['preset']['description'] = array(
203 '#type' => 'textfield',
204 '#title' => t('Description'),
205 '#description' => t('Enter a description for your preset. It should help users understand what the preset provides.'),
206 );
207
208 // Features/settings form
209 $form['features_form'] = _spaces_features_form($space);
210 $form['features_form']['#tree'] = FALSE;
211 $form['features_form']['#theme'] = 'spaces_features_form';
212
213 // Add locks to features/settings
214 $form['features_form']['locked'] = array(
215 '#tree' => TRUE,
216 'features' => array('#tree' => TRUE),
217 'settings' => array('#tree' => TRUE),
218 );
219 foreach (element_children($form['features_form']['features']) as $id) {
220 $form['features_form']['locked']['features'][$id] = array(
221 '#type' => 'checkbox',
222 );
223 }
224 foreach (element_children($form['features_form']['settings']) as $id) {
225 $form['features_form']['locked']['settings'][$id] = array(
226 '#type' => 'checkbox',
227 );
228 }
229
230 // Type-specific form
231 $form[$type] = $space->form('preset');
232
233 // Set default values
234 if ($op == 'edit') {
235 $form['preset']['id']['#default_value'] =
236 $form['preset']['id']['#value'] = $preset_id;
237 $form['preset']['id']['#disabled'] = true;
238 $form['preset']['name']['#default_value'] = $preset['name'];
239 $form['preset']['description']['#default_value'] = $preset['description'];
240
241 foreach (element_children($form['features_form']['features']) as $id) {
242 $form['features_form']['locked']['features'][$id]['#default_value'] = isset($preset['preset']['locked']['features'][$id]) ? $preset['preset']['locked']['features'][$id] : 0;
243 }
244 foreach (element_children($form['features_form']['settings']) as $id) {
245 $form['features_form']['locked']['settings'][$id]['#default_value'] = isset($preset['preset']['locked']['settings'][$id]) ? $preset['preset']['locked']['settings'][$id] : 0;
246 }
247 }
248
249 $form['submit'] = array(
250 '#type' => 'submit',
251 '#value' => t('Submit'),
252 '#weight' => 10,
253 );
254
255 $form['#theme'] = 'spaces_form';
256
257 return $form;
258 }
259
260 /**
261 * Validation for the preset form.
262 */
263 function spaces_preset_form_validate($form_id, $form_values) {
264 $space = $form_values['space'];
265
266 // Validate preset info
267 if ($form_values['preset']['id'] && !preg_match('!^[a-z0-9_-]+$!', $form_values['preset']['id'])) {
268 form_set_error('preset][id', t('The preset name may only contain lowercase letters, numbers, underscores or dashes.'));
269 }
270
271 // Validate the features form
272 spaces_features_form_validate($form_id, $form_values);
273
274 // Allow the space type to run its own validations
275 $space->validate($form_values);
276 }
277
278 /**
279 * Submit handler for spaces preset form.
280 */
281 function spaces_preset_form_submit($form, &$form_state) {
282 // Retrieve the space object from the form
283 $space = $form_state['values']['space'];
284
285 $preset = array(
286 'name' => '',
287 'description' => '',
288 'features' => array(),
289 'settings' => array(),
290 'locked' => array(),
291 );
292
293 // Set name / description
294 $preset['name'] = $form_state['values']['preset']['name'];
295 $preset['description'] = $form_state['values']['preset']['description'];
296
297 // Setting features & locks is easy
298 $preset['features'] = $form_state['values']['features'];
299 $preset['locked'] = $form_state['values']['locked'];
300
301 // Set setting values
302 $settings = spaces_settings();
303 foreach ($form_state['values']['settings'] as $setting => $value) {
304 $preset['settings'][$setting] = $settings[$setting]->submit($space, $value);
305 }
306
307 // Allow space_type to have its own preset values
308 $preset[$space->type] = $space->submit($form_state['values']);
309
310 spaces_preset_save($space->type, $form_state['values']['preset']['id'], $preset);
311 $form_state['redirect'] = 'admin/build/spaces';
312 }
313
314 /**
315 * Saves a spaces preset.
316 *
317 * @param $type
318 * The space type for this preset.
319 * @param $id
320 * The preset identifier string.
321 * @param $values
322 * An array of values that define the preset settings.
323 */
324 function spaces_preset_save($type, $id, $values) {
325 $name = isset($values['name']) ? $values['name'] : '';
326 $description = isset($values['description']) ? $values['description'] : '';
327 unset($values['name']);
328 unset($values['description']);
329
330 $exists = db_result(db_query("SELECT count(id) FROM {spaces_presets} WHERE type = '%s' AND id = '%s'", $type, $id));
331 $success = false;
332 if ($exists) {
333 $success = db_query("UPDATE {spaces_presets} SET name = '%s', description = '%s', value = '%s' WHERE type = '%s' AND id = '%s'", $name, $description, serialize($values), $type, $id);
334 }
335 else {
336 $success = db_query("INSERT INTO {spaces_presets} (type, id, name, description, value) VALUES('%s', '%s', '%s', '%s', '%s')", $type, $id, $name, $description, serialize($values));
337 }
338 if ($success) {
339 drupal_set_message(t('The preset !preset was saved successfully.', array('!preset' => $id)));
340 }
341 else {
342 drupal_set_message(t('There was an error saving the preset !preset.', array('!preset' => $id)));
343 }
344 }
345
346 /**
347 * Load a spaces preset.
348 *
349 * @param $type
350 * The space type for this preset.
351 * @param $id
352 * The preset identifier string.
353 */
354 function spaces_preset_load($type, $id) {
355 $value = db_result(db_query("SELECT value FROM {spaces_presets} WHERE type = '%s' AND id = '%s'", $type, $id));
356 if ($value) {
357 return unserialize($value);
358 }
359 else {
360 drupal_set_message(t('An error occurred while trying to load !preset.'), array('!preset' => $id));
361 return array();
362 }
363 }
364
365 function spaces_preset_delete($type, $preset) {
366 $success = db_query("DELETE FROM {spaces_presets} WHERE type = '%s' AND id = '%s'", $type, $preset);
367 $message = $success ? t('The preset !preset was deleted successfully.', array('!preset' => $preset)) : t('An error occurred while trying to delete !preset.', array('!preset' => $preset));
368 drupal_set_message($message);
369 }
370
371 function spaces_preset_disable($type, $preset) {
372 $disabled = variable_get('spaces_disabled_presets', array());
373 if (!isset($disabled[$type])) {
374 $disabled[$type] = array();
375 }
376 if (!isset($disabled[$type][$preset])) {
377 $disabled[$type][$preset] = 1;
378 variable_set('spaces_disabled_presets', $disabled);
379 }
380 }
381
382 function spaces_preset_enable($type, $preset) {
383 $disabled = variable_get('spaces_disabled_presets', array());
384 if (!isset($disabled[$type])) {
385 $disabled[$type] = array();
386 }
387 if (isset($disabled[$type][$preset])) {
388 unset($disabled[$type][$preset]);
389 variable_set('spaces_disabled_presets', $disabled);
390 }
391 }
392
393 /**
394 * Page callback wrapper around spaces_preset_disable()
395 */
396 function _spaces_preset_disable_page($type, $preset) {
397 spaces_preset_disable($type, $preset);
398 drupal_goto('admin/build/spaces');
399 }
400
401 /**
402 * Page callback wrapper around spaces_preset_enable()
403 */
404 function _spaces_preset_enable_page($type, $preset) {
405 spaces_preset_enable($type, $preset);
406 drupal_goto('admin/build/spaces');
407 }
408
409 function spaces_preset_delete_form($type, $preset_id) {
410 // @TODO: proper check for preset existence
411 $presets = spaces_presets(null, TRUE);
412 if (isset($presets[$type][$preset_id]) && $preset = $presets[$type][$preset_id]) {
413 $form = array();
414 $form['type'] = array(
415 '#type' => 'hidden',
416 '#value' => $type,
417 );
418 $form['preset'] = array(
419 '#type' => 'hidden',
420 '#value' => $preset_id,
421 );
422 $question = t('Are you sure you want to delete !preset?', array('!preset' => $preset['name']));
423 $description = t('Any spaces using this preset will need to reset.');
424 $form = confirm_form($form, $question, 'admin/build/spaces', $description);
425 return $form;
426 }
427 return '';
428 }
429
430 function spaces_preset_delete_form_submit($form, &$form_state) {
431 $type = $form_state['values']['type'];
432 $preset = $form_state['values']['preset'];
433 spaces_preset_delete($type, $preset);
434 $form_state['redirect'] = 'admin/build/spaces';
435 }
436
437 /**
438 * BASIC FORM =========================================================
439 */
440
441 function theme_spaces_form($form, $output = '') {
442 if (
443 (isset($form['submit']) && $form['submit']['#type'] == 'submit') ||
444 (isset($form['reset']) && $form['reset']['#type'] == 'submit')
445 ) {
446 $buttons = "<div class='buttons'>";
447 $buttons .= isset($form['submit']) ? drupal_render($form['submit']) : '';
448 $buttons .= isset($form['reset']) ? drupal_render($form['reset']) : '';
449 $buttons .= "</div>";
450 }
451
452 $output .= drupal_render($form) . $buttons;
453 return $output;
454 }
455
456 function spaces_basic_form(&$form_state, $space = NULL) {
457 // Attempt to get current space if not provided
458 $space = !isset($space) ? spaces_get_space() : $space;
459
460 $form = array();
461
462 $form['space'] = array(
463 '#type' => 'value',
464 '#value' => $space,
465 );
466
467 $types = spaces_types();
468 if (isset($types[$space->type]['custom prefixes']) && $types[$space->type]['custom prefixes'] != FALSE) {
469 // Add context prefix form
470 $form['context_prefix'] = context_prefix_form('spaces', $space->type .':'. $space->sid, $space->prefix);
471 }
472
473 // Add preset form
474 $form['preset'] = spaces_form_presets($space);
475
476 $form['submit'] = array(
477 '#type' => 'submit',
478 '#value' => t('Submit'),
479 '#submit' => array('spaces_basic_form_submit'),
480 );
481
482 $form['#theme'] = 'spaces_form';
483
484 return $form;
485 }
486
487 function spaces_basic_form_submit($form, &$form_state) {
488 $space = $form_state['values']['space'];
489
490 // Grab prefixes if space uses custom prefixes
491 if (isset($form_state['values']['context_prefix'])) {
492 $space->prefix = $form_state['values']['context_prefix']['prefix'];
493 }
494
495 // Retrieve selected preset and enforce values
496 $space->preset = $form_state['values']['preset'];
497 spaces_preset_enforce($space);
498
499 spaces_save($space);
500 }
501
502 /**
503 * FEATURE SETTINGS ===================================================
504 */
505
506 function spaces_features_form(&$form_state, $space = NULL) {
507 // Attempt to get current space if not provided
508 $space = !isset($space) ? spaces_get_space() : $space;
509
510 // Set a wide layout for themes that support it
511 context_set('theme', 'layout', 'wide');
512
513 spaces_preset_enforce($space);
514
515 $form = _spaces_features_form($space);
516
517 // Add customization link if feature is not disabled
518 $form['customize'] = array('#tree' => TRUE);
519 foreach (element_children($form['features']) as $id) {
520 if (isset($space->features[$id]) && ($space->features[$id] != SPACES_FEATURE_DISABLED)) {
521 $form['customize'][$id] = array(
522 '#type' => 'markup',
523 '#value' => l(t('Customize'), 'spaces/customize/'. $id),
524 );
525 }
526 }
527
528 // Lock features
529 if ($space->preset) {
530 $presets = spaces_presets($space->type);
531 $preset = $presets[$space->preset];
532 if (isset($preset['locked']['features'])) {
533 foreach ($preset['preset']['locked']['features'] as $id => $value) {
534 if ($value) {
535 $form['features'][$id]['#disabled'] = true;
536 $form['features'][$id]['#locked'] = true; // attribute used in theme layer
537 }
538 }
539 }
540 }
541
542 $form['submit'] = array(
543 '#type' => 'submit',
544 '#value' => t('Submit'),
545 '#weight' => 10,
546 );
547
548 return $form;
549 }
550
551 /**
552 * Core form for controlling features / settings
553 */
554 function _spaces_features_form($space) {
555 $form = array();
556 $form['space'] = array(
557 '#type' => 'value',
558 '#value' => $space,
559 );
560 $form['features'] = array(
561 '#type' => 'fieldset',
562 '#title' => t('Features'),
563 '#description' => t('Control the features are enabled for this group by adjusting the settings below. Private features will limit access to members of this group, while public features allow any users to view any content within that feature.'),
564 '#tree' => TRUE,
565 );
566 $form['settings'] = array(
567 '#type' => 'fieldset',
568 '#title' => t('Settings'),
569 '#description' => t('Settings are options for customizing your group that do not affect the privacy of your content.'),
570 '#tree' => TRUE,
571 );
572
573 // Generate features form
574 foreach (spaces_features($space->type) as $id => $feature) {
575 $options = $space->feature_options() ? $space->feature_options() : array();
576 // TODO: reimplement option limiting as part of blueprinting
577 if (count($options) > 0) {
578 $form['features'][$id] = array(
579 '#type' => 'select',
580 '#title' => $feature->spaces['label'],
581 '#description' => $feature->spaces['description'],
582 '#options' => $options,
583 '#default_value' => isset($space->features[$id]) ? $space->features[$id] : SPACES_FEATURE_DISABLED,
584 );
585 }
586 }
587
588 // Generate settings form
589 foreach (spaces_settings() as $setting) {
590 $setting_value = isset($space->settings[$setting->id]) ? $space->settings[$setting->id] : NULL;
591 $form['settings'][$setting->id] = $setting->form($space, $setting_value);
592 }
593
594 return $form;
595 }
596
597 /**
598 * Validate handler for spaces features form
599 */
600 function spaces_features_form_validate($form_id, $form_values) {
601 $space = $form_values['space'];
602 $settings = spaces_settings();
603 if (isset($space->sid) && array_sum($form_values['features']) == 0) {
604 return form_set_error('', t('You must enable at least 1 feature for this group.'));
605 }
606 if (is_array($form_values['settings'])) {
607 foreach ($form_values['settings'] as $setting => $value) {
608 $settings[$setting]->validate($space, $value);
609 }
610 }
611 }
612
613 /**
614 * Submit handler for spaces features form
615 */
616 function spaces_features_form_submit($form, &$form_state) {
617 // Retrieve the space object from the form
618 $space = $form_state['values']['space'];
619
620 // Set feature values
621 foreach ($form_state['values']['features'] as $feature => $value) {
622 $space->features[$feature] = $value;
623 }
624
625 // Set setting values
626 $settings = spaces_settings();
627 foreach ($form_state['values']['settings'] as $setting => $value) {
628 $space->settings[$setting] = $settings[$setting]->submit($space, $value);
629 }
630
631 // Save the space
632 spaces_save($space);
633 drupal_set_message(t('The space configuration has been saved successfully.'));
634 }
635
636 /**
637 * Theme for spaces featuers/settings form.
638 */
639 function theme_spaces_features_form($form) {
640 drupal_add_css(drupal_get_path('module', 'spaces') .'/spaces.css');
641 $output = '';
642 foreach (array('features', 'settings') as $type) {
643 $header = array(
644 $type == 'features' ? t('Feature') : t('Setting'),
645 t('Status'),
646 t('Description'),
647 !isset($form['space']['#value']->sid) ? t('Locked') : '',
648 );
649 $rows = array();
650 foreach (element_children($form[$type]) as $element) {
651 $feature_name = "<strong>". $form[$type][$element]['#title'] ."</strong>";
652 $description = "<div class='description'>". $form[$type][$element]['#description'] ."</div>";
653 $customize = isset($form['customize'][$element]) ? drupal_render($form['customize'][$element]) : '';
654 $locked = isset($form['locked']) ? drupal_render($form['locked'][$type][$element]) : '';
655 unset($form[$type][$element]['#title']);
656 unset($form[$type][$element]['#description']);
657
658 $row = array(
659 'name' => $feature_name,
660 'option' => drupal_render($form[$type][$element]),
661 'description' => $description,
662 'action' => $customize . $locked,
663 );
664 foreach ($row as $key => $data) {
665 $row[$key] = array(
666 'data' => $data,
667 'class' => $key,
668 );
669 }
670
671 $class = $form[$type][$element]['#default_value'] ? 'enabled' : 'disabled';
672 $class .= $form[$type][$element]['#locked'] ? ' locked' : '';
673
674 $rows[] = array(
675 'data' => $row,
676 'class' => $class,
677 );
678 }
679 $output .= "<h3>". $form[$type]['#title'] ."</h3>";
680 $output .= "<div class='description'>". $form[$type]['#description'] ."</div>";
681 $output .= theme('table', $header, $rows, array('class' => 'spaces-'. $type));
682
683 // Prevent section from being rendered by drupal_render().
684 unset($form[$type]);
685 }
686
687 if (isset($form['submit']) && $form['submit']['#type'] == 'submit') {
688 $output .= "<div class='buttons'>";
689 $output .= drupal_render($form['submit']);
690 $output .= "</div>";
691 }
692
693 $output .= drupal_render($form);
694 return $output;
695 }
696
697 /**
698 * FEATURE CUSTOMIZATION ==============================================
699 */
700
701 /**
702 * Customization page callback.
703 */
704 function spaces_customize($space = NULL, $feature = NULL) {
705 // Attempt to get current space if not provided
706 $space = !isset($space) ? spaces_get_space() : $space;
707
708 $features = spaces_features($space->type);
709 if (isset($features[$feature])) {
710 drupal_set_title(t('Customize feature: !feature', array('!feature' => $features[$feature]->spaces['label'])));
711 return drupal_get_form('spaces_customize_form', $space, $feature);
712 }
713 else {
714 $rows = array();
715 foreach (spaces_features($space->type) as $id => $feature) {
716 if (isset($space->features[$id]) && ($space->features[$id] != SPACES_FEATURE_DISABLED)) {
717 $label = "<strong>". $feature->spaces['label'] ."</strong>";
718 $description = "<div class='description'>". $feature->spaces['description'] ."</div>";
719 $rows[] = array(
720 $label . $description,
721 l(t('Customize'), 'spaces/customize/'. $id),
722 );
723 }
724 }
725 return theme('table', array(t('Available features'), ''), $rows);
726 }
727 }
728
729 /**
730 * Feature customization form.
731 */
732 function spaces_customize_form($space, $feature) {
733 $form = array();
734
735 $form['space'] = array(
736 '#type' => 'value',
737 '#value' => $space,
738 );
739 $form['feature'] = array(
740 '#type' => 'value',
741 '#value' => $feature,
742 );
743
744 $customizers = spaces_customizers();
745 foreach ($customizers as $id => $customizer) {
746 $form[$id] = array(
747 '#title' => t($customizer->name),
748 '#type' => 'fieldset',
749 '#tree' => TRUE,
750 '#theme' => 'spaces_customize_item',
751 '#collapsible' => TRUE,
752 );
753 $form[$id] = $form[$id] + $customizer->form($space, $feature);
754 }
755
756 $form['submit'] = array(
757 '#type' => 'submit',
758 '#value' => t('Save settings'),
759 '#submit' => array('spaces_customize_form_submit'),
760 );
761 $form['reset'] = array(
762 '#type' => 'submit',
763 '#value' => t('Reset to defaults'),
764 '#submit' => array('spaces_customize_form_reset'),
765 );
766
767 $form['#theme'] = 'spaces_form';
768
769 return $form;
770 }
771
772 /**
773 * Validate handler for spaces features form
774 */
775 function spaces_customize_form_validate($form_id, $form_values) {
776 $space = $form_values['space'];
777 $feature = $form_values['feature'];
778 $customizers = spaces_customizers();
779 foreach ($customizers as $id => $customizer) {
780 $customizer->validate($space, $feature, $form_values[$id]);
781 }
782 }
783
784 /**
785 * Submit handler for feature customization form.
786 */
787 function spaces_customize_form_submit($form, &$form_state) {
788 $space = $form_state['values']['space'];
789 $feature = $form_state['values']['feature'];
790 $customizers = spaces_customizers();
791
792 $feature_customizer = array();
793 foreach ($customizers as $id => $customizer) {
794 $feature_customizer[$id] = $customizer->submit($space, $feature, $form_state['values'][$id]);
795 }
796 $space->customizer[$feature] = $feature_customizer;
797 spaces_save($space);
798 drupal_set_message(t('Customizations saved for space !title.', array('!title' => $space->title)));
799 }
800
801 /**
802 * Reset submit handler for feature customization form.
803 */
804 function spaces_customize_form_reset($form, &$form_state) {
805 $space = $form_state['values']['space'];
806 $feature = $form_state['values']['feature'];
807 $customizers = spaces_customizers();
808
809 unset($space->customizer[$feature]);
810 spaces_save($space);
811 }
812
813 /**
814 * Form theme function for customization items.
815 */
816 function theme_spaces_customize_item($form) {
817 $output = '';
818 $rows = array();
819 foreach (element_children($form) as $element) {
820 if ($form[$element]['#type'] == 'fieldset') {
821 $title = $form[$element]['#title'];
822 unset($form[$element]['#title']);
823 unset($form[$element]['#type']);
824 $rows[] = array(
825 "<strong>$title</strong>",
826 drupal_render($form[$element]),
827 );
828 }
829 }
830 $output .= theme('table', array(), $rows);
831 return $output;
832 }

  ViewVC Help
Powered by ViewVC 1.1.2