2014-02-26 16:37:20 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Code for the Farm Area feature.
|
|
|
|
*/
|
|
|
|
|
|
|
|
include_once 'farm_area.features.inc';
|
2014-11-21 20:51:25 +01:00
|
|
|
|
2015-09-08 21:14:16 +02:00
|
|
|
/**
|
2015-10-04 06:04:53 +02:00
|
|
|
* Implements hook_farm_access_perms().
|
2015-09-08 21:14:16 +02:00
|
|
|
*/
|
2015-10-04 06:04:53 +02:00
|
|
|
function farm_area_farm_access_perms($role) {
|
2015-09-08 21:14:16 +02:00
|
|
|
|
2015-10-04 06:04:53 +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',
|
|
|
|
),
|
|
|
|
);
|
2015-10-04 06:04:53 +02:00
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2015-06-20 17:57:04 +02:00
|
|
|
/**
|
|
|
|
* Implements hook_farm_area_type_info().
|
|
|
|
*/
|
|
|
|
function farm_area_farm_area_type_info() {
|
|
|
|
return array(
|
|
|
|
'building' => array(
|
|
|
|
'label' => t('Building'),
|
2015-09-08 18:15:10 +02:00
|
|
|
'style' => 'farm_map_style_red',
|
2015-07-31 02:51:08 +02:00
|
|
|
'weight' => 0,
|
2015-06-20 17:57:04 +02:00
|
|
|
),
|
|
|
|
'field' => array(
|
|
|
|
'label' => t('Field'),
|
2015-07-29 00:14:07 +02:00
|
|
|
'style' => 'farm_map_style_yellow',
|
2015-07-31 02:51:08 +02:00
|
|
|
'weight' => 10,
|
|
|
|
),
|
|
|
|
'landmark' => array(
|
|
|
|
'label' => t('Landmark'),
|
2015-09-08 18:15:10 +02:00
|
|
|
'style' => 'farm_map_style_orange',
|
2015-07-31 02:51:08 +02:00
|
|
|
'weight' => 50,
|
2015-06-20 17:57:04 +02:00
|
|
|
),
|
|
|
|
'water' => array(
|
2015-11-02 05:15:24 +01:00
|
|
|
'label' => t('Water'),
|
2015-07-29 00:14:07 +02:00
|
|
|
'style' => 'farm_map_style_blue',
|
2015-07-31 02:51:08 +02:00
|
|
|
'weight' => 50,
|
2015-06-20 17:57:04 +02:00
|
|
|
),
|
2015-07-31 02:51:08 +02:00
|
|
|
'property' => array(
|
|
|
|
'label' => t('Property'),
|
|
|
|
'style' => 'farm_map_style_purple',
|
2015-06-20 17:57:04 +02:00
|
|
|
'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;
|
|
|
|
}
|
|
|
|
|
2014-11-24 23:46:00 +01:00
|
|
|
/**
|
2015-03-13 01:00:58 +01:00
|
|
|
* Implements hook_farm_admin_actions().
|
2014-11-24 23:46:00 +01:00
|
|
|
*/
|
2015-03-13 01:00:58 +01:00
|
|
|
function farm_area_farm_admin_actions() {
|
2014-11-24 23:46:00 +01:00
|
|
|
|
2015-03-13 01:00:58 +01:00
|
|
|
// Define farm admin actions.
|
2014-11-24 23:46:00 +01:00
|
|
|
$actions = array(
|
|
|
|
'area' => array(
|
|
|
|
'title' => t('Add an area'),
|
2015-03-13 01:00:58 +01:00
|
|
|
'href' => 'admin/structure/taxonomy/farm_areas/add',
|
2015-03-14 15:53:58 +01:00
|
|
|
'views' => array(
|
|
|
|
'farm_areas',
|
2015-03-13 01:00:58 +01:00
|
|
|
),
|
2015-03-27 15:40:33 +01:00
|
|
|
'paths' => array(
|
|
|
|
'farm',
|
|
|
|
),
|
2014-11-24 23:46:00 +01:00
|
|
|
),
|
|
|
|
);
|
2015-03-13 01:00:58 +01:00
|
|
|
return $actions;
|
2014-11-24 23:46:00 +01:00
|
|
|
}
|
|
|
|
|
2015-04-16 18:12:35 +02:00
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
|
2015-07-29 23:09:45 +02:00
|
|
|
// 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',
|
2015-07-29 23:09:45 +02:00
|
|
|
'#markup' => theme('item_list', $variables),
|
2015-05-27 17:43:44 +02:00
|
|
|
'#weight' => 100,
|
2015-05-15 16:56:26 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// Return the render array.
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
2015-05-15 16:55:12 +02:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2014-11-21 20:51:25 +01:00
|
|
|
/**
|
2014-12-01 15:03:10 +01:00
|
|
|
* Generate area links, sorted by weight.
|
2014-11-21 20:51:25 +01:00
|
|
|
*
|
2015-04-10 23:28:54 +02:00
|
|
|
* @param int $id
|
2014-11-21 20:51:25 +01:00
|
|
|
* The area id.
|
|
|
|
*
|
2015-07-29 23:09:45 +02:00
|
|
|
* @return array
|
|
|
|
* Returns an array of links.
|
2014-11-21 20:51:25 +01:00
|
|
|
*/
|
2015-07-29 23:09:45 +02:00
|
|
|
function farm_area_get_links($id) {
|
2014-11-21 20:51:25 +01:00
|
|
|
|
|
|
|
// Call out to modules that want to provide links.
|
2014-12-01 15:03:10 +01:00
|
|
|
$area_links = module_invoke_all('farm_area_links', check_plain($id));
|
2014-11-21 20:51:25 +01:00
|
|
|
|
2014-12-01 15:03:10 +01:00
|
|
|
// 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) {
|
2015-03-21 15:05:36 +01:00
|
|
|
|
|
|
|
// If a title and href are available...
|
2014-12-01 15:03:10 +01:00
|
|
|
if (!empty($link['title']) && !empty($link['href'])) {
|
2015-03-21 15:05:36 +01:00
|
|
|
|
|
|
|
// 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);
|
2014-12-01 15:03:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-29 23:09:45 +02:00
|
|
|
// Return links.
|
|
|
|
return $links;
|
2015-01-03 23:35:45 +01:00
|
|
|
}
|
2015-06-16 22:10:49 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)) {
|
2015-06-16 22:10:49 +02:00
|
|
|
|
|
|
|
// Load the areas map.
|
2015-07-22 23:34:03 +02:00
|
|
|
$map = \Drupal\openlayers\Openlayers::load('Map', 'farm_areas');
|
2015-06-16 22:10:49 +02:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|