You are here

function context_ui_form in Context 5

Same name and namespace in other branches
  1. 6.3 context_ui/export_ui/context_export_ui.class.php \context_ui_form()
  2. 6 context_ui/context_ui.admin.inc \context_ui_form()
  3. 6 context_ui/export_ui/context_export_ui.class.php \context_ui_form()
  4. 6.2 context_ui/context_ui.admin.inc \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 51

Code

function context_ui_form($op, $cid = NULL, $context = NULL) {
  drupal_add_css(drupal_get_path("module", "context_ui") . "/context_ui.css");
  drupal_add_js(drupal_get_path("module", "context_ui") . "/context_ui.js");
  switch ($op) {
    case 'add':
      if (is_object($context)) {
        $context->system = true;
        if ($exists = context_ui_context('load', $context)) {
          drupal_set_message(t('A module is already providing a context with this namespace/attribute/value identifier. Your context definition will override its settings.'));
        }
        $context->system = false;
      }
      break;
    case 'view':
      if (is_numeric($cid) && ($context = context_ui_context('load', $cid))) {
        drupal_set_title(t('View %title', array(
          '%title' => $context->value,
        )));
      }
      else {
        drupal_goto('admin/build/context');
        return;
      }
      break;
    case 'edit':
      if (is_numeric($cid) && ($context = context_ui_context('load', $cid))) {
        if (!$context->system) {
          drupal_set_title(t('Edit context: %title', array(
            '%title' => $context->value,
          )));
        }
        else {
          drupal_goto('admin/build/context');
          return;
        }
      }
      break;
    case 'clone':
      if (is_numeric($cid) && ($context = context_ui_context('load', $cid))) {
        drupal_set_title(t('Clone context: %title', array(
          '%title' => $context->value,
        )));
        $context->system = 0;
        $context->cid = null;
        $cid = null;
      }
      else {
        drupal_goto('admin/build/context');
        return;
      }
      break;
  }

  // Core context definition
  $form = array(
    '#base' => 'context_ui_form',
    '#theme' => 'context_ui_form',
  );
  $form['value'] = $form['attribute'] = $form['namespace'] = array(
    '#type' => 'textfield',
    '#required' => true,
    '#maxlength' => 64,
    '#size' => 20,
  );
  $form['value']['#title'] = t('Value');
  $form['value']['#description'] = t('A system name for this context. May only contain lowercase letters, underscores, and numbers. Example: <b>science_blog</b>');
  $form['attribute']['#title'] = t('Attribute');
  $form['attribute']['#default_value'] = 'section';
  $form['attribute']['#description'] = t('The type of context information provided in this namespace. Example: <b>section</b>');
  $form['namespace']['#title'] = t('Namespace');
  $form['namespace']['#default_value'] = 'context_ui';
  $form['namespace']['#description'] = t('The namespace for this context definition. Example: <b>context_ui</b>');
  $form['items'] = array(
    '#tree' => true,
  );

  // Generate settings for context item associations
  foreach (context_ui_types('full') as $type => $item) {
    if (in_array($item['#type'], array(
      'select',
      'radios',
      'checkboxes',
      'textfield',
    ))) {
      $form['items'][$type] = $item;
    }
  }

  // Control block visibility
  init_theme();

  // we need to initialize theme in order to deal with blocks
  global $theme_key;
  $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) {
    if (!isset($context->block[$block->bid])) {
      $block_options[$block->module][$block->bid] = $block->label . " ({$block->bid})";
    }
  }
  ksort($block_options);
  $form['block'] = array(
    '#tree' => true,
  );
  $form['block']['selector'] = array(
    '#description' => 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.'),
    '#type' => 'item',
    '#tree' => true,
    '#prefix' => '<div class="context-ui-block-selector">',
    '#suffix' => '</div>',
    'blocks' => array(
      '#type' => 'select',
      '#multiple' => true,
      '#size' => 15,
      '#title' => t('Blocks'),
      '#options' => $block_options,
    ),
    'regions' => array(
      '#type' => 'select',
      '#title' => t('Regions'),
      '#options' => $regions,
    ),
    'add' => array(
      '#type' => 'markup',
      '#value' => "<input id='edit-block-selector-add' class='form-submit' type='button' value='+ " . t('Add') . "'/>",
    ),
  );
  $form['block']['regions'] = array(
    '#type' => 'item',
    '#tree' => true,
    '#prefix' => '<div class="context-ui-block-regions">',
    '#suffix' => '</div>',
    '#value' => theme('context_ui_block_ui', $regions, $context),
  );
  foreach (array_keys($regions) as $region) {
    $defaults = array();
    $midpoint = false;
    foreach (_context_ui_get_blocks($region, $context) as $block) {
      if ($block->type == 'context_ui') {
        $defaults[] = $block->bid;
      }
      else {
        if (!$midpoint) {
          $midpoint = true;
          $defaults[] = 'system';
        }
      }
    }
    if (!$defaults) {
      $defaults = array(
        'system',
      );
    }
    $defaults = implode(',', $defaults);
    $form['block']['regions'][$region] = array(
      '#type' => 'hidden',
      '#default_value' => $defaults,
    );
  }
  if ($op == 'view') {
    $form['back'] = array(
      '#type' => 'item',
      '#value' => l(t('Back'), 'admin/build/context'),
    );
  }
  if ($op != 'view') {
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Save'),
    );
  }
  if ($op == 'edit') {
    $form['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete'),
    );
  }
  if ($op == 'view' || $op == 'edit' || $op == 'clone' || $op == 'add' && $context) {
    if ($context) {
      $form['value']['#default_value'] = $context->value;
      $form['attribute']['#default_value'] = $context->attribute;
      $form['namespace']['#default_value'] = $context->namespace;
      $form['cid'] = array(
        '#type' => 'value',
        '#value' => $cid,
      );
      $form['system'] = array(
        '#type' => 'value',
        '#value' => $context->system,
      );
      if ($op == 'view' || $context->system) {
        $form['value']['#disabled'] = $form['attribute']['#disabled'] = $form['namespace']['#disabled'] = $form['block']['selector']['blocks']['#disabled'] = $form['block']['selector']['regions']['#disabled'] = true;
      }

      // Set default values for each item type (except blocks)
      foreach (context_ui_types('full') as $type => $item) {
        if (is_array($context->{$type})) {
          if ($item['#type'] == 'checkboxes' || $item['#type'] == 'select' && $item['#multiple'] == true) {
            $defaults = array();
            foreach ($context->{$type} as $id) {
              $defaults[$id] = $id;
            }
          }
          else {
            $defaults = current($context->{$type});
          }
          $form['items'][$type]['#default_value'] = $defaults;
        }
        $form['items'][$type]['#disabled'] = $op == 'view' ? true : false;
      }

      // Blocks must be selected by region
      if (is_array($context->block)) {
        foreach ($regions as $region => $label) {
          if (isset($form['block'][$region]) && is_array($form['block'][$region])) {
            $defaults = array();
            foreach ($form['block'][$region]['#options'] as $block => $label) {
              if (array_search($block, $context->block) !== false) {
                $defaults[$block] = $block;
              }
            }
            $form['block'][$region]['#default_value'] = $defaults;
          }
          $form['block'][$region]['#disabled'] = $op == 'view' ? true : false;
        }
      }
    }
    else {
      return drupal_goto('admin/build/context');
    }
  }
  return $form;
}