farmOS/modules/farm/farm_livestock/farm_livestock.module

450 lines
13 KiB
Plaintext
Raw Normal View History

<?php
/**
* @file
* Code for the Farm Livestock feature.
*/
include_once 'farm_livestock.features.inc';
/**
* Implements hook_farm_ui_entities().
*/
function farm_livestock_farm_ui_entities() {
return array(
'farm_asset' => array(
'animal' => array(
'label' => t('Animal'),
'label_plural' => t('Animals'),
'view' => 'farm_animals',
),
),
'log' => array(
'farm_medical' => array(
'label' => t('Medical record'),
'label_plural' => t('Medical records'),
'view' => 'farm_log_medical',
'farm_asset' => 'animal',
'weight' => 20,
),
),
'taxonomy_term' => array(
'farm_animal_types' => array(
'label' => t('Type'),
'label_plural' => t('Types'),
'view' => 'farm_animal_types',
'farm_asset' => 'animal',
'asset_view_arg' => 2,
),
),
);
}
/**
* Implements hook_farm_area_type_info().
*/
function farm_livestock_farm_area_type_info() {
return array(
'paddock' => array(
'label' => t('Paddock'),
2015-11-02 05:23:41 +01:00
'style' => 'farm_map_style_dark_green',
'weight' => 5,
),
);
}
/**
* Implements hook_farm_log_categories().
*/
function farm_livestock_farm_log_categories() {
// Provide an "Animals" log category.
return array('Animals');
}
/**
* Implements hook_farm_log_categories_populate().
*/
function farm_livestock_farm_log_categories_populate($log) {
$categories = array();
if ($log->type == 'farm_medical') {
$categories[] = 'Animals';
}
return $categories;
}
/**
* Implements hook_feeds_importer_default_alter().
*/
function farm_livestock_feeds_importer_default_alter($importers) {
// Add extra field mappings to animals.
$name = 'farm_asset_animal';
if (!empty($importers[$name])) {
$mappings = array(
array(
'source' => 'Nicknames',
'target' => 'field_farm_animal_nicknames',
'unique' => FALSE,
'language' => 'und',
),
array(
'source' => 'Date of birth',
'target' => 'field_farm_date:start',
'unique' => FALSE,
'language' => 'und',
),
array(
'source' => 'Species/breed',
'target' => 'field_farm_animal_type',
'term_search' => '0',
'autocreate' => 1,
'language' => 'und',
),
array(
'source' => 'Sex',
'target' => 'field_farm_animal_sex',
'unique' => FALSE,
'language' => 'und',
),
array(
'source' => 'Castrated',
'target' => 'field_farm_animal_castrated',
'unique' => FALSE,
'language' => 'und',
),
array(
'source' => 'Tag ID',
'target' => 'field_farm_animal_tag:field_farm_animal_tag_id',
'unique' => FALSE,
'language' => 'und',
),
array(
'source' => 'Tag type',
'target' => 'field_farm_animal_tag:field_farm_animal_tag_type',
'unique' => FALSE,
'language' => 'und',
),
array(
'source' => 'Tag location',
'target' => 'field_farm_animal_tag:field_farm_animal_tag_location',
'unique' => FALSE,
'language' => 'und',
),
);
$importer_mappings =& $importers[$name]->config['processor']['config']['mappings'];
$importer_mappings = array_merge($importer_mappings, $mappings);
}
}
/**
* Implements hook_feeds_tamper_default_alter().
*/
function farm_livestock_feeds_tamper_default_alter(&$feeds_tampers) {
// If farm_import is not installed, bail.
if (!module_exists('farm_import')) {
return;
}
// Make species/breed required.
$feeds_tamper = farm_import_feeds_tamper_plugin('farm_asset', 'animal', 'Species/breed', 'required');
$feeds_tampers[$feeds_tamper->id] = $feeds_tamper;
// Convert "male" to "M".
$feeds_tamper = farm_import_feeds_tamper_plugin('farm_asset', 'animal', 'Sex', 'find_replace', array('find' => t('male'), 'replace' => 'M'));
$feeds_tamper->id .= '-male';
$feeds_tamper->weight = 1;
$feeds_tampers[$feeds_tamper->id] = $feeds_tamper;
// Convert "female" to "F".
$feeds_tamper = farm_import_feeds_tamper_plugin('farm_asset', 'animal', 'Sex', 'find_replace', array('find' => t('female'), 'replace' => 'F'));
$feeds_tamper->id .= '-female';
$feeds_tamper->weight = 2;
$feeds_tampers[$feeds_tamper->id] = $feeds_tamper;
// Convert castrated to boolean.
$feeds_tamper = farm_import_feeds_tamper_plugin('farm_asset', 'animal', 'Castrated', 'boolean_default_false');
$feeds_tampers[$feeds_tamper->id] = $feeds_tamper;
// Convert date of birth to a Unix timestamp.
$feeds_tamper = farm_import_feeds_tamper_plugin('farm_asset', 'animal', 'Date of birth', 'strtotime');
$feeds_tampers[$feeds_tamper->id] = $feeds_tamper;
// Explode nicknames to allow multiple values.
$feeds_tamper = farm_import_feeds_tamper_plugin('farm_asset', 'animal', 'Nicknames', 'explode');
$feeds_tampers[$feeds_tamper->id] = $feeds_tamper;
}
/**
* Implements hook_entity_view_alter().
*/
function farm_livestock_entity_view_alter(&$build, $type) {
// If it's not a farm_asset, or if the entity object is not available, bail.
if ($type != 'farm_asset' || empty($build['#entity'])) {
return;
}
// Alias the asset variable.
$asset = $build['#entity'];
// If it isn't an animal asset, bail.
if ($asset->type != 'animal') {
return;
}
// Get the animal's current weight.
$weight = farm_livestock_animal_weight($asset);
// If a weight measurement isn't available, bail.
if (empty($weight)) {
return;
}
// Get the value and the units.
$value = !empty($weight['value']) ? $weight['value'] : '';
$units = !empty($weight['units']) ? $weight['units'] : '';
// Build the weight display.
$output = '<strong>' . t('Weight') . ':</strong> ' . $value . ' ' . $units;
// If the animal has an inventory greater than 1, add "(average)".
$inventory = farm_inventory($asset);
if ($inventory > 1) {
$output .= ' (' . t('average') . ')';
}
// Add it to the build array.
$build['weight'] = array(
'#markup' => $output,
'#prefix' => '<div class="weight">',
'#suffix' => '</div>',
'#weight' => -120,
);
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function farm_livestock_form_farm_asset_form_alter(&$form, &$form_state, $form_id) {
// Get the farm asset entity from the form.
$asset = $form['farm_asset']['#value'];
// If the asset is not an animal, bail.
if ($asset->type != 'animal') {
return;
}
// Get the animal's current weight.
$weight = farm_livestock_animal_weight($asset);
// Add a field for setting the animal's current weight.
$form['weight'] = array(
'#type' => 'fieldset',
'#title' => t('Weight'),
'#description' => t('Set the current weight for this animal. If this record is used to represent multiple animals, enter their average weight. An observation log will be created automatically to record the weight.'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#weight' => 100,
'#tree' => TRUE,
);
$form['weight']['value'] = array(
'#type' => 'textfield',
'#title' => t('Weight'),
'#default_value' => !empty($weight['value']) ? $weight['value'] : '',
);
$form['weight']['units'] = array(
'#type' => 'textfield',
'#title' => t('Units'),
'#autocomplete_path' => 'taxonomy/autocomplete/field_farm_quantity_units',
'#default_value' => !empty($weight['units']) ? $weight['units'] : '',
);
$form['actions']['submit']['#validate'][] = 'farm_livestock_asset_form_validate';
$form['actions']['submit']['#submit'][] = 'farm_livestock_asset_form_submit';
$form['#group_children']['weight'] = 'group_farm_general';
}
/**
* Validate handler for processing the animal weight field.
*
* @param array $form
* The form array.
* @param array $form_state
* The form state array.
*/
function farm_livestock_asset_form_validate(array $form, array &$form_state) {
// If units are set, but not weight, show an error.
if (empty($form_state['values']['weight']['value']) && !empty($form_state['values']['weight']['units'])) {
form_set_error('weight][value', t('Both weight and units must be specified.'));
}
}
/**
* Submit handler for processing the animal weight field.
*
* @param array $form
* The form array.
* @param array $form_state
* The form state array.
*/
function farm_livestock_asset_form_submit(array $form, array &$form_state) {
// Only proceed if weight has a value.
if (empty($form_state['values']['weight']['value'])) {
return;
}
// Only proceed if the value is not the default value.
if ($form_state['values']['weight']['value'] == $form['weight']['value']['#default_value']) {
return;
}
// If an asset doesn't exist, bail.
if (empty($form_state['values']['farm_asset'])) {
return;
}
// Grab the asset, weight, and units.
$asset = $form_state['values']['farm_asset'];
$weight = $form_state['values']['weight']['value'];
$units = $form_state['values']['weight']['units'];
// Create an observation log to set the weight.
farm_livestock_weight_set($asset, $weight, $units);
}
/**
* Helper function for retrieving the weight of an animal.
*
* @param FarmAsset $asset
* The animal asset to get weight for.
*
* @return array
* Returns an array of quantity information about the asset's weight, based
* on its latest weight quantity log. Returns an empty array if nothing is
* found.
*/
function farm_livestock_animal_weight($asset) {
// Load the latest log with a 'weight' quantity measurement for this asset.
$log = farm_quantity_asset_log($asset, 'weight');
// if no weight observation log exists for asset
if (empty($log)) {
return array();
}
// Extract quantity data from the log.
$data = farm_quantity_log_data($log, 'weight');
// Iterate through the data and return the first one with a value.
foreach ($data as $quantity) {
if (!empty($quantity['value'])) {
return $quantity;
}
}
// If nothing was returned, return an empty array.
return array();
}
/**
* Create a weight measurement log associated with an animal.
*
* @param FarmAsset $asset
* The asset to add quantity measurements to.
* @param string $weight
* The animal's current weight.
2017-12-16 16:42:27 +01:00
* @param string $units
* The units of measurement.
* @param int $timestamp
* The timestamp of the measurement. Defaults to the current time.
* @param bool $done
* Boolean indicating whether or not the log should be marked "done".
* Defaults to TRUE.
*
* @return \Log
* Returns the log that was created.
*/
function farm_livestock_weight_set($asset, $weight, $units, $timestamp = REQUEST_TIME, $done = TRUE) {
// Create a new log entity.
$log = entity_create('log', array('type' => 'farm_observation'));
// Create an entity wrapper for the log.
$log_wrapper = entity_metadata_wrapper('log', $log);
// Set the name to: "Current weight: [value]".
$log_name = t('Current weight') . ': ' . check_plain($weight) . ' ' . check_plain($units);
$log_wrapper->name->set($log_name);
// Set the date.
$log_wrapper->timestamp->set($timestamp);
// Add the asset to the asset reference field.
$log_wrapper->field_farm_asset[] = $asset;
// Create a new quantity field_collection entity attached to the log.
$quantity = entity_create('field_collection_item', array('field_name' => 'field_farm_quantity'));
$quantity->setHostEntity('log', $log);
// Create an entity wrapper for the quantity.
$quantity_wrapper = entity_metadata_wrapper('field_collection_item', $quantity);
// Set the quantity measure.
$quantity_wrapper->field_farm_quantity_measure->set('weight');
// Set the quantity value.
$value_fraction = fraction_from_decimal($weight);
$quantity_wrapper->field_farm_quantity_value->numerator->set($value_fraction->getNumerator());
$quantity_wrapper->field_farm_quantity_value->denominator->set($value_fraction->getDenominator());
// Look up the units taxonomy term.
$units_terms = taxonomy_get_term_by_name($units, 'farm_quantity_units');
// If terms were found, use the first one.
if (!empty($units_terms)) {
$units_term = reset($units_terms);
}
// If a term wasn't found, create it.
else {
$farm_units = taxonomy_vocabulary_machine_name_load('farm_quantity_units');
$units_term = new stdClass();
$units_term->name = check_plain($units);
$units_term->vid = $farm_units->vid;
taxonomy_term_save($units_term);
}
// Set the quantity units.
$quantity_wrapper->field_farm_quantity_units = $units_term;
// Set the log's done status.
if (!empty($done)) {
$log_wrapper->done->set(TRUE);
}
else {
$log_wrapper->done->set(FALSE);
}
// Set the log owner.
global $user;
$log_wrapper->field_farm_log_owner[] = $user;
// Save the quantity.
$quantity_wrapper->save();
// Save the log.
$log_wrapper->save();
// Set a message.
$label = entity_label('log', $log);
$uri = entity_uri('log', $log);
drupal_set_message('Log created: ' . l($label, $uri['path']));
// Return the log.
return $log;
}