242 lines
6.4 KiB
Plaintext
242 lines
6.4 KiB
Plaintext
<?php
|
|
/**
|
|
* @file
|
|
* Code for the Farm Log: Movement feature.
|
|
*/
|
|
|
|
// Include Features code.
|
|
include_once 'farm_log_movement.features.inc';
|
|
|
|
// Include Asset Location code.
|
|
include_once 'farm_log_movement.location.inc';
|
|
|
|
/**
|
|
* Implements hook_farm_ui_taxonomy_views().
|
|
*/
|
|
function farm_log_movement_farm_ui_taxonomy_views($term) {
|
|
|
|
// If the term is not an area, bail.
|
|
if ($term->vocabulary_machine_name != 'farm_areas') {
|
|
return array();
|
|
}
|
|
|
|
// Return a list of Views to include on Areas.
|
|
return array(
|
|
|
|
// Area asset history (at the bottom).
|
|
array(
|
|
'name' => 'farm_area_assets',
|
|
'weight' => 100,
|
|
'always' => TRUE,
|
|
),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Implements hook_entity_presave().
|
|
*/
|
|
function farm_log_movement_entity_presave($entity, $type) {
|
|
|
|
// When a movement field collection entity is being saved, populate the
|
|
// geometry field from the "move to" area reference field.
|
|
if ($type == 'field_collection_item' && $entity->field_name == 'field_farm_movement') {
|
|
farm_log_movement_populate_geometry($entity);
|
|
}
|
|
|
|
// When a log is being saved, populate the "move from" field with current
|
|
// asset location(s).
|
|
if ($type == 'log') {
|
|
farm_log_movement_populate_move_from($entity);
|
|
}
|
|
|
|
// If an activity log is being saved with movement information, auto-fill the
|
|
// log title.
|
|
if ($type == 'log' && $entity->type == 'farm_activity') {
|
|
farm_log_movement_populate_name($entity);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Helper function for populating a movement field collection geometry from the
|
|
* "move to" area reference field.
|
|
*
|
|
* @param Entity $entity
|
|
* The entity to act upon.
|
|
*
|
|
* @see farm_log_entity_presave().
|
|
*/
|
|
function farm_log_movement_populate_geometry($entity) {
|
|
|
|
// Define the area field name.
|
|
$area_field = 'field_farm_move_to';
|
|
|
|
// If the log doesn't have an area reference field, bail.
|
|
if (!isset($entity->{$area_field})) {
|
|
return;
|
|
}
|
|
|
|
// If a geometry is already defined, bail.
|
|
if (!empty($entity->field_farm_geofield[LANGUAGE_NONE][0]['geom'])) {
|
|
return;
|
|
}
|
|
|
|
// Load the area(s) referenced by the area reference field.
|
|
$area_ids = array();
|
|
if (!empty($entity->{$area_field}[LANGUAGE_NONE])) {
|
|
foreach ($entity->{$area_field}[LANGUAGE_NONE] as $area_reference) {
|
|
if (!empty($area_reference['tid'])) {
|
|
$area_ids[] = $area_reference['tid'];
|
|
}
|
|
}
|
|
}
|
|
|
|
// Extract geometries from the areas.
|
|
$geoms = farm_area_extract_geoms($area_ids);
|
|
|
|
// Populate the geofield.
|
|
farm_map_geofield_populate($entity, $geoms);
|
|
}
|
|
|
|
/**
|
|
* Helper function for populating a movement field collection's "from" field
|
|
* based on current asset location(s).
|
|
*
|
|
* @param Entity $log
|
|
* The log entity.
|
|
*/
|
|
function farm_log_movement_populate_move_from($log) {
|
|
|
|
// Create an entity wrapper for the log.
|
|
$log_wrapper = entity_metadata_wrapper('log', $log);
|
|
|
|
// If the movement field collection doesn't exist, bail.
|
|
if (!isset($log_wrapper->field_farm_movement) || empty($log_wrapper->field_farm_movement->value())) {
|
|
return;
|
|
}
|
|
|
|
// If the "move to" field is empty, then this isn't a movement, so bail.
|
|
if (empty($log_wrapper->field_farm_movement->field_farm_move_to->value())) {
|
|
return;
|
|
}
|
|
|
|
// If the "move from" field is already populated, bail.
|
|
if (!empty($log_wrapper->field_farm_movement->field_farm_move_from->value())) {
|
|
return;
|
|
}
|
|
|
|
// If there are no assets referenced, bail.
|
|
if (empty($log_wrapper->field_farm_asset->value())) {
|
|
return;
|
|
}
|
|
|
|
// Load assets.
|
|
$assets = array();
|
|
foreach ($log_wrapper->field_farm_asset->value() as $asset) {
|
|
if (!empty($asset)) {
|
|
$assets[] = $asset;
|
|
}
|
|
}
|
|
|
|
// Keep track of what areas these assets are coming from.
|
|
$from_areas = array();
|
|
|
|
// Iterate through the assets.
|
|
foreach ($assets as $asset) {
|
|
|
|
// Load the asset's current location.
|
|
$areas = farm_log_movement_asset_location($asset);
|
|
|
|
// Build a list of areas.
|
|
if (!empty($areas)) {
|
|
foreach ($areas as $area) {
|
|
|
|
// Avoid adding the same area more than once.
|
|
if (in_array($area->tid, $from_areas)) {
|
|
continue;
|
|
}
|
|
|
|
// Remember the areas that have already been added.
|
|
$from_areas[] = $area->tid;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Add the areas to the "move from" field.
|
|
$log_wrapper->field_farm_movement->field_farm_move_from->set($from_areas);
|
|
}
|
|
|
|
/**
|
|
* Helper function for populating a log's name when it contains movement info.
|
|
*
|
|
* @param Entity $log
|
|
* The log entity.
|
|
*/
|
|
function farm_log_movement_populate_name($log) {
|
|
|
|
// If the log already has a name, bail.
|
|
if (!empty($log->name)) {
|
|
return;
|
|
}
|
|
|
|
// Create an entity wrapper for the log.
|
|
$log_wrapper = entity_metadata_wrapper('log', $log);
|
|
|
|
// If there are no assets, bail.
|
|
if (empty($log_wrapper->field_farm_asset)) {
|
|
return;
|
|
}
|
|
|
|
// If the movement field collection doesn't exist, bail.
|
|
if (!isset($log_wrapper->field_farm_movement) || empty($log_wrapper->field_farm_movement->value())) {
|
|
return;
|
|
}
|
|
|
|
// If there are no "movement to" areas, bail.
|
|
if (empty($log_wrapper->field_farm_movement->field_farm_move_to)) {
|
|
return;
|
|
}
|
|
|
|
// If there are more than one assets, summarize.
|
|
$asset_names = '';
|
|
$count_assets = count($log_wrapper->field_farm_asset);
|
|
if ($count_assets > 1) {
|
|
$asset_names .= $log_wrapper->field_farm_asset[0]->name->value() . ' (+ ' . ($count_assets - 1) . ' ' . t('more') . ')';
|
|
}
|
|
else {
|
|
$asset_names .= $log_wrapper->field_farm_asset[0]->name->value();
|
|
}
|
|
|
|
// If there are more than one areas, summarize.
|
|
$area_names = '';
|
|
$count_areas = count($log_wrapper->field_farm_movement->field_farm_move_to);
|
|
if ($count_areas > 1) {
|
|
$area_names .= $log_wrapper->field_farm_movement->field_farm_move_to[0]->name->value() . ' (+ ' . ($count_areas - 1) . ' ' . t('more') . ')';
|
|
}
|
|
else {
|
|
$area_names .= $log_wrapper->field_farm_movement->field_farm_move_to[0]->name->value();
|
|
}
|
|
|
|
// Build the log name.
|
|
$log->name = t('Move') . ' ' . $asset_names . ' to ' . $area_names;
|
|
}
|
|
|
|
/**
|
|
* Implements hook_entity_view_alter().
|
|
*/
|
|
function farm_log_movement_entity_view_alter(&$build, $type) {
|
|
|
|
// If it's not a farm_asset, or if the entity object is not available, bail.
|
|
if ($type != 'farm_asset' || empty($build['#entity'])) {
|
|
return;
|
|
}
|
|
|
|
// Generate markup to describe the location.
|
|
$output = farm_log_movement_asset_location_markup($build['#entity']);
|
|
|
|
// Add it to the build array.
|
|
$build['location'] = array(
|
|
'#markup' => $output,
|
|
'#weight' => -100,
|
|
);
|
|
}
|