TRUE);
$form['labels'] = array('#tree' => FALSE);
$form['settings'] = array('#tree' => FALSE);
$space = spaces_get_space();
$parent = menu_get_item();
$features = spaces_features($space ? $space->type : 'site');
ksort($features);
foreach ($features as $feature => $info) {
$label = "". t($info->info['name']) ."";
$label .= "
". t($info->info['description']) ."
";
$form['labels'][$feature] = array(
'#value' => $label,
'#type' => 'markup',
);
$form['spaces_features'][$feature] = array(
'#type' => 'select',
'#options' => $space ? $space->feature_options() : array(0 => t('Disabled'), 1 => t('Enabled')),
'#default_value' => isset($spaces_features[$feature]) ? $spaces_features[$feature] : 0,
);
// By convention, features can provide settings pages at features/[feature].
// This will detect the items that have been grafted from features/* onto the
// portion fo the menu tree that relates to this pace.
$item = menu_get_item("{$parent['href']}/{$feature}");
if ($item && $item['href'] != $parent['href'] && $item['access']) {
$settings = l($item['title'], $item['href'], array('query' => 'destination='. $_GET['q']));
}
else {
$settings = '';
}
$form['settings'][$feature] = array(
'#type' => 'markup',
'#value' => $settings,
);
}
$form = system_settings_form($form);
$form['#theme'] = 'spaces_features_form';
$form['buttons']['#weight'] = 100;
return $form;
}
/**
* Generate a form snippet for choosing a spaces preset.
*/
function spaces_preset_form($presets, $type, $actions = 'actions') {
$types = spaces_types();
$form = array(
'#tree' => FALSE,
'#title' => isset($types[$type]['title']) ? $types[$type]['title'] : '',
'#theme' => 'spaces_preset_form',
'#space_type' => $type,
);
$form["spaces_preset_{$type}"] = array(
'#type' => 'radios',
'#options' => array(),
'#default_value' => variable_get("spaces_preset_{$type}", NULL),
);
foreach ($presets as $preset) {
// Default preset radio
if (empty($preset->disabled)) {
$form["spaces_preset_{$preset->space_type}"]['#options'][$preset->name] = '';
}
// Label
$label = "". check_plain($preset->title) ."";
$label .= "". check_plain($preset->description) ."
";
$form['labels'][$preset->name] = array('#type' => 'markup', '#value' => $label);
// Actions
if (user_access('administer spaces')) {
switch ($actions) {
case 'actions':
if (module_exists('spaces_ui')) {
$form['storage'][$preset->name] = array('#type' => 'markup', '#value' => check_plain($preset->type));
$links = array(
'edit' => l(t('Edit'), "admin/build/spaces/{$preset->name}"),
'delete' => l(t('Delete'), "admin/build/spaces/{$preset->name}/delete"),
'revert' => l(t('Revert'), "admin/build/spaces/{$preset->name}/revert"),
'export' => l(t('Export'), "admin/build/spaces/{$preset->name}/export"),
'disable' => l(t('Disable'), "admin/build/spaces/{$preset->name}/disable"),
'enable' => l(t('Enable'), "admin/build/spaces/{$preset->name}/enable"),
);
foreach (array_keys($links) as $key) {
if (!spaces_ui_preset_task_access($preset, $key)) {
unset($links[$key]);
}
}
$form['actions'][$preset->name] = array('#type' => 'markup', '#value' => implode(' | ', $links));
}
break;
}
}
}
return $form;
}
/**
* Form for managing space override values.
*/
function spaces_overrides_form($form_state) {
$space = spaces_get_space();
$stack = array('space', 'preset');
$form = array();
if ($space) {
foreach (array_keys(spaces_controllers()) as $controller) {
foreach ($stack as $env) {
foreach ($space->controllers->{$controller}->get(NULL, $env) as $key => $value) {
if (!isset($form[$controller][$key])) {
$form[$controller][$key] = array(
'#type' => 'checkbox',
'#title' => $key,
'#disabled' => $env !== 'space',
'#description' => $space->controllers->{$controller}->summary($key, $value),
'#default_value' => NULL,
);
}
}
}
}
if (element_children($form)) {
$form['#tree'] = TRUE;
$form['#theme'] = 'spaces_overrides_form';
$form['space'] = array('#type' => 'value', '#value' => $space);
if ($presets = spaces_preset_load(NULL, $space->type)) {
$preset_name = $presets[variable_get('spaces_preset_'. $space->type, NULL)]->title;
$form['preset'] = array(
'#tree' => FALSE,
'#type' => 'item',
'#title' => t('Save'),
'#description' => t('Save selected overrides from this space to the current preset !preset.', array('!preset' => $preset_name)),
);
$form['preset']['preset_save'] = array(
'#type' => 'submit',
'#value' => t('Save to preset'),
'#submit' => array('spaces_overrides_preset_save'),
);
}
$form['revert'] = array(
'#tree' => FALSE,
'#type' => 'item',
'#title' => t('Revert'),
'#description' => t('Revert the selected overrides for this space.'),
);
$form['revert']['revert'] = array(
'#type' => 'submit',
'#value' => t('Revert overrides'),
'#submit' => array('spaces_overrides_revert'),
);
}
}
return $form;
}
/**
* Submit handler for saving overrides to a preset.
*/
function spaces_overrides_preset_save(&$form, &$form_state) {
$space = $form_state['values']['space'];
$preset = spaces_preset_load(variable_get('spaces_preset_'. $space->type, NULL), NULL, TRUE);
$selected = FALSE;
$space->activate();
foreach (array_keys(spaces_controllers()) as $controller) {
$save = !empty($form_state['values'][$controller]) ? array_filter($form_state['values'][$controller]) : array();
foreach (array_keys($save) as $key) {
$override = $space->controllers->{$controller}->get($key, 'space');
if ($override !== NULL) {
$selected = TRUE;
$preset->value[$controller][$key] = $override;
}
}
}
if ($selected) {
spaces_preset_save($preset);
drupal_set_message(t('Saved preset %title.', array('%title' => $preset->title)));
}
else {
drupal_set_message(t('You need to select at least one override to save to the preset.'), 'error');
}
}
/**
* Submit handler for reverting overrides.
*/
function spaces_overrides_revert(&$form, &$form_state) {
$space = $form_state['values']['space'];
foreach (array_keys(spaces_controllers()) as $controller) {
$remove = !empty($form_state['values'][$controller]) ? array_filter($form_state['values'][$controller]) : array();
foreach (array_keys($remove) as $key) {
$space->controllers->{$controller}->del($key);
}
}
drupal_set_message(t('Saved %title.', array('%title' => $space->title())));
}