function farm_log_query in farmOS 7
Helper function for building a select query of logs.
This function is used by other modules to build queries and Views handlers that need to find the most recent log in a specific context.
Modules can use this to generate a base query, and then add their own modifications on top of that.
Parameters
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.
$done: Whether or not to only show logs that are marked as "done". TRUE will limit to logs that are done, and FALSE will limit to logs that are not done. If any other value is used, no filtering will be applied. Defaults to TRUE.
string|null $type: The log type to filter by. If this is NULL, no filtering will be applied.
bool $single: Whether or not to limit the query to a single result. Defaults to TRUE.
Return value
\SelectQuery Returns a SelectQuery object.
4 calls to farm_log_query()
- farm_inventory_query in modules/
farm/ farm_inventory/ farm_inventory.module - Build a query to calculate an asset's inventory level.
- farm_log_asset_query in modules/
farm/ farm_log/ farm_log.module - Helper function for building a select query of logs related to assets.
- farm_movement_area_movement_query in modules/
farm/ farm_movement/ farm_movement.location.inc - Build a query to find movement logs to a specific area.
- farm_quantity_log_query in modules/
farm/ farm_quantity/ farm_quantity_log/ farm_quantity_log.module - Build a query to find logs that define quantity measurements.
File
- modules/
farm/ farm_log/ farm_log.module, line 692 - Code for the Farm Log feature.
Code
function farm_log_query($time = REQUEST_TIME, $done = TRUE, $type = NULL, $single = TRUE) {
/**
* This query may be used as a sub-query join in a Views handler via the
* views_join_subquery class (for an example see:
* farm_movement_handler_relationship_location). When a sub-query is added
* via views_join_subquery, it is not possible to use query arguments in the
* sub-query itself. So we cannot use the query::condition() method, or any
* other methods that take query arguments separately and perform sanitation
* on them. Thus, it is the responsibility of this function to sanitize any
* inputs and use them directly in the SQL.
*
* We use the "ss_" prefix on aliases to avoid potential name conflicts when
* this query is used as a sub-select inside another query.
*/
// Ensure $time is valid, because it may be used directly in the query
// string. This is defensive code. See note about views_join_subquery above.
if (!is_numeric($time) || $time < 0) {
$time = REQUEST_TIME;
}
// Ensure that $type is a valid strings, because we use it directly in the
// query's WHERE statements below. This is defensive code. See note about
// views_join_subquery in farm_log_query().
if (!is_null($type)) {
$type = db_escape_field($type);
}
// Build a query to find a log that references an asset.
$query = db_select('log', 'ss_log');
// Add a query tag to identify where this came from.
$query
->addTag('farm_log_query');
// If $type is not empty, filter to logs of that type.
if (!empty($type)) {
$query
->where("ss_log.type = '" . $type . "'");
}
// 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('ss_log.timestamp <= ' . $time);
}
// Filter logs based on whether they are done or not. This will only happen
// if $done is explicitly set to TRUE or FALSE. If any other value is used,
// filtering will not take place.
if ($done === TRUE) {
$query
->where('ss_log.done = 1');
}
elseif ($done === FALSE) {
$query
->where('ss_log.done = 0');
}
// Order by timestamp and then log id, descending.
$query
->orderBy('ss_log.timestamp', 'DESC');
$query
->orderBy('ss_log.id', 'DESC');
// Limit the query to a single result (the first one), if desired.
if (!empty($single)) {
$query
->range(0, 1);
}
// Return the query object.
return $query;
}