You are here

function farm_quantity_log_query in farmOS 7

Build a query to find logs that define quantity measurements.

Parameters

string $measure: The quantity measure to search for (ie: weight).

string $label: The quantity label to search for.

int $start_time: Unix timestamp limiter. Only logs after this time will be included. Defaults to 0, which will not limit the logs at all.

int $end_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.

string $category: The log category to search for.

Return value

\SelectQuery Returns a SelectQuery object.

1 call to farm_quantity_log_query()
farm_quantity_log in modules/farm/farm_quantity/farm_quantity_log/farm_quantity_log.module
Load all quantity logs that match certain criteria.

File

modules/farm/farm_quantity/farm_quantity_log/farm_quantity_log.module, line 251
Farm quantity log module.

Code

function farm_quantity_log_query($measure = NULL, $label = NULL, $start_time = 0, $end_time = REQUEST_TIME, $done = TRUE, $type = NULL, $category = NULL) {

  /**
   * Please read the comments in farm_log_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.
   */

  // Use the farm_log_query() helper function to start a query object.
  $query = farm_log_query($end_time, $done, $type, FALSE);

  // Add a query tag to identify where this came from.
  $query
    ->addTag('farm_quantity_log_query');

  // Add quantity filters to the query using a helper function.
  farm_quantity_log_query_add_filters($query, $measure, $label);

  // Ensure $start_time is valid, because it may be used directly in the query
  // string. This is defensive code. See note about views_join_subquery above.
  // Similar logic is used for the $end_time in farm_log_query().
  // In this case, we want to make sure that it is numeric AND that it is not
  // greater than the end time.
  if (!is_numeric($start_time) || $start_time > $end_time) {
    $start_time = 0;
  }

  // If $start_time is not zero, limit to only logs after it. This allows the
  // absolute first log to be found by setting $start_time to zero.
  // Similar logic is used for the $end_time in farm_log_query().
  if ($start_time !== 0) {
    $query
      ->where('ss_log.timestamp >= ' . $start_time);
  }

  // If $category is not empty, load the category ID, then join in the log
  // category field data and limit the query to logs with a matching category.
  if (!empty($category)) {
    $category_term = farm_term($category, 'farm_log_categories', FALSE);
    if (!empty($category_term->tid)) {
      $query
        ->innerJoin('field_data_field_farm_log_category', 'ss_fdfflc', "ss_fdfflc.entity_type = 'log' AND ss_fdfflc.entity_id = ss_log.id AND ss_fdfflc.deleted = 0");
      $query
        ->where("ss_fdfflc.field_farm_log_category_tid = '" . $category_term->tid . "'");
    }
  }

  // Return the query object.
  return $query;
}