raimund@588: /* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz
raimund@588:  * Software engineering by Intevation GmbH
raimund@588:  *
raimund@588:  * This file is Free Software under the GNU GPL (v>=3)
raimund@588:  * and comes with ABSOLUTELY NO WARRANTY! Check out
raimund@588:  * the documentation coming with IMIS-Labordaten-Application for details.
raimund@588:  */
raimund@588: 
raimund@588: /*
raimund@588:  * Controller for a Messungengrid
raimund@588:  */
raimund@588: Ext.define('Lada.controller.grid.Messung', {
raimund@588:     extend: 'Ext.app.Controller',
raimund@588: 
raimund@588:     requires: [
raimund@588:         'Lada.view.window.MessungEdit'
raimund@588:     ],
raimund@588: 
dustin@742:     /**
dustin@742:      * Inhitialize the controller
dustin@742:      * It has 3 listeners
dustin@742:      */
raimund@588:     init: function() {
raimund@588:         this.control({
raimund@592:             'messunggrid': {
raimund@588:                 itemdblclick: this.editItem
raimund@588:             },
raimund@592:             'messunggrid button[action=add]': {
raimund@588:                 click: this.add
raimund@588:             },
raimund@592:             'messunggrid button[action=delete]': {
raimund@588:                 click: this.remove
raimund@588:             }
raimund@588:         });
raimund@588:     },
raimund@588: 
dustin@742:     /**
dustin@742:      * This function opens a new {@link Lada.view.window.MessungEdit}
dustin@742:      * Window.
dustin@742:      */
raimund@588:     editItem: function(grid, record) {
raimund@690:         var probe = grid.up('window').record;
dustin@962:         /* Only open a new Window when:
dustin@962:            statusEdit = True
dustin@962:            -or-
dustin@963:            the value of status is not 0
dustin@963:            -or-
dustin@963:            the owner = True
dustin@963: 
dustin@963:            the statusWert attribute is not present in the original data.
dustin@963:            it is appended, when the value and name of the status were
dustin@963:            determined.
dustin@962:         */
dustin@963:         if (record.get('statusEdit')
dustin@963:             || record.get('statusWert') > 0
dustin@963:             || record.get('owner')) {
dustin@962:             var win = Ext.create('Lada.view.window.MessungEdit', {
dustin@962:                 parentWindow: grid.up('window'),
dustin@962:                 probe: probe,
dustin@962:                 record: record,
dustin@962:                 grid: grid
dustin@962:             });
dustin@962:             win.show();
dustin@962:             win.initData();
dustin@962:         }
raimund@588:     },
raimund@588: 
dustin@742:     /**
dustin@742:      * This function opens a window add a Messung
dustin@742:      */
raimund@622:     add: function(button) {
raimund@622:         var probe = button.up('window').record;
raimund@622:         var win = Ext.create('Lada.view.window.MessungCreate', {
raimund@644:             record: probe,
raimund@644:             grid: button.up('messunggrid')
raimund@622:         });
raimund@622:         win.show();
raimund@622:         win.initData();
raimund@588:     },
raimund@588: 
dustin@742:     /**
dustin@742:      * This function removes a Messung
dustin@742:      * It displays a Confirmation-Popup.
dustin@742:      * When the Removal was confirmed and the operation was successful,
dustin@742:      * the Messung-row is removed from the grid.
dustin@742:      * On failure an Errormessage is shown
dustin@742:      */
raimund@588:     remove: function(button) {
raimund@588:         var grid = button.up('grid');
raimund@588:         var selection = grid.getView().getSelectionModel().getSelection()[0];
raimund@588:         Ext.MessageBox.confirm(
raimund@588:             'Messung löschen',
raimund@588:             'Sind Sie sicher?',
raimund@588:             function(btn) {
raimund@588:                 if (btn === 'yes') {
raimund@588:                     selection.destroy({
raimund@588:                         success: function() {
raimund@588:                             button.up('window').initData();
raimund@588:                         },
dustin@705:                         failure: function(request, response) {
dustin@704:                             var json = response.request.scope.reader.jsonData;
dustin@704:                             if (json) {
dustin@704:                                 if (json.message){
dustin@704:                                     Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.delete.title')
dustin@704:                                         +' #'+json.message,
dustin@704:                                         Lada.getApplication().bundle.getMsg(json.message));
dustin@704:                                 } else {
dustin@704:                                     Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.delete.title'),
dustin@704:                                         Lada.getApplication().bundle.getMsg('err.msg.generic.body'));
dustin@704:                                 }
dustin@704:                             } else {
dustin@704:                                 Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.delete.title'),
dustin@704:                                     Lada.getApplication().bundle.getMsg('err.msg.response.body'));
dustin@704:                             }
raimund@588:                         }
raimund@588:                     });
raimund@588:                 }
raimund@622:             }
raimund@622:         );
dustin@826:         grid.down('button[action=delete]').disable();
raimund@588:     }
raimund@588: });