2016-03-21 16:12:33 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Farm quantity module.
|
|
|
|
*/
|
2016-03-21 16:57:31 +01:00
|
|
|
|
|
|
|
// Include Features code.
|
|
|
|
include_once 'farm_quantity.features.inc';
|
|
|
|
|
2016-03-21 18:05:59 +01:00
|
|
|
/**
|
|
|
|
* Implements hook_permission().
|
|
|
|
*/
|
|
|
|
function farm_quantity_permission() {
|
|
|
|
$perms = array(
|
|
|
|
'administer farm_quantity module' => array(
|
|
|
|
'title' => t('Administer farm quantity module'),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
return $perms;
|
|
|
|
}
|
|
|
|
|
2016-03-21 16:57:31 +01:00
|
|
|
/**
|
|
|
|
* Implements hook_farm_access_perms().
|
|
|
|
*/
|
|
|
|
function farm_quantity_farm_access_perms($role) {
|
|
|
|
|
|
|
|
// Assemble a list of entity types provided by this module.
|
|
|
|
$types = array(
|
|
|
|
'taxonomy' => array(
|
|
|
|
'farm_quantity_units',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Grant different CRUD permissions based on the role.
|
|
|
|
$perms = array();
|
|
|
|
switch ($role) {
|
|
|
|
|
|
|
|
// Farm Manager and Worker
|
|
|
|
case 'Farm Manager':
|
|
|
|
case 'Farm Worker':
|
|
|
|
$perms = farm_access_entity_perms($types);
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Farm Viewer
|
|
|
|
case 'Farm Viewer':
|
|
|
|
$perms = farm_access_entity_perms($types, array('view'));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-03-21 18:05:59 +01:00
|
|
|
// If the role is "Farm Manager", grant access to quantity configuration.
|
|
|
|
if ($role == 'Farm Manager') {
|
|
|
|
$perms[] = 'administer farm_quantity module';
|
|
|
|
}
|
|
|
|
|
2016-03-21 16:57:31 +01:00
|
|
|
return $perms;
|
|
|
|
}
|
2016-03-21 18:05:59 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements hook_menu().
|
|
|
|
*/
|
|
|
|
function farm_quantity_menu() {
|
|
|
|
|
|
|
|
// Quantity configuration form.
|
|
|
|
$items['admin/config/farm/quantity'] = array(
|
|
|
|
'title' => 'Quantity',
|
|
|
|
'description' => 'Quantity configuration settings.',
|
|
|
|
'page callback' => 'drupal_get_form',
|
|
|
|
'page arguments' => array('farm_quantity_settings_form'),
|
|
|
|
'access arguments' => array('administer farm_quantity module'),
|
|
|
|
);
|
|
|
|
|
|
|
|
return $items;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Quantity settings form.
|
|
|
|
*/
|
|
|
|
function farm_quantity_settings_form($form, &$form_state) {
|
|
|
|
|
|
|
|
// Metric or US/Imperial.
|
|
|
|
$form['farm_quantity_unit_system'] = array(
|
|
|
|
'#type' => 'radios',
|
|
|
|
'#title' => t('System of measurement'),
|
|
|
|
'#description' => t('Select the system of measurement you would like to use in farmOS.'),
|
|
|
|
'#options' => array(
|
|
|
|
'metric' => t('Metric'),
|
|
|
|
'us' => t('US/Imperial'),
|
|
|
|
),
|
|
|
|
'#default_value' => variable_get('farm_quantity_unit_system', 'metric'),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Return it as a system settings form.
|
|
|
|
return system_settings_form($form);
|
|
|
|
}
|
2017-09-28 20:24:54 +02:00
|
|
|
|
|
|
|
/**
|
2017-11-07 18:50:28 +01:00
|
|
|
* Define information about available quantity measures.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
* Returns an array of measure information.
|
2017-09-28 20:24:54 +02:00
|
|
|
*/
|
2017-11-07 18:50:28 +01:00
|
|
|
function farm_quantity_measures() {
|
2017-09-28 20:24:54 +02:00
|
|
|
return array(
|
2017-11-07 18:50:28 +01:00
|
|
|
'count' => array(
|
|
|
|
'label' => t('Count'),
|
|
|
|
),
|
|
|
|
'length' => array(
|
|
|
|
'label' => t('Length'),
|
|
|
|
),
|
|
|
|
'weight' => array(
|
|
|
|
'label' => t('Weight'),
|
|
|
|
),
|
|
|
|
'volume' => array(
|
|
|
|
'label' => t('Volume'),
|
|
|
|
),
|
|
|
|
'time' => array(
|
|
|
|
'label' => t('Time'),
|
|
|
|
),
|
|
|
|
'temperature' => array(
|
|
|
|
'label' => t('Temperature'),
|
|
|
|
),
|
|
|
|
'water_content' => array(
|
|
|
|
'label' => t('Water content'),
|
|
|
|
),
|
|
|
|
'value' => array(
|
|
|
|
'label' => t('Value'),
|
|
|
|
),
|
|
|
|
'rating' => array(
|
|
|
|
'label' => t('Rating'),
|
|
|
|
),
|
|
|
|
'ratio' => array(
|
|
|
|
'label' => t('Ratio'),
|
|
|
|
),
|
|
|
|
'probability' => array(
|
|
|
|
'label' => t('Probability'),
|
|
|
|
),
|
2017-09-28 20:24:54 +02:00
|
|
|
);
|
|
|
|
}
|
2017-10-06 18:45:20 +02:00
|
|
|
|
2017-11-07 18:50:28 +01:00
|
|
|
/**
|
|
|
|
* Define available options for the Measure field.
|
|
|
|
*/
|
|
|
|
function farm_quantity_measure_options() {
|
|
|
|
|
|
|
|
// Start an empty options array.
|
|
|
|
$options = array();
|
|
|
|
|
|
|
|
// Load information about measures.
|
|
|
|
$measures = farm_quantity_measures();
|
|
|
|
|
|
|
|
// Iterate through the measures and build a list of options.
|
|
|
|
foreach ($measures as $measure => $data) {
|
|
|
|
$options[$measure] = $data['label'];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the array of options.
|
|
|
|
return $options;
|
|
|
|
}
|
|
|
|
|
2017-10-06 18:45:20 +02:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-12 16:09:53 +02:00
|
|
|
* Build a query to find the latest log of an asset that defines a quantity.
|
2017-10-06 18:45:20 +02:00
|
|
|
*
|
|
|
|
* @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) {
|
|
|
|
|
|
|
|
/**
|
2017-10-12 16:09:53 +02:00
|
|
|
* 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.
|
2017-10-06 18:45:20 +02:00
|
|
|
*/
|
|
|
|
|
2017-10-12 16:09:53 +02:00
|
|
|
// 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().
|
2017-10-06 18:45:20 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-10-12 16:09:53 +02:00
|
|
|
// Use the farm_log_asset_query() helper function to start a query object.
|
|
|
|
$query = farm_log_asset_query($asset_id, $time, $done);
|
2017-10-06 18:45:20 +02:00
|
|
|
|
|
|
|
// Join in the Quantity field collection and filter to only include logs that
|
|
|
|
// have quantities.
|
2017-10-12 16:09:53 +02:00
|
|
|
$query->join('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");
|
|
|
|
$query->where('ss_fdffq.field_farm_quantity_value IS NOT NULL');
|
2017-10-06 18:45:20 +02:00
|
|
|
|
|
|
|
// Filter to only include logs with a matching measure.
|
|
|
|
if (!empty($measure)) {
|
2017-10-12 16:09:53 +02:00
|
|
|
$query->join('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 . "'");
|
2017-10-06 18:45:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Filter to only include logs with a matching label.
|
|
|
|
if (!empty($label)) {
|
2017-10-12 16:09:53 +02:00
|
|
|
$query->join('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 . "'");
|
2017-10-06 18:45:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the query object.
|
|
|
|
return $query;
|
|
|
|
}
|