3
0
Fork 0
mirror of https://github.com/farmOS/farmOS.git synced 2024-02-23 11:37:38 +01:00
farmOS/farm_area.module

300 lines
6.6 KiB
Plaintext
Raw Normal View History

<?php
/**
* @file
* Code for the Farm Area feature.
*/
include_once 'farm_area.features.inc';
2015-09-08 21:14:16 +02:00
/**
* Implements hook_farm_access_perms().
2015-09-08 21:14:16 +02:00
*/
function farm_area_farm_access_perms($role) {
2015-09-08 21:14:16 +02:00
// Assemble a list of entity types provided by this module.
2015-09-08 21:14:16 +02:00
$types = array(
'taxonomy' => array(
'farm_areas',
),
);
// 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;
2015-09-08 21:14:16 +02:00
}
/**
* Implements hook_farm_area_type_info().
*/
function farm_area_farm_area_type_info() {
return array(
'building' => array(
'label' => t('Building'),
'style' => 'farm_map_style_red',
'weight' => 0,
),
'field' => array(
'label' => t('Field'),
2015-07-29 00:14:07 +02:00
'style' => 'farm_map_style_yellow',
'weight' => 10,
),
'landmark' => array(
'label' => t('Landmark'),
'style' => 'farm_map_style_orange',
'weight' => 50,
),
'water' => array(
'label' => t('Water'),
2015-07-29 00:14:07 +02:00
'style' => 'farm_map_style_blue',
'weight' => 50,
),
'property' => array(
'label' => t('Property'),
'style' => 'farm_map_style_purple',
'weight' => 100,
),
);
}
/**
* Get information about all available area types.
*/
function farm_area_types() {
// Get available types from modules.
$area_types = module_invoke_all('farm_area_type_info');
// Iterate through the types and create an index by weight.
$weight_index = array();
foreach ($area_types as $key => $type) {
// Default weight is zero.
if (empty($type['weight'])) {
$type['weight'] = 0;
}
// Add it to the weight index array.
$weight_index[$key] = $type['weight'];
}
// Sort the weight index array.
asort($weight_index);
// Iterate through the weight index to build the final sorted list.
$area_types_sorted = array();
foreach ($weight_index as $key => $weight) {
$area_types_sorted[$key] = $area_types[$key];
}
// Return the sorted list.
return $area_types_sorted;
}
/**
* Get an array of available farm area type options.
*
* @return array
* Returns an array of farm area type options provided by modules, for use
* in a form select element.
*/
function farm_area_type_options() {
// Start with an empty options array.
$options = array();
// Get available types from modules.
$area_types = module_invoke_all('farm_area_type_info');
// Iterate through the weight index to build the final options list.
foreach ($area_types as $key => $type) {
if (!empty($type['label'])) {
$options[$key] = $type['label'];
}
}
// Return the list of options.
return $options;
}
/**
* Implements hook_farm_admin_actions().
*/
function farm_area_farm_admin_actions() {
// Define farm admin actions.
$actions = array(
'area' => array(
'title' => t('Add an area'),
'href' => 'admin/structure/taxonomy/farm_areas/add',
'views' => array(
'farm_areas',
),
'paths' => array(
'farm',
),
),
);
return $actions;
}
/**
* Implements hook_farm_taxonomy_breadcrumb().
*/
function farm_area_farm_taxonomy_breadcrumb($term) {
$breadcrumb = array();
// If the term is in farm_areas, add a link to /farm/areas.
if ($term->vocabulary_machine_name == 'farm_areas') {
$breadcrumb[] = l(t('Areas'), 'farm/areas');
}
return $breadcrumb;
}
2015-05-15 16:56:26 +02:00
/**
* Implements hook_farm_area_details().
*/
function farm_area_farm_area_details($id) {
// Start a render array.
$output = array();
// Get area links.
$area_links = farm_area_get_links($id);
// Create variables for an item list.
$variables = array(
'items' => $area_links,
'attributes' => array(),
);
2015-05-15 16:56:26 +02:00
// Add area links to the details.
$output[] = array(
'#type' => 'markup',
'#markup' => theme('item_list', $variables),
'#weight' => 100,
2015-05-15 16:56:26 +02:00
);
// Return the render array.
return $output;
}
/**
* Generate area details.
*
* @param int $id
* The area id.
*
* @return string
* Returns a string of links.
*/
function farm_area_get_details($id) {
// Call out to modules that want to provide links.
$area_details = module_invoke_all('farm_area_details', check_plain($id));
// Render and return.
return drupal_render($area_details);
}
/**
* Generate area links, sorted by weight.
*
2015-04-10 23:28:54 +02:00
* @param int $id
* The area id.
*
* @return array
* Returns an array of links.
*/
function farm_area_get_links($id) {
// Call out to modules that want to provide links.
$area_links = module_invoke_all('farm_area_links', check_plain($id));
// Build an index of links and their weights.
$weight_index = array();
foreach ($area_links as $key => $link) {
// Default the weight to zero if it hasn't been set.
if (!isset($link['weight'])) {
$link['weight'] = 0;
}
// Assign the weight to the index, based on it's key in the original array.
$weight_index[$key] = $link['weight'];
}
// Sort the index by weight.
asort($weight_index);
// Rebuild the array of links based on their weights.
$sorted_area_links = array();
foreach ($weight_index as $key => $weight) {
$sorted_area_links[] = $area_links[$key];
}
// Generate an array of Drupal links.
$links = array();
foreach ($sorted_area_links as $link) {
// If a title and href are available...
if (!empty($link['title']) && !empty($link['href'])) {
// Build link options.
$options = array();
if (!empty($link['options'])) {
$options = $link['options'];
}
// Build the link and add it to the array.
$links[] = l($link['title'], $link['href'], $options);
}
}
// Return links.
return $links;
}
/**
* Implements hook_page_build().
*/
function farm_area_page_build(&$page) {
// If this is the farm dashboard, display the areas map.
2015-08-08 18:10:43 +02:00
$current_path = current_path();
$map_paths = array(
'farm',
2015-08-09 18:51:17 +02:00
'farm/dashboard',
2015-08-08 18:10:43 +02:00
'farm/areas',
'farm/areas/list',
);
if (in_array($current_path, $map_paths)) {
// Load the areas map.
$map = \Drupal\openlayers\Openlayers::load('Map', 'farm_areas');
// Build the map and add it to the page content.
$page['content']['farm_areas'] = $map->build();
// Set the weight to -100 so that it appears on top.
$page['content']['farm_areas']['#weight'] = -100;
// Set the content region #sorted flag to FALSE so that it resorts.
$page['content']['#sorted'] = FALSE;
}
}