From f9980bbc8e411c19d39e9d53e148e48b7a5b883c Mon Sep 17 00:00:00 2001
From: Pol Dell'Aiera <Pol@47194.no-reply.drupal.org>
Date: Thu, 2 Aug 2012 13:42:34 +0200
Subject: [PATCH] Use CTools Export UI for Maps & Styles.

---
 .../openlayers_ui/includes/openlayers_ui.maps.inc  |  121 ------------
 .../includes/openlayers_ui.styles.inc              |   60 ------
 modules/openlayers_ui/openlayers_ui.module         |  201 +-------------------
 openlayers.install                                 |    2 +-
 openlayers.module                                  |    3 +
 .../openlayers_layers_ctools_export_ui.inc         |   52 +++++
 plugins/export_ui/openlayers_layers_ui.class.php   |   15 ++
 .../export_ui/openlayers_maps_ctools_export_ui.inc |  102 ++++++++++
 plugins/export_ui/openlayers_maps_ui.class.php     |  101 ++++++++++
 .../openlayers_styles_ctools_export_ui.inc         |   54 ++++++
 plugins/export_ui/openlayers_styles_ui.class.php   |  133 +++++++++++++
 11 files changed, 464 insertions(+), 380 deletions(-)
 create mode 100644 plugins/export_ui/openlayers_layers_ctools_export_ui.inc
 create mode 100644 plugins/export_ui/openlayers_layers_ui.class.php
 create mode 100644 plugins/export_ui/openlayers_maps_ctools_export_ui.inc
 create mode 100644 plugins/export_ui/openlayers_maps_ui.class.php
 create mode 100644 plugins/export_ui/openlayers_styles_ctools_export_ui.inc
 create mode 100644 plugins/export_ui/openlayers_styles_ui.class.php

diff --git a/modules/openlayers_ui/includes/openlayers_ui.maps.inc b/modules/openlayers_ui/includes/openlayers_ui.maps.inc
index 731b446..de2e822 100644
--- a/modules/openlayers_ui/includes/openlayers_ui.maps.inc
+++ b/modules/openlayers_ui/includes/openlayers_ui.maps.inc
@@ -8,16 +8,6 @@
  * @ingroup openlayers
  */
 
-function openlayers_ui_maps_clone($map) {
-  $form_state = array();
-  drupal_set_title(
-    t('Clone map %map',
-      array('%map' => $map->name)
-    ), PASS_THROUGH
-  );
-  return drupal_get_form('openlayers_ui_maps_form', $map);
-}
-
 /**
  * Menu Callback for Add Map
  */
@@ -570,83 +560,10 @@ function openlayers_ui_maps_form($form, &$form_state, $map = FALSE, $edit = FALS
     );
   }
 
