You are here

function context_ui_form in Context 6

Same name in this branch
  1. 6 context_ui/context_ui.admin.inc \context_ui_form()
  2. 6 context_ui/export_ui/context_export_ui.class.php \context_ui_form()
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.2 context_ui/context_ui.admin.inc \context_ui_form()
  4. 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.

4 string references to 'context_ui_form'
context.inc in context_ui/export_ui/context.inc
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 89

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
  foreach ($blocks[0] as $block) {
    $block_options[$block->module][$block->bid] = $block->label;
  }
  ksort($block_options);
  $form['block'] = array(
    '#tree' => true,
  );
  $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[] = $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' => 20,
          '#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' => $block->label,
          '#weight' => $block->weight,
          'weight' => array(
            '#type' => 'weight',
            '#delta' => 20,
            '#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'),
    );
  }
  return $form;
}