You are here

function OLDpurge_item_form in Purge 7.2

Menu callback for purge item settins. @TODO REMOVE THIS. Die old functional code, die!.

File

includes/purge_ui.inc, line 140
Provides administrative interface for the Purge module.

Code

function OLDpurge_item_form($form, &$form_state, $item_type, $item_name = NULL, $action = 'view') {

  // First lets load a bundle.
  $bundle = new PurgePurgerBundleUI();

  // Select the object we need for this form.
  if ($action == 'add') {
    switch ($item_type) {
      case 'target':
        $item = new PurgeTargetStatic();
        break;
      case 'domain':
        $item = new PurgeDomainStatic();
        break;
      case 'header':
        $item = new PurgeHeaderStatic();
        break;
      case 'purger':
        $item = new PurgePurgerBasicUrl();
        break;
    }
    $item_name = '';
  }
  elseif ($action == 'clone') {
    $item = clone $bundle->{$item_type}[$item_name];
    $item->name = '';
    $item->description = '';
  }
  elseif ($action == 'example') {

    // Get the example bundle
    module_load_include('inc', 'purge', 'includes/defaults');
    $examples = new PurgePurgerBundleExample();
    $item = clone $examples->{$item_type}[$item_name];

    // Set pointers for purgers.
    if ($item_type == 'purger') {
      foreach ($item->pointers as $pointer_type => $pointers) {
        foreach ($pointers as $pointer_name) {

          // Check if the bundle has the item.
          if (!isset($bundle->{$pointer_type}[$pointer_name])) {

            // Check look for it in the exampele bundle.
            if (isset($example->{$pointer_type}[$pointer_name])) {

              // add it to the bundle.
              $bundle->{$pointer_type}[$pointer_name] = clone $example[$pointer_type][$pointer_name];
            }
          }

          // Link the item to the bundle object.
          $item->{$pointer_type}[$pointer_name] = $bundle->{$pointer_type}[$pointer_name];
        }
      }
    }
  }
  else {
    $item = $bundle->{$item_type}[$item_name];
  }

  // The Basic Form elements.
  $form = array();
  $form['purge_item'] = array(
    '#type' => 'fieldset',
    '#title' => t('Basic Item Configuration'),
  );

  // First some hidden fields for internal use.
  $form['purge_item']['item_action'] = array(
    '#type' => 'hidden',
    '#default_value' => $action,
  );
  $form['purge_tem']['item_type'] = array(
    '#type' => 'hidden',
    '#default_value' => $item_type,
  );
  $form['purge_item']['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Name'),
    '#required' => TRUE,
    '#description' => t('The user-friendy name for the item.'),
    '#default_value' => $item->name,
  );
  if ($action == 'add' || $action == 'clone') {
    $form['purge_item']['machine_name'] = array(
      '#type' => 'machine_name',
      '#maxlength' => 21,
      '#default_value' => '',
      '#machine_name' => array(
        'exists' => 'purge_item_name_exists',
        'source' => array(
          'purge_item',
          'name',
        ),
      ),
    );
  }
  else {
    $form['purge_item']['machine_name'] = array(
      '#type' => 'hidden',
      '#default_value' => $item_name,
    );
  }
  $form['purge_item']['description'] = array(
    '#type' => 'textfield',
    '#title' => t('Description'),
    '#default_value' => $item->description,
  );
  $form['purge_item']['enabled'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enabled'),
    '#description' => t('Disabled items will ignored during operations, even when included in purgers.'),
    '#default_value' => $item->enabled,
  );

  // Now switch between the item_types to generate their respecitve forms.
  switch ($item_type) {

    // The target form.
    case 'target':
      $form['target'] = array(
        '#type' => 'fieldset',
        '#title' => 'Target Configuration',
      );
      $form['target']['urls'] = array(
        '#type' => 'textarea',
        '#title' => t('URLs'),
        '#description' => t('The URLs where the Purge requests will be sent to. One URL per line.'),
        '#required' => true,
        '#default_value' => implode("\n", $item->urls),
      );
      break;
    case 'domain':
      $form['domain'] = array(
        '#type' => 'fieldset',
        '#title' => 'Domain Configuration',
      );
      $form['domain']['domains'] = array(
        '#type' => 'textarea',
        '#title' => t('Domains'),
        '#description' => t('For each domain name Purge sends out a request. One domain per line.'),
        '#required' => true,
        '#default_value' => implode("\n", $item->domains),
      );
      break;
    case 'header':
      $form['header'] = array(
        '#type' => 'fieldset',
        '#title' => 'Header Configuration',
      );
      $headers = array();
      foreach ($item->headers as $header_key => $header_value) {
        $headers[] = implode(": ", array(
          $header_key,
          $header_value,
        ));
      }
      $form['header']['headers'] = array(
        '#type' => 'textarea',
        '#title' => t('Header'),
        '#description' => t('Headers are added to each request. One header per line. Format: Header: value'),
        '#required' => true,
        '#default_value' => implode("\n", $headers),
      );
      break;
    case 'purger':
      $form['purger'] = array(
        '#type' => 'fieldset',
        '#title' => 'Purger configuration',
      );

      // Generate the set
      $table_header = array(
        'name' => t('Name'),
        'description' => t('Description'),
      );
      foreach ($item->pointers as $pointer_type => $pointer_names) {
        $rows = array();
        $default_value = array();
        $multiple = true;
        if ($pointer_type == 'handler' || $pointer_type == 'queue') {
          $multiple = false;
        }
        foreach ($bundle->{$pointer_type} as $pointer_name => $pointer) {
          $rows[$pointer_name] = array(
            'name' => $bundle->{$pointer_type}[$pointer_name]->name,
            'description' => $bundle->{$pointer_type}[$pointer_name]->description,
          );
          if (in_array($pointer_name, $item->pointers[$pointer_type])) {
            if ($multiple) {
              $default_value[$pointer_name] = $pointer_name;
            }
            else {
              $default_value = $pointer_name;
            }
          }
        }
        $form['purger'][$pointer_type] = array(
          '#type' => 'fieldset',
          '#title' => $bundle->type[$pointer_type]->name,
          '#description' => $bundle->type[$pointer_type]->description,
        );
        $form['purger'][$pointer_type][$pointer_type . '_settings'] = array(
          '#type' => 'tableselect',
          '#header' => $table_header,
          '#options' => $rows,
          '#default_value' => $default_value,
          '#multiple' => $multiple,
        );

        // Check if this type has options.
        $options = array();
        if (isset($bundle->type[$pointer_type]->options)) {

          // Now get the form items for the options.
          $form['purger'][$pointer_type][$pointer_type . '_options'] = array(
            '#type' => 'fieldset',
            '#title' => $bundle->type[$pointer_type]->name . ' ' . t('Options'),
          );

          // Display the type options
          $options = $bundle->type[$pointer_type]->options;
          $all_item_options = array();
          foreach ($options as $option_name => $option) {

            // First display a form for all type specific options
            if ($options[$option_name]['context'] == 'type') {

              // If the purger has a value for this option
              if (isset($item->options[$pointer_type][$option_name])) {
                $option['purger_value'] = $item->options[$pointer_type][$option_name];
              }

              // Now generate a form for these options.
              $form['purger'][$pointer_type][$pointer_type . '_options'][$item_name . '_' . $option_name] = purge_option_form($option);
            }
            elseif ($options[$option_name]['context'] == 'item') {
              $all_item_options[$option_name] = $option;
            }
          }

          // First go to all items of this type
          foreach ($bundle->{$pointer_type} as $pointer_name => $pointer) {
            $this_item_options = array();

            // Now pass each item option
            foreach ($all_item_options as $item_option_name => $item_option) {

              // Check if the item has the option defined.
              if (isset($bundle->{$pointer_type}[$pointer_name]->options[$item_option_name])) {
                $this_item_options[$item_option_name] = $item_option;
                if (isset($item->options[$pointer_type][$item_option_name])) {
                  $this_item_options[$item_option_name]['purger_value'] = $item->options[$pointer_type][$item_option_name];
                }
              }
            }
            if (count($this_item_options) > 0) {
              $form['purger'][$pointer_type][$pointer_type . '_options'][$pointer_name] = array(
                '#type' => 'fieldset',
                '#title' => $bundle->{$pointer_type}[$pointer_name]->name . ' ' . t('Options'),
                '#states' => array(
                  'visible' => array(
                    ':input[name="' . $pointer_type . '_settings"]' => array(
                      'value' => $pointer_name,
                    ),
                  ),
                ),
              );

              // Generate an option form.
              foreach ($this_item_options as $option_name => $option) {
                $form['purger'][$pointer_type][$pointer_type . '_options'][$pointer_name][$pointer_name . '_' . $option_name] = purge_option_form($option);
              }
            }
          }
        }
      }
      break;
  }

  // Now the items type options.
  $options = array();
  $options = $bundle->type[$item_type]->options;
  if (count($options) > 0) {
    if ($item_type == 'purger') {
      $form[$item_type]['options'] = array(
        '#type' => 'fieldset',
        '#title' => t('Purger Options'),
        '#description' => t('These settings apply to this purger only.'),
      );
      foreach ($options as $option_name => $option) {
        if (isset($item->options['purger'][$option_name])) {
          $option['purger_value'] = $item->options['purger'][$option_name];
        }
        $form[$item_type]['options'][$item_type . '_' . $option_name] = purge_option_form($option);
      }
    }
    else {
      $form[$item_type]['options'] = array(
        '#type' => 'fieldset',
        '#title' => t('Default Options'),
        '#description' => t('These defaults can be overruled per purger.'),
      );
    }
  }

  // Now display submit buttons.
  if ($action != 'view' && in_array(PURGE_ACCESS_FULL, $item->access)) {
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Save'),
    );
    if ($item instanceof PurgeValidateable) {
      $form['validate'] = array(
        '#type' => 'button',
        '#value' => t('Validate'),
      );
    }
  }

  // Enable/disable buton.
  if (in_array(PURGE_ACCESS_ENABLE, $item->access)) {
    if ($item->enabled) {
      $form['disable'] = array(
        '#type' => 'submit',
        '#name' => 'disable',
        '#value' => t('Disable'),
        '#submit' => array(
          'purge_form_button_callback',
        ),
      );
    }
    else {
      $form['enable'] = array(
        '#type' => 'submit',
        '#name' => 'enable',
        '#value' => t('Enable'),
        '#submit' => array(
          'purge_form_button_callback',
        ),
      );
    }
  }
  if (in_array(PURGE_ACCESS_FULL, $item->access)) {
    $form['delete'] = array(
      '#type' => 'submit',
      '#name' => 'delete',
      '#value' => t('Delete'),
      '#submit' => array(
        'purge_form_button_callback',
      ),
    );
  }
  return $form;
}