You are here

spaces_admin.inc in Spaces 5

Same filename and directory in other branches
  1. 5.2 spaces_admin.inc
  2. 6 spaces_admin.inc
  3. 6.2 spaces_admin.inc

File

spaces_admin.inc
View source
<?php

/**
 * Define form for controlling features
 */
function spaces_features_form($nid) {
  drupal_set_title(t('Group Features'));

  // apparently 'title' for local tasks only sets the tab name
  // Load defaults -- check first for group specific, then site defaults
  $default_values = array(
    'features' => spaces_features($nid) ? spaces_features($nid) : (spaces_features(0) ? spaces_features(0) : array()),
    'settings' => spaces_settings($nid) ? spaces_settings($nid) : (spaces_settings(0) ? spaces_settings(0) : array()),
  );
  $form['nid'] = array(
    '#type' => 'hidden',
    '#value' => $nid,
  );
  $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,
  );
  if ($nid > 0) {
    $node = node_load($nid);
    $mask = array(
      'og_selective' => $node->og_selective,
      'og_directory' => $node->og_directory,
      'og_register' => $node->og_register,
      'og_private' => $node->og_private,
    );
    $mask = spaces_groupmask('check', $mask);
    $spaces_masks = spaces_groupmask('mask');
    $mask = $spaces_masks[$mask];
    if (isset($mask['limit options'])) {
      $allowed_options = $mask['limit options'];
    }
  }

  // Generate features form
  foreach (spaces_features() as $id => $feature) {
    if (is_array($feature->spaces['options'])) {
      $options = $feature->spaces['options'];

      // Only limit options if feature is associated with content types
      if (isset($allowed_options) && count($feature->node)) {
        foreach ($options as $key => $value) {
          if ($key != 0 && !in_array($key, $allowed_options)) {
            unset($options[$key]);
          }
        }
      }
    }
    else {
      if (is_string($feature->spaces['options']) && function_exists($feature->spaces['options'])) {
        $option_func = $feature->spaces['options'];
        $options = $option_func();
      }
      else {
        $options = array();
      }
    }

    // TODO resolve this issue - I'm not certain how to best handle OG ommited content types and if to exclude them from being used in a feature.
    // Skip settings for any feature that requires an OG omitted content type
    // if (count($feature['content_types']) && count(array_intersect(variable_get('og_omitted', array()), $feature['content_types']))) {
    //   continue;
    // }
    if (count($options) > 0) {
      $form['features'][$id] = array(
        '#type' => 'select',
        '#title' => $feature->spaces['label'],
        '#description' => $feature->spaces['description'],
        '#options' => $options,
        '#default_value' => isset($default_values['features'][$id]) ? $default_values['features'][$id] : 0,
      );
    }
  }

  // Generate settings form
  foreach (spaces_settings() as $id => $setting) {
    if (is_array($setting['options'])) {
      $options = $setting['options'];
    }
    else {
      if (is_string($setting['options']) && function_exists($setting['options'])) {
        $option_func = $setting['options'];
        $options = $option_func();
      }
      else {
        $options = array();
      }
    }
    $form['settings'][$id] = array(
      '#type' => 'select',
      '#title' => $setting['label'],
      '#description' => $setting['description'],
      '#options' => $options,
      '#default_value' => isset($default_values['settings'][$id]) ? $default_values['settings'][$id] : 0,
    );
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );
  return $form;
}

/**
 * Validate handler for spaces features form
 */
function spaces_features_form_validate($form_id, $form_values) {
  $enabled_features = array_sum($form_values['features']);
  if (!$enabled_features) {
    return form_set_error('', t('You must enable at least 1 feature for this group.'));
  }
  if (!$form_values['settings']['spaces_home']) {
    return form_set_error('settings][spaces_home', t('You must choose a homepage for this group.'));
  }
  else {
    if ($homepage = $form_values['settings']['spaces_home']) {
      if (!$form_values['features'][$homepage]) {
        return form_set_error('settings][spaces_home', t('You must choose an enabled feature for use as this group\'s homepage.'));
      }
    }
  }
}

/**
 * Submit handler for spaces features form
 */
