You are here

function farm_livestock_birth_form in farmOS 7

Birth quick form.

2 string references to 'farm_livestock_birth_form'
farm_livestock_birth_form_submit in modules/farm/farm_livestock/farm_livestock.farm_quick.birth.inc
Submit callback for birth quick form.
farm_livestock_farm_quick_forms in modules/farm/farm_livestock/farm_livestock.farm_quick.inc
Implements hook_farm_quick_forms().

File

modules/farm/farm_livestock/farm_livestock.farm_quick.birth.inc, line 10
Farm livestock birth quick form.

Code

function farm_livestock_birth_form($form, &$form_state) {

  // Wrapper fieldset.
  $form['birth'] = array(
    '#type' => 'fieldset',
    '#title' => t('Record an animal birth'),
    '#description' => t('Use this form to record the birth of one or more animals. A new birth log will be created, along with the new child animal records.'),
    '#tree' => TRUE,
  );

  // Date select (default to now).
  $form['birth']['timestamp'] = array(
    '#type' => 'date_select',
    '#title' => t('Date'),
    '#date_format' => 'M j Y H:i',
    '#date_type' => DATE_FORMAT_UNIX,
    '#date_year_range' => '-10:+3',
    '#default_value' => REQUEST_TIME,
    '#required' => TRUE,
  );

  // Mother animal reference. Required because we need to be able to get the
  // species/breed from at least on of the parents.
  $form['birth']['mother'] = array(
    '#type' => 'textfield',
    '#title' => t('Mother'),
    '#description' => t('Select the mother animal. As you type, a dropdown of matching animal names will appear. Click on the one you want, and the field will be filled in with the name and asset ID (example: "Betsy [id: 123]"). The "[id: X]" portion is required.'),
    '#autocomplete_path' => 'farm_asset/autocomplete/animal',
    '#required' => TRUE,
  );

  // Father animal reference.
  $form['birth']['father'] = array(
    '#type' => 'textfield',
    '#title' => t('Father'),
    '#description' => t('Select the father animal (optional). See the mother field above for instructions.'),
    '#autocomplete_path' => 'farm_asset/autocomplete/animal',
  );

  // Number of children.
  $form['birth']['children'] = array(
    '#type' => 'select',
    '#title' => t('How many children were born?'),
    '#options' => drupal_map_assoc(range(1, 15)),
    '#default_value' => 1,
    '#ajax' => array(
      'callback' => 'farm_livestock_birth_form_children_ajax',
      'wrapper' => 'farm-livestock-birth-children',
    ),
  );

  // Create a wrapper around all child fields, for AJAX replacement.
  $form['birth']['child'] = array(
    '#prefix' => '<div id="farm-livestock-birth-children">',
    '#suffix' => '</div>',
  );

  // Add fields for each child.
  $children = 1;
  if (!empty($form_state['values']['birth']['children'])) {
    $children = $form_state['values']['birth']['children'];
  }
  for ($i = 0; $i < $children; $i++) {

    // Fieldset for the child.
    $form['birth']['child'][$i] = array(
      '#type' => 'fieldset',
      '#title' => t('Child @number', array(
        '@number' => $i + 1,
      )),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
    );

    // Animal name.
    $form['birth']['child'][$i]['name'] = array(
      '#type' => 'textfield',
      '#title' => t('Name'),
      '#description' => t('Give the animal a name (and/or tag ID below). If the name is left blank, then it will be copied from the tag ID.'),
      '#weight' => 0,
    );

    // Tag ID.
    $form['birth']['child'][$i]['tag_id'] = array(
      '#type' => 'textfield',
      '#title' => t('Tag ID'),
      '#weight' => 0,
    );

    // Male or female.
    $form['birth']['child'][$i]['sex'] = array(
      '#type' => 'radios',
      '#title' => t('Sex'),
      '#options' => array(
        'F' => t('Female'),
        'M' => t('Male'),
      ),
      '#weight' => 10,
    );

    // Animal description.
    $form['birth']['child'][$i]['description'] = array(
      '#type' => 'text_format',
      '#title' => t('Description'),
      '#format' => 'farm_format',
      '#weight' => 30,
    );

    // Survived.
    $form['birth']['child'][$i]['survived'] = array(
      '#type' => 'checkbox',
      '#title' => t('Survived birth'),
      '#description' => t('Uncheck this if the child did not survive. The child animal record will still be created, but will be immediately archived.'),
      '#default_value' => TRUE,
      '#weight' => 40,
    );
  }

  // Group assignment choice
  $form['birth']['assign_group'] = array(
    '#type' => 'checkbox',
    '#title' => t('Assign group(s)'),
    '#description' => t('By default, assign the mother\'s groups to the children. Check this to manually assign groups.'),
    '#default_value' => FALSE,
    '#required' => FALSE,
    '#ajax' => array(
      'callback' => 'farm_livestock_birth_form_group_ajax',
      'wrapper' => 'farm-livestock-birth-group',
    ),
  );

  // Create a wrapper around the group field, for AJAX replacement.
  $form['birth']['group'] = array(
    '#prefix' => '<div id="farm-livestock-birth-group">',
    '#suffix' => '</div>',
  );

  // Group
  if (!empty($form_state['values']['birth']['assign_group'])) {
    $form['birth']['group']['group'] = array(
      '#type' => 'select',
      '#title' => t('Group'),
      '#multiple' => TRUE,
      '#options' => farm_group_options(),
      '#required' => FALSE,
    );
  }

  // Birth notes.
  $form['birth']['notes'] = array(
    '#type' => 'text_format',
    '#title' => t('Birth notes'),
    '#format' => 'farm_format',
  );

  // Submit button.
  $form['birth']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save birth records'),
  );

  // Return the form.
  return $form;
}