View source
<?php
function spaces_preset_default_form() {
$default_presets = variable_get('spaces_default_presets', array());
$form = array(
'#theme' => 'spaces_preset_default_form',
'types' => array(
'#tree' => TRUE,
),
);
foreach (spaces_types() as $type => $info) {
$form['types'][$type] = array(
'#tree' => TRUE,
'#title' => $info['title'],
);
$form['types'][$type]['add'] = array(
'#type' => 'markup',
'#value' => l(t('Add preset'), 'admin/build/spaces/presets/add/' . $type, array(
'class' => 'button',
)),
);
$presets = spaces_presets($type, TRUE);
if (count($presets)) {
$form['types'][$type]['default'] = array(
'#type' => 'radios',
'#options' => array(),
);
foreach ($presets as $id => $preset) {
if (!$preset['disabled']) {
$form['types'][$type]['default']['#options'][$id] = $preset['name'];
if ($id == $default_presets[$type]) {
$form['types'][$type]['default']['#default_value'] = $id;
}
}
$links = array();
if (isset($preset['module'])) {
$links[] = $preset['disabled'] ? l(t('Enable'), 'admin/build/spaces/presets/enable/' . $type . '/' . $id) : l(t('Disable'), 'admin/build/spaces/presets/disable/' . $type . '/' . $id);
}
else {
$links[] = l(t('Edit'), 'admin/build/spaces/presets/edit/' . $type . '/' . $id);
$links[] = $preset['disabled'] ? l(t('Enable'), 'admin/build/spaces/presets/enable/' . $type . '/' . $id) : l(t('Disable'), 'admin/build/spaces/presets/disable/' . $type . '/' . $id);
$links[] = l(t('Delete'), 'admin/build/spaces/presets/delete/' . $type . '/' . $id);
}
$links = implode(' | ', $links);
$form['types'][$type]['info'][$id] = array(
'#type' => 'value',
'#value' => array(
'name' => $preset['name'],
'description' => $preset['description'],
'links' => $links,
'disabled' => $preset['disabled'],
),
);
}
}
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save defaults'),
);
$form['reset'] = array(
'#type' => 'submit',
'#value' => t('Clear defaults'),
);
return $form;
}
function spaces_preset_default_form_submit($form_id, $form_values) {
switch (trim($form_values['op'])) {
case t('Save defaults'):
$default_presets = variable_get('spaces_default_presets', array());
foreach (spaces_types() as $type => $dummy) {
if (isset($form_values['types'][$type]['default']) && !empty($form_values['types'][$type]['default'])) {
$default_presets[$type] = $form_values['types'][$type]['default'];
}
else {
unset($default_presets[$type]);
}
}
variable_set('spaces_default_presets', $default_presets);
drupal_set_message(t('Spaces preset defaults were saved successfully.'));
break;
case t('Clear defaults'):
variable_del('spaces_default_presets');
drupal_set_message(t('Spaces preset defaults were cleared successfully.'));
break;
}
return;
}
function theme_spaces_preset_default_form($form) {
drupal_add_css(drupal_get_path('module', 'spaces') . '/spaces.css');
$output = '';
foreach (element_children($form['types']) as $type) {
$rows = array();
foreach (element_children($form['types'][$type]['info']) as $preset) {
if ($form['types'][$type]['info'][$preset]['#value']['disabled']) {
$preset_option = $form['types'][$type]['info'][$preset]['#value']['name'];
$disabled = TRUE;
}
else {
$preset_option = drupal_render($form['types'][$type]['default'][$preset]);
$disabled = FALSE;
}
$row = array(
'data' => array(
$preset_option,
$preset,
$form['types'][$type]['info'][$preset]['#value']['description'],
$form['types'][$type]['info'][$preset]['#value']['links'],
),
'class' => $disabled ? 'disabled' : '',
);
$rows[] = $row;
}
$rows[] = array(
array(
'data' => drupal_render($form['types'][$type]['add']),
'class' => 'actions',
'colspan' => 4,
),
);
$output .= "<h3>" . $form['types'][$type]['#title'] . "</h3>";
$output .= theme('table', array(
t('Default'),
t('ID'),
t('Description'),
'',
), $rows, array(
'class' => 'spaces-admin',
));
}
$output .= "<div class='buttons'>";
$output .= drupal_render($form['submit']) . drupal_render($form['reset']);
$output .= "</div>";
$output .= drupal_render($form);
return $output;
}
function spaces_preset_form($op = 'add', $type, $preset_id = NULL) {
$form = array();
$space = spaces_load($type);
switch ($op) {
case 'add':
drupal_set_title(t('Add spaces preset'));
break;
case 'edit':
if ($type && $preset_id) {
drupal_set_title(t('Edit preset: !presetname', array(
'!presetname' => $preset_id,
)));
$space->preset = $preset_id;
spaces_preset_enforce($space);
$presets = spaces_presets($type);
$preset = $presets[$preset_id];
}
break;
}
$form['preset'] = array(
'#tree' => true,
);
$form['preset']['id'] = array(
'#type' => 'textfield',
'#title' => t('Preset id'),
'#required' => true,
'#maxlength' => 64,
'#description' => t('Enter an id for your preset. It may only contain lowercase letters, numbers, and underscores or dashes. <strong>Example:</strong> private_space'),
);
$form['preset']['name'] = array(
'#type' => 'textfield',
'#title' => t('Preset name'),
'#required' => true,
'#maxlength' => 255,
'#description' => t('Enter a name for your preset. It will be displayed to users in forms and messages.'),
);
$form['preset']['description'] = array(
'#type' => 'textfield',
'#title' => t('Description'),
'#description' => t('Enter a description for your preset. It should help users understand what the preset provides.'),
);
$form['features_form'] = _spaces_features_form($space);
$form['features_form']['#tree'] = FALSE;
$form['features_form']['#theme'] = 'spaces_features_form';
$form['features_form']['locked'] = array(
'#tree' => TRUE,
'features' => array(
'#tree' => TRUE,
),
'settings' => array(
'#tree' => TRUE,
),
);
foreach (element_children($form['features_form']['features']) as $id) {
$form['features_form']['locked']['features'][$id] = array(
'#type' => 'checkbox',
);
}
foreach (element_children($form['features_form']['settings']) as $id) {
$form['features_form']['locked']['settings'][$id] = array(
'#type' => 'checkbox',
);
}
$form[$type] = $space
->form('preset');
if ($op == 'edit') {
$form['preset']['id']['#default_value'] = $form['preset']['id']['#value'] = $preset_id;
$form['preset']['id']['#disabled'] = true;
$form['preset']['name']['#default_value'] = $preset['name'];
$form['preset']['description']['#default_value'] = $preset['description'];
foreach (element_children($form['features_form']['features']) as $id) {
$form['features_form']['locked']['features'][$id]['#default_value'] = isset($preset['preset']['locked']['features'][$id]) ? $preset['preset']['locked']['features'][$id] : 0;
}
foreach (element_children($form['features_form']['settings']) as $id) {
$form['features_form']['locked']['settings'][$id]['#default_value'] = isset($preset['preset']['locked']['settings'][$id]) ? $preset['preset']['locked']['settings'][$id] : 0;
}
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
'#weight' => 10,
);
$form['#theme'] = 'spaces_form';
return $form;
}
function spaces_preset_form_validate($form_id, $form_values) {
$space = $form_values['space'];
if ($form_values['preset']['id'] && !preg_match('!^[a-z0-9_-]+$!', $form_values['preset']['id'])) {
form_set_error('preset][id', t('The preset name may only contain lowercase letters, numbers, underscores or dashes.'));
}
spaces_features_form_validate($form_id, $form_values);
$space
->validate($form_values);
}
function spaces_preset_form_submit($form_id, $form_values) {
$space = $form_values['space'];
$preset = array(
'name' => '',
'description' => '',
'features' => array(),
'settings' => array(),
'locked' => array(),
);
$preset['name'] = $form_values['preset']['name'];
$preset['description'] = $form_values['preset']['description'];
$preset['features'] = $form_values['features'];
$preset['locked'] = $form_values['locked'];
$settings = spaces_settings();
foreach ($form_values['settings'] as $setting => $value) {
$preset['settings'][$setting] = $settings[$setting]
->submit($space, $value);
}
$preset[$space->type] = $space
->submit($form_values);
spaces_preset_save($space->type, $form_values['preset']['id'], $preset);
return 'admin/build/spaces';
}
function spaces_preset_save($type, $id, $values) {
$name = isset($values['name']) ? $values['name'] : '';
$description = isset($values['description']) ? $values['description'] : '';
unset($values['name']);
unset($values['description']);
$exists = db_result(db_query("SELECT count(id) FROM {spaces_presets} WHERE type = '%s' AND id = '%s'", $type, $id));
$success = false;
if ($exists) {
$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);
}
else {
$success = db_query("INSERT INTO {spaces_presets} (type, id, name, description, value) VALUES('%s', '%s', '%s', '%s', '%s')", $type, $id, $name, $description, serialize($values));
}
if ($success) {
drupal_set_message(t('The preset !preset was saved successfully.', array(
'!preset' => $id,
)));
}
else {
drupal_set_message(t('There was an error saving the preset !preset.', array(
'!preset' => $id,
)));
}
}
function spaces_preset_load($type, $id) {
$value = db_result(db_query("SELECT value FROM {spaces_presets} WHERE type = '%s' AND id = '%s'", $type, $id));
if ($value) {
return unserialize($value);
}
else {
drupal_set_message(t('An error occurred while trying to load !preset.'), array(
'!preset' => $id,
));
return array();
}
}
function spaces_preset_delete($type, $preset) {
$success = db_query("DELETE FROM {spaces_presets} WHERE type = '%s' AND id = '%s'", $type, $preset);
$message = $success ? t('The preset !preset was deleted successfully.', array(
'!preset' => $preset,
)) : t('An error occurred while trying to delete !preset.', array(
'!preset' => $preset,
));
drupal_set_message($message);
}
function spaces_preset_disable($type, $preset) {
$disabled = variable_get('spaces_disabled_presets', array());
if (!isset($disabled[$type])) {
$disabled[$type] = array();
}
if (!isset($disabled[$type][$preset])) {
$disabled[$type][$preset] = 1;
variable_set('spaces_disabled_presets', $disabled);
}
}
function spaces_preset_enable($type, $preset) {
$disabled = variable_get('spaces_disabled_presets', array());
if (!isset($disabled[$type])) {
$disabled[$type] = array();
}
if (isset($disabled[$type][$preset])) {
unset($disabled[$type][$preset]);
variable_set('spaces_disabled_presets', $disabled);
}
}
function _spaces_preset_disable_page($type, $preset) {
spaces_preset_disable($type, $preset);
drupal_goto('admin/build/spaces');
}
function _spaces_preset_enable_page($type, $preset) {
spaces_preset_enable($type, $preset);
drupal_goto('admin/build/spaces');
}
function spaces_preset_delete_form($type, $preset_id) {
$presets = spaces_presets(null, TRUE);
if (isset($presets[$type][$preset_id]) && ($preset = $presets[$type][$preset_id])) {
$form = array();
$form['type'] = array(
'#type' => 'hidden',
'#value' => $type,
);
$form['preset'] = array(
'#type' => 'hidden',
'#value' => $preset_id,
);
$question = t('Are you sure you want to delete !preset?', array(
'!preset' => $preset['name'],
));
$description = t('Any spaces using this preset will need to reset.');
$form = confirm_form($form, $question, 'admin/build/spaces', $description);
return $form;
}
return '';
}
function spaces_preset_delete_form_submit($form_id, $form_values) {
$type = $form_values['type'];
$preset = $form_values['preset'];
spaces_preset_delete($type, $preset);
return 'admin/build/spaces';
}
function theme_spaces_form($form, $output = '') {
if (isset($form['submit']) && $form['submit']['#type'] == 'submit' || isset($form['reset']) && $form['reset']['#type'] == 'submit') {
$buttons = "<div class='buttons'>";
$buttons .= isset($form['submit']) ? drupal_render($form['submit']) : '';
$buttons .= isset($form['reset']) ? drupal_render($form['reset']) : '';
$buttons .= "</div>";
}
$output .= drupal_render($form) . $buttons;
return $output;
}
function spaces_basic_form($space) {
$form = array();
$form['space'] = array(
'#type' => 'value',
'#value' => $space,
);
$types = spaces_types();
if (isset($types[$space->type]['custom prefixes']) && $types[$space->type]['custom prefixes'] != FALSE) {
$form['context_prefix'] = context_prefix_form('spaces', $space->type . ':' . $space->sid, $space->prefix);
}
$form['preset'] = spaces_form_presets($space);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
$form['#theme'] = 'spaces_form';
return $form;
}
function spaces_basic_form_submit($form_id, $form_values) {
$space = $form_values['space'];
if (isset($form_values['context_prefix'])) {
$space->prefix = $form_values['context_prefix']['prefix'];
}
$space->preset = $form_values['preset'];
spaces_preset_enforce($space);
spaces_save($space);
}
function spaces_features_form($space) {
context_set('theme', 'layout', 'wide');
spaces_preset_enforce($space);
$form = _spaces_features_form($space);
$form['customize'] = array(
'#tree' => TRUE,
);
foreach (element_children($form['features']) as $id) {
if (isset($space->features[$id]) && $space->features[$id] != SPACES_FEATURE_DISABLED) {
$form['customize'][$id] = array(
'#type' => 'markup',
'#value' => l(t('Customize'), 'spaces/customize/' . $id),
);
}
}
if ($space->preset) {
$presets = spaces_presets($space->type);
$preset = $presets[$space->preset];
if (isset($preset['locked']['features'])) {
foreach ($preset['preset']['locked']['features'] as $id => $value) {
if ($value) {
$form['features'][$id]['#disabled'] = true;
$form['features'][$id]['#locked'] = true;
}
}
}
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
'#weight' => 10,
);
return $form;
}
function _spaces_features_form($space) {
$form = array();
$form['space'] = array(
'#type' => 'value',
'#value' => $space,
);
$form['features'] = array(
'#type' => 'fieldset',
'#title' => t('Features'),
'#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.'),
'#tree' => TRUE,
);
$form['settings'] = array(
'#type' => 'fieldset',
'#title' => t('Settings'),
'#description' => t('Settings are options for customizing your group that do not affect the privacy of your content.'),
'#tree' => TRUE,
);
foreach (spaces_features($space->type) as $id => $feature) {
$options = $space
->feature_options() ? $space
->feature_options() : array();
if (count($options) > 0) {
$form['features'][$id] = array(
'#type' => 'select',
'#title' => $feature->spaces['label'],
'#description' => $feature->spaces['description'],
'#options' => $options,
'#default_value' => isset($space->features[$id]) ? $space->features[$id] : SPACES_FEATURE_DISABLED,
);
}
}
foreach (spaces_settings() as $setting) {
$setting_value = isset($space->settings[$setting->id]) ? $space->settings[$setting->id] : NULL;
$form['settings'][$setting->id] = $setting
->form($space, $setting_value);
}
return $form;
}
function spaces_features_form_validate($form_id, $form_values) {
$space = $form_values['space'];
$settings = spaces_settings();
if (isset($space->sid) && array_sum($form_values['features']) == 0) {
return form_set_error('', t('You must enable at least 1 feature for this group.'));
}
foreach ($form_values['settings'] as $setting => $value) {
$settings[$setting]
->validate($space, $value);
}
}
function spaces_features_form_submit($form_id, $form_values) {
$space = $form_values['space'];
foreach ($form_values['features'] as $feature => $value) {
$space->features[$feature] = $value;
}
$settings = spaces_settings();
foreach ($form_values['settings'] as $setting => $value) {
$space->settings[$setting] = $settings[$setting]
->submit($space, $value);
}
spaces_save($space);
drupal_set_message(t('The space configuration has been saved successfully.'));
}
function theme_spaces_features_form($form) {
drupal_add_css(drupal_get_path('module', 'spaces') . '/spaces.css');
$output = '';
foreach (array(
'features',
'settings',
) as $type) {
$header = array(
$type == 'features' ? t('Feature') : t('Setting'),
t('Status'),
t('Description'),
!isset($form['space']['#value']->sid) ? t('Locked') : '',
);
$rows = array();
foreach (element_children($form[$type]) as $element) {
$feature_name = "<strong>" . $form[$type][$element]['#title'] . "</strong>";
$description = "<div class='description'>" . $form[$type][$element]['#description'] . "</div>";
$customize = isset($form['customize'][$element]) ? drupal_render($form['customize'][$element]) : '';
$locked = isset($form['locked']) ? drupal_render($form['locked'][$type][$element]) : '';
unset($form[$type][$element]['#title']);
unset($form[$type][$element]['#description']);
$row = array(
'name' => $feature_name,
'option' => drupal_render($form[$type][$element]),
'description' => $description,
'action' => $customize . $locked,
);
foreach ($row as $key => $data) {
$row[$key] = array(
'data' => $data,
'class' => $key,
);
}
$class = $form[$type][$element]['#default_value'] ? 'enabled' : 'disabled';
$class .= $form[$type][$element]['#locked'] ? ' locked' : '';
$rows[] = array(
'data' => $row,
'class' => $class,
);
}
$output .= "<h3>" . $form[$type]['#title'] . "</h3>";
$output .= "<div class='description'>" . $form[$type]['#description'] . "</div>";
$output .= theme('table', $header, $rows, array(
'class' => 'spaces-' . $type,
));
unset($form[$type]);
}
if (isset($form['submit']) && $form['submit']['#type'] == 'submit') {
$output .= "<div class='buttons'>";
$output .= drupal_render($form['submit']);
$output .= "</div>";
}
$output .= drupal_render($form);
return $output;
}
function spaces_customize($space, $feature = NULL) {
$features = spaces_features($space->type);
if (isset($features[$feature])) {
drupal_set_title(t('Customize feature: !feature', array(
'!feature' => $features[$feature]->spaces['label'],
)));
return drupal_get_form('spaces_customize_form', $space, $feature);
}
else {
$rows = array();
foreach (spaces_features($space->type) as $id => $feature) {
if (isset($space->features[$id]) && $space->features[$id] != SPACES_FEATURE_DISABLED) {
$label = "<strong>" . $feature->spaces['label'] . "</strong>";
$description = "<div class='description'>" . $feature->spaces['description'] . "</div>";
$rows[] = array(
$label . $description,
l(t('Customize'), 'spaces/customize/' . $id),
);
}
}
return theme('table', array(
t('Available features'),
'',
), $rows);
}
}
function spaces_customize_form($space, $feature) {
$form = array();
$form['space'] = array(
'#type' => 'value',
'#value' => $space,
);
$form['feature'] = array(
'#type' => 'value',
'#value' => $feature,
);
$customizers = spaces_customizers();
foreach ($customizers as $id => $customizer) {
$form[$id] = array(
'#title' => t($customizer->name),
'#type' => 'fieldset',
'#tree' => TRUE,
'#theme' => 'spaces_customize_item',
'#collapsible' => TRUE,
);
$form[$id] = $form[$id] + $customizer
->form($space, $feature);
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save settings'),
);
$form['reset'] = array(
'#type' => 'submit',
'#value' => t('Reset to defaults'),
);
$form['#theme'] = 'spaces_form';
return $form;
}
function spaces_customize_form_validate($form_id, $form_values) {
$space = $form_values['space'];
$feature = $form_values['feature'];
$customizers = spaces_customizers();
foreach ($customizers as $id => $customizer) {
$customizer
->validate($space, $feature, $form_values[$id]);
}
}
function spaces_customize_form_submit($form_id, $form_values) {
$space = $form_values['space'];
$feature = $form_values['feature'];
$customizers = spaces_customizers();
switch (trim($form_values['op'])) {
case t('Save settings'):
$feature_customizer = array();
foreach ($customizers as $id => $customizer) {
$feature_customizer[$id] = $customizer
->submit($space, $feature, $form_values[$id]);
}
$space->customizer[$feature] = $feature_customizer;
spaces_save($space);
drupal_set_message(t('Customizations saved for space !title.', array(
'!title' => $space->title,
)));
break;
case t('Reset to defaults'):
unset($space->customizer[$feature]);
spaces_save($space);
break;
}
}
function theme_spaces_customize_item($form) {
$output = '';
$rows = array();
foreach (element_children($form) as $element) {
if ($form[$element]['#type'] == 'fieldset') {
$title = $form[$element]['#title'];
unset($form[$element]['#title']);
unset($form[$element]['#type']);
$rows[] = array(
"<strong>{$title}</strong>",
drupal_render($form[$element]),
);
}
}
$output .= theme('table', array(), $rows);
return $output;
}