2014-02-04 09:41:17 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Code for the Farm Log feature.
|
|
|
|
*/
|
|
|
|
|
|
|
|
include_once 'farm_log.features.inc';
|
2014-11-13 21:53:13 +01:00
|
|
|
|
2014-11-26 22:57:00 +01:00
|
|
|
/**
|
|
|
|
* Implements hook_menu_local_tasks_alter().
|
|
|
|
*/
|
|
|
|
function farm_log_menu_local_tasks_alter(&$data, $router_item, $root_path) {
|
|
|
|
|
|
|
|
// Define actions.
|
|
|
|
$actions = array(
|
2014-11-29 01:45:46 +01:00
|
|
|
'activity' => array(
|
2014-12-01 01:42:41 +01:00
|
|
|
'title' => t('Add an activity'),
|
2014-11-29 01:45:46 +01:00
|
|
|
'href' => 'log/add/farm_activity',
|
|
|
|
),
|
2014-11-26 22:57:00 +01:00
|
|
|
'movement' => array(
|
2014-12-01 01:42:41 +01:00
|
|
|
'title' => t('Add a movement'),
|
2014-11-26 22:57:00 +01:00
|
|
|
'href' => 'log/add/farm_movement',
|
|
|
|
),
|
2014-11-27 13:47:07 +01:00
|
|
|
'issue' => array(
|
2014-12-01 01:42:41 +01:00
|
|
|
'title' => t('Add an issue'),
|
2014-11-27 13:47:07 +01:00
|
|
|
'href' => 'log/add/farm_issue',
|
|
|
|
),
|
2014-11-26 22:57:00 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// Define actions for various paths.
|
|
|
|
$path_actions = array(
|
2014-11-29 01:45:46 +01:00
|
|
|
'admin/farm' => array('activity', 'issue'),
|
|
|
|
'admin/farm/plan/activities' => array('activity'),
|
2014-11-29 01:44:58 +01:00
|
|
|
'admin/farm/plan/movements' => array('movement'),
|
|
|
|
'admin/farm/plan/issues' => array('issue'),
|
|
|
|
'farm/asset/%' => array('movement'),
|
|
|
|
'farm/asset/%/movements' => array('movement'),
|
2014-11-26 22:57:00 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// Add actions depending on the root path.
|
|
|
|
if (array_key_exists($root_path, $path_actions)) {
|
2014-11-29 01:44:58 +01:00
|
|
|
foreach ($path_actions[$root_path] as $action) {
|
|
|
|
$output = array(
|
|
|
|
'#theme' => 'menu_local_action',
|
|
|
|
'#link' => array(
|
|
|
|
'title' => $actions[$action]['title'],
|
|
|
|
'href' => $actions[$action]['href'],
|
|
|
|
'localized_options' => array(
|
|
|
|
'query' => array(
|
|
|
|
'destination' => $root_path,
|
|
|
|
),
|
2014-11-26 22:57:00 +01:00
|
|
|
),
|
|
|
|
),
|
2014-11-29 01:44:58 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// If this is a farm asset movements path...
|
|
|
|
if (in_array($root_path, array(
|
|
|
|
'farm/asset/%',
|
|
|
|
'farm/asset/%/movements'
|
|
|
|
))) {
|
|
|
|
|
|
|
|
// Get the asset id from the path.
|
|
|
|
$asset_id = check_plain(arg(2));
|
|
|
|
|
|
|
|
// Set the destination to the farm asset page.
|
|
|
|
$output['#link']['localized_options']['query']['destination'] = 'farm/asset/' . $asset_id;
|
|
|
|
|
|
|
|
// Set the farm_asset query string to the asset id. This will be used to
|
|
|
|
// prepopualte asset reference fields in log entities.
|
|
|
|
// See hook_form_alter() below
|
|
|
|
$output['#link']['localized_options']['query']['farm_asset'] = $asset_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the action output.
|
|
|
|
if (!empty($output)) {
|
|
|
|
$data['actions']['output'][] = $output;
|
|
|
|
}
|
|
|
|
}
|
2014-11-27 15:08:42 +01:00
|
|
|
}
|
2014-11-26 22:57:00 +01:00
|
|
|
}
|
|
|
|
|
2014-11-13 21:53:13 +01:00
|
|
|
/**
|
|
|
|
* Implements hook_form_alter().
|
|
|
|
*/
|
|
|
|
function farm_log_form_alter(&$form, &$form_state, $form_id) {
|
|
|
|
|
|
|
|
// If this is the farm_movement log form...
|
|
|
|
if ($form_id == 'log_form' && $form['#bundle'] == 'farm_movement') {
|
|
|
|
|
2014-11-19 19:25:22 +01:00
|
|
|
// Alter the form using our helper function.
|
|
|
|
$asset = farm_log_form_prepopulate_asset($form, 'field_farm_asset');
|
|
|
|
|
|
|
|
// If no asset was returned, don't continue.
|
|
|
|
if (empty($asset)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the "from" field is empty...
|
|
|
|
if (empty($form['field_farm_move_from'][LANGUAGE_NONE][0]['#default_value'])) {
|
2014-11-13 22:33:33 +01:00
|
|
|
|
2014-11-19 19:25:22 +01:00
|
|
|
// Look up the asset's last location and prepopulate the "from" field.
|
|
|
|
$query = new EntityFieldQuery();
|
|
|
|
$query->entityCondition('entity_type', 'log')
|
|
|
|
->entityCondition('bundle', 'farm_movement')
|
|
|
|
->fieldCondition('field_farm_asset', 'target_id', $asset->id)
|
|
|
|
->fieldCondition('field_farm_date', 'value', REQUEST_TIME, '<=')
|
|
|
|
->fieldOrderBy('field_farm_date', 'value', 'DESC')
|
|
|
|
->propertyOrderBy('id', 'DESC')
|
|
|
|
->range(0, 1);
|
|
|
|
$result = $query->execute();
|
|
|
|
if (!empty($result['log'])) {
|
|
|
|
foreach ($result['log'] as $id => $entity) {
|
|
|
|
$log = log_load($id);
|
|
|
|
if (!empty($log->field_farm_move_to[LANGUAGE_NONE][0]['tid'])) {
|
|
|
|
$term = taxonomy_term_load($log->field_farm_move_to[LANGUAGE_NONE][0]['tid']);
|
|
|
|
if (!empty($term)) {
|
|
|
|
$form['field_farm_move_from'][LANGUAGE_NONE]['#default_value'] = check_plain($term->name);
|
2014-11-13 22:33:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-19 19:25:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-13 21:53:45 +01:00
|
|
|
|
2014-11-19 19:25:22 +01:00
|
|
|
/**
|
|
|
|
* Helper function for enabling asset prepopulation in log forms.
|
|
|
|
*
|
|
|
|
* @param $form
|
|
|
|
* The form array to modify, passed by reference.
|
|
|
|
* @param $field_name
|
|
|
|
* The machine name of the entity reference field that should be populated.
|
|
|
|
*
|
2014-11-26 22:57:29 +01:00
|
|
|
* @return FarmAsset farm_asset
|
2014-11-19 19:25:22 +01:00
|
|
|
* Returns the asset object, if found.
|
|
|
|
*/
|
|
|
|
function farm_log_form_prepopulate_asset(&$form, $field_name = 'field_farm_asset') {
|
|
|
|
$asset = NULL;
|
|
|
|
|
|
|
|
// If the "farm_asset" query parameter is set...
|
|
|
|
$params = drupal_get_query_parameters();
|
|
|
|
if (!empty($params['farm_asset'])) {
|
|
|
|
|
|
|
|
// Verify that the farm_asset is valid.
|
|
|
|
$asset = farm_asset_load($params['farm_asset']);
|
|
|
|
if ($asset) {
|
|
|
|
|
|
|
|
// Add the asset to the form.
|
|
|
|
$form['farm_asset'] = array(
|
|
|
|
'#type' => 'value',
|
|
|
|
'#value' => $asset,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Prepopulate the movement's asset reference field.
|
|
|
|
if (empty($form[$field_name][LANGUAGE_NONE][0]['target_id']['#default_value'])) {
|
|
|
|
$form[$field_name][LANGUAGE_NONE][0]['target_id']['#default_value'] = entity_label('farm_asset', $asset) . ' (' . $asset->id . ')';
|
2014-11-13 21:53:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-19 19:25:22 +01:00
|
|
|
|
|
|
|
return $asset;
|
2014-11-13 21:53:13 +01:00
|
|
|
}
|