3
0
Fork 0
mirror of https://github.com/farmOS/farmOS.git synced 2024-02-23 11:37:38 +01:00
farmOS/farm_log.module

453 lines
12 KiB
Text
Raw Normal View History

<?php
/**
* @file
* Code for the Farm Log feature.
*/
// Include Features code.
include_once 'farm_log.features.inc';
// Include Asset Location code.
include_once 'farm_log.location.inc';
/**
* Implements hook_permission().
*/
function farm_log_permission() {
return array(
'view farm logs' => array(
'title' => t('View farm logs'),
'description' => t('View all farm-related log items.'),
),
);
}
2015-09-08 21:15:55 +02:00
/**
* Implements hook_farm_access_perms().
2015-09-08 21:15:55 +02:00
*/
function farm_log_farm_access_perms($role) {
2015-09-08 21:15:55 +02:00
// Assemble a list of log types and taxonomies provided by this module.
2015-09-08 21:15:55 +02:00
$types = array(
'log' => array(
'farm_activity',
'farm_movement',
'farm_observation',
),
'taxonomy' => array(
'farm_observation_types',
'farm_priority',
'farm_quantity_units',
),
);
// Grant different CRUD permissions based on the role.
$perms = array();
switch ($role) {
// Farm Manager and Worker
case 'Farm Manager':
case 'Farm Worker':
$perms = farm_access_entity_perms($types);
break;
// Farm Viewer
case 'Farm Viewer':
$perms = farm_access_entity_perms($types, array('view'));
break;
}
2015-09-08 21:15:55 +02:00
// Add 'view farm logs' permission.
$perms[] = 'view farm logs';
return $perms;
}
/**
* Implements hook_farm_admin_actions().
*/
function farm_log_farm_admin_actions() {
// Define farm area actions.
$actions = array(
'log' => array(
'title' => t('Add a log'),
'href' => 'log/add',
'paths' => array(
'farm',
),
'weight' => 1,
),
2014-11-29 01:45:46 +01:00
'activity' => array(
'title' => t('Add an activity'),
2014-11-29 01:45:46 +01:00
'href' => 'log/add/farm_activity',
'assets' => array(
'all',
),
'views' => array(
'farm_log_activity',
),
'paths' => array(
'taxonomy/term/%',
),
2014-11-29 01:45:46 +01:00
),
'movement' => array(
'title' => t('Add a movement'),
'href' => 'log/add/farm_movement',
'assets' => array(
'all',
),
'views' => array(
'farm_log_movement',
),
'paths' => array(
'taxonomy/term/%',
),
),
'observation' => array(
'title' => t('Add an observation'),
'href' => 'log/add/farm_observation',
'assets' => array(
'all',
),
'views' => array(
'farm_log_observation',
),
'paths' => array(
2015-05-27 22:31:33 +02:00
'taxonomy/term/%',
),
),
);
return $actions;
}
/**
* Implements hook_farm_taxonomy_term_view_views().
*/
function farm_log_farm_taxonomy_term_view_views($term) {
// If the term is not an area, bail.
if ($term->vocabulary_machine_name != 'farm_areas') {
2015-04-13 22:49:57 +02:00
return array();
}
// Return a list of Views to include on Areas.
return array(
2015-04-13 22:49:57 +02:00
// Activities in this area.
array(
'name' => 'farm_log_activity',
'arg' => 2,
),
2015-05-27 22:31:33 +02:00
// Observations in this area.
array(
'name' => 'farm_log_observation',
'arg' => 2,
),
2015-04-13 22:49:57 +02:00
// Area asset history (at the bottom).
array(
'name' => 'farm_area_assets',
'weight' => 100,
),
);
}
/**
* Implements hook_farm_area_links().
*/
function farm_log_farm_area_links($id) {
$links = array();
// Add link to activities.
$view = views_get_view('farm_log_activity');
$view->preview('default', array($id));
if ($view->total_rows > 0) {
$links[] = array(
'title' => t('Activities') . ': ' . $view->total_rows,
2016-03-07 22:17:05 +01:00
'href' => 'farm/logs/activities/all/' . $id,
'weight' => -100,
);
}
// Add link to observations.
$view = views_get_view('farm_log_observation');
$view->preview('default', array($id));
if ($view->total_rows > 0) {
$links[] = array(
'title' => t('Observations') . ': ' . $view->total_rows,
2016-03-07 22:17:05 +01:00
'href' => 'farm/logs/observations/all/' . $id,
2015-05-27 22:31:33 +02:00
'weight' => -90,
);
}
return $links;
}
/**
* Implements hook_entity_presave().
*/
function farm_log_entity_presave($entity, $type) {
// When a movement log is saved, populate the Geometry field.
if ($type == 'log' && $entity->type == 'farm_movement') {
farm_log_movement_geometry_populate($entity);
}
}
/**
2015-04-13 22:49:57 +02:00
* Implements hook_entity_view_alter().
*/
function farm_log_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_asset_location_markup($build['#entity']);
// Add it to the build array.
$build['location'] = array(
'#markup' => $output,
'#weight' => -100,
);
}
/**
* Implements hook_form_alter().
*/
function farm_log_form_alter(&$form, &$form_state, $form_id) {
// If this is a log form...
if ($form_id == 'log_form') {
// If there is an asset(s) reference field.
if (!empty($form['field_farm_asset'])) {
// Alter the form using our helper function.
// ($assets are used below for movement logs.)
$assets = farm_log_form_prepopulate_asset($form, 'field_farm_asset');
}
// If there is an area(s) reference field...
if (!empty($form['field_farm_area'])) {
// Alter the form with the farm_log helper function.
farm_log_form_prepopulate_area($form, 'field_farm_area');
}
// If this is a farm_movement...
if ($form['#bundle'] == 'farm_movement') {
// If the "to" field is empty...
if (empty($form['field_farm_move_to'][LANGUAGE_NONE][0]['#default_value'])) {
// Alter the form with the farm_log helper function.
farm_log_form_prepopulate_area($form, 'field_farm_move_to');
}
// If the "from" field is empty, and assets are available...
if (empty($form['field_farm_move_from'][LANGUAGE_NONE][0]['#default_value']) && !empty($assets)) {
// Look up the asset's last location and prepopulate the "from" field.
farm_log_prepopulate_movement_from($form['field_farm_move_from'], $assets, TRUE);
}
}
// Or, if this is a farm_observation...
elseif ($form['#bundle'] == 'farm_observation') {
// If the observation type field is empty, prepopulate it with "General".
if (empty($form['field_farm_observation_type'][LANGUAGE_NONE]['#default_value'])) {
$form['field_farm_observation_type'][LANGUAGE_NONE]['#default_value'] = t('General');
}
}
}
}
/**
* Helper function for enabling asset prepopulation in log forms.
*
2015-04-13 22:49:57 +02:00
* @param array $form
* The form array to modify, passed by reference.
2015-04-13 22:49:57 +02:00
* @param string $field_name
* The machine name of the entity reference field that should be populated.
*
* @return array|bool farm_asset
* Returns the asset objects in an array, if found, FALSE otherwise.
*/
function farm_log_form_prepopulate_asset(array &$form, $field_name = 'field_farm_asset') {
// If the "farm_asset" GET parameter isn't set, bail.
$params = drupal_get_query_parameters();
if (empty($params['farm_asset'])) {
return FALSE;
}
// If only a single asset id is passed, convert it to an array.
if (!is_array($params['farm_asset'])) {
$params['farm_asset'] = array($params['farm_asset']);
}
// Validate that all the asset IDs are valid by loading the assets themselves.
$assets = array();
foreach($params['farm_asset'] as $asset_id) {
// Attempt to load the asset.
$asset = farm_asset_load($asset_id);
// If it loaded, add it to the array.
if (!empty($asset)) {
$assets[] = $asset;
}
}
// If there are no assets, bail.
if (empty($assets)) {
return FALSE;
}
// Load the field instance definition.
$entity_type = $form['#entity_type'];
$bundle = $form['#bundle'];
$field_instance = field_info_instance($entity_type, $field_name, $bundle);
// If the widget type is "radios/checkboxes" or "select list"...
if (in_array($field_instance['widget']['type'], array('options_buttons', 'options_select'))) {
// Build a list of asset ID.
$asset_ids = array();
foreach ($assets as $asset) {
$asset_ids[] = $asset->id;
}
// Use the array of asset IDs as the field's default value.
if (empty($form[$field_name][LANGUAGE_NONE]['#default_value'])) {
$form[$field_name][LANGUAGE_NONE]['#default_value'] = $asset_ids;
}
}
// If the widget type is "autocomplete" or "autocomplete tags"...
elseif (in_array($field_instance['widget']['type'], array('entityreference_autocomplete', 'entityreference_autocomplete_tags'))) {
// Build a list of asset labels in the format that the widget expects.
$asset_labels = array();
foreach ($assets as $asset) {
$asset_labels[] = entity_label('farm_asset', $asset) . ' (' . $asset->id . ')';
}
// For "autocomplete", add each one as a separate field.
if ($field_instance['widget']['type'] == 'entityreference_autocomplete') {
foreach ($asset_labels as $key => $label) {
// If the item isn't empty, skip it.
if (!empty($form[$field_name][LANGUAGE_NONE][$key]['target_id']['#default_value'])) {
continue;
}
/**
* @todo
* This seems to be the easiest way to autopopulate entityreference_autocomplete
* widgets, but it is MESSY! If anyone can figure out a better way, I will buy
* you a beer.
*/
// Copy the initial array structure from the first element.
$form[$field_name][LANGUAGE_NONE][$key] = $form[$field_name][LANGUAGE_NONE][0];
// Set the default, delta, and weight values.
$form[$field_name][LANGUAGE_NONE][$key]['target_id']['#default_value'] = $label;
$form[$field_name][LANGUAGE_NONE][$key]['target_id']['#delta'] = $key;
$form[$field_name][LANGUAGE_NONE][$key]['target_id']['#weight'] = $key;
// Only make the first one required.
if ($key > 0) {
$form[$field_name][LANGUAGE_NONE][$key]['target_id']['#required'] = 0;
}
$form[$field_name][LANGUAGE_NONE]['#max_delta'] = $key;
$form[$field_name][LANGUAGE_NONE][$key]['_weight']['#delta'] = $key;
$form[$field_name][LANGUAGE_NONE][$key]['_weight']['#default_value'] = $key;
}
}
// For "autocomplete tags", implode them all into one comma-separated list.
elseif ($field_instance['widget']['type'] == 'entityreference_autocomplete_tags') {
if (empty($form[$field_name][LANGUAGE_NONE]['#default_value'])) {
$form[$field_name][LANGUAGE_NONE]['#default_value'] = implode(', ', $asset_labels);
}
}
}
// If the widget type is "entity reference view widget"...
elseif ($field_instance['widget']['type'] == 'entityreference_view_widget') {
// Add a set of checkbox form elements, as the entityreference_view_widget
// module expects...
foreach ($assets as $key => $asset) {
// If the item isn't empty, skip it.
if (!empty($form[$field_name][LANGUAGE_NONE][$key]['target_id'])) {
continue;
}
// Add the checkbox element.
$form[$field_name][LANGUAGE_NONE][$key]['target_id'] = array(
'#type' => 'checkbox',
'#return_value' => $asset->id,
'#value' => $asset->id,
'#title_display' => 'after',
'#attributes' => array(
'checked' => 'checked',
),
'#title' => entity_label('farm_asset', $asset),
);
}
}
return $assets;
2014-12-01 03:49:46 +01:00
}
/**
* Helper function for enabling area prepopulation in log forms.
*
2015-04-13 22:49:57 +02:00
* @param array $form
2014-12-01 03:49:46 +01:00
* The form array to modify, passed by reference.
2015-04-13 22:49:57 +02:00
* @param string $field_name
2014-12-01 03:49:46 +01:00
* The machine name of the term reference field that should be populated.
*
* @return TaxonomyTerm term
* Returns the taxonomy term object, if found.
*/
2015-04-13 22:49:57 +02:00
function farm_log_form_prepopulate_area(array &$form, $field_name = 'field_farm_area') {
2014-12-01 03:49:46 +01:00
$area = NULL;
// Alias for the field's default value.
$field_value = &$form[$field_name][LANGUAGE_NONE]['#default_value'];
2014-12-01 03:49:46 +01:00
// If the "farm_area" query parameter is set...
$params = drupal_get_query_parameters();
if (!empty($params['farm_area'])) {
// Verify that the farm_area is valid.
$area = taxonomy_term_load($params['farm_area']);
if ($area) {
// Add the area to the form.
$form['farm_area'] = array(
'#type' => 'value',
'#value' => $area,
);
// Prepopulate the area reference field.
if (empty($field_value)) {
$field_value = entity_label('taxonomy_term', $area);
2014-12-01 03:49:46 +01:00
}
}
}
return $area;
}