mirror of
https://github.com/farmOS/farmOS.git
synced 2024-02-23 11:37:38 +01:00
247 lines
6.2 KiB
Text
247 lines
6.2 KiB
Text
<?php
|
|
/**
|
|
* @file
|
|
* Code for the Farm Log feature.
|
|
*/
|
|
|
|
// Include Features code.
|
|
include_once 'farm_log.features.inc';
|
|
|
|
/**
|
|
* Implements hook_form_alter().
|
|
*/
|
|
function farm_log_form_alter(&$form, &$form_state, $form_id) {
|
|
|
|
// If this is a log form, attempt to prepopulate name and reference fields.
|
|
if ($form_id == 'log_form') {
|
|
farm_log_prepopulate_log_form_name($form);
|
|
farm_log_prepopulate_log_form_references($form);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implements hook_entity_presave().
|
|
*/
|
|
function farm_log_entity_presave($entity, $type) {
|
|
|
|
// When a log entity is being saved, populate the geometry field from areas.
|
|
if ($type == 'log') {
|
|
farm_log_populate_geometry($entity);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
|
|
// Translate the category name.
|
|
$term_name = t($category);
|
|
|
|
// Create the new term.
|
|
$term = new stdClass();
|
|
$term->name = $term_name;
|
|
$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);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Helper function for populating a log's geometry from an area reference field.
|
|
*
|
|
* @param Entity $entity
|
|
* The entity to act upon.
|
|
*
|
|
* @see farm_log_entity_movement_presave().
|
|
*/
|
|
function farm_log_populate_geometry($entity) {
|
|
|
|
// Define the area field name.
|
|
$area_field = 'field_farm_area';
|
|
|
|
// 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 the name field in log forms.
|
|
*
|
|
* @param array $form
|
|
* The form array to modify, passed by reference.
|
|
*/
|
|
function farm_log_prepopulate_log_form_name(&$form) {
|
|
|
|
// If the GET parameter isn't set, bail.
|
|
$params = drupal_get_query_parameters();
|
|
if (empty($params['name'])) {
|
|
return;
|
|
}
|
|
|
|
// If the log form name field already has a default value, bail.
|
|
if (!empty($form['name']['#default_value'])) {
|
|
return;
|
|
}
|
|
|
|
// Set the name in the form.
|
|
$form['name']['#default_value'] = $params['name'];
|
|
}
|
|
|
|
/**
|
|
* Helper function for populating entity reference fields in log forms.
|
|
*
|
|
* @param array $form
|
|
* The form array to modify, passed by reference.
|
|
*/
|
|
function farm_log_prepopulate_log_form_references(&$form) {
|
|
|
|
// Define the fields we will be populating.
|
|
$fields = array(
|
|
'field_farm_asset' => array(
|
|
'entity_type' => 'farm_asset',
|
|
'url_param' => 'farm_asset',
|
|
),
|
|
'field_farm_area' => array(
|
|
'entity_type' => 'taxonomy_term',
|
|
'url_param' => 'farm_area',
|
|
),
|
|
'field_farm_log_category' => array(
|
|
'entity_type' => 'taxonomy_term',
|
|
'lookup' => TRUE,
|
|
'vocabulary' => 'farm_log_categories',
|
|
'url_param' => 'category',
|
|
),
|
|
'field_farm_log_owner' => array(
|
|
'entity_type' => 'user',
|
|
),
|
|
);
|
|
|
|
// Populate the fields.
|
|
foreach ($fields as $field => $info) {
|
|
|
|
// Start with an empty array of IDs.
|
|
$ids = array();
|
|
|
|
// If the field does not exist on the log, skip it.
|
|
if (!isset($form[$field])) {
|
|
continue;
|
|
}
|
|
|
|
// If a URL param is available, get a list of entity IDs from it.
|
|
if (!empty($info['url_param'])) {
|
|
|
|
// Get query parameters.
|
|
$params = drupal_get_query_parameters();
|
|
|
|
// If the URL param is set, pull the IDs out.
|
|
if (!empty($params[$info['url_param']])) {
|
|
$ids = $params[$info['url_param']];
|
|
}
|
|
}
|
|
|
|
// Or, if the entity type is 'user', load the ID from the current user.
|
|
elseif ($info['entity_type'] == 'user') {
|
|
global $user;
|
|
if (!empty($user->uid)) {
|
|
$ids[] = $user->uid;
|
|
}
|
|
}
|
|
|
|
// Ensure that the IDs are an array.
|
|
if (!is_array($ids)) {
|
|
$ids = array($ids);
|
|
}
|
|
|
|
// If there are no IDs, skip.
|
|
if (empty($ids)) {
|
|
continue;
|
|
}
|
|
|
|
// Look up taxonomy term IDs, if necessary.
|
|
if ($info['entity_type'] == 'taxonomy_term' && !empty($info['lookup']) && !empty($info['vocabulary'])) {
|
|
$term_names = $ids;
|
|
$ids = array();
|
|
foreach ($term_names as $name) {
|
|
$terms = taxonomy_get_term_by_name($name, $info['vocabulary']);
|
|
$term = reset($terms);
|
|
if (!empty($term->tid)) {
|
|
$ids[] = $term->tid;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Prepopulate with the farm_fields helper function.
|
|
farm_fields_prepopulate_entityreference($form, $info['entity_type'], $field, $ids);
|
|
}
|
|
}
|