function spaces_features_form_submit($form_id, $form_values) {
  $available = array(
    'features' => spaces_features(),
    'settings' => spaces_settings(),
  );
  db_query('DELETE FROM {spaces_features} WHERE gid = %d', $form_values['nid']);
  foreach ($available as $type => $items) {
    foreach ($items as $id => $item) {
      if (isset($form_values[$type][$id])) {
        db_query('INSERT INTO {spaces_features} (gid, type, id, value) VALUES (%d, %d, "%s", "%s")', $form_values['nid'], $type == 'features' ? 0 : 1, $id, $form_values[$type][$id]);
      }
    }
  }
  drupal_set_message(t('Feature settings have been saved.'));
}

/**
 * Theme for spaces featuers/settings form.
 */
function theme_spaces_features_form($form) {
  drupal_add_css(drupal_get_path('module', 'spaces') . '/spaces.css');
  $output = '';
  foreach (array(
    'features',
    'settings',
  ) as $type) {
    $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>";
      unset($form[$type][$element]['#title']);
      unset($form[$type][$element]['#description']);
      $rows[] = array(
        'data' => array(
          $feature_name,
          drupal_render($form[$type][$element]),
          $description,
        ),
        'class' => $form[$type][$element]['#default_value'] ? 'enabled' : 'disabled',
      );
    }
    $output .= "<h3>" . $form[$type]['#title'] . "</h3>";
    $output .= "<div class='description'>" . $form[$type]['#description'] . "</div>";
    unset($form[$type]);
    $output .= theme('table', array(), $rows, array(
      'class' => 'spaces-' . $type,
    ));
  }
  $output .= "<div class='buttons'>" . drupal_render($form['submit']) . "</div>";
  $output .= drupal_render($form);
  return $output;
}

/**
 * Menu customization form.
 */
function spaces_features_labels($nid) {
  drupal_set_title(t('Menu Labels'));
  $form = array();
  $weights = array();
  for ($i = -10; $i <= 10; $i++) {
    $weights[$i] = $i;
  }
  $spaces_features = spaces_features();
  $active_features = spaces_features($nid);

  // Check for active features
  if ($active_features != false) {
    $form['help'] = array(
      '#type' => 'item',
      '#value' => "<p>" . t("You can customize this group's menu by entering custom labels and weights. Any changes you make will only affect this group and can be undone using the <strong>Reset</strong> button.") . "</p>",
    );
    $form['nid'] = array(
      '#type' => 'hidden',
      '#value' => $nid,
    );
    foreach ($active_features as $feature => $active) {
      if ($active && isset($spaces_features[$feature]->menu) && ($menu = _spaces_feature_menu_tree($spaces_features[$feature], null, $nid))) {
        $path = $menu['href'];
        $form[$feature] = array(
          '#title' => $spaces_features[$feature]->spaces['label'],
          '#description' => $spaces_features[$feature]->spaces['description'],
          '#tree' => TRUE,
        );
        $form[$feature][$path]['label'] = array(
          '#type' => 'textfield',
          '#title' => $menu['title'],
          '#description' => $menu['href'],
          '#default_value' => $menu['title'],
          '#size' => 30,
          '#required' => true,
        );
        $form[$feature][$path]['weight'] = array(
          '#type' => 'select',
          '#default_value' => $menu['#weight'] ? $menu['#weight'] : 0,
          '#options' => $weights,
          '#required' => true,
        );
        if (isset($menu['children'])) {
          $form[$feature]['#title'] = $spaces_features[$feature]->spaces['label'];
          foreach ($menu['children'] as $child) {
            $form[$feature][$child['href']]['label'] = array(
              '#type' => 'textfield',
              '#title' => $child['title'],
              '#description' => $child['href'],
              '#default_value' => $child['title'],
              '#size' => 30,
              '#required' => true,
            );
            $form[$feature][$child['href']]['weight'] = array(
              '#type' => 'select',
              '#default_value' => $child['#weight'] ? $child['#weight'] : 0,
              '#options' => $weights,
              '#required' => true,
            );
          }
        }
      }
    }
    $form['#theme'] = 'spaces_features_labels';
    $form['save'] = array(
      '#type' => 'submit',
      '#value' => t('Save'),
    );
    $form['reset'] = array(
      '#type' => 'submit',
      '#value' => t('Reset'),
    );
  }
  else {
    $form['error'] = array(
      '#value' => t('No active features in this group. You must enable features before customizing labels.'),
    );
  }
  return $form;
}

