raimund@603: /* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz
raimund@603:  * Software engineering by Intevation GmbH
raimund@603:  *
raimund@603:  * This file is Free Software under the GNU GPL (v>=3)
raimund@603:  * and comes with ABSOLUTELY NO WARRANTY! Check out
raimund@603:  * the documentation coming with IMIS-Labordaten-Application for details.
raimund@603:  */
raimund@603: 
dustin@742: /*
dustin@1013:  * This is a controller for an Ortszuordnung Form
dustin@742:  */
dustin@1013: Ext.define('Lada.controller.form.Ortszuordnung', {
raimund@603:     extend: 'Ext.app.Controller',
raimund@603: 
dustin@742:     /**
dustin@742:      * Initialize the Controller with 4 listeners
dustin@742:      */
raimund@603:     init: function() {
raimund@603:         this.control({
dustin@1021:             'ortszuordnungform button[action=setOrt]': {
raimund@1274:                 toggle: this.chooseLocation
dustin@1021:             },
dustin@1013:             'ortszuordnungform button[action=save]': {
raimund@603:                 click: this.save
raimund@603:             },
dustin@1013:             'ortszuordnungform button[action=discard]': {
raimund@603:                 click: this.discard
raimund@603:             },
dustin@1013:             'ortszuordnungform': {
raimund@603:                 dirtychange: this.dirtyForm
raimund@603:             }
raimund@603:         });
raimund@603:     },
raimund@603: 
dustin@742:      /**
dustin@972:       * The save function saves the content of the Ort form.
dustin@742:       * On success it will reload the Store,
dustin@742:       * on failure, it will display an Errormessage
dustin@742:       */
dustin@742:      save: function(button) {
dustin@1021: 
dustin@1022:         var formPanel = button.up('ortszuordnungform');
dustin@1022: 
dustin@1021:         //try to disable ortPickerButton:
dustin@1021:         try {
dustin@1022:            formPanel.down('button[action=setOrt]').toggle(false);
dustin@1021:         }
dustin@1021:         catch (e) {
dustin@1021:         }
dustin@1021: 
raimund@603:         var data = formPanel.getForm().getFieldValues(true);
dustin@1013:         var i18n = Lada.getApplication().bundle;
raimund@603:         for (var key in data) {
raimund@603:             formPanel.getForm().getRecord().set(key, data[key]);
raimund@603:         }
dustin@972:         if (!formPanel.getForm().getRecord().get('letzteAenderung')) {
dustin@972:             formPanel.getForm().getRecord().data.letzteAenderung = new Date();
dustin@972:         }
raimund@603:         formPanel.getForm().getRecord().save({
raimund@603:             success: function(record, response) {
raimund@603:                 var json = Ext.decode(response.response.responseText);
raimund@641:                 if (json) {
raimund@603:                     button.setDisabled(true);
raimund@603:                     button.up('toolbar').down('button[action=discard]')
raimund@603:                         .setDisabled(true);
raimund@603:                     formPanel.clearMessages();
raimund@603:                     formPanel.setRecord(record);
raimund@603:                     formPanel.setMessages(json.errors, json.warnings);
raimund@1134:                     formPanel.up('window').parentWindow.initData();
raimund@603:                 }
dustin@1022:                 //try to refresh the Grid of the Probe
dustin@1022:                 try {
dustin@1022:                     formPanel.up('window').parentWindow
dustin@1022:                         .down('ortszuordnunggrid').store.reload();
dustin@1022:                 }
dustin@1022:                 catch (e) {
dustin@1022: 
dustin@1022:                 }
raimund@603:             },
raimund@603:             failure: function(record, response) {
raimund@603:                 button.setDisabled(true);
raimund@603:                 button.up('toolbar').down('button[action=discard]')
raimund@603:                     .setDisabled(true);
raimund@603:                 formPanel.getForm().loadRecord(formPanel.getForm().getRecord());
raimund@603:                 var json = response.request.scope.reader.jsonData;
raimund@603:                 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@1013:                         Ext.Msg.alert(i18n.getMsg('err.msg.save.title')
dustin@701:                             +' #'+json.message,
dustin@1013:                             i18n.getMsg(json.message));
dustin@701:                     } else {
dustin@1013:                          Ext.Msg.alert(i18n.getMsg('err.msg.save.title'),
dustin@1013:                             i18n.getMsg('err.msg.generic.body'));
dustin@695:                     }
dustin@701:                 } else {
dustin@1013:                     Ext.Msg.alert(i18n.getMsg('err.msg.save.title'),
dustin@1013:                         i18n.getMsg('err.msg.response.body'));
raimund@603:                 }
raimund@603:             }
raimund@603:         });
raimund@603:     },
raimund@603: 
dustin@1013:     /**
dustin@1013:      * The discard function resets the Location form
dustin@1013:      * to its original state.
dustin@1013:      */
dustin@1013:     discard: function(button) {
raimund@603:         var formPanel = button.up('form');
dustin@1022:         var record = formPanel.getForm().getRecord();
dustin@1022:         formPanel.getForm().loadRecord(record);
dustin@1022:         try {
dustin@1022:             formPanel.refreshOrt(record.get('ortId'));
dustin@1022:             formPanel.down('button[action=setOrt]').toggle(false);
dustin@1022:         }
dustin@1022:         catch (e) {
dustin@1022:         }
dustin@1022:         //set undirty.
dustin@1022:         formPanel.fireEvent('dirtychange', formPanel.getForm(), false);
raimund@603:     },
raimund@603: 
dustin@1013:     /**
dustin@1021:      * When the button is Active, a Record can be selected.
dustin@1021:      * If the Record was selected from a grid this function
dustin@1021:      *  sets the ortzuordnung.
dustin@1021:      * TODO: Check if the selected Record is a ORT
dustin@1021:      * TODO: Enable picking from Maps
dustin@1021:      */
dustin@1021:      pickOrt: function(button, pressed, opts) {
dustin@1021:         var i18n = Lada.getApplication().bundle;
dustin@1021:         var oForm = button.up('form');
dustin@1021:         var osg = button.up('window').down('ortstammdatengrid');
dustin@1021:         if (button.pressed) {
dustin@1021:             button.setText(i18n.getMsg('ortszuordnung.form.setOrt.pressed'));
dustin@1021:             osg.addListener('select',oForm.setOrt, oForm);
dustin@1021:         }
dustin@1021:         else {
dustin@1021:             button.setText(i18n.getMsg('ortszuordnung.form.setOrt'));
dustin@1021:             osg.removeListener('select',oForm.setOrt, oForm);
dustin@1021:         }
dustin@1021:      },
dustin@1021: 
raimund@1274:     chooseLocation: function(button, pressed, opts) {
raimund@1274:         var win = button.up('window');
raimund@1274:         var gridPanel = win.down('panel[name=ortgrid]');
raimund@1274:         if (pressed) {
raimund@1274:             win.setHeight(Ext.getBody().getViewSize().height - 50);
raimund@1274:             win.setY(25);
raimund@1274:             gridPanel.show();
raimund@1274:         }
raimund@1274:         else {
raimund@1274:             var y = (Ext.getBody().getViewSize().height - 465) / 2
raimund@1274:             win.setHeight(465);
raimund@1274:             win.setY(y);
raimund@1274:             gridPanel.hide();
raimund@1274:         }
raimund@1274:     },
raimund@1274: 
dustin@1021: 
dustin@1021:     /**
dustin@1013:      * The dirtyForm function enables or disables the save and discard
dustin@1013:      * button which are present in the toolbar of the form.
dustin@1013:      * The Buttons are only active if the content of the form was altered
dustin@1013:      * (the form is dirty).
dustin@1013:      */
raimund@603:     dirtyForm: function(form, dirty) {
raimund@603:         if (dirty) {
tom@1147:             if (form.getValues().ortId !== ''
tom@1147:                 && /[UEZA]/.test(form.getValues().ortszuordnungTyp)
tom@1147:                ) {
raimund@1144:                 form.owner.down('button[action=save]').setDisabled(false);
raimund@1144:             }
raimund@603:             form.owner.down('button[action=discard]').setDisabled(false);
raimund@603:         }
raimund@603:         else {
raimund@603:             form.owner.down('button[action=save]').setDisabled(true);
raimund@603:             form.owner.down('button[action=discard]').setDisabled(true);
raimund@603:         }
raimund@603:     }
raimund@603: });