You are here

function farm_livestock_birth_form_submit in farmOS 7

Submit callback for birth quick form.

File

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

Code

function farm_livestock_birth_form_submit($form, &$form_state) {

  // Get the birth timestamp.
  $timestamp = strtotime($form_state['values']['birth']['timestamp']);

  // Get the mother and father animals, if they exists.
  $parents = array(
    'mother' => FALSE,
    'father' => FALSE,
  );
  if (!empty($form_state['storage']['mother'])) {
    $parents['mother'] = $form_state['storage']['mother'];
  }
  if (!empty($form_state['storage']['father'])) {
    $parents['father'] = $form_state['storage']['father'];
  }

  // Iterate through the children, and build an array of their asset records.
  $children = array();
  foreach ($form_state['values']['birth']['child'] as $child) {

    // If the name is not set, but tag ID is, copy the tag ID to the name.
    if (empty($child['name']) && !empty($child['tag_id'])) {
      $child['name'] = $child['tag_id'];
    }

    // Create a new animal asset.
    $values = array(
      'type' => 'animal',
      'name' => $child['name'],
      'created' => $timestamp,
    );
    $child_asset = entity_create('farm_asset', $values);
    $child_wrapper = entity_metadata_wrapper('farm_asset', $child_asset);

    // Set the animal's birthdate to the date of the log.
    $child_wrapper->field_farm_date
      ->set($timestamp);

    // Set the animal's tag ID, if available. Create a new ID tag
    // field_collection entity attached to the animal.
    if (!empty($child['tag_id'])) {
      $animal_tag = entity_create('field_collection_item', array(
        'field_name' => 'field_farm_animal_tag',
      ));
      $animal_tag
        ->setHostEntity('farm_asset', $child_asset);
      $animal_tag_wrapper = entity_metadata_wrapper('field_collection_item', $animal_tag);
      $animal_tag_wrapper->field_farm_animal_tag_id
        ->set($child['tag_id']);
      $animal_tag_wrapper
        ->save();
    }

    // Set the animal's sex, if available.
    if (!empty($child['sex'])) {
      $child_wrapper->field_farm_animal_sex
        ->set($child['sex']);
    }

    // Set the animal's description, if available.
    if (!empty($child['description']['value'])) {
      $child_wrapper->field_farm_description
        ->set($child['description']);
    }

    // Iterate through the parents.
    foreach ($parents as $name => $parent) {

      // If an asset is not loaded, skip it.
      if (empty($parent)) {
        continue;
      }

      // Add them to the child's parents.
      $child_wrapper->field_farm_parent[] = $parent->id;

      // Load metadata wrapper.
      $parent_wrapper = entity_metadata_wrapper('farm_asset', $parent);

      // If this is the mother...
      if ($name == 'mother') {

        // Copy the species/breed to the child.
        $animal_type = $parent_wrapper->field_farm_animal_type
          ->value();
        $child_wrapper->field_farm_animal_type
          ->set($animal_type);
      }
    }

    // If the child did not survive, then archive them.
    if (empty($child['survived'])) {
      $child_wrapper->archived
        ->set($timestamp);
    }

    // Save the animal asset.
    $child_wrapper
      ->save();

    // Add it to the array.
    $children[] = $child_asset;

    // Link the asset to this quick form.
    if (function_exists('farm_quick_entity_link')) {
      farm_quick_entity_link('farm_livestock_birth_form', 'farm_asset', $child_asset);
    }

    // Set a message.
    $label = entity_label('farm_asset', $child_asset);
    $uri = entity_uri('farm_asset', $child_asset);
    drupal_set_message(t('Child animal created') . ': ' . l($label, $uri['path']));
  }

  // Create a birth log. Leave the name blank so that it is auto-generated.
  $log_type = 'farm_birth';
  $log_name = '';
  $log = farm_log_create($log_type, $log_name, $timestamp, TRUE, $children);

  // Create an entity metadata wrapper for the log.
  $log_wrapper = entity_metadata_wrapper('log', $log);

  // Set the birth mother.
  $log_wrapper->field_farm_mother
    ->set($parents['mother']->id);

  // Set the location (from the mother, if available).
  $movement_log = farm_movement_asset_latest_movement($parents['mother']);
  if (!empty($movement_log)) {
    $movement_log_wrapper = entity_metadata_wrapper('log', $movement_log);
    $movement_field = entity_create('field_collection_item', array(
      'field_name' => 'field_farm_movement',
    ));
    $movement_field
      ->setHostEntity('log', $log);
    $movement_field_wrapper = entity_metadata_wrapper('field_collection_item', $movement_field);
    $movement_field_wrapper->field_farm_move_to
      ->set($movement_log_wrapper->field_farm_movement->field_farm_move_to
      ->value());
    $movement_field_wrapper->field_farm_geofield
      ->set($movement_log_wrapper->field_farm_movement->field_farm_geofield
      ->value());
    $movement_field_wrapper
      ->save();
  }

  // Get group membership - by default from the mother (if available), or user selected (if any)
  $groups = array();
  if (empty($form_state['values']['birth']['assign_group'])) {
    $membership_log = farm_group_asset_latest_membership($parents['mother']);
    if (!empty($membership_log)) {
      $membership_log_wrapper = entity_metadata_wrapper('log', $membership_log);
      $groups = $membership_log_wrapper->field_farm_membership->field_farm_group
        ->value();
    }
  }
  else {

    // Load the selected groups; only proceed if the group multiselect has a value
    if (!empty($form_state['values']['birth']['group']['group'])) {
      $groups = farm_asset_load_multiple($form_state['values']['birth']['group']['group']);
    }
  }

  // Set group membership
  if (!empty($groups)) {
    $membership_field = entity_create('field_collection_item', array(
      'field_name' => 'field_farm_membership',
    ));
    $membership_field
      ->setHostEntity('log', $log);
    $membership_field_wrapper = entity_metadata_wrapper('field_collection_item', $membership_field);
    $membership_field_wrapper->field_farm_group
      ->set($groups);
    $membership_field_wrapper
      ->save();
  }

  // Set the birth log notes, if available.
  if (!empty($form_state['values']['birth']['notes']['value'])) {
    $log_wrapper->field_farm_notes
      ->set($form_state['values']['birth']['notes']);
  }

  // Save the log.
  $log_wrapper
    ->save();

  // Link the log to the quick form.
  if (function_exists('farm_quick_entity_link')) {
    farm_quick_entity_link('farm_livestock_birth_form', 'log', $log);
  }

  // Set a message linking to the mother animal.
  $label = entity_label('farm_asset', $parents['mother']);
  $uri = entity_uri('farm_asset', $parents['mother']);
  drupal_set_message(t("View the mother's animal record") . ': ' . l($label, $uri['path']));

  // Add the children to $form_state['storage'] so that other submit functions
  // can work with them.
  $form_state['storage']['children'] = $children;
}