PageRenderTime 26ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/openlayers_ui/plugins/export_ui/openlayers_layers_ui.class.php

https://bitbucket.org/asanchez75/openlayers
PHP | 312 lines | 195 code | 50 blank | 67 comment | 31 complexity | 3f0b24e92eb920036c155f0f79ccdc59 MD5 | raw file
  1. <?php
  2. /**
  3. * @file
  4. */
  5. class openlayers_layers_ui extends ctools_export_ui {
  6. function edit_form(&$form, &$form_state) {
  7. parent::edit_form($form, $form_state);
  8. $layer_types = openlayers_layer_types();
  9. $options = array('' => t('Select the layer type'));
  10. $layer = NULL;
  11. if ($form_state['op'] == 'edit' && $form_state['form type'] == 'edit') {
  12. $layer = openlayers_layer_load($form_state['item']->name);
  13. }
  14. if ($form_state['op'] == 'add' && $form_state['form type'] == 'clone') {
  15. $layer = openlayers_layer_load($form_state['function args'][2]->name);
  16. $layer->title = t('Clone of ') . $layer->title;
  17. }
  18. $form['info']['title'] = array(
  19. '#id' => 'layertitle',
  20. '#type' => 'textfield',
  21. '#weight' => -1,
  22. '#title' => t('Layer Title'),
  23. '#default_value' => isset($layer->title) ? $layer->title : '',
  24. '#description' => t('The friendly name of your layer, which will appear in the administration interface as well on the map interface if it is exposed.'),
  25. );
  26. $form['info']['name']['#machine_name']['source'] = array('info', 'title');
  27. $layers_options = array();
  28. // Go trough all layer types and get each options form.
  29. foreach ($layer_types as $layer_type) {
  30. if (is_object($layer) && get_class($layer) == $layer_type['layer_type']['class']) {
  31. // Layer is of current layer type.
  32. $layer_type_object = $layer;
  33. } else {
  34. // Otherwise load layer object for current layer type.
  35. $layer_type_object = openlayers_layer_type_load($layer_type['name']);
  36. }
  37. $layer_options_form = array();
  38. if (method_exists($layer_type_object, 'options_form')) {
  39. // This is because the class openlayers_layer_type
  40. // is not abstract.
  41. // Maybe there's a better way to do ?
  42. $parent = get_parent_class($layer_type_object);
  43. $parent_object = new $parent;
  44. $layer_options_form = $layer_type_object->options_form() + $parent_object->options_form($layer);
  45. }
  46. // TODO First case can never be true without crashing earlier
  47. if ($layer_type_object == FALSE || empty($layer_options_form)) {
  48. continue;
  49. }
  50. $layers_option = array(
  51. '#type' => 'fieldset',
  52. '#tree' => TRUE,
  53. '#title' => t('Layer specific options for @layer_title', array('@layer_title' => $layer_type['title'])),
  54. );
  55. $layers_option += $layer_options_form;
  56. $layers_option['#states'] = array(
  57. 'visible' => array(
  58. ':input[name="layer_type"]' => array('value' => $layer_type['name']),
  59. ),
  60. );
  61. $layers_option['layer_type'] = array(
  62. '#type' => 'hidden',
  63. '#value' => $layer_type['name'],
  64. );
  65. $layers_options[$layer_type['name']] = $layers_option;
  66. $options[$layer_type['name']] = $layer_type['title'];
  67. }
  68. $form['layer_type'] = array(
  69. '#type' => 'select',
  70. '#title' => t('Layer Type'),
  71. '#default_value' => isset($layer->data['layer_type']) ? $layer->data['layer_type']: '',
  72. '#description' => t('Select the type of layer.'),
  73. '#options' => $options,
  74. );
  75. $form += $layers_options;
  76. $form['buttons']['submit']['#weight'] = 0;
  77. $form['buttons']['delete']['#weight'] = 20;
  78. $form['buttons']['saveandedit'] = array(
  79. '#type' => 'submit',
  80. '#value' => t('Save and edit'),
  81. '#weight' => 10
  82. );
  83. }
  84. function edit_form_validate(&$form, &$form_state) {
  85. ctools_get_plugins('openlayers', 'layer_types');
  86. $layer = openlayers_layer_type_load($form_state['values']['layer_type']);
  87. $form_state['values']['data'] = $form_state['values'][$form_state['values']['layer_type']];
  88. if (empty($form_state['values']['layer_type'])) {
  89. form_set_error('layer_type', 'Layer type cannot be empty.');
  90. }
  91. $parent = get_parent_class($layer);
  92. $parent_object = new $parent;
  93. $form_state['values']['data'] += $layer->options_init();
  94. $form_state['values']['data'] += $parent_object->options_init();
  95. $layer_types = openlayers_layer_types();
  96. foreach($layer_types as $layer_type) {
  97. unset($form_state['values'][$layer_type['name']]);
  98. }
  99. unset($form_state['values']['layer_type']);
  100. if (method_exists($layer, 'options_form_validate')) {
  101. $layer->options_form_validate($form, $form_state['values']);
  102. }
  103. parent::edit_form_validate($form, $form_state);
  104. }
  105. /**
  106. * Prepare the tag values before they are added to the database.
  107. */
  108. function edit_form_submit(&$form, &$form_state) {
  109. $layer = openlayers_layer_type_load($form_state['values']['data']['layer_type']);
  110. $layer->options_form_submit($form, $form_state);
  111. parent::edit_form_submit($form, $form_state);
  112. }
  113. /**
  114. * Implements ctools_export_ui::edit_execute_form().
  115. *
  116. * This is hacky, but since CTools Export UI uses drupal_goto() we have to
  117. * effectively change the plugin to modify the redirect path dynamically.
  118. */
  119. function edit_execute_form(&$form_state) {
  120. $output = parent::edit_execute_form($form_state);
  121. if (!empty($form_state['executed'])) {
  122. $clicked = $form_state['clicked_button']['#value'];
  123. if (t('Save and edit') == $clicked) {
  124. // We always want to redirect back to this page when adding an item,
  125. // but we want to preserve the destination so we can be redirected back
  126. // to where we came from after clicking "Save".
  127. $options = array();
  128. if (!empty($_GET['destination'])) {
  129. $options['query']['destination'] = $_GET['destination'];
  130. unset($_GET['destination']);
  131. }
  132. // Sets redirect path and options.
  133. $op = $form_state['op'];
  134. $name = $form_state['values']['name'];
  135. $path = ('add' != $op) ? current_path() : 'admin/structure/openlayers/layers/list/' . $name . '/edit';
  136. $this->plugin['redirect'][$op] = array($path, $options);
  137. }
  138. }
  139. return $output;
  140. }
  141. /**
  142. * Deletes exportable items from the database.
  143. */
  144. function delete_form_submit(&$form_state) {
  145. $item = $form_state['item'];
  146. $layer = openlayers_layer_type_load($item->data['layer_type']);
  147. if (method_exists($layer, 'delete')) {
  148. $layer->delete($item);
  149. }
  150. parent::delete_form_submit($form_state);
  151. }
  152. /**
  153. * hook_menu() entry point.
  154. *
  155. * Child implementations that need to add or modify menu items should
  156. * probably call parent::hook_menu($items) and then modify as needed.
  157. */
  158. function hook_menu(&$items) {
  159. parent::hook_menu($items);
  160. $items['admin/structure/openlayers/layers']['type'] = MENU_LOCAL_TASK;
  161. }
  162. /**
  163. * Build a row based on the item.
  164. *
  165. * By default all of the rows are placed into a table by the render
  166. * method, so this is building up a row suitable for theme('table').
  167. * This doesn't have to be true if you override both.
  168. */
  169. function list_build_row($item, &$form_state, $operations) {
  170. // Set up sorting
  171. $name = $item->{$this->plugin['export']['key']};
  172. $schema = ctools_export_get_schema($this->plugin['schema']);
  173. $layers_types = openlayers_layer_types();
  174. if (!isset($layers_types[$item->data['layer_type']])) {
  175. return;
  176. }
  177. // Note: $item->{$schema['export']['export type string']} should have already been set up by export.inc so
  178. // we can use it safely.
  179. switch ($form_state['values']['order']) {
  180. case 'disabled':
  181. $this->sorts[$name] = empty($item->disabled) . $name;
  182. break;
  183. case 'title':
  184. $this->sorts[$name] = $item->{$this->plugin['export']['admin_title']};
  185. break;
  186. case 'name':
  187. $this->sorts[$name] = $name;
  188. break;
  189. case 'storage':
  190. $this->sorts[$name] = $item->{$schema['export']['export type string']} . $name;
  191. break;
  192. }
  193. $this->rows[$name]['data'] = array();
  194. $this->rows[$name]['class'] = !empty($item->disabled) ? array('ctools-export-ui-disabled') : array('ctools-export-ui-enabled');
  195. // If we have an admin title, make it the first row.
  196. if (!empty($this->plugin['export']['admin_title'])) {
  197. $this->rows[$name]['data'][] = array('data' => check_plain($item->{$this->plugin['export']['admin_title']}), 'class' => array('ctools-export-ui-title'));
  198. }
  199. $this->rows[$name]['data'][] = array('data' => $item->title, 'class' => array('ctools-export-ui-title'));
  200. $this->rows[$name]['data'][] = array('data' => $layers_types[$item->data['layer_type']]['title'], 'class' => array('ctools-export-ui-layer-type'));
  201. $this->rows[$name]['data'][] = array('data' => $item->description, 'class' => array('ctools-export-ui-description'));
  202. $this->rows[$name]['data'][] = array('data' => check_plain($item->{$schema['export']['export type string']}), 'class' => array('ctools-export-ui-storage'));
  203. // This should be in the module openlayers_views, but I'm still looking
  204. // for a solution to do it properly.
  205. // Temporarily removed.
  206. /*
  207. if ($item->data['layer_type'] == 'openlayers_views_vector') {
  208. $operations['edit']['href'] = 'admin/structure/views/view/' . $item->data['views']['view'] . '/edit/' . $item->data['views']['display'];
  209. $operations['disable']['href'] = 'admin/structure/views/view/' . $item->data['views']['view'] . '/disable/' . $item->data['views']['display'];
  210. $operations['clone']['href'] = 'admin/structure/views/view/' . $item->data['views']['view'] . '/clone/' . $item->data['views']['display'];
  211. $operations['export']['href'] = 'admin/structure/views/view/' . $item->data['views']['view'] . '/export/' . $item->data['views']['display'];
  212. }
  213. */
  214. $ops = theme('links__ctools_dropbutton', array('links' => $operations, 'attributes' => array('class' => array('links', 'inline'))));
  215. $this->rows[$name]['data'][] = array('data' => $ops, 'class' => array('ctools-export-ui-operations'));
  216. // Add an automatic mouseover of the description if one exists.
  217. if (!empty($this->plugin['export']['admin_description'])) {
  218. $this->rows[$name]['title'] = $item->{$this->plugin['export']['admin_description']};
  219. }
  220. }
  221. /**
  222. * Provide the table header.
  223. *
  224. * If you've added columns via list_build_row() but are still using a
  225. * table, override this method to set up the table header.
  226. */
  227. function list_table_header() {
  228. $header = array();
  229. if (!empty($this->plugin['export']['admin_title'])) {
  230. $header[] = array('data' => t('Name'), 'class' => array('ctools-export-ui-name'));
  231. }
  232. $header[] = array('data' => t('Title'), 'class' => array('ctools-export-ui-title'));
  233. $header[] = array('data' => t('Type'), 'class' => array('ctools-export-ui-layer-type'));
  234. $header[] = array('data' => t('Description'), 'class' => array('ctools-export-ui-description'));
  235. $header[] = array('data' => t('Storage'), 'class' => array('ctools-export-ui-storage'));
  236. $header[] = array('data' => t('Operations'), 'class' => array('ctools-export-ui-operations'));
  237. return $header;
  238. }
  239. /**
  240. * Provide a list of sort options.
  241. *
  242. * Override this if you wish to provide more or change how these work.
  243. * The actual handling of the sorting will happen in build_row().
  244. */
  245. function list_sort_options() {
  246. if (!empty($this->plugin['export']['admin_title'])) {
  247. $options = array(
  248. 'disabled' => t('Enabled, title'),
  249. $this->plugin['export']['admin_title'] => t('Title'),
  250. );
  251. }
  252. else {
  253. $options = array(
  254. 'disabled' => t('Enabled, title'),
  255. );
  256. }
  257. $options += array(
  258. 'storage' => t('Storage'),
  259. );
  260. return $options;
  261. }
  262. }