farmOS/modules/farm/farm_crop/farm_crop.module

289 lines
6.8 KiB
Plaintext

<?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_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_entities().
*/
function farm_crop_farm_ui_entities() {
return array(
'farm_asset' => array(
'planting' => array(
'label' => t('Planting'),
'label_plural' => t('Plantings'),
'view' => 'farm_plantings',
),
),
'log' => array(
'farm_seeding' => array(
'label' => t('Seeding'),
'label_plural' => t('Seedings'),
'view' => 'farm_log_seeding',
'farm_asset' => 'planting',
),
'farm_transplanting' => array(
'label' => t('Transplanting'),
'label_plural' => t('Transplantings'),
'view' => 'farm_log_transplanting',
'farm_asset' => 'planting',
),
),
'taxonomy_term' => array(
'farm_crops' => array(
'label' => t('Crop/variety'),
'label_plural' => t('Crops/varieties'),
'view' => 'farm_crops',
'farm_asset' => 'planting',
),
'farm_crop_families' => array(
'label' => t('Crop Family'),
'label_plural' => t('Crop Families'),
'farm_asset' => 'planting',
),
),
);
}
/**
* Implements hook_farm_ui_asset_views().
*/
function farm_crop_farm_ui_asset_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_ui_taxonomy_views().
*/
function farm_crop_farm_ui_taxonomy_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_log_categories().
*/
function farm_crop_farm_log_categories() {
// Provide an "Plantings" log category.
return array('Plantings');
}