Copyright (c) 2002, DM Solutions Group Inc. * 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. */ include_once("XMLThemeParser.php"); include_once(dirname(__FILE__)."/../Widget.php"); include_once(dirname(__FILE__)."/../Popup.php"); if (!defined("CHAMELEON_PATH")) { define( "CHAMELEON_PATH", realpath(str_replace("\\","/",dirname(__FILE__))."/..") ); } include_once( CHAMELEON_PATH."/TemplateProcessor.php" ); class XMLThemeLegend extends CWCWidget { var $moPopup = ""; var $mszContextFile= ""; var $mszThemeFile= ""; var $mszRendererFile = ""; var $moThemeParser = ""; var $moTemplateProcessor = ""; var $maSelections = array(); var $mszInputBase = "CWC2_CS"; function XMLThemeLegend() { $this->mszLanguageResource = str_replace("\\","/",dirname(__FILE__))."/XMLThemeLegend.dbf"; // invoke constructor of parent parent::CWCWidget(); $this->moTemplateProcessor = new TemplateProcessor(); // set the description for this widget $this->szWidgetDescription = <<moPopup = new CWCPopup( $this ); $this->mnPriority = PRIORITY_MEDIUM; $this->mnMaturityLevel = MATURITY_BETA; } /** * per-widget initialization to initialize default values from * attributes etc. */ function InitDefaults() { parent::InitDefaults(); $oApp = GetChameleonApplication(); //$this->mszContextFile = $this->GetCurrentContext(); //process selections from tag. if (isset($this->maszContents["SELECTION"])) { for($i=0; $imaszContents["SELECTION"]); $i++) { $aSel = $this->maszContents["SELECTION"][$i]; $aNewSel = array(); $aNewSel["selection_selected"] = "1"; $aNewSel["selection_name"] = $aSel["NAME"]; $aNewSel["selection_contextfile"] = $aSel["CONTEXTFILE"]; $aNewSel["selection_templatefile"] = $aSel["TEMPLATE"]; $aNewSel["selection_rendererfile"] = $aSel["RENDERERFILE"]; $aNewSel["selection_themefile"] = $aSel["THEMEFILE"]; $aNewSel["selection_url"] = "javascript:ContextSelected( '".$aSel["TEMPLATE"]."', '".$aSel["CONTEXTFILE"]."')"; $szFile = $aNewSel['selection_rendererfile']; if (!(substr($szFile, 0, 1) == "/" || substr($szFile, 0, 1) == "\\" || substr($szFile, 1, 1) == ":" || strcasecmp(substr($szFile, 0, 4), "http") == 0)) { $szFile = realpath( $_SESSION['gszAppPath']."/".$szFile ); } $this->mszRendererFile = $oApp->getRemoteFile($szFile); $this->mszThemeFile = $oApp->getRemoteFile($aNewSel["selection_themefile"]); array_push( $this->maSelections, $aNewSel ); } } //print_r($this->maSelections); $this->moPopup->InitDefaults(); } /** * GetJavascriptVariables * * Return JS global variables and global code. */ function GetJavascriptVariables() { $aReturn = array(); if (!is_object( $this->moThemeParser )) { $aReturn["gContextSelectorManager"] = ""; return $aReturn; } $nThemes = $this->moThemeParser->GetNumberOfThemes(); $szVariable = "gContextSelectorManager"; $szValue = " var $szVariable = \"0\";\n"; $aReturn[$szVariable] = $szValue; return $aReturn; } /** * GetJavascriptOnloadFunctions() */ function GetJavascriptOnLoadFunctions() { $aReturn = array(); $szJsName = "ContextSelector_initialize"; $szJs = "ContextSelector_initialize();"; $aReturn[$szJsName] = $szJs; return $aReturn; } /** * GetJavascriptIncludeFunctions * * */ function GetJavascriptIncludeFunctions() { $aReturn = array(); $szJsIncludeName = $_SESSION['gszCoreWebPath'] ."/widgets/js/cwc_cs.js"; $szInclude = ""; $aReturn[$szJsIncludeName] = $szInclude; return $aReturn; } /** * GetHTMLHiddenVariables * */ function GetHTMLHiddenVariables() { /* -------------------------------------------------------------------- */ /* CONTEXTSELECTOR is used to see if it is the first */ /* time that the page loads so we don't setlayer on/off. */ /* -------------------------------------------------------------------- */ $szVariable = "CONTEXTSELECTOR"; $szValue = ""; $aReturn[$szVariable] = $szValue; $szVariable = "CONTEXT"; $szValue = ""; $aReturn[$szVariable] = $szValue; $szVariable = "TEMPLATE"; $szValue = ""; $aReturn[$szVariable] = $szValue; $szVariable = "CONTEXTSELECTOR_CONTEXT_CHANGED"; $szValue = ""; $aReturn[$szVariable] = $szValue; $szLayersOn = ""; $szAllLayers = ""; $oMap = $this->moMapObject->oMap; $nLayers = $oMap->numlayers; $szSep0 = ""; $szSep1 = ""; for ($i=0; $i<$nLayers; $i++) { $oLayer = $oMap->getLayer($i); //get name from wms metadata or mapfile tags $szName = $oLayer->getMetadata("WMS_NAME"); //handle non-wms layers if ($szName == "") { $szName = $oLayer->name; } if (isset($this->moThemeParser->maLayers[$szName]) || ($this->moThemeParser->maGlobals["gThemeExtendContext"] == "true") ) { if ($oLayer->status == MS_ON) { $szLayersOn .= $szSep1 . $szName; $szSep1 = "|"; } $szAllLayers .= $szSep0 . $szName; $szSep0 = "|"; } } //used to contain the name of the layers set to on $szVariable = "CONTEXTSELECTOR_LAYERS_ON"; $szValue = ""; $aReturn[$szVariable] = $szValue; //used to contain the name of the layers set to on $szVariable = "CONTEXTSELECTOR_ALL_LAYERS"; $szValue = ""; $aReturn[$szVariable] = $szValue; //used to test if the template has changed $szVariable = "CONTEXTSELECTOR_TEMPLATE"; $szTemplate = ""; if (isset($_SESSION["gszTemplate"])) $szTemplate = $_SESSION["gszTemplate"]; $szValue = ""; $aReturn[$szVariable] = $szValue; $aReturn[$szVariable] = $szValue; return $aReturn; } /** * GetJavascriptFunctions * */ function GetJavascriptFunctions() { $aReturn = array(); $oMap = $this->moMapObject->oMap; $aszContextLayers = array(); for ($i=0; $i<$oMap->numlayers; $i++) { $oLayer = $oMap->GetLayer( $i ); //set name based on WMS_NAME or mapfile name $szName = $oLayer->getmetadata("WMS_NAME"); if ($szName == "") { $szName = $oLayer->name; } $aszContextLayers[$szName] = ($oLayer->status == MS_ON) ? true : false; } $szInit = " gContextSelectorManager = new CWC2_CS_Manager();\n"; $szInit .= " gContextSelectorManager.form = ".$this->mszHTMLForm.";\n"; if (is_array($this->moThemeParser->maThemes) && count($this->moThemeParser->maThemes) > 0) { foreach($this->moThemeParser->maThemes as $szThemeName => $aGroups) { $szInit .= " oTheme = new CWC2_CS_Theme( \"".$szThemeName."\" );\n"; $szInit .= " gContextSelectorManager.AddTheme( oTheme );\n"; $szType = $this->moThemeParser->GetThemeAttribute( $szThemeName, "theme_type" ); $szInit .= " oTheme.SetType( \"".$szType."\" );\n"; $bThemeOn = ($szType == "checkbox"); foreach ($aGroups as $szGroupName => $aLayersInGroup) { $szInit .= " oGroup = new CWC2_CS_Group( \"".$szGroupName."\" );\n"; if ($this->moThemeParser->GetGroupAttribute( $szThemeName, $szGroupName, "group_visible" ) == "false") { $szInit .= " oGroup.SetVisible( false );\n"; } $szInit .= " oTheme.AddGroup( oGroup );\n"; $bOn = false; $nGroupLayers = 0; foreach( $aLayersInGroup as $szLayerName ) { if (array_key_exists( $szLayerName, $aszContextLayers )) { ++$nGroupLayers; $szInit .= " oGroup.AddLayer( new CWC2_CS_Layer( \"".$szLayerName."\" ) );\n"; $bOn |= $aszContextLayers[$szLayerName]; } } if ($szType == "checkbox" && $nGroupLayers > 0) $bThemeOn &= $bOn; else $bThemeOn |= $bOn; if ($bOn && $nGroupLayers > 0) $szInit .= " oGroup.SetStatus( true );\n"; if ($nGroupLayers == 0) $szInit .= " oGroup.SetVisible( false );\n"; } if ($bThemeOn) $szInit .= " oTheme.SetStatus( true );\n"; } } //process the layers in the map file and see if they are all supported for ($i=0; $i<$oMap->numlayers; $i++) { $oLayer = $oMap->GetLayer( $i ); //get name from wms metadata or mapfile tags $szLayer = $oLayer->getMetaData( "WMS_NAME" ); //handle non-wms layers if ($szLayer == "") { $szLayer = $oLayer->name; } if (!array_key_exists( $szLayer, $this->moThemeParser->maLayers ) ) { if ($this->moThemeParser->maGlobals["gThemeExtendContext"] == "true") { $szInit .= " oTheme = new CWC2_CS_Theme( \"".$szLayer."\" );\n"; $szInit .= " gContextSelectorManager.AddTheme( oTheme );\n"; //$szInit .= " oTheme.SetType( 'checkbox' );\n"; $szInit .= " oTheme.SetType( \"".$szType."\" );\n"; $szInit .= " oGroup = new CWC2_CS_Group( \"".$szLayer."\" );\n"; $szInit .= " oTheme.AddGroup( oGroup );\n"; $szInit .= " oGroup.SetVisible( false );\n"; $szInit .= " oGroup.SetStatus( true );\n"; $szInit .= " oGroup.AddLayer( new CWC2_CS_Layer( \"".$szLayer."\" ) );\n"; if ($oLayer->status == MS_ON) $szInit .= " oTheme.SetStatus( true );\n"; } } } $szInit .= " gContextSelectorManager.UpdateStatus();\n"; $szJsFunctionName = "ContextSelector_initialize"; $szFunction = <<moPopup->mszLink = $_SESSION['gszCoreWebPath']."/widgets/XMLThemeLegend/XMLThemeLegendInfo.phtml"; $this->moPopup->mszParam = "&groups='+groupName+'&groups_abstracts='+groupAbstract+'&groups_layers='+szGroupLayers+'"; $szPopupURL = $this->moPopup->DrawPublish(); $szJsFunctionName = "ContextSelector_openGroupInfo"; $sCommon = COMMON; $sWebPath = $_SESSION['gszCoreWebPath']; $sSID = SID; $szFunction = <<moPopup->mszLink = $_SESSION['gszCoreWebPath']."/widgets/XMLThemeLegend/XMLThemeLegendInfo.phtml"; $this->moPopup->mszParam = "&theme='+themeName+'&theme_abstract='+themeAbstract+'&groups='+szGroups+'&groups_abstracts='+groupAbstract+'&groups_layers='+szGroupLayers+'"; $szJsFunctionName = "ContextSelector_openThemeInfo"; $sCommon = COMMON; $sWebPath = $_SESSION['gszCoreWebPath']; $sSID = SID; $szFunction = <<mszHTMLForm}.CONTEXTSELECTOR_LAYERS_ON.value = gContextSelectorManager.GetVisibleLayerNames(); } EOT; $aReturn[$szJsFunctionName] = $szFunction; $szJsFunctionName = "ContextSelector_SetGroupStatus"; $szFunction = <<mszHTMLForm}.CONTEXTSELECTOR_LAYERS_ON.value = gContextSelectorManager.GetVisibleLayerNames(); } EOT; $aReturn[$szJsFunctionName] = $szFunction; $szJsFunctionName = "ContextSelected"; $szFunction = <<mszHTMLForm}.CONTEXT.value = szContext; {$this->mszHTMLForm}.TEMPLATE.value = szTemplate; {$this->mszHTMLForm}.CONTEXTSELECTOR_CONTEXT_CHANGED.value = "1"; {$this->mszHTMLForm}.submit(); } EOT; $aReturn[$szJsFunctionName] = $szFunction; return $aReturn; } /** * ParseURL * * Loop trough all layers and set them to on/off if found in * the URL. */ function ParseURL() { if ( $this->isVarSet( "THEMEFILE" ) ) $this->mszThemeFile = $this->getVar( "THEMEFILE" ); $this->moThemeParser = new ThemeParser($this->mszThemeFile); //handle unthemed layers //this is part of widget initialization but is done here because it needs //the map object. $this->moThemeParser->ProcessExtraLayers($this->moMapObject->oMap); if ($this->isVarSet( "CONTEXTSELECTOR" )) { $szAllLayers = $this->getVar( "CONTEXTSELECTOR_ALL_LAYERS" ); $aszAllLayers = explode("|", $szAllLayers); $szLayersOn = $this->getVar( "CONTEXTSELECTOR_LAYERS_ON" ); $aszLayersOn = explode("|", $szLayersOn); $oMap = $this->moMapObject->oMap; $nLayers = $oMap->numlayers; for ($i=0; $i<$nLayers; $i++) { $oLayer = $oMap->getLayer($i); $szName = $oLayer->getMetadata("WMS_NAME"); if ($szName == "") { $szName = $oLayer->name; } if (in_array($szName, $aszAllLayers)) { if (in_array( $szName, $aszLayersOn )) { $oLayer->set("status", MS_ON); } else { $oLayer->set("status", MS_OFF); } } } } } /** * CheckThemeOn * * Utility function to set on/off theme checkbox. */ function CheckThemeOn($szThemeName) { $oMap = $this->moMapObject->oMap; $aszGroups = $this->moThemeParser->GetGroups($szThemeName); $szType = $this->moThemeParser->GetThemeAttribute( $szThemeName, "theme_type" ); $bOn = ($szType == "checkbox"); foreach( $aszGroups as $aszLayers ) { foreach( $aszLayers as $szLayerName ) { $oLayer = $this->GetLayerByName($szLayerName); if ($oLayer->status == MS_OFF && $szType == "checkbox") { return false; } else if ($oLayer->status == MS_ON && $szType == "radio") { return true; } } } return $bOn; } /** * GetLayerByName * * Return a layer if it's wms_name or layer name corresponds to the * szTestName passed as argument */ function GetLayerByName( $szTestName ) { $poMap = $this->moMapObject->oMap; for ($i=0;$i<$poMap->numlayers;$i++) { $oLayer = $poMap->getLayer( $i ); $szName = $oLayer->getmetadata( "WMS_NAME" ); //handle non-wms layers if ($szName == "") { $szName = $oLayer->name; } if (strcasecmp(trim($szName), $szTestName) == 0) { return $oLayer; } } return false; } /** * return the status of a group of layers. Assume that if any one * layer is 'on' then they are all on */ function CheckGroupOn( $szThemeName, $szGroupName ) { $aszLayers = $this->moThemeParser->maThemes[$szThemeName][$szGroupName]; foreach( $aszLayers as $layer ) { $oLayer = $this->GetLayerByName($layer); if ($oLayer->status == MS_ON || $oLayer->status == MS_DEFAULT) { return true; } } return false; } /** * GetCurrentContext * * Get the current context used. */ function GetCurrentContext() { $szContext= trim(basename($_SESSION["gszCurrentContext"])); return $szContext; } /** * DrawPublish * * Build layerconrol corresponding to the context and the theme * file associated. * The CWC tag looks like : * * * * */ function DrawPublish() { $oMap = $this->moMapObject->oMap; $szHTML=""; $aVals = $this->moThemeParser->maVals; $aExtraLayers = array(); $aActiveLayers = array(); //pre-process the layers in the map file and see if they are all supported for ($i=0; $i<$oMap->numlayers; $i++) { $oLayer = $oMap->GetLayer( $i ); //removed wms layer check. $szLayer = $oLayer->getMetaData( "WMS_NAME" ); //handle non-wms layers if ($szLayer == "") { $szLayer = $oLayer->name; } if (array_key_exists( $szLayer, $this->moThemeParser->maLayers ) ) { $aActiveLayers[$szLayer] = ($oLayer->status == MS_ON) ? " CHECKED" : ""; } else { if ($this->moThemeParser->maGlobals["gThemeExtendContext"] == "true") { $aExtraLayers[$szLayer] = ($oLayer->status == MS_ON) ? " CHECKED" : ""; } } } //need to update the inputs and urls for themes and groups now $i = 0; //track theme id $nLastThemeId = 0; $nLastGroupId = 0; foreach($this->moThemeParser->maThemes as $szThemeName => $aGroups) { $nThemeGroups = count($aGroups); $szAThemeGroups = "new Array("; $szThemeSep = ""; $nThemeLayers = 0; //if there is a group in the theme if ($nThemeGroups > 0) { $theme_idx = $this->moThemeParser->GetThemeIndex( $szThemeName ); $szThemeChecked = ($this->CheckThemeOn($szThemeName)) ? " CHECKED" : ""; $szThemeAbstract = $this->moThemeParser->GetThemeAttribute( $szThemeName, "theme_abstract" ); $szExtendThemeAbstract = $this->moThemeParser->GetThemeAttribute( $szThemeName, "theme_extendabstract" ); $aVals["theme"][$theme_idx]["theme_input"] = ""; $j = 0; //track current group id foreach ($aGroups as $szGroupName => $aLayersInGroup) { $szChecked = ($this->CheckGroupOn($szThemeName, $szGroupName)) ? " CHECKED" : ""; //if ($nThemeGroups > 1) //{ $szOnClick = "onClick=\"ContextSelector_SetGroupStatus('$i', '$j', this.checked)\""; //} //else // $szOnClick ="onClick=\"ContextSelector_UpdateGroupsOn()\""; $nGroupLayers = 0; $szLayers = ""; $szSep = ""; foreach($aLayersInGroup as $szLayer) { if (array_key_exists( $szLayer, $aActiveLayers) || array_key_exists( $szLayer, $aExtraLayers )) { $szLayers .= $szSep."'".$szLayer."'"; $szSep = ","; ++$nGroupLayers; } } $szExtendGroupAbstract = $this->moThemeParser->GetGroupAttribute( $szThemeName, $szGroupName, "group_extendabstract" ); $szAGroupLayers = "new Array("; if ($szExtendGroupAbstract == "true" && $nGroupLayers > 0) $szAGroupLayers .= $szLayers; $szAGroupLayers .= ")"; $szGroupAbstract = $this->moThemeParser->GetGroupAttribute( $szThemeName, $szGroupName, "group_abstract" ); $szGroupInfo = "'".$szGroupName."', '".$szGroupAbstract."', ".$szAGroupLayers; $group_idx = $this->moThemeParser->GetGroupIndex( $szThemeName, $szGroupName ); $szInputType = $aVals["theme"][$theme_idx]["theme_type"]; $aVals["group"][$group_idx]["group_input"] = ""; $aVals["group"][$group_idx]["group_url"] = "javascript:ContextSelector_openGroupInfo(".$szGroupInfo.")"; $aVals["group"][$group_idx]["group_numlayers"] = "$nGroupLayers"; ++$j; ++$nLastGroupId; if ( $szExtendThemeAbstract == "true" ) { $szAThemeGroups .= $szThemeSep."new Array(".$szGroupInfo.")"; $szThemeSep = ","; } $nThemeLayers += $nGroupLayers; } } $szAThemeGroups .= ")"; $aVals["theme"][$theme_idx]["theme_url"] = "javascript:ContextSelector_openThemeInfo('". $szThemeName."', '".$szThemeAbstract."', ". $szAThemeGroups.");"; $aVals["theme"][$theme_idx]["theme_numlayers"] = "$nThemeLayers"; ++$i; ++$nLastThemeId; } $aVals["selection"] = $this->maSelections; /* * TODO: * This widget should be able to load from a URL. */ if (file_exists( $this->mszRendererFile )) { $szHTML .= $this->moTemplateProcessor->ProcessTemplate( $this->mszRendererFile, $aVals ); } return $szHTML; } }//end of class ?>