<?php
/**
 * Maptip
 *

 * Portions copyright (c) 2006, DM Solutions Group Inc.
 * Portions copyright (c) 2008, ENPLAN
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

/*****************************************************************************
 * Purpose: duplicate maptip functionality on mapserver
 *****************************************************************************/

/* set up the session */
include(dirname(__FILE__).'/../../../common/php/Utilities.php');
include(dirname(__FILE__).'/Common.php');
include(dirname(__FILE__).'/Utilities.php');

/* the name of the layer (only one) in the map to query */

if ($_REQUEST['layer'] != '') {
    $layer = $_REQUEST['layer'];
} 
if ($_REQUEST['textfield'] != '') {
    $mapTipTextField = $_REQUEST['textfield'];
}
if ($_REQUEST['customURL'] != '') {
    $mapTipURL = $_REQUEST['customURL'];
}
if ($_REQUEST['mapname'] != '') {
    $mapname = $_REQUEST['mapname'];
}
/* selection is intersects only */
$variant = 'intersects';


/* a spatial filter in the form on a WKT geometry */
$spatialFilter = (isset($_REQUEST['spatialfilter']) && $_REQUEST['spatialfilter'] != '') ? urldecode($_REQUEST['spatialfilter']) : false;
//echo "spatial filter is $spatialFilter<BR>";

if (!isset($mapName)) {
    die('mapname not set');
}
if (isset($_SESSION['maps']) && isset($_SESSION['maps'][$mapName])) {
    $oMap = ms_newMapObj($_SESSION['maps'][$mapName]);
}

/* add the spatial filter if provided.  It is expected to come as a
   WKT string, so we need to convert it to a shape */
if ($spatialFilter !== false ) {
    $oSpatialFilter = ms_shapeObjFromWkt($spatialFilter);
}

$oLayer = $oMap->GetLayerByName($layer);

$oLayer->set('tolerance', 0);

if ($oLayer->type ==  MS_LAYER_RASTER || $oLayer->type == MS_LAYER_QUERY ||
        $oLayer->type ==  MS_LAYER_CIRCLE ||  $oLayer->type == MS_LAYER_CHART) {
        die('maptips are only valid for vector layers');
    }

//header('Content-type: text/x-json');
//header('X-JSON: true');
if (@$oLayer->queryByShape($oSpatialFilter) == MS_SUCCESS) {

    $oRes = $oLayer->getResult(0);
    $oLayer->open();

    $oShape = $oLayer->getShape($oRes->tileindex,$oRes->shapeindex);

    $mapTipLink = buildCustonUrl($oShape->values,$mapTipURL);

    /*if (isset($mapTipLinkField)){
        $mapTipLink = $oShape->getValue($oLayer, $mapTipLinkField);
    }*/
    echo "{mapTipText :'";
    echo $oShape->values[$mapTipTextField];
    echo "',mapTipLink:'";
    echo $mapTipLink;
    echo "'}";
}else{
echo "{mapTipText: '', mapTipLink: ''}";
}

$oLayer->close();

function buildCustonUrl($aValues,$url){

    $pattern = "/\[(.+?)\]/";
    preg_match_all($pattern, $url, $values,PREG_PATTERN_ORDER);

    if(is_array($values[0])){
        foreach($values[0] as $key=>$item){
            $url = str_replace($item,$aValues[$values[1][$key]],$url);
        }
    }
    return $url;
}

?>