<html>
<head><title>Hello, map</title></head>
<body>
<p>
<?php
$configFilePath = 'C:\Program Files\MapGuideOpenSource\WebServerExtensions\www\webconfig.ini';
$mgSessionId = ($_SERVER['REQUEST_METHOD'] == "POST")? $_POST['SESSION']: $_GET['SESSION'];
$mapName = ($_SERVER['REQUEST_METHOD'] == "POST")? $_POST['MAPNAME']: $_GET['MAPNAME'];
$xmlSelectString = urldecode(stripslashes($_REQUEST["Key"]));
$LayerName = urldecode(stripslashes($_REQUEST["LayerName"]));
$KeyName = urldecode(stripslashes($_REQUEST["DesiredField"]));
$DestinationKeyName = urldecode(stripslashes($_REQUEST["DestinationKeyName"]));
$AdditionalData = $_REQUEST["Additional"];
$ReportURL = urldecode(stripslashes($_REQUEST["URL"]));

// Initialize the Web Extensions and connect to the Server using
// the Web Extensions session identifier stored in PHP
// session state.

MgInitializeWebTier ($configFilePath);
$userInfo = new MgUserInformation($mgSessionId);
$siteConnection = new MgSiteConnection();
$siteConnection->Open($userInfo);
$resourceService = $siteConnection->CreateService(0);
$featureService = $siteConnection->CreateService(2);
$queryOptions = new MgFeatureQueryOptions();
$map = new MgMap();
$map->Open($resourceService, $mapName);

// Create the selection object by retrieving the current
// selection from the map.

$selection = new MgSelection($map,$xmlSelectString);
$layers = $selection->GetLayers();

if ($layers)
{
            for ($i = 0; $i < $layers->GetCount(); $i++)
            {
// Only check selected features in the Parcels layer.

                        $layer = $layers->GetItem($i);
                        if ($layer && $layer->GetName() == $LayerName)
                                    {
                                    // Create a filter containing the IDs of the selected
                                    // features on this layer

                                    $layerClassName = $layer->GetFeatureClassName();
                                    $selectionString = $selection->GenerateFilter($layer, $layerClassName);

                                    // Get the feature resource for the selected layer

                                    $layerFeatureId = $layer->GetFeatureSourceId();
                                    $layerFeatureResource = new
                                    MgResourceIdentifier($layerFeatureId);

                                    // Apply the filter to the feature resource for the
                                    // selected layer. This returns
                                    // an MgFeatureReader of all the selected features.

                                    $queryOptions->SetFilter($selectionString);
                                    $featureReader = $featureService->SelectFeatures($layerFeatureResource, $layerClassName, $queryOptions);

                                    // Process each item in the MgFeatureReader, displaying the
                                    // owner name and address

                                    $val="'";
                                    while ($featureReader->ReadNext())
                                                {
                                                $val =  $val . $featureReader->GetString($KeyName) . "','";
                                                }
                                                $val = substr($val, 0, strlen($val)-2);
                        $val = urlencode(str_replace("'", '', $val));
                        
                        header("Location: " . $ReportURL . "?" . $DestinationKeyName . "=" . $val . "&" . $AdditionalData);
                        exit;
                                                include $ReportURL . $val;
                                    }
            }
}
else
            echo 'No selected layers';

?>
</p>
</body>
</html>