/**
     * Stores this WMC in the database. The WMC has to be instantiated first, see above.
     *
     * @return mixed[] an assoc array with attributes "success" (boolean) and "message" (String).
     */
    public function insert($overwrite)
    {
        $result = array();
        if ($this->userId && $this->xml && $this->wmc_title) {
            try {
                $user = new user($this->userId);
            } catch (Exception $E) {
                $errMsg = "Error while saving WMC document " . $this->wmc_title . "': Invalid UserId";
                $result["success"] = false;
                $result["message"] = $errMsg;
                $e = new mb_exception("mod_insertWMCIntoDB: " . $errMsg);
                return $result;
            }
            $overwrite = $user->isPublic() ? false : $overwrite;
            //put keywords into Document
            try {
                $WMCDoc = DOMDocument::loadXML($this->toXml());
            } catch (Exception $E) {
                new mb_exception("WMC XML is broken.");
            }
            $xpath = new DOMXPath($WMCDoc);
            $xpath->registerNamespace("wmc", "http://www.opengis.net/context");
            $xpath->registerNamespace("mapbender", "http://www.mapbender.org/context");
            $xpath->registerNamespace("xlink", "http://www.w3.org/1999/xlink");
            $query_KeywordList = "/wmc:ViewContext/wmc:General/wmc:KeywordList";
            $query_general = "/wmc:ViewContext/wmc:General";
            $DocKeywordLists = $xpath->query($query_KeywordList);
            // we just use a single <general> element
            $NewKeywordList = $WMCDoc->createElementNS('http://opengis.net/context', 'wmc:KeywordList');
            $WMCDoc->appendChild($NewKeywordList);
            foreach ($this->wmc_keyword as $keyword) {
                $Keyword = $WMCDoc->createElementNS('http://opengis.net/context', 'wmc:Keyword', $keyword);
                $NewKeywordList->appendChild($Keyword);
            }
            $generalList = $xpath->query($query_general);
            $general = $generalList->item(0);
            if ($DocKeywordLists->item(0)) {
                $tmpNode = $WMCDoc->importNode($DocKeywordLists->item(0), true);
                $general->replaceChild($NewKeywordList, $tmpNode);
            } else {
                $tmpNode = $WMCDoc->importNode($NewKeywordList, true);
                $general->appendChild($tmpNode);
            }
            $this->xml = $WMCDoc->saveXML();
            db_begin();
            if ($overwrite) {
                $findsql = "SELECT fkey_user_id,wmc_title,wmc_timestamp, wmc_serial_id FROM mb_user_wmc WHERE fkey_user_id = \$1 AND wmc_serial_id = \$2 ORDER BY wmc_timestamp DESC LIMIT 1;";
                $v = array($this->userId, $this->wmc_id);
                $t = array("i", "i");
                $res = db_prep_query($findsql, $v, $t);
                if (db_error()) {
                    $errMsg = "Error while saving WMC document '" . $this->wmc_title . "': " . db_error();
                    $result["success"] = false;
                    $result["message"] = $errMsg;
                    $e = new mb_exception("mod_insertWMCIntoDB: " . $errMsg);
                    return $result;
                }
                if ($row = db_fetch_row($res)) {
                    $sql = "UPDATE mb_user_wmc SET wmc = \$1, wmc_timestamp = \$2, abstract = \$3, srs = \$4, minx = \$5, miny = \$6," . " maxx = \$7, maxy = \$8, wmc_title = \$9 WHERE fkey_user_id = \$10 AND wmc_serial_id=\$11 AND wmc_timestamp = \$12;";
                    $v = array($this->xml, time(), $this->wmc_abstract, $this->wmc_srs, $this->wmc_extent->minx, $this->wmc_extent->miny, $this->wmc_extent->maxx, $this->wmc_extent->maxy, administration::convertOutgoingString($this->wmc_title), $this->userId, $this->wmc_id, $row[2]);
                    $t = array("s", "s", "s", "s", "i", "i", "i", "i", "s", "i", "i", "s");
                    $res = db_prep_query($sql, $v, $t);
                    // need the database Id
                    $wmc_DB_ID = $row[3];
                    $delsqlCustomTopic = "DELETE FROM wmc_custom_category WHERE fkey_wmc_serial_id = \$1;";
                    $delvCustomTopic = array($wmc_DB_ID);
                    $deltCustomTopic = array("s");
                    db_prep_query($delsqlCustomTopic, $delvCustomTopic, $deltCustomTopic);
                    $delsqlInspireTopic = "DELETE FROM wmc_inspire_category WHERE fkey_wmc_serial_id = \$1;";
                    $delvInspireTopic = array($wmc_DB_ID);
                    $deltInspireTopic = array("s");
                    db_prep_query($delsqlInspireTopic, $delvInspireTopic, $deltInspireTopic);
                    $delsql = "DELETE FROM wmc_md_topic_category WHERE fkey_wmc_serial_id = \$1;";
                    $delv = array($wmc_DB_ID);
                    $delt = array("s");
                    db_prep_query($delsql, $delv, $delt);
                    $delkwsql = "DELETE FROM wmc_keyword WHERE fkey_wmc_serial_id = \$1;";
                    $delkwv = array($wmc_DB_ID);
                    $delkwt = array("s");
                    db_prep_query($delkwsql, $delkwv, $delkwt);
                } else {
                    $sql = "SELECT max(wmc_serial_id) AS i FROM mb_user_wmc";
                    $res = db_query($sql);
                    $row = db_fetch_assoc($res);
                    $wmc_DB_ID_new = intval($row["i"]) + 1;
                    $sql = "INSERT INTO mb_user_wmc (" . "wmc_id, fkey_user_id, wmc, wmc_title, wmc_public, wmc_timestamp, wmc_timestamp_create, " . "abstract, srs, minx, miny, maxx, maxy, wmc_serial_id" . ") VALUES (\$1, \$2, \$3, \$4, \$5, \$6, \$7, \$8, \$9, \$10, \$11, \$12, \$13, \$14);";
                    $v = array(time(), $this->userId, $this->xml, administration::convertOutgoingString($this->wmc_title), $this->isPublic() ? 1 : 0, time(), time(), $this->wmc_abstract, $this->wmc_srs, $this->wmc_extent->minx, $this->wmc_extent->miny, $this->wmc_extent->maxx, $this->wmc_extent->maxy, $wmc_DB_ID_new);
                    $t = array("s", "i", "s", "s", "i", "s", "s", "s", "s", "i", "i", "i", "i", "i");
                    $res = db_prep_query($sql, $v, $t);
                    //$sql = "SELECT max(wmc_serial_id) AS i FROM mb_user_wmc";
                    //$res = db_query($sql);
                    //$row = db_fetch_assoc($res);
                    //$wmc_DB_ID = intval($row["i"]);
                }
            } else {
                $sql = "SELECT max(wmc_serial_id) AS i FROM mb_user_wmc";
                $res = db_query($sql);
                $row = db_fetch_assoc($res);
                $wmc_DB_ID_new = intval($row["i"]) + 1;
                $sql = "INSERT INTO mb_user_wmc (" . "wmc_id, fkey_user_id, wmc, wmc_title, wmc_public, wmc_timestamp, wmc_timestamp_create, " . "abstract, srs, minx, miny, maxx, maxy, wmc_serial_id" . ") VALUES (\$1, \$2, \$3, \$4, \$5, \$6, \$7, \$8, \$9, \$10, \$11, \$12, \$13, \$14);";
                //$e = new mb_exception($sql);
                $v = array(time(), $this->userId, $this->xml, administration::convertOutgoingString($this->wmc_title), $this->isPublic() ? 1 : 0, time(), time(), $this->wmc_abstract, $this->wmc_srs, $this->wmc_extent->minx, $this->wmc_extent->miny, $this->wmc_extent->maxx, $this->wmc_extent->maxy, $wmc_DB_ID_new);
                $t = array("s", "i", "s", "s", "i", "s", "s", "s", "s", "i", "i", "i", "i", "i");
                $res = db_prep_query($sql, $v, $t);
            }
            if (db_error()) {
                $errMsg = "Error while saving WMC document '" . $this->wmc_title . "': " . db_error();
                $result["success"] = false;
                $result["message"] = $errMsg;
                $e = new mb_exception("mod_insertWMCIntoDB: " . $errMsg);
            } else {
                // because the wmc id is created each time a wmc is instantiated $this->wmc_id has nothing to do with the database wmc_id
                // this is some duct tape to fix it :-(
                // see also above where wmc_DB_ID is defined if we need to update
                if (!isset($wmc_DB_ID_new)) {
                    $wmc_DB_ID_new = $this->wmc_id;
                }
                // update keywords
                foreach ($this->wmc_keyword as $keyword) {
                    // if a keyword does not yet exist, create it
                    $keywordExistsSql = "SELECT keyword FROM keyword WHERE keyword = \$1";
                    $keywordCreateSql = "INSERT INTO keyword (keyword) VALUES(\$1);";
                    $v = array($keyword);
                    $t = array("s");
                    $res = db_prep_query($keywordExistsSql, $v, $t);
                    if (db_num_rows($res) == 0) {
                        $res = db_prep_query($keywordCreateSql, $v, $t);
                        if ($a = db_error()) {
                        }
                    }
                    $keywordsql = <<<SQL
INSERT INTO wmc_keyword (fkey_keyword_id,fkey_wmc_serial_id)
\tSELECT keyword.keyword_id,\$1 FROM keyword
\tWHERE keyword = \$2 AND keyword.keyword_id NOT IN (
\t\tSELECT fkey_keyword_id FROM wmc_keyword WHERE fkey_wmc_serial_id = \$3
\t)
SQL;
                    $v = array($wmc_DB_ID_new, $keyword, $wmc_DB_ID_new);
                    $t = array("s", "s", "s");
                    $res = db_prep_query($keywordsql, $v, $t);
                    if ($a = db_error()) {
                    }
                }
                // update iso topic categories
                $this->isoTopicCats = $this->isoTopicCats ? $this->isoTopicCats : array();
                foreach ($this->isoTopicCats as $catId) {
                    $catSql = "INSERT INTO wmc_md_topic_category (fkey_wmc_serial_id, fkey_md_topic_category_id) VALUES (\$1,\$2)";
                    $v = array($wmc_DB_ID_new, $catId);
                    $t = array("s", "s");
                    $res = db_prep_query($catSql, $v, $t);
                }
                // update inspire categories
                $this->inspireCats = $this->inspireCats ? $this->inspireCats : array();
                foreach ($this->inspireCats as $catId) {
                    $catSql = "INSERT INTO wmc_inspire_category (fkey_wmc_serial_id, fkey_inspire_category_id) VALUES (\$1,\$2)";
                    $v = array($wmc_DB_ID_new, $catId);
                    $t = array("s", "s");
                    $res = db_prep_query($catSql, $v, $t);
                }
                // update custom categories
                $this->customCats = $this->customCats ? $this->customCats : array();
                foreach ($this->customCats as $catId) {
                    $catSql = "INSERT INTO wmc_custom_category (fkey_wmc_serial_id, fkey_custom_category_id) VALUES (\$1,\$2)";
                    $v = array($wmc_DB_ID_new, $catId);
                    $t = array("s", "s");
                    $res = db_prep_query($catSql, $v, $t);
                }
                $result["success"] = true;
                $msg = "WMC document '" . $this->wmc_title . "' has been saved.";
                $result["message"] = $msg;
                $e = new mb_notice("mod_insertWMCIntoDB: WMC  '" . $this->wmc_title . "' saved successfully.");
            }
        } else {
            $result["success"] = false;
            $errMsg = "missing parameters (user_id: " . $this->userId . ", title: " . $this->wmc_title . ")";
            $result["message"] = $errMsg;
            $e = new mb_exception("mod_insertWMCIntoDB: " . $errMsg . ")");
        }
        db_commit();
        return $result;
    }
