Skip to content
Snippets Groups Projects
OsmMap.php 3.72 KiB
<?php
/**
 * Copyright (c) 2012-2019, Agence Française Informatique (AFI). All rights reserved.
 *
 * BOKEH is free software; you can redistribute it and/or modify
 * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
 * the Free Software Foundation.
 *
 * There are special exceptions to the terms and conditions of the AGPL as it
 * is applied to this software (see README file).
 *
 * BOKEH is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
 *
 * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
 * along with BOKEH; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
 */


class Intonation_View_OsmMap extends ZendAfi_View_Helper_BaseHelper {
  protected $_wrapper_id, $_map_id;


  public function init() {
    $this->setOSMId(uniqid());
  }


  public function setOSMId($id) {
    $this->_wrapper_id = 'osm_wrap_' . $id;
    $this->_map_id = 'osm_map_' . $id;
  }



  public function osmMap($elements, $html_renderer = null) {
    if (!$html_renderer)
      $html_renderer = [$this->view, 'osmCard'];

    if (!$elements || !($datas = $this->_collectDatas($elements, $html_renderer)))
      return '';

    $this->renderHeadScriptsOn(Class_ScriptLoader::getInstance());

    return
      $this->_div(['id' => $this->_wrapper_id],
                  $this->_div(['id' => $this->_map_id,
                               'class' => 'leaflet_osm',
                               'data-osm' => json_encode($datas),
                               'data-osm-geojson' => '',
                               'data-osm-layer' => 1,
                               'data-osm-zoom' => 0,
                               'data-osm-open-url-icon' => $this->_getMarkerOrDefault('osm_opened_marker', 'map/open-marker-icon.png'),
                               'data-osm-close-url-icon' => $this->_getMarkerOrDefault('osm_closed_marker', 'map/close-marker-icon.png'),
                               'data-osm-default-url-icon' => $this->_getMarkerOrDefault('osm_marker', 'map/marker-icon.png')]));
  }


  public function renderHeadScriptsOn($script_loader) {
    $script_loader
      ->loadLeafletJS()
      ->addOPACScripts(['openStreetMap/openStreetMap.js', 'openStreetMap/leaflet.markercluster.js'])
      ->addOPACStyleSheets(['../js/openStreetMap/openStreetMap.css',
                            '../js/openStreetMap/MarkerCluster.css',
                            '../js/openStreetMap/MarkerCluster.Default.css'])
      ->addJQueryReady('setTimeout(function() {$("#'. $this->_wrapper_id . '").openStreetMap();}, 100);');
    return $this;
  }


  protected function _getMarkerOrDefault($key, $default) {
    $current = Class_Template::current();

    if ($current->isLegacy())
      return $this->_getUrlIcon($default);

    return ($ico = $current->getIco($this->view, $key, 'utils'))
      ? $ico
      : $this->_getUrlIcon($default);
  }


  protected function _collectDatas($elements, $html_renderer) {
    return array_filter($elements
                        ->collect(function($e) use ($html_renderer)
                                  {
                                    return $this->_buildOSMDatas($e, $html_renderer);
                                  })
                        ->getArrayCopy());
  }


  protected function _buildOSMDatas($element, $html_renderer) {
    if (!$data = $element->getOsmData())
      return null;

    $data->sethtml($html_renderer($element));
    return $data->toArray();
  }


  protected function _getUrlIcon($path) {
    return $this->view->skinImageUrl($path);
  }
}