View source
<?php
include_once 'farm_map.block.inc';
include_once 'farm_map.geo.inc';
function farm_map_hook_info() {
$hooks['farm_map_behaviors'] = array(
'group' => 'farm_map',
);
$hooks['farm_map_behavior_settings'] = array(
'group' => 'farm_map',
);
$hooks['farm_map_view'] = array(
'group' => 'farm_map',
);
return $hooks;
}
function farm_map_permission() {
$perms = array(
'administer farm_map module' => array(
'title' => t('Administer farm map module'),
),
);
return $perms;
}
function farm_map_farm_access_perms($role) {
$perms = array();
$roles = farm_access_roles();
if (!empty($roles[$role]['access']['config'])) {
$perms[] = 'administer farm_map module';
}
return $perms;
}
function farm_map_menu() {
$items['admin/config/farm/map'] = array(
'title' => 'Map',
'description' => 'Map configuration settings.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'farm_map_settings_form',
),
'access arguments' => array(
'administer farm_map module',
),
);
return $items;
}
function farm_map_libraries_info() {
$libraries['farmOS-map'] = array(
'name' => 'farmOS Map',
'vendor url' => 'https://github.com/farmOS/farmOS-map',
'download url' => 'https://github.com/farmOS/farmOS-map/releases',
'version arguments' => array(
'file' => 'farmOS-map.js',
'pattern' => '/farmOS-map v([0-9\\.]+)/',
'lines' => 1,
),
'files' => array(
'js' => array(
'farmOS-map.js',
),
),
);
return $libraries;
}
function farm_map_theme($existing, $type, $theme, $path) {
return array(
'farm_map' => array(
'render element' => 'element',
),
);
}
function theme_farm_map(&$vars) {
$element = $vars['element'];
libraries_load('farmOS-map');
if (empty($element['#map_name'])) {
$element['#map_name'] = 'farm_map';
}
$map_name = $element['#map_name'];
$id = drupal_html_id($map_name);
if (!empty($element['#attributes']['id'])) {
$id = $element['#attributes']['id'];
}
else {
$element['#attributes']['id'] = $id;
}
if (empty($element['#attributes']['class']) || !in_array('farm-map', $element['#attributes']['class'])) {
$element['#attributes']['class'][] = 'farm-map';
}
$settings = array(
'farm_map' => array(),
);
$settings['farm_map']['base_path'] = str_replace('farm', '', url('farm'));
$system_of_measurement = farm_quantity_system_of_measurement();
$settings['farm_map']['units'] = $system_of_measurement;
if (isset($element['#wkt'])) {
$settings['farm_map']['wkt'][$id] = $element['#wkt'];
}
drupal_add_js($settings, 'setting');
drupal_add_js(drupal_get_path('module', 'farm_map') . '/js/farm_map.js');
drupal_add_css(drupal_get_path('module', 'farm_map') . '/css/farm_map.css');
module_invoke_all('farm_map_view', $map_name, $element);
return '<div' . drupal_attributes($element['#attributes']) . '></div>';
}
function farm_map_element_info() {
return array(
'farm_map' => array(
'#theme' => 'farm_map',
),
);
}
function farm_map_build($map_name, $fieldset = FALSE, $title = '', $collapsed = FALSE) {
$build = array(
'#type' => 'farm_map',
'#map_name' => $map_name,
);
if (!empty($fieldset)) {
$build = array(
'#type' => 'fieldset',
'#title' => $title,
'#collapsible' => TRUE,
'#collapsed' => $collapsed,
'farm_map' => $build,
);
drupal_add_library('system', 'drupal.collapse');
$build['#attributes']['class'][] = 'collapsible';
$build['#attributes']['class'][] = 'collapsed';
}
return $build;
}
function farm_map_add_behavior($behavior, $settings = array()) {
$hook = 'farm_map_behaviors';
$modules = module_implements($hook);
foreach ($modules as $module) {
$behaviors = module_invoke($module, $hook);
if (!(array_key_exists($behavior, $behaviors) && !empty($behaviors[$behavior]['js']))) {
continue;
}
$settings_hook = 'farm_map_behavior_settings';
$default_settings = array();
if (function_exists($module . '_' . $settings_hook)) {
$default_settings = module_invoke($module, $settings_hook, $behavior);
}
$settings = array_merge($default_settings, $settings);
if (!empty($settings)) {
drupal_add_js(array(
'farm_map' => array(
'behaviors' => array(
$behavior => $settings,
),
),
), array(
'type' => 'setting',
));
}
$path = drupal_get_path('module', $module) . '/' . $behaviors[$behavior]['js'];
drupal_add_js($path);
}
}
function farm_map_settings_form($form, &$form_state) {
$form = array();
return system_settings_form($form);
}
function farm_map_entity_geometries($entity_type, $entity) {
return module_invoke_all('farm_map_entity_geometries', $entity_type, $entity);
}
function farm_map_farm_map_entity_geometries($entity_type, $entity) {
$geometries = array();
if (!empty($entity->field_farm_geofield[LANGUAGE_NONE][0]['geom'])) {
$geometries[] = $entity->field_farm_geofield[LANGUAGE_NONE][0]['geom'];
}
return $geometries;
}
function farm_map_geofield_populate(&$entity, $geoms = array()) {
if (empty($geoms)) {
return;
}
$geometry = farm_map_combine_geoms($geoms);
if (empty($geometry)) {
return;
}
if (!empty($geometry)) {
$entity->field_farm_geofield[LANGUAGE_NONE][0] = geofield_get_values_from_geometry($geometry);
}
}