You are here

function recipe_form in Recipe 5

Same name and namespace in other branches
  1. 6 recipe.module \recipe_form()
  2. 7.2 recipe.module \recipe_form()
  3. 7 recipe.module \recipe_form()

Implementation of hook_form().

File

./recipe.module, line 114
recipe.module - share recipes for drupal 5.x

Code

function recipe_form(&$node) {

  // drupal 4.7 requires the title field to be defined by the custom node's module
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#size' => 60,
    '#maxlength' => 128,
    '#required' => TRUE,
    '#default_value' => $node->title,
  );

  // Now we define the form elements specific to our node type.
  $form['body'] = array(
    '#type' => 'textarea',
    '#title' => t('Description'),
    '#default_value' => $node->body,
    '#cols' => 60,
    '#rows' => 2,
    '#description' => t('A short description or "teaser" for the recipe.'),
    '#required' => TRUE,
  );
  $form['yield'] = array(
    '#type' => 'textfield',
    '#title' => t('Yield'),
    '#default_value' => $node->yield,
    '#size' => 10,
    '#maxlength' => 10,
    '#description' => t('The number of servings the recipe will make.'),
    '#attributes' => NULL,
    '#required' => TRUE,
  );
  $form['preptime'] = array(
    '#type' => 'select',
    '#title' => t("Preparation time"),
    '#default_value' => $node->preptime,
    '#options' => array(
      5 => t('5 minutes'),
      10 => t('10 minutes'),
      15 => t('15 minutes'),
      20 => t('20 minutes'),
      30 => t('30 minutes'),
      45 => t('45 minutes'),
      60 => t('1 hour'),
      90 => t('1 1/2 hours'),
      120 => t('2 hours'),
      150 => t('2 1/2 hours'),
      180 => t('3 hours'),
      210 => t('3 1/2 hours'),
      240 => t('4 hours'),
      300 => t('5 hours'),
      360 => t('6 hours'),
    ),
    '#description' => t("How long does this recipe take to prepare (i.e. elapsed time)"),
  );
  $form["source"] = array(
    '#type' => 'textfield',
    '#title' => t("Source"),
    '#default_value' => $node->source,
    '#size' => 60,
    '#maxlength' => 127,
    '#description' => t("Optional. Does anyone else deserve credit for this recipe?"),
  );

  // Table of existing ingredients
  $form['ingredients']['#tree'] = TRUE;
  $system = variable_get('recipe_ingredient_system', 'complex');
  if ($system == 'complex') {
    $form['ingredients']['headings'] = array(
      '#value' => '<div><table ><thead><tr><th>' . t('Quantity') . '</th><th>' . t('Units') . '</th><th>' . t('Ingredient Name') . '</th></tr></thead>' . "\n" . '<tbody>',
    );
  }
  else {
    $form['ingredients']['headings'] = array(
      '#value' => '<div><table ><thead><tr><th>' . t('Ingredients') . '</th></tr></thead>' . "\n" . '<tbody>',
    );
  }
  $rows = array();
  $callback = 'recipe/ingredient/autocomplete';
  $num_ingredients = 0;
  if ($node->ingredients) {
    foreach ($node->ingredients as $id => $ingredient) {
      $num_ingredients = $id + 1;
      if ($id == 0) {
        $j = '0';
      }
      else {
        $j = $id;
      }
      if ($ingredient->name && isset($ingredient->quantity)) {

        // When can the following statement be true?
        if (!$ingredient) {
          drupal_set_message(t('Recipe Module: An error has occured.  Please report this error to the system administrator.'), 'error');
          $ingredient->quantity = '';
          $ingredient->unit_id = 21;
          $ingredient->name = '';
        }
        if ($system == 'complex') {
          $form['ingredients'][$j]['open_tags'] = array(
            '#value' => '<tr><th>',
          );
          $form['ingredients'][$j]['quantity'] = array(
            '#type' => 'textfield',
            '#title' => '',
            '#default_value' => preg_replace('/\\&frasl;/', '/', recipe_ingredient_quantity_from_decimal($ingredient->quantity)),
            '#size' => 8,
            '#maxlength' => 8,
          );
          $form['ingredients'][$j]['mid1_tags'] = array(
            '#value' => '</th><th>',
          );
          $form['ingredients'][$j]['unit_id'] = array(
            '#type' => 'select',
            '#title' => '',
            '#default_value' => $ingredient->unit_id,
            '#options' => recipe_unit_options(),
          );
          $form['ingredients'][$j]['mid2_tags'] = array(
            '#value' => '</th><th>',
          );
          $form['ingredients'][$j]['name'] = array(
            '#type' => 'textfield',
            '#title' => '',
            '#default_value' => $ingredient->name,
            '#size' => 64,
            '#maxlength' => 128,
            '#autocomplete_path' => $callback,
          );
          $form['ingredients'][$j]['close_tags'] = array(
            '#value' => '</th></tr>',
          );
        }
        else {
          if ($ingredient->name) {
            if ($ingredient->quantity == 0) {
              $ingredient->quantity = '';
            }
            else {
              $ingredient->quantity .= ' ';
            }
            if ($ingredient->abbreviation != '') {
              $ingredient->abbreviation .= ' ';
            }
            $ingredient->name = $ingredient->quantity . $ingredient->abbreviation . $ingredient->name;
          }
          $form['ingredients'][$j]['open_tags'] = array(
            '#value' => '<tr><th>',
            '#tree' => TRUE,
          );
          $form['ingredients'][$j]['name'] = array(
            '#type' => 'textfield',
            '#title' => '',
            '#default_value' => $ingredient->name,
            '#size' => 64,
            '#maxlength' => 128,
            '#autocomplete_path' => $callback,
          );
          $form['ingredients'][$j]['close_tags'] = array(
            '#value' => '</th></tr>',
          );
        }

        // else
      }

      // if ($ingredient->name && isset($ingredient->quantity))
    }

    // foreach ($node->ingredients as $id => $ingredient)
  }

  // if ($node->ingredients)
  // Add ten more spots for ingredients than are already used
  for ($i = $num_ingredients; $i < $num_ingredients + 10; $i++) {
    if ($i == 0) {
      $j = '0';
    }
    else {
      $j = $i;
    }
    if ($system == 'complex') {
      $form['ingredients'][$j]['open_tags'] = array(
        '#value' => '<tr><th>',
      );
      $form['ingredients'][$j]['quantity'] = array(
        '#type' => 'textfield',
        '#title' => '',
        '#size' => 8,
        '#maxlength' => 8,
      );
      $form['ingredients'][$j]['mid1_tags'] = array(
        '#value' => '</th><th>',
      );
      $form['ingredients'][$j]['unit_id'] = array(
        '#type' => 'select',
        '#title' => '',
        '#options' => recipe_unit_options(),
        '#default_value' => 2,
      );
      $form['ingredients'][$j]['mid2_tags'] = array(
        '#value' => '</th><th>',
      );
      $form['ingredients'][$j]['name'] = array(
        '#type' => 'textfield',
        '#title' => '',
        '#size' => 64,
        '#maxlength' => 128,
        '#autocomplete_path' => $callback,
      );
      $form['ingredients'][$j]['close_tags'] = array(
        '#value' => '</th></tr>',
      );
    }
    else {
      $form['ingredients'][$j]['open_tags'] = array(
        '#value' => '<tr><th>',
      );
      $form['ingredients'][$j]['name'] = array(
        '#type' => 'textfield',
        '#title' => '',
        '#size' => 64,
        '#maxlength' => 128,
        '#autocomplete_path' => $callback,
      );
      $form['ingredients'][$j]['close_tags'] = array(
        '#value' => '</th></tr>',
      );
    }
  }
  $form['ingredients']['end'] = array(
    '#value' => '</tbody>' . "\n" . '</table></div>' . "\n",
  );
  $form['instructions'] = array(
    '#type' => 'textarea',
    '#title' => t('Instructions'),
    '#default_value' => $node->instructions,
    '#cols' => 60,
    '#rows' => 10,
    '#description' => t('Step by step instructions on how to prepare and cook the recipe.'),
  );
  $form['notes'] = array(
    '#type' => 'textarea',
    '#title' => t("Additional Notes"),
    '#default_value' => $node->notes,
    '#cols' => 60,
    '#rows' => 5,
    '#description' => t("Optional. Describe a great dining experience relating to this recipe, or note which wine or other dishes complement this recipe"),
  );
  $form['filter'] = filter_form($node->format);
  return $form;
}