/**
 * Form theme for spaces_features_labels()
 */
function theme_spaces_features_labels($form) {
  $output .= drupal_render($form['help']);
  $rows = array();
  foreach (spaces_features($form['nid']['#value']) as $feature => $dummy) {
    $feature_label = false;
    if (is_array($form[$feature])) {
      foreach ($form[$feature] as $path => $item) {
        $row = array();
        if (is_array($item) && isset($item['label'])) {
          $path = $item['label']['#description'];
          unset($form[$feature][$path]['label']['#description']);
          $rows[] = array(
            !$feature_label ? "<strong>" . $form[$feature]['#title'] . "</strong>" : '',
            $path,
            drupal_render($form[$feature][$path]['label']),
            drupal_render($form[$feature][$path]['weight']),
          );
          $feature_label = true;
        }
      }
    }
  }
  $output .= theme('table', array(
    t('Feature'),
    t('Path'),
    t('Label'),
    t('Weight'),
  ), $rows);
  $output .= "<div class='buttons'>" . drupal_render($form['save']) . drupal_render($form['reset']) . "</div>";
  $output .= drupal_render($form);
  return $output;
}

/**
 * Submit handler for the menu customization form.
 */
function spaces_features_labels_submit($form_id, $form_values) {
  if ($form_values['op'] == t('Save')) {
    $labels = array();
    $weights = array();
    foreach (spaces_features($form_values['nid']) as $feature => $dummy) {
      if (is_array($form_values[$feature])) {
        foreach ($form_values[$feature] as $path => $item) {
          if ($label = $item['label']) {

            // check for empty strings
            $label = trim($label);
            if (!empty($label)) {
              $labels[$path] = $label;
            }
          }
          if ($weight = $item['weight']) {
            $weights[$path] = $weight;
          }
        }
      }
    }
    db_query("REPLACE INTO {spaces_features_custom} VALUES(%d, '%s', '%s')", $form_values['nid'], serialize($labels), serialize($weights));
    drupal_set_message(t('Feature lables have been saved.'));
  }
  else {
    if ($form_values['op'] == t('Reset')) {
      db_query("DELETE FROM {spaces_features_custom} WHERE gid = %d", $form_values['nid']);
      drupal_set_message(t("This group's labels and weights have been reset to their default values."));
    }
  }
}

/**
 *  View header customization form - ALPHA
 *
 * Allows group administrators to set a node to be used as the header text for a view in a specific group.
 *
 * TODO
 * - We need a much better UI
 * - The data shoudn't all be stored in the variables table.
 */
function spaces_view_headers() {
  $nid = arg(1);
  $spaces_features = spaces_features();
  $active_features = spaces_features($nid);
  if (count($active_features)) {
    $form = array();
    $form[] = array(
      '#value' => t('<p>For each view you can select a node to display as the intro text to the listing.</p>'),
    );
    foreach ($active_features as $feature => $active) {
      if ($active && count($spaces_features[$feature]->views)) {
        $form[$feature] = array(
          '#type' => 'fieldset',
          '#title' => $spaces_features[$feature]->spaces['label'],
          '#collapsible' => true,
        );
        foreach ($spaces_features[$feature]->views as $view) {
          $form[$feature]["spaces_header_" . $view . "_" . $nid] = array(
            '#title' => t($view . ' header'),
            '#type' => 'textfield',
            '#maxlength' => 10,
            '#default_value' => variable_get("spaces_header_" . $view . "_" . $nid, ''),
          );
        }
      }
    }
  }
  return system_settings_form($form);
}

Functions

Namesort descending Description
spaces_features_form Define form for controlling features
spaces_features_form_submit Submit handler for spaces features form
spaces_features_form_validate Validate handler for spaces features form
spaces_features_labels Menu customization form.
spaces_features_labels_submit Submit handler for the menu customization form.
spaces_view_headers View header customization form - ALPHA
theme_spaces_features_form Theme for spaces featuers/settings form.
theme_spaces_features_labels Form theme for spaces_features_labels()