1 function removeReportAtom (args) {
4 var active_tab = filterByAttribute(
5 $('used-source-fields-tabbox').getElementsByTagName('tab'),
9 var tabname = active_tab.getAttribute('id');
11 var tabpanel = $( tabname + 'panel' );
12 var tree = tabpanel.getElementsByTagName('tree')[0];
13 var fields = getSelectedItems(tree);
16 for (var i in fields) {
17 var field = fields[i];
18 var colname = field.firstChild.firstChild.nextSibling.getAttribute('label');
19 var relation_alias = field.getAttribute('relation');
21 delete rpt_rel_cache[relation_alias].fields[tabname][colname];
22 if (tabname == 'dis_tab') {
24 for each (var _o_col in rpt_rel_cache.order_by) {
25 if (_o_col.relation == relation_alias && _o_col.field == colname) continue;
26 _o_tmp.push( _o_col );
28 rpt_rel_cache.order_by = _o_tmp
31 with (rpt_rel_cache[relation_alias].fields) {
32 if ( getKeys(dis_tab).length == 0 && getKeys(filter_tab).length == 0 && getKeys(aggfilter_tab).length == 0 )
33 delete rpt_rel_cache[relation_alias];
40 function addReportAtoms () {
41 var nope = $( 'source-add' ).getAttribute('disabled');
42 if (nope == 'true') return false;
44 var class_tree = $('class-view');
45 var transform_tree = $('trans-view');
47 var active_tab = filterByAttribute(
48 $('used-source-fields-tabbox').getElementsByTagName('tab'),
53 var tabname = active_tab.getAttribute('id')
55 var items = getSelectedItems(class_tree);
56 var transform = getSelectedItems(transform_tree)[0];
58 var reltype = $('path-label').getAttribute('reltype');
59 var class_label = $('path-label').value;
60 var relation_alias = hex_md5(class_label);
62 for (var i in items) {
65 var class_path = item.getAttribute('fullpath');
66 var field_class = item.getAttribute('idlclass');
67 var datatype = item.getAttribute('datatype');
68 var colname = item.getAttribute('idlfield');
69 var field_label = item.firstChild.firstChild.getAttribute('label');
71 var table_name = getIDLClass(field_class).getAttributeNS(persistNS,'tablename');
73 if ( !rpt_rel_cache[relation_alias] ) {
74 rpt_rel_cache[relation_alias] =
75 { label : class_label,
76 alias : relation_alias,
79 idlclass : field_class,
81 fields: { dis_tab : {}, filter_tab : {}, aggfilter_tab : {} }
85 if ( !rpt_rel_cache[relation_alias].fields[tabname][colname] ) {
86 rpt_rel_cache[relation_alias].fields[tabname][colname] =
88 transform : (transform && transform.getAttribute('name')) || 'Bare',
89 aggregate : transform && transform.getAttribute('aggregate'),
90 params : transform && transform.getAttribute('params'),
91 transform_label: (transform && transform.getAttribute('alias')) || 'Raw Data',
99 if (!rpt_rel_cache.order_by)
100 rpt_rel_cache.order_by = [];
102 if (tabname == 'dis_tab')
103 rpt_rel_cache.order_by.push( { relation : relation_alias, field : colname } );
107 'You have already added the [' + field_label +
108 '] field\nfrom the [' + class_label + '] source. Click OK if you\nwould like ' +
109 'to reset this field.'
112 rpt_rel_cache[relation_alias].fields[tabname][colname] =
114 transform : (transform && transform.getAttribute('name')) || 'Bare',
115 aggregate : transform && transform.getAttribute('aggregate'),
116 params : transform && transform.getAttribute('params'),
117 transform_label: (transform && transform.getAttribute('alias')) || 'Raw Data',
130 function changeDisplayOrder (dir) {
131 var active_tab = filterByAttribute(
132 $('used-source-fields-tabbox').getElementsByTagName('tab'),
137 var tabname = active_tab.getAttribute('id');
139 var tabpanel = $( tabname + 'panel' );
140 var tree = tabpanel.getElementsByTagName('tree')[0];
141 var item = getSelectedItems(tree)[0];
143 var item_pos = tree.view.selection.currentIndex;
146 if ( item.previousSibling ) {
147 item.parentNode.insertBefore( item, item.previousSibling );
150 } else if (dir == 'd') {
151 if ( item.nextSibling ) {
152 if (item.nextSibling.nextSibling ) item.parentNode.insertBefore( item, item.nextSibling.nextSibling );
153 else item.parentNode.appendChild( item );
158 rpt_rel_cache.order_by = [];
159 var ordered_list = tree.getElementsByTagName('treeitem');
160 for (var i = 0; i < ordered_list.length; i++) {
161 rpt_rel_cache.order_by.push(
162 { relation : ordered_list[i].getAttribute('relation'),
163 field : ordered_list[i].firstChild.firstChild.nextSibling.getAttribute('label')
168 tree.view.selection.select( item_pos );
171 function alterColumnLabel () {
172 var active_tab = filterByAttribute(
173 $('used-source-fields-tabbox').getElementsByTagName('tab'),
178 var tabname = active_tab.getAttribute('id');
180 var tabpanel = $( tabname + 'panel' );
181 var tree = tabpanel.getElementsByTagName('tree')[0];
182 var items = getSelectedItems(tree);
184 for (var i in items) {
186 var relation_alias = item.getAttribute('relation');
188 var field = item.firstChild.firstChild;
189 var colname = field.nextSibling.getAttribute('label');
191 var new_label = prompt(
192 "Change the column header to:",
193 field.getAttribute("label")
197 rpt_rel_cache[relation_alias].fields[tabname][colname].alias = new_label;
203 function alterColumnTransform (trans) {
205 var transform = OILS_RPT_TRANSFORMS[trans];
207 var active_tab = filterByAttribute(
208 $('used-source-fields-tabbox').getElementsByTagName('tab'),
213 var tabname = active_tab.getAttribute('id');
215 var tabpanel = $( tabname + 'panel' );
216 var tree = tabpanel.getElementsByTagName('tree')[0];
217 var item = getSelectedItems(tree)[0];
218 var relation_alias = item.getAttribute('relation');
220 var field = item.firstChild.firstChild;
221 var colname = field.nextSibling.getAttribute('label');
223 rpt_rel_cache[relation_alias].fields[tabname][colname].transform = trans;
224 rpt_rel_cache[relation_alias].fields[tabname][colname].aggregate = transform.aggregate;
225 rpt_rel_cache[relation_alias].fields[tabname][colname].params = transform.params;
226 rpt_rel_cache[relation_alias].fields[tabname][colname].transform_label = transform.label;
232 function changeOperator (args) {
234 var active_tab = filterByAttribute(
235 $('used-source-fields-tabbox').getElementsByTagName('tab'),
240 var tabname = active_tab.getAttribute('id');
242 var tabpanel = $( tabname + 'panel' );
243 var tree = tabpanel.getElementsByTagName('tree')[0];
244 var items = getSelectedItems(tree);
246 for (var i in items) {
248 var relation_alias = item.getAttribute('relation');
250 var field = item.firstChild.firstChild;
251 var colname = field.nextSibling.getAttribute('label');
253 rpt_rel_cache[relation_alias].fields[tabname][colname].op = args.op;
254 rpt_rel_cache[relation_alias].fields[tabname][colname].op_label = args.label;
262 function removeTemplateFilterValue () {
264 var active_tab = filterByAttribute(
265 $('used-source-fields-tabbox').getElementsByTagName('tab'),
270 var tabname = active_tab.getAttribute('id');
272 var tabpanel = $( tabname + 'panel' );
273 var tree = tabpanel.getElementsByTagName('tree')[0];
274 var items = getSelectedItems(tree);
276 for (var i in items) {
278 var relation_alias = item.getAttribute('relation');
280 var field = item.firstChild.firstChild;
281 var colname = field.nextSibling.getAttribute('label');
283 rpt_rel_cache[relation_alias].fields[tabname][colname].op_value = {};
289 function timestampSetDate (obj, cal, date) {
290 obj.op_value.value = date;
291 obj.op_value.object = cal.date;
292 obj.op_value.label = '"' + date + '"';
299 function changeTemplateFilterValue () {
301 var active_tab = filterByAttribute(
302 $('used-source-fields-tabbox').getElementsByTagName('tab'),
307 var tabname = active_tab.getAttribute('id');
309 var tabpanel = $( tabname + 'panel' );
310 var tree = tabpanel.getElementsByTagName('tree')[0];
311 var items = getSelectedItems(tree);
313 var targetCmd = $( tabname + '_value_action' );
315 targetCmd.menu = null;
316 targetCmd.command = null;
317 targetCmd.oncommand = null;
318 targetCmd.removeEventListener( 'command', __handler_cache, true );
320 for (var i in items) {
322 var relation_alias = item.getAttribute('relation');
324 var field = item.firstChild.firstChild;
325 var colname = field.nextSibling.getAttribute('label');
327 var obj = rpt_rel_cache[relation_alias].fields[tabname][colname]
328 var operation = OILS_RPT_FILTERS[obj.op];
330 switch (obj.datatype) {
332 var cal_popup = $('calendar-widget');
334 while (cal_popup.firstChild) cal_popup.removeChild(cal_popup.lastChild);
335 var calendar = new Calendar(
338 function (cal,date) { timestampSetDate(obj,cal,date) },
339 function (cal) { cal_popup.hidePopup(); cal.destroy(); }
342 var format = OILS_RPT_TRANSFORMS[obj.transform].cal_format || '%Y-%m-%d';
344 calendar.setDateFormat(format);
345 calendar.create(cal_popup);
347 targetCmd.menu = 'calendar-widget';
353 function __bool_value_event_handler () {
357 // get a reference to the prompt service component.
358 var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
359 .getService(Components.interfaces.nsIPromptService);
361 // set the buttons that will appear on the dialog. It should be
362 // a set of constants multiplied by button position constants. In this case,
363 // three buttons appear, Save, Cancel and a custom button.
364 var flags=promptService.BUTTON_TITLE_IS_STRING * promptService.BUTTON_POS_0 +
365 promptService.BUTTON_TITLE_IS_STRING * promptService.BUTTON_POS_1 +
366 promptService.BUTTON_TITLE_CANCEL * promptService.BUTTON_POS_2;
368 // display the dialog box. The flags set above are passed
369 // as the fourth argument. The next three arguments are custom labels used for
370 // the buttons, which are used if BUTTON_TITLE_IS_STRING is assigned to a
371 // particular button. The last two arguments are for an optional check box.
372 answer = promptService.select(
375 "Select the value, or cancel:",
376 2, ["True", "False"], state
380 state = confirm("Click OK for TRUE and Cancel for FALSE.");
381 state ? state = 0 : state = 1;
386 obj.op_value.value = 'f';
387 obj.op_value.label = 'False';
389 obj.op_value.value = 't';
390 obj.op_value.label = 'True';
394 targetCmd.removeEventListener( 'command', __bool_value_event_handler, true );
398 __handler_cache = __bool_value_event_handler;
399 targetCmd.addEventListener( 'command', __bool_value_event_handler, true );
406 var promptstring = "Field does not match one of list (comma separated):";
410 promptstring = "Field value is not between (comma separated):";
414 promptstring = "Field value is between (comma separated):";
418 promptstring = "Field does not match one of list (comma separated):";
422 promptstring = "Field matches one of list (comma separated):";
426 promptstring = "Value " + obj.op_label + ":";
430 function __default_value_event_handler () {
432 if (_v = prompt( promptstring, obj.op_value.value || '' )) {
438 obj.op_value.value = _v.split(/\s*,\s*/);
442 obj.op_value.value = _v;
446 obj.op_value.label = '"' + obj.op_value.value + '"';
449 targetCmd.removeEventListener( 'command', __default_value_event_handler, true );
453 __handler_cache = __default_value_event_handler;
454 targetCmd.addEventListener( 'command', __default_value_event_handler, true );
460 //renderSources(true);
463 function populateOperatorContext () {
465 var active_tab = filterByAttribute(
466 $('used-source-fields-tabbox').getElementsByTagName('tab'),
471 var tabname = active_tab.getAttribute('id');
473 var tabpanel = $( tabname + 'panel' );
474 var tree = tabpanel.getElementsByTagName('tree')[0];
475 var items = getSelectedItems(tree);
478 for (var i in items) {
480 dtypes.push( item.getAttribute('datatype') );
483 var menu = $(tabname + '_op_menu');
484 while (menu.firstChild) menu.removeChild(menu.lastChild);
486 for (var i in OILS_RPT_FILTERS) {
487 var o = OILS_RPT_FILTERS[i];
491 oncommand : "changeOperator({op:'"+i+"',label:'"+o.label+"'})",
496 var keys = getKeys(o.labels);
497 for ( var k in keys ) {
499 if ( grep(function(x){return key==x},dtypes).length ) {
502 { label : o.labels[key],
503 oncommand : "changeOperator({op:'"+i+"',label:'"+o.labels[key]+"'})",
513 function populateTransformContext () {
515 var active_tab = filterByAttribute(
516 $('used-source-fields-tabbox').getElementsByTagName('tab'),
521 var tabname = active_tab.getAttribute('id');
523 var tabpanel = $( tabname + 'panel' );
524 var tree = tabpanel.getElementsByTagName('tree')[0];
525 var items = getSelectedItems(tree);
528 for (var i in items) {
530 var dtype = item.getAttribute('datatype');
531 var item_transforms = getTransforms({ datatype : dtype });
533 for (var j in item_transforms) {
534 transforms[item_transforms[j]] = OILS_RPT_TRANSFORMS[item_transforms[j]];
535 transforms[item_transforms[j]].name = item_transforms[j];
539 var transformList = [];
540 for (var i in transforms) {
541 transformList.push( transforms[i] );
544 transformList.sort( sortHashLabels );
546 var menu = $(tabname + '_trans_menu');
547 while (menu.firstChild) menu.removeChild(menu.lastChild);
549 for (var i in transformList) {
550 var t = transformList[i];
552 if (tabname.match(/filter/)) {
553 if (tabname.match(/^agg/)) {
554 if (!t.aggregate) continue;
556 if (t.aggregate) continue;
562 { aggregate : t.aggregate,
567 oncommand : "alterColumnTransform('"+t.name+"')",
573 menu.parentNode.setAttribute('disabled','false');
574 if (menu.childNodes.length == 0) {
575 menu.parentNode.setAttribute('disabled','true');
580 function renderSources (selected) {
582 var tree = $('used-sources');
583 var sources = $('used-sources-treetop');
584 var tabs = $('used-source-fields-tabbox').getElementsByTagName('tab');
587 while (sources.firstChild) sources.removeChild(sources.lastChild);
589 selected = getSelectedItems(tree);
590 if (!selected.length) {
591 selected = undefined;
592 while (sources.firstChild) sources.removeChild(sources.lastChild);
596 for (var j = 0; j < tabs.length; j++) {
598 var tabname = tab.getAttribute('id')
599 var tabpanel = $( tab.getAttribute('id') + 'panel' );
600 var fieldtree = tabpanel.getElementsByTagName('treechildren')[0];
602 while (fieldtree.firstChild) fieldtree.removeChild(fieldtree.lastChild);
605 for (var relation_alias in rpt_rel_cache) {
606 if (!rpt_rel_cache[relation_alias].fields) continue;
612 return x.getAttribute('relation') == relation_alias;
619 $('used-sources-treetop').appendChild(
621 { relation : rpt_rel_cache[relation_alias].alias,
622 idlclass : rpt_rel_cache[relation_alias].idlclass,
623 reltype : rpt_rel_cache[relation_alias].reltype,
624 path : rpt_rel_cache[relation_alias].path,
628 createTreeCell({ label : rpt_rel_cache[relation_alias].label }),
629 createTreeCell({ label : rpt_rel_cache[relation_alias].table }),
630 createTreeCell({ label : rpt_rel_cache[relation_alias].alias }),
631 createTreeCell({ label : rpt_rel_cache[relation_alias].reltype })
637 for each (var tabname in ['filter_tab','aggfilter_tab']) {
638 tabpanel = $( tabname + 'panel' );
639 fieldtree = tabpanel.getElementsByTagName('treechildren')[0];
641 for (var colname in rpt_rel_cache[relation_alias].fields[tabname]) {
642 with (rpt_rel_cache[relation_alias].fields[tabname][colname]) {
643 fieldtree.appendChild(
645 { relation : relation_alias, datatype : datatype },
648 createTreeCell({ label : alias }),
649 createTreeCell({ label : colname }),
650 createTreeCell({ label : datatype }),
651 createTreeCell({ label : transform_label }),
652 createTreeCell({ label : transform })
657 fieldtree.lastChild.firstChild.appendChild(
658 createTreeCell({ op : op, label : op_label })
661 if (op_value.value != undefined) {
662 fieldtree.lastChild.firstChild.appendChild(
663 createTreeCell({ label : op_value.label })
671 tabpanel = $( 'dis_tabpanel' );
672 fieldtree = tabpanel.getElementsByTagName('treechildren')[0];
673 for each (var order in rpt_rel_cache.order_by) {
679 return x.getAttribute('relation') == order.relation;
686 with (rpt_rel_cache[order.relation].fields.dis_tab[order.field]) {
687 fieldtree.appendChild(
689 { relation : order.relation, datatype : datatype },
692 createTreeCell({ label : alias }),
693 createTreeCell({ label : colname }),
694 createTreeCell({ label : datatype }),
695 createTreeCell({ label : transform_label }),
696 createTreeCell({ label : transform })
701 fieldtree.lastChild.firstChild.appendChild(
702 createTreeCell({ op : op, label : op_label })
705 if (op_value.value != undefined) {
706 fieldtree.lastChild.firstChild.appendChild(
707 createTreeCell({ label : op_value.label })
717 filter_tab : 'where',
718 aggfilter_tab : 'having',
722 function save_template () {
727 core_class : $('sources-treetop').getElementsByTagName('treeitem')[0].getAttribute('idlclass'),
735 for (var relname in rpt_rel_cache) {
736 var relation = rpt_rel_cache[relname];
737 if (!relation.fields) continue;
739 // first, add the where and having clauses (easier)
740 for each (var tab_name in [ 'filter_tab', 'aggfilter_tab' ]) {
741 var tab = relation.fields[tab_name];
742 for (var field in tab) {
743 fleshTemplateField( template, relation, tab_name, field );
747 // now the from clause
748 fleshFromPath( template, relation );
751 // and now for select (based on order_by)
752 for each (var order in rpt_rel_cache.order_by)
753 fleshTemplateField( template, rpt_rel_cache[order.relation], 'dis_tab', order.field );
756 // and the saving throw ...
758 var session = cgi.param('ses');
759 fetchUser( session );
762 tmpl.name( $('template-name').value );
763 tmpl.description( $('template-description').value );
764 tmpl.owner(USER.id());
765 tmpl.folder(cgi.param('folder'));
766 tmpl.data(js2JSON(template));
768 // prompt( 'template', js2JSON( tmpl ) );
770 if(!confirm('Name : '+tmpl.name() + '\nDescription: ' + tmpl.description()+'\nSave Template?'))
773 var req = new Request('open-ils.reporter:open-ils.reporter.template.create', session, tmpl);
774 req.request.alertEvent = false;
777 var res = r.getResultObject();
778 if(checkILSEvent(res)) {
781 if( res && res != '0' ) {
782 confirm('Template ' + tmpl.name() + ' was successfully saved.');
783 _l('../oils_rpt.xhtml');
792 function fleshFromPath ( template, rel ) {
793 var table_path = rel.path.split( /\./ );
794 if (table_path.length > 1 || rel.path.indexOf('-') > -1) table_path.push( rel.idlclass );
798 var current_path = '';
799 var current_obj = template.from;
801 while (link = table_path.shift()) {
802 if (current_path) current_path += '-';
803 current_path += link;
805 var leaf = table_path.length == 0 ? true : false;
807 current_obj.path = current_path;
808 current_obj.table = getIDLClass( link.split(/-/)[0] ).getAttributeNS( persistNS, 'tablename' );
810 if (prev_link != '') {
811 var prev_class = getIDLClass( prev_link.split(/-/)[0] );
812 var prev_field = prev_link.split(/-/)[1];
814 var current_link = getIDLLink( prev_class, prev_field );
815 current_obj.key = current_link.getAttribute('key');
818 current_link.getAttribute('reltype') != 'has_a' ||
819 prev_type == 'left' ||
820 rel.reltype != 'has_a'
821 ) current_obj.type = 'left';
823 prev_type = current_obj.type;
827 current_obj.label = rel.label;
828 current_obj.alias = rel.alias;
829 current_obj.idlclass = rel.idlclass;
830 current_obj.template_path = rel.path;
832 var current_class = getIDLClass( link.split(/-/)[0] );
833 var join_field = link.split(/-/)[1];
834 var join_link = getIDLLink(current_class, join_field);
836 if (join_link.getAttribute('reltype') != 'has_a') {
837 var fields_el = current_class.getElementsByTagName('fields')[0];
838 join_field = fields_el.getAttributeNS(persistNS, 'primary') + '-' + join_link.getAttribute('key');
841 current_obj.alias = hex_md5( current_path ) ;
842 join_field += '-' + current_obj.alias;
843 if (table_path.length == 1) join_field += '-' + rel.alias;
845 if (!current_obj.join) current_obj.join = {};
846 if (!current_obj.join[join_field]) current_obj.join[join_field] = {};
848 if (current_obj.type == 'left') current_obj.join[join_field].type = 'left';
850 current_obj = current_obj.join[join_field];
859 function fleshTemplateField ( template, rel, tab_name, field ) {
861 if (!rel.fields[tab_name] || !rel.fields[tab_name][field]) return;
863 var tab = rel.fields[tab_name];
865 var field_path = rel.path + '-' + field;
866 field_path = field_path.replace(/\./g, '-');
869 alias : tab[field].alias,
872 transform : tab[field].transform,
873 transform_label : tab[field].transform_label
879 if (tab_name.match(/filter/)) {
880 element.condition = {};
881 element.condition[tab[field].op] =
882 tab[field].op_value.value ?
883 tab[field].op_value.value :
884 '::P' + param_count++;
887 template[tab_use[tab_name]].push(element);