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
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
2017-06-12 21:58:57 +02:00
|
|
|
// If there is an asset(s) reference field, try to prepopulate it.
|
2015-04-09 04:49:34 +02:00
|
|
|
if (!empty($form['field_farm_asset'])) {
|
2017-04-27 16:26:36 +02:00
|
|
|
farm_log_form_prepopulate_asset($form);
|
2015-04-09 04:49:34 +02:00
|
|
|
}
|
2014-11-19 19:25:22 +01:00
|
|
|
|
2017-06-12 21:58:57 +02:00
|
|
|
// If there is an area(s) reference field, try to prepopulate it.
|
2015-04-09 04:49:34 +02:00
|
|
|
if (!empty($form['field_farm_area'])) {
|
2017-04-27 16:26:36 +02:00
|
|
|
farm_log_form_prepopulate_area($form);
|
2014-11-19 19:25:22 +01:00
|
|
|
}
|
2017-06-12 21:59:09 +02:00
|
|
|
|
|
|
|
// If there is an owner reference field, try to prepopulate it.
|
|
|
|
if (!empty($form['field_farm_log_owner'])) {
|
|
|
|
farm_log_form_prepopulate_owner($form);
|
|
|
|
}
|
2014-11-19 19:25:22 +01:00
|
|
|
}
|
2015-05-27 17:53:19 +02:00
|
|
|
}
|
|
|
|
|
2016-06-08 21:01:48 +02:00
|
|
|
/**
|
|
|
|
* Implements hook_entity_presave().
|
|
|
|
*/
|
|
|
|
function farm_log_entity_presave($entity, $type) {
|
|
|
|
|
2017-05-10 15:43:01 +02:00
|
|
|
// When a log entity is being saved, populate the geometry field from areas.
|
2016-06-08 21:01:48 +02:00
|
|
|
if ($type == 'log') {
|
2017-05-10 15:43:01 +02:00
|
|
|
farm_log_populate_geometry($entity);
|
2016-06-08 21:01:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-30 20:29:14 +02:00
|
|
|
/**
|
|
|
|
* Helper function for creating log categories. Terms will only be added if
|
|
|
|
* they don't already exist.
|
|
|
|
*
|
|
|
|
* @param array $categories
|
|
|
|
* An array of strings that will be added as terms to the Farm Log Categories
|
|
|
|
* vocabulary.
|
|
|
|
*/
|
|
|
|
function farm_log_categories_create($categories) {
|
|
|
|
|
|
|
|
// If the categories is not an array, bail.
|
|
|
|
if (!is_array($categories)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Define the vocabulary machine name.
|
|
|
|
$vocabulary_machine_name = 'farm_log_categories';
|
|
|
|
|
|
|
|
// Load the vocabulary.
|
|
|
|
$vocabulary = taxonomy_vocabulary_machine_name_load($vocabulary_machine_name);
|
|
|
|
|
|
|
|
// If the vocabulary doesn't exist, bail.
|
|
|
|
if (empty($vocabulary->vid)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate through the categories.
|
|
|
|
foreach ($categories as $category) {
|
|
|
|
|
|
|
|
// First, check to see if the term already exists. If it does, skip it.
|
|
|
|
$terms = taxonomy_get_term_by_name($category, $vocabulary_machine_name);
|
|
|
|
if (!empty($terms)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-05-30 20:45:38 +02:00
|
|
|
// Translate the category name.
|
|
|
|
$term_name = t($category);
|
|
|
|
|
2017-05-30 20:29:14 +02:00
|
|
|
// Create the new term.
|
|
|
|
$term = new stdClass();
|
2017-05-30 20:45:38 +02:00
|
|
|
$term->name = $term_name;
|
2017-05-30 20:29:14 +02:00
|
|
|
$term->vid = $vocabulary->vid;
|
|
|
|
taxonomy_term_save($term);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Always reset the categories to alphabetical order.
|
|
|
|
/**
|
|
|
|
* @see taxonomy_vocabulary_confirm_reset_alphabetical_submit()
|
|
|
|
*/
|
|
|
|
db_update('taxonomy_term_data')
|
|
|
|
->fields(array('weight' => 0))
|
|
|
|
->condition('vid', $vocabulary->vid)
|
|
|
|
->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create log categories on behalf of all modules that provide them.
|
|
|
|
*/
|
|
|
|
function farm_log_categories_create_all() {
|
|
|
|
$categories = module_invoke_all('farm_log_categories');
|
|
|
|
if (!empty($categories)) {
|
|
|
|
farm_log_categories_create($categories);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-08 21:01:48 +02:00
|
|
|
/**
|
2017-05-10 15:43:01 +02:00
|
|
|
* Helper function for populating a log's geometry from an area reference field.
|
2016-06-08 21:01:48 +02:00
|
|
|
*
|
2017-05-10 15:43:01 +02:00
|
|
|
* @param Entity $entity
|
|
|
|
* The entity to act upon.
|
2017-04-27 16:24:02 +02:00
|
|
|
*
|
|
|
|
* @see farm_log_entity_movement_presave().
|
2016-06-08 21:01:48 +02:00
|
|
|
*/
|
2017-05-10 15:43:01 +02:00
|
|
|
function farm_log_populate_geometry($entity) {
|
|
|
|
|
|
|
|
// Define the area field name.
|
|
|
|
$area_field = 'field_farm_area';
|
2016-06-08 21:01:48 +02:00
|
|
|
|
2016-10-22 18:15:37 +02:00
|
|
|
// If the log doesn't have an area reference field, bail.
|
2017-05-10 15:43:01 +02:00
|
|
|
if (!isset($entity->{$area_field})) {
|
2016-06-08 21:01:48 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If a geometry is already defined, bail.
|
2017-05-10 15:43:01 +02:00
|
|
|
if (!empty($entity->field_farm_geofield[LANGUAGE_NONE][0]['geom'])) {
|
2016-06-08 21:01:48 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-10 16:26:16 +02:00
|
|
|
// Load the area(s) referenced by the area reference field.
|
2017-05-10 15:34:09 +02:00
|
|
|
$area_ids = array();
|
2017-05-10 15:43:01 +02:00
|
|
|
if (!empty($entity->{$area_field}[LANGUAGE_NONE])) {
|
|
|
|
foreach ($entity->{$area_field}[LANGUAGE_NONE] as $area_reference) {
|
2016-06-08 21:01:48 +02:00
|
|
|
if (!empty($area_reference['tid'])) {
|
2017-05-10 15:34:09 +02:00
|
|
|
$area_ids[] = $area_reference['tid'];
|
2016-06-08 21:01:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-10 15:34:09 +02:00
|
|
|
// Extract geometries from the areas.
|
|
|
|
$geoms = farm_area_extract_geoms($area_ids);
|
2016-06-08 21:01:48 +02:00
|
|
|
|
2017-05-10 15:03:11 +02:00
|
|
|
// Populate the geofield.
|
2017-05-10 15:43:01 +02:00
|
|
|
farm_map_geofield_populate($entity, $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.
|
|
|
|
*/
|
2017-07-10 16:22:48 +02:00
|
|
|
function farm_log_form_prepopulate_asset(&$form) {
|
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)) {
|
2017-07-10 16:22:48 +02:00
|
|
|
return;
|
2015-10-02 02:51:40 +02:00
|
|
|
}
|
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'];
|
2017-04-27 16:26:36 +02:00
|
|
|
$field_instance = field_info_instance($entity_type, 'field_farm_asset', $bundle);
|
2015-09-11 17:30:03 +02:00
|
|
|
|
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.
|
2017-04-27 16:26:36 +02:00
|
|
|
if (empty($form['field_farm_asset'][LANGUAGE_NONE]['#default_value'])) {
|
|
|
|
$form['field_farm_asset'][LANGUAGE_NONE]['#default_value'] = $asset_ids;
|
2015-10-02 02:51:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2017-04-27 16:26:36 +02:00
|
|
|
if (!empty($form['field_farm_asset'][LANGUAGE_NONE][$key]['target_id']['#default_value'])) {
|
2015-10-02 02:51:40 +02:00
|
|
|
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.
|
2017-04-27 16:26:36 +02:00
|
|
|
$form['field_farm_asset'][LANGUAGE_NONE][$key] = $form['field_farm_asset'][LANGUAGE_NONE][0];
|
2015-10-02 02:51:40 +02:00
|
|
|
|
|
|
|
// Set the default, delta, and weight values.
|
2017-04-27 16:26:36 +02:00
|
|
|
$form['field_farm_asset'][LANGUAGE_NONE][$key]['target_id']['#default_value'] = $label;
|
|
|
|
$form['field_farm_asset'][LANGUAGE_NONE][$key]['target_id']['#delta'] = $key;
|
|
|
|
$form['field_farm_asset'][LANGUAGE_NONE][$key]['target_id']['#weight'] = $key;
|
2015-10-02 02:51:40 +02:00
|
|
|
|
|
|
|
// Only make the first one required.
|
|
|
|
if ($key > 0) {
|
2017-04-27 16:26:36 +02:00
|
|
|
$form['field_farm_asset'][LANGUAGE_NONE][$key]['target_id']['#required'] = 0;
|
2015-10-02 02:51:40 +02:00
|
|
|
}
|
|
|
|
|
2017-04-27 16:26:36 +02:00
|
|
|
$form['field_farm_asset'][LANGUAGE_NONE]['#max_delta'] = $key;
|
|
|
|
$form['field_farm_asset'][LANGUAGE_NONE][$key]['_weight']['#delta'] = $key;
|
|
|
|
$form['field_farm_asset'][LANGUAGE_NONE][$key]['_weight']['#default_value'] = $key;
|
2015-10-02 02:51:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// For "autocomplete tags", implode them all into one comma-separated list.
|
|
|
|
elseif ($field_instance['widget']['type'] == 'entityreference_autocomplete_tags') {
|
2017-04-27 16:26:36 +02:00
|
|
|
if (empty($form['field_farm_asset'][LANGUAGE_NONE]['#default_value'])) {
|
|
|
|
$form['field_farm_asset'][LANGUAGE_NONE]['#default_value'] = implode(', ', $asset_labels);
|
2015-10-02 02:51:40 +02:00
|
|
|
}
|
|
|
|
}
|
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.
|
2017-04-27 16:26:36 +02:00
|
|
|
if (!empty($form['field_farm_asset'][LANGUAGE_NONE][$key]['target_id'])) {
|
2015-10-02 02:51:40 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the checkbox element.
|
2017-04-27 16:26:36 +02:00
|
|
|
$form['field_farm_asset'][LANGUAGE_NONE][$key]['target_id'] = array(
|
2015-10-02 02:51:40 +02:00
|
|
|
'#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-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.
|
|
|
|
*/
|
2017-07-10 16:22:48 +02:00
|
|
|
function farm_log_form_prepopulate_area(&$form) {
|
2014-12-01 03:49:46 +01:00
|
|
|
|
2015-04-08 21:49:07 +02:00
|
|
|
// Alias for the field's default value.
|
2017-04-27 16:26:36 +02:00
|
|
|
$field_value = &$form['field_farm_area'][LANGUAGE_NONE]['#default_value'];
|
2015-04-08 21:49:07 +02:00
|
|
|
|
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) {
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-22 23:33:34 +01:00
|
|
|
}
|
2017-06-12 21:59:09 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function for enabling owner prepopulation in log forms.
|
|
|
|
*
|
|
|
|
* @param array $form
|
|
|
|
* The form array to modify, passed by reference.
|
|
|
|
*/
|
2017-07-10 16:22:48 +02:00
|
|
|
function farm_log_form_prepopulate_owner(&$form) {
|
2017-06-12 21:59:09 +02:00
|
|
|
|
|
|
|
// Alias for the field's default value.
|
|
|
|
$field_value = &$form['field_farm_log_owner'][LANGUAGE_NONE]['#default_value'];
|
|
|
|
|
|
|
|
// Only proceed if the field is empty.
|
|
|
|
if (!empty($field_value)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the current authenticated user.
|
|
|
|
global $user;
|
|
|
|
|
|
|
|
// If the user id is empty for whatever reason, bail.
|
|
|
|
if (empty($user->uid)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the user's id to the list of log owners.
|
|
|
|
$field_value = array($user->uid);
|
|
|
|
}
|