2014-02-04 09:41:17 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Code for the Farm Log feature.
|
|
|
|
*/
|
|
|
|
|
2015-11-28 19:02:44 +01:00
|
|
|
// Include Features code.
|
2014-02-04 09:41:17 +01:00
|
|
|
include_once 'farm_log.features.inc';
|
2014-11-13 21:53:13 +01:00
|
|
|
|
2014-12-02 19:07:26 +01:00
|
|
|
/**
|
|
|
|
* 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.'),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-06-08 21:12:47 +02:00
|
|
|
/**
|
|
|
|
* Implements hook_farm_access_perms().
|
|
|
|
*/
|
|
|
|
function farm_log_farm_access_perms($role) {
|
|
|
|
$perms = array();
|
|
|
|
|
|
|
|
// Grant access to view farm logs.
|
|
|
|
$perms[] = 'view farm logs';
|
|
|
|
|
|
|
|
return $perms;
|
|
|
|
}
|
|
|
|
|
2014-11-26 22:57:00 +01:00
|
|
|
/**
|
2015-03-13 01:02:09 +01:00
|
|
|
* Implements hook_farm_admin_actions().
|
2014-11-26 22:57:00 +01:00
|
|
|
*/
|
2015-03-13 01:02:09 +01:00
|
|
|
function farm_log_farm_admin_actions() {
|
2014-11-26 22:57:00 +01:00
|
|
|
|
2015-03-13 01:02:09 +01:00
|
|
|
// Define farm area actions.
|
2014-11-26 22:57:00 +01:00
|
|
|
$actions = array(
|
2016-02-01 01:28:27 +01:00
|
|
|
'log' => array(
|
|
|
|
'title' => t('Add a log'),
|
|
|
|
'href' => 'log/add',
|
|
|
|
'paths' => array(
|
|
|
|
'farm',
|
2016-03-09 13:24:00 +01:00
|
|
|
'farm/logs',
|
2016-02-01 01:28:27 +01:00
|
|
|
),
|
2016-02-01 18:45:04 +01:00
|
|
|
'weight' => 1,
|
2016-02-01 01:28:27 +01:00
|
|
|
),
|
2014-11-26 22:57:00 +01:00
|
|
|
);
|
2015-03-13 01:02:09 +01:00
|
|
|
return $actions;
|
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) {
|
|
|
|
|
2015-04-09 04:49:34 +02:00
|
|
|
// If this is a log form...
|
|
|
|
if ($form_id == 'log_form') {
|
2014-11-13 21:53:13 +01:00
|
|
|
|
2015-04-09 04:49:34 +02:00
|
|
|
// If there is an asset(s) reference field.
|
|
|
|
if (!empty($form['field_farm_asset'])) {
|
2015-01-03 21:43:57 +01:00
|
|
|
|
2015-04-09 04:49:34 +02:00
|
|
|
// Alter the form using our helper function.
|
2016-06-08 19:28:51 +02:00
|
|
|
farm_log_form_prepopulate_asset($form, 'field_farm_asset');
|
2015-04-09 04:49:34 +02:00
|
|
|
}
|
2014-11-19 19:25:22 +01:00
|
|
|
|
2015-04-09 04:49:34 +02:00
|
|
|
// If there is an area(s) reference field...
|
|
|
|
if (!empty($form['field_farm_area'])) {
|
2015-01-03 21:43:57 +01:00
|
|
|
|
2015-04-09 04:49:34 +02:00
|
|
|
// Alter the form with the farm_log helper function.
|
|
|
|
farm_log_form_prepopulate_area($form, 'field_farm_area');
|
2014-11-19 19:25:22 +01:00
|
|
|
}
|
|
|
|
}
|
2015-05-27 17:53:19 +02:00
|
|
|
}
|
|
|
|
|
2017-04-27 16:21:09 +02:00
|
|
|
/**
|
|
|
|
* Implements hook_entityreference_view_widget_views_arguments_alter().
|
|
|
|
*/
|
|
|
|
function farm_crop_entityreference_view_widget_views_arguments_alter(&$arguments, $form_state, $view) {
|
|
|
|
|
|
|
|
// This module provides a View that is used for searching and selecting assets
|
|
|
|
// for the field_farm_asset entity reference field, using the Entity
|
|
|
|
// Reference View Widget module.
|
|
|
|
//
|
|
|
|
// In some cases, we want to limit what shows up in the View of assets, based
|
|
|
|
// on the log type that the field is on. For instance, a seeding should only
|
|
|
|
// be able to reference planting assets, but it still uses the general
|
|
|
|
// field_farm_asset field, which technically can reference any asset type.
|
|
|
|
//
|
|
|
|
// The Entity Reference View Widget module provides this hook to allow the
|
|
|
|
// View arguments to be altered. We implement this hook, and invoke our own
|
|
|
|
// custom hook to allow farmOS modules to filter the View results to a
|
|
|
|
// specific entity type, given the current log type.
|
|
|
|
//
|
|
|
|
// The View that we use is farm_asset_entityreference_view, which has two
|
|
|
|
// contextual arguments: asset id (to filter out already-selected assets), and
|
|
|
|
// asset type. The code below only tries to set the asset type argument, and
|
|
|
|
// maintains any asset id argument that is already set.
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @todo
|
|
|
|
* Note that this does NOT alter the "Asset type" exposed filter. So users
|
|
|
|
* will still see that filter, and it will still default to "- Any -". If
|
|
|
|
* the contextual filter is activated here, however, then selecting any other
|
|
|
|
* asset type with the exposed filter will return an empty result set. That's
|
|
|
|
* confusing, so perhaps we should find a way to hide that exposed filter if
|
|
|
|
* this code successfully sets a contextual filter argument.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Figure out what the log type is.
|
|
|
|
if (!empty($form_state['build_info']['args'][0]->type)) {
|
|
|
|
$log_type = $form_state['build_info']['args'][0]->type;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If that failed, bail.
|
|
|
|
else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ask modules if we should limit the asset type.
|
|
|
|
$limit_types = module_invoke_all('farm_log_allowed_asset_reference_type', $log_type);
|
|
|
|
|
|
|
|
// The asset type argument only supports a single value, so we're just going
|
|
|
|
// to take the first one.
|
|
|
|
if (!empty($limit_types[0])) {
|
|
|
|
$type = $limit_types[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
// If a type was not found, bail.
|
|
|
|
if (empty($type)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to ensure that the first argument is reserved for asset id(s). So
|
|
|
|
// if it's not set, set it to 'all'.
|
|
|
|
if (empty($arguments)) {
|
|
|
|
$arguments[0] = 'all';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the asset type as the second argument.
|
|
|
|
$arguments[1] = $type;
|
|
|
|
}
|
|
|
|
|
2016-06-08 21:01:48 +02:00
|
|
|
/**
|
|
|
|
* Implements hook_entity_presave().
|
|
|
|
*/
|
|
|
|
function farm_log_entity_presave($entity, $type) {
|
|
|
|
|
|
|
|
// When a log is saved, populate the Geometry field if available.
|
|
|
|
if ($type == 'log') {
|
|
|
|
|
|
|
|
// In the case of movement logs, the area reference field name is
|
|
|
|
// 'field_farm_move_to'.
|
|
|
|
/**
|
|
|
|
* @todo
|
|
|
|
* Migrate field_farm_move_to to field_farm_area? Currently farm_log depends
|
|
|
|
* on farm_log_movement, but if that needs to change in the future this will
|
|
|
|
* also need to change.
|
|
|
|
*/
|
|
|
|
$area_field = 'field_farm_area';
|
|
|
|
if ($entity->type == 'farm_movement') {
|
|
|
|
$area_field = 'field_farm_move_to';
|
|
|
|
}
|
|
|
|
farm_log_geometry_populate($entity, $area_field);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Populate the geometry of logs from the area(s) they reference, if no
|
|
|
|
* geometry is already defined.
|
|
|
|
*
|
|
|
|
* @param Log $log
|
|
|
|
* The log entity.
|
|
|
|
* @param string $area_field
|
|
|
|
* Optionally, the machine name of the area reference field to look for.
|
|
|
|
* Defaults to 'field_farm_area'.
|
|
|
|
*/
|
|
|
|
function farm_log_geometry_populate(&$log, $area_field = 'field_farm_area') {
|
|
|
|
|
2016-10-22 18:15:37 +02:00
|
|
|
// If the log doesn't have an area reference field, bail.
|
|
|
|
if (!isset($log->{$area_field})) {
|
2016-06-08 21:01:48 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If a geometry is already defined, bail.
|
|
|
|
if (!empty($log->field_farm_geofield[LANGUAGE_NONE][0]['geom'])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-10 16:26:16 +02:00
|
|
|
// Load the area(s) referenced by the area reference field.
|
2016-06-08 21:01:48 +02:00
|
|
|
$areas = array();
|
|
|
|
if (!empty($log->{$area_field}[LANGUAGE_NONE])) {
|
|
|
|
foreach ($log->{$area_field}[LANGUAGE_NONE] as $area_reference) {
|
|
|
|
if (!empty($area_reference['tid'])) {
|
|
|
|
$areas[] = taxonomy_term_load($area_reference['tid']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If no areas are referenced, bail.
|
|
|
|
if (empty($areas)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate over the areas to find geometries.
|
|
|
|
$geoms = array();
|
|
|
|
foreach ($areas as $area) {
|
|
|
|
if (!empty($area->field_farm_geofield[LANGUAGE_NONE][0]['geom'])) {
|
|
|
|
$geoms[] = $area->field_farm_geofield[LANGUAGE_NONE][0]['geom'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-10 15:03:11 +02:00
|
|
|
// Populate the geofield.
|
|
|
|
farm_map_geofield_populate($log, $geoms);
|
2016-06-08 21:01:48 +02:00
|
|
|
}
|
|
|
|
|
2014-11-19 19:25:22 +01:00
|
|
|
/**
|
|
|
|
* Helper function for enabling asset prepopulation in log forms.
|
|
|
|
*
|
2015-04-13 22:49:57 +02:00
|
|
|
* @param array $form
|
2014-11-19 19:25:22 +01:00
|
|
|
* The form array to modify, passed by reference.
|
2015-04-13 22:49:57 +02:00
|
|
|
* @param string $field_name
|
2014-11-19 19:25:22 +01:00
|
|
|
* The machine name of the entity reference field that should be populated.
|
|
|
|
*
|
2016-07-13 19:22:48 +02:00
|
|
|
* @return array|bool
|
2015-10-02 02:51:40 +02:00
|
|
|
* Returns the asset objects in an array, if found, FALSE otherwise.
|
2014-11-19 19:25:22 +01:00
|
|
|
*/
|
2015-09-11 17:30:03 +02:00
|
|
|
function farm_log_form_prepopulate_asset(array &$form, $field_name = 'field_farm_asset') {
|
2015-09-11 21:01:25 +02:00
|
|
|
|
2016-07-13 19:22:48 +02:00
|
|
|
// Load assets from the ?farm_asset query parameter.
|
|
|
|
$assets = farm_asset_load_assets_from_url();
|
2015-10-02 02:51:40 +02:00
|
|
|
|
|
|
|
// If there are no assets, bail.
|
|
|
|
if (empty($assets)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
2014-11-19 19:25:22 +01:00
|
|
|
|
2015-09-11 17:30:03 +02:00
|
|
|
// Load the field instance definition.
|
|
|
|
$entity_type = $form['#entity_type'];
|
|
|
|
$bundle = $form['#bundle'];
|
|
|
|
$field_instance = field_info_instance($entity_type, $field_name, $bundle);
|
|
|
|
|
2015-10-02 02:51:40 +02:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
}
|
2015-04-08 21:49:07 +02:00
|
|
|
}
|
|
|
|
|
2015-10-02 02:51:40 +02:00
|
|
|
// 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),
|
|
|
|
);
|
2014-11-13 21:53:13 +01:00
|
|
|
}
|
|
|
|
}
|
2014-11-19 19:25:22 +01:00
|
|
|
|
2015-10-02 02:51:40 +02:00
|
|
|
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;
|
|
|
|
|
2015-04-08 21:49:07 +02:00
|
|
|
// 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.
|
2015-04-08 21:49:07 +02:00
|
|
|
if (empty($field_value)) {
|
2016-10-26 20:29:18 +02:00
|
|
|
$field_value = $area->tid;
|
2014-12-01 03:49:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $area;
|
2014-12-22 23:33:34 +01:00
|
|
|
}
|