1 /**
  2  * @fileOverview  
  3  * Copyright (c) 2013 Regione Autonoma della Sardegna
  4  * Published under the GPL license.<br>
  5  * See <a href="https://sardegnageoportale.it/webgis/license.txt">https://sardegnageoportale.it/webgis/license.txt</a>
  6  * for the full text of the license.
  7  * @version 0.1
  8  */
  9 
 10 /**
 11  * @namespace framework.plugins
 12  */
 13 
 14 Ext.namespace('framework.widgets');
 15 
 16 /**
 17  * Creates a combo box that handles results from a geocoding service. By
 18  * default it uses Directory OpenLS service by Regione Autonoma della Sardegna, but it can be configured with a custom store
 19  * to use other services standard OpenLS. If the user enters a valid address in the search
 20  * box, the combo's store will be populated with records that match the
 21  * address.
 22  * @name_ RoutingPanel
 23  * @class Creates a form that handles results from a routing service OpenLS.
 24  * @constructor
 25  * @extends <a target="_blank" href="http://docs.sencha.com/extjs/3.4.0/#!/api/Ext.form.FormPanel">Ext.Panel</a>
 26  */
 27 
 28 framework.widgets.ExportSearchPanel = Ext.extend(Ext.Panel,
 29         /** 
 30          * @lends framework.widgets.ExportSearchPanel.prototype 
 31          */
 32                 {
 33                      /** api: config[idOpenPoiCmp]
 34                      *  
 35                      *  id of POI component used get poi search parameters
 36                      *  @public
 37                      *  @type String
 38                      */
 39                     idOpenPoiCmp: null,
 40                     /** api: config[urlService]
 41                      *  
 42                      *  url to geoServer where get the service
 43                      *  @public
 44                      *  @type String
 45                      */
 46                     urlGeoServer: null,
 47                     /** api: config[workspaceName]
 48                      *  
 49                      *  workspace on geoserver where published the layer where make the search
 50                      *  @public
 51                      *  @type String
 52                      */
 53                     workspaceName: null,
 54                     /** api: config[layerName]
 55                      *  
 56                      *  layer on geoserver where make the search
 57                      *  @public
 58                      *  @type String
 59                      */
 60                     layerName: null,
 61                     /** api: config[orderByField]
 62                      *  
 63                      *  field where order resutls
 64                      *  @public
 65                      *  @type String
 66                      */
 67                     orderByField: null,
 68                     /** api: config[serviceType]
 69                      *  
 70                      *  Service type for map server service (query string parameter)
 71                      *  Default WFS
 72                      *  @public
 73                      *  @type String
 74                      */
 75                     serviceType: 'WFS',
 76                     /** api: config[version]
 77                      *  
 78                      *  Version for WFS service (query string parameter)
 79                      *  Default 1.0.0
 80                      *  @public
 81                      *  @type String
 82                      */
 83                     version: '1.0.0',
 84                     /** api: config[GetFeature]
 85                      *  
 86                      *  Request type for WFS service (query string parameter)
 87                      *  Default GetFeature
 88                      *  @public
 89                      *  @type String
 90                      */
 91                     requestType: 'GetFeature',
 92                     /** api: config[infoFormat]
 93                      *  
 94                      *  maxFeatures for WFS service (query string parameter)
 95                      *  Default 50
 96                      *  @public
 97                      *  @type String
 98                      */
 99                     maxFeatures: 50,
100                     /** 
101                      * Create a panel with a form for export functionality.
102                      * @private
103                      */
104                     outputFormats: null,
105                     
106                     map: null,
107                     
108                     divmap: null,
109 
110                     initComponent: function() {
111                         if (!this.map)
112                             this.map = Ext.getCmp(this.divmap).map;
113                         this.geoJSON = new OpenLayers.Format.GeoJSON();
114                         this.outputFormats = new Ext.data.ArrayStore({     
115                             fields: ['code','name'],
116                             data : [['KML','KML'],['GML2','GML'],['application/json','GeoJSON'],['shape-zip','SHP'],['CSV','CSV']]
117                         });
118 
119                         this.comboOutputFormats = new Ext.form.ComboBox({
120                             store: this.outputFormats,
121                             displayField:'name',
122                             typeAhead: true,
123                             mode: 'local',
124                             triggerAction: 'all',
125                             emptyText:'Seleziona un formato...',
126                             selectOnFocus:true,
127                             forceSelection:true,
128                             editable: false,
129                             valueField: 'code',
130                             value: this.outputFormats.data.items[0].json[0],
131                             width: 165
132                         });                       
133 
134                         this.btnSearch = new Ext.Button({
135                             text: "Vai",
136                             cellCls: "sideFormTableLayout",
137                             handler: this.submit,
138                             scope: this
139                         }),
140                         this.items = [
141                             {
142                                 layout: {
143                                     type: 'table',
144                                     columns: 3
145 
146                                 },
147                                 border: false,
148                                 items: [
149                                     {xtype: "label", text: 'Formato di output', cellCls: "sideFormTableLayout"},
150                                     this.comboOutputFormats,
151 //                                    {xtype: "label", text: '', cellCls: "sideFormTableLayout"},
152                                     {
153                                         layout: {
154                                             type: 'table',
155                                             columns: 2
156                                         },
157                                         border: false,
158                                         items: [
159                                             this.btnSearch, {xtype: "label", text: ''}
160                                         ]}
161                                 ]}
162                         ];
163                         framework.widgets.ExportSearchPanel.superclass.initComponent.apply(this, arguments);
164                     },                   
165                     /** 
166                      * Submit search form
167                      * @private
168                      */
169                     submit: function() {
170                         var url = this.urlGeoServer + this.getQueryString();
171                         this.ownerCt.hide();
172                         //document.location = url;
173                         window.open(url);
174                     },                    
175                     /** 
176                      * Create query string for wfs map server service
177                      * @private
178                      */
179                     getQueryString: function() {
180                         
181                         var queryString = '/' + this.workspaceName + '/ows?service='+ this.serviceType + '&version=' + this.version
182                         + '&request=' + this.requestType + '&typeName=' + this.workspaceName + ':' + this.layerName
183                         + '&maxFeatures=' + this.maxFeatures + '&outputFormat=' + this.comboOutputFormats.getValue() 
184                         + '&sortBy=' + this.orderByField + this.getQueryFilter();
185                 
186                 
187       //       Ext.Msg.alert('Avviso', queryString);
188                         
189 //                        service: this.serviceType, version: this.version, request: this.requestType,
190 //                            typeName: this.typeName, maxFeatures: this.maxFeatures, outputFormat: 'application/json',
191 //                            FILTER: this.getQueryFilter(),
192 //                            authorizzationName: this.authorizzationName};
193                         return queryString;
194                     },
195                     /** 
196                      * Create filter for query string
197                      * @private
198                      */
199                     getQueryFilter: function() {
200                         //LABEL, CATEGORY, MACROCATEGORY, ADDRESS, ISTAT
201                         var queryFilter = '&cql_filter=';
202                         if(!this.openPoiCmp)
203                             this.openPoiCmp = Ext.getCmp(this.idOpenPoiCmp);
204                         var count = 0;
205                         var baseParams = this.openPoiCmp.lastInternalSearchParams;
206                         if(baseParams.name && baseParams.name !=''){
207                            queryFilter+= "LABEL ILIKE '" + baseParams.name.replace(/%/g,'%25') +"'";
208                            count++;
209                         }
210                         
211                         if(baseParams.category){
212                             
213                             var arrayCat = baseParams.category.split(',');
214                             
215                             if(count > 0)
216                                queryFilter+= " AND ";
217                             
218                             queryFilter+= "(";
219                             for (var i = 0; i < arrayCat.length; i++) {
220                                 if (i == 0)
221                                     queryFilter+= " CATEGORY = '" + arrayCat[i]+"'";
222                                 else
223                                     queryFilter+= " OR CATEGORY = '" + arrayCat[i]+"'";
224                             }
225                             queryFilter+= ")";
226                             
227                             count++;
228                         }
229                         
230                         if(baseParams.istat){
231                           if(count > 0)
232                                queryFilter+= " AND ";
233                            queryFilter+= " ISTAT = '" + baseParams.istat+"'"; 
234                            
235                            count++;
236                         }
237                         
238                         if(baseParams.address){
239                           if(count > 0)
240                                queryFilter+= " AND ";
241                            queryFilter+= " ADDRESS LIKE '%" + baseParams.address+"%'"; 
242                            
243                            count++;
244                         }
245                         
246                         if(baseParams.extent){
247                            if(count > 0)
248                                queryFilter+= " AND ";
249                            queryFilter+= "INTERSECTS(GEOGPT," + baseParams.extent + ")";
250                         }
251 
252                         return queryFilter;
253                     },
254                     /** 
255                      * Create equal condition for query string filter
256                      * @private
257                      */
258                     getEqualFilter: function(PropertyName, literal) {
259                         return '<PropertyIsEqualTo><PropertyName>' + PropertyName +
260                                 '</PropertyName><Literal>' + literal + '</Literal></PropertyIsEqualTo>'
261                     }
262                 });
263 
264         /** api: xtype = framework_exportsearchpanel */
265         Ext.reg("framework_exportsearchpanel", framework.widgets.ExportSearchPanel);