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(dirname(__FILE__)."/../Widget.php"); include_once(dirname(__FILE__)."/../Button.php"); include_once(dirname(__FILE__)."/../Popup.php"); /** * LegendTemplate * * @desc LegendTemplate widget class */ class LegendTemplate extends CWCWidget { var $mszTemplate; // [string] var $moButton; // Button object var $moPopup; // Popup object var $mbControlVisibility = true; var $mbEmbedded = true; /** * LegendTemplate * * Constctor method for the LegendPopup widget. */ function LegendTemplate() { $this->mszLanguageResource = str_replace("\\","/",dirname(__FILE__))."/LegendPopup.dbf"; // invoke constructor of parent parent::CWCWidget(); // set the description for this widget $this->szWidgetDescription = <<maAttributes["TEMPLATE"] = new StringAttribute( "TEMPLATE", true ); $this->maAttributes['CONTROLVISIBILITY'] = new BooleanAttribute( 'CONTROLVISIBILITY', false ); $this->maAttributes['EMBEDDED'] = new BooleanAttribute( 'EMBEDDED', false ); $this->moButton = new CWCButton( $this ); $this->moPopup = new CWCPopup( $this ); $this->mnMaturityLevel = MATURITY_TECHNICALRELEASE; } function InitDefaults() { parent::InitDefaults(); if (isset($this->maParams['CONTROLVISIBILITY'])) { $this->mbControlVisibility = ($this->maParams['CONTROLVISIBILITY'] == "true") ? true : false; } //this widget should never belong to a toolset $this->maParams["TOOLSET"] = ""; if (isset($this->maParams["EMBEDDED"])) { $this->mbEmbedded = (strcasecmp($this->maParams["EMBEDDED"], "FALSE") == 0) ? false : true; } $this->moPopup->InitDefaults(); if (!$this->mbEmbedded) { $this->moButton->InitDefaults(); $this->moButton->SetOnClick("LegendTemplatePopup", $this->mnId); $szVisibility = "&controlvisibility=".($this->mbControlVisibility ? '1' : '0'); $this->moPopup->mszLink = $_SESSION['gszCoreWebPath']."widgets/LegendTemplate/LegendTemplatePopup.phtml?widgetId='+widgetId+'".$szVisibility; } else { //button not used in this case ... $this->moButton = null; $this->moPopup->mszLink = $_SESSION['gszCoreWebPath']."/widgets/LegendTemplate/LegendInfo.phtml?layerName='+layername+'"; } if (isset($_SESSION['LEGENDTEMPLATE'.$this->mnId]) && isset($this->maParams['TEMPLATE']) ) { if( basename( $_SESSION['LEGENDTEMPLATE'.$this->mnId]) !== trim($this->maParams["TEMPLATE"] ) ) { unset( $_SESSION['LEGENDTEMPLATE'.$this->mnId] ); } } if (!isset($_SESSION['LEGENDTEMPLATE'.$this->mnId])) { if (isset($this->maParams["TEMPLATE"])) { $this->mszTemplate = trim($this->maParams["TEMPLATE"]); if (strtoupper(substr($this->mszTemplate, 0, 7)) == "HTTP://" || strtoupper(substr($this->mszTemplate, 0, 8)) == "HTTPS://") { $aszFile = file($this->mszTemplate); $szTmpName = tempnam($_SESSION['gszTmpPath'], "legendtemplate").".html"; $fh = fopen($szTmpName, "w"); $nbLine = count($aszFile); for($i=0; $i<$nbLine; $i++) { fwrite($fh, $aszFile[$i]); } fclose($fh); $this->mszTemplate = $szTmpName; } else { $oApp= GetChameleonApplication(); //two possibilities ... if (get_class($oApp) == "serviceinstanceapp" && !$oApp->IsAbsolutePath( $this->mszTemplate)) { //then it is relative so we'll try to get it from the (possibly) remote //server by looking relative to the current template's URL $szAppTemplate = $oApp->GetCurrentTemplate(); if ($szAppTemplate !== false) { $this->mszTemplate = dirname( $szAppTemplate ) . "/" . $this->mszTemplate; $szLocalTemplate = $oApp->getRemoteFile( $this->mszTemplate, "html"); if ($szLocalTemplate === false) { //fatal error with this widget? $_SESSION['gErrorManager']->setError(ERR_WARNING, $this->mszTemplate." could not be found relative to ".dirname($szAppTemplate)); $this->mszTemplate = ""; } else $this->mszTemplate = $szLocalTemplate; } else { $_SESSION['gErrorManager']->setError(ERR_WARNING, $this->mszTemplate." could not be found relative to ".dirname($szAppTemplate)); //fatal error with this widget? } } else { $this->mszTemplate = $oApp->resolvePath2($this->mszTemplate, $GLOBALS['gszAppPath'] ); } } } $_SESSION['LEGENDTEMPLATE'.$this->mnId] = $this->mszTemplate; } else $this->mszTemplate = $_SESSION['LEGENDTEMPLATE'.$this->mnId]; } function GetJavascriptInitFunctions() { if (!$this->mbEmbedded) { return $this->moButton->GetJavascriptInitFunctions(); } else return array(); } function GetJavascriptVariables() { $aResult = array(); if (!$this->mbEmbedded) { $aResult = $this->moButton->GetJavascriptVariables(); } } function GetJavascriptIncludeFunctions() { if (!$this->mbEmbedded) { return $this->moButton->GetJavascriptIncludeFunctions(); } else return array(); } /** * GetJavascriptFunctions. * */ function GetJavascriptFunctions() { $poMap = $this->moMapObject->oMap; $aReturn = array(); $aReturn = parent::GetJavascriptFunctions(); $url_sid = SID; if (isset($_SESSION['gszCoreWebPath'])) { $szWidgetPath = $_SESSION['gszCoreWebPath'] . "/widgets/LegendTemplate/"; } else { $szWidgetPath = "widgets/LegendTemplate/"; } $szLayers = "aLayers = new Array();\n"; $oMap = $this->moMapObject->oMap; $icounter = 0; for( $i=0; $i< $oMap->numlayers; $i++) { $oLayer = $oMap->getLayer( $i ); if ($oLayer->status == MS_DELETE) { } else { $oLayer->setmetadata( "layer_index", $icounter ); $szLayers .= 'aLayers['.$icounter.'] = "'.$oLayer->name.'";'."\n"; $icounter++; } } $szJsFunctionName = "LegendTemplateGetLayerName"; $szFunction = <<mbEmbedded) { $szJsFunctionName = "LegendTemplateLayerInfo"; $szButtonJS = $this->moPopup->DrawPublish(); $szFunction = <<mbEmbedded) { $szJsFunctionName = "LegendTemplatePopup"; $szFunction = "function $szJsFunctionName( button, widgetId )\n" . "{\n". $this->moPopup->DrawPublish()."\n". "return;\n" . "}\n"; $aReturn[$szJsFunctionName] = $szFunction; } // add javascript API functions if (isset($this->maSharedResourceWidgets["CWCJSAPI"])) $bCWCJSAPI = 1; else $bCWCJSAPI = 0; if ($bCWCJSAPI) { $szJsFunctionName = "LegendTemplateWRegisterForEvent"; $szFunction = <<mnId}); } EOT; $aReturn[$szJsFunctionName] = $szFunction; } ///////////////////////////////////// return $aReturn; } /** * return an array of javascript functions needed by Legend widget * and called when the page is loaded. * @return array of name = function values */ function GetJavascriptOnLoadFunctions() { if (!$this->mbEmbedded) { $aReturn = $this->moButton->GetJavascriptOnLoadFunctions(); } else $aReturn = parent::GetJavascriptOnLoadFunctions(); if (isset($this->maSharedResourceWidgets["CWCJSAPI"])) { $szJsFunctionName = "LegendTemplateWRegisterForEvent"; $szFunction = "$szJsFunctionName();\n"; $aReturn[$szJsFunctionName] = $szFunction; } return $aReturn; } /** * ParseURL * * Look for layers name in the url and set the status on/off. */ function ParseURL() { if (!$this->mbControlVisibility) return true; // parse layer stuff $oMap = $this->moMapObject->oMap; if ($this->mbEmbedded) { if ($this->isVarSet("LEGENDTEMPLATE_LAYERS")) { $aLayers = explode( "~~", $this->getVar("LEGENDTEMPLATE_LAYERS") ); } else { $aLayers = array(); } if ($this->isVarSet("LEGENDTEMPLATE_RESET") && $this->getVar("LEGENDTEMPLATE_RESET") != "") { } elseif ($this->isVarSet("legendlayername") || (!$this->isVarSet("legendgroupname") && count($aLayers) > 0 )) { if ($this->isVarSet("legendlayername")) $aLegendLayers = $this->getVar("legendlayername"); else $aLegendLayers = array(); $nLayers = count($aLegendLayers); for ($i=0; $i<$oMap->numlayers; $i++) { $oLayer = @$oMap->getlayer($i); if ($oLayer->status == MS_DELETE) continue; if (in_array($oLayer->name, $aLegendLayers)) { $oLayer->set("status", MS_ON); } else { if (in_array($oLayer->name, $aLayers )) { if (!($oLayer->status == MS_DEFAULT)) { $oLayer->set("status", MS_OFF); } } } } } elseif ($this->isVarSet("legendgroupname") || count($aLayers) > 0 ) { if ($this->isVarSet("legendgroupname")) $aLegendGroups = $this->getVar("legendgroupname"); else $aLegendGroups = array(); $nLegendGroups = count($aLegendGroups); $aAllGroups = $oMap->getAllGroupNames(); for ($i=0; $i<$oMap->numlayers; $i++) { $oLayer = @$oMap->getlayer($i); if (in_array($oLayer->name, $aLayers ) && in_array($oLayer->group, $aAllGroups)) { $oLayer->set("status", MS_OFF); } } for ($i=0; $i<$nLegendGroups; $i++) { if (in_array($aLegendGroups[$i], $aAllGroups)) { $aLayerInGroup = $oMap->getLayersIndexByGroup($aLegendGroups[$i]); $nLayerInGroup = count($aLayerInGroup); for ($j=0; $j<$nLayerInGroup; $j++) { $oLayer = @$oMap->getlayer($aLayerInGroup[$j]); if ($oLayer->status == MS_DELETE) continue; if (in_array($oLayer->name, $aLayers )) { $oLayer->set("status", MS_ON); } } } } } } // return success return true; } function GetHTMLHiddenVariables() { if (!$this->mbEmbedded) { $aReturn = $this->moButton->GetHTMLHiddenVariables(); } else { $aReturn = array(); } $szVariable = "LEGENDTEMPLATE_RESET"; $szValue = " \n"; $aReturn[$szVariable] = $szValue; //record layer names so that new layers don't get turned off by mistake $szLayerNames = ""; $szSep = ""; $oMap = $this->moMapObject->oMap; for ($i=0; $i<$oMap->numlayers; $i++) { $oLayer = $oMap->getlayer($i); // Don't include layers that will be deleted. if( $oLayer->status != MS_DELETE )//&& $oLayer->isVisible()) { $szLayerNames .= $szSep . $oLayer->name; $szSep = "~~"; } } $szVariable = "LEGENDTEMPLATE_LAYERS"; $szValue = " \n"; $aReturn[$szVariable] = $szValue; return $aReturn; } /** * DrawPublish * * Return the HTML code that displays the legend based on the * legend template or a link to display the legend in a popup * windows if the paramater EMBEDDED is false. */ function DrawPublish() { $poMap = $this->moMapObject->oMap; $szOldTemplate = $poMap->legend->template; $poMap->legend->set("template",$this->mszTemplate); if (!$this->mbVisible) return ""; if ($this->mszTemplate != "") { /* *************************************************** */ /* If specified template is a remote file then copy it */ /* localy before using it. */ /* *************************************************** */ /* ==================================================================== */ /* Patch to be able to order layers in reverse order that they */ /* appear in the map file. This should be remove as soon as the */ /* legend html mapserver code will support the ascending and */ /* descending order tag. */ /* ==================================================================== */ $nLayers = $poMap->numlayers; for ($i=0; $i<$nLayers; $i++) { $poLayer = $poMap->getlayer($i); $poLayer->setMetaData("LEGEND_ORDER", $i); } if ($this->mbEmbedded) { $szLegendTemp = @$poMap->processLegendTemplate( array()); if (!$szLegendTemp) { $_SESSION['gErrorManager']->setError( ERR_FILE_IO, 'ERROR: MapScript cannot find legend template file '.$poMap->legend->template."." ); //return ""; } //$szReturn ""; // add div tags if in javascript API mode if (isset($this->maSharedResourceWidgets["CWCJSAPI"])) $szReturn = "
".$szLegendTemp."
"; else $szReturn = $szLegendTemp; //echo ""; return $szReturn; } else //not embedded { $szReturn = $this->moButton->DrawPublish(); return $szReturn; } } else // no template : fall back on mapserver legend ?? { $_SESSION['gErrorManager']->setError(ERR_WARNING, trim($this->moMLT->get("1", "No Template found for Legend widget."))); } if ($szOldTemplate != "" && $szOldTemplate != $poMap->legend->template ) $poMap->legend->set("template",$szOldTemplate); } } ?>