NCCOOS Trac Projects: Top | Web | Platforms | Processing | Viz | Sprints | Sandbox | (Wind)

root/Chameleon/trunk/Chameleon/LegendTemplate/LegendTemplate.widget.php

Revision 13 (checked in by jcleary, 17 years ago)

Latest Chameleon code checkout from previous repository

Line 
1 <?php
2 /**
3  * LegendTemplate Widget class
4  *
5  * @project     CWC2
6  * @revision    $Id: LegendTemplate.widget.php,v 1.32 2005/05/27 20:15:58 bartvde Exp $
7  * @purpose     Legend Template Widget class
8  * @author      DM Solutions Group (assefa@dmsolutions.ca)
9  * @copyright
10  * <b>Copyright (c) 2002, DM Solutions Group Inc.</b>
11  * Permission is hereby granted, free of charge, to any person obtaining a
12  * copy of this software and associated documentation files (the "Software"),
13  * to deal in the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  * and/or sell copies of the Software, and to permit persons to whom the
16  * Software is furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be included
19  * in all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
24  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  */
29
30 include_once(dirname(__FILE__)."/../Widget.php");
31 include_once(dirname(__FILE__)."/../Button.php");
32 include_once(dirname(__FILE__)."/../Popup.php");
33
34 /**
35  * LegendTemplate
36  *
37  * @desc LegendTemplate widget class
38  */
39 class LegendTemplate extends CWCWidget
40 {
41     var $mszTemplate;      // [string]
42
43     var $moButton;         // Button object
44     var $moPopup;          // Popup object
45     var $mbControlVisibility = true;
46     var $mbEmbedded = true;
47
48
49     /**
50      * LegendTemplate
51      *
52      * Constctor method for the LegendPopup widget.
53      */
54     function LegendTemplate()
55     {
56         $this->mszLanguageResource = str_replace("\\","/",dirname(__FILE__))."/LegendPopup.dbf";
57
58         // invoke constructor of parent
59         parent::CWCWidget();
60
61         // set the description for this widget
62         $this->szWidgetDescription = <<<EOT
63 The Legend widget allows the user display a legend based on a legend template.
64 The Legend can either be embedded in the same page or displayed in a separate window.
65 EOT;
66         $this->maAttributes["TEMPLATE"] = new StringAttribute( "TEMPLATE", true );
67         $this->maAttributes['CONTROLVISIBILITY'] = new BooleanAttribute( 'CONTROLVISIBILITY', false );
68         $this->maAttributes['EMBEDDED'] = new BooleanAttribute( 'EMBEDDED', false );
69
70         $this->moButton = new CWCButton( $this );
71         $this->moPopup = new CWCPopup( $this );
72
73         $this->mnMaturityLevel = MATURITY_TECHNICALRELEASE;
74     }
75
76
77
78     function InitDefaults()
79     {
80         parent::InitDefaults();
81
82         if (isset($this->maParams['CONTROLVISIBILITY']))
83         {
84             $this->mbControlVisibility = ($this->maParams['CONTROLVISIBILITY'] == "true") ? true : false;
85         }
86
87         //this widget should never belong to a toolset
88         $this->maParams["TOOLSET"] = "";
89         if (isset($this->maParams["EMBEDDED"]))
90         {
91             $this->mbEmbedded = (strcasecmp($this->maParams["EMBEDDED"], "FALSE") == 0) ? false : true;
92         }
93
94         $this->moPopup->InitDefaults();
95
96         if (!$this->mbEmbedded)
97         {
98             $this->moButton->InitDefaults();
99             $this->moButton->SetOnClick("LegendTemplatePopup", $this->mnId);
100             $szVisibility = "&controlvisibility=".($this->mbControlVisibility ? '1' : '0');
101             $this->moPopup->mszLink = $_SESSION['gszCoreWebPath']."widgets/LegendTemplate/LegendTemplatePopup.phtml?widgetId='+widgetId+'".$szVisibility;
102         }
103         else
104         {
105             //button not used in this case ...
106             $this->moButton = null;
107             $this->moPopup->mszLink = $_SESSION['gszCoreWebPath']."/widgets/LegendTemplate/LegendInfo.phtml?layerName='+layername+'";
108         }
109
110         if (isset($_SESSION['LEGENDTEMPLATE'.$this->mnId]) && isset($this->maParams['TEMPLATE'])        )
111         {
112             if( basename( $_SESSION['LEGENDTEMPLATE'.$this->mnId]) !== trim($this->maParams["TEMPLATE"] ) )
113             {
114                 unset( $_SESSION['LEGENDTEMPLATE'.$this->mnId] );
115             }
116         }
117
118         if (!isset($_SESSION['LEGENDTEMPLATE'.$this->mnId]))
119         {
120             if (isset($this->maParams["TEMPLATE"]))
121             {
122                 $this->mszTemplate = trim($this->maParams["TEMPLATE"]);
123                 if (strtoupper(substr($this->mszTemplate, 0, 7)) == "HTTP://" ||
124                     strtoupper(substr($this->mszTemplate, 0, 8)) == "HTTPS://")
125                 {
126                     $aszFile = file($this->mszTemplate);
127                     $szTmpName = tempnam($_SESSION['gszTmpPath'], "legendtemplate").".html";
128                     $fh = fopen($szTmpName, "w");
129
130                     $nbLine = count($aszFile);
131
132                     for($i=0; $i<$nbLine; $i++)
133                     {
134                         fwrite($fh, $aszFile[$i]);
135                     }
136                     fclose($fh);
137
138                     $this->mszTemplate = $szTmpName;
139                 }
140                 else
141                 {
142                     $oApp= GetChameleonApplication();
143                     //two possibilities ...
144                     if (get_class($oApp) == "serviceinstanceapp" &&
145                         !$oApp->IsAbsolutePath( $this->mszTemplate))
146                     {
147                         //then it is relative so we'll try to get it from the (possibly) remote
148                         //server by looking relative to the current template's URL
149                         $szAppTemplate = $oApp->GetCurrentTemplate();
150                         if ($szAppTemplate !== false)
151                         {
152                            $this->mszTemplate = dirname( $szAppTemplate ) . "/" . $this->mszTemplate;
153                            $szLocalTemplate = $oApp->getRemoteFile( $this->mszTemplate, "html");
154                            if ($szLocalTemplate === false)
155                            {
156                                //fatal error with this widget?
157                                $_SESSION['gErrorManager']->setError(ERR_WARNING, $this->mszTemplate." could not be found relative to ".dirname($szAppTemplate));
158                                $this->mszTemplate = "";
159                            }
160                            else
161                             $this->mszTemplate = $szLocalTemplate;
162                         }
163                         else
164                         {
165                             $_SESSION['gErrorManager']->setError(ERR_WARNING, $this->mszTemplate." could not be found relative to ".dirname($szAppTemplate));
166                             //fatal error with this widget?
167                         }
168                     }
169                     else
170                     {
171                         $this->mszTemplate = $oApp->resolvePath2($this->mszTemplate, $GLOBALS['gszAppPath'] );
172                     }
173
174                 }
175             }
176             $_SESSION['LEGENDTEMPLATE'.$this->mnId] = $this->mszTemplate;
177         }
178         else
179             $this->mszTemplate = $_SESSION['LEGENDTEMPLATE'.$this->mnId];
180     }
181
182     function GetJavascriptInitFunctions()
183     {
184         if (!$this->mbEmbedded)
185         {
186             return $this->moButton->GetJavascriptInitFunctions();
187         }
188         else
189             return array();
190     }
191
192     function GetJavascriptVariables()
193     {
194         $aResult = array();
195
196         if (!$this->mbEmbedded)
197         {
198             $aResult = $this->moButton->GetJavascriptVariables();
199         }
200
201
202     }
203
204     function GetJavascriptIncludeFunctions()
205     {
206         if (!$this->mbEmbedded)
207         {
208             return $this->moButton->GetJavascriptIncludeFunctions();
209         }
210         else
211             return array();
212     }
213
214     /**
215      * GetJavascriptFunctions.
216      *
217      */
218     function GetJavascriptFunctions()
219     {
220         $poMap = $this->moMapObject->oMap;
221
222         $aReturn = array();
223
224         $aReturn = parent::GetJavascriptFunctions();
225
226         $url_sid = SID;
227         if (isset($_SESSION['gszCoreWebPath']))
228         {
229             $szWidgetPath = $_SESSION['gszCoreWebPath'] . "/widgets/LegendTemplate/";
230         }
231         else
232         {
233             $szWidgetPath = "widgets/LegendTemplate/";
234         }
235
236
237         $szLayers = "aLayers = new Array();\n";
238         $oMap = $this->moMapObject->oMap;
239
240         $icounter = 0;
241
242         for( $i=0; $i< $oMap->numlayers; $i++)
243         {
244             $oLayer = $oMap->getLayer( $i );
245             if ($oLayer->status == MS_DELETE) { }
246             else
247             {
248               $oLayer->setmetadata( "layer_index", $icounter );
249               $szLayers .= 'aLayers['.$icounter.'] = "'.$oLayer->name.'";'."\n";
250               $icounter++;
251             }
252         }
253
254         $szJsFunctionName = "LegendTemplateGetLayerName";
255         $szFunction = <<<EOT
256 function {$szJsFunctionName}( index )
257 {
258 {$szLayers}
259 return aLayers[index];
260 }
261
262 EOT;
263         $aReturn[$szJsFunctionName] = $szFunction;
264
265         if ($this->mbEmbedded)
266         {
267             $szJsFunctionName = "LegendTemplateLayerInfo";
268             $szButtonJS = $this->moPopup->DrawPublish();
269             $szFunction = <<<EOT
270 /**
271  * {$szJsFunctionName}
272  * called to popup infos on the layer into a separate window
273  */
274 function {$szJsFunctionName}(layername)
275 {
276     {$szButtonJS}
277 }
278 EOT;
279
280             $aReturn[$szJsFunctionName] = $szFunction;
281         }
282
283         if (!$this->mbEmbedded)
284         {
285             $szJsFunctionName = "LegendTemplatePopup";
286             $szFunction = "function $szJsFunctionName( button, widgetId )\n" .
287               "{\n".
288               $this->moPopup->DrawPublish()."\n".
289               "return;\n" .
290               "}\n";
291             $aReturn[$szJsFunctionName] = $szFunction;
292         }
293
294         // add javascript API functions
295         if (isset($this->maSharedResourceWidgets["CWCJSAPI"]))
296           $bCWCJSAPI = 1;
297         else
298           $bCWCJSAPI = 0;
299
300         if ($bCWCJSAPI)
301         {
302             $szJsFunctionName = "LegendTemplateWRegisterForEvent";
303             $szFunction = <<<EOT
304 /**
305  * {$szJsFunctionName}
306  * called to register events when the layers change (JSAPI)
307  */
308 function {$szJsFunctionName}()
309 {
310     goCWCJSAPI.RegisterEvent(LAYER_STATUS_CHANGED, "LegendTemplateWLayersChanged");
311     goCWCJSAPI.RegisterEvent(LAYER_ORDER_CHANGED, "LegendTemplateWLayersChanged");
312     goCWCJSAPI.RegisterEvent(MAP_NEW_LAYER_ADDED, "LegendTemplateWLayersChanged");
313 }
314 EOT;
315
316            $aReturn[$szJsFunctionName] = $szFunction;
317            $szJsFunctionName = "LegendTemplateWLayersChanged";
318            $szFunction = <<<EOT
319 /**
320  * {$szJsFunctionName}
321  * called when the layers are changed(JSAPI)
322  */
323 function {$szJsFunctionName}()
324 {
325     goCWCJSAPI.ProcessLegendTemplate({$this->mnId});
326 }
327 EOT;
328             $aReturn[$szJsFunctionName] = $szFunction;
329
330         }
331
332 /////////////////////////////////////
333
334         return $aReturn;
335     }
336
337     /**
338      * return an array of javascript functions needed by Legend widget
339      * and called when the page is loaded.
340      * @return array of name = function values
341      */
342     function GetJavascriptOnLoadFunctions()
343     {
344         if (!$this->mbEmbedded)
345         {
346             $aReturn = $this->moButton->GetJavascriptOnLoadFunctions();
347         }
348         else
349             $aReturn = parent::GetJavascriptOnLoadFunctions();
350
351         if (isset($this->maSharedResourceWidgets["CWCJSAPI"]))
352         {
353             $szJsFunctionName = "LegendTemplateWRegisterForEvent";
354             $szFunction = "$szJsFunctionName();\n";
355             $aReturn[$szJsFunctionName] = $szFunction;
356         }
357
358         return $aReturn;
359     }
360
361     /**
362      * ParseURL
363      *
364      * Look for layers name in the url and set the status on/off.
365      */
366     function ParseURL()
367     {
368         if (!$this->mbControlVisibility)
369             return true;
370
371         // parse layer stuff
372         $oMap = $this->moMapObject->oMap;
373
374         if ($this->mbEmbedded)
375         {
376             if ($this->isVarSet("LEGENDTEMPLATE_LAYERS"))
377             {
378                 $aLayers = explode( "~~", $this->getVar("LEGENDTEMPLATE_LAYERS") );
379             }
380             else
381             {
382                 $aLayers = array();
383             }
384
385             if ($this->isVarSet("LEGENDTEMPLATE_RESET") && $this->getVar("LEGENDTEMPLATE_RESET") != "")
386             {
387             }
388             elseif ($this->isVarSet("legendlayername") || (!$this->isVarSet("legendgroupname") && count($aLayers) > 0 ))
389             {
390                 if ($this->isVarSet("legendlayername"))
391                     $aLegendLayers = $this->getVar("legendlayername");
392                 else
393                     $aLegendLayers = array();
394                 $nLayers = count($aLegendLayers);
395
396                 for ($i=0; $i<$oMap->numlayers; $i++)
397                 {
398                     $oLayer = @$oMap->getlayer($i);
399                     if ($oLayer->status == MS_DELETE)
400                         continue;
401
402                     if (in_array($oLayer->name, $aLegendLayers))
403                     {
404                         $oLayer->set("status", MS_ON);
405                     }
406                     else
407                     {
408                         if (in_array($oLayer->name, $aLayers ))
409                         {
410                             if (!($oLayer->status == MS_DEFAULT))
411                             {
412                               $oLayer->set("status", MS_OFF);
413                             }
414                         }
415                     }
416                 }
417             }
418             elseif ($this->isVarSet("legendgroupname") || count($aLayers) > 0 )
419             {
420                 if ($this->isVarSet("legendgroupname"))
421                     $aLegendGroups = $this->getVar("legendgroupname");
422                 else
423                     $aLegendGroups = array();
424                 $nLegendGroups = count($aLegendGroups);
425                 $aAllGroups = $oMap->getAllGroupNames();
426                 for ($i=0; $i<$oMap->numlayers; $i++)
427                 {
428                     $oLayer = @$oMap->getlayer($i);
429                     if (in_array($oLayer->name, $aLayers ) && in_array($oLayer->group, $aAllGroups))
430                     {
431                         $oLayer->set("status", MS_OFF);
432                     }
433                 }
434
435                 for ($i=0; $i<$nLegendGroups; $i++)
436                 {
437                     if (in_array($aLegendGroups[$i], $aAllGroups))
438                     {
439                         $aLayerInGroup =
440                           $oMap->getLayersIndexByGroup($aLegendGroups[$i]);
441                         $nLayerInGroup = count($aLayerInGroup);
442                         for ($j=0; $j<$nLayerInGroup; $j++)
443                         {
444                             $oLayer = @$oMap->getlayer($aLayerInGroup[$j]);
445                             if ($oLayer->status == MS_DELETE)
446                                 continue;
447                             if (in_array($oLayer->name, $aLayers ))
448                             {
449                                 $oLayer->set("status", MS_ON);
450                             }
451                         }
452                     }
453                 }
454             }
455         }
456
457         // return success
458         return true;
459     }
460
461     function GetHTMLHiddenVariables()
462     {
463         if (!$this->mbEmbedded)
464         {
465             $aReturn = $this->moButton->GetHTMLHiddenVariables();
466         }
467         else
468         {
469             $aReturn = array();
470         }
471
472         $szVariable = "LEGENDTEMPLATE_RESET";
473         $szValue = " <INPUT TYPE=HIDDEN NAME=$szVariable VALUE=\"\">\n";
474         $aReturn[$szVariable] = $szValue;
475
476         //record layer names so that new layers don't get turned off by mistake
477         $szLayerNames = "";
478         $szSep = "";
479         $oMap = $this->moMapObject->oMap;
480         for ($i=0; $i<$oMap->numlayers; $i++)
481         {
482             $oLayer = $oMap->getlayer($i);
483             // Don't include layers that will be deleted.
484             if( $oLayer->status != MS_DELETE )//&& $oLayer->isVisible())
485             {
486                 $szLayerNames .= $szSep . $oLayer->name;
487                 $szSep = "~~";
488             }
489         }
490
491         $szVariable = "LEGENDTEMPLATE_LAYERS";
492         $szValue = " <INPUT TYPE=\"HIDDEN\" NAME=\"$szVariable\" VALUE=\"$szLayerNames\">\n";
493         $aReturn[$szVariable] = $szValue;
494
495         return $aReturn;
496     }
497
498     /**
499      * DrawPublish
500      *
501      * Return the HTML code that displays the legend based on the
502      * legend template or a link to display the legend in a popup
503      * windows if the paramater EMBEDDED is false.
504      */
505     function DrawPublish()
506     {
507         $poMap = $this->moMapObject->oMap;
508         $szOldTemplate = $poMap->legend->template;
509         $poMap->legend->set("template",$this->mszTemplate);
510         if (!$this->mbVisible)
511             return "<!-- LegendTemplateWidget is hidden -->";
512
513
514         if ($this->mszTemplate != "")
515         {
516 /* *************************************************** */
517 /* If specified template is a remote file then copy it */
518 /* localy before using it.                             */
519 /* *************************************************** */
520 /* ==================================================================== */
521 /*      Patch to be able to order layers in reverse order that they     */
522 /*      appear in the map file. This should be remove as soon as the    */
523 /*      legend html mapserver code will support the ascending and       */
524 /*      descending order tag.                                           */
525 /* ==================================================================== */
526             $nLayers = $poMap->numlayers;
527             for ($i=0; $i<$nLayers; $i++)
528             {
529                 $poLayer = $poMap->getlayer($i);
530                 $poLayer->setMetaData("LEGEND_ORDER", $i);
531             }
532             if ($this->mbEmbedded)
533             {
534                 $szLegendTemp = @$poMap->processLegendTemplate( array());
535
536                 if (!$szLegendTemp)
537                 {
538                     $_SESSION['gErrorManager']->setError( ERR_FILE_IO, 'ERROR: MapScript cannot find legend template file '.$poMap->legend->template."." );
539                     //return "<!-- error in legend template $this->mszTemplate -->";
540                 }
541                 //$szReturn "<TABLE BORDER=\"0\" CELLSPACING=\"0\" CELLPADDING=\"2\" >";
542
543                 // add div tags if in javascript API mode
544                 if (isset($this->maSharedResourceWidgets["CWCJSAPI"]))
545                     $szReturn = "<div name=\"legendTemplateDiv\" id=\"legendTemplateDiv\"><table>".$szLegendTemp."</table></div>";
546                 else
547                     $szReturn = $szLegendTemp;
548                 //echo "</TABLE>";
549
550                 return $szReturn;
551             }
552             else //not embedded
553             {
554                 $szReturn = $this->moButton->DrawPublish();
555
556                 return $szReturn;
557             }
558         }
559         else // no template : fall back on mapserver legend ??
560         {
561             $_SESSION['gErrorManager']->setError(ERR_WARNING, trim($this->moMLT->get("1", "No Template found for Legend widget.")));
562         }
563         if ($szOldTemplate != "" &&
564             $szOldTemplate != $poMap->legend->template )
565             $poMap->legend->set("template",$szOldTemplate);
566
567     }
568
569 }
570 ?>
571
Note: See TracBrowser for help on using the browser.