-  // Buttons
-  $form['buttons'] = array('#tree' => FALSE);
-  $form['buttons']['openlayers_save'] = array(
-    '#type' => 'submit',
-    '#value' => t('Save')
-  );
-  $form['buttons']['openlayers_preview'] = array(
-    '#type' => 'submit',
-    '#value' => t('Preview')
-  );
-  $form['buttons']['openlayers_cancel'] = array(
-    '#type' => 'submit',
-    '#value' => t('Cancel')
-  );
-
   return $form;
 }
 
 /**
- * OpenLayers Map Form Validate
- *
- * Validates a map form submission.
- *
- * @param $map_form
- *   Array of values to validate
- * @return
- *   Does not return anything. Uses form_set_error() to communicate errors.
- */
-function openlayers_ui_maps_form_validate($form, &$form_state) {
-  $values = $form_state['values'];
-  $found_error = FALSE;
-
-  // Check if ahah submitting
-  if ($form_state['clicked_button']['#id'] == 'edit-openlayers-projection-ahah' ||
-      $form_state['clicked_button']['#id'] == 'edit-openlayers-cancel') {
-    return TRUE;
-  }
-
-  // Check map name first
-  if (!preg_match('!^[a-z0-9_]+$!', $values['name'])) {
-    form_set_error('name', t('Map Name must contain only lowercase letters, numbers, and underscores.'));
-    $found_error = TRUE;
-  }
-
-  // Check if adding and name already exists
-  $existing = openlayers_map_load($values['name'], TRUE);
-  if (!empty($existing) && (!$form_state['values']['map_edit']) &&
-      ($existing->export_type & EXPORT_IN_DATABASE)) {
-    form_set_error('name', t('The Map Name already exists.'));
-    $found_error = TRUE;
-  }
-
-  // Attempt to render map to find any errors
-  $map = openlayers_ui_maps_form_process($form_state['values']);
-  $map = openlayers_build_map($map);
-  // Check if any errors found
-  if (is_array($map['errors']) && count($map['errors']) > 0) {
-    foreach ($map['errors'] as $error) {
-      form_set_error('openlayers',
-        t('OpenLayers Map Rendering Error: !error', array('!error' => $error)));
-      $found_error = TRUE;
-    }
-  }
-
-  // If found error, rebuild form
-  if ($found_error) {
-    // Add OpenLayers CSS & Javascript, because the form function will not be run on rebuild
-    openlayers_include();
-    drupal_add_js(drupal_get_path('module', 'openlayers_ui') .
-      '/js/openlayers_ui.maps.js', 'module');
-    drupal_add_css(drupal_get_path('module', 'openlayers_ui') .
-      '/openlayers_ui.css');
-    $form_state['rebuild'] = TRUE;
-  }
-}
-
-/**
  * Form submit for map add form, for the projection add ahah
  */
 function openlayers_ui_maps_add_projection_submit($form, &$form_state) {
@@ -655,44 +572,6 @@ function openlayers_ui_maps_add_projection_submit($form, &$form_state) {
   $form_state['rebuild'] = TRUE;
 }
 
-/**
- * Form submit for map add form
- */
-function openlayers_ui_maps_form_submit($form, &$form_state) {
-  // Check for cancel
-  if ($form_state['clicked_button']['#id'] == 'edit-openlayers-cancel') {
-    $form_state['redirect'] = 'admin/structure/openlayers/maps/list';
-  }
-
-  // On preview, process, but don't save, rebuild.
-  if ($form_state['clicked_button']['#id'] == 'edit-openlayers-preview') {
-    $map_data = openlayers_ui_maps_form_process($form_state['values']);
-    $form_state['values']['preview_map'] = $map_data;
-    $form_state['rebuild'] = TRUE;
-  }
-
-  // Only save if save button is pressed
-  if ($form_state['clicked_button']['#id'] == 'edit-openlayers-save') {
-    $map_data = openlayers_ui_maps_form_process($form_state['values']);
-
-    // Save map
-    $map_object = new stdClass();
-    $map_object->name = $form_state['values']['name'];
-    $map_object->title = $form_state['values']['title'];
-    $map_object->description = $form_state['values']['description'];
-    $map_object->data = $map_data;
-    $success = openlayers_map_save($map_object);
-
-    // Redirect to edit page
-    if ($success) {
-      drupal_set_message(t('Map saved.'));
-      $form_state['redirect'] = "admin/structure/openlayers/maps/{$map_object->name}/edit";
-    }
-    else {
-      form_set_error('openlayers', t('Error trying to save map'));
-    }
-  }
-}
 
 /**
  * OpenLayers AHAH
diff --git a/modules/openlayers_ui/includes/openlayers_ui.styles.inc b/modules/openlayers_ui/includes/openlayers_ui.styles.inc
index 3045d74..e3409e2 100644
--- a/modules/openlayers_ui/includes/openlayers_ui.styles.inc
+++ b/modules/openlayers_ui/includes/openlayers_ui.styles.inc
@@ -275,65 +275,5 @@ function openlayers_ui_styles_form($form, &$form_state, $style = NULL, $edit = F
     }
   }
 
-  $form['submit'] = array(
-    '#type' => 'submit',
-    '#value' => t('Save'),
-  );
-  $form['preview'] = array(
-    '#type' => 'submit',
-    '#value' => t('Preview'),
-  );
   return $form;
 }
-
-/**
- * Submit handler for layers.
- */
-function openlayers_ui_styles_form_submit(&$form, &$form_state) {
-  $style_data = $form_state['values']['style_data'];
-
-  // Cast and unset values so JS can handle them better,
-  // Unless values are in the form ${attribute}
-  foreach ($form_state['values']['data'] as $key => $value) {
-    if ($form_state['values']['data'][$key] === '') {
-      unset($form_state['values']['data'][$key]);
-    }
-    elseif (isset($style_data[$key]['type']) &&
-      strpos($form_state['values']['data'][$key], '${') !== 0) {
-      if ($style_data[$key]['type'] == 'integer') {
-        $form_state['values']['data'][$key] =
-          (int) $form_state['values']['data'][$key];
-      }
-      elseif ($style_data[$key]['type'] == 'float') {
-        $form_state['values']['data'][$key] =
-          (float) $form_state['values']['data'][$key];
-      }
-    }
-  }
-
-  // Create style object
-  $style = new stdClass();
-  $style->name = $form_state['values']['name'];
-  $style->title = $form_state['values']['title'];
-  $style->description = $form_state['values']['description'];
-  $style->data = $form_state['values']['data'];
-
-  // On preview, process, but don't save, rebuild.
-  if ($form_state['clicked_button']['#id'] == 'edit-preview') {
-    $form_state['values']['preview_style'] = $style;
-    $form_state['rebuild'] = TRUE;
-    return;
-  }
-
-  // Save
-  $success = openlayers_style_save($style);
-
-  // Redirect to edit page
-  if ($success) {
-    drupal_set_message(t('Style saved.'));
-    $form_state['redirect'] = 'admin/structure/openlayers/styles/' . $style->name . '/edit';
-  }
-  else {
-    form_set_error('openlayers', t('Error trying to save style.'));
-  }
-}
diff --git a/modules/openlayers_ui/openlayers_ui.module b/modules/openlayers_ui/openlayers_ui.module
index c2ffc1c..f96d73e 100644
--- a/modules/openlayers_ui/openlayers_ui.module
+++ b/modules/openlayers_ui/openlayers_ui.module
@@ -209,147 +209,6 @@ function openlayers_ui_menu() {
     'weight' => -10,
   );
 
-  // Styles.
-  $items['admin/structure/openlayers/styles'] = array(
-    'title' => 'Styles',
-    'page callback' => 'openlayers_ui_object_list',
-    'page arguments' => array(3),
-    'access arguments' => array('administer openlayers'),
-    'file' => 'includes/openlayers_ui.styles.inc',
-    'type' => MENU_LOCAL_TASK,
-    'weight' => -10,
-  );
-  $items['admin/structure/openlayers/styles/list'] = array(
-    'title' => 'List',
-    'type' => MENU_DEFAULT_LOCAL_TASK,
-    'weight' => -20,
-  );
-  $items['admin/structure/openlayers/styles/add'] = array(
-    'title' => 'Add',
-    'page callback' => 'drupal_get_form',
-    'page arguments' => array('openlayers_ui_styles_form'),
-    'access arguments' => array('administer openlayers'),
-    'file' => 'includes/openlayers_ui.styles.inc',
-    'type' => MENU_LOCAL_TASK,
-    'weight' => -10,
-  );
-  $items['admin/structure/openlayers/styles/add/%openlayers_style'] = array(
-    'title' => 'Add',
-    'page callback' => 'drupal_get_form',
-    'page arguments' => array('openlayers_ui_styles_form', 5),
-    'access arguments' => array('administer openlayers'),
-    'file' => 'includes/openlayers_ui.styles.inc',
-    'type' => MENU_NORMAL_ITEM,
-    'weight' => -10,
-  );
-  $items['admin/structure/openlayers/styles/clone/%openlayers_style'] = array(
-    'title' => 'Clone style',
-    'page callback' => 'drupal_get_form',
-    'page arguments' => array('openlayers_ui_styles_form', 5),
-    'access arguments' => array('administer openlayers'),
-    'file' => 'includes/openlayers_ui.styles.inc',
-    'type' => MENU_LOCAL_TASK,
-  );
-  $items['admin/structure/openlayers/styles/%openlayers_style/edit'] = array(
-    'title' => 'Edit style',
-    'page callback' => 'drupal_get_form',
-    'page arguments' => array('openlayers_ui_styles_form', 4, TRUE),
-    'access arguments' => array('administer openlayers'),
-    'file' => 'includes/openlayers_ui.styles.inc',
-    'type' => MENU_LOCAL_TASK
-  );
-  $items['admin/structure/openlayers/styles/%openlayers_style/delete'] = array(
-    'title' => 'Delete style',
-    'page callback' => 'drupal_get_form',
-    'page arguments' => array('openlayers_ui_object_delete', 3, 4),
-    'access arguments' => array('administer openlayers'),
-    'file' => 'includes/openlayers_ui.styles.inc',
-    'type' => MENU_LOCAL_TASK
-  );
-  $items['admin/structure/openlayers/styles/%openlayers_style/export'] = array(
-    'title' => 'Export style',
-    'page callback' => 'drupal_get_form',
-    'page arguments' => array('openlayers_ui_object_export', 3, 4),
-    'access arguments' => array('administer openlayers'),
-    'file' => 'includes/openlayers_ui.styles.inc',
-    'type' => MENU_LOCAL_TASK
-  );
-
-  // Maps.
-  $items['admin/structure/openlayers/maps'] = array(
-    'title' => 'Maps',
-    'description' => 'Create OpenLayers maps.',
-    'page callback' => 'openlayers_ui_object_list',
-    'page arguments' => array(3),
-    'access arguments' => array('administer openlayers'),
-    'file' => 'includes/openlayers_ui.maps.inc',
-    'type' => MENU_LOCAL_TASK,
-    'weight' => 10,
-  );
-  $items['admin/structure/openlayers/maps/list'] = array(
-    'title' => 'List',
-    'type' => MENU_DEFAULT_LOCAL_TASK,
-    'weight' => -20,
-  );
-  $items['admin/structure/openlayers/maps/add'] = array(
-    'title' => 'Add',
-    'description' => 'Add OpenLayers maps.',
-    'page callback' => 'drupal_get_form',
-    'page arguments' => array('openlayers_ui_maps_form'),
-    'access arguments' => array('administer openlayers'),
-    'file' => 'includes/openlayers_ui.maps.inc',
-    'type' => MENU_LOCAL_TASK,
-    'weight' => -10,
-  );
-  $items['admin/structure/openlayers/maps/add/%openlayers_map'] = array(
-    'title' => 'Clone Map',
-    'description' => 'Add OpenLayers maps.',
-    'page callback' => 'drupal_get_form',
-    'page arguments' => array('openlayers_ui_maps_form', 5),
-    'access arguments' => array('administer openlayers'),
-    'file' => 'includes/openlayers_ui.maps.inc',
-    'type' => MENU_NORMAL_ITEM,
-    'weight' => -10,
-  );
-  $items['admin/structure/openlayers/maps/clone/%openlayers_map'] = array(
-    'title' => 'Clone Map',
-    'description' => 'Clone OpenLayers maps.',
-    'page callback' => 'openlayers_ui_maps_clone',
-    'page arguments' => array(5),
-    'access arguments' => array('administer openlayers'),
-    'file' => 'includes/openlayers_ui.maps.inc',
-    'type' => MENU_LOCAL_TASK,
-  );
-  $items['admin/structure/openlayers/maps/%openlayers_map/edit'] = array(
-    'title' => 'Edit map',
-    'description' => 'Edit OpenLayers maps.',
-    'page callback' => 'drupal_get_form',
-    'page arguments' => array('openlayers_ui_maps_form', 4, TRUE),
-    'access arguments' => array('administer openlayers'),
-    'file' => 'includes/openlayers_ui.maps.inc',
-    'type' => MENU_LOCAL_TASK,
-    'tab_parent' => 'admin/structure/openlayers/maps',
-  );
-  $items['admin/structure/openlayers/maps/%openlayers_map/delete'] = array(
-    'title' => 'Delete map',
-    'description' => 'Delete OpenLayers map.',
-    'page callback' => 'drupal_get_form',
-    'page arguments' => array('openlayers_ui_object_delete', 3, 4),
-    'access arguments' => array('administer openlayers'),
-    'file' => 'includes/openlayers_ui.maps.inc',
-    'type' => MENU_LOCAL_TASK,
-    'tab_parent' => 'admin/structure/openlayers/maps',
-  );
-  $items['admin/structure/openlayers/maps/%openlayers_map/export'] = array(
-    'title' => 'Export map',
-    'description' => 'Export OpenLayers map.',
-    'page callback' => 'drupal_get_form',
-    'page arguments' => array('openlayers_ui_object_export', 3, 4),
-    'access arguments' => array('administer openlayers'),
-    'file' => 'includes/openlayers_ui.maps.inc',
-    'type' => MENU_LOCAL_TASK,
-    'tab_parent' => 'admin/structure/openlayers/maps',
-  );
   $items['admin/structure/openlayers/callbacks/version_check/%'] = array(
     'title' => 'OpenLayers Version Check',
     'page callback' => 'openlayers_ui_version_check',
@@ -404,22 +263,11 @@ function openlayers_ui_object_list($type) {
   $rows = array();
 
   switch ($type) {
-    case 'styles':
-      $header = array(t('Preview'), t('Title'), t('Description'), t('Actions'));
-      $objects = openlayers_styles(TRUE);
-      break;
-
     case 'layers':
       $header = array(t('Title'), t('Type'), t('Description'), t('Actions'));
       $layer_types = openlayers_layer_types();
       $objects = openlayers_layers_load(TRUE);
       break;
-
-    case 'maps':
-      $header = array(t('Title'), t('Layers'), t('Description'), t('Actions'));
-      $layers = openlayers_layers_load();
-      $objects = openlayers_maps(TRUE);
-      break;
   }
 
   // Create table rows
@@ -427,27 +275,11 @@ function openlayers_ui_object_list($type) {
     $row = array();
     $links = array();
 
-    // Any pre-title cells.
-    switch ($type) {
-      case 'styles':
-      	// Add the icon as an image in the row if it exists.
-        if (isset($object->data['externalGraphic'])) {
-          $row[] = theme('image', array('path' => $object->data['externalGraphic']));
-      	}
-      	else {
-      	  $row[] = openlayers_ui_style_preview($object);
-      	}
-        break;
-
-      default:
-      	break;
-    }
-
     // Check map type and create links accordingly.
     $links[] = l(t('Export'), "admin/structure/openlayers/{$type}/{$object->name}/export");
-    
+
     // Do not allow for cloning of Views based layers or specifically unclonable
-    if (!($type == 'layers' && isset($object->data['views'])) || 
+    if (!($type == 'layers' && isset($object->data['views'])) ||
       (isset($object->data['clonable']) && $object->data['clonable'] === FALSE)) {
       $links[] = l(t('Clone'), "admin/structure/openlayers/{$type}/clone/{$object->name}");
     }
@@ -475,17 +307,6 @@ function openlayers_ui_object_list($type) {
       case 'layers':
         $row[] = filter_xss_admin($layer_types[$object->data['layer_type']]['title']);
       	break;
-
-      case 'maps':
-        if (!empty($object->data['layers'])) {
-          $layer_names = array();
-          foreach ($object->data['layers'] as $layer) {
-            $layer_names[] = empty($layers[$layer]->title) ? $layer : filter_xss_admin($layers[$layer]->title);
-          }
-          $row[] = '<small>' . implode(', ', $layer_names) . '</small>';
-        }
-        break;
-
       default:
       	break;
     }
@@ -531,17 +352,9 @@ function openlayers_ui_object_delete_submit($form, &$form_state) {
   $object = $form_state['values']['object'];
 
   switch ($type) {
-    case 'styles':
-      $result = openlayers_style_delete($object);
-      break;
-
     case 'layers':
       $result = openlayers_layer_delete($object);
       break;
-
-    case 'maps':
-      $result = openlayers_map_delete($object);
-      break;
   }
 
   // Check query results
@@ -564,17 +377,9 @@ function openlayers_ui_object_export($form, $form_state, $type, $object) {
 
   // Determine type
   switch ($type) {
-    case 'styles':
-      $api = 'openlayers_styles';
-      break;
-
     case 'layers':
       $api = 'openlayers_layers';
       break;
-
-    case 'maps':
-      $api = 'openlayers_maps';
-      break;
   }
 
   // Use CTools to create export code
@@ -790,4 +595,4 @@ function openlayers_ui_style_preview($style, $crosshairs = FALSE) {
 
   // TODO: This should probably be in a theme function.
   return '<div id="' . $style->name . '"class="openlayers-ui-style-preview"></div>';
-}
\ No newline at end of file
+}
diff --git a/openlayers.install b/openlayers.install
index ece5fd4..fecff3d 100644
--- a/openlayers.install
+++ b/openlayers.install
@@ -296,4 +296,4 @@ function openlayers_update_7201() {
   // Set default map variable
   variable_set('openlayers_default_map', variable_get('openlayers_default_preset', 'default'));
   variable_del('openlayers_default_preset');
-}
\ No newline at end of file
+}
diff --git a/openlayers.module b/openlayers.module
index a94b880..3d94d7e 100644
--- a/openlayers.module
+++ b/openlayers.module
@@ -72,6 +72,9 @@ function openlayers_ctools_plugin_directory($module, $plugin) {
   if ($plugin == 'content_types' && !empty($plugin)) {
     return 'includes/' . $plugin;
   }
+  if ($module == 'ctools' && $plugin == 'export_ui') {
+    return 'plugins/' . $plugin;
+  }
   if ($module == 'openlayers' && !empty($plugin)) {
     return 'plugins/' . $plugin;
   }
diff --git a/plugins/export_ui/openlayers_layers_ctools_export_ui.inc b/plugins/export_ui/openlayers_layers_ctools_export_ui.inc
new file mode 100644
index 0000000..aa271bf
--- /dev/null
+++ b/plugins/export_ui/openlayers_layers_ctools_export_ui.inc
@@ -0,0 +1,52 @@
+<?php
+/**
+ * Define this Export UI plugin.
+ */
+$plugin = array(
+  'schema' => 'openlayers_layers',
+  'access' => 'administer openlayers',
+  'menu' => array(
+    'menu prefix' => 'admin/structure/openlayers',
+    'menu item' => 'layers',
+    'menu title' => 'OpenLayers Layers',
+    'menu description' => 'Administer OpenLayers Layers presets.',
+  ),
+
+  'handler' => array(
+    'class' => 'openlayers_layers_ui',
+  ),
+
+  'title singular' => t('layer'),
+  'title plural' => t('layers'),
+  'title singular proper' => t('OpenLayers Layer preset'),
+  'title plural proper' => t('OpenLayers Layers presets'),
+
+  'form' => array(
+    'settings' => 'openlayers_layers_ctools_export_ui_form_settings',
+  ),
+);
+
+?>
+
+<?php
+
+/**
+ * Define the preset add/edit form.
+ */
+function openlayers_layers_ctools_export_ui_form_settings(&$form, &$form_state) {
+  $layer = $form_state['item'];
+  module_load_include('inc', 'openlayers_ui', 'includes/openlayers_ui.layers');
+
+  if ($form_state['op'] == 'add') {
+    $form = openlayers_ui_layers_form($form_state);
+  }
+
+  if ($form_state['op'] == 'edit') {
+    $form = openlayers_ui_layers_form($form_state, $layer);
+  }
+}
+
+
+
+
+?>
diff --git a/plugins/export_ui/openlayers_layers_ui.class.php b/plugins/export_ui/openlayers_layers_ui.class.php
new file mode 100644
index 0000000..109e78c
--- /dev/null
+++ b/plugins/export_ui/openlayers_layers_ui.class.php
@@ -0,0 +1,15 @@
+<?php
+/**
+ * @file
+ */
+
+class openlayers_layers_ui extends ctools_export_ui {
+
+  /**
+   * Prepare the tag values before they are added to the database.
+   */
+  function edit_form_submit(&$form, &$form_state) {
+    parent::edit_form_submit($form, $form_state);
+  }
+
+}
diff --git a/plugins/export_ui/openlayers_maps_ctools_export_ui.inc b/plugins/export_ui/openlayers_maps_ctools_export_ui.inc
new file mode 100644
index 0000000..d764b43
--- /dev/null
+++ b/plugins/export_ui/openlayers_maps_ctools_export_ui.inc
@@ -0,0 +1,102 @@
+<?php
+/**
+ * Define this Export UI plugin.
+ */
+$plugin = array(
+  'schema' => 'openlayers_maps',
+  'access' => 'administer openlayers',
+  'menu' => array(
+    'menu prefix' => 'admin/structure/openlayers',
+    'menu item' => 'maps',
+    'menu title' => 'Maps',
+    'menu description' => 'Administer OpenLayers Maps presets.',
+  ),
+
+  'handler' => array(
+    'class' => 'openlayers_maps_ui'
+  ),
+
+  'title singular' => t('map'),
+  'title plural' => t('maps'),
+  'title singular proper' => t('Openlayers Map preset'),
+  'title plural proper' => t('Openlayers Maps presets'),
+
+  'form' => array(
+    'settings' => 'openlayers_maps_ctools_export_ui_form_settings',
+    'validate' => 'openlayers_maps_ctools_export_ui_form_validate',
+  ),
+);
+
+?>
+
+<?php
+
+/**
+ * Define the preset add/edit form.
+ */
+function openlayers_maps_ctools_export_ui_form_settings(&$form, &$form_state) {
+  $map = $form_state['item'];
+  if (empty($map->name)) {
+    $map = NULL;
+  }
+
+  module_load_include('inc', 'openlayers_ui', 'includes/openlayers_ui.maps');
+
+  if ($form_state['op'] == 'add') {
+    $form = openlayers_ui_maps_form($form, $form_state, $map, FALSE);
+  }
+
+  if ($form_state['op'] == 'edit') {
+    $form = openlayers_ui_maps_form($form, $form_state, $map, TRUE);
+  }
+}
+
+function openlayers_maps_ctools_export_ui_form_validate($form, $form_state) {
+  $values = $form_state['values'];
+  $found_error = FALSE;
+
+  // Check if ahah submitting
+  if ($form_state['clicked_button']['#id'] == 'edit-openlayers-projection-ahah' ||
+    $form_state['clicked_button']['#id'] == 'edit-openlayers-cancel') {
+    return TRUE;
+  }
+
+  // Check map name first
+  if (!preg_match('!^[a-z0-9_]+$!', $values['name'])) {
+    form_set_error('name', t('Map Name must contain only lowercase letters, numbers, and underscores.'));
+    $found_error = TRUE;
+  }
+
+  // Check if adding and name already exists
+  $existing = openlayers_map_load($values['name'], TRUE);
+  if (!empty($existing) && (!$form_state['values']['map_edit']) &&
+    ($existing->export_type & EXPORT_IN_DATABASE)) {
+    form_set_error('name', t('The Map Name already exists.'));
+    $found_error = TRUE;
+  }
+
+  // Attempt to render map to find any errors
+  $map = openlayers_ui_maps_form_process($form_state['values']);
+  $map = openlayers_build_map($map);
+  // Check if any errors found
+  if (is_array($map['errors']) && count($map['errors']) > 0) {
+    foreach ($map['errors'] as $error) {
+      form_set_error('openlayers',
+        t('OpenLayers Map Rendering Error: !error', array('!error' => $error)));
+      $found_error = TRUE;
+    }
+  }
+
+  // If found error, rebuild form
+  if ($found_error) {
+    // Add OpenLayers CSS & Javascript, because the form function will not be run on rebuild
+    openlayers_include();
+    drupal_add_js(drupal_get_path('module', 'openlayers_ui') .
+      '/js/openlayers_ui.maps.js', 'module');
+    drupal_add_css(drupal_get_path('module', 'openlayers_ui') .
+      '/openlayers_ui.css');
+    $form_state['rebuild'] = TRUE;
+  }
+}
+
+?>
diff --git a/plugins/export_ui/openlayers_maps_ui.class.php b/plugins/export_ui/openlayers_maps_ui.class.php
new file mode 100644
index 0000000..8493287
--- /dev/null
+++ b/plugins/export_ui/openlayers_maps_ui.class.php
@@ -0,0 +1,101 @@
+<?php
+/**
+ * @file
+ */
+
+class openlayers_maps_ui extends ctools_export_ui {
+
+  /**
+   * Prepare the tag values before they are added to the database.
+   */
+  function edit_form_submit(&$form, &$form_state) {
+    $map_data = openlayers_ui_maps_form_process($form_state['values']);
+    $form_state['values']['data'] = $map_data;
+    parent::edit_form_submit($form, $form_state);
+  }
+
+  /**
+   * Build a row based on the item.
+   *
+   * By default all of the rows are placed into a table by the render
+   * method, so this is building up a row suitable for theme('table').
+   * This doesn't have to be true if you override both.
+   */
+  function list_build_row($item, &$form_state, $operations) {
+    // Set up sorting
+    $name = $item->{$this->plugin['export']['key']};
+    $schema = ctools_export_get_schema($this->plugin['schema']);
+
+    // Note: $item->{$schema['export']['export type string']} should have already been set up by export.inc so
+    // we can use it safely.
+    switch ($form_state['values']['order']) {
+      case 'disabled':
+        $this->sorts[$name] = empty($item->disabled) . $name;
+        break;
+      case 'title':
+        $this->sorts[$name] = $item->{$this->plugin['export']['admin_title']};
+        break;
+      case 'name':
+        $this->sorts[$name] = $name;
+        break;
+      case 'storage':
+        $this->sorts[$name] = $item->{$schema['export']['export type string']} . $name;
+        break;
+    }
+
+    $this->rows[$name]['data'] = array();
+    $this->rows[$name]['class'] = !empty($item->disabled) ? array('ctools-export-ui-disabled') : array('ctools-export-ui-enabled');
+
+    // If we have an admin title, make it the first row.
+    if (!empty($this->plugin['export']['admin_title'])) {
+      $this->rows[$name]['data'][] = array('data' => check_plain($item->{$this->plugin['export']['admin_title']}), 'class' => array('ctools-export-ui-title'));
+    }
+
+    $this->rows[$name]['data'][] = array('data' => check_plain($name), 'class' => array('ctools-export-ui-name'));
+    $this->rows[$name]['data'][] = array('data' => $item->title, 'class' => array('ctools-export-ui-title'));
+    $this->rows[$name]['data'][] = array('data' => $item->description, 'class' => array('ctools-export-ui-description'));
+    $this->rows[$name]['data'][] = array('data' => check_plain($item->{$schema['export']['export type string']}), 'class' => array('ctools-export-ui-storage'));
+
+    $ops = theme('links__ctools_dropbutton', array('links' => $operations, 'attributes' => array('class' => array('links', 'inline'))));
+
+    $this->rows[$name]['data'][] = array('data' => $ops, 'class' => array('ctools-export-ui-operations'));
+
+    // Add an automatic mouseover of the description if one exists.
+    if (!empty($this->plugin['export']['admin_description'])) {
+      $this->rows[$name]['title'] = $item->{$this->plugin['export']['admin_description']};
+    }
+  }
+
+  /**
+   * Provide the table header.
+   *
+   * If you've added columns via list_build_row() but are still using a
+   * table, override this method to set up the table header.
+   */
+  function list_table_header() {
+    $header = array();
+    if (!empty($this->plugin['export']['admin_title'])) {
+      $header[] = array('data' => t('Title'), 'class' => array('ctools-export-ui-title'));
+    }
+
+    $header[] = array('data' => t('Name'), 'class' => array('ctools-export-ui-name'));
+    $header[] = array('data' => t('Title'), 'class' => array('ctools-export-ui-title'));
+    $header[] = array('data' => t('Description'), 'class' => array('ctools-export-ui-description'));
+    $header[] = array('data' => t('Storage'), 'class' => array('ctools-export-ui-storage'));
+    $header[] = array('data' => t('Operations'), 'class' => array('ctools-export-ui-operations'));
+
+    return $header;
+  }
+
+  /**
+   * hook_menu() entry point.
+   *
+   * Child implementations that need to add or modify menu items should
+   * probably call parent::hook_menu($items) and then modify as needed.
+   */
+  function hook_menu(&$items) {
+    parent::hook_menu($items);
+    $items['admin/structure/openlayers/maps']['type'] = MENU_LOCAL_TASK;
+  }
+
+}
diff --git a/plugins/export_ui/openlayers_styles_ctools_export_ui.inc b/plugins/export_ui/openlayers_styles_ctools_export_ui.inc
new file mode 100644
index 0000000..dd75c3d
--- /dev/null
+++ b/plugins/export_ui/openlayers_styles_ctools_export_ui.inc
@@ -0,0 +1,54 @@
+<?php
+/**
+ * Define this Export UI plugin.
+ */
+$plugin = array(
+  'schema' => 'openlayers_styles',
+  'access' => 'administer openlayers',
+  'menu' => array(
+    'menu prefix' => 'admin/structure/openlayers',
+    'menu item' => 'styles',
+    'menu title' => 'Styles',
+    'menu description' => 'Administer OpenLayers Styles presets.',
+  ),
+
+  'handler' => array(
+    'class' => 'openlayers_styles_ui',
+  ),
+
+  'title singular' => t('style'),
+  'title plural' => t('styles'),
+  'title singular proper' => t('OpenLayers Style preset'),
+  'title plural proper' => t('OpenLayers Styles presets'),
+
+  'form' => array(
+    'settings' => 'openlayers_styles_ctools_export_ui_form_settings',
+  ),
+);
+
+?>
+
+<?php
+
+/**
+ * Define the preset add/edit form.
+ */
+function openlayers_styles_ctools_export_ui_form_settings(&$form, &$form_state) {
+  $style = $form_state['item'];
+  if (empty($style->name)) {
+    $style = NULL;
+  }
+
+  module_load_include('inc', 'openlayers_ui', 'includes/openlayers_ui.styles');
+
+  if ($form_state['op'] == 'add') {
+    $form = openlayers_ui_styles_form($form, $form_state, $style, FALSE);
+  }
+
+  if ($form_state['op'] == 'edit') {
+    $form = openlayers_ui_styles_form($form, $form_state, $style, TRUE);
+  }
+}
+
+
+?>
diff --git a/plugins/export_ui/openlayers_styles_ui.class.php b/plugins/export_ui/openlayers_styles_ui.class.php
new file mode 100644
index 0000000..7cf56d4
--- /dev/null
+++ b/plugins/export_ui/openlayers_styles_ui.class.php
@@ -0,0 +1,133 @@
+<?php
+/**
+ * @file
+ */
+
+class openlayers_styles_ui extends ctools_export_ui {
+
+  /**
+   * Prepare the tag values before they are added to the database.
+   */
+  function edit_form_submit(&$form, &$form_state) {
+    $style_data = $form_state['values']['style_data'];
+
+    // Cast and unset values so JS can handle them better,
+    // Unless values are in the form ${attribute}
+    foreach ($form_state['values']['data'] as $key => $value) {
+      if ($form_state['values']['data'][$key] === '') {
+        unset($form_state['values']['data'][$key]);
+      }
+      elseif (isset($style_data[$key]['type']) &&
+        strpos($form_state['values']['data'][$key], '${') !== 0) {
+        if ($style_data[$key]['type'] == 'integer') {
+          $form_state['values']['data'][$key] =
+            (int) $form_state['values']['data'][$key];
+        }
+        elseif ($style_data[$key]['type'] == 'float') {
+          $form_state['values']['data'][$key] =
+            (float) $form_state['values']['data'][$key];
+        }
+      }
+    }
+
+    parent::edit_form_submit($form, $form_state);
+  }
+
+  /**
+   * Build a row based on the item.
+   *
+   * By default all of the rows are placed into a table by the render
+   * method, so this is building up a row suitable for theme('table').
+   * This doesn't have to be true if you override both.
+   */
+  function list_build_row($item, &$form_state, $operations) {
+    // Set up sorting
+    $name = $item->{$this->plugin['export']['key']};
+    $schema = ctools_export_get_schema($this->plugin['schema']);
+
+    // Note: $item->{$schema['export']['export type string']} should have already been set up by export.inc so
+    // we can use it safely.
+    switch ($form_state['values']['order']) {
+      case 'disabled':
+        $this->sorts[$name] = empty($item->disabled) . $name;
+        break;
+      case 'title':
+        $this->sorts[$name] = $item->{$this->plugin['export']['admin_title']};
+        break;
+      case 'name':
+        $this->sorts[$name] = $name;
+        break;
+      case 'storage':
+        $this->sorts[$name] = $item->{$schema['export']['export type string']} . $name;
+        break;
+    }
+
+    $this->rows[$name]['data'] = array();
+    $this->rows[$name]['class'] = !empty($item->disabled) ? array('ctools-export-ui-disabled') : array('ctools-export-ui-enabled');
+
+    // If we have an admin title, make it the first row.
+    if (!empty($this->plugin['export']['admin_title'])) {
+      $this->rows[$name]['data'][] = array('data' => check_plain($item->{$this->plugin['export']['admin_title']}), 'class' => array('ctools-export-ui-title'));
+    }
+
+    // Add the icon as an image in the row if it exists.
+    if (isset($item->data['externalGraphic'])) {
+      $style = theme('image', array('path' => $item->data['externalGraphic']));
+    }
+    else {
+      $style = openlayers_ui_style_preview($item);
+    }
+
+    $this->rows[$name]['data'][] = array('data' => $style, 'class' => array('ctools-export-ui-preview'));
+    $this->rows[$name]['data'][] = array('data' => check_plain($name), 'class' => array('ctools-export-ui-name'));
+    $this->rows[$name]['data'][] = array('data' => $item->title, 'class' => array('ctools-export-ui-title'));
+    $this->rows[$name]['data'][] = array('data' => $item->description, 'class' => array('ctools-export-ui-description'));
+    $this->rows[$name]['data'][] = array('data' => check_plain($item->{$schema['export']['export type string']}), 'class' => array('ctools-export-ui-storage'));
+
+    $ops = theme('links__ctools_dropbutton', array('links' => $operations, 'attributes' => array('class' => array('links', 'inline'))));
+
+    $this->rows[$name]['data'][] = array('data' => $ops, 'class' => array('ctools-export-ui-operations'));
+
+    // Add an automatic mouseover of the description if one exists.
+    if (!empty($this->plugin['export']['admin_description'])) {
+      $this->rows[$name]['title'] = $item->{$this->plugin['export']['admin_description']};
+    }
+  }
+
+
+  /**
+   * Provide the table header.
+   *
+   * If you've added columns via list_build_row() but are still using a
+   * table, override this method to set up the table header.
+   */
+  function list_table_header() {
+    $header = array();
+    if (!empty($this->plugin['export']['admin_title'])) {
+      $header[] = array('data' => t('Title'), 'class' => array('ctools-export-ui-title'));
+    }
+
+    $header[] = array('data' => t('Preview'), 'class' => array('ctools-export-ui-preview'));
+    $header[] = array('data' => t('Name'), 'class' => array('ctools-export-ui-name'));
+    $header[] = array('data' => t('Title'), 'class' => array('ctools-export-ui-title'));
+    $header[] = array('data' => t('Description'), 'class' => array('ctools-export-ui-description'));
+    $header[] = array('data' => t('Storage'), 'class' => array('ctools-export-ui-storage'));
+    $header[] = array('data' => t('Operations'), 'class' => array('ctools-export-ui-operations'));
+
+    return $header;
+  }
+
+  /**
+   * hook_menu() entry point.
+   *
+   * Child implementations that need to add or modify menu items should
+   * probably call parent::hook_menu($items) and then modify as needed.
+   */
+  function hook_menu(&$items) {
+    parent::hook_menu($items);
+    $items['admin/structure/openlayers/styles']['type'] = MENU_LOCAL_TASK;
+  }
+
+
+
+}
-- 
1.7.3.4

