]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/conify/global/config/marc_code_maps.js
use fieldmapper.AutoIDL inside conify to allow openils.PermaCrud to function properly
[Evergreen.git] / Open-ILS / web / conify / global / config / marc_code_maps.js
1 /*
2 # ---------------------------------------------------------------------------
3 # Copyright (C) 2008  Georgia Public Library Service / Equinox Software, Inc
4 # Mike Rylander <miker@esilibrary.com>
5
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 # ---------------------------------------------------------------------------
16 */
17
18 dojo.require('fieldmapper.AutoIDL');
19 dojo.require('fieldmapper.dojoData');
20 dojo.require('openils.widget.TranslatorPopup');
21 dojo.require('dojo.cookie');
22 dojo.require('dojo.parser');
23 dojo.require('dojo.string');
24 dojo.require('dojo.data.ItemFileWriteStore');
25 dojo.require('dijit.form.Form');
26 dojo.require('dijit.form.TextBox');
27 dojo.require('dijit.form.ValidationTextBox');
28 dojo.require('dijit.form.Textarea');
29 dojo.require('dijit.layout.TabContainer');
30 dojo.require('dijit.layout.ContentPane');
31 dojo.require('dijit.layout.LayoutContainer');
32 dojo.require('dijit.layout.BorderContainer');
33 dojo.require('dojox.widget.Toaster');
34 dojo.require('dojox.fx');
35 dojo.require('dojox.grid.Grid');
36 dojo.requireLocalization("openils.conify", "conify");
37
38 console.log('loading marc_code_maps.js');
39
40 // some handy globals
41 var cgi = new CGI();
42 var ses = dojo.cookie('ses') || cgi.param('ses');
43 var pCRUD = new OpenSRF.ClientSession('open-ils.permacrud');
44
45 console.log('initialized pcrud session');
46
47 var stores = {};
48 var current_item = {};
49
50 var cam_strings = dojo.i18n.getLocalization('openils.conify', 'conify');
51
52 /*
53 var highlighter = {
54         green : dojox.fx.highlight( { color : '#B4FFB4', node : 'grid_container', duration : 500 } ),
55         red : dojox.fx.highlight( { color : '#FF2018', node : 'grid_container', duration : 500 } )
56 };
57
58 console.log('highlighters set up');
59 */
60
61 var dirtyStore = [];
62
63 function status_update (markup) {
64         if (parent !== window && parent.status_update) parent.status_update( markup );
65 }
66
67 console.log('local status function built');
68
69 function save_code (classname) {
70
71         var item = current_item[classname];
72         var obj = new fieldmapper[classname]().fromStoreItem( item );
73
74         obj.ischanged( 1 );
75         obj.code( dojo.string.trim( obj.code() ) );
76         obj.value( dojo.string.trim( obj.value() ) );
77         if(classname == 'cam' || classname == 'clfm')
78                 obj.description( dojo.string.trim( obj.description() ) );
79
80         pCRUD.request({
81                 method : 'open-ils.permacrud.update.' + classname,
82                 timeout : 10,
83                 params : [ ses, modified_ppl ],
84                 onerror : function (r) {
85                         //highlighter.red.play();
86                         status_update( dojo.string.substitute(cam_strings.ERROR_SAVING_DATA_CAM, [classname, obj.code()]) );
87                 },
88                 oncomplete : function (r) {
89                         var res = r.recv();
90                         if ( res && res.content() ) {
91                                 stores[classname].setValue( current_item, 'ischanged', 0 );
92                                 //highlighter.green.play();
93                                 status_update( dojo.string.substitute(cam_strings.SUCCESS_SAVE, stores[classname].getValue( item, 'code' )) );
94                         } else {
95                                 //highlighter.red.play();
96                                 status_update( dojo.string.substitute( cam_strings.ERROR_SAVING_DATA_CAM, [classname, stores[classname].getValue( item, 'code' )] ) );
97                         }
98                 },
99         }).send();
100 }
101
102 function save_them_all (event) {
103
104         for (var classname in stores) {
105
106                 var store = stores[classname];
107                 store.fetch({
108                         query : { ischanged : 1 },
109                         onItem : function (item, req) { try { if (this.isItem( item )) window.dirtyStore.push( item ); } catch (e) { /* meh */ } },
110                         scope : store
111                 });
112
113                 var confirmation = true;
114
115                 if (event && dirtyStore.length > 0) {
116                         confirmation = confirm( cam_strings.CONFIRM_EXIT_CAM );
117                         event = null;
118                 }
119
120                 if (confirmation) {
121                         for (var i in dirtyStore) {
122                                 current_item[classname] = dirtyStore[i];
123                                 save_object(classname);
124                         }
125
126                         dirtyStore = [];
127                 }
128         }
129 }
130
131 dojo.addOnUnload( save_them_all );
132
133 function delete_grid_selection(classname, grid ) {
134
135     var selected_rows = grid.selection.getSelected();
136         
137     var selected_items = [];
138     for (var i in selected_rows) {
139         selected_items.push(
140             grid.model.getRow( selected_rows[i] ).__dojo_data_item
141         );
142     }
143
144     grid.selection.clear();
145
146     for (var i in selected_items) {
147         var item = selected_items[i];
148
149         if ( confirm( dojo.string.substitute( cam_strings.CONFIRM_DELETE, [grid.model.store.getValue( item, 'value' )] ) ) ) {
150
151             grid.model.store.setValue( item, 'isdeleted', 1 );
152             
153             var obj = new fieldmapper[classname]().fromStoreItem( item );
154             obj.isdeleted( 1 );
155             
156             pCRUD.request({
157                 method : 'open-ils.permacrud.delete.' + classname,
158                 timeout : 10,
159                 params : [ ses, obj ],
160                 onerror : function (r) {
161                     //highlighter.red.play();
162                     status_update( dojo.string.substitute( cam_strings.ERROR_DELETING, [grid.model.store.getValue( item, 'value' )] ) );
163                 },
164                 oncomplete : function (r) {
165                     var res = r.recv();
166                     var old_name = grid.model.store.getValue( item, 'value' );
167                     if ( res && res.content() ) {
168
169                         grid.model.store.fetch({
170                             query : { code : grid.model.store.getValue( item, 'code' ) },
171                             onItem : function (item, req) { try { if (this.isItem( item )) this.deleteItem( item ); } catch (e) { /* meh */ } },
172                             scope : grid.model.store
173                         });
174             
175                         //highlighter.green.play();
176                         status_update( dojo.string.substitute( cam_strings.STATUS_DELETED, [old_name] ) );
177                     } else {
178                         //highlighter.red.play();
179                         status_update( dojo.string.substitute( cam_strings.ERROR_DELETING, [old_name] ) );
180                     }
181                 }
182             }).send();
183         
184         }
185     }
186 }
187
188 function create_marc_code (data) {
189
190         var cl = data.classname;
191         if (!cl) return false;
192
193         data.code = dojo.string.trim( data.code );
194         data.value = dojo.string.trim( data.value );
195
196         if(!data.code || !data.value) return false;
197
198         if(cl == 'cam' || cl == 'clfm')
199                 data.description = dojo.string.trim( data.description );
200
201     var new_fm_obj = new fieldmapper[cl]().fromHash( data )
202     new_fm_obj.isnew(1);
203
204     var err = false;
205     pCRUD.request({
206         method : 'open-ils.permacrud.create.' + cl,
207         timeout : 10,
208         params : [ ses, new_fm_obj ],
209         onerror : function (r) {
210             //highlighter.red.play();
211             status_update( dojo.string.substitute( cam_strings.ERROR_CALLING_METHOD_CAM, [cl] ) );
212             err = true;
213         },
214         oncomplete : function (r) {
215             var res = r.recv();
216             if ( res && res.content() ) {
217                 var new_item_hash = res.content().toHash();
218                 stores[cl].newItem( new_item_hash );
219                 status_update( dojo.string.substitute( cam_strings.SUCCESS_CREATING_CODE, [new_item_hash.code, cl] ) );
220                 //highlighter.green.play();
221             } else {
222                 //highlighter.red.play();
223                 status_update( cam_strings.ERROR_CREATING_PERMISSION );
224                 err = true;
225             }
226         }
227     }).send();
228
229         return false;
230 }
231