raimund@638: /* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz
raimund@638:  * Software engineering by Intevation GmbH
raimund@638:  *
raimund@638:  * This file is Free Software under the GNU GPL (v>=3)
raimund@638:  * and comes with ABSOLUTELY NO WARRANTY! Check out
raimund@638:  * the documentation coming with IMIS-Labordaten-Application for details.
raimund@638:  */
raimund@638: 
dustin@742: /**
dustin@742:  * This is a Controller for a Location Form
dustin@742:  */
raimund@638: Ext.define('Lada.controller.form.Location', {
raimund@638:     extend: 'Ext.app.Controller',
dustin@742:     /**
dustin@742:      * Initialize the Controller with
dustin@742:      * 5 listeners
dustin@742:      */
raimund@638:     init: function() {
raimund@638:         this.control({
raimund@638:             'locationform button[action=save]': {
raimund@638:                 click: this.save
raimund@638:             },
raimund@638:             'locationform button[action=discard]': {
raimund@638:                 click: this.discard
raimund@638:             },
raimund@638:             'locationform': {
raimund@638:                 dirtychange: this.dirtyForm
raimund@638:             },
raimund@638:             'locationform numberfield[name=latitude]': {
raimund@638:                 change: this.updateFeatureLatitude
raimund@638:             },
raimund@638:             'locationform numberfield[name=longitude]': {
dustin@718:                 change: this.updateFeatureLongitude
raimund@638:             }
raimund@638:         });
raimund@638:     },
raimund@638: 
dustin@742:     /**
dustin@742:      * The save function saves the content of the Location form.
dustin@742:      * On success it will reload the Store,
dustin@742:      * on failure, it will display an Errormessage
dustin@742:      */
raimund@638:     save: function(button) {
raimund@638:         var formPanel = button.up('form');
raimund@638:         var data = formPanel.getForm().getFieldValues(true);
raimund@638:         for (var key in data) {
raimund@638:             formPanel.getForm().getRecord().set(key, data[key]);
raimund@638:         }
dustin@972:         if (!formPanel.getForm().getRecord().get('letzteAenderung')) {
dustin@972:             formPanel.getForm().getRecord().data.letzteAenderung = new Date();
dustin@972:         }
raimund@638:         formPanel.getForm().getRecord().save({
raimund@638:             success: function(record, response) {
raimund@638:                 var json = Ext.decode(response.response.responseText);
raimund@638:                 if (json) {
raimund@638:                     button.setDisabled(true);
raimund@638:                     button.up('toolbar').down('button[action=discard]')
raimund@638:                         .setDisabled(true);
raimund@638:                     formPanel.clearMessages();
raimund@638:                     formPanel.setRecord(record);
raimund@638:                     formPanel.setMessages(json.errors, json.warnings);
raimund@638:                     button.up('window').down('map').locationRecord = null;
raimund@638:                     var orte = button.up('window').down('ortform combobox');
raimund@638:                     orte.store.reload({
raimund@638:                         callback: function() {
raimund@638:                             orte.setValue(record.data.id);
raimund@638:                         }
raimund@638:                     });
raimund@638:                 }
raimund@638:             },
raimund@638:             failure: function(record, response) {
raimund@638:                 button.setDisabled(true);
raimund@638:                 button.up('toolbar').down('button[action=discard]')
raimund@638:                     .setDisabled(true);
raimund@638:                 formPanel.getForm().loadRecord(formPanel.getForm().getRecord());
raimund@638:                 var json = response.request.scope.reader.jsonData;
raimund@638:                 if (json) {
dustin@695:                     if(json.errors.totalCount > 0 || json.warnings.totalCount > 0){
dustin@695:                         formPanel.setMessages(json.errors, json.warnings);
dustin@695:                     }
dustin@695: 
dustin@695:                     if(json.message){
dustin@704:                         Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.save.title')
dustin@701:                             +' #'+json.message,
dustin@695:                             Lada.getApplication().bundle.getMsg(json.message));
dustin@701:                     } else {
dustin@701:                          Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.save.title'),
dustin@701:                             Lada.getApplication().bundle.getMsg('err.msg.generic.body'));
dustin@695:                     }
dustin@701:                 } else {
dustin@701:                     Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.save.title'),
dustin@701:                         Lada.getApplication().bundle.getMsg('err.msg.response.body'));
raimund@638:                 }
raimund@638:             }
raimund@638:         });
raimund@638:     },
raimund@638: 
dustin@742:     /**
dustin@742:      * The discard function resets the Location form
dustin@742:      * to its original state.
dustin@742:      */
raimund@638:     discard: function(button) {
raimund@638:         var formPanel = button.up('form');
raimund@638:         formPanel.getForm().loadRecord(formPanel.getForm().getRecord());
raimund@638:         button.up('window').down('map').locationRecord = null;
raimund@638:     },
raimund@638: 
dustin@742:     /**
dustin@742:      * The dirtyForm function enables or disables the save and discard
dustin@742:      * button which are present in the toolbar of the form.
dustin@742:      * The Buttons are only active if the content of the form was altered
dustin@742:      * (the form is dirty).
dustin@742:      */
raimund@638:     dirtyForm: function(form, dirty) {
raimund@638:         if (dirty) {
raimund@638:             form.owner.down('button[action=save]').setDisabled(false);
raimund@638:             form.owner.down('button[action=discard]').setDisabled(false);
raimund@638:         }
raimund@638:         else {
raimund@638:             form.owner.down('button[action=save]').setDisabled(true);
raimund@638:             form.owner.down('button[action=discard]').setDisabled(true);
raimund@638:         }
raimund@638:     },
raimund@638: 
dustin@742:     /**
dustin@742:      * This function updates the Latitude (heigth-value / y-value) of a feature
dustin@742:      * (geospatial object). The feature can be a marker in the map
dustin@742:      * @param {Ext.form.field.Field} field the Ext.field which was altered
dustin@895:      * @param nValue the new Latitude as WGS84 in decimaldegrees
dustin@742:      */
raimund@638:     updateFeatureLatitude: function(field, nValue) {
raimund@638:         var layer = field.up('window').down('map').selectControl.layer;
raimund@638:         var newLocation = field.up('window').down('map').locationRecord;
raimund@638:         if (layer && layer.selectedFeatures[0] && newLocation) {
raimund@638:             var feature = layer.getFeatureById(layer.selectedFeatures[0].id);
raimund@638:             feature.move(new OpenLayers.LonLat(feature.geometry.x, nValue));
raimund@638:             layer.refresh();
raimund@638:         }
raimund@638:     },
raimund@638: 
dustin@742:     /**
dustin@742:      * This function updates the Longitude (right-value / x-value) of a feature
dustin@742:      * (geospatial object). The feature can be a marker in the map
dustin@742:      * @param {Ext.form.field.Field} field the Ext.field which was altered
dustin@895:      * @param nValue the new Longitude as WGS84 in decimaldegrees
dustin@742:      */
raimund@638:     updateFeatureLongitude: function(field, nValue) {
raimund@638:         var layer = field.up('window').down('map').selectControl.layer;
raimund@638:         var newLocation = field.up('window').down('map').locationRecord;
raimund@638:         if (layer && layer.selectedFeatures[0] && newLocation) {
raimund@638:             var feature = layer.getFeatureById(layer.selectedFeatures[0].id);
raimund@638:             feature.move(new OpenLayers.LonLat(nValue, feature.geometry.y));
raimund@638:             layer.refresh();
raimund@638:         }
raimund@638:     }
raimund@638: });