dustin@611: /* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz
dustin@611:  * Software engineering by Intevation GmbH
dustin@611:  *
dustin@611:  * This file is Free Software under the GNU GPL (v>=3)
dustin@611:  * and comes with ABSOLUTELY NO WARRANTY! Check out
dustin@611:  * the documentation coming with IMIS-Labordaten-Application for details.
dustin@611:  */
dustin@611: 
dustin@742: /**
dustin@742:  * This is a controller for a Messung form
dustin@742:  */
dustin@611: Ext.define('Lada.controller.form.Messung', {
dustin@611:     extend: 'Ext.app.Controller',
dustin@611: 
dustin@742:     /**
dustin@742:      * Initialize the Controller
dustin@742:      * It has 3 listeners
dustin@742:      */
dustin@611:     init: function() {
dustin@611:         this.control({
dustin@611:             'messungform button[action=save]': {
dustin@611:                 click: this.save
dustin@611:             },
dustin@611:             'messungform button[action=discard]': {
dustin@611:                 click: this.discard
dustin@611:             },
dustin@611:             'messungform': {
dustin@611:                 dirtychange: this.dirtyForm
dustin@611:             }
dustin@611:         });
dustin@611:     },
dustin@611: 
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:      */
dustin@611:     save: function(button) {
dustin@611:         var formPanel = button.up('form');
raimund@713:         formPanel.setLoading(true);
dustin@611:         var data = formPanel.getForm().getFieldValues(true);
dustin@611:         for (var key in data) {
dustin@611:             formPanel.getForm().getRecord().set(key, data[key]);
dustin@611:         }
dustin@611:         formPanel.getForm().getRecord().save({
dustin@611:             success: function(record, response) {
dustin@611:                 var json = Ext.decode(response.response.responseText);
raimund@622:                 if (json) {
dustin@611:                     button.setDisabled(true);
dustin@611:                     button.up('toolbar').down('button[action=discard]')
dustin@611:                         .setDisabled(true);
dustin@611:                     formPanel.clearMessages();
dustin@611:                     formPanel.setRecord(record);
dustin@611:                     formPanel.setMessages(json.errors, json.warnings);
raimund@710:                     formPanel.up('window').initData();
raimund@644:                     formPanel.up('window').grid.store.reload();
raimund@710:                     var parentWin = button.up('window').grid.up('window');
raimund@710:                     parentWin.initData();
raimund@639:                     if (response.action === 'create' && json.success) {
raimund@710:                         var oldWin = button.up('window');
raimund@710:                         var probe = oldWin.record;
raimund@710:                         oldWin.close();
raimund@639:                         var win = Ext.create('Lada.view.window.MessungEdit', {
raimund@710:                             probe: probe,
raimund@710:                             parentWindow: parentWin,
raimund@710:                             grid: oldWin.grid,
raimund@639:                             record: record
raimund@639:                         });
raimund@639:                         win.show();
raimund@639:                         win.initData();
raimund@639:                     }
dustin@611:                 }
raimund@713:                 formPanel.setLoading(false);
dustin@611:             },
dustin@611:             failure: function(record, response) {
dustin@611:                 button.setDisabled(true);
dustin@611:                 button.up('toolbar').down('button[action=discard]')
dustin@611:                     .setDisabled(true);
dustin@611:                 formPanel.getForm().loadRecord(formPanel.getForm().getRecord());
dustin@611:                 var json = response.request.scope.reader.jsonData;
dustin@611:                 if (json) {
dustin@972:                     if (json.errors || json.warnings) {
dustin@972:                         if (json.errors.totalCount > 0 || json.warnings.totalCount > 0) {
dustin@972:                             formPanel.setMessages(json.errors, json.warnings);
dustin@972:                         }
dustin@693:                     }
raimund@712:                     if (json.message) {
dustin@701:                         Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.save.title')
raimund@712:                             + ' #' + json.message,
dustin@695:                             Lada.getApplication().bundle.getMsg(json.message));
raimund@712:                     }
raimund@712:                     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@693:                     }
dustin@720:                     formPanel.clearMessages();
dustin@720:                     formPanel.setRecord(record);
dustin@720:                     formPanel.setMessages(json.errors, json.warnings);
dustin@720:                     formPanel.up('window').initData();
dustin@720:                     formPanel.up('window').grid.store.reload();
dustin@720:                   }
raimund@712:                 else {
dustin@701:                     Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.save.title'),
dustin@701:                         Lada.getApplication().bundle.getMsg('err.msg.response.body'));
dustin@611:                 }
raimund@713:                 formPanel.setLoading(false);
dustin@611:             }
dustin@611:         });
dustin@611:     },
dustin@611: 
dustin@742:      /**
dustin@742:       * The discard function resets the Location form
dustin@742:       * to its original state.
dustin@742:       */
dustin@742:      discard: function(button) {
dustin@611:         var formPanel = button.up('form');
dustin@611:         formPanel.getForm().loadRecord(formPanel.getForm().getRecord());
dustin@611:     },
dustin@611: 
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:       * In Additon it calls the disableChildren() function of the window
dustin@742:       * embedding the form. Only when the record does not carry the readonly
dustin@742:       * flag, the function calls the embedding windows enableChilren() function
dustin@742:       */
dustin@742:      dirtyForm: function(form, dirty) {
dustin@611:         if (dirty) {
dustin@611:             form.owner.down('button[action=save]').setDisabled(false);
dustin@611:             form.owner.down('button[action=discard]').setDisabled(false);
dustin@687:             form.owner.up('window').disableChildren();
dustin@611:         }
dustin@611:         else {
dustin@611:             form.owner.down('button[action=save]').setDisabled(true);
dustin@611:             form.owner.down('button[action=discard]').setDisabled(true);
dustin@715:             //Only enable children if the form was not readOnly
dustin@715:             if (!form.getRecord().get('readonly')) {
dustin@715:                 form.owner.up('window').enableChildren();
dustin@715:             }
dustin@611:         }
dustin@611:     }
dustin@611: });