]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/reports/oils_rpt.js
67d6654338c70cd6ee9773bb5455d98377457b5b
[Evergreen.git] / Open-ILS / web / reports / oils_rpt.js
1 var perms = [ 'RUN_REPORTS', 'SHARE_REPORT_FOLDER', 'VIEW_REPORT_OUTPUT' ];
2
3 function oilsInitReports() {
4         oilsRptIdObjects();
5
6         /* tell FF to capture mouse movements */
7         document.captureEvents(Event.MOUSEMOVE);
8         document.onmousemove = setMousePos;
9
10         DEBUGSLIM = true;
11
12         var cgi = new CGI();
13         fetchUser(cgi.param('ses'));
14         DOM.oils_rpt_user.appendChild(text(USER.usrname()));
15
16         if( cgi.param('dbg') ) oilsRptDebugEnabled = true;
17
18         fetchHighestPermOrgs(SESSION, USER.id(), perms);
19         if( PERMS.RUN_REPORTS == -1 && PERMS.VIEW_REPORT_OUTPUT == -1 ) {
20                 unHideMe(DOM.oils_rpt_permission_denied);
21                 hideMe(DOM.oils_rpt_tree_loading);
22                 return false;
23         }
24
25
26     dojo.require('dojo.cookie');
27         oilsRptCurrentOrg = USER.ws_ou();
28         dojo.cookie(COOKIE_SES, SESSION, { 'path' : '/', 'secure' : true});
29         dojo.cookie('ws_ou', USER.ws_ou(), { 'path' : '/', 'secure' : true});
30
31         oilsRptFetchOrgTree(
32                 function() {
33                         oilsLoadRptTree(
34                                 function() {
35                                         hideMe(DOM.oils_rpt_tree_loading); 
36                                         unHideMe(DOM.oils_rpt_folder_table);
37                                 }
38                         )
39                 }
40         );
41         return true;
42 }
43
44 function oilsRtpInitFolders() {
45         oilsRptCurrentFolderManager = 
46                 new oilsRptFolderManager(DOM.oils_rpt_folder_tree_div);
47         oilsRptCurrentFolderManager.draw(SESSION);
48 }
49
50 function oilsCleanupReports() {
51         try {oilsRptDebugWindow.close();} catch(e) {}
52         DOM = null;
53         oilsRptObjectCache = null;
54         oilsRptObject.objectCache =  null;
55 }
56
57
58
59
60 /* ---------------------------------------------------------------------
61         Define the report object
62         --------------------------------------------------------------------- */
63 function oilsReport(templateObj, reportObj) {
64         this.def = {
65                 select  : [],
66                 from            : {},
67                 where           : [],
68                 having  : [],
69                 order_by : []
70         };
71
72         this.params     = {};
73         this.name       = "";
74         this.reportObject = reportObj;
75
76         if(templateObj) this.setTemplate(templateObj);
77
78         if( reportObj ) 
79                 this.params = JSON2js(reportObj.data());
80         if(!this.params) this.params = {};
81 }
82
83
84 oilsReport.prototype.setTemplate = function(template) {
85         this.def                = JSON2js(template.data());
86         this.name       = template.name();
87         this.templateObject = template;
88 }
89
90 oilsReport.prototype.toString = function() {
91         return formatJSON(js2JSON(this));
92 }
93
94 oilsReport.prototype.toHTMLString = function() {
95         return formatJSONHTML(js2JSON(this));
96 }
97
98 oilsReport.prototype.gatherParams = function() {
99         //if(oilsRptObjectKeys(this.params).length == 0) return;
100         _debug("we have params: " + js2JSON(this.params));
101
102         var params      = [];
103         this._gatherParams(params, this.def.where, 'where', 'condition');
104         this._gatherParams(params, this.def.having, 'having', 'condition');
105         return params;
106 }
107
108 oilsReport.prototype._gatherParams = function(params, arr, type, field) {
109         if(!arr) return;
110         for( var i = 0; i < arr.length; i++ ) {
111
112                 var obj = arr[i];
113                 node = obj[field];
114                 var key; 
115                 var op;
116
117                 /* add select transform support */
118
119                 if( typeof node == 'string' ) {
120                         key = node.match(/::.*/);
121                 } else {
122                         op = oilsRptObjectKeys(node)[0];
123                         key = (node[op] +'').match(/::.*/);
124                 }
125
126                 if(!key) continue;
127                 key = key[0].replace(/::/,'');
128                 _debug("key = "+key+", param = " + this.params[key]);
129
130                 params.push( { 
131                         key             : key,
132                         op                      : op,
133                         value           : this.params[key],
134                         column  : obj.column,
135                         path            : obj.path,
136                         type            : type, 
137                         relation : obj.relation,
138                         field           : field
139                 });
140         }
141 }
142
143
144
145 oilsReport.prototype.gatherTemplateParams = function() {
146     var arr = this.__gatherTemplateParams(this.def.where, 'where');
147     arr.concat(this.__gatherTemplateParams(this.def.having, 'having'));
148     _debug("template params: " + js2JSON(arr));
149     return arr;
150 }
151
152 oilsReport.prototype.__gatherTemplateParams = function(arr, type) {
153
154     if(!arr) return [];
155     var params = [];
156
157         for( var i = 0; i < arr.length; i++ ) {
158
159                 var obj = arr[i];
160                 var node = obj.condition;
161             var op = oilsRptObjectKeys(node)[0];
162                 var key = node[op];
163
164         /** if this is a dynamic param, skip it */
165         if( key && (key+'').match(/::.*/) ) continue;
166
167                 _debug("template params: op = " + op + ", value = " + js2JSON(key));
168
169                 params.push( { 
170                         key             : key, /* key == value, since this is not a dynamic param */
171                         op              : op,
172             value   : key,
173                         column  : obj.column,
174                         path    : obj.path,
175                         type    : type, 
176                         relation : obj.relation,
177                         field   : field
178                 });
179         }
180
181     return params;
182 }
183
184