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

root/Chameleon/trunk/Chameleon/ExpressionBuilder/ExpressionBuilder.php

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

Latest Chameleon code checkout from previous repository

Line 
1 <?php
2 /**
3  * @project     GeoDan
4  * @revision    $Id: ExpressionBuilder.php,v 1.9 2004/07/06 06:08:48 bartvde Exp $
5  * @purpose     This contains the supporting php code.
6  * @author      William A. Bronsema, C.E.T. (dev@dmsolutions.ca)
7  * @copyright
8  * <b>Copyright (c) 2003, DM Solutions Group Inc.</b>
9  * Permission is hereby granted, free of charge, to any person obtaining a
10  * copy of this software and associated documentation files (the "Software"),
11  * to deal in the Software without restriction, including without limitation
12  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13  * and/or sell copies of the Software, and to permit persons to whom the
14  * Software is furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included
17  * in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
22  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25  * DEALINGS IN THE SOFTWARE.
26  **/
27
28 //set the language resource file
29 $szLanguageResource = str_replace("\\","/",dirname(__FILE__))."/ExpressionBuilder.dbf";
30
31
32 // set the flag to setup the mapsession
33 if ( !defined("LOAD_MAPSESSION") )
34     define("LOAD_MAPSESSION", 1);
35
36 // include the session handling file (currently only required for MLT)
37 include_once(dirname(__FILE__)."/../session.inc.php");
38 include_once(dirname(__FILE__)."/../CWC2ButtonCache.php");
39
40 // include the layer attribute handling functions
41 include( dirname(__FILE__)."/../LayerAttributes.php" );
42
43 // get the form vars
44 $_FORM = array_merge( $_GET, $_POST );
45
46 /* ============================================================================
47  * Define the default properties for the "LIKE" operator
48  * ========================================================================= */
49 define( "LIKE_WILDCARD", urldecode( $_FORM["wildcard"] ) );
50 define( "LIKE_SINGLECHAR", urldecode( $_FORM["singlechar"] ) );
51 define( "LIKE_ESCAPECHAR", urldecode( $_FORM["escape"] ) );
52
53 /* ============================================================================
54  * Determine the layer specific info
55  * ========================================================================= */
56 // init vars
57 $nCount = 0;
58 $nSelectedLayer = "";
59 $szSelectedLayerName = "";
60 $szSelectedLayerConnType = "";
61 $aszAttributes = array();
62 $szErrorNotice = "";
63 $szWFSConnection = "";
64 $nLayerCount = $oMapSession->oMap->numlayers;
65 $szWFSTypeName = '';
66
67 /* ============================================================================
68  * Get selected WFS layer
69  * ========================================================================= */
70 // check if selected layer list has been passes through URL
71 $aszLayers = array();
72 if ( isset( $_FORM["selectedlayers"] ) &&
73                               strlen( trim( $_FORM["selectedlayers"] ) ) > 0 )
74 {
75     // put layers into array
76     $aszLayers = explode( ',', $_FORM["selectedlayers"] );
77     $nTmpCount = count( $aszLayers );
78     for( $i=0; $i<$nTmpCount; $i++ )
79         $aszLayers[$i] = trim( $aszLayers[$i] );
80
81 }
82
83 // check for layers from the url
84 if ( count( $aszLayers ) > 0 )
85 {
86     //convert WMS_TITLE to WMS_NAME ... this should make sure WMS_NAME is
87     //set first but I didn't have time to do that properly ... it is safe
88     //when working with map files coming from Contexts but possibly not
89     //when working with hand-crafted map files
90     for( $i=0; $i<count($aszLayers); $i++ )
91     {
92         for ($j=0; $j<$nLayerCount; $j++)
93         {
94             $oLayer = $oMapSession->oMap->getLayer($j);
95             if ($oLayer->connectiontype == MS_WMS)
96             {
97                 if ($aszLayers[$i] == $oLayer->getMetadata( "WMS_TITLE" ))
98                 {
99                     $aszLayers[$i] = $oLayer->getMetadata( "WMS_NAME" );
100                 }
101             }
102         }
103     }
104 }
105 else
106 {
107     // get list from metadata
108     for ( $i=0; $i<$nLayerCount; $i++ )
109     {
110         // get layer
111         $oLayer = $oMapSession->oMap->getLayer( $i );
112
113         // check metadata
114         if ( $oLayer->getmetadata("selected") == 1 )
115         {
116             // add to the list of layers
117             array_push( $aszLayers, $oLayer->name );
118         }
119     }
120 }
121
122 // check for an empty first one
123 if ( $aszLayers[0] == '' )
124     array_shift( $aszLayers );
125
126 // update the active layer count
127 $nCount = count( $aszLayers );
128
129 // only process the first WMS or WFS layer
130 for ( $i=0; $i<$nCount; $i++ )
131 {
132     // get layer by name
133     $oLayer = $oMapSession->oMap->getLayerbyName( $aszLayers[$i] );
134     if ($oLayer == null)
135     {
136         //desperate attempt to find layer ...
137         for ($j=0; $j<$oMapSession->oMap->numlayers; $j++)
138         {
139             $oLayer = $oMapSession->oMap->getLayer( $j );
140             if ($oLayer->getMetaData( "WMS_NAME" ) == $aszLayers[$i] ||
141                 $oLayer->getMetaData( "WMS_TITLE" ) == $aszLayers[$i] )
142             {
143                 break;
144             }
145         }
146     }
147     // only process WMS and WFS layers
148     if ( $oLayer->connectiontype == MS_WFS ||
149          $oLayer->connectiontype == MS_WMS )
150     {
151         // record layer index
152         $nSelectedLayer = $oLayer->index;
153
154         // store the layer name to use
155         $szSelectedLayerName = $oLayer->name;
156
157         // store the connection type
158         $szSelectedLayerConnType = $oLayer->connectiontype;
159
160         // process according to layer type
161         switch( $oLayer->connectiontype )
162         {
163             case MS_WFS:
164                 // record the connection string
165                 $szWFSConnection = $oLayer->connection;
166                 break;
167             case MS_WMS:
168                 // get WFS the connection string
169                 $szSelectedLayerName = $oLayer->getMetadata( "WMS_NAME" );
170                 //echo $szSelectedLayerName."<BR>";
171                 $aszWFSInfo = getWFSConnection( $oLayer->connection,
172                                                       $szSelectedLayerName );
173
174                 // function can return false instead of array!
175                 if ($aszWFSInfo != false)
176                 {
177                   $szWFSConnection = $aszWFSInfo['wfs'];
178                   //echo $szWFSConnection."<BR>";
179                   // loop and build typename
180                   foreach( $aszWFSInfo['typename'] as $szTypeName )
181                   {
182                     $szWFSTypeName .= $szTypeName.',';
183                   }
184
185                   // remove trailing ','
186                   if ( substr( $szWFSTypeName, -1, 1 ) == ',' )
187                   {
188                     $szWFSTypeName = substr( $szWFSTypeName, 0, -1 );
189                   }
190
191                   // check for false (meaning no associated WFS)
192                   if ( $szWFSConnection === false )
193                   {
194                       // give error
195                       $szErrorNotice = $oMLT->get("30", 'An invalid WMS layer was '.
196                           'selected.  A WMS layer must have a WFS associated with '.
197                           'it in order to be filtered.' );
198
199                       // reset
200                       $szWFSConnection = '';
201                       $nSelectedLayer = '';
202                       $szSelectedLayerName = '';
203                   }
204                 }
205                 else
206                 {
207                   // give error
208                   $szErrorNotice = $oMLT->get("30", 'An invalid WMS layer was '.
209                       'selected.  A WMS layer must have a WFS associated with '.
210                       'it in order to be filtered.' );
211
212
213                   // reset
214                   $szWFSConnection = '';
215                   $nSelectedLayer = '';
216                   $szSelectedLayerName = '';
217                 }
218                 break;
219         }
220
221         // save expressions if necessary
222         if ( isset( $_FORM['txtSaveExpressions'] ) &&
223                                         $_FORM['txtSaveExpressions'] == 1 )
224         {
225             // store these as current layer's metadata expression
226             $oLayer = $oMapSession->oMap->getLayer( $nSelectedLayer );
227             $oLayer->setMetadata( 'cham_x_build_expressions',
228                                                   $_FORM['txtExpressions'] );
229         }
230
231         // get expressions from metadata
232         $_FORM['txtExpressions'] =
233                         $oLayer->getMetadata( 'cham_x_build_expressions' );
234
235         // one layer has been processed so exit loop
236         break;
237     }
238
239 // for loop
240 }
241
242 /* ============================================================================
243  * Create javascript necessary to load saved expressions
244  * ========================================================================= */
245 if ( isset( $_FORM['txtExpressions'] ) && strlen( $_FORM['txtExpressions'] ) > 0 )
246 {
247     $szJavascriptExpressions =
248                          buildJavascriptExpressions( $_FORM['txtExpressions'] );
249 }
250
251 /* ============================================================================
252  * Process attribute info
253  * ========================================================================= */
254 if ( strlen( trim( $szWFSConnection ) ) > 0 )
255 {
256     // check the the WFS online resource contains the necessary params
257     //echo "fixing WFS url $szWFSConnection for layer $szSelectedLayerName<BR>";
258     $szWFSConnection = fixWFSURL( $szWFSConnection, $szWFSTypeName );
259
260     // get the attribute types
261     $aszAttributes = getAttributeTypes( $szWFSConnection.
262                             'request=describefeaturetype', $szSelectedLayerName );
263
264     // build list of attrtibutes
265     $szAttributeOptions = '';
266     $szLayerType = '';
267     foreach( $aszAttributes as $key=>$value )
268     {
269         // skip layer type
270         if ( $key == 'LayerType' )
271         {
272             $szLayerType = $value;
273             continue;
274         }
275
276         // assemble string
277         $szTmpAttribute = $key." (".$value.")";
278
279         // create new option list item
280         $szAttributeOptions .= "<option value=\"".$key."\">".
281             $szTmpAttribute."</option>\n";
282     }
283 }
284 else
285 {
286     // set error message
287     $szErrorNotice = $oMLT->get("20", "Invalid layer or none selected.  ".
288         "Please ensure that the selected layer is a WFS or WMS layer." );
289 }
290
291 /* ============================================================================
292  * Build style select
293  * ========================================================================= */
294 if ( isset($_FORM['styles']  ) && strlen( $_FORM['styles'] ) > 0 )
295 {
296     // get title
297     $szTmpTitle = $oMLT->get("28", "Select SLD");
298
299     // loop and build list of drop down options
300     $aszStyles = explode( '|', $_FORM['styles'] );
301     $nSLDCount = count( $aszStyles );
302     $szStyleOptions = '';
303     for ( $i=0; $i<$nSLDCount; $i++ )
304     {
305         // separate the name from the sld file
306         // [0] = name
307         // [1] = sld url
308         // [2] = namedlayer
309         // [3] = layer type
310         $aszSplit = explode( '@', $aszStyles[$i] );
311         if (!isset($szLayerType) || strcasecmp($szLayerType, 'MS_LAYER_'.$aszSplit[3] ) == 0)
312         {
313             $szStyleOptions .= '<option value="'.$aszStyles[$i].'">'.$aszSplit[0].
314                                                                             '</option>';
315         }
316     }
317
318     // set
319     $szStyleForm = <<<EOT
320   <tr>
321     <td colspan="2" class="layoutTable">
322       <table class="titleArea" width="100%" border="0" cellpadding="4" cellspacing="0">
323         <tr>
324           <td><span class="title">{$szTmpTitle}</span></td>
325         </tr>
326       </table>
327       <table class="contentArea" width="100%" border="0" cellpadding="4" cellspacing="0">
328         <tr>
329           <td><select name="selSLD" class="inputList">
330                 {$szStyleOptions}
331               </select></td>
332         </tr>
333       </table>
334     </td>
335   </tr>
336 EOT;
337
338 /* ============================================================================
339  * Build layer type HTML
340  * ========================================================================= */
341     if ( !isset( $szLayerType ) || strlen( $szLayerType ) <= 0 )
342     {
343         $szLayerTypeHTML = '<td valign="middle" rowspan="2"><p class="helpArea">'.
344             $oMLT->get('33', 'Unable to determine layer data type.  Please specify the underlying data type for this layer.').
345             '</p></td>'.
346             '</tr>'.
347             '<tr>'.
348             '<td valign="top"><span class="label">'.
349             $oMLT->get("31", "Type").
350             ': </span><select name="selLayerType" class="inputList">'.
351             '<option value="MS_LAYER_POINT">Point</option>'.
352             '<option value="MS_LAYER_LINE">Line</option>'.
353             '<option value="MS_LAYER_POLYGON">Polygon</option></select></td>';
354     }
355     else
356     {
357         if ( !isset( $szLayerType ) ) $szLayerType = '';
358         $szLayerTypeHTML = '<input type="hidden" name="selLayerType" value="'.
359                                                                 $szLayerType.'">';
360     }
361 }
362
363 function buildJavascriptExpressions( $szExpressionsString )
364 {
365
366     // explode individual expressions
367     $aszExpressions = explode( '|@|', $szExpressionsString );
368
369     // init vars
370     $szJavascriptExpressions = "";
371
372     // process each expression
373     foreach( $aszExpressions as $szExpression )
374     {
375         // skip if empty
376         if ( strlen( trim( $szExpression ) ) <= 0 )
377             continue;
378
379         // break apart
380         $aszExpComponents = explode( '|', $szExpression );
381
382         // create javascript to add expressions
383         $szJavascriptExpressions .= 'addExpression( "'.
384                                         $aszExpComponents[0].'", "'.
385                                         $aszExpComponents[1].'", "'.
386                                         $aszExpComponents[2].'", "'.
387                                         $aszExpComponents[4].'", "'.
388                                         $aszExpComponents[5].'", "'.
389                                         $aszExpComponents[2].'", "'.
390                                         $aszExpComponents[3].'", "'.
391                                         $aszExpComponents[4].'", "'.
392                                         $aszExpComponents[5].'" );';
393     }
394
395     // return
396     return $szJavascriptExpressions;
397 }
398
399 ?>
400
Note: See TracBrowser for help on using the browser.