mirror of
https://github.com/farmOS/farmOS.git
synced 2024-02-23 11:37:38 +01:00
278 lines
9.3 KiB
Text
278 lines
9.3 KiB
Text
<?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;
|
|
}
|
|
|
|
/**
|
|
* Helper function for creating a farm quantity measurement log.
|
|
*
|
|
* @param string $type
|
|
* The log type machine name.
|
|
* @param string $name
|
|
* Optionally specify a name for the new log.
|
|
* @param int $timestamp
|
|
* The timestamp of the log (defaults to REQUEST_TIME).
|
|
* @param bool $done
|
|
* Boolean indicating whether or not the log is done.
|
|
* @param array $assets
|
|
* An array of assets to reference in the log.
|
|
* @param array $measurements
|
|
* An array of measurements to add to the log. Example:
|
|
* $measurements = array(
|
|
* array(
|
|
* 'measure' => 'weight',
|
|
* 'value' => 100,
|
|
* 'units' => 'lbs',
|
|
* 'label' => 'Foo',
|
|
* ),
|
|
* );
|
|
* @param string $notes
|
|
* Notes to add to the log.
|
|
* @param array $categories
|
|
* An array of categories to add to the log.
|
|
*
|
|
* @return \Log
|
|
* Returns a saved log entity.
|
|
*/
|
|
function farm_quantity_log_create($type, $name = '', $timestamp = REQUEST_TIME, $done = TRUE, $assets = array(), $measurements = array(), $notes = '', $categories = array()) {
|
|
|
|
// Create a new log entity.
|
|
$log = farm_log_create($type, $name, $timestamp, $done, $assets, $notes, $categories);
|
|
|
|
// Iterate through the measurements.
|
|
foreach ($measurements as $measurement) {
|
|
|
|
// 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, if available.
|
|
if (!empty($measurement['measure'])) {
|
|
$quantity_wrapper->field_farm_quantity_measure->set($measurement['measure']);
|
|
}
|
|
|
|
// Set the quantity value, if available.
|
|
if (!empty($measurement['value'])) {
|
|
$value_fraction = fraction_from_decimal($measurement['value']);
|
|
$quantity_wrapper->field_farm_quantity_value->numerator->set($value_fraction->getNumerator());
|
|
$quantity_wrapper->field_farm_quantity_value->denominator->set($value_fraction->getDenominator());
|
|
}
|
|
|
|
// Set the units, if available.
|
|
if (!empty($measurement['units'])) {
|
|
|
|
// Look up the units taxonomy term.
|
|
$units_terms = taxonomy_get_term_by_name($measurement['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($measurement['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 label, if available.
|
|
if (!empty($measurement['label'])) {
|
|
$quantity_wrapper->field_farm_quantity_label->set($measurement['label']);
|
|
}
|
|
|
|
// Save the quantity.
|
|
$quantity_wrapper->save();
|
|
}
|
|
|
|
// Return the log.
|
|
return $log;
|
|
}
|