# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
require_once dirname(__FILE__) . "/../../core/globalSettings.php";
require_once dirname(__FILE__) . "/../classes/class_gml2.php";
require_once dirname(__FILE__) . "/../classes/class_json.php";
$ajaxResponse = new AjaxResponse($_POST);
if ($ajaxResponse->getMethod() != "createUsemap") {
    $ajaxResponse->setSuccess(false);
    $ajaxResponse->setMessage("method invalid");
    $ajaxResponse->send();
    exit;
}
$url = $ajaxResponse->getParameter('url');
//urldecode($_REQUEST["url"]);
$g = new gml2();
$g->parsegml($url);
$um_title = array();
$um_x = array();
$um_y = array();
for ($i = 0; $i < $g->getMemberCount(); $i++) {
    $um_title[] = utf8_decode($g->getValueBySeparatedKey($i, "name"));
    $um_x[] = $g->getXfromMemberAsString($i, 0);
    $um_y[] = $g->getYfromMemberAsString($i, 0);
}
$resultObj = array();
$resultObj['um_title'] = $um_title;
$resultObj['um_x'] = $um_x;
$resultObj['um_y'] = $um_y;
$ajaxResponse->setSuccess(true);
$ajaxResponse->setResult($resultObj);
$resultObj["wmcTou"]["message"] = $tou;
#$resultObj["wmcTou"]["message"] = "Terms of Use";
//
// Output
//
// Check if session WMC module is loaded
$sql = "SELECT COUNT(e_id) AS i FROM gui_element WHERE fkey_gui_id = \$1 AND e_id = \$2";
$v = array(Mapbender::session()->get("mb_user_gui"), "sessionWmc");
$t = array("s", "s");
$res = db_prep_query($sql, $v, $t);
$row = db_fetch_assoc($res);
$isSessionWmcModuleLoaded = intval($row["i"]);
// check if Session contains a GML, and then zoom to it
$gml_string = Mapbender::session()->get("GML");
if ($gml_string) {
    $gml = new gml2();
    $gml->parse_xml($gml_string);
    $bbox = new Mapbender_bbox($gml->bbox[0], $gml->bbox[1], $gml->bbox[2], $gml->bbox[3], $epsg = "EPSG:" . $gml->epsg);
    $wmcGetApi->mainMap->setExtent($bbox);
}
if (count($resultObj["withoutId"]["wms"]) === 0 && count($resultObj["invalidId"]["wms"]) === 0 && count($resultObj["unavailable"]["wms"]) === 0 || !$isSessionWmcModuleLoaded) {
    Mapbender::session()->set("wmcConstraints", $resultObj);
    $output = $wmcGetApi->wmsToJavaScript();
    $wmcJs = $wmcGetApi->toJavaScript(array());
    $wmcJs = implode(";\n", $wmcJs);
    $extentJs = $wmcGetApi->extentToJavaScript();
    $output[] = <<<JS
\t\tMapbender.events.afterInit.register(function () {
\t\t\t{$wmcJs};
\t\t});
\t\tMapbender.events.beforeInit.register(function () {
     $v = array($wfs_conf_id);
     $t = array('i');
     #echo $sql;
     $res = db_prep_query($sql, $v, $t);
     $col = array();
     while ($row = db_fetch_array($res)) {
         array_push($col, $row["element_name"]);
     }
     if (count($col) == 0) {
         die("wfs_conf_element data not available");
     }
     // append authorisation condition to filter
     $filter = checkAccessConstraint($filter, $wfs_conf_id);
     $admin = new administration();
     $req = urldecode($url) . urlencode($admin->char_decode(stripslashes($filter)));
     $mygml = new gml2();
     $mygml->parseFile($req);
     if (!empty($exportToShape)) {
         $filenamePrefix = md5(microtime());
         $mygml->toShape($filenamePrefix);
         header("Content-Type: application/json; charset=utf-8");
         echo '{"filename": "' . $filenamePrefix . '.zip"}';
     } else {
         header("Content-Type: application/json; charset=utf-8");
         echo $mygml->toGeoJSON();
     }
 } else {
     if ($command == "getFeature") {
         $admin = new administration();
         $wfsGetFeature = $admin->checkURL($wfsGetFeature);
         $url = $wfsGetFeature . "REQUEST=getFeature&VERSION=1.0.0&SERVICE=WFS&MAXFEATURES=20" . "&typename=" . $wfsFeatureTypeName;
# any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
require_once dirname(__FILE__) . "/../php/mb_validateSession.php";
require_once dirname(__FILE__) . "/../classes/class_gml2.php";
define("GML_HIGHLIGHT_Z_INDEX", 1000);
$gml_string = Mapbender::session()->get("GML");
if ($gml_string) {
    $gml = new gml2();
    $gml->parse_xml($gml_string);
    $bbox = $gml->bbox;
    echo "Mapbender.events.afterInit.register(highlight_init);\n";
    echo "function highlight_init() {\n";
    echo "var mf = new Array(";
    for ($i = 0; $i < count($e_target); $i++) {
        if ($i > 0) {
            echo ", ";
        }
        echo "'" . $e_target[$i] . "'";
    }
    echo ");\n";
    echo "hl = new Highlight(mf, 'GML_rendering', {'position':'absolute', 'top':'0px', 'left':'0px', 'z-index':" . GML_HIGHLIGHT_Z_INDEX . "});\n";
    echo $gml->exportMemberToJS(0, false);
    echo "hl.add(q);\n";