raimund@594: /* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz raimund@594: * Software engineering by Intevation GmbH raimund@594: * raimund@594: * This file is Free Software under the GNU GPL (v>=3) raimund@594: * and comes with ABSOLUTELY NO WARRANTY! Check out raimund@594: * the documentation coming with IMIS-Labordaten-Application for details. raimund@594: */ raimund@594: dustin@742: /** dustin@742: * This is a controller for a grid of Status dustin@742: */ raimund@594: Ext.define('Lada.controller.grid.Status', { raimund@594: extend: 'Ext.app.Controller', raimund@594: dustin@742: /** dustin@742: * Initialize the Controller with dustin@742: * 3 Listeners dustin@742: */ dustin@742: init: function() { raimund@594: this.control({ raimund@594: 'statusgrid': { raimund@637: edit: this.gridSave, dustin@945: canceledit: this.cancelEdit, raimund@594: }, raimund@594: 'statusgrid button[action=add]': { raimund@594: click: this.add dustin@994: }, dustin@994: 'statusgrid button[action=reset]': { dustin@994: click: this.reset raimund@594: } raimund@594: }); raimund@594: }, raimund@594: dustin@742: /** dustin@742: * This function is called when the grids roweditor saves dustin@742: * the record. dustin@742: * On success it refreshes the windows which contains the grid dustin@974: * it also tries to refresh the ProbeWindow and the messunggrid dustin@742: * On failure it displays a message dustin@742: */ dustin@742: gridSave: function(editor, context) { mstanko@944: context.record.set('sdatum', new Date()); raimund@594: context.record.save({ raimund@594: success: function() { raimund@594: context.grid.initData(); dustin@974: var win = context.grid.up('window'); dustin@974: win.initData(); dustin@974: try { dustin@974: win.parentWindow.initData(); dustin@974: win.parentWindow.down('messunggrid').store.reload(); dustin@974: } dustin@974: catch(e) { dustin@974: } raimund@594: }, dustin@701: failure: function(request, response) { dustin@701: var json = response.request.scope.reader.jsonData; dustin@701: if (json) { dustin@701: if (json.message){ dustin@701: Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.save.title') dustin@701: +' #'+json.message, dustin@701: 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@701: } 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')); dustin@701: } raimund@594: } raimund@594: }); raimund@594: }, raimund@594: dustin@742: /** dustin@742: * When the edit was canceled, dustin@742: * the empty row might have been created by the roweditor is removed dustin@742: */ dustin@742: cancelEdit: function(editor, context) { raimund@637: if (!context.record.get('id') || raimund@637: context.record.get('id') === '') { raimund@637: editor.getCmp().store.remove(context.record); raimund@637: } raimund@637: }, raimund@637: dustin@742: /** dustin@742: * This function adds a new row to add a Status dustin@990: * and copies the data of the previous status into the new one dustin@990: * if possible. dustin@742: */ dustin@742: add: function(button) { dustin@945: var lastrow = button.up('statusgrid').store.count() raimund@594: dustin@992: // retrive current status from the messung. dustin@992: var s = button.up('window').down('messungform').getRecord().get('status'); dustin@992: var recentStatus = button.up('statusgrid').store.getById(s); dustin@945: dustin@990: //If possible copy the previous record into the new one. dustin@990: //this assumes the store is ordered correctly, most recent status last. dustin@992: // Do not copy, if current userid differs from the id of the current status dustin@992: if (lastrow > 0 && dustin@992: Ext.Array.contains(Lada.mst, recentStatus.get('erzeuger'))) { dustin@992: dustin@992: if (recentStatus) { dustin@992: // clone the status dustin@992: var record = recentStatus.copy() dustin@992: } dustin@992: dustin@990: record.set('id', null); dustin@990: } else { dustin@990: //create a new one dustin@990: var record = Ext.create('Lada.model.Status', { dustin@990: messungsId: button.up('statusgrid').recordId dustin@990: }); dustin@990: } dustin@945: dustin@990: //Set the Date dustin@990: record.set('datum', new Date()); dustin@945: dustin@990: button.up('statusgrid').store.insert(lastrow, record); dustin@990: button.up('statusgrid').getPlugin('rowedit').startEdit(lastrow, 1); dustin@994: }, dustin@994: dustin@994: /** dustin@994: * Reset dustin@994: * This Function instructs the server to reset the current status dustin@994: * WIP / TODO dustin@994: * dustin@994: **/ dustin@994: reset: function(button) { dustin@994: var s = button.up('window').down('messungform').getRecord().get('status'); dustin@994: var messId = button.up('window').down('messungform').getRecord().get('id'); dustin@994: var recentStatus = button.up('statusgrid').store.getById(s); dustin@994: dustin@994: //Set Status to 'Resetted' (8) dustin@996: if (recentStatus) { dustin@996: var record = recentStatus.copy(); dustin@996: record.set('datum', new Date()); dustin@996: record.set('statusWert', 8); dustin@996: record.set('id', null); dustin@996: record.set('text', null); dustin@996: } dustin@994: dustin@994: Ext.Ajax.request({ dustin@994: url: 'lada-server/status', dustin@994: jsonData: record.getData(), dustin@994: method: 'POST', dustin@994: success: function(response) { dustin@994: button.up('window').initData(); dustin@996: button.up('grid').initData(); dustin@994: }, dustin@994: failure: function(response) { dustin@994: // TODO sophisticated error handling, with understandable Texts dustin@994: var json = Ext.JSON.decode(response.responseText); dustin@994: if (json) { dustin@994: if(json.errors.totalCount > 0 || json.warnings.totalCount > 0){ dustin@994: formPanel.setMessages(json.errors, json.warnings); dustin@994: } dustin@994: if(json.message){ dustin@994: Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.generic.title') dustin@994: +' #'+json.message, dustin@994: Lada.getApplication().bundle.getMsg(json.message)); dustin@994: } else { dustin@994: Ext.Msg.alert(i18n.getMsg('err.msg.generic.title'), dustin@994: i18n.getMsg('err.msg.generic.body')); dustin@994: } dustin@994: } else { dustin@994: Ext.Msg.alert(i18n.getMsg('err.msg.generic.title'), dustin@994: i18n.getMsg('err.msg.generic.body')); dustin@994: } dustin@994: } dustin@994: }); dustin@994: dustin@994: } raimund@594: });