Use farm_log_asset_query() to build quantity query.
This commit is contained in:
parent
efb3c20073
commit
ae939e345a
|
@ -3,6 +3,7 @@ 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
|
||||
|
|
|
@ -223,7 +223,7 @@ function farm_quantity_asset_log(FarmAsset $asset, $measure = NULL, $label = NUL
|
|||
}
|
||||
|
||||
/**
|
||||
* Build a query to find the latest log of an asset that define a movement.
|
||||
* 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.
|
||||
|
@ -244,24 +244,14 @@ function farm_quantity_asset_log(FarmAsset $asset, $measure = NULL, $label = NUL
|
|||
function farm_quantity_asset_log_query($asset_id, $measure = NULL, $label = NULL, $time = REQUEST_TIME, $done = TRUE) {
|
||||
|
||||
/**
|
||||
* This function is modeled after farm_movement_asset_movement_query().
|
||||
* It intentionally does not use any database query methods that pass
|
||||
* arguments separately (ie: condition()), because those do not work when the
|
||||
* query is used as a sub-select join (via the views_join_subquery class). So
|
||||
* it is the responsibility of this function to sanitize any inputs that are
|
||||
* used directly in SQL.
|
||||
* 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 $asset_id and $time are positive integers, and $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 above.
|
||||
if (!is_int($asset_id) || $asset_id < 0) {
|
||||
$asset_id = (int) $asset_id;
|
||||
}
|
||||
if (!is_int($time) || $time < 0) {
|
||||
$time = REQUEST_TIME;
|
||||
}
|
||||
// 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)) {
|
||||
|
@ -272,52 +262,26 @@ function farm_quantity_asset_log_query($asset_id, $measure = NULL, $label = NULL
|
|||
$label = db_escape_field($label);
|
||||
}
|
||||
|
||||
// Build a query to find an asset's latest log that defines a quantity.
|
||||
$query = db_select('log', 'l');
|
||||
|
||||
// Join in asset references and filter to only include logs that reference the
|
||||
// specified asset.
|
||||
$query->join('field_data_field_farm_asset', 'fdffa', "fdffa.entity_type = 'log' AND fdffa.entity_id = l.id AND fdffa.deleted = 0");
|
||||
$query->where('fdffa.field_farm_asset_target_id = ' . $asset_id);
|
||||
// Use the farm_log_asset_query() helper function to start a query object.
|
||||
$query = farm_log_asset_query($asset_id, $time, $done);
|
||||
|
||||
// Join in the Quantity field collection and filter to only include logs that
|
||||
// have quantities.
|
||||
$query->join('field_data_field_farm_quantity', 'fdffq', "fdffq.entity_type = 'log' AND fdffq.entity_id = l.id AND fdffq.deleted = 0");
|
||||
$query->where('fdffq.field_farm_quantity_value IS NOT NULL');
|
||||
$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');
|
||||
|
||||
// Filter to only include logs with a matching measure.
|
||||
if (!empty($measure)) {
|
||||
$query->join('field_data_field_farm_quantity_measure', 'fdffqm', "fdffqm.entity_id = fdffq.field_farm_quantity_value AND fdffqm.deleted = 0");
|
||||
$query->where("fdffqm.field_farm_quantity_measure_value = '" . $measure . "'");
|
||||
$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 . "'");
|
||||
}
|
||||
|
||||
// Filter to only include logs with a matching label.
|
||||
if (!empty($label)) {
|
||||
$query->join('field_data_field_farm_quantity_label', 'fdffql', "fdffql.entity_id = fdffq.field_farm_quantity_value AND fdffql.deleted = 0");
|
||||
$query->where("fdffql.field_farm_quantity_label_value = '" . $label . "'");
|
||||
$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 . "'");
|
||||
}
|
||||
|
||||
// Order by timestamp and then log id, descending.
|
||||
$query->orderBy('l.timestamp', 'DESC');
|
||||
$query->orderBy('l.id', 'DESC');
|
||||
|
||||
// We only want the first result.
|
||||
$query->range(0, 1);
|
||||
|
||||
// If only "done" movement logs should be included, add a filter.
|
||||
if ($done) {
|
||||
$query->where('l.done = 1');
|
||||
}
|
||||
|
||||
// If $time is not zero, limit to only logs before it. This allows the
|
||||
// absolute last log to be found by setting $time to zero.
|
||||
if ($time !== 0) {
|
||||
$query->where('l.timestamp <= ' . $time);
|
||||
}
|
||||
|
||||
// Add the log ID field.
|
||||
$query->addField('l', 'id');
|
||||
|
||||
// Return the query object.
|
||||
return $query;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue