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  * @requires plugins/WMSGetFeatureInfo.js
 12  */
 13 
 14 /**
 15  * @namespace framework.plugins
 16  */
 17 Ext.namespace("framework.plugins");
 18 
 19 /** 
 20  * This plugins provides an action which, when active, will issue a
 21  * GetFeatureInfo request to the WMS of all layers on the map. The output
 22  * will be displayed in a popup. 
 23  * @name_ WMSGetFeatureInfoLink
 24  * @class Provides an action which, when active, will issue a GetFeatureInfo request to the WMS
 25  * @constructor
 26  * @extends <a target="_blank" href="http://gxp.opengeo.org/master/doc/lib/plugins/WMSGetFeatureInfo.html">gxp.plugins.WMSGetFeatureInfo</a>
 27  */
 28 framework.plugins.WMSGetFeatureInfoLink = Ext.extend(gxp.plugins.WMSGetFeatureInfo,
 29         /** 
 30          * @lends framework.plugins.WMSGetFeatureInfoLink.prototype 
 31          */
 32                 {
 33                     /**
 34                      * framework_wmsgetfeatureinfolink plugin type.
 35                      * @public
 36                      * @type String
 37                      */
 38                     ptype: "framework_wmsgetfeatureinfolink",
 39                     /** api: method[addActions]
 40                      */
 41 
 42                     addActions: function() {
 43                         this.popupCache = {};
 44 
 45                         var actions = gxp.plugins.WMSGetFeatureInfo.superclass.addActions.call(this, [{
 46                                 tooltip: this.infoActionTip,
 47                                 iconCls: "gxp-icon-getfeatureinfo",
 48                                 buttonText: this.buttonText,
 49                                 toggleGroup: this.toggleGroup,
 50                                 enableToggle: true,
 51                                 allowDepress: true,
 52                                 toggleHandler: function(button, pressed) {
 53                                     for (var i = 0, len = info.controls.length; i < len; i++) {
 54                                         if (pressed) {
 55                                             info.controls[i].activate();
 56                                         } else {
 57                                             info.controls[i].deactivate();
 58                                         }
 59                                     }
 60                                 }
 61                             }]);
 62                         var infoButton = this.actions[0].items[0];
 63 
 64                         var info = {controls: []};
 65                         var updateInfo = function() {
 66                             var queryableLayers = this.target.mapPanel.layers.queryBy(function(x) {
 67                                 return x.get("queryable");
 68                             });
 69 
 70                             var map = this.target.mapPanel.map;
 71                             var control;
 72                             for (var i = 0, len = info.controls.length; i < len; i++) {
 73                                 control = info.controls[i];
 74                                 control.deactivate();  // TODO: remove when http://trac.openlayers.org/ticket/2130 is closed
 75                                 control.destroy();
 76                             }
 77 
 78                             info.controls = [];
 79                             queryableLayers.each(function(x) {
 80 
 81                                 var layer = x.getLayer();
 82                                 var vendorParams = Ext.apply({}, this.vendorParams), param;
 83                                 if (this.layerParams) {
 84                                     for (var i = this.layerParams.length - 1; i >= 0; --i) {
 85                                         param = this.layerParams[i].toUpperCase();
 86                                         vendorParams[param] = layer.params[param];
 87                                     }
 88                                 }
 89                                 var infoFormat = x.get("infoFormat");
 90 
 91                                 if (infoFormat === undefined) {
 92                                     if (x.data.infoFormats) {
 93                                         x.data.infoFormats.forEach(function(info) {
 94                                             if (info == "application/vnd.ogc.wms_xml" || info == "application/vnd.ogc.gml")
 95                                                 infoFormat = info;
 96                                         });
 97                                     }
 98 
 99                                     if (!infoFormat)
100                                         infoFormat = "text/html";
101 
102                                     // TODO: check if chosen format exists in infoFormats array
103                                     // TODO: this will not work for WMS 1.3 (text/xml instead for GML)
104                                     //   infoFormat = this.format == "html" ? "text/html" : "application/vnd.ogc.gml";
105                                 }
106                                 var control = new OpenLayers.Control.WMSGetFeatureInfo(Ext.applyIf({
107                                     url: layer.url,
108                                     queryVisible: true,
109                                     layers: [layer],
110                                     infoFormat: infoFormat,
111                                     vendorParams: vendorParams,
112                                     eventListeners: {
113                                         getfeatureinfo: function(evt) {
114                                             var title = x.get("title") || x.get("name");
115                                             if (infoFormat == "text/html") {
116                                                 var match = evt.text.match(/<body[^>]*>([\s\S]*)<\/body>/);
117                                                 if (match && !match[1].match(/^\s*$/)) {
118                                                     this.displayPopup(evt, title, match[1]);
119                                                 }
120                                             } else if (infoFormat == "text/plain") {
121                                                 this.displayPopup(evt, title, '<pre>' + evt.text + '</pre>');
122                                             } else if (evt.features && evt.features.length > 0) {
123                                                 this.displayPopup(evt, title, null, x.get("getFeatureInfo"));
124                                             }
125                                             if(this.serveltName){
126                                                 this.logCatasto(evt);
127                                             }
128                                         },
129                                         scope: this
130                                     }
131                                 }, this.controlOptions));
132                                 map.addControl(control);
133                                 info.controls.push(control);
134                                 if (infoButton.pressed) {
135                                     control.activate();
136                                 }
137                             }, this);
138 
139                         };
140 
141                         this.target.mapPanel.layers.on("update", updateInfo, this);
142                         this.target.mapPanel.layers.on("add", updateInfo, this);
143                         this.target.mapPanel.layers.on("remove", updateInfo, this);
144 
145                         return actions;
146                     },
147                     
148                     /** Send to server info to log on db.
149                      * @private
150                      * @param {msg} info to send to server
151                      */
152                     logCatasto: function(evt) {
153                         // se la featureInfo e' su un layer del catasto e se ci sono risultati allora viene inviato un log da salvare su db
154                         var url = Ext.urlDecode(evt.object.url);
155                         url = Object.keys(url)[0];
156                         if(url.indexOf(this.castastoUrlService) > -1 && evt.features.length > 0){
157                             var msg = evt.features[0].fid;
158                             for(var i=1;i<evt.features.length;i++){
159                                 msg = msg + ';' + evt.features[i].fid;
160                             }
161                             Ext.Ajax.request({
162                                 method: 'POST',
163                                 url: this.serveltName,
164                                 params: {method: "saveLog", "tipologia": "FEATURE_INFO", operazione: msg},
165                                 success: function(response, opts) {
166                                     console.log('sucess' + response.responseText);
167                                 },
168                                 failure: function(response, opts) {
169                                     console.log('error' + response.responseText);
170                                 }
171                             });
172                         }
173                     },
174                     /** Create a info popup.
175                      * @private
176                      * @param {OpenLayers.Control.GetFeatureInfo} evt the event object control
177                      * @param {String} title use for the title of the results section reporting the info to the user
178                      * @param {Object} text body text
179                      */
180                     displayPopup: function(evt, title, text, featureinfo) {
181                         var popup;
182                         var popupKey = evt.xy.x + "." + evt.xy.y;
183                         featureinfo = featureinfo || {};
184                         if (!(popupKey in this.popupCache)) {
185                             popup = this.addOutput({
186                                 xtype: "gx_popup",
187                                 title: this.popupTitle,
188                                 layout: "accordion",
189                                 fill: false,
190                                 autoScroll: true,
191                                 location: evt.xy,
192                                 map: this.target.mapPanel,
193                                 width: 250,
194                                 height: 300,
195                                 defaults: {
196                                     layout: "fit",
197                                     autoScroll: true,
198                                     autoHeight: true,
199                                     autoWidth: true,
200                                     collapsible: true
201                                 },
202                                 listeners: {
203                                     close: (function(key) {
204                                         return function(panel) {
205                                             delete this.popupCache[key];
206                                         };
207                                     })(popupKey),
208                                     scope: this
209                                 }
210                             });
211                             this.popupCache[popupKey] = popup;
212                         } else {
213                             popup = this.popupCache[popupKey];
214                         }
215 
216                         var features = evt.features, config = [];
217                         if (!text && features) {
218                             var feature;
219                             for (var i = 0, ii = features.length; i < ii; ++i) {
220                                 feature = features[i];
221 
222                                 // find url and create hyperlink
223                                 var myObj = {};
224                                 var searchURL;
225                                 var strValue;
226                                 for (strName in feature.attributes)
227                                 {
228                                     strValue = feature.attributes[strName];
229                                     if (strValue) {
230                                         searchURL = strValue.search("http://");
231                                         if (searchURL > -1) {
232                                             myObj[strName] = function(v) {
233                                                 return '<a href="' + v + '" target="_blank">' + v + '</a>';
234                                             }
235                                         }
236                                         ;
237                                     }
238                                 }
239                                 ;
240                                 config.push(Ext.apply({
241                                     xtype: "propertygrid",
242                                     listeners: {
243                                         'beforeedit': function(e) {
244                                             return false;
245                                         }
246                                     },
247                                     title: feature.fid ? feature.fid : title,
248                                     //title: title,
249                                     //title: title+' ('+feature.gml.featureType+')',
250                                     store: new Ext.grid.PropertyStore({
251                                         sortable: false,
252                                         defaultSortable: false
253                                     }),
254                                     source: feature.attributes,
255                                     customRenderers: myObj
256                                 },
257                                 this.itemConfig
258                                         ));
259 
260                             }
261                         } else if (text) {
262                             config.push(Ext.apply({
263                                 title: title,
264                                 html: text
265                             }, this.itemConfig));
266                         }
267                         popup.add(config);
268                         popup.doLayout();
269                     }
270 
271                 });
272 
273         Ext.preg(framework.plugins.WMSGetFeatureInfoLink.prototype.ptype, framework.plugins.WMSGetFeatureInfoLink);
274