You are here

public static function Botcha::getAdminForm in BOTCHA Spam Prevention 6.2

Same name and namespace in other branches
  1. 6.3 controller/application/botcha.application.controller.inc \Botcha::getAdminForm()
  2. 7.2 controller/botcha.controller.inc \Botcha::getAdminForm()
  3. 7.3 controller/application/botcha.application.controller.inc \Botcha::getAdminForm()
2 calls to Botcha::getAdminForm()
botcha_forms_form in ./botcha.admin.inc
Callback for "Forms" admin page. Configuration of which forms to protect, with what recipe.
botcha_form_form in ./botcha.admin.inc
Edit existent or add BOTCHA protection to another form.

File

controller/botcha.controller.inc, line 33
Contains Botcha class.

Class

Botcha
Singleton realization of botcha application.

Code

public static function getAdminForm($form_name) {
  $form = array();
  switch ($form_name) {
    case 'form_edit':
      list($form_name, $form_state, $botcha_form) = func_get_args();
      if ($botcha_form instanceof BotchaFormNone) {

        // Redirect in case we are trying to edit unexisting item.
        drupal_goto(Botcha::BOTCHA_ADMIN_PATH . '/form/add', array(
          'query' => array(
            'botcha_form_id' => $botcha_form->id,
          ),
        ));
      }

      // Determine default values depending on whether we add or edit form.
      if ($edit = !empty($botcha_form)) {
        $operation = 'edit';
        $isEnabled = $botcha_form
          ->isEnabled();
        $recipebook = $botcha_form
          ->getRecipebook();
        $botcha_form_id_default = check_plain($botcha_form->id);

        // @todo Abstract it.
        $validate = array();
        $button = t('Save');
      }
      else {
        $operation = 'add';
        $isEnabled = TRUE;

        // 'default' will be selected.
        $recipebook = Botcha::getRecipebook();

        // @todo Abstract it.

        //$query_params = drupal_get_query_parameters();
        $botcha_form_id_default = !empty($_GET['botcha_form_id']) ? $_GET['botcha_form_id'] : NULL;

        // @todo Abstract it.
        $validate = array(
          'botcha_form_id_validate',
        );
        $button = t('Add');
      }

      // Insert a field that allows to understand whether it is creation or edition form submission.
      $form['botcha_operation'] = array(
        '#type' => 'value',
        '#value' => $operation,
      );

      // Determine whether the form enabled or not.
      $form['botcha_enabled'] = array(
        '#type' => 'checkbox',
        '#title' => t('Enabled'),
        '#description' => t('Enable BOTCHA protection for the form.'),
        '#default_value' => $isEnabled,
      );

      // Use passed as a parameter form id.
      $form['botcha_form_id'] = array(
        '#type' => 'textfield',
        '#title' => t('Form ID'),
        '#description' => t('The Drupal form_id of the form to add the BOTCHA protection to.'),
        // @todo Abstract it.

        //'#default_value' => $botcha_form_id_default,

        //'#value' => $botcha_form_id_default,
        '#required' => TRUE,
        '#disabled' => $edit,
        '#maxlength' => 128,
        '#element_validate' => $validate,
      );

      // @todo Abstract it.
      if ($edit) {
        $form['botcha_form_id']['#value'] = $botcha_form_id_default;
      }
      else {
        $form['botcha_form_id']['#default_value'] = $botcha_form_id_default;
      }
      $options = array();
      $recipebooks = Botcha::getRecipebooks(TRUE);
      foreach ($recipebooks as $rb) {
        $options[$rb->id] = $rb->title;
      }
      $form['botcha_form_recipebook'] = array(
        '#type' => 'select',
        '#title' => t('Recipe book'),
        '#description' => t('Recipe book to apply to the form.'),
        '#default_value' => $recipebook->id,
        '#options' => $options,
        '#disabled' => !$isEnabled,
      );
      $form['submit'] = array(
        '#type' => 'submit',
        '#value' => $button,
      );
      break;
    case 'form_list':
      if (module_exists('captcha')) {

        // Checkbox to put BOTCHA on same forms as CAPTCHA.
        $form['botcha_form_protection']['botcha_on_captcha_forms'] = array(
          '#type' => 'checkbox',
          '#title' => t('Add BOTCHA to forms selected for CAPTCHA'),
          '#default_value' => variable_get('botcha_on_captcha_forms', TRUE),
          '#description' => t('This option makes it easy to manage BOTCHA settings on forms. When enabled, all forms that are configured to have CAPTCHA on them (<a href="@captcha">see configuration</a>) will also be selected for BOTCHA protection.!more', array(
            // @todo Abstract it.

            //'@captcha' => url('admin/config/people/captcha'),
            '@captcha' => url('admin/user/captcha'),
            '!more' => module_exists('captcha') ? '' : '<br />' . t('<b>Note:</b> <a href="@captcha_home">CAPTCHA module</a> is not installed. This setting will have no effect.', array(
              '@captcha_home' => 'http://drupal.org/project/captcha',
            )),
          )),
        );
      }

      // List known form_ids.
      $form['botcha_form_protection']['botcha_form_id_overview'] = array(
        '#theme' => 'botcha_forms_form_botcha_forms',
        '#tree' => TRUE,
      );

      // @todo Remove it.

      //$form['botcha_form_protection']['botcha_form_id_overview']['#header'] = array('form_id', t('Recipe book'), t('Operations'));
      $form['botcha_form_protection']['botcha_form_id_overview']['#header'] = array(
        t('Enabled'),
        'form_id',
        t('Recipe book'),
      );
      $form['botcha_form_protection']['botcha_form_id_overview']['botcha_forms'] = array();
      $forms = Botcha::getForms(TRUE);
      foreach ($forms as $botcha_form) {

        // Get individual admin form as a row of our overview table.
        $item_form = Botcha::getAdminForm('form_edit', array(), $botcha_form);

        // Remove submit button ...
        unset($item_form['submit']);

        // ... and botcha_operation field ...
        unset($item_form['botcha_operation']);

        // ... and also additional properties of some fields.
        unset($item_form['botcha_enabled']['#title']);
        unset($item_form['botcha_enabled']['#description']);
        $form['botcha_form_protection']['botcha_form_id_overview']['botcha_forms'][$botcha_form->id] = $item_form;

        /* @todo Remove it.
           $form['botcha_form_protection']['botcha_form_id_overview']['botcha_forms'][$botcha_form->id]['form_id'] = array(
             // @todo Abstract it.
             //'#markup' => $botcha_form->id,
             '#value' => $botcha_form->id,
           );
           // Recipe book, enabled for this form.
           $recipebook = $botcha_form->getRecipebook();
           // @todo Remove hardcode.
           $value = ($recipebook instanceof BotchaRecipebookNone || $recipebook->id == 'forbidden_forms')
             ? $recipebook->id
             : l($recipebook->id, Botcha::BOTCHA_ADMIN_PATH . "/recipebook/{$recipebook->id}");
           $form['botcha_form_protection']['botcha_form_id_overview']['botcha_forms'][$botcha_form->id]['recipebook'] = array(
             // @todo Abstract it.
             //'#markup' => $botcha_form->id,
             '#value' => $value,
           );
           // Additional operations.
           $form['botcha_form_protection']['botcha_form_id_overview']['botcha_forms'][$botcha_form->id]['operations'] = array(
             // @todo Abstract it.
             //'#markup' => implode(" | ", array(
             '#value' => ($recipebook->id == 'forbidden_forms')
               ? ''
               : implode(" | ", array(
                 l(t('Edit'), Botcha::BOTCHA_ADMIN_PATH . "/form/{$botcha_form->id}"),
                 l(t('Delete'), Botcha::BOTCHA_ADMIN_PATH . "/form/{$botcha_form->id}/delete"),
               )),
           );
            *
            */
      }

      // Field for the BOTCHA administration mode.
      $form['botcha_form_protection']['botcha_administration_mode'] = array(
        '#type' => 'checkbox',
        '#title' => t('Add BOTCHA administration links to forms'),
        '#default_value' => variable_get('botcha_administration_mode', FALSE),
        '#description' => t('This option makes it easy to manage BOTCHA settings on forms. When enabled, users with the "%adminbotcha" permission will see a fieldset with BOTCHA administration links on all forms, except on administrative pages.', array(
          '%adminbotcha' => t('administer BOTCHA settings'),
        )),
      );

      // Field for the BOTCHAs on admin pages.
      $form['botcha_form_protection']['botcha_allow_on_admin_pages'] = array(
        '#type' => 'checkbox',
        '#title' => t('Allow BOTCHAs and BOTCHA administration links on administrative pages'),
        '#default_value' => variable_get('botcha_allow_on_admin_pages', FALSE),
        '#description' => t('This option makes it possible to add BOTCHAs to forms on administrative pages. BOTCHAs are disabled by default on administrative pages (which shouldn\'t be accessible to untrusted users normally) to avoid the related overhead. In some situations, e.g. in the case of demo sites, it can be usefull to allow BOTCHAs on administrative pages.'),
      );
      $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Save configuration'),
      );
      $form['#theme'] = 'system_settings_form';
      break;
  }
  return $form;
}