PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/sites/all/modules/openlayers/src/Plugin/Map/OLMap/OLMap.php

https://gitlab.com/leoplanxxi/dr7-web-buap-2016
PHP | 366 lines | 312 code | 26 blank | 28 comment | 19 complexity | 23d2df11bb206b152bebccba5d7da8cc MD5 | raw file
  1. <?php
  2. /**
  3. * @file
  4. * Map: Map.
  5. */
  6. namespace Drupal\openlayers\Plugin\Map\OLMap;
  7. use Drupal\openlayers\Config;
  8. use Drupal\openlayers\Openlayers;
  9. use Drupal\openlayers\Types\Map;
  10. /**
  11. * Class OLMap.
  12. *
  13. * @OpenlayersPlugin(
  14. * id = "OLMap"
  15. * )
  16. */
  17. class OLMap extends Map {
  18. /**
  19. * {@inheritdoc}
  20. */
  21. public function optionsForm(array &$form, array &$form_state) {
  22. $form['options']['ui'] = array(
  23. '#type' => 'fieldset',
  24. '#title' => t('User interface'),
  25. 'width' => array(
  26. '#type' => 'textfield',
  27. '#title' => 'Width of the map',
  28. '#default_value' => $this->getOption('width', 'auto'),
  29. '#parents' => array('options', 'width'),
  30. ),
  31. 'height' => array(
  32. '#type' => 'textfield',
  33. '#title' => t('Height of the map'),
  34. '#default_value' => $this->getOption('height', '300px'),
  35. '#parents' => array('options', 'height'),
  36. ),
  37. );
  38. $form['options']['view'] = array(
  39. '#type' => 'fieldset',
  40. '#title' => t('Center and rotation'),
  41. '#tree' => TRUE,
  42. );
  43. if ($this->getMachineName() != Config::get('openlayers.edit_view_map')) {
  44. if (($map = Openlayers::load('Map', Config::get('openlayers.edit_view_map'))) == TRUE) {
  45. $map_configuration = $map->getConfiguration();
  46. if ($view = $this->getOption('view')) {
  47. // Don't apply min / max zoom settings to this map to avoid lock-in.
  48. $view['minZoom'] = 0;
  49. $view['maxZoom'] = 0;
  50. $map->setOption('view', $view);
  51. }
  52. $form['options']['view']['map'] = array(
  53. '#type' => 'openlayers',
  54. '#description' => $map->getPluginDescription(),
  55. '#map' => $map,
  56. );
  57. }
  58. }
  59. $form['options']['view']['center'] = array(
  60. '#tree' => TRUE,
  61. 'lat' => array(
  62. '#type' => 'textfield',
  63. '#title' => t('Latitude'),
  64. '#default_value' => $this->getOption(array('view', 'center', 'lat'), 0),
  65. ),
  66. 'lon' => array(
  67. '#type' => 'textfield',
  68. '#title' => t('Longitude'),
  69. '#default_value' => $this->getOption(array('view', 'center', 'lat'), 0),
  70. ),
  71. );
  72. $form['options']['view']['rotation'] = array(
  73. '#type' => 'textfield',
  74. '#title' => t('Rotation'),
  75. '#default_value' => $this->getOption(array('view', 'rotation'), 0),
  76. );
  77. $form['options']['view']['zoom'] = array(
  78. '#type' => 'textfield',
  79. '#title' => t('Zoom'),
  80. '#default_value' => $this->getOption(array('view', 'zoom'), 0),
  81. );
  82. $form['options']['view']['minZoom'] = array(
  83. '#type' => 'textfield',
  84. '#title' => t('Min zoom'),
  85. '#default_value' => $this->getOption(array('view', 'minZoom'), 0),
  86. );
  87. $form['options']['view']['maxZoom'] = array(
  88. '#type' => 'textfield',
  89. '#title' => t('Max zoom'),
  90. '#default_value' => $this->getOption(array('view', 'maxZoom'), 0),
  91. );
  92. $form['options']['view']['limit_extent'] = array(
  93. '#type' => 'select',
  94. '#title' => t('Limit to extent'),
  95. '#empty_option' => t('Disabled'),
  96. '#empty_value' => '',
  97. '#options' => array('custom' => 'Custom extent', 'projection' => 'Map projection'),
  98. '#description' => t('If enabled navigation on the map is limited to the give extent.'),
  99. '#default_value' => $this->getOption(array('view', 'limit_extent'), FALSE),
  100. );
  101. $form['options']['view']['extent'] = array(
  102. '#type' => 'textfield',
  103. '#title' => t('Extent [minx, miny, maxx, maxy]'),
  104. '#default_value' => $this->getOption(array('view', 'extent'), ''),
  105. '#states' => array(
  106. 'visible' => array(
  107. ':input[name="options[view][limit_extent]"]' => array('value' => 'custom'),
  108. ),
  109. ),
  110. );
  111. $form['options']['misc'] = array(
  112. '#type' => 'fieldset',
  113. '#title' => t('Miscellaneous options'),
  114. );
  115. $form['options']['misc']['renderer'] = array(
  116. '#type' => 'radios',
  117. '#title' => t('Renderer'),
  118. '#description' => t('Renderer by default. Canvas, DOM and WebGL renderers are tested for support in that order. Note that at present only the Canvas renderer support vector data.'),
  119. '#options' => array(
  120. 'canvas' => t('Canvas'),
  121. 'dom' => t('DOM'),
  122. 'webgl' => t('WebGL'),
  123. ),
  124. '#default_value' => $this->getOption('renderer', 'canvas'),
  125. '#parents' => array('options', 'renderer'),
  126. );
  127. $i = 0;
  128. $data = array();
  129. $map_options = $this->getOptions();
  130. /** @var \Drupal\openlayers\Types\Object $object */
  131. foreach ($this->getCollection()->getFlatList() as $object) {
  132. $weight = 0;
  133. if (isset($map_options['capabilities']['options']['table'][$object->getMachineName()])) {
  134. $weight = array_search($object->getMachineName(), array_keys($map_options['capabilities']['options']['table']));
  135. }
  136. $data[$object->getMachineName()] = array(
  137. 'name' => $object->getName(),
  138. 'machine_name' => $object->getMachineName(),
  139. 'text' => isset($map_options['capabilities']['options']['table'][$object->getMachineName()]) ? $map_options['capabilities']['options']['table'][$object->getMachineName()] : $object->getPluginDescription(),
  140. 'weight' => $weight,
  141. 'enabled' => isset($map_options['capabilities']['options']['table'][$object->getMachineName()]) ? TRUE : FALSE,
  142. 'default' => 1,
  143. );
  144. }
  145. uasort($data, function($a, $b) {
  146. if ($a['enabled'] > $b['enabled']) {
  147. return -1;
  148. }
  149. elseif ($a['enabled'] < $b['enabled']) {
  150. return 1;
  151. }
  152. if ($a['weight'] < $b['weight']) {
  153. return -1;
  154. }
  155. elseif ($a['weight'] > $b['weight']) {
  156. return 1;
  157. }
  158. return 0;
  159. });
  160. $rows = array();
  161. $row_elements = array();
  162. foreach ($data as $id => $entry) {
  163. $rows[$id] = array(
  164. 'data' => array(
  165. array('class', array('entry-cross')),
  166. array(
  167. 'data' => array(
  168. '#type' => 'weight',
  169. '#title' => t('Weight'),
  170. '#title_display' => 'invisible',
  171. '#default_value' => $entry['weight'],
  172. '#attributes' => array(
  173. 'class' => array('entry-order-weight'),
  174. ),
  175. ),
  176. ),
  177. array(
  178. 'data' => array(
  179. '#type' => 'hidden',
  180. '#default_value' => $entry['machine_name'],
  181. ),
  182. ),
  183. array(
  184. 'data' => array(
  185. '#type' => 'checkbox',
  186. '#title' => t('Enable'),
  187. '#title_display' => 'invisible',
  188. '#default_value' => $entry['enabled'],
  189. ),
  190. ),
  191. array(
  192. 'data' => array(
  193. '#type' => 'textfield',
  194. '#title' => t('Text'),
  195. '#title_display' => 'invisible',
  196. '#default_value' => $entry['text'],
  197. '#maxlength' => 256,
  198. ),
  199. ),
  200. check_plain($entry['name']),
  201. check_plain($entry['machine_name']),
  202. ),
  203. 'class' => array('draggable'),
  204. );
  205. // Build rows of the form elements in the table.
  206. $row_elements[$id] = array(
  207. 'weight' => &$rows[$id]['data'][1]['data'],
  208. 'machine_name' => &$rows[$id]['data'][2]['data'],
  209. 'enabled' => &$rows[$id]['data'][3]['data'],
  210. 'text' => &$rows[$id]['data'][4]['data'],
  211. );
  212. }
  213. $form['options']['capabilities'] = array(
  214. '#type' => 'fieldset',
  215. '#title' => 'Map description and capabilities',
  216. '#collapsible' => FALSE,
  217. '#collapsed' => FALSE,
  218. 'enabled' => array(
  219. '#type' => 'checkbox',
  220. '#title' => t('Enable map capabilities ?'),
  221. '#default_value' => (bool) $this->getOption(array('capabilities'), FALSE),
  222. ),
  223. 'options' => array(
  224. '#type' => 'container',
  225. '#states' => array(
  226. 'visible' => array(
  227. ':input[name="options[capabilities][enabled]"]' => array('checked' => TRUE),
  228. ),
  229. ),
  230. 'container_type' => array(
  231. '#type' => 'select',
  232. '#title' => t('Container type'),
  233. '#options' => array(
  234. 'fieldset' => 'Fieldset',
  235. 'container' => 'Simple div',
  236. ),
  237. '#default_value' => $this->getOption(array('capabilities', 'options', 'container_type'), 'fieldset'),
  238. ),
  239. 'title' => array(
  240. '#type' => 'textfield',
  241. '#title' => t('Title'),
  242. '#description' => t('Show a title ? Empty to disable.'),
  243. '#default_value' => $this->getOption(array('capabilities', 'options', 'title'), t('Map capabilities')),
  244. '#states' => array(
  245. 'visible' => array(
  246. ':input[name="options[capabilities][options][container_type]"]' => array('value' => 'fieldset'),
  247. ),
  248. ),
  249. ),
  250. 'description' => array(
  251. '#type' => 'textfield',
  252. '#title' => t('Description'),
  253. '#description' => t('Show a description ? Empty to disable.'),
  254. '#default_value' => $this->getOption(array('capabilities', 'options', 'description'), t('Description')),
  255. '#states' => array(
  256. 'visible' => array(
  257. ':input[name="options[capabilities][options][container_type]"]' => array('value' => 'fieldset'),
  258. ),
  259. ),
  260. ),
  261. 'collapsible' => array(
  262. '#type' => 'checkbox',
  263. '#title' => t('Collapsible'),
  264. '#default_value' => (bool) $this->getOption(array('capabilities', 'options', 'collapsible'), TRUE),
  265. '#states' => array(
  266. 'visible' => array(
  267. ':input[name="options[capabilities][options][container_type]"]' => array('value' => 'fieldset'),
  268. ),
  269. ),
  270. ),
  271. 'collapsed' => array(
  272. '#type' => 'checkbox',
  273. '#title' => t('Collapsed'),
  274. '#default_value' => (bool) $this->getOption(array('capabilities', 'options', 'collapsed'), TRUE),
  275. '#states' => array(
  276. 'visible' => array(
  277. ':input[name="options[capabilities][options][container_type]"]' => array('value' => 'fieldset'),
  278. ),
  279. ),
  280. ),
  281. ),
  282. );
  283. // Add the table to the form.
  284. $form['options']['capabilities']['options']['table'] = array(
  285. '#theme' => 'table',
  286. // The row form elements need to be processed and build,
  287. // therefore pass them as element children.
  288. 'elements' => $row_elements,
  289. '#header' => array(
  290. // We need two empty columns for the weigth field and the cross.
  291. array('data' => NULL, 'colspan' => 2),
  292. array('data' => t('Enabled'), 'colspan' => 2),
  293. array('data' => t('Description'), 'colspan' => 1),
  294. t('Name'),
  295. t('Machine name'),
  296. ),
  297. '#rows' => $rows,
  298. '#empty' => t('There are no entries available.'),
  299. '#attributes' => array('id' => 'entry-order-objects'),
  300. );
  301. drupal_add_tabledrag('entry-order-objects', 'order', 'sibling', 'entry-order-weight');
  302. }
  303. /**
  304. * {@inheritdoc}
  305. */
  306. public function optionsFormSubmit(array $form, array &$form_state) {
  307. // So we can use the map API instead of working with arrays.
  308. parent::optionsFormSubmit($form, $form_state);
  309. $limit_extent = $this->getOption(array('view', 'limit_extent'), '');
  310. $extent = $this->getOption(array('view', 'extent'), '');
  311. if (empty($limit_extent) || ($limit_extent == 'custom' && empty($extent))) {
  312. unset($form_state['values']['options']['view']['extent']);
  313. unset($form_state['values']['options']['view']['limit_extent']);
  314. }
  315. $capabilities = array();
  316. if (isset($form_state['values']['options']['capabilities']['enabled']) && (bool) $form_state['values']['options']['capabilities']['enabled'] == TRUE) {
  317. $elements = (array) $form_state['values']['options']['capabilities']['options']['table']['elements'];
  318. uasort($elements, function($a, $b) {
  319. return $a['weight'] - $b['weight'];
  320. });
  321. foreach ($elements as $data) {
  322. if ((bool) $data['enabled'] == TRUE && !empty($data['text'])) {
  323. $capabilities[$data['machine_name']] = $data['text'];
  324. }
  325. }
  326. $form_state['values']['options']['capabilities']['options']['table'] = $capabilities;
  327. }
  328. else {
  329. $this->clearOption('capabilities');
  330. unset($form_state['values']['options']['capabilities']);
  331. }
  332. parent::optionsFormSubmit($form, $form_state);
  333. }
  334. /**
  335. * {@inheritdoc}
  336. */
  337. public function attached() {
  338. return array_merge_recursive(parent::attached(), Openlayers::getAttached());
  339. }
  340. }