raimund@587: /* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz
raimund@587:  * Software engineering by Intevation GmbH
raimund@587:  *
raimund@587:  * This file is Free Software under the GNU GPL (v>=3)
raimund@587:  * and comes with ABSOLUTELY NO WARRANTY! Check out
raimund@587:  * the documentation coming with IMIS-Labordaten-Application for details.
raimund@587:  */
raimund@587: 
dustin@742: /**
dustin@742:  * A Controller for a Probe form
dustin@742:  */
raimund@587: Ext.define('Lada.controller.form.Probe', {
raimund@587:     extend: 'Ext.app.Controller',
raimund@587: 
dustin@742:     /**
dustin@742:      * Initialize the Controller
dustin@742:      * It has 4 listeners
dustin@742:      */
raimund@587:     init: function() {
raimund@587:         this.control({
raimund@587:             'probeform button[action=save]': {
raimund@587:                 click: this.save
raimund@587:             },
raimund@587:             'probeform button[action=discard]': {
raimund@587:                 click: this.discard
raimund@587:             },
raimund@587:             'probeform': {
raimund@587:                 dirtychange: this.dirtyForm
dustin@717:             },
raimund@925:             'probeform messstelle combobox': {
dustin@798:                 expand: this.filter,
dustin@841:                 keydown: this.filter,
dustin@841:                 select: this.setNetzbetreiber
dustin@798:             },
dustin@717:             'probeform [xtype="datetime"] field': {
dustin@717:                 blur: this.checkDate
raimund@771:             },
raimund@771:             'probeform panel[xtype="deskriptor] combobox': {
raimund@771:                 select: this.deskriptorSelect
raimund@587:             }
raimund@587:         });
raimund@587:     },
raimund@587: 
dustin@742:     /**
dustin@798:      * The Messtellen Store contains ALL Messtellen.
dustin@798:      * Filter the store in this combobox to reduce the choices
dustin@798:      * to the subset which the user is allowed to use.
dustin@967:      *
dustin@967:      * The app.js also creates a messstellenFiltered store,
dustin@967:      * which contains this selection. Maybe this can be used here in future
dustin@967:      * TODO
dustin@798:      */
dustin@798:     filter: function(field) {
dustin@798:         var fil =  Ext.create('Ext.util.Filter', {
dustin@798:             filterFn: function(item) {
dustin@798:                 if (Ext.Array.contains(Lada.mst, item.get('id'))) {
dustin@798:                     return true;
dustin@798:                 }
dustin@798:                 return false;
dustin@798:             }
dustin@798:         });
dustin@798:         field.getStore().filter(fil);
dustin@798:     },
dustin@798: 
dustin@798:     /**
dustin@841:      * When a Messtelle is selected, modify the Netzbetreiber
dustin@841:      * according to the Messstelle
dustin@841:      */
dustin@841:     setNetzbetreiber: function(combo, records){
dustin@841:         var netzbetreiber = combo.up().up('form')
dustin@841:                 .down('netzbetreiber').down('combobox');
dustin@841:         var nbId = records[0].get('netzbetreiberId');
dustin@841: 
dustin@846:         if (nbId != null) {
dustin@841:             //select the NB in the NB-Combobox
dustin@841:             netzbetreiber.select(nbId);
dustin@841:         }
dustin@841:     },
dustin@841: 
dustin@841:     /**
dustin@972:      * The save function saves the content of the Messung form.
dustin@742:      * On success it will reload the Store,
dustin@742:      * on failure, it will display an Errormessage
dustin@742:      */
raimund@587:     save: function(button) {
raimund@587:         var formPanel = button.up('form');
raimund@587:         var data = formPanel.getForm().getFieldValues(true);
raimund@587:         for (var key in data) {
raimund@587:             formPanel.getForm().getRecord().set(key, data[key]);
raimund@587:         }
dustin@972:         if (!formPanel.getForm().getRecord().get('letzteAenderung')) {
dustin@972:             formPanel.getForm().getRecord().data.letzteAenderung = new Date();
dustin@972:         }
raimund@587:         formPanel.getForm().getRecord().save({
raimund@587:             success: function(record, response) {
raimund@587:                 var json = Ext.decode(response.response.responseText);
raimund@620:                 if (json) {
raimund@587:                     button.setDisabled(true);
raimund@587:                     button.up('toolbar').down('button[action=discard]')
raimund@587:                         .setDisabled(true);
raimund@587:                     formPanel.clearMessages();
raimund@587:                     formPanel.setRecord(record);
raimund@587:                     formPanel.setMessages(json.errors, json.warnings);
raimund@639:                     if (response.action === 'create' && json.success) {
raimund@639:                         button.up('window').close();
raimund@639:                         var win = Ext.create('Lada.view.window.ProbeEdit', {
raimund@639:                             record: record
raimund@639:                         });
raimund@639:                         win.show();
raimund@639:                         win.initData();
raimund@639:                     }
raimund@587:                 }
raimund@587:             },
raimund@587:             failure: function(record, response) {
raimund@587:                 button.setDisabled(true);
raimund@587:                 button.up('toolbar').down('button[action=discard]')
raimund@587:                     .setDisabled(true);
raimund@678:                 var rec = formPanel.getForm().getRecord();
raimund@678:                 rec.dirty = false;
raimund@728:                 formPanel.getForm().loadRecord(record);
raimund@587:                 var json = response.request.scope.reader.jsonData;
raimund@587:                 if (json) {
dustin@693:                     if(json.errors.totalCount > 0 || json.warnings.totalCount > 0){
dustin@693:                         formPanel.setMessages(json.errors, json.warnings);
dustin@693:                     }
dustin@693: 
dustin@693:                     if(json.message){
dustin@701:                         Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.save.title')
dustin@701:                             +' #'+json.message,
dustin@693:                             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@693:                     }
dustin@720:                     formPanel.clearMessages();
raimund@728:                     //formPanel.setRecord(record);
dustin@720:                     formPanel.setMessages(json.errors, json.warnings);
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@587:                 }
dustin@720: 
raimund@587:             }
raimund@587:         });
raimund@587:     },
raimund@587: 
dustin@742:      /**
dustin@816:       * The discard function resets the Probe form
dustin@742:       * to its original state.
dustin@742:       */
raimund@587:     discard: function(button) {
raimund@587:         var formPanel = button.up('form');
dustin@816: 
dustin@816:         formPanel.down('fset[name=entnahmePeriod]').clearMessages();
dustin@816:         formPanel.down('fset[name=sollzeitPeriod]').clearMessages();
dustin@816:         formPanel.down('datetime[name=probeentnahmeBeginn]').clearWarningOrError();
dustin@816:         formPanel.down('datetime[name=probeentnahmeEnde]').clearWarningOrError();
dustin@816:         formPanel.down('datetime[name=solldatumBeginn]').clearWarningOrError();
dustin@816:         formPanel.down('datetime[name=solldatumEnde]').clearWarningOrError();
dustin@816: 
raimund@741:         formPanel.down('umwelt').store.clearFilter();
raimund@587:         formPanel.getForm().loadRecord(formPanel.getForm().getRecord());
raimund@587:     },
raimund@587: 
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. Likewise it calls the embedding windows
dustin@742:       * enableChilren() function
dustin@742:       */
raimund@587:     dirtyForm: function(form, dirty) {
raimund@587:         if (dirty) {
raimund@587:             form.owner.down('button[action=save]').setDisabled(false);
raimund@587:             form.owner.down('button[action=discard]').setDisabled(false);
dustin@686:             form.owner.up('window').disableChildren();
raimund@587:         }
raimund@587:         else {
raimund@587:             form.owner.down('button[action=save]').setDisabled(true);
raimund@587:             form.owner.down('button[action=discard]').setDisabled(true);
dustin@686:             form.owner.up('window').enableChildren(); // todo this might not be true in all cases
raimund@587:         }
dustin@717:     },
dustin@717: 
dustin@742:     /**
dustin@742:      * checkDate() is called when a xtype=datetime field was modified
dustin@742:      * It checks for two things:
dustin@742:      *  - Is the date in the future
dustin@742:      *  - Does the date belong to a time period and the end is before start
dustin@742:      * In both cases it adds a warning to the field which was checked.
dustin@742:      */
dustin@717:     checkDate: function(field) {
dustin@717:         var now = Date.now();
dustin@717:         var w = 0 //amount of warnings
dustin@717:         var e = 0 //errors
dustin@717:         var emsg = '';
dustin@717:         var wmsg = '';
dustin@717: 
dustin@717:         if (field.getValue() > now){
dustin@717:             wmsg += Lada.getApplication().bundle.getMsg('661');
dustin@717:             w++;
dustin@717:         }
dustin@717:         // This field might be a field within a DateTime-Period.
dustin@717:         // Search for Partner field (period: end/start) and validate
dustin@717:         // End Before Start validation
dustin@717:         if (field.period) {
dustin@717:             var partners = new Array();
dustin@717:                 partners[0] = field.up('fieldset').down('datetime[period=start]').down().getValue()
dustin@717:                 partners[1] = field.up('fieldset').down('datetime[period=end]').down().getValue()
dustin@717:             if (partners[0] && partners[1] && partners[0] > partners [1]) {
dustin@717:                 var msg = Lada.getApplication().bundle.getMsg('662');
dustin@717:                 field.up('fieldset').showWarningOrError(true, msg, false, '');
dustin@717:             } else {
dustin@717:                 field.up('fieldset').clearMessages();
dustin@717:             }
dustin@717:         }
dustin@717: 
dustin@717:         if (w) {
dustin@717:             field.up().showWarnings(wmsg);
dustin@717:         }
dustin@717:         if (e) {
dustin@717:             field.up().showErrors(emsg);
dustin@717:         }
dustin@717: 
dustin@717:         // Clear Warnings or Errors if none Are Present
dustin@717:         if (w == 0 && e == 0) {
dustin@717:             field.up().clearWarningOrError();
dustin@717:         }
raimund@771:     },
raimund@771: 
raimund@771:     deskriptorSelect: function(field, records) {
raimund@771:         var desk = field.up('deskriptor');
raimund@771:         var media = field.up('probeform').down('textfield[name="mediaDesk"]');
raimund@771:         var current = media.getValue().split(' ');
raimund@829: 
raimund@771:         if (current.length < 13) {
raimund@771:             for (var i = 0; i <= 12; i++) {
raimund@771:                 if (i === 0) {
raimund@771:                     current.push('D:');
raimund@771:                 }
raimund@771:                 else if (i === desk.layer + 1) {
raimund@771:                     var value;
raimund@771:                     if (records[0].get('sn') < 10) {
raimund@771:                         value = '0' + records[0].get('sn');
raimund@771:                     }
raimund@771:                     else {
raimund@771:                         value = records[0].get('sn');
raimund@771:                     }
raimund@771:                     current.push(value);
raimund@771:                 }
raimund@771:                 else {
raimund@771:                     current.push('00');
raimund@771:                 }
raimund@771:             }
raimund@771:         }
raimund@771:         else {
raimund@771:             var value;
raimund@771:             if (records[0].get('sn') < 10) {
raimund@771:                 value = '0' + records[0].get('sn');
raimund@771:             }
raimund@771:             else {
raimund@771:                 value = records[0].get('sn');
raimund@771:             }
raimund@771:             current[desk.layer + 1] = value;
mstanko@932:             if (desk.layer < 2) {
mstanko@932:                 for (var i = desk.layer + 2; i < 13; i++) {
mstanko@932:                     current[i] = '00';
mstanko@932:                 }
mstanko@932:                 this.clearChildDesk(desk);
raimund@925:             }
mstanko@932:             else if (desk.layer === 2 && current[1] === '01') {
mstanko@932:                 current[4] = '00';
raimund@933:                 desk.up('fieldset').down('deskriptor[layer=3]').clearValue();
mstanko@932:             }
raimund@771:         }
raimund@771:         media.setValue(current.join(' ').trim());
raimund@771:     },
raimund@771: 
raimund@925:     clearChildDesk: function(field) {
raimund@771:         var allS = field.up('fieldset').items.items;
raimund@771:         for (var i = field.layer + 1; i < 12; i++) {
raimund@771:             allS[i].clearValue();
raimund@771:         }
raimund@587:     }
raimund@771: 
raimund@587: });