1 /** initializes reports, some basid display settings,
2 * grabs and builds the IDL tree
4 function oilsInitReportBuilder() {
5 if(!oilsInitReports()) return false;
6 oilsReportBuilderReset();
7 DOM.oils_rpt_table.onclick =
8 function(){hideMe(DOM.oils_rpt_column_editor)};
11 hideMe(DOM.oils_rpt_tree_loading);
12 unHideMe(DOM.oils_rpt_table);
16 DOM.oils_rpt_builder_save_template.onclick = oilsReportBuilderSave;
19 function oilsReportBuilderReset() {
20 var n = (oilsRpt) ? oilsRpt.name : "";
21 oilsRpt = new oilsReport();
23 oilsRptDisplaySelector = DOM.oils_rpt_display_selector;
24 oilsRptFilterSelector = DOM.oils_rpt_filter_selector;
25 oilsRptHavingSelector = DOM.oils_rpt_agg_filter_selector;
26 removeChildren(oilsRptDisplaySelector);
27 removeChildren(oilsRptFilterSelector);
28 removeChildren(oilsRptHavingSelector);
29 //removeChildren(oilsRptOrderBySelector);
33 function oilsReportBuilderSave() {
36 tmpl.name(DOM.oils_rpt_builder_new_name.value);
37 tmpl.description(DOM.oils_rpt_builder_new_desc.value);
38 tmpl.owner(USER.id());
39 tmpl.folder(new CGI().param('folder'));
40 tmpl.data(js2JSON(oilsRpt.def));
42 if(!confirm('Name : '+tmpl.name() + '\nDescription: ' + tmpl.description()+'\nSave Template?'))
48 var req = new Request(OILS_RPT_CREATE_TEMPLATE, SESSION, tmpl);
51 var res = r.getResultObject();
52 if( res && res != '0' ) {
53 oilsRptAlertSuccess();
64 /* adds an item to the display window */
65 function oilsAddRptDisplayItem(path, name, tform, params) {
66 if( ! oilsAddSelectorItem(oilsRptDisplaySelector, path, name) )
69 /* add the selected columns to the report output */
70 name = (name) ? name : oilsRptPathCol(path);
71 if( !tform ) tform = 'Bare';
73 var aggregate = oilsRptGetIsAgg(tform);
75 /* add this item to the select blob */
77 relation: hex_md5(oilsRptPathRel(path)),
80 column: { transform: tform, colname: oilsRptPathCol(path) }
83 if( params ) sel.column.params = params;
85 if(!oilsRptGetIsAgg(tform)) {
88 for( var i = 0; i < oilsRpt.def.select.length; i++ ) {
89 var item = oilsRpt.def.select[i];
90 if( !added && oilsRptGetIsAgg( item.column.transform ) ) {
96 if(!added) select.push(sel);
97 oilsRpt.def.select = select;
99 oilsRpt.def.select.push(sel);
103 mergeObjects( oilsRpt.def.from, oilsRptBuildFromClause(path));
107 function oilsRptGetIsAgg(tform) {
108 return OILS_RPT_TRANSFORMS[tform].aggregate;
111 var sel = $n(DOM.oils_rpt_tform_table,'selector');
112 for( var i = 0; i < sel.options.length; i++ ) {
113 var opt = sel.options[i];
114 if( opt.getAttribute('value') == tform )
115 return opt.getAttribute('aggregate');
119 /* takes a column path and builds a from-clause object for the path */
120 function oilsRptBuildFromClause(path) {
122 /* the path is the full path (relation) from the source
123 object to the column in question (e.g. au-home_ou-aou-name)*/
124 var parts = path.split(/-/);
126 /* the final from clause */
129 /* reference to the current position in the from clause */
134 /* walk the path, fleshing the from clause as we go */
135 for( var i = 0; i < parts.length; i += 2 ) {
137 var cls = parts[i]; /* class name (id) */
138 var col = parts[i+1]; /* column name */
140 /* a "node" is a class description from the IDL, it
141 contains relevant info, plus a list of "fields",
143 var node = oilsIDL[cls];
144 var pkey = oilsRptFindField(node, node.pkey);
146 /* a "field" is a parsed version of a column from the IDL,
147 contains datatype, column name, etc. */
148 var field = oilsRptFindField(node, col);
150 /* re-construct the path as we go so
151 we know what all we've seen thus far */
152 newpath = (newpath) ? newpath + '-'+ cls : cls;
154 /* extract relevant info */
155 tobj.table = node.table;
157 tobj.alias = hex_md5(newpath);
159 _debug('field type is ' + field.type);
160 if( i == (parts.length - 2) ) break;
162 /* we still have columns left in the path, keep adding join's */
164 if(field.reltype != 'has_a')
165 col = pkey.name + '-' + col;
172 if( field.type == 'link' )
173 tobj.key = field.key;
175 newpath = newpath + '-'+ path_col;
178 _debug("built 'from' clause: path="+path+"\n"+formatJSON(js2JSON(obj)));
183 /* removes a specific item from the display window */
184 function oilsDelDisplayItem(val) {
185 oilsDelSelectorItem(oilsRptDisplaySelector, val);
188 /* removes selected items from the display window */
189 function oilsDelSelectedDisplayItems() {
190 var list = oilsDelSelectedItems(oilsRptDisplaySelector);
192 _debug('deleting list: ' + list);
194 /* remove the de-selected columns from the report output */
195 oilsRpt.def.select = grep( oilsRpt.def.select,
197 for( var j = 0; j < list.length; j++ ) {
198 var d = list[j]; /* path */
201 _debug('in delete, looking at list = '+d+' : col = ' +
202 col.colname + ' : relation = ' + i.relation + ' : encoded = ' + hex_md5(oilsRptPathRel(d)) );
204 if( hex_md5(oilsRptPathRel(d)) == i.relation && oilsRptPathCol(d) == col.colname ) {
212 if(!oilsRpt.def.select) oilsRpt.def.select = [];
214 oilsRptPruneFromList(list);
217 for( var j = 0; j < list.length; j++ ) {
218 debug('seeing if we can prune from clause with relation = ' + hex_md5(oilsRptPathRel(list[j])));
219 if( !grep(oilsRpt.def.select,
220 function(i){ return (i.relation == hex_md5(oilsRptPathRel(list[j]))); })
221 && !grep(oilsRpt.def.where,
222 function(i){ return (i.relation == hex_md5(oilsRptPathRel(list[j]))); })
223 && !grep(oilsRpt.def.having,
224 function(i){ return (i.relation == hex_md5(oilsRptPathRel(list[j]))); })
226 _debug('pruning from clause');
227 oilsRptPruneFromClause(oilsRptPathRel(list[j]));
235 function oilsRptPruneFromList(pathlist) {
237 for( var j = 0; j < pathlist.length; j++ ) {
238 /* if there are no items left in the "select", "where", or "having" clauses
239 for the given relation, trim this relation from the "from" clause */
240 var path = pathlist[j];
241 var encrel = hex_md5(oilsRptPathRel(path));
243 debug('seeing if we can prune from clause with relation = ' + encrel +' : path = ' + path);
245 var func = function(i){ return (i.relation == hex_md5(oilsRptPathRel(path))); };
247 if( !grep(oilsRpt.def.select, func) &&
248 !grep(oilsRpt.def.where, func) &&
249 !grep(oilsRpt.def.having, func) ) {
251 oilsRptPruneFromClause(oilsRptPathRel(pathlist[j]));
257 /* for each item in the path list, remove the associated data
258 from the "from" clause */
260 function oilsRptPruneFromClause(relation, node) {
261 _debug("removing relation from 'from' clause: " + relation);
262 if(!node) node = oilsRpt.def.from.join;
263 if(!node) return false;
265 for( var i in node ) {
266 _debug("from prune looking at node "+node[i].path);
267 // first, descend into the tree, and prune leaves first
269 oilsRptPruneFromClause(relation, node[i].join);
270 if(oilsRptObjectKeys(node[i].join).length == 0)
277 var key = oilsRptObjectKeys(node)[0];
278 var from_alias = node[key].alias;
279 var func = function(n){ return (n.relation == from_alias)};
281 _debug("pruning from clause with alias "+ from_alias);
283 if( !grep(oilsRpt.def.select, func) &&
284 !grep(oilsRpt.def.where, func) &&
285 !grep(oilsRpt.def.having, func) ) {
287 // if we're at an unused empty leaf, remove it
296 function oilsRptMkFilterTags(path, tform, filter) {
297 var name = oilsRptMakeLabel(path);
298 if(tform) name += ' ('+tform+')';
299 name += ' "' + filter + '"';
300 var epath = path + ':'+filter+':';
301 if(tform) epath += tform;
303 return [ name, epath ];
307 function oilsAddRptFilterItem(path, tform, filter) {
308 _debug("Adding filter item for "+path+" tform="+tform+" filter="+filter);
310 var name = oilsRptMkFilterTags(path, tform, filter);
314 if( ! oilsAddSelectorItem(oilsRptFilterSelector, epath, name) )
318 relation: hex_md5(oilsRptPathRel(path)),
320 column: { transform: tform, colname: oilsRptPathCol(path) },
323 if( filter == 'is' || filter == 'is not' )
324 where.condition[filter] = null;
325 else where.condition[filter] = oilsRptNextParam();
328 case 'substring' : where.column.params = oilsRptNextParam();
331 oilsRpt.def.where.push(where);
332 mergeObjects( oilsRpt.def.from, oilsRptBuildFromClause(path));
337 function oilsAddRptHavingItem(path, tform, filter) {
338 _debug("Adding filter item for "+path+" tform="+tform+" filter="+filter);
340 var name = oilsRptMkFilterTags(path, tform, filter);
344 if( ! oilsAddSelectorItem(oilsRptHavingSelector, epath, name) )
348 relation: hex_md5(oilsRptPathRel(path)),
350 column: { transform: tform, colname: oilsRptPathCol(path) },
353 if( filter == 'is' || filter == 'is not' )
354 having.condition[filter] = null;
355 else having.condition[filter] = oilsRptNextParam();
358 case 'substring' : having.column.params = oilsRptNextParam();
361 oilsRpt.def.having.push(having);
362 mergeObjects( oilsRpt.def.from, oilsRptBuildFromClause(path));
368 function oilsDelSelectedFilterItems() {
369 _oilsDelSelectedFilterItems('where');
371 function oilsDelSelectedAggFilterItems() {
372 _oilsDelSelectedFilterItems('having');
375 function _oilsDelSelectedFilterItems(type) {
377 /* the values in this list are formed: <path>:<operation>:<transform> */
378 var list = oilsDelSelectedItems(oilsRptFilterSelector);
380 for( var i = 0; i < list.length; i++ ) {
381 var enc_path = list[i];
382 var data = oilsRptParseFilterEncPath(enc_path);
383 oilsRpt.def[type] = grep(
386 return oilsRptFilterDataMatches(
387 f, data.path, data.operation, data.tform );
392 if(!oilsRpt.def[type]) oilsRpt.def[type] = [];
393 oilsRptPruneFromList(list);
397 function oilsRptParseFilterEncPath(item) {
399 path: item.replace(/:.*/,''),
400 operation: item.replace(/.*:(.*):.*/,'$1'),
401 tform: item.replace(/.*?:.*?:(.*)/,'$1')
406 function oilsRptFilterDataMatches(filter, path, operation, tform) {
407 var rel = hex_md5(oilsRptPathRel(path));
408 var col = oilsRptPathCol(path);
410 if( col == filter.column.colname &&
411 rel == filter.relation &&
412 tform == filter.column.transform &&
413 operation == oilsRptObjectKeys(filter)[0] ) return true;
419 function oilsRptFilterGrep(flist, filter) {
421 for( var j = 0; j < flist.length; j++ ) {
424 var col = filter.column;
425 var frel = hex_md5(oilsRptPathRel(fil.path));
426 var fcol = oilsRptPathCol(fil.path);
428 var op = oilsRptObjectKeys(filter.condition)[0];
430 if( frel == filter.relation &&
431 fcol == col.colname &&
432 fil.operation == op &&
433 fil.tform == col.transform ) {
441 /* adds an item to the display window */
442 function oilsAddRptAggFilterItem(val) {
443 oilsAddSelectorItem(oilsRptHavingFilterSelector, val);
446 /* removes a specific item from the display window */
447 function oilsDelAggFilterItem(val) {
448 oilsDelSelectorItem(oilsRptHavingFilterSelector, val);
454 function ___oilsDelSelectedAggFilterItems() {
455 var list = oilsDelSelectedItems(oilsRptHavingFilterSelector);
456 oilsRpt.def.having = grep( oilsRpt.def.having,
458 for( var j = 0; j < list.length; j++ ) {
462 if( typeof col != 'string' )
463 for( var c in col ) col = col[c];
465 if( typeof col != 'string' ) col = col[0];
467 if( oilsRptPathRel(d) == i.relation && oilsRptPathCol(d) == col ) {
469 // var param = (i.alias) ? i.alias.match(/::P\d*/) : null;
471 if( param ) delete oilsRpt.params[param];
479 if(!oilsRpt.def.having) oilsRpt.def.having = [];
480 oilsRptPruneFromList(list);
486 /* adds an item to the display window */
487 function oilsAddSelectorItem(sel, val, name) {
488 name = (name) ? name : oilsRptMakeLabel(val);
489 for( var i = 0; i < sel.options.length; i++ ) {
490 var opt = sel.options[i];
491 if( opt.value == val ) return false;
493 insertSelectorVal( sel, -1, name, val );
498 /* removes a specific item from the display window */
499 function oilsDelSelectorItem(sel, val) {
500 var opts = sel.options;
501 for( var i = 0; i < opts.length; i++ ) {
503 if( opt.value == val ) {
504 if( i == opts.length - 1 )
506 else opts[i] = opts[i+1];
512 /* removes selected items from the display window */
513 function oilsDelSelectedItems(sel) {
514 var list = getSelectedList(sel);
515 for( var i = 0; i < list.length; i++ )
516 oilsDelSelectorItem(sel, list[i]);
521 /* hides the different field editor tabs */
522 function oilsRptHideEditorDivs() {
523 hideMe(DOM.oils_rpt_tform_div);
524 hideMe(DOM.oils_rpt_filter_div);
525 hideMe(DOM.oils_rpt_agg_filter_div);
526 hideMe(DOM.oils_rpt_order_by_div);
531 This draws the 3-tabbed window containing the transform,
532 filter, and aggregate filter picker window
534 function oilsRptDrawDataWindow(path) {
535 var col = oilsRptPathCol(path);
536 var cls = oilsRptPathClass(path);
537 var field = oilsRptFindField(oilsIDL[cls], col);
539 appendClear(DOM.oils_rpt_editor_window_label, text(oilsRptMakeLabel(path)));
540 appendClear(DOM.oils_rpt_editor_window_datatype, text(field.datatype));
542 _debug("setting update data window for column "+col+' on class '+cls);
544 var div = DOM.oils_rpt_column_editor;
545 /* set a preliminary top position so the page won't bounce around */
546 div.setAttribute('style','top:'+oilsMouseX+'px');
548 /* unhide the div so we can determine the dimensions */
551 /* don't let them see the floating div until the position is fully determined */
552 div.style.visibility='hidden';
554 oilsRptDrawTransformWindow(path, col, cls, field);
555 oilsRptDrawFilterWindow(path, col, cls, field);
556 oilsRptDrawHavingWindow(path, col, cls, field);
557 oilsRptDrawOrderByWindow(path, col, cls, field);
559 buildFloatingDiv(div, 600);
561 /* now let them see it */
562 div.style.visibility='visible';
563 oilsRptSetDataWindowActions(div);
567 function oilsRptSetDataWindowActions(div) {
568 /* give the tab links behavior */
569 DOM.oils_rpt_tform_tab.onclick =
570 function(){oilsRptHideEditorDivs();unHideMe(DOM.oils_rpt_tform_div)};
571 DOM.oils_rpt_filter_tab.onclick =
572 function(){oilsRptHideEditorDivs();unHideMe(DOM.oils_rpt_filter_div)};
573 DOM.oils_rpt_agg_filter_tab.onclick =
574 function(){oilsRptHideEditorDivs();unHideMe(DOM.oils_rpt_agg_filter_div)};
576 DOM.oils_rpt_order_by_tab.onclick =
578 oilsRptHideEditorDivs();
579 oilsRptDrawOrderByWindow();
580 unHideMe(DOM.oils_rpt_order_by_div);
583 DOM.oils_rpt_tform_tab.onclick();
584 DOM.oils_rpt_column_editor_close_button.onclick = function(){hideMe(div);};
588 function oilsRptDrawFilterWindow(path, col, cls, field) {
590 var tformPicker = new oilsRptTformPicker( {
591 node : DOM.oils_rpt_filter_tform_table,
592 datatype : field.datatype,
597 var filterPicker = new oilsRptFilterPicker({
598 node : DOM.oils_rpt_filter_op_table,
599 datatype : field.datatype
603 DOM.oils_rpt_filter_submit.onclick = function() {
604 oilsAddRptFilterItem(
605 path, tformPicker.getSelected(), filterPicker.getSelected());
610 function oilsRptDrawHavingWindow(path, col, cls, field) {
611 var tformPicker = new oilsRptTformPicker( {
612 node : DOM.oils_rpt_agg_filter_tform_table,
613 datatype : field.datatype,
618 var filterPicker = new oilsRptFilterPicker({
619 node : DOM.oils_rpt_agg_filter_op_table,
620 datatype : field.datatype
624 DOM.oils_rpt_agg_filter_submit.onclick = function() {
625 oilsAddRptHavingItem(
626 path, tformPicker.getSelected(), filterPicker.getSelected());
630 /* draws the transform window */
631 function oilsRptDrawTransformWindow(path, col, cls, field) {
632 DOM.oils_rpt_tform_label_input.value = oilsRptMakeLabel(path);
633 var dtype = field.datatype;
635 var tformPicker = new oilsRptTformPicker( {
636 node : DOM.oils_rpt_tform_table,
637 datatype : field.datatype,
638 non_aggregate : true,
643 DOM.oils_rpt_tform_submit.onclick =
645 oilsAddRptDisplayItem(path,
646 DOM.oils_rpt_tform_label_input.value, tformPicker.getSelected() );
649 DOM.oils_rpt_tform_label_input.focus();
650 DOM.oils_rpt_tform_label_input.select();
652 _debug("Building transform window for datatype "+dtype);
656 //function oilsRptDrawOrderByWindow(path, col, cls, field) {
657 function oilsRptDrawOrderByWindow() {
658 var sel = DOM.oils_rpt_order_by_selector;
660 DOM.oils_rpt_order_by_submit.onclick = function() {
661 oilsRptAddOrderBy(getSelectorVal(sel));
664 var cols = oilsRpt.def.select;
665 for( var i = 0; i < cols.length; i++ ) {
667 insertSelectorVal(sel, -1, obj.alias, obj.path);
671 function oilsRptAddOrderBy(path) {
672 var rel = hex_md5(oilsRptPathRel(path));
673 var order_by = oilsRpt.def.order_by;
675 /* if this item is already in the order by remove it and overwrite it */
676 order_by = grep(oilsRpt.def.order_by,
677 function(i) {return (i.path != path)});
679 if(!order_by) order_by = [];
681 /* find the column definition in the select blob */
682 var obj = grep(oilsRpt.def.select,
683 function(i) {return (i.path == path)});
689 relation : obj.relation,
691 direction : getSelectorVal(DOM.oils_rpt_order_by_dir)
694 oilsRpt.def.order_by = order_by;