Kuestenschmiede / MapsBundle

The interactive maps brick of the Contao GIS-kit con4gis.
https://maps.con4gis.org
GNU Lesser General Public License v3.0
7 stars 6 forks source link

No handling for direct links on markers from external table #1

Closed ghost closed 6 years ago

ghost commented 6 years ago

I think there is a bug in the LayerContentApi.php file. I collect markers from an external table (in this case an Isotope shop). The markers should link directly to the product details and an appropriate config is present:

$GLOBALS['con4gis']['maps']['sourcetable']['tl_iso_product'] = array
(
    'geox'          => 'geo_longitude',
    'geoy'          => 'geo_latitude',
    'label'         => 'sku',
    'locstyle'      => 'c4g_locstyle',
    'linkurl'       => 'shop/[alias].html',
    'sqlwhere'      => 'published = 1 AND pid = 0',
);

In sum, this leads to the correct markers being placed on the map, but without links. The JSON looks like this:

{
  "config": {
    "countAll": 1,
    "layer": [
      {
        "id": "3",
        "pid": "2",
        "name": "Karten-Marker",
        "zoom_locations": "1",
        "hide": "",
        "display": false,
        "type": "table",
        "content": [
          {
            "id": "1",
            "type": "GeoJSON",
            "format": "GeoJSON",
            "origType": "table",
            "locationStyle": "1",
            "cluster_fillcolor": "4975A8",
            "cluster_fontcolor": "ffffff",
            "cluster_zoom": "17",
            "loc_linkurl": "",
            "hover_location": "",
            "hover_style": "1",
            "data": {
              "type": "Feature",
              "geometry": {
                "type": "Point",
                "coordinates": [
                  10.432443,
                  44.312349
                ]
              },
              "properties": {
                "projection": "EPSG:4326",
                "popup": {
                  "async": false,
                  "content": ""
                },
                "tooltip": null,
                "label": "1",
                "zoom_onclick": "0"
              }
            }
[…]

As one can see, the loc_linkurl field is empty. I read through the PHP class for a while and found that the results from the database query are mainly parsed inside a conditional structure for the popup. But there is no popup in my configuration.

I think a new, separate block is needed which takes care of the linkurl field. If somebody acknowledges this as a bug, I could write a fix. But first I'd like some confirmation I'm heading in the right direction.

ghost commented 6 years ago

My suggestion would be to add something like this:

                        if ($arrConfig['linkurl']) {
                            $link = $arrConfig['linkurl'];
                            $link = str_replace('[id]', $result->id, $link);
                            $matches = [];
                            if (preg_match('/\[[a-z]+\]/', $link, $matches)) {
                                foreach ($matches as $key => $value) {
                                    $replacedValue = str_replace('[', '', $value);
                                    $replacedValue = str_replace(']', '', $replacedValue);
                                    if ($result->$replacedValue) {
                                        $replacedValue = $result->$replacedValue;
                                    }
                                    $matches[$key] = $replacedValue;
                                }

                                $link = preg_replace(['/\[[a-z]+\]/'], $matches, $link);
                            }
                        }

and then use the $link instead of $objLayer->loc_linkurl to build the array for the JSON structure.

coastforge-fsc commented 6 years ago

I pushed a possible fix in the feature-branch. I tried your solution and added a few tweaks to make it more viable with different links using the contao redirect targets. That should not affect your solution. Thank you for your suggestion.

ghost commented 6 years ago

Totally cool! Thank you for this great extension!