Move quantity log query functions into a new module to fix hidden circular dependency between farm_area and farm_quantity.

This commit is contained in:
Michael Stenta 2018-03-21 11:44:48 -04:00
parent 71655083d7
commit 2aa4e58123
8 changed files with 199 additions and 181 deletions

View File

@ -707,6 +707,13 @@ function farm_update_7039(&$sandbox) {
_farm_update_enable_modules(array('farm_flags'));
}
/**
* Enable new Farm Quantity Log module.
*/
function farm_update_7040(&$sandbox) {
_farm_update_enable_modules(array('farm_quantity_log'));
}
/**
* Update helper function: enable modules.
*/

View File

@ -7,6 +7,7 @@ dependencies[] = ctools
dependencies[] = farm_fields
dependencies[] = farm_flags
dependencies[] = farm_map
dependencies[] = farm_quantity
dependencies[] = features
dependencies[] = field_group
dependencies[] = file

View File

@ -18,7 +18,7 @@ dependencies[] = farm_group
dependencies[] = farm_inventory
dependencies[] = farm_map
dependencies[] = farm_movement
dependencies[] = farm_quantity
dependencies[] = farm_quantity_log
dependencies[] = features
dependencies[] = field_collection
dependencies[] = field_group

View File

@ -477,7 +477,7 @@ function farm_livestock_asset_form_submit(array $form, array &$form_state) {
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');
$log = farm_quantity_log_asset($asset, 'weight');
// if no weight observation log exists for asset
if (empty($log)) {

View File

@ -3,7 +3,6 @@ description = Provides a framework for dealing with quantities.
core = 7.x
package = farmOS
dependencies[] = ctools
dependencies[] = farm_log
dependencies[] = features
dependencies[] = field_collection
dependencies[] = fraction

View File

@ -170,181 +170,3 @@ function farm_quantity_preprocess_field(&$vars) {
drupal_add_css(drupal_get_path('module', 'farm_quantity') . '/farm_quantity.css');
}
}
/**
* Extract quantity data from a log, with optional filters for measure/label.
*
* @param Log $log
* The log object to extract quantity information from.
* @param string $measure
* The quantity measure to search for (ie: weight).
* @param string $label
* The quantity label to search for.
*
* @return array
* Returns a structured array of information about the quantities recorded
* on the log.
*/
function farm_quantity_log_data(Log $log, $measure = NULL, $label = NULL) {
// Start with an empty data array.
$data = array();
// Load the log entity metadata wrapper.
$log_wrapper = entity_metadata_wrapper('log', $log);
// If there are no quantities, bail.
if (empty($log_wrapper->field_farm_quantity)) {
return $data;
}
// Iterate over the quantities.
foreach ($log_wrapper->field_farm_quantity as $quantity) {
// If a measure is specified, and it doesn't match, skip this one.
if (!empty($measure) && $quantity->field_farm_quantity_measure->value() != $measure) {
continue;
}
// If a label is specified, and it doesn't match, skip this one.
if (!empty($label) && $quantity->field_farm_quantity_label->value() != $measure) {
continue;
}
// Get the quantity value and convert to a decimal.
$value = '';
if (!empty($quantity->field_farm_quantity_value->value())) {
if (!empty($quantity->field_farm_quantity_value->value()['fraction'])) {
$value = $quantity->field_farm_quantity_value->value()['fraction']->toDecimal(0, TRUE);
}
}
// Get the quantity units name.
$units = '';
if (!empty($quantity->field_farm_quantity_units->value())) {
if (!empty($quantity->field_farm_quantity_units->value()->name)) {
$units = $quantity->field_farm_quantity_units->value()->name;
}
}
// Add quantity data to the array.
$data[] = array(
'measure' => $quantity->field_farm_quantity_measure->value(),
'value' => $value,
'units' => $units,
'label' => $quantity->field_farm_quantity_label->value(),
);
}
// Return the data.
return $data;
}
/**
* Load an asset's latest log with a given quantity measure and/or label.
*
* @param FarmAsset $asset
* The farm_asset object to look for.
* @param string $measure
* The quantity measure to search for (ie: weight).
* @param string $label
* The quantity label to search for.
* @param int $time
* Unix timestamp limiter. Only logs before this time will be included.
* Defaults to the current time. Set to 0 to load the absolute last.
* @param bool $done
* Whether or not to only show logs that are marked as "done". Defaults to
* TRUE.
*
* @return Log|bool
* Returns a log entity. FALSE if something goes wrong.
*/
function farm_quantity_asset_log(FarmAsset $asset, $measure = NULL, $label = NULL, $time = REQUEST_TIME, $done = TRUE) {
// If the asset doesn't have an ID (for instance if it is new and hasn't been
// saved yet), bail.
if (empty($asset->id)) {
return FALSE;
}
// Make a query for loading the latest quantity log.
$query = farm_quantity_asset_log_query($asset->id, $measure, $label, $time, $done);
// Execute the query and gather the log id.
$result = $query->execute();
$log_id = $result->fetchField();
// If a log id exists, load and return it.
if (!empty($log_id)) {
return log_load($log_id);
}
return FALSE;
}
/**
* Build a query to find the latest log of an asset that defines a quantity.
*
* @param int $asset_id
* The asset id to search for.
* @param string $measure
* The quantity measure to search for (ie: weight).
* @param string $label
* The quantity label to search for.
* @param int $time
* Unix timestamp limiter. Only logs before this time will be included.
* Defaults to the current time. Set to 0 to load the absolute last.
* @param bool $done
* Whether or not to only show logs that are marked as "done". Defaults to
* TRUE.
*
* @return \SelectQuery
* Returns a SelectQuery object.
*/
function farm_quantity_asset_log_query($asset_id, $measure = NULL, $label = NULL, $time = REQUEST_TIME, $done = TRUE) {
/**
* Please read the comments in farm_log_asset_query() to understand how this
* works, and to be aware of the limitations and responsibilities we have in
* this function with regard to sanitizing query inputs.
*/
// Ensure that $measure and $label are valid strings, because we use them
// directly in the query's WHERE statements below. This is defensive code.
// See note about views_join_subquery in farm_log_asset_query().
if (!is_null($measure)) {
$measures = farm_quantity_measure_options();
if (!array_key_exists($measure, $measures)) {
$measure = '';
}
}
if (!is_null($label)) {
$label = db_escape_field($label);
}
// Use the farm_log_asset_query() helper function to start a query object.
$query = farm_log_asset_query($asset_id, $time, $done);
// Add the log ID field.
$query->addField('ss_log', 'id');
// Join in the Quantity field collection. Use an inner join to exclude logs
// that do not have quantity field collection attached.
$query->innerJoin('field_data_field_farm_quantity', 'ss_fdffq', "ss_fdffq.entity_type = 'log' AND ss_fdffq.entity_id = ss_log.id AND ss_fdffq.deleted = 0");
// Filter to only include logs with a matching measure. Use an inner join to
// exclude logs that do not have a measure.
if (!empty($measure)) {
$query->innerJoin('field_data_field_farm_quantity_measure', 'ss_fdffqm', "ss_fdffqm.entity_id = ss_fdffq.field_farm_quantity_value AND ss_fdffqm.deleted = 0");
$query->where("ss_fdffqm.field_farm_quantity_measure_value = '" . $measure . "'");
}
// Filter to only include logs with a matching label. Use an inner join to
// exclude logs that do not have a label.
if (!empty($label)) {
$query->innerJoin('field_data_field_farm_quantity_label', 'ss_fdffql', "ss_fdffql.entity_id = ss_fdffq.field_farm_quantity_value AND ss_fdffql.deleted = 0");
$query->where("ss_fdffql.field_farm_quantity_label_value = '" . $label . "'");
}
// Return the query object.
return $query;
}

View File

@ -0,0 +1,6 @@
name = Farm Quantity Log
description = Provides helper functions for querying logs with quantity data.
core = 7.x
package = farmOS
dependencies[] = farm_log
dependencies[] = farm_quantity

View File

@ -0,0 +1,183 @@
<?php
/**
* @file
* Farm quantity log module.
*/
/**
* Extract quantity data from a log, with optional filters for measure/label.
*
* @param Log $log
* The log object to extract quantity information from.
* @param string $measure
* The quantity measure to search for (ie: weight).
* @param string $label
* The quantity label to search for.
*
* @return array
* Returns a structured array of information about the quantities recorded
* on the log.
*/
function farm_quantity_log_data(Log $log, $measure = NULL, $label = NULL) {
// Start with an empty data array.
$data = array();
// Load the log entity metadata wrapper.
$log_wrapper = entity_metadata_wrapper('log', $log);
// If there are no quantities, bail.
if (empty($log_wrapper->field_farm_quantity)) {
return $data;
}
// Iterate over the quantities.
foreach ($log_wrapper->field_farm_quantity as $quantity) {
// If a measure is specified, and it doesn't match, skip this one.
if (!empty($measure) && $quantity->field_farm_quantity_measure->value() != $measure) {
continue;
}
// If a label is specified, and it doesn't match, skip this one.
if (!empty($label) && $quantity->field_farm_quantity_label->value() != $measure) {
continue;
}
// Get the quantity value and convert to a decimal.
$value = '';
if (!empty($quantity->field_farm_quantity_value->value())) {
if (!empty($quantity->field_farm_quantity_value->value()['fraction'])) {
$value = $quantity->field_farm_quantity_value->value()['fraction']->toDecimal(0, TRUE);
}
}
// Get the quantity units name.
$units = '';
if (!empty($quantity->field_farm_quantity_units->value())) {
if (!empty($quantity->field_farm_quantity_units->value()->name)) {
$units = $quantity->field_farm_quantity_units->value()->name;
}
}
// Add quantity data to the array.
$data[] = array(
'measure' => $quantity->field_farm_quantity_measure->value(),
'value' => $value,
'units' => $units,
'label' => $quantity->field_farm_quantity_label->value(),
);
}
// Return the data.
return $data;
}
/**
* Load an asset's latest log with a given quantity measure and/or label.
*
* @param FarmAsset $asset
* The farm_asset object to look for.
* @param string $measure
* The quantity measure to search for (ie: weight).
* @param string $label
* The quantity label to search for.
* @param int $time
* Unix timestamp limiter. Only logs before this time will be included.
* Defaults to the current time. Set to 0 to load the absolute last.
* @param bool $done
* Whether or not to only show logs that are marked as "done". Defaults to
* TRUE.
*
* @return Log|bool
* Returns a log entity. FALSE if something goes wrong.
*/
function farm_quantity_log_asset(FarmAsset $asset, $measure = NULL, $label = NULL, $time = REQUEST_TIME, $done = TRUE) {
// If the asset doesn't have an ID (for instance if it is new and hasn't been
// saved yet), bail.
if (empty($asset->id)) {
return FALSE;
}
// Make a query for loading the latest quantity log.
$query = farm_quantity_log_asset_query($asset->id, $measure, $label, $time, $done);
// Execute the query and gather the log id.
$result = $query->execute();
$log_id = $result->fetchField();
// If a log id exists, load and return it.
if (!empty($log_id)) {
return log_load($log_id);
}
return FALSE;
}
/**
* Build a query to find the latest log of an asset that defines a quantity.
*
* @param int $asset_id
* The asset id to search for.
* @param string $measure
* The quantity measure to search for (ie: weight).
* @param string $label
* The quantity label to search for.
* @param int $time
* Unix timestamp limiter. Only logs before this time will be included.
* Defaults to the current time. Set to 0 to load the absolute last.
* @param bool $done
* Whether or not to only show logs that are marked as "done". Defaults to
* TRUE.
*
* @return \SelectQuery
* Returns a SelectQuery object.
*/
function farm_quantity_log_asset_query($asset_id, $measure = NULL, $label = NULL, $time = REQUEST_TIME, $done = TRUE) {
/**
* Please read the comments in farm_log_asset_query() to understand how this
* works, and to be aware of the limitations and responsibilities we have in
* this function with regard to sanitizing query inputs.
*/
// Ensure that $measure and $label are valid strings, because we use them
// directly in the query's WHERE statements below. This is defensive code.
// See note about views_join_subquery in farm_log_asset_query().
if (!is_null($measure)) {
$measures = farm_quantity_measure_options();
if (!array_key_exists($measure, $measures)) {
$measure = '';
}
}
if (!is_null($label)) {
$label = db_escape_field($label);
}
// Use the farm_log_asset_query() helper function to start a query object.
$query = farm_log_asset_query($asset_id, $time, $done);
// Add the log ID field.
$query->addField('ss_log', 'id');
// Join in the Quantity field collection. Use an inner join to exclude logs
// that do not have quantity field collection attached.
$query->innerJoin('field_data_field_farm_quantity', 'ss_fdffq', "ss_fdffq.entity_type = 'log' AND ss_fdffq.entity_id = ss_log.id AND ss_fdffq.deleted = 0");
// Filter to only include logs with a matching measure. Use an inner join to
// exclude logs that do not have a measure.
if (!empty($measure)) {
$query->innerJoin('field_data_field_farm_quantity_measure', 'ss_fdffqm', "ss_fdffqm.entity_id = ss_fdffq.field_farm_quantity_value AND ss_fdffqm.deleted = 0");
$query->where("ss_fdffqm.field_farm_quantity_measure_value = '" . $measure . "'");
}
// Filter to only include logs with a matching label. Use an inner join to
// exclude logs that do not have a label.
if (!empty($label)) {
$query->innerJoin('field_data_field_farm_quantity_label', 'ss_fdffql', "ss_fdffql.entity_id = ss_fdffq.field_farm_quantity_value AND ss_fdffql.deleted = 0");
$query->where("ss_fdffql.field_farm_quantity_label_value = '" . $label . "'");
}
// Return the query object.
return $query;
}