View source
<?php
function spaces_ui_init() {
if (arg(0) . '/' . arg(1) . '/' . arg(2) . '/' . arg(3) == 'admin/build/spaces/features') {
include_once drupal_get_path("module", "context_ui") . "/context_ui_admin.inc";
}
}
function spaces_ui_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'admin/build/spaces/features',
'title' => t('Features'),
'description' => t('Page listing spaces features.'),
'callback' => 'spaces_ui_features',
'access' => user_access('administer spaces'),
'type' => MENU_LOCAL_TASK,
);
$items[] = array(
'path' => 'admin/build/spaces/features/list',
'title' => t('List'),
'description' => t('Page listing spaces features.'),
'callback' => 'spaces_ui_features',
'access' => user_access('administer spaces'),
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => 0,
);
$items[] = array(
'path' => 'admin/build/spaces/features/add',
'title' => t('Add'),
'description' => t('Add a new feature.'),
'callback' => 'drupal_get_form',
'callback arguments' => array(
'context_ui_form',
'add',
),
'access' => user_access('administer spaces'),
'type' => MENU_LOCAL_TASK,
'weight' => 1,
);
}
return $items;
}
function spaces_ui_context_define() {
$items = array();
$result = db_query('SELECT feature, value FROM {spaces_features_ui}');
while ($row = db_fetch_object($result)) {
$c = new StdClass();
$c->namespace = 'spaces';
$c->attribute = 'feature';
$c->value = $row->feature;
if ($c = context_ui_context('load', $c)) {
$c->spaces = unserialize($row->value);
$c->system = 0;
$c->status = 1;
$items[] = (array) $c;
}
}
return $items;
}
function spaces_ui_form_alter($form_id, &$form) {
switch ($form_id) {
case 'context_ui_delete_confirm':
$cid = $form['cid']['#value'];
$context = context_ui_context('load', $cid);
if ($context->namespace == 'spaces' && $context->attribute == 'feature') {
$form['spaces_feature'] = array(
'#type' => 'value',
'#value' => $context->value,
);
$form['#submit']['spaces_ui_feature_delete'] = array();
$form['#submit'] = array_reverse($form['#submit']);
}
break;
case 'context_ui_form':
if ($form['namespace']['#default_value'] == 'spaces' && $form['attribute']['#default_value'] == 'feature') {
$is_feature = TRUE;
}
else {
$is_feature = FALSE;
}
if (!empty($form['value']['#default_value'])) {
$id = $form['value']['#default_value'];
$row = db_fetch_object(db_query('SELECT * FROM {spaces_features_ui} WHERE feature = "%s"', $id));
if ($row) {
$feature = unserialize($row->value);
}
else {
$features = spaces_features();
if (isset($features[$id])) {
$feature = $features[$id]->spaces;
}
}
}
$form['feature'] = array(
'#type' => 'fieldset',
'#title' => t('Spaces feature'),
'#weight' => -5,
'#tree' => true,
);
$form['feature']['label'] = array(
'#type' => 'textfield',
'#title' => t('Label'),
'#description' => t('Name of the feature.'),
'#required' => true,
'#maxlength' => 30,
'#default_value' => isset($feature['label']) ? $feature['label'] : '',
);
$form['feature']['description'] = array(
'#type' => 'textfield',
'#title' => t('Description'),
'#description' => t('A brief description of the feature.'),
'#required' => true,
'#default_value' => isset($feature['description']) ? $feature['description'] : '',
);
$options = array(
0 => t('All spaces'),
);
foreach (spaces_types() as $type => $info) {
$options[$type] = $info['title'];
}
$form['feature']['types'] = array(
'#type' => 'select',
'#title' => t('Compatibility'),
'#description' => t('Choose the space type compatible with this feature.'),
'#required' => true,
'#options' => $options,
'#default_value' => isset($feature['types']) ? $feature['types'] : 0,
);
$menu_items = isset($feature['menu']) ? array_values($feature['menu']) : array();
$menu_paths = isset($feature['menu']) ? array_keys($feature['menu']) : array();
$form['feature']['menu'] = array(
'#tree' => true,
'#theme' => 'spaces_ui_feature_form_menu',
);
$form['feature']['menu']['help'] = array(
'#title' => t('Feature menu'),
'#type' => 'item',
'#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".'),
);
for ($i = 0; $i < 10; $i++) {
$form['feature']['menu'][$i] = array(
'#tree' => true,
);
$form['feature']['menu'][$i]['path'] = $form['feature']['menu'][$i]['title'] = array(
'#type' => 'textfield',
'#size' => 30,
);
$form['feature']['menu'][$i]['path']['#default_value'] = isset($menu_paths[$i]) ? $menu_paths[$i] : '';
$form['feature']['menu'][$i]['title']['#default_value'] = isset($menu_items[$i]['title']) ? $menu_items[$i]['title'] : '';
}
$form['attribute']['#disabled'] = true;
$form['attribute']['#default_value'] = 'feature';
$form['namespace']['#disabled'] = true;
$form['namespace']['#default_value'] = 'spaces';
$form['#submit']['spaces_ui_feature_form_submit'] = array();
$form['#submit'] = array_reverse($form['#submit']);
$form['#redirect'] = 'admin/build/spaces/features';
break;
}
}
function spaces_ui_feature_form_submit($form_id, $form_values) {
$feature = $form_values['value'];
$settings = array();
$settings['label'] = $form_values['feature']['label'];
$settings['description'] = $form_values['feature']['description'];
if ($form_values['feature']['types'] == 0) {
$settings['types'] = array();
}
else {
$settings['types'] = array(
$form_values['feature']['types'],
);
}
$settings['menu'] = array();
foreach ($form_values['feature']['menu'] as $item) {
if (!empty($item['title']) && !empty($item['path'])) {
$settings['menu'][$item['path']] = array(
'title' => $item['title'],
);
}
}
ksort($settings['menu']);
db_query('DELETE FROM {spaces_features_ui} WHERE feature = "%s"', $feature);
db_query('INSERT INTO {spaces_features_ui} (feature, value) VALUES ("%s", "%s")', $feature, serialize($settings));
}
function spaces_ui_feature_delete($form_id, $form_values) {
$feature = $form_values['spaces_feature'];
db_query('DELETE FROM {spaces_features_ui} WHERE feature = "%s"', $feature);
}
function spaces_ui_features() {
drupal_set_title(t('Spaces features'));
$spaces_types = spaces_types();
$spaces_features = spaces_features();
$output = '';
$rows = array();
foreach ($spaces_features as $id => $feature) {
$context = context_ui_context('load', $feature);
$label = "<strong>" . $feature->spaces['label'] . "</strong>";
$label .= "<div class='description'>" . $feature->spaces['description'] . "</div>";
$links = array();
if ($context->system == 0) {
$links[] = l(t('Edit'), 'admin/build/context/edit/' . $context->cid);
$links[] = l(t('Delete'), 'admin/build/context/delete/' . $context->cid, array(), 'destination=admin/build/spaces/features');
}
else {
$links[] = l(t('Override'), 'admin/build/context/clone/' . $context->cid);
}
$links = implode(' | ', $links);
if (isset($feature->spaces['types']) && count($feature->spaces['types'])) {
$types = array();
foreach ($feature->spaces['types'] as $type) {
$types[] = $spaces_types[$type]['title'];
}
$types = implode(', ', $types);
}
else {
$types = t('All spaces');
}
$rows[] = array(
$label,
$types,
$links,
);
}
$header = array(
t('Feature'),
t('Usable for'),
t('Actions'),
);
$output .= theme('table', $header, $rows);
return $output;
}
function theme_spaces_ui_feature_form_menu($form) {
drupal_add_js(drupal_get_path('module', 'spaces_ui') . '/spaces_ui.js');
drupal_add_css(drupal_get_path('module', 'spaces_ui') . '/spaces.css');
$output = drupal_render($form['help']);
$rows = array();
foreach (element_children($form) as $element) {
if (is_numeric($element)) {
$class = !$form[$element]['title']['#default_value'] ? 'hidden' : '';
$rows[] = array(
'data' => array(
drupal_render($form[$element]['title']),
drupal_render($form[$element]['path']),
l(t('Remove'), $_GET['q'], array(
'class' => 'spaces-remove-menu',
)),
),
'class' => $class,
);
}
}
$rows[] = array(
array(
'data' => l(t('Add menu item'), $_GET['q'], array(
'class' => 'button spaces-add-menu',
)),
'colspan' => 3,
'class' => 'actions',
),
);
$output .= theme('table', array(
t('Title'),
t('Path'),
'',
), $rows, array(
'class' => 'spaces-admin',
));
return $output;
}