You are here

function farm_quantity_log_data in farmOS 7

Extract quantity data from a log, with optional filters for measure/label.

Parameters

Log $log: The log object to extract quantity information from.

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

string $label: The quantity label to search for.

Return value

array Returns a structured array of information about the quantities recorded on the log.

3 calls to farm_quantity_log_data()
farm_livestock_weight in modules/farm/farm_livestock/farm_livestock_weight/farm_livestock_weight.module
Helper function for retrieving the weight of an animal.
farm_livestock_weight_all in modules/farm/farm_livestock/farm_livestock_weight/farm_livestock_weight.module
Helper function for retrieving all weight logs of an animal.
farm_livestock_weight_group_report in modules/farm/farm_livestock/farm_livestock_weight/farm_livestock_weight.module
Generate Animal Weight Group Report

File

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

Code

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() != $label) {
      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;
}