$caps = $capabilitiesURL;
#$e = new mb_exception("mod_createJSObjFromXML: CapUrl decodes to load: ".$caps);
$caps = html_entity_decode($_REQUEST['caps']);
//$caps = html_entity_decode(base64_decode($_REQUEST['caps']));
$mywms->createObjFromXML($caps);
$errorMessage = _mb("Error: The Capabilities Document could not be accessed. " . "Please check whether the server is responding and accessible to " . "Mapbender.");
if (!$mywms->wms_status) {
    $output .= "try {" . "Mapbender.Modules.dialogManager.openDialog({" . "content: '" . $errorMessage . "<br><br><b>" . $capabilitiesURL . "', modal: false, effectShow: 'puff'});" . "} catch (e) {" . "prompt('" . $errorMessage . "', '" . $capabilitiesURL . "');" . "}";
} else {
    if ($noHtml) {
        $output .= $mywms->createJsObjFromWMS_(false);
    } else {
        $output .= $mywms->createJsObjFromWMS_(true);
    }
}
$js = administration::convertOutgoingString($output);
unset($output);
if ($noHtml) {
    echo $js;
} else {
    /*
    	$js .= "parent.mod_addWMS_refresh();";
    	echo <<<HTML
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>Load WMS</title>
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="Content-Type" content="text/html; charset='$charset'">	
    $wmcJs = $wmc->toJavaScript(array());
    $wmcJs = implode(";\n", $wmcJs);
    $extentJs = $wmc->extentToJavaScript();
    $output[] = <<<JS
\t\tMapbender.events.afterInit.register(function () {
\t\t\t{$wmcJs};
\t\t});
\t\tMapbender.events.beforeInit.register(function () {
\t\t\t{$extentJs}
\t\t});
JS;
    Mapbender::session()->set("wmcGetApi", $wmcGetApi);
}
$outputString = "";
for ($i = 0; $i < count($output); $i++) {
    $outputString .= administration::convertOutgoingString($output[$i]);
}
$wmcFeaturetypeJson = $wmc->featuretypeConfToJavaScript();
$wfsConfIdString = $wmcGetApi->generalExtensionArray['WFSCONFIDSTRING'];
if ($wfsConfIdString != "") {
    $wmcFeaturetypeStr = <<<JS
\t\tMapbender.events.afterInit.register(function () {
\t\t\t\$('#body').trigger('addFeaturetypeConfs', [
\t\t\t\t{ featuretypeConfObj : {$wmcFeaturetypeJson},
\t\t\t\t\twfsConfIdString: "{$wfsConfIdString}"}
\t\t\t]);
\t\t});
JS;
}
$outputString .= $wmcFeaturetypeStr;
$GeoRSSStr = " Mapbender.events.afterInit.register(function () {";