You are here

function context_ui_form in Context 6.2

Same name and namespace in other branches
  1. 5 context_ui/context_ui_admin.inc \context_ui_form()
  2. 6.3 context_ui/export_ui/context_export_ui.class.php \context_ui_form()
  3. 6 context_ui/context_ui.admin.inc \context_ui_form()
  4. 6 context_ui/export_ui/context_export_ui.class.php \context_ui_form()
  5. 7.3 context_ui/export_ui/context_export_ui.class.php \context_ui_form()

Generates the omnibus context definition editing form. Note: submission and validation handlers are in context_ui.admin.inc

Parameters

$op: The type of form to build. Either "add", "view" or "edit"

$cid: The db context identifier - required when $op == "edit"

Return value

A Drupal form array.

3 string references to 'context_ui_form'
context_ui_import_page in context_ui/context_ui.admin.inc
Page callback for import form. Switches form output to context form if import submission has occurred.
context_ui_import_submit in context_ui/context_ui.admin.inc
Import form submit handler. Evaluates import code and transfers to context definition form.
context_ui_menu in context_ui/context_ui.module
Implementation of hook_menu().

File

context_ui/context_ui.admin.inc, line 160

Code

function context_ui_form(&$form_state, $op, $context = NULL) {
  switch ($op) {
    case 'add':
      drupal_set_title(t('Add a new context'));
      break;
    case 'edit':
      if (!$context->system) {
        drupal_set_title(t('Edit context: %title', array(
          '%title' => $context->value,
        )));
      }
      else {
        drupal_set_title(t('View %title', array(
          '%title' => $context->value,
        )));
      }
      break;
    case 'clone':
      drupal_set_title(t('Clone context: %title', array(
        '%title' => $context->value,
      )));
      $context->system = 0;
      $context->cid = NULL;
      $cid = NULL;
      break;
  }

  // Initialize context object if it doesn't already exist
  $context = !$context ? new stdClass() : $context;

  // Core context definition
  $form = array(
    '#base' => 'context_ui_form',
    '#theme' => 'context_ui_form',
  );
  $form['cid'] = array(
    '#type' => 'value',
    '#value' => isset($context->cid) ? $context->cid : NULL,
  );
  $form['system'] = array(
    '#type' => 'value',
    '#value' => isset($context->system) ? $context->system : 0,
  );
  foreach (array(
    'value',
    'attribute',
    'namespace',
    'description',
  ) as $field) {
    $form[$field] = array(
      '#type' => 'textfield',
      '#required' => TRUE,
      '#maxlength' => 64,
      '#size' => 20,
      '#disabled' => !empty($context->system) ? TRUE : FALSE,
      '#default_value' => isset($context->{$field}) ? $context->{$field} : '',
      '#title' => t(ucfirst($field)),
    );
  }
  $form['value']['#description'] = t('A system name for this context. May only contain lowercase letters, underscores, and numbers. Example: <strong>science_blog</strong>');
  $form['attribute']['#default_value'] = empty($form['attribute']['#default_value']) ? 'section' : $form['attribute']['#default_value'];
  $form['attribute']['#description'] = t('The type of context information provided in this namespace. Example: <strong>section</strong>');
  $form['namespace']['#default_value'] = empty($form['namespace']['#default_value']) ? 'context_ui' : $form['namespace']['#default_value'];
  $form['namespace']['#description'] = t('The namespace for this context definition. Example: <strong>context_ui</strong>');
  $form['description']['#required'] = FALSE;
  $form['description']['#size'] = 40;
  $form['description']['#maxlength'] = 255;
  $form['description']['#description'] = t('The description of this context definition.');
  $form['items'] = array(
    '#tree' => TRUE,
  );

  // We need to initialize theme in order to deal with blocks
  // and also let themes integrate against context_ui
  init_theme();
  $theme_key = variable_get('theme_default', 'garland');

  // Generate settings for context item associations
  foreach (context_conditions(TRUE) + context_reactions(TRUE) as $id => $info) {
    $form['items'][$id] = $info;

    // Disable element
    if (isset($context->system) && $context->system) {
      $form['items'][$id]['#disabled'] = TRUE;
    }

    // Default values
    if (isset($context->{$id})) {
      $form['items'][$id] = context_ui_default_values($form['items'][$id], $context->{$id});
    }
  }
  $modules = array();
  $query = db_query("SELECT name, info FROM {system} WHERE type = '%s'", 'module');
  while ($result = db_fetch_object($query)) {
    $info = unserialize($result->info);
    $modules[$result->name] = $info['name'];
  }

  // Control block visibility
  $block_options = $block_defaults = array();
  $blocks = _context_ui_get_blocks();
  $regions = system_region_list($theme_key);

  // $blocks in [0] have not been assigned a region
  // Weights range from -delta to +delta, so delta should be at least half
  // of the amount of blocks present. This makes sure all blocks in the same
  // region get an unique weight.
  $block_count = 0;
  foreach ($blocks as $region => $block_list) {
    $block_count += count($block_list);
  }

  // Add 2 to make sure there's space at either end of the block list
  $weight_delta = round(($block_count + 2) / 2);
  foreach ($blocks[0] as $block) {
    $block_options[$block->module][$block->bid] = check_plain($block->label);
  }
  ksort($block_options);
  $form['block'] = array(
    '#tree' => TRUE,
  );

  //Save the value to use in the process function
  $form['block']['max_block_weight'] = array(
    '#value' => $weight_delta,
    '#type' => 'value',
  );
  $form['block']['help'] = array(
    '#type' => 'markup',
    '#value' => t('Control block visibility using context. Selected blocks will be shown when this context is set provided that custom block visibility settings and/or throttling do not hide them. Grayed out blocks are those provided by Drupal\'s standard block settings. These settings apply to the current theme and any enabled themes with regions in common.'),
  );
  $form['block']['selector'] = array(
    '#type' => 'item',
    '#tree' => TRUE,
    '#prefix' => '<div class="context-ui-block-selector">',
    '#suffix' => '</div>',
  );
  foreach ($block_options as $module => $module_blocks) {
    if (!empty($module_blocks)) {
      $form['block']['selector'][$module] = array(
        '#type' => 'checkboxes',
        '#title' => $modules[$module],
        '#options' => $module_blocks,
        '#disabled' => isset($context->system) && $context->system ? TRUE : FALSE,
      );
    }
  }
  $form['block']['blocks'] = array(
    '#tree' => TRUE,
    '#theme' => 'context_ui_block_ui',
  );
  foreach ($regions as $region => $label) {
    $defaults = array();
    $midpoint = FALSE;
    $form['block']['blocks'][$region] = array(
      '#type' => 'item',
      '#title' => $label,
      '#tree' => TRUE,
    );
    $system = _context_ui_get_blocks($region);
    if ($system) {
      $system_blocks = array();
      foreach ($system as $block) {
        $system_blocks[] = check_plain($block->label);
      }
      $system_blocks = "<span class='system-blocks'>" . implode(", ", $system_blocks) . "</span";
      $form['block']['blocks'][$region]['system'] = array(
        '#type' => 'markup',
        '#tree' => TRUE,
        '#value' => $system_blocks,
        '#weight' => 0,
        'weight' => array(
          '#type' => 'weight',
          '#delta' => $weight_delta,
          '#default_value' => 0,
        ),
      );
    }
    $i = 0;
    foreach (_context_ui_get_blocks($region, $context) as $block) {
      if ($block->type == 'context_ui') {
        $form['block']['blocks'][$region][$block->bid] = array(
          '#type' => 'markup',
          '#tree' => TRUE,
          '#value' => check_plain($block->label),
          '#weight' => $block->weight,
          'weight' => array(
            '#type' => 'weight',
            '#delta' => $weight_delta,
            '#default_value' => $block->weight,
          ),
        );
      }
      $i++;
    }
    uasort($form['block']['blocks'][$region], 'element_sort');
    $defaults = implode(',', element_children($form['block']['blocks'][$region]));
    $form['block']['regions'][$region] = array(
      '#type' => 'hidden',
      '#default_value' => $defaults,
    );
  }
  if (!empty($context->system)) {
    $form['back'] = array(
      '#type' => 'item',
      '#value' => l(t('Back'), 'admin/build/context'),
    );
  }
  else {
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Save'),
    );
  }
  if (empty($context->system) && $op == 'edit') {
    $form['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete'),
      // Skip validation for this button, as we're just doing a redirect:
      '#validate' => array(),
      '#submit' => array(
        'context_ui_form_delete_submit',
      ),
    );
  }
  return $form;
}