dustin@757: /* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz
dustin@757:  * Software engineering by Intevation GmbH
dustin@757:  *
dustin@757:  * This file is Free Software under the GNU GPL (v>=3)
dustin@757:  * and comes with ABSOLUTELY NO WARRANTY! Check out
dustin@757:  * the documentation coming with IMIS-Labordaten-Application for details.
dustin@757:  */
dustin@757: 
dustin@757: /**
mstanko@1086:  * A Controller for a Messprogramm form
dustin@757:  */
dustin@757: Ext.define('Lada.controller.form.Messprogramm', {
dustin@757:     extend: 'Ext.app.Controller',
dustin@757: 
dustin@797:     requires: [
dustin@797:         'Lada.view.window.MessprogrammOrt'
dustin@797:     ],
dustin@797: 
dustin@757:     /**
dustin@757:      * Initialize the Controller
dustin@757:      */
dustin@757:     init: function() {
dustin@757:         this.control({
dustin@757:             'messprogrammform button[action=save]': {
dustin@757:                 click: this.save
dustin@757:             },
dustin@757:             'messprogrammform button[action=discard]': {
dustin@757:                 click: this.discard
dustin@757:             },
dustin@797:             'messprogrammform button[action=ort]': {
dustin@797:                 click: this.editOrtWindow
dustin@797:             },
dustin@757:             'messprogrammform': {
dustin@757:                 dirtychange: this.dirtyForm
dustin@757:             },
raimund@1070:             'messprogrammform messstellelabor combobox': {
dustin@842:                 select: this.setNetzbetreiber
dustin@798:             },
dustin@797:             'messprogrammform location combobox': {
dustin@797:                 select: this.syncOrtWindow
dustin@797:             },
tom@1197:             'messprogrammform numfield numberfield': {
tom@1197:                 change: this.checkPeriod
dustin@774:             },
dustin@777:             'messprogrammform [name="teilintervallVon"]': {
dustin@757:                 change: this.synchronizeSlider,
dustin@757:             },
dustin@777:             'messprogrammform [name="teilintervallBis"]': {
dustin@777:                 change: this.synchronizeSlider,
dustin@777:             },
dustin@818:             'messprogrammform probenintervall combobox': {
dustin@757:                 select: this.updateIntervalls
raimund@784:             },
tom@1210:             'messprogrammform dayofyear [hidden]': {
tom@1210:                 change: this.alignSubIntervall
tom@1210:             },
raimund@784:             'messprogrammform panel[xtype="deskriptor] combobox': {
raimund@784:                 select: this.deskriptorSelect
dustin@757:             }
dustin@757:         });
dustin@757:     },
dustin@798: 
dustin@798:     /**
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@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@757:     /**
dustin@842:      * When a Messtelle is selected, modify the Netzbetreiber
dustin@842:      * according to the Messstelle
dustin@842:      */
dustin@842:     setNetzbetreiber: function(combo, records){
dustin@842:         var netzbetreiber = combo.up().up('form')
dustin@842:                 .down('netzbetreiber').down('combobox');
dustin@842:         var nbId = records[0].get('netzbetreiberId');
dustin@846:         if (nbId != null) {
dustin@842:             //select the NB in the NB-Combobox
dustin@842:             netzbetreiber.select(nbId);
dustin@842:         }
dustin@842:     },
dustin@842: 
dustin@842:     /**
dustin@757:      * When the Probenintervall was changed, update the Sliders
tom@1210:      * and the numberfield.
dustin@757:      */
tom@1210:     updateIntervalls: function(field) {
dustin@757:         var form = field.up('messprogrammform');
dustin@757:         var record = form.getRecord();
dustin@757:         form.populateIntervall(record, field.getValue());
dustin@757:     },
tom@1198: 
dustin@797:     /**
tom@1210:      * When the validity period was changed, align the subintervall
tom@1210:      * in case of yearly intervall.
tom@1210:      */
tom@1210:     alignSubIntervall: function(field) {
tom@1210:         var form = field.up('messprogrammform');
tom@1210:         var intervall = form.down('probenintervall').down('combobox')
tom@1210:             .getValue();
tom@1210:         if (intervall == 'J') {
tom@1210:             if (field.getName() == 'gueltigVon') {
tom@1210:                 form.down('[name=teilintervallVon]')
tom@1210:                     .setValue(field.getValue());
tom@1210:             } else {
tom@1210:                 form.down('[name=teilintervallBis]')
tom@1210:                     .setValue(field.getValue());
tom@1210:             }
tom@1210:         }
tom@1210:     },
tom@1210: 
tom@1210:     /**
dustin@797:      * The function will open a new Window to edit the Ort of a Messprogramm
dustin@797:      */
dustin@797:     editOrtWindow: function(button) {
dustin@797:         var formPanel = button.up('form');
dustin@797:         //Only Open if the WIndow does not exist, else focus
dustin@797:         if (!formPanel.ortWindow) {
dustin@797:             var data = formPanel.getForm().getFieldValues(true);
dustin@797:             formPanel.ortWindow = Ext.create('Lada.view.window.MessprogrammOrt', {
dustin@797:                 record: formPanel.getRecord(),
dustin@797:                 parentWindow: formPanel.up('window')
dustin@797:             });
dustin@797:             formPanel.ortWindow.show();
dustin@797:             formPanel.ortWindow.initData();
dustin@797:        }
dustin@797:        else {
dustin@797:             formPanel.ortWindow.focus();
dustin@797:             formPanel.ortWindow.setActive(true);
dustin@797:        }
dustin@797:     },
dustin@797: 
dustin@797:     /**
dustin@797:      * When a OrtWindow exist, and the value of the location combobox is changed, update the window.
dustin@797:      */
dustin@797:     syncOrtWindow: function(combo, record){
dustin@797:         var formPanel = combo.up('messprogrammform');
dustin@797:         if (formPanel.ortWindow) {
dustin@797:             var ortwindowlocation = formPanel
dustin@797:                 .ortWindow.down('location')
dustin@797:             var ortwindowcombo = ortwindowlocation
dustin@797:                 .down('combobox');
dustin@797: 
dustin@797:             ortwindowcombo.select(combo.getValue());
dustin@797:             ortwindowlocation.fireEvent('select',
dustin@797:                 ortwindowcombo, ortwindowcombo.record);
dustin@797:        }
dustin@797:     },
dustin@757: 
dustin@757:     /**
dustin@757:      * When the Slider was used,
dustin@757:      * update the Value of the Teilintervallfields
dustin@757:      */
dustin@757:     synchronizeFields: function(slider, newValue, thumb) {
dustin@757:         var formPanel = slider.up('form');
dustin@757:         if (thumb.index == 0) {
dustin@757:             formPanel.getForm()
dustin@757:                 .findField('teilintervallVon')
dustin@757:                 .setValue(newValue);
dustin@757:         }
dustin@757:         else if (thumb.index == 1) {
dustin@757:             formPanel.getForm()
dustin@757:                 .findField('teilintervallBis')
dustin@757:                 .setValue(newValue);
dustin@757:          }
dustin@757: 
dustin@757:     },
dustin@757: 
dustin@757:     /**
dustin@757:      * When the IntervallFields were used,
dustin@757:      * update the Slider
dustin@757:      */
dustin@757:     synchronizeSlider: function(field, newValue, oldValue) {
dustin@757:         var formPanel = field.up('form');
dustin@757:         if (field.name == 'teilintervallVon') {
dustin@757:             formPanel.down('probenintervallslider')
dustin@757:                 .setValue(0, newValue, false);
dustin@757:         }
dustin@757:         else if (field.name == 'teilintervallBis') {
dustin@757:             formPanel.down('probenintervallslider')
dustin@757:                 .setValue(1, newValue, false);
dustin@757:          }
dustin@757: 
dustin@757:     },
dustin@757:     /**
dustin@972:      * The save function saves the content of the Messprogramm form.
dustin@757:      * On success it will reload the Store,
dustin@757:      * on failure, it will display an Errormessage
dustin@757:      */
dustin@757:     save: function(button) {
dustin@757:         var formPanel = button.up('form');
tom@1231:         var data = formPanel.getForm().getFieldValues();
tom@1217:         var orte = Ext.data.StoreManager.get('orte');
raimund@1250:         var gemId;
raimund@1250:         if (data['ortId'] !== null && data['ortId'] !== '') {
raimund@1250:             gemId = orte.getById(data['ortId']).get('gemId');
raimund@1250:         }
dustin@757:         for (var key in data) {
dustin@757:             formPanel.getForm().getRecord().set(key, data[key]);
dustin@757:         }
tom@1217:         formPanel.getForm().getRecord().set('gemId', gemId);
dustin@972:         if (!formPanel.getForm().getRecord().get('letzteAenderung')) {
dustin@972:             formPanel.getForm().getRecord().data.letzteAenderung = new Date();
dustin@972:         }
dustin@757:         formPanel.getForm().getRecord().save({
dustin@757:             success: function(record, response) {
dustin@757:                 var json = Ext.decode(response.response.responseText);
dustin@757:                 if (json) {
dustin@757:                     button.setDisabled(true);
dustin@757:                     button.up('toolbar').down('button[action=discard]')
dustin@757:                         .setDisabled(true);
dustin@757:                     formPanel.clearMessages();
dustin@757:                     formPanel.setRecord(record);
dustin@757:                     formPanel.setMessages(json.errors, json.warnings);
dustin@757:                     if (response.action === 'create' && json.success) {
dustin@757:                         button.up('window').close();
dustin@759:                         var win = Ext.create('Lada.view.window.Messprogramm', {
dustin@757:                             record: record
dustin@757:                         });
dustin@757:                         win.show();
dustin@757:                         win.initData();
dustin@757:                     }
dustin@757:                 }
dustin@757:             },
dustin@757:             failure: function(record, response) {
dustin@757:                 button.setDisabled(true);
dustin@757:                 button.up('toolbar').down('button[action=discard]')
dustin@757:                     .setDisabled(true);
dustin@757:                 var rec = formPanel.getForm().getRecord();
dustin@757:                 rec.dirty = false;
dustin@757:                 formPanel.getForm().loadRecord(record);
dustin@757:                 var json = response.request.scope.reader.jsonData;
dustin@757:                 if (json) {
dustin@757:                     if(json.errors.totalCount > 0 || json.warnings.totalCount > 0){
dustin@757:                         formPanel.setMessages(json.errors, json.warnings);
dustin@757:                     }
dustin@757: 
dustin@757:                     if(json.message){
dustin@757:                         Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.save.title')
dustin@757:                             +' #'+json.message,
dustin@757:                             Lada.getApplication().bundle.getMsg(json.message));
dustin@757:                     } else {
dustin@757:                          Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.save.title'),
dustin@757:                             Lada.getApplication().bundle.getMsg('err.msg.generic.body'));
dustin@757:                     }
dustin@757:                     formPanel.clearMessages();
dustin@757:                     //formPanel.setRecord(record);
dustin@757:                     formPanel.setMessages(json.errors, json.warnings);
dustin@757:                 } else {
dustin@757:                     Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.save.title'),
dustin@757:                         Lada.getApplication().bundle.getMsg('err.msg.response.body'));
dustin@757:                 }
dustin@757: 
dustin@757:             }
dustin@757:         });
dustin@757:     },
dustin@757: 
dustin@757:      /**
tom@1210:       * The discard function resets the form
dustin@757:       * to its original state.
dustin@757:       */
dustin@757:     discard: function(button) {
dustin@757:         var formPanel = button.up('form');
dustin@757:         formPanel.getForm().loadRecord(formPanel.getForm().getRecord());
dustin@757:         formPanel.getForm().owner.populateIntervall(
dustin@757:             formPanel.getForm().getRecord());
dustin@757:     },
dustin@757: 
dustin@757:      /**
dustin@757:       * The dirtyForm function enables or disables the save and discard
dustin@757:       * button which are present in the toolbar of the form.
dustin@757:       * The Buttons are only active if the content of the form was altered
dustin@757:       * (the form is dirty).
dustin@757:       */
dustin@757:     dirtyForm: function(form, dirty) {
dustin@757:         if (dirty) {
dustin@757:             form.owner.down('button[action=save]').setDisabled(false);
dustin@757:             form.owner.down('button[action=discard]').setDisabled(false);
dustin@757:         }
dustin@757:         else {
dustin@757:             form.owner.down('button[action=save]').setDisabled(true);
dustin@757:             form.owner.down('button[action=discard]').setDisabled(true);
dustin@757:         }
dustin@757:     },
dustin@757: 
dustin@757:     /**
dustin@757:      * checkPeriod() is called when a fields defining an intervall
dustin@757:      * were modified
dustin@757:      * The function validates if the start is smaller than end.
dustin@757:      */
dustin@757:     checkPeriod: function(field) {
dustin@757:         // This field might be a field within a Period.
dustin@757:         // Search for Partner field (period: end/start) and validate
dustin@757:         // End Before Start validation
dustin@757:         if (field.period) {
dustin@757:             var partners = new Array();
tom@1194:             partners[0] = field.up('fieldset')
tom@1194:                 .down('numberfield[period=start]').getValue();
tom@1194:             partners[1] = field.up('fieldset')
tom@1194:                 .down('numberfield[period=end]').getValue();
dustin@757:             if (partners[0] && partners[1] && partners[0] > partners [1]) {
dustin@757:                 var msg = Lada.getApplication().bundle.getMsg('662');
tom@1197:                 field.up('fieldset').showWarningOrError(false, '', true, msg);
dustin@757:             } else {
dustin@757:                 field.up('fieldset').clearMessages();
dustin@757:             }
dustin@757:         }
dustin@777:     },
dustin@777: 
raimund@784: 
raimund@784:     deskriptorSelect: function(field, records) {
raimund@784:         var desk = field.up('deskriptor');
raimund@784:         var media = field.up('messprogrammform').down('textfield[name="mediaDesk"]');
raimund@784:         var current = media.getValue().split(' ');
mstanko@1088:         if (current.length < 12) {
ehuber@1108:             var value;
raimund@784:             for (var i = 0; i <= 12; i++) {
raimund@784:                 if (i === 0) {
raimund@784:                     current.push('D:');
raimund@784:                 }
raimund@784:                 else if (i === desk.layer + 1) {
raimund@784:                     var value;
raimund@784:                     if (records[0].get('sn') < 10) {
raimund@784:                         value = '0' + records[0].get('sn');
raimund@784:                     }
raimund@784:                     else {
raimund@784:                         value = records[0].get('sn');
raimund@784:                     }
raimund@784:                     current.push(value);
raimund@784:                 }
raimund@784:                 else {
raimund@784:                     current.push('00');
raimund@784:                 }
raimund@784:             }
raimund@784:         }
raimund@784:         else {
raimund@784:             var value;
raimund@784:             if (records[0].get('sn') < 10) {
raimund@784:                 value = '0' + records[0].get('sn');
raimund@784:             }
raimund@784:             else {
raimund@784:                 value = records[0].get('sn');
raimund@784:             }
raimund@784:             current[desk.layer + 1] = value;
mstanko@1086:             if (desk.layer < 2) {
mstanko@1088:                 for (var i = desk.layer + 2; i < 12; i++) {
mstanko@1086:                     current[i] = '00';
mstanko@1086:                 }
mstanko@1086:                 this.clearChildDesk(desk);
mstanko@1086:             }
mstanko@1086:             else if (desk.layer === 2 && current[1] === '01') {
mstanko@1086:                 current[4] = '00';
mstanko@1086:                 desk.up('fieldset').down('deskriptor[layer=3]').clearValue();
mstanko@1086:             }
raimund@784:         }
raimund@784:         media.setValue(current.join(' ').trim());
ehuber@1108: 
ehuber@1108: 		if (current[0].length == 0) {
ehuber@1108: 			current.splice(0,1);
ehuber@1108: 		}
ehuber@1108: 		var mediatext = field.up('messprogrammform').down('textfield[name="media"]');
ehuber@1108: 		
ehuber@1108: 		if ( (desk.layer === 0 ) && (records[0].get('sn') === 0) ){
ehuber@1108: 				mediatext.setValue('');
ehuber@1108: 		} else {
ehuber@1108: 				if ( current[1] === '01') { 
ehuber@1108: 					if ( (current[5] !== '00') && (desk.layer === 4 ) ){
ehuber@1108: 						//mediatext.setValue(records[0].data.beschreibung);
ehuber@1108: 					} else if ( (current[4] !== '00') && (desk.layer === 3) ) {
ehuber@1108: 						mediatext.setValue(records[0].data.beschreibung);
ehuber@1108: 					} else if ( (current[3] !== '00') && (desk.layer === 2) ) {
ehuber@1108: 						mediatext.setValue(records[0].data.beschreibung);
ehuber@1108: 					} else if ( (current[2] !== '00') && (desk.layer === 1) ) {
ehuber@1108: 						mediatext.setValue(records[0].data.beschreibung);
ehuber@1108: 					} else if ( (current[1] !== '00') && (desk.layer === 0 )) {
ehuber@1108: 						mediatext.setValue(records[0].data.beschreibung);
ehuber@1108: 					} 
ehuber@1108: 				}
ehuber@1108: 				
ehuber@1108: 				if ( current[1] !== '01') { 
ehuber@1108: 					if ((current[2] !== '00') && (desk.layer === 1 )) {
ehuber@1108: 						mediatext.setValue(records[0].data.beschreibung);
ehuber@1108: 					} else if ((current[1] !== '00') && (desk.layer === 0 )) {
ehuber@1108: 						mediatext.setValue(records[0].data.beschreibung);
ehuber@1108: 					} 
ehuber@1108: 				}
ehuber@1108:             }
raimund@784:     },
raimund@784: 
mstanko@1086:     clearChildDesk: function(field) {
raimund@784:         var allS = field.up('fieldset').items.items;
raimund@784:         for (var i = field.layer + 1; i < 12; i++) {
raimund@784:             allS[i].clearValue();
raimund@784:         }
raimund@784:     }
mstanko@1086: 
mstanko@1086:     
raimund@784: });