SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Widgets/MapWidget.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. namespace Sprout\Widgets;
  15.  
  16. use Kohana;
  17.  
  18. use Sprout\Helpers\Enc;
  19. use Sprout\Helpers\Form;
  20. use Sprout\Helpers\GeoSeach;
  21. use Sprout\Helpers\Needs;
  22. use Sprout\Helpers\View;
  23.  
  24. /**
  25. * Displays a google map
  26. **/
  27. class MapWidget extends Widget
  28. {
  29. protected $friendly_name = "Map";
  30. protected $friendly_desc = 'Street map of a specific address';
  31. protected $default_settings = [
  32. 'width' => 800,
  33. 'height' => 300,
  34. ];
  35.  
  36.  
  37. /**
  38.   * Ensure settings are sane
  39.   */
  40. public function cleanupSettings()
  41. {
  42. if (empty($this->settings['width'])) $this->settings['width'] = 500;
  43. if (empty($this->settings['height'])) $this->settings['height'] = 400;
  44. if (empty($this->settings['zoom'])) $this->settings['zoom'] = 15;
  45. if (empty($this->settings['align'])) $this->settings['align'] = '';
  46.  
  47. $this->settings['width'] = (int) $this->settings['width'];
  48. $this->settings['height'] = (int) $this->settings['height'];
  49. $this->settings['zoom'] = (int) $this->settings['zoom'];
  50. }
  51.  
  52.  
  53. /**
  54.   * Return the front-end view of this widget
  55.   *
  56.   * @param int $orientation The orientation of the widget.
  57.   **/
  58. public function render($orientation)
  59. {
  60. if (!empty($this->settings['lat']) and !empty($this->settings['lng'])) {
  61. $latlng = ['lat' => $this->settings['lat'], 'lng' => $this->settings['lng']];
  62. } else if (!empty($this->settings['address'])) {
  63. $latlng = GeoSeach::getByQuery($this->settings['address']);
  64. } else {
  65. return null;
  66. }
  67.  
  68. if (empty($latlng)) return null;
  69.  
  70. $this->cleanupSettings();
  71.  
  72. Needs::addCssInclude('https://unpkg.com/leaflet@1.5.1/dist/leaflet.css', ['integrity' => 'sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ==', 'crossorigin' => ''], 'leaflet_css');
  73. Needs::addJavascriptInclude('https://unpkg.com/leaflet@1.5.1/dist/leaflet.js', ['integrity' => 'sha512-GffPMF3RvMeYyc1LWMHtK8EbPv0iNZ8/oTtHPx9/cc2ILxQ+u905qIwdpULaqDkyBKgOaB57QTMg7ztg8Jm2Og==', 'crossorigin' => ''], 'leaflet_js');
  74. Needs::fileGroup('sprout/map_widget');
  75.  
  76. $view = new View('sprout/map_widget');
  77. $view->width = $this->settings['width'];
  78. $view->height = $this->settings['height'];
  79. $view->unique = md5(microtime(true));
  80. $view->zoom = $this->settings['zoom'];
  81. $view->latlng = $latlng;
  82. $view->align = $this->settings['align'];
  83.  
  84. return $view->render();
  85. }
  86.  
  87.  
  88. /**
  89.   * Return the settings form for this widget
  90.   **/
  91. public function getSettingsForm()
  92. {
  93. if (empty($this->settings['type'])) $this->settings['type'] = 'Road';
  94.  
  95. $out = '';
  96.  
  97. Form::nextFieldDetails('Location', false);
  98. $out .= Form::googleMap('lat,lng,zoom');
  99.  
  100. Form::nextFieldDetails('Width', false);
  101. $out .= Form::text('width');
  102.  
  103. Form::nextFieldDetails('Height', false);
  104. $out .= Form::text('height');
  105.  
  106. Form::nextFieldDetails('Align', false);
  107. $out .= Form::dropdown('align', [], ['left' => 'Left', 'right' => 'Right']);
  108.  
  109. Form::nextFieldDetails('Options', false);
  110. $out .= Form::checkboxList(['new_window' => 'Open in a new window when clicked']);
  111.  
  112. return $out;
  113. }
  114.  
  115.  
  116. /**
  117.   * Returns a label which describes the contents of this widget
  118.   * See {@link Widget::get_info_label} for full documentation
  119.   **/
  120. public function getInfoLabels()
  121. {
  122. return array(
  123. 'Address' => @$this->settings['address'],
  124. );
  125. }
  126.  
  127. }
  128.  
  129.