]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/conify/global/action/survey.js
layout tweaks
[working/Evergreen.git] / Open-ILS / web / js / ui / default / conify / global / action / survey.js
1 dojo.require('dojox.grid.DataGrid');
2 dojo.require('dojox.grid.cells.dijit');
3 dojo.require('dojo.data.ItemFileWriteStore');
4 dojo.require('dijit.form.TextBox');
5 dojo.require('dijit.form.CurrencyTextBox');
6 dojo.require('dijit.Dialog');
7 dojo.require('openils.DojoPatch');
8 dojo.require('dojox.widget.PlaceholderMenuItem');
9 dojo.require('fieldmapper.OrgUtils');
10 dojo.require('openils.widget.OrgUnitFilteringSelect');
11 dojo.require('openils.PermaCrud');
12
13 var svCache = {};
14 var surveyMap;
15 var svId;
16 var questionId;
17
18
19 /** really need to put this in a shared location... */
20 function getOrgInfo(rowIndex, item) {
21     if(!item) return '';
22     var orgId = svGrid.store.getValue(item, this.field);
23     return fieldmapper.aou.findOrgUnit(orgId).shortname();
24 }
25
26 function getDateTimeField(rowIndex, item) {
27     if(!item) return '';
28     var data = svGrid.store.getValue(item, this.field);
29     var date = dojo.date.stamp.fromISOString(data);
30     return dojo.date.locale.format(date, {formatLength:'short'});
31 }
32
33 function formatBool(inDatum) {
34     switch (inDatum) {
35         case 't':
36             return "<span style='color:green;'>&#x2713;</span>";
37         case 'f':
38             return "<span style='color:red;'>&#x2717;</span>";
39     default:
40         return'';
41     }
42 }
43
44 function endSurvey() {
45     _endSurvey(svGrid.selection.getSelected(), 0);
46 }   
47
48 function _endSurvey(list, idx) {
49     if(idx >= list.length) // we've made it through the list
50         return;
51    
52     var item = list[idx];
53     var svId = svGrid.store.getValue(item, 'id');
54     var pcrud = new openils.PermaCrud();
55     var survey = pcrud.retrieve('asv', svId);
56     var today = new Date();
57     var date = dojo.date.stamp.toISOString(today);
58     survey.end_date(date);
59     survey.ischanged(true);
60     pcrud.update(survey);
61     _endSurvey(list, ++idx);               
62
63 }
64
65 function buildSVGrid() {
66     var store = new dojo.data.ItemFileWriteStore({data:asv.initStoreData('id', {identifier:'id'})});
67     svGrid.setStore(store);
68     svGrid.render();
69     var user = new openils.User();
70     var pcrud = new openils.PermaCrud();
71     var retrieveSurveys = function(orgList) {
72               pcrud.search('asv',
73                      {owner : orgList},
74                      {
75                          async : true,
76                          streaming : true,
77                          onresponse : function(r) {
78                              var survey = openils.Util.readResponse(r);
79                              if(!survey) return'';
80                              svCache[survey.id()] = survey;
81                              store.newItem(survey.toStoreItem());
82                          }
83                      }
84                     );
85     }
86     user.getPermOrgList('ADMIN_SURVEY', retrieveSurveys, true, true);
87
88 }
89
90 function svPage() {
91     var pcrud = new openils.PermaCrud();
92     var survey = pcrud.retrieve('asv', surveyId);
93     dojo.byId("name").innerHTML = survey.name();
94     dojo.byId("description").innerHTML = survey.description();
95     dojo.byId("start_date").innerHTML = survey.start_date();
96     dojo.byId("end_date").innerHTML = survey.end_date();
97     dojo.byId("opac").innerHTML = survey.opac();
98     dojo.byId("poll").innerHTML = survey.poll();
99     dojo.byId("required").innerHTML = survey.required();
100     dojo.byId("usr_summary").innerHTML = survey.usr_summary();
101     dojo.byId("svQuestion").innerHTML = survey.question();
102     dojo.byId("svAnswer").innerHTML = survey.answer();
103     
104 }
105
106 function svNewSurvey() {
107     new openils.User().buildPermOrgSelector('ADMIN_SURVEY', asvOwningOrg);
108     svSurveyDialog.show();
109
110 }
111
112 function svCreate(args) {
113   
114     var sv = new asv();
115     sv.name(args.svName);
116     sv.owner(args.svOwner);
117     sv.description(args.svDescription);
118     sv.start_date(args.svStart_date);
119     sv.end_date(args.svEnd_date);
120     if(args.svPoll == 'on')
121         sv.poll('t')
122         else
123             sv.poll('f');
124
125     if(args.svPoll == 'on')
126         sv.poll('t')
127         else
128             sv.poll('f');
129
130     if(args.svOpac == 'on')
131         sv.opac('t')
132         else
133             sv.opac('f');
134
135     if(args.svRequired == 'on')
136         sv.required('t')
137         else
138             sv.required('f');
139
140     if(args.svUsr_summary == 'on')
141         sv.usr_summary('t')
142         else
143             sv.usr_summary('f');
144    
145     var pcrud = new openils.PermaCrud();
146     pcrud.create(sv,
147                  {           
148                      oncomplete: function(r) {
149                          var obj = openils.Util.readResponse(r);
150                          if(!obj) return '';
151                          svGrid.store.newItem(asv.toStoreItem(obj));
152                          svSurveyDialog.hide();
153                          svId = obj.id();
154                          document.location.href = "/eg/conify/global/action/survey/edit/"+svId;
155                      }
156                  }
157                  );
158 }
159
160 function redirect(svId) {
161
162 }
163     
164
165 function deleteFromGrid() {
166     _deleteFromGrid(svGrid.selection.getSelected(), 0);
167 }   
168
169 function _deleteFromGrid(list, idx) {
170     if(idx >= list.length) // we've made it through the list
171         return;
172
173     var item = list[idx];
174     var code = svGrid.store.getValue(item, 'id');
175   
176     fieldmapper.standardRequest(
177        ['open-ils.circ', 'open-ils.circ.survey.delete.cascade'],
178        {   async: true,
179                streaming: true,
180                params: [openils.User.authtoken, code],
181                onresponse: function(r) {
182                if(stat = openils.Util.readResponse(r)) {
183                    console.log(stat);
184                    svGrid.store.deleteItem(item); 
185                }
186                _deleteFromGrid(list, ++idx);               
187                
188            }
189        }
190     );
191 }
192 openils.Util.addOnLoad(buildSVGrid);
193
194