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': { mkrambach@1288: validitychange: this.validityChange, mkrambach@1288: dirtychange: this.validityChange 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: } mkrambach@1289: var data = formPanel.getForm().getFieldValues(false); dustin@1013: var i18n = Lada.getApplication().bundle; mkrambach@1289: //TODO: the forms seem not to submit anything into record. mkrambach@1289: var recordData = formPanel.getForm().getRecord().data; mkrambach@1289: if (recordData['probeId'] !== undefined) { mkrambach@1289: // TODO: as model.ort.ortId and model.ortszuordnung.ortId coexist, mkrambach@1289: // but mean different things, data.ortId is an array here, of mkrambach@1289: // which we need the first entry mkrambach@1289: recordData.ortId = data.ortId[0]; mkrambach@1289: recordData.ortszuordnungTyp = data.ortszuordnungTyp; mkrambach@1289: } else { //messprogramm mkrambach@1289: recordData.ortsTyp = data.ortsTyp; mkrambach@1289: recordData.ort = data.ort; raimund@603: } mkrambach@1289: recordData.ortszusatztext = data.ortszusatztext; mkrambach@1289: if (!data.letzteAenderung) { mkrambach@1289: recordData.letzteAenderung = new Date(); mkrambach@1289: } else { mkrambach@1289: recordData.letzteAenderung = data.letzteAenderung; 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: 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: formPanel.getForm().loadRecord(formPanel.getForm().getRecord()); raimund@603: var json = response.request.scope.reader.jsonData; raimund@603: if (json) { mkrambach@1288: if(Object.keys(json.errors).length > 0 || mkrambach@1288: Object.keys(json.warnings).length > 0) { mkrambach@1288: 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@1021: * When the button is Active, a Record can be selected. dustin@1021: * If the Record was selected from a grid this function mkrambach@1288: * sets the ortzuordnung. dustin@1021: */ mkrambach@1288: chooseLocation: function(button, pressed, opts) { dustin@1021: var i18n = Lada.getApplication().bundle; raimund@1274: var win = button.up('window'); raimund@1274: var gridPanel = win.down('panel[name=ortgrid]'); mkrambach@1288: var osg = win.down('ortstammdatengrid'); mkrambach@1288: var oForm = button.up('form'); mkrambach@1289: osg.addListener('select',oForm.setOrt, oForm); mkrambach@1293: var map = win.down('map'); raimund@1274: if (pressed) { raimund@1274: win.setHeight(Ext.getBody().getViewSize().height - 50); mkrambach@1288: button.setText(i18n.getMsg('ortszuordnung.form.setOrt.pressed')); mkrambach@1293: map.featureLayer.setVisibility(true); raimund@1274: win.setY(25); raimund@1274: gridPanel.show(); mkrambach@1293: win.doLayout(); mkrambach@1288: osg.addListener('select',oForm.setOrt, oForm); mkrambach@1288: raimund@1274: } raimund@1274: else { mkrambach@1293: map.featureLayer.setVisibility(false); mkrambach@1293: var y = (Ext.getBody().getViewSize().height - 465) / 2; raimund@1274: win.setHeight(465); raimund@1274: win.setY(y); mkrambach@1288: button.setText(i18n.getMsg('ortszuordnung.form.setOrt')); raimund@1274: gridPanel.hide(); mkrambach@1288: osg.removeListener('select',oForm.setOrt, oForm); mkrambach@1293: oForm.doLayout(); raimund@1274: } raimund@1274: }, raimund@1274: dustin@1021: /** mkrambach@1291: * The validitychange function enables or disables the save button which mkrambach@1291: * is present in the toolbar of the form. dustin@1013: */ mkrambach@1288: validityChange: function(form, valid) { mkrambach@1288: if (form.isDirty()) { mkrambach@1289: if (valid) { mkrambach@1289: if (form.getValues().ortId !== '' mkrambach@1289: && /[UEZA]/.test(form.getValues().ortszuordnungTyp)) { mkrambach@1289: // valid ortzuordnung(Probe) mkrambach@1291: form.owner.down('button[action=save]').setDisabled(false); mkrambach@1289: } else if (form.getValues().ort !== '' mkrambach@1289: && /[UEZA]/.test(form.getValues().ortsTyp)) { mkrambach@1289: // valid ortzuordnung(messprogramm) mkrambach@1289: form.owner.down('button[action=save]').setDisabled(false); mkrambach@1289: } else { mkrambach@1289: form.owner.down('button[action=save]').setDisabled(true); mkrambach@1289: } mkrambach@1288: } else { mkrambach@1289: //invalid mkrambach@1288: form.owner.down('button[action=save]').setDisabled(true); raimund@1144: } mkrambach@1288: } else { mkrambach@1289: //not dirty mkrambach@1289: form.owner.down('button[action=save]').setDisabled(true); raimund@603: } raimund@603: } raimund@603: });