vendor/con4gis/maps/Resources/contao/classes/MapDataConfigurator.php line 42

Open in your IDE?
  1. <?php
  2. /**
  3.  * con4gis - the gis-kit
  4.  *
  5.  * @version   php 7
  6.  * @package   con4gis
  7.  * @author    con4gis contributors (see "authors.txt")
  8.  * @license   GNU/LGPL http://opensource.org/licenses/lgpl-3.0.html
  9.  * @copyright Küstenschmiede GmbH Software & Design 2011 - 2018
  10.  * @link      https://www.kuestenschmiede.de
  11.  */
  12. namespace con4gis\MapsBundle\Resources\contao\classes;
  13. use con4gis\MapsBundle\Resources\contao\models\C4gMapBaselayersModel;
  14. use con4gis\MapsBundle\Resources\contao\models\C4gMapProfilesModel;
  15. use con4gis\MapsBundle\Resources\contao\models\C4gMapsModel;
  16. /**
  17.  * Class MapDataConfigurator
  18.  * @package con4gis\MapsBundle\Resources\contao\classes
  19.  */
  20. class MapDataConfigurator
  21. {
  22.     private static $allLocstyles false;
  23.     /**
  24.      * [prepareMapData description]
  25.      * @param  [type] $objThis  [description]
  26.      * @param  [type] $database [description]
  27.      * @param  array  $options  [description]
  28.      * @return [type]           [description]
  29.      */
  30.     public static function prepareMapData($objThis$database$options = array())
  31.     {
  32.         $mapData = array();
  33.         $mapData['mapId'] = $objThis->id;
  34.         // import user, if not already done
  35.         if (!isset($objThis->User)) {
  36.             if ($options['backend']) {
  37.                 $objThis->import('BackendUser''User');
  38.             } else {
  39.                 $objThis->import('FrontendUser''User');
  40.             }
  41.         }
  42.         //TODO: currently when in backend mode, not the correct things are loaded
  43.         //TODO: no profile is found
  44.         // get map
  45.         $map C4gMapsModel::findByPk($objThis->c4g_map_id);
  46.         if (!$map) {
  47.             $map = new C4gMapsModel();
  48.             $map->id 0;
  49.         }
  50.         $mapData['id'] = $map->id;
  51.         // ------------------------------------------------------------------------
  52.         // get profile for map
  53.         // ------------------------------------------------------------------------
  54.         $profileId $map->profile;
  55.         // check for mobile-profile
  56.         $isMobile false;
  57.         if (($map->profile_mobile 0) && (\Input::cookie('TL_VIEW') == 'mobile' || (\Environment::get('agent')->mobile && \Input::cookie('TL_VIEW') != 'desktop'))) {
  58.             $isMobile true;
  59.             $profileId $map->profile_mobile;
  60.         }
  61.         //check if we are in backend mode
  62.         if ($options['geoeditor']) {
  63.             // select selected backend profile
  64.             $result $database->prepare("SELECT id FROM `tl_c4g_map_profiles` WHERE `is_backend_editor_default` = '1'")->limit(1)->execute();
  65.             $profileId $result->row();
  66.             $profileId $profileId['id'];
  67.         }
  68.         //check if we are in backend mode
  69.         if ($options['type'] == 'geopicker') {
  70.             // select selected backend profile
  71.             $result $database->prepare("SELECT id FROM `tl_c4g_map_profiles` WHERE `is_backend_geopicker_default` = '1'")->limit(1)->execute();
  72.             $profileId $result->row();
  73.             $profileId $profileId['id'];
  74.         }
  75.         // check for special-profile
  76.         if ((FE_USER_LOGGED_IN) && ($map->use_specialprofile)) {
  77.             $groupMatch array_intersect($objThis->User->groupsdeserialize($map->specialprofile_groups));
  78.             if (!empty($groupMatch)) {
  79.                 if (($isMobile) && ($map->specialprofile_mobile)) {
  80.                     $profileId $map->specialprofile_mobile;
  81.                 } else {
  82.                     $profileId $map->specialprofile;
  83.                 }
  84.             }
  85.         }
  86.         // get appropriate profile from database
  87.         $profile C4gMapProfilesModel::findByPk($profileId);
  88.         // use default if the profile was not found
  89.         if (!$profile) {
  90.             if (!$options['type'] == 'geopicker') {
  91.                 $profile C4gMapProfilesModel::findBy('is_backend_geopicker_default',1);
  92.                 if (!$profile) {
  93.                     $profile C4gMapProfilesModel::findBy('is_default',1);
  94.                     if (!$profile) {
  95.                         $profiles C4gMapProfilesModel::findAll();
  96.                         if ($profiles && (count($profiles) > 0)) {
  97.                             $length count($profiles);
  98.                             $profile $profiles[$length-1];
  99.                         }
  100.                     }
  101.                 }
  102.             }
  103.             if ($profile) {
  104.                 $profileId $profile->id;
  105.             } else {
  106.                 // set defaults for geopicker
  107.                 $mapData['zoom_panel'] = true;
  108.                 $mapData['zoom_slider'] = false;
  109.                 $mapData['zoom_extent'] = false;
  110.                 $mapData['zoom_home'] = false;
  111.                 $mapData['zoom_position'] = false;
  112.                 $mapData['mouse_nav']['wheel_zoom'] = true;
  113.                 $mapData['mouse_nav']['doubleclick_zoom'] = true;
  114.                 $mapData['mouse_nav']['drag_pan'] = true;
  115.                 // map-information
  116.                 //
  117.                 $mapData['attribution'] = array();
  118.                 $mapData['attribution']['enable'] = true;
  119.                 $mapData['attribution']['collapsed'] = true;
  120.                 $mapData['attribution']['cfg_logo'] = false;
  121.                 $mapData['scaleline'] = false;
  122.                 $mapData['mouseposition'] = false;
  123.                 $mapData['zoomlevel'] = 1;
  124.             }
  125.         }
  126.         $mapData['profile'] = $profileId;
  127.         // ------------------------------------------------------------------------
  128.         // set basic map options
  129.         // ------------------------------------------------------------------------
  130.         if (!$mapData['id'])
  131.         {
  132.             $mapData['mapId'] = 0;
  133.             $mapData['id'] = 0;
  134.         }
  135.         $mapData['width'] = '100%';
  136.         $mapData['height'] = '100vh';
  137.         if ($map->id != 0) {
  138.             // map-center
  139.             if (!empty($map->center_geox)) {
  140.                 $mapData['center']['lon'] = \Contao\Controller::replaceInsertTags($map->center_geox);
  141.             }
  142.             if (!empty($map->center_geoy)) {
  143.                 $mapData['center']['lat'] = \Contao\Controller::replaceInsertTags($map->center_geoy);
  144.             }
  145.             // map-zoom
  146.             if (!empty($map->zoom)) {
  147.                 $mapData['center']['zoom'] = \Contao\Controller::replaceInsertTags($map->zoom);
  148.             }
  149.             // geolocation (use user-location, if possible)
  150.             if ($map->geolocation) {
  151.                 $mapData['geolocation'] = true;
  152.                 $mapData['geolocation_zoom'] = \Contao\Controller::replaceInsertTags($map->geolocation_zoom);
  153.             }
  154.             // map-size
  155.             $mapWidth deserialize($map->width);
  156.             if (is_array($mapWidth) && ($mapWidth['value'] != 0) && !empty($mapWidth['unit'])) {
  157.                 $mapData['width'] = $mapWidth['value'] . $mapWidth['unit'];
  158.             }
  159.             $mapHeight deserialize($map->height);
  160.             if (is_array($mapHeight) && ($mapHeight['value'] != 0) && !empty($mapHeight['unit'])) {
  161.                 $mapData['height'] = $mapHeight['value'] . $mapHeight['unit'];
  162.             }
  163.             // map-margin
  164.             $mapMargin deserialize($map->margin);
  165.             if (is_array($mapMargin) && !empty($mapMargin['unit'])) {
  166.                 // [note]: inspired by contaos stylesheet-handling
  167.                 $top $mapMargin['top'];
  168.                 $right $mapMargin['right'];
  169.                 $bottom $mapMargin['bottom'];
  170.                 $left $mapMargin['left'];
  171.                 $unit $mapMargin['unit'];
  172.                 // Try to shorten the definition
  173.                 if ($top != '' && $right != '' && $bottom != '' && $left != '') {
  174.                     if ($top == $right && $top == $bottom && $top == $left) {
  175.                         $mapData['margin'] = $top . (($top == 'auto' || $top === '0') ? '' $unit);
  176.                     } elseif ($top == $bottom && $right == $left) {
  177.                         $mapData['margin'] = $top . (($top == 'auto' || $top === '0') ? '' $unit) . ' ' $right . (($right == 'auto' || $right === '0') ? '' $unit);
  178.                     } else {
  179.                         $mapData['margin'] = $top . (($top == 'auto' || $top === '0') ? '' $unit) . ' ' $right . (($right == 'auto' || $right === '0') ? '' $unit) . ' ' $bottom . (($bottom == 'auto' || $bottom === '0') ? '' $unit) . ' ' $left . (($left == 'auto' || $left === '0') ? '' $unit);
  180.                     }
  181.                 } else {
  182.                     $mapData['margin'] = ($top $top $unit '0') . ' ' . ($right $right $unit '0') . ' ' . ($bottom $bottom $unit '0') . ' ' . ($left $left $unit '0');
  183.                 }
  184.                 // [/note]
  185.             }
  186.             // map-extend
  187.             if($map->show_locations == "1"){
  188.                 $mapData['calc_extent'] = 'LOCATIONS';
  189.             }
  190.             else{
  191.                 $mapData['calc_extent'] = 'CENTERZOOM';
  192.             }
  193.             if ($mapData['calc_extent'] == 'LOCATIONS') {
  194.                 $mapData['min_gap'] = $map->min_gap;
  195.             }
  196.             // map-restriction
  197.             $mapData['restrict_area'] = $map->restrict_area;
  198.             if ($map->restrict_area) {
  199.                 $mapData['restr_bottomleft_lon'] = $map->restr_bottomleft_lon;
  200.                 $mapData['restr_bottomleft_lat'] = $map->restr_bottomleft_lat;
  201.                 $mapData['restr_topright_lon'] = $map->restr_topright_geox;
  202.                 $mapData['restr_topright_lat'] = $map->restr_topright_geoy;
  203.             }
  204.         }
  205.         // override map-zoom from structure, with values from CE/FE, if set
  206.         if (is_numeric($objThis->c4g_map_zoom) && ($objThis->c4g_map_zoom 0)) {
  207.             $mapData['center']['zoom'] = $objThis->c4g_map_zoom;
  208.         }
  209.         // override map-size from structure, with values from CE/FE, if set
  210.         $mapWidth deserialize($objThis->c4g_map_width);
  211.         if (is_array($mapWidth) && ($mapWidth['value'] != 0) && !empty($mapWidth['unit'])) {
  212.             $mapData['width'] = $mapWidth['value'] . $mapWidth['unit'];
  213.         }
  214.         $mapHeight deserialize($objThis->c4g_map_height);
  215.         if (is_array($mapHeight) && ($mapHeight['value'] != 0) && !empty($mapHeight['unit'])) {
  216.             $mapData['height'] = $mapHeight['value'] . $mapHeight['unit'];
  217.         }
  218.         // ------------------------------------------------------------------------
  219.         // collect data from map profile
  220.         // ------------------------------------------------------------------------
  221.         if ($profile) {
  222.             // general
  223.             //
  224.             // basemaps
  225.             //
  226.             $mapData['default_baselayer'] = $profile->default_baselayer;
  227.             // location-styles
  228.             //
  229.             // map-navigation
  230.             //
  231.             $mapData['zoom_panel']  = $profile->zoom_panel;
  232.             switch ($profile->zoom_panel_button) {
  233.                 case '3':
  234.                     $mapData['zoom_position'] = true;
  235.                     break;
  236.                 case '2':
  237.                     $mapData['zoom_home'] = true;
  238.                     break;
  239.                 case '1':
  240.                     $mapData['zoom_extent'] = true;
  241.                     break;
  242.                 default:
  243.             }
  244.             $mapData['zoom_slider'] = ($profile->zoom_panel_slider == 1);
  245.             if ($profile->mouse_nav) {
  246.                 $mapData['mouse_nav']['drag_pan'] = $profile->mouse_nav;
  247.                 $mapData['mouse_nav']['wheel_zoom'] = $profile->mouse_nav_wheel;
  248.                 $mapData['mouse_nav']['doubleclick_zoom'] = $profile->mouse_nav_doubleclick_zoom;
  249.                 $mapData['mouse_nav']['drag_zoom'] = $profile->mouse_nav_zoombox;
  250.                 switch ($profile->mouse_nav_dragmode) {
  251.                     case '2':
  252.                         $mapData['mouse_nav']['drag_rotate_zoom'] = true;
  253.                         // falltrough
  254.                     case '1':
  255.                         $mapData['mouse_nav']['drag_rotate'] = true;
  256.                         break;
  257.                     default:
  258.                 }
  259.                 $mapData['mouse_nav']['kinetic'] = $profile->mouse_nav_kinetic;
  260.                 $mapData['mouse_nav']['toolbar'] = $profile->mouse_nav_toolbar;
  261.             }
  262.             if ($profile->touch_nav) {
  263.                 $mapData['touch_nav']['rotate'] = $profile->touch_nav;
  264.                 $mapData['touch_nav']['zoom'] = $profile->touch_nav;
  265.             }
  266.             if ($profile->keyboard_nav) {
  267.                 $mapData['keyboard_nav']['pan'] = $profile->keyboard_nav;
  268.                 $mapData['keyboard_nav']['zoom'] = $profile->keyboard_nav;
  269.             }
  270.             $mapData['fullscreen'] = $profile->fullscreen;
  271.             // Starboard
  272.             //
  273.             if ($profile->starboard) {
  274.                 $mapData['starboard']['enable'] = $profile->starboard;
  275.                 $mapData['starboard']['open'] = $profile->starboard_open;
  276.                 $mapData['starboard']['label'] = \Contao\Controller::replaceInsertTags($profile->starboard_label);
  277.                 $mapData['starboard']['div'] = $profile->starboard_div;
  278.                 $mapData['starboard']['filter'] = $profile->starboard_filter;
  279.                 $mapData['starboard']['button'] = $profile->starboard_button;
  280.                 $mapData['cluster_all'] = $profile->cluster_all;
  281.                 $mapData['cluster_distance'] = $profile->cluster_distance;
  282.                 $mapData['cluster_fillcolor'] = $profile->cluster_fillcolor;
  283.                 $mapData['cluster_fontcolor'] = $profile->cluster_fontcolor;
  284.                 $mapData['cluster_zoom'] = $profile->cluster_zoom;
  285.                 // Baselayerswitcher
  286.                 $mapData['baselayerswitcher']['enable'] = $profile->baselayerswitcher;
  287.                 $mapData['baselayerswitcher']['label'] = \Contao\Controller::replaceInsertTags($profile->baselayerswitcher_label);
  288.                 // Layerswitcher
  289.                 $mapData['layerswitcher']['enable'] = $profile->layerswitcher;
  290.                 $mapData['layerswitcher']['label'] = \Contao\Controller::replaceInsertTags($profile->layerswitcher_label);
  291.             }
  292.             // map-tools
  293.             //
  294.             if ($profile->measuretool) {
  295.                 $mapData['measuretools']['enable'] = $profile->measuretool;
  296.             }
  297.             //todo first draft for map printing
  298.             //$mapData['exporttools']['enable'] = true;
  299.             $mapData['graticule'] = $profile->graticule;
  300.             // map-information
  301.             //
  302.             $mapData['attribution']['enable'] = $profile->attribution;
  303.             if ($profile->attribution) {
  304.                 $mapData['attribution']['collapsed'] = $profile->collapsed_attribution;
  305.                 if ($profile->cfg_logo_attribution) {
  306.                     $mapData['attribution']['cfg_logo'] = $profile->cfg_logo_attribution;
  307.                 }
  308.                 if ($profile->div_attribution) {
  309.                     $mapData['attribution']['div'] = $profile->div_attribution;
  310.                 }
  311.                 if ($profile->add_attribution) {
  312.                     $mapData['attribution']['additional'] = \Contao\Controller::replaceInsertTags($profile->add_attribution);
  313.                 }
  314.             }
  315.             $mapData['overviewmap'] = $profile->overviewmap;
  316.             $mapData['scaleline'] = $profile->scaleline;
  317.             $mapData['mouseposition'] = $profile->mouseposition;
  318.             $mapData['permalink']['enable'] = $profile->permalink;
  319.             if ($profile->permalink) {
  320.                 $mapData['permalink']['get_parameter'] = $profile->permalink_get_param;
  321.             }
  322.             $mapData['zoomlevel'] = $profile->zoomlevel;
  323.             // geosearch
  324.             //
  325.             if ($profile->geosearch) {
  326.                 $mapData['geosearch']['geosearch_engine'] = $profile->geosearch_engine;
  327.                 $mapData['geosearch']['enable'] = ($profile->geosearch && $profile->geosearch_show);
  328.                 if ($profile->geosearch_customengine_attribution) {
  329.                     $mapData['geosearch']['custom_attribution'] = \Contao\Controller::replaceInsertTags($profile->geosearch_customengine_attribution);
  330.                 }
  331.                 $mapData['geosearch']['results'] = $profile->geosearch_results;
  332.                 $mapData['geosearch']['div'] = $profile->geosearch_div;
  333.                 $mapData['geosearch']['searchzoom'] = $profile->geosearch_zoomto;
  334.                 $mapData['geosearch']['zoombounds'] = $profile->geosearch_zoombounds;
  335.                 $mapData['geosearch']['animate'] = $profile->geosearch_animate;
  336.                 $mapData['geosearch']['markresult'] = $profile->geosearch_markresult;
  337.                 $mapData['geosearch']['popup'] = $profile->geosearch_popup;
  338.                 $mapData['geosearch']['attribution'] = \Contao\Controller::replaceInsertTags($profile->geosearch_attribution);
  339.                 $mapData['geosearch']['collapsed'] = $profile->geosearch_collapsed;
  340.                 if ($profile->attribution && $profile->geosearch_attribution) {
  341.                     $mapData['attribution']['geosearch'] = \Contao\Controller::replaceInsertTags($profile->geosearch_attribution);
  342.                 }
  343.             }
  344.             // geopicker
  345.             //
  346.             if ($profile->geopicker) {
  347.                 $mapData['geopicker']['enable'] = $profile->geopicker;
  348.                 $mapData['geopicker']['type'] = 'frontend';
  349.                 $mapData['geopicker']['input_geo_x'] = '#' $profile->geopicker_fieldx;
  350.                 $mapData['geopicker']['input_geo_y'] = '#' $profile->geopicker_fieldy;
  351.                 $mapData['geopicker']['anonymous'] = $profile->geopicker_anonymous;
  352.                 $mapData['geopicker']['disabled']  = $profile->geopicker_disabled;
  353.             }
  354.             // router
  355.             //
  356.             if ($profile->router) {
  357.                 $mapData['router_enable'] = $profile->geosearch && $profile->router;
  358.                 $mapData['router_viaroute_precision'] = $profile->router_viaroute_url 1e5 1e6;
  359.                 if ($profile->router_attribution)
  360.                 {
  361.                     $mapData['router_attribution'] = \Contao\Controller::replaceInsertTags($profile->router_attribution);
  362.                     if ($profile->attribution) {
  363.                         $mapData['attribution']['router'] = \Contao\Controller::replaceInsertTags($profile->router_attribution);
  364.                     }
  365.                 }
  366.                 $mapData['router_from_locstyle'] = $profile->router_from_locstyle;
  367.                 $mapData['router_to_locstyle'] = $profile->router_to_locstyle;
  368.                 $mapData['router_point_locstyle'] = $profile->router_point_locstyle;
  369.                 $mapData['router_interim_locstyle'] = $profile->router_interim_locstyle;
  370.                 $mapData['router_api_selection'] = $profile->router_api_selection;
  371.                 $mapData['router_alternative'] = $profile->router_alternative;
  372.             }
  373.             // editor
  374.             //
  375.             if ($profile->editor) {
  376.                 $mapData['editor']['enable'] = $profile->editor;
  377.                 $mapData['editor']['type'] = 'frontend';
  378.                 $mapData['editor']['open'] = false;
  379.             }
  380.             // cesium
  381.             //
  382.             if ($profile->cesium) {
  383.                 $mapData['cesium'] = array();
  384.                 $mapData['cesium']['enable'] = $profile->cesium;
  385.                 $mapData['cesium']['always'] = $profile->cesium_always;
  386.             }
  387.             // expert-configs
  388.             //
  389.             // miscellaneous
  390.             //
  391.             $mapData['infopage'] =  \Contao\Controller::replaceInsertTags($profile->infopage);
  392.             $mapData['link_newwindow'] = $profile->link_newwindow;
  393.             $mapData['hover_popups'] = $profile->hover_popups;
  394.             $mapData['hover_popups_stay'] = $profile->hover_popups_stay;
  395.         }
  396.         // -----
  397.         // (...)
  398.         // -----
  399.         // mapservice
  400.         $baseLayer C4gMapBaselayersModel::findById($objThis->c4g_map_default_mapservice);
  401.         if ($baseLayer) {
  402.             $mapData['baselayer'] = $baseLayer->id;
  403.             $mapData['minZoom'] = $baseLayer->minzoomlevel;
  404.             $mapData['maxZoom'] = $baseLayer->maxzoomlevel;
  405.         }
  406.         // API
  407.         //
  408.         $mapData['api']['baselayer'] = $GLOBALS['con4gis']['maps']['api']['baselayer'];
  409.         $mapData['api']['layer'] = $GLOBALS['con4gis']['maps']['api']['layer'];
  410.         $mapData['api']['layercontent'] = $GLOBALS['con4gis']['maps']['api']['layercontent'];
  411.         $mapData['api']['locstyle'] = $GLOBALS['con4gis']['maps']['api']['locstyle'];
  412.         $mapData['api']['infowindow'] = $GLOBALS['con4gis']['maps']['api']['infowindow'];
  413.         $mapData['api']['editor'] = $GLOBALS['con4gis']['maps']['api']['editor'];
  414.         $mapData['api']['geosearch'] = $GLOBALS['con4gis']['maps']['api']['geosearch'];
  415.         $mapData['api']['geosearch_reverse'] = $GLOBALS['con4gis']['maps']['api']['geosearch_reverse'];
  416.         $mapData['api']['routing'] = $GLOBALS['con4gis']['maps']['api']['routing'];
  417.         // load resources
  418.         //
  419.         // tell the Core to load all resources needed for maps
  420.         ResourceLoader::loadResourcesForModule('maps');
  421.         // load internal scripts and themes
  422.         if ($profileId) {
  423.             ResourceLoader::loadResourcesForProfile($profileId$options['type'] == 'geopicker');
  424.         } else {
  425.             ResourceLoader::loadResources();
  426.             ResourceLoader::loadTheme();
  427.         }
  428.         // @TODO: Check
  429.         $mapData['addIdToDiv'] = true;
  430.         return $mapData;
  431.     }
  432. }