From f2d5c7c336cfa40cf6f051552d426394dabda03a Mon Sep 17 00:00:00 2001 From: Michael Stenta Date: Wed, 30 May 2018 12:41:21 -0400 Subject: [PATCH] Initial commit of new farm_map_kml module - provides a KML export action for farmOS geometry fields. --- .../farm_map/farm_map_kml/farm_map_kml.info | 6 + .../farm_map/farm_map_kml/farm_map_kml.module | 123 ++++++++++++++++++ .../farm_map_kml/farm_map_kml.theme.inc | 49 +++++++ .../farm_map_kml/farm_map_kml.tpl.php | 7 + .../farm_map_kml_placemark.tpl.php | 5 + 5 files changed, 190 insertions(+) create mode 100644 modules/farm/farm_map/farm_map_kml/farm_map_kml.info create mode 100644 modules/farm/farm_map/farm_map_kml/farm_map_kml.module create mode 100644 modules/farm/farm_map/farm_map_kml/farm_map_kml.theme.inc create mode 100644 modules/farm/farm_map/farm_map_kml/farm_map_kml.tpl.php create mode 100644 modules/farm/farm_map/farm_map_kml/farm_map_kml_placemark.tpl.php diff --git a/modules/farm/farm_map/farm_map_kml/farm_map_kml.info b/modules/farm/farm_map/farm_map_kml/farm_map_kml.info new file mode 100644 index 00000000..540c4724 --- /dev/null +++ b/modules/farm/farm_map/farm_map_kml/farm_map_kml.info @@ -0,0 +1,6 @@ +name = Farm Map KML +description = Provides KML features for farm maps. +core = 7.x +package = farmOS +dependencies[] = farm_map +dependencies[] = geophp diff --git a/modules/farm/farm_map/farm_map_kml/farm_map_kml.module b/modules/farm/farm_map/farm_map_kml/farm_map_kml.module new file mode 100644 index 00000000..3f2e0154 --- /dev/null +++ b/modules/farm/farm_map/farm_map_kml/farm_map_kml.module @@ -0,0 +1,123 @@ + array( + 'variables' => array( + 'content' => NULL, + 'placemarks' => array(), + ), + 'template' => 'farm_map_kml', + 'file' => 'farm_map_kml.theme.inc', + ), + 'farm_map_kml_placemark' => array( + 'variables' => array( + 'pid' => NULL, + 'name' => NULL, + 'description' => NULL, + 'geometry' => NULL, + 'kml' => NULL, + ), + 'template' => 'farm_map_kml_placemark', + 'file' => 'farm_map_kml.theme.inc', + ), + ); +} + +/** + * Implements hook_action_info(). + */ +function farm_map_kml_action_info() { + return array( + 'farm_map_kml_action' => array( + 'type' => 'entity', + 'label' => t('KML'), + 'triggers' => array('any'), + 'configurable' => FALSE, + 'aggregate' => TRUE, + ), + ); +} + +/** + * Action function for farm_map_kml_action. + * + * Creates a KML file containing shapes from selected entities. + * + * @param array $entities + * An array of entities. + * @param array $context + * Array with parameters for this action. + */ +function farm_map_kml_action(array $entities, $context = array()) { + + // Iterate through the entities to generate placemarks. + $placemarks = array(); + foreach ($entities as $entity) { + + // Get the geometry. If it is empty, skip the entity. + if (empty($entity->field_farm_geofield[LANGUAGE_NONE][0]['geom'])) { + continue; + } + $geometry = $entity->field_farm_geofield[LANGUAGE_NONE][0]['geom']; + + // Get the entity id and label. + list($id, $rid, $bundle) = entity_extract_ids($context['entity_type'], $entity); + $label = entity_label($context['entity_type'], $entity); + + // Create a placemark. + $placemark = array( + 'pid' => $id, + 'name' => $label, + 'geometry' => $geometry, + ); + + // If this is an area entity (taxonomy_term), add the description. + if ($context['entity_type'] == 'taxonomy_term' && $entity->vocabulary_machine_name == 'farm_areas') { + if (!empty($entity->description)) { + $placemark['description'] = $entity->description; + } + } + + // Add the placemark to the list. + $placemarks[] = $placemark; + } + + // If there are no placemarks, bail with a warning. + if (empty($placemarks)) { + drupal_set_message(t('No placemarks were found.'), 'warning'); + return; + } + + // Create KML output. + $kml = theme('farm_map_kml', array('placemarks' => $placemarks)); + + // Ensure that a directory exists to store the KML file in. + $directory = 'public://kml'; + file_prepare_directory($directory, FILE_CREATE_DIRECTORY); + + // Create the temporary KML file. + $filename = 'kml_export-' . date('c') . '.kml'; + $destination = $directory . '/' . $filename; + $file = file_save_data($kml, $destination); + + // Make the file temporary. + $file->status = 0; + file_save($file); + + // Show a link to the file. + $message = 'KML file created: %filename'; + $args = array( + '!path' => file_create_url($file->uri), + '%filename' => $file->filename, + ); + drupal_set_message(t($message, $args)); +} diff --git a/modules/farm/farm_map/farm_map_kml/farm_map_kml.theme.inc b/modules/farm/farm_map/farm_map_kml/farm_map_kml.theme.inc new file mode 100644 index 00000000..0a041aa5 --- /dev/null +++ b/modules/farm/farm_map/farm_map_kml/farm_map_kml.theme.inc @@ -0,0 +1,49 @@ +out('kml'); + + // Append the KML to the output. + $vars['kml'] .= $kml . "\n"; + } +} diff --git a/modules/farm/farm_map/farm_map_kml/farm_map_kml.tpl.php b/modules/farm/farm_map/farm_map_kml/farm_map_kml.tpl.php new file mode 100644 index 00000000..357fc997 --- /dev/null +++ b/modules/farm/farm_map/farm_map_kml/farm_map_kml.tpl.php @@ -0,0 +1,7 @@ +'; ?> + + + + + + diff --git a/modules/farm/farm_map/farm_map_kml/farm_map_kml_placemark.tpl.php b/modules/farm/farm_map/farm_map_kml/farm_map_kml_placemark.tpl.php new file mode 100644 index 00000000..223e4d81 --- /dev/null +++ b/modules/farm/farm_map/farm_map_kml/farm_map_kml_placemark.tpl.php @@ -0,0 +1,5 @@ + + + + +