You are here

function botcha_recipebook_form in BOTCHA Spam Prevention 6.2

Same name and namespace in other branches
  1. 7.2 botcha.admin.inc \botcha_recipebook_form()

Edit existent or add a new recipe book.

Parameters

array $form_state: Form API form state array.

BotchaRecipebookAbstract $recipebook: Recipe book object.

3 string references to 'botcha_recipebook_form'
BotchaModel::getRecipebooksForms in model/botcha.model.inc
botcha_menu in ./botcha.module
Implements hook_menu().
botcha_update_6200 in ./botcha.install
Implementation of hook_update_N(). Create flexible relationships between recipe books and recipes and between recipe books and forms.

File

./botcha.admin.inc, line 216
Implementation of botcha administration forms.

Code

function botcha_recipebook_form(&$form_state, $recipebook = NULL) {
  if ($recipebook instanceof BotchaRecipebookNone) {

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

  // Determine default values depending on whether we add or edit recipe book.
  if ($edit = !empty($recipebook)) {
    $default_id = $recipebook->id;
    $default_title = $recipebook->title;
    $default_description = $recipebook->description;
    $default_recipes = array_keys($recipebook
      ->getRecipes());

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

    // @todo Unused yet. Do we need it?
    $default_id = !empty($_GET['botcha_rbid']) ? $_GET['botcha_rbid'] : NULL;
    $default_title = '';
    $default_description = '';
    $default_recipes = array();

    // @todo Abstract it.
    $validate = array(
      'botcha_form_id_validate',
    );
    $button = t('Add');
  }
  $form['id'] = array(
    '#type' => 'textfield',
    '#title' => t('Id'),
    '#description' => t('The unique identifier of the recipe book.'),
    // @todo Abstract it.

    //'#default_value' => $default_id,

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

  // @todo Abstract it.
  if ($edit) {
    $form['id']['#value'] = $default_id;
  }
  else {
    $form['id']['#default_value'] = $default_id;
  }
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#description' => t('A title for this recipe book. You can always change this name later.'),
    '#default_value' => $default_title,
    '#required' => TRUE,
    '#maxlength' => 128,
  );
  $form['description'] = array(
    '#type' => 'textarea',
    '#rows' => 5,
    '#title' => t('Description'),
    '#description' => t('A description of the recipe book.'),
    '#default_value' => $default_description,
  );

  // Form a list of recipes.
  $form['recipes'] = array(
    '#type' => 'fieldset',
    '#title' => t('Recipes'),
    '#description' => t('Choose what recipes are included in recipe book.'),
    '#tree' => TRUE,
  );
  foreach (Botcha::getRecipes() as $recipe) {
    $form['recipes'][$recipe->id] = array(
      '#type' => 'checkbox',
      '#title' => $recipe
        ->getTitle(),
      '#description' => $recipe
        ->getDescription(),
      '#default_value' => in_array($recipe->id, $default_recipes),
    );
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => $button,
  );
  return $form;
}