mirror of
https://github.com/farmOS/farmOS.git
synced 2024-02-23 11:37:38 +01:00
299 lines
6.5 KiB
Plaintext
299 lines
6.5 KiB
Plaintext
<?php
|
|
/**
|
|
* @file
|
|
* Code for the Farm Crop feature.
|
|
*/
|
|
|
|
include_once 'farm_crop.features.inc';
|
|
|
|
/**
|
|
* 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_harvest',
|
|
'farm_input',
|
|
'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',
|
|
'weight' => -5,
|
|
),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Implements hook_farm_admin_actions().
|
|
*/
|
|
function farm_crop_farm_admin_actions() {
|
|
|
|
// Define actions.
|
|
$actions = array(
|
|
'crop' => array(
|
|
'title' => t('Add a crop'),
|
|
'href' => 'admin/structure/taxonomy/farm_crops/add',
|
|
'paths' => array(
|
|
'farm/assets/plantings/crops',
|
|
),
|
|
),
|
|
'harvest' => array(
|
|
'title' => t('Add a harvest'),
|
|
'href' => 'log/add/farm_harvest',
|
|
'assets' => array(
|
|
'planting',
|
|
),
|
|
'views' => array(
|
|
'farm_log_harvest',
|
|
),
|
|
),
|
|
'input' => array(
|
|
'title' => t('Add an input'),
|
|
'href' => 'log/add/farm_input',
|
|
'assets' => array(
|
|
'planting',
|
|
),
|
|
'views' => array(
|
|
'farm_log_input',
|
|
),
|
|
'paths' => array(
|
|
'taxonomy/term/%',
|
|
),
|
|
),
|
|
'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'), '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(
|
|
'farm_log_seeding',
|
|
'farm_log_transplanting',
|
|
'farm_log_input',
|
|
'farm_log_harvest',
|
|
'farm_log_activity',
|
|
'farm_log_observation',
|
|
'farm_log_movement',
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 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,
|
|
);
|
|
$views[] = array(
|
|
'name' => 'farm_log_input',
|
|
'arg' => 2,
|
|
);
|
|
break;
|
|
|
|
// Farm crops.
|
|
case 'farm_crops':
|
|
$views[] = array(
|
|
'name' => 'farm_plantings',
|
|
'arg' => 2,
|
|
);
|
|
break;
|
|
|
|
// Farm crop family.
|
|
case 'farm_crop_families':
|
|
$views[] = 'farm_crops';
|
|
break;
|
|
}
|
|
|
|
return $views;
|
|
}
|
|
|
|
/**
|
|
* Implements hook_form_alter().
|
|
*/
|
|
function farm_crop_form_alter(&$form, &$form_state, $form_id) {
|
|
|
|
// If this is a log form with a planting reference field...
|
|
if ($form_id == 'log_form' && !empty($form['field_farm_planting'])) {
|
|
|
|
// Alter the form with the farm_log helper function.
|
|
farm_log_form_prepopulate_asset($form, 'field_farm_planting');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implements hook_farm_area_links().
|
|
*/
|
|
function farm_crop_farm_area_links($id) {
|
|
$links = array();
|
|
|
|
// Add link to inputs.
|
|
$links[] = array(
|
|
'title' => t('Inputs'),
|
|
'href' => 'taxonomy/term/' . $id,
|
|
'options' => array(
|
|
'fragment' => 'Inputs',
|
|
),
|
|
'weight' => 10,
|
|
);
|
|
|
|
// Add link to plantings.
|
|
$links[] = array(
|
|
'title' => t('Plantings'),
|
|
'href' => 'taxonomy/term/' . $id,
|
|
'options' => array(
|
|
'fragment' => 'Plantings',
|
|
),
|
|
);
|
|
|
|
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 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');
|
|
$map_build = $map->build();
|
|
$output = drupal_render($map_build) . $output;
|
|
}
|
|
}
|
|
}
|