mirror of
https://github.com/farmOS/farmOS.git
synced 2024-02-23 11:37:38 +01:00
436 lines
10 KiB
Text
436 lines
10 KiB
Text
<?php
|
|
/**
|
|
* @file
|
|
* Code for the Farm Crop feature.
|
|
*/
|
|
|
|
include_once 'farm_crop.features.inc';
|
|
|
|
/**
|
|
* Implements hook_form_FORM_ID_alter().
|
|
*/
|
|
function farm_crop_form_farm_asset_form_alter(&$form, &$form_state, $form_id) {
|
|
|
|
// Get the farm asset entity from the form.
|
|
$asset = $form['farm_asset']['#value'];
|
|
|
|
// If the asset is not a planting, bail.
|
|
if (empty($asset->type) || $asset->type != 'planting') {
|
|
return;
|
|
}
|
|
|
|
// Only show the "Plan" fieldset when adding a new planting.
|
|
/**
|
|
* @todo
|
|
* Expand the form logic below so that it can also work with existing
|
|
* plantings. See https://www.drupal.org/node/2394839#comment-12141865
|
|
* for some considerations.
|
|
*/
|
|
if (!empty($asset->id)) {
|
|
return;
|
|
}
|
|
|
|
// Add a "Plan" fieldset.
|
|
$form['plan'] = array(
|
|
'#type' => 'fieldset',
|
|
'#title' => t('Plan'),
|
|
'#description' => t('Use these fields to automatically generate seeding/transplanting logs for this planting. A log will only be generated if a date is specified.'),
|
|
'#collapsible' => TRUE,
|
|
'#collapsed' => FALSE,
|
|
);
|
|
|
|
// Add fields for specifying seeding and transplanting date and location.
|
|
$date_format = 'Y-m-d';
|
|
$fields = array(
|
|
'seeding' => array(
|
|
'label' => 'Seeding',
|
|
'default_value' => NULL,
|
|
),
|
|
'transplanting' => array(
|
|
'label' => 'Transplanting',
|
|
'default_value' => NULL,
|
|
),
|
|
);
|
|
foreach ($fields as $field => $data) {
|
|
$form['plan'][$field . '_date'] = array(
|
|
'#type' => 'date_select',
|
|
'#title' => t($data['label'] . ' date'),
|
|
'#default_value' => $data['default_value'],
|
|
'#date_format' => $date_format,
|
|
'#date_label_position' => 'within',
|
|
'#date_year_range' => '-3:+10',
|
|
);
|
|
$form['plan'][$field . '_location'] = array(
|
|
'#type' => 'textfield',
|
|
'#title' => t($data['label'] . ' location'),
|
|
'#autocomplete_path' => 'taxonomy/autocomplete/field_farm_area',
|
|
);
|
|
$form['plan'][$field . '_done'] = array(
|
|
'#type' => 'checkbox',
|
|
'#title' => t($data['label'] . ' has taken place (mark the log as done)'),
|
|
);
|
|
}
|
|
|
|
// Add a submit handler.
|
|
$form['actions']['submit']['#submit'][] = 'farm_crop_planting_plan_submit';
|
|
}
|
|
|
|
/**
|
|
* Submit handler for processing the asset location field.
|
|
*
|
|
* @param array $form
|
|
* The form array.
|
|
* @param array $form_state
|
|
* The form state array.
|
|
*/
|
|
function farm_crop_planting_plan_submit(array $form, array &$form_state) {
|
|
|
|
// If an asset doesn't exist, bail.
|
|
if (empty($form_state['values']['farm_asset'])) {
|
|
return;
|
|
}
|
|
|
|
// Grab the asset.
|
|
$asset = $form_state['values']['farm_asset'];
|
|
|
|
// For each type: get the date, parse the areas, and create a log.
|
|
$types = array(
|
|
'seeding',
|
|
'transplanting',
|
|
);
|
|
foreach ($types as $type) {
|
|
|
|
// If a date is not set, skip it.
|
|
if (empty($form_state['values'][$type . '_date'])) {
|
|
continue;
|
|
}
|
|
|
|
// Convert the date to a timestamp.
|
|
$timestamp = strtotime($form_state['values'][$type . '_date']);
|
|
|
|
// If the timestamp couldn't be determined, skip.
|
|
if (empty($timestamp)) {
|
|
continue;
|
|
}
|
|
|
|
// If the location is available, load areas.
|
|
$areas = array();
|
|
if (!empty($form_state['values'][$type . '_location'])) {
|
|
$areas = farm_log_movement_parse_areas($form_state['values'][$type . '_location']);
|
|
}
|
|
|
|
// Mark the log as done or not done.
|
|
$done = FALSE;
|
|
if (!empty($form_state['values'][$type . '_done'])) {
|
|
$done = TRUE;
|
|
}
|
|
|
|
// Create a log.
|
|
farm_log_movement_create($asset, $areas, $timestamp, 'farm_' . $type, $done);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implements hook_farm_access_perms($role).
|
|
*/
|
|
function farm_crop_farm_access_perms($role) {
|
|
|
|
// Assemble a list of entity types provided by this module.
|
|
$types = array(
|
|
'farm_asset' => array(
|
|
'planting',
|
|
),
|
|
'log' => array(
|
|
'farm_seeding',
|
|
'farm_transplanting',
|
|
),
|
|
'taxonomy' => array(
|
|
'farm_crops',
|
|
'farm_crop_families',
|
|
),
|
|
);
|
|
|
|
// 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;
|
|
}
|
|
return $perms;
|
|
}
|
|
|
|
/**
|
|
* Implements hook_farm_area_type_info().
|
|
*/
|
|
function farm_crop_farm_area_type_info() {
|
|
return array(
|
|
'greenhouse' => array(
|
|
'label' => t('Greenhouse'),
|
|
'style' => 'farm_map_style_green',
|
|
'weight' => 0,
|
|
),
|
|
'bed' => array(
|
|
'label' => t('Bed'),
|
|
'style' => 'farm_map_style_green',
|
|
'zoom_levels' => array(18, 19, 20, 21, 22, 23, 24),
|
|
'weight' => -5,
|
|
),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Implements hook_farm_ui_actions().
|
|
*/
|
|
function farm_crop_farm_ui_actions() {
|
|
|
|
// Define actions.
|
|
$actions = array(
|
|
'crop' => array(
|
|
'title' => t('Add a crop/variety'),
|
|
'href' => 'admin/structure/taxonomy/farm_crops/add',
|
|
'paths' => array(
|
|
'farm/assets/plantings/crops',
|
|
),
|
|
),
|
|
'planting' => array(
|
|
'title' => t('Add a planting'),
|
|
'href' => 'farm/asset/add/planting',
|
|
'views' => array(
|
|
'farm_plantings',
|
|
),
|
|
),
|
|
'seeding' => array(
|
|
'title' => t('Add a seeding'),
|
|
'href' => 'log/add/farm_seeding',
|
|
'assets' => array(
|
|
'planting',
|
|
),
|
|
'views' => array(
|
|
'farm_log_seeding',
|
|
),
|
|
),
|
|
'transplanting' => array(
|
|
'title' => t('Add a transplanting'),
|
|
'href' => 'log/add/farm_transplanting',
|
|
'assets' => array(
|
|
'planting',
|
|
),
|
|
'views' => array(
|
|
'farm_log_transplanting',
|
|
),
|
|
),
|
|
);
|
|
return $actions;
|
|
}
|
|
|
|
/**
|
|
* Implements hook_farm_asset_breadcrumb().
|
|
*/
|
|
function farm_crop_farm_asset_breadcrumb($farm_asset) {
|
|
|
|
// If the asset is a planting, add a link to the plantings list.
|
|
$breadcrumb = array();
|
|
if ($farm_asset->type == 'planting') {
|
|
$breadcrumb[] = l(t('Assets'), 'farm/assets');
|
|
$breadcrumb[] = l(t('Plantings'), 'farm/assets/plantings');
|
|
}
|
|
return $breadcrumb;
|
|
}
|
|
|
|
/**
|
|
* Implements hook_farm_taxonomy_breadcrumb().
|
|
*/
|
|
function farm_crop_farm_taxonomy_breadcrumb($term) {
|
|
$breadcrumb = array();
|
|
|
|
// Switch through available vocabularies.
|
|
switch ($term->vocabulary_machine_name) {
|
|
|
|
// If the term is in farm_crops...
|
|
case 'farm_crops':
|
|
$breadcrumb[] = l(t('Assets'), 'farm/assets');
|
|
$breadcrumb[] = l(t('Plantings'), 'farm/assets/plantings');
|
|
$breadcrumb[] = l(t('Crops/Varieties'), 'farm/assets/plantings/crops');
|
|
break;
|
|
|
|
// If the term is in farm_crop_families...
|
|
case 'farm_crop_families':
|
|
$breadcrumb[] = l(t('Assets'), 'farm/assets');
|
|
$breadcrumb[] = l(t('Plantings'), 'farm/assets/plantings');
|
|
$breadcrumb[] = t('Crop Families');
|
|
break;
|
|
|
|
}
|
|
|
|
return $breadcrumb;
|
|
}
|
|
|
|
/**
|
|
* Implements hook_farm_asset_view_views().
|
|
*/
|
|
function farm_crop_farm_asset_view_views($farm_asset) {
|
|
|
|
// If the entity is not a planting, bail.
|
|
if ($farm_asset->type != 'planting') {
|
|
return array();
|
|
}
|
|
|
|
// Return a list of Views to include on Plantings.
|
|
return array(
|
|
array(
|
|
'name' => 'farm_log_activity',
|
|
'weight' => 0,
|
|
),
|
|
array(
|
|
'name' => 'farm_log_observation',
|
|
'weight' => 10,
|
|
),
|
|
array(
|
|
'name' => 'farm_log_seeding',
|
|
'weight' => 20,
|
|
),
|
|
array(
|
|
'name' => 'farm_log_transplanting',
|
|
'weight' => 30,
|
|
),
|
|
array(
|
|
'name' => 'farm_log_input',
|
|
'weight' => 50,
|
|
),
|
|
array(
|
|
'name' => 'farm_log_harvest',
|
|
'weight' => 60,
|
|
),
|
|
array(
|
|
'name' => 'farm_log_movement',
|
|
'weight' => 100,
|
|
),
|
|
array(
|
|
'name' => 'farm_asset_children',
|
|
'display' => 'page',
|
|
'title' => t('Children'),
|
|
'weight' => 110,
|
|
),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Implements hook_farm_taxonomy_term_view_views().
|
|
*/
|
|
function farm_crop_farm_taxonomy_term_view_views($term) {
|
|
|
|
// Start a list of View names.
|
|
$views = array();
|
|
|
|
// Switch logic depending on the vocabulary.
|
|
switch ($term->vocabulary_machine_name) {
|
|
|
|
// Farm areas:
|
|
case 'farm_areas':
|
|
$views[] = array(
|
|
'name' => 'farm_plantings',
|
|
'arg' => 1,
|
|
'weight' => -20,
|
|
);
|
|
break;
|
|
|
|
// Farm crops.
|
|
case 'farm_crops':
|
|
$views[] = array(
|
|
'name' => 'farm_plantings',
|
|
'arg' => 2,
|
|
'always' => TRUE,
|
|
);
|
|
break;
|
|
|
|
// Farm crop family.
|
|
case 'farm_crop_families':
|
|
$views[] = 'farm_crops';
|
|
break;
|
|
}
|
|
|
|
return $views;
|
|
}
|
|
|
|
/**
|
|
* Implements hook_farm_area_links().
|
|
*/
|
|
function farm_crop_farm_area_links($id) {
|
|
$links = array();
|
|
|
|
// Add link to plantings.
|
|
$view = views_get_view('farm_plantings');
|
|
$view->preview('default', array($id));
|
|
if ($view->total_rows > 0) {
|
|
$links[] = array(
|
|
'title' => t('Plantings') . ': ' . $view->total_rows,
|
|
'href' => 'farm/assets/plantings/' . $id,
|
|
);
|
|
}
|
|
|
|
return $links;
|
|
}
|
|
|
|
/**
|
|
* Implements hook_views_post_render().
|
|
*/
|
|
function farm_crop_views_post_render(&$view, &$output, &$cache) {
|
|
|
|
// If this is the Farm Plantings page...
|
|
if ($view->name == 'farm_plantings' && $view->current_display == 'page') {
|
|
|
|
// If dashboard maps are disabled in the farm_map module settings, bail.
|
|
if (!variable_get('farm_map_show', TRUE)) {
|
|
return;
|
|
}
|
|
|
|
// If there are any arguments, bail.
|
|
/**
|
|
* @todo
|
|
* Display a map that is filtered by the same arguments.
|
|
*/
|
|
if (!empty($view->args)) {
|
|
return;
|
|
}
|
|
|
|
// If the View result is not empty...
|
|
if (!empty($view->result)) {
|
|
|
|
// Add the Plantings asset map to the top of the View.
|
|
$map = \Drupal\openlayers\Openlayers::load('Map', 'farm_assets_planting');
|
|
if (!empty($map)) {
|
|
$map_build = $map->build();
|
|
$output = drupal_render($map_build) . $output;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implements hook_farm_log_allowed_asset_reference_type().
|
|
*/
|
|
function farm_crop_farm_log_allowed_asset_reference_type($log_type) {
|
|
|
|
// On seeding and transplanting logs, only plantings can be referenced.
|
|
if (in_array($log_type, array('farm_seeding', 'farm_transplanting'))) {
|
|
return 'planting';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implements hook_farm_log_categories().
|
|
*/
|
|
function farm_crop_farm_log_categories() {
|
|
|
|
// Provide an "Plantings" log category.
|
|
return array('Plantings');
|
|
}
|