2 * Report template builder
5 angular.module('egReporter',
6 ['ngRoute', 'ui.bootstrap', 'egCoreMod', 'egUiMod', 'egGridMod', 'egReportMod', 'treeControl', 'ngToast'])
8 .config(['ngToastProvider', function(ngToastProvider) {
9 ngToastProvider.configure({
10 verticalPosition: 'bottom',
15 .config(function($routeProvider, $locationProvider, $compileProvider) {
16 $locationProvider.html5Mode(true);
17 $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|mailto|blob):/); // grid export
19 var resolver = {delay : function(egStartup) {return egStartup.go()}};
21 $routeProvider.when('/reporter/template/clone/:folder/:id', {
22 templateUrl: './reporter/t_edit_template',
23 controller: 'ReporterTemplateEdit',
27 $routeProvider.when('/reporter/legacy/template/clone/:folder/:id', {
28 templateUrl: './reporter/t_legacy',
29 controller: 'ReporterTemplateLegacy',
33 $routeProvider.when('/reporter/template/new/:folder', {
34 templateUrl: './reporter/t_edit_template',
35 controller: 'ReporterTemplateEdit',
39 $routeProvider.when('/reporter/legacy/main', {
40 templateUrl: './reporter/t_legacy',
41 controller: 'ReporterTemplateLegacy',
46 $routeProvider.otherwise({redirectTo : '/reporter/legacy/main'});
50 * controller for legacy template stuff
52 .controller('ReporterTemplateLegacy',
53 ['$scope','$routeParams','$location','egCore',
54 function($scope , $routeParams , $location , egCore) {
56 var template_id = $routeParams.id;
57 var folder_id = $routeParams.folder;
59 $scope.rurl = '/reports/oils_rpt.xhtml?ses=' + egCore.auth.token();
62 $scope.rurl = '/reports/oils_rpt_builder.xhtml?ses=' +
63 egCore.auth.token() + '&folder=' + folder_id;
65 if (template_id) $scope.rurl += '&ct=' + template_id;
71 * Uber-controller for template editing
73 .controller('ReporterTemplateEdit',
74 ['$scope','$q','$routeParams','$location','$timeout','$window','egCore','$uibModal','egPromptDialog',
75 'egGridDataProvider','egReportTemplateSvc','$uibModal','egConfirmDialog','egSelectDialog','ngToast',
76 function($scope , $q , $routeParams , $location , $timeout , $window, egCore , $uibModal , egPromptDialog ,
77 egGridDataProvider , egReportTemplateSvc , $uibModal , egConfirmDialog , egSelectDialog , ngToast) {
79 function values(o) { return Object.keys(o).map(function(k){return o[k]}) };
81 var template_id = $routeParams.id;
82 var folder_id = $routeParams.folder;
84 $scope.grid_display_fields_provider = egGridDataProvider.instance({
85 get : function (offset, count) {
86 return this.arrayNotifier(egReportTemplateSvc.display_fields, offset, count);
89 $scope.grid_filter_fields_provider = egGridDataProvider.instance({
90 get : function (offset, count) {
91 return this.arrayNotifier(egReportTemplateSvc.filter_fields, offset, count);
95 var dgrid = $scope.display_grid_controls = {};
96 var fgrid = $scope.filter_grid_controls = {};
98 var default_filter_obj = {
100 label : egReportTemplateSvc.Filters['='].label
103 var default_transform_obj = {
105 label : egReportTemplateSvc.Transforms.Bare.label,
109 function mergePaths (items) {
112 items.forEach(function (item) {
116 item.path.forEach(function (p, i, a) {
117 var alias; // unpredictable hashes are fine for intermediate tables
119 if (i) { // not at the top of the tree
120 if (i == 1) join_path = join_path.split('-')[0];
122 // SQLBuilder relies on the first dash-separated component
123 // of the join key to specify the column of left-hand relation
124 // to join on; for has_many and might_have link types, we have to grab the
125 // primary key of the left-hand table; otherwise, we can
126 // just use the field/column name found in p.uplink.name.
127 var uplink = (p.uplink.reltype == 'has_many' || p.uplink.reltype == 'might_have') ?
128 egCore.idl.classes[p.from.split('.').slice(-1)[0]].pkey + '-' + p.uplink.name :
130 join_path += '-' + uplink;
131 alias = hex_md5(join_path);
133 var uplink_alias = uplink + '-' + alias;
135 if (!t.join) t.join = {};
136 if (!t.join[uplink_alias]) t.join[uplink_alias] = {};
138 t = t.join[uplink_alias];
140 var djtype = 'inner';
141 if (p.uplink.reltype != 'has_a') djtype = 'left';
143 t.type = p.jtype || djtype;
144 t.key = p.uplink.key;
146 join_path = p.classname + '-' + p.classname;
147 alias = hex_md5(join_path);
150 if (!t.alias) t.alias = alias;
153 t.table = p.struct.source ? p.struct.source : p.table;
154 t.idlclass = p.classname;
156 if (a.length == i + 1) { // end of the path array, need a predictable hash
157 t.label = item.path_label;
158 t.alias = hex_md5(item.path_label);
166 // expose for testing
167 $scope._mergePaths = mergePaths;
169 $scope.constructTemplate = function () {
170 var param_counter = 0;
173 doc_url : $scope.templateDocURL,
174 core_class : egCore.idl.classTree.top.classname,
175 select : dgrid.allItems().map(function (i) {
178 path : i.path[i.path.length - 1].classname + '-' + i.name,
179 field_doc : i.doc_text,
180 relation : hex_md5(i.path_label),
183 transform : i.transform ? i.transform.transform : '',
184 transform_label : i.transform ? i.transform.label : '',
185 aggregate : !!i.transform.aggregate
189 from : mergePaths( dgrid.allItems().concat(fgrid.allItems()) ),
190 where : fgrid.allItems().filter(function(i) {
191 return !i.transform.aggregate;
192 }).map(function (i) {
195 i.operator.op == 'is' ||
196 i.operator.op == 'is not' ||
197 i.operator.op == 'is blank' ||
198 i.operator.op == 'is not blank'
200 cond[i.operator.op] = null;
202 if (i.value === undefined) {
203 cond[i.operator.op] = '::P' + param_counter++;
205 cond[i.operator.op] = i.value;
210 path : i.path[i.path.length - 1].classname + '-' + i.name,
211 field_doc : i.doc_text,
212 relation : hex_md5(i.path_label),
215 transform : i.transform.transform,
216 transform_label : i.transform.label,
219 condition : cond // constructed above
222 having : fgrid.allItems().filter(function(i) {
223 return !!i.transform.aggregate;
224 }).map(function (i) {
226 cond[i.operator.op] = '::P' + param_counter++;
229 path : i.path[i.path.length - 1].classname + '-' + i.name,
230 field_doc : i.doc_text,
231 relation : hex_md5(i.path_label),
234 transform : i.transform.transform,
235 transform_label : i.transform.label,
238 condition : cond // constructed above
241 display_cols: angular.copy( dgrid.allItems() ).map(strip_item),
242 filter_cols : angular.copy( fgrid.allItems() ).map(strip_item)
245 function strip_item (i) {
247 i.path.forEach(function(p){
251 delete p.struct.permacrud;
252 delete p.struct.field_map;
253 delete p.struct.fields;
260 $scope.upgradeTemplate = function(template) {
261 template.name(template.name() + ' (converted from XUL)');
262 template.data.version = 5;
266 for (var key in template.data.rel_cache) {
267 if (key == 'order_by') {
268 order_by = template.data.rel_cache[key];
270 rels.push(template.data.rel_cache[key]);
274 template.data['display_cols'] = [];
275 template.data['filter_cols'] = [];
277 var dispcol_index = 0;
279 function _convertPath(orig, rel) {
282 var table_path = rel.path.split(/\./);
283 if (table_path.length > 1 || rel.path.indexOf('-') > -1) table_path.push( rel.idlclass );
287 table_path.forEach(function(link) {
288 var cls = link.split(/-/)[0];
289 var fld = link.split(/-/)[1];
291 label : egCore.idl.classes[cls].label
293 if (prev_link != '') {
294 args['from'] = prev_link.split(/-/)[0];
295 var prev_col = prev_link.split(/-/)[1].split(/>/)[0];
296 egCore.idl.classes[prev_link.split(/-/)[0]].fields.forEach(function(f) {
297 if (prev_col == f.name) {
302 newPath.push(egCore.idl.classTree.buildNode(cls, args));
309 rels.map(function(rel) {
310 for (var col in rel.fields.dis_tab) {
311 var orig = rel.fields.dis_tab[col];
314 path : _convertPath(orig, rel),
315 index : dispcol_index++,
317 datatype : orig.datatype,
318 doc_text : orig.field_doc,
320 label : orig.transform_label,
321 transform : orig.transform,
322 aggregate : orig.aggregate
324 path_label : rel.label
326 template.data.display_cols.push(display_col);
330 rels.map(function(rel) {
331 for (var col in rel.fields.filter_tab) {
332 var orig = rel.fields.filter_tab[col];
335 path : _convertPath(orig, rel),
336 index : dispcol_index++,
338 datatype : orig.datatype,
339 doc_text : orig.field_doc,
342 label : orig.op_label
345 label : orig.transform_label,
346 transform : orig.transform,
347 aggregate : orig.aggregate
349 path_label : rel.label
351 if ('value' in orig.op_value) {
352 filter_col['value'] = orig.op_value.value;
354 template.data.filter_cols.push(filter_col);
360 function loadTemplate () {
361 if (!template_id) return;
362 egCore.pcrud.retrieve( 'rt', template_id)
363 .then( function(template) {
364 template.data = angular.fromJson(template.data());
365 if (template.data.version < 5) {
366 $scope.upgradeTemplate(template);
369 $scope.templateName = template.name() + ' (clone)';
370 $scope.templateDescription = template.description();
371 $scope.templateDocURL = template.data.doc_url;
373 $scope.changeCoreSource( template.data.core_class );
375 egReportTemplateSvc.display_fields = template.data.display_cols;
376 egReportTemplateSvc.filter_fields = template.data.filter_cols;
386 $scope.saveTemplate = function () {
387 var tmpl = new egCore.idl.rt();
388 tmpl.name( $scope.templateName );
389 tmpl.description( $scope.templateDescription );
390 tmpl.owner(egCore.auth.user().id());
391 tmpl.folder(folder_id);
392 tmpl.data(angular.toJson($scope.constructTemplate()));
394 egConfirmDialog.open(tmpl.name(), egCore.strings.TEMPLATE_CONF_CONFIRM_SAVE,
396 return egCore.pcrud.create( tmpl )
399 ngToast.create(egCore.strings.TEMPLATE_CONF_SUCCESS_SAVE);
402 $window.location.href = egCore.env.basePath + 'reporter/legacy/main';
408 ngToast.warning(egCore.strings.TEMPLATE_CONF_FAIL_SAVE);
415 $scope.addDisplayFields = function () {
416 var t = $scope.selected_transform;
417 if (!t) t = default_transform_obj;
419 egReportTemplateSvc.addFields(
421 $scope.selected_source_field_list,
423 $scope.currentPathLabel,
429 $scope.addFilterFields = function () {
430 var t = $scope.selected_transform;
431 if (!t) t = default_transform_obj;
432 f = default_filter_obj;
434 egReportTemplateSvc.addFields(
436 $scope.selected_source_field_list,
438 $scope.currentPathLabel,
445 $scope.moveDisplayFieldUp = function (items) {
446 items.reverse().forEach(function(item) {
447 egReportTemplateSvc.moveFieldUp('display_fields', item);
452 $scope.moveDisplayFieldDown = function (items) {
453 items.forEach(function(item) {
454 egReportTemplateSvc.moveFieldDown('display_fields', item);
459 $scope.removeDisplayField = function (items) {
460 items.forEach(function(item) {egReportTemplateSvc.removeField('display_fields', item)});
464 $scope.changeDisplayLabel = function (items) {
465 items.forEach(function(item) {
466 egPromptDialog.open(egCore.strings.TEMPLATE_CONF_PROMPT_CHANGE, item.label || '',
467 {ok : function(value) {
468 if (value) egReportTemplateSvc.display_fields[item.index].label = value;
475 $scope.changeDisplayFieldDoc = function (items) {
476 items.forEach(function(item) {
477 egPromptDialog.open(egCore.strings.TEMPLATE_FIELD_DOC_PROMPT_CHANGE, item.doc_text || '',
478 {ok : function(value) {
479 if (value) egReportTemplateSvc.display_fields[item.index].doc_text = value;
486 $scope.changeFilterFieldDoc = function (items) {
487 items.forEach(function(item) {
488 egPromptDialog.open(egCore.strings.TEMPLATE_FIELD_DOC_PROMPT_CHANGE, item.doc_text || '',
489 {ok : function(value) {
490 if (value) egReportTemplateSvc.filter_fields[item.index].doc_text = value;
497 $scope.changeFilterValue = function (items) {
498 items.forEach(function(item) {
500 if (item.datatype == "bool") {
501 var displayVal = typeof item.value === "undefined" ? egCore.strings.TEMPLATE_CONF_UNSET :
502 item.value === 't' ? egCore.strings.TEMPLATE_CONF_TRUE :
503 item.value === 'f' ? egCore.strings.TEMPLATE_CONF_FALSE :
504 item.value.toString();
505 egConfirmDialog.open(egCore.strings.TEMPLATE_CONF_DEFAULT, displayVal,
507 egReportTemplateSvc.filter_fields[item.index].value = 't';
509 cancel : function() {
510 egReportTemplateSvc.filter_fields[item.index].value = 'f';
511 }}, egCore.strings.TEMPLATE_CONF_TRUE, egCore.strings.TEMPLATE_CONF_FALSE
514 egPromptDialog.open(egCore.strings.TEMPLATE_CONF_DEFAULT, item.value || '',
515 {ok : function(value) {
516 if (value) _update_filter_value(item, value);
524 _update_filter_value = function(item, value) {
525 switch (item.operator.op) {
530 //if value isn't an array yet, split into an array for
531 // operators that need it
532 if (typeof value === 'string') {
533 value = value.split(/\s*,\s*/);
538 //if value was split but shouldn't be, then convert back to
539 // comma-separated string
540 if (Array.isArray(value)) {
541 value = value.toString();
545 egReportTemplateSvc.filter_fields[item.index].value = value;
548 $scope.changeTransform = function (items) {
553 angular.forEach(egReportTemplateSvc.Transforms, function (o,n) {
554 if ( o.datatype.indexOf(f.datatype) > -1) {
555 if (tlist.indexOf(o.label) == -1) tlist.push( o.label );
559 items.forEach(function(item) {
561 egCore.strings.SELECT_TFORM, tlist, item.transform.label,
562 {ok : function(value) {
564 var t = egReportTemplateSvc.getTransformByLabel(value);
568 aggregate : egReportTemplateSvc.Transforms[t].aggregate ? true : false
578 $scope.changeOperator = function (items) {
581 Object.keys(egReportTemplateSvc.Filters).forEach(function(k){
582 var v = egReportTemplateSvc.Filters[k];
583 if (flist.indexOf(v.label) == -1) flist.push(v.label);
584 if (v.labels && v.labels.length > 0) {
585 v.labels.forEach(function(l) {
586 if (flist.indexOf(l) == -1) flist.push(l);
591 items.forEach(function(item) {
592 var l = item.operator ? item.operator.label : '';
594 egCore.strings.SELECT_OP, flist, l,
595 {ok : function(value) {
597 var t = egReportTemplateSvc.getFilterByLabel(value);
598 item.operator = { label: value, op : t };
600 //Update the filter value based on the new operator, because
601 // different operators treat the value differently
602 _update_filter_value(item, egReportTemplateSvc.filter_fields[item.index].value);
611 $scope.removeFilterValue = function (items) {
612 items.forEach(function(item) {delete egReportTemplateSvc.filter_fields[item.index].value});
616 $scope.removeFilterField = function (items) {
617 items.forEach(function(item) {egReportTemplateSvc.removeField('filter_fields', item)});
621 $scope.allSources = values(egCore.idl.classes).sort( function(a,b) {
622 if (a.core && !b.core) return -1;
623 if (b.core && !a.core) return 1;
624 aname = a.label ? a.label : a.name;
625 bname = b.label ? b.label : b.name;
626 if (aname > bname) return 1;
630 $scope.class_tree = [];
631 $scope.selected_source = null;
632 $scope.selected_source_fields = [];
633 $scope.selected_source_field_list = [];
634 $scope.available_field_transforms = [];
635 $scope.coreSource = null;
636 $scope.coreSourceChosen = false;
637 $scope.currentPathLabel = '';
639 $scope.treeExpand = function (node, expanding) {
640 if (expanding) node.children.map(egCore.idl.classTree.fleshNode);
643 $scope.filterFields = function (n) {
644 return n.virtual ? false : true;
645 // should we hide links?
646 return n.datatype && n.datatype != 'link'
649 $scope.field_tree_opts = {
650 multiSelection: true,
651 equality : function(node1, node2) {
652 return node1.name == node2.name;
656 $scope.field_transforms_tree_opts = {
657 equality : function(node1, node2) {
658 if (!node2) return false;
659 return node1.transform == node2.transform;
663 $scope.selectFields = function () {
664 while ($scope.available_field_transforms.length) {
665 $scope.available_field_transforms.pop();
668 angular.forEach( $scope.selected_source_field_list, function (f) {
669 angular.forEach(egReportTemplateSvc.Transforms, function (o,n) {
670 if ( o.datatype.indexOf(f.datatype) > -1) {
673 angular.forEach($scope.available_field_transforms, function (t) {
674 if (t.transform == n) include = false;
677 if (include) $scope.available_field_transforms.push({
680 aggregate : o.aggregate ? true : false
688 $scope.selectSource = function (node, selected, $path) {
690 while ($scope.selected_source_field_list.length) {
691 $scope.selected_source_field_list.pop();
693 while ($scope.selected_source_fields.length) {
694 $scope.selected_source_fields.pop();
698 $scope.currentPath = angular.copy( $path().reverse() );
699 $scope.selected_source = node;
700 $scope.currentPathLabel = $scope.currentPath.map(function(n,i){
702 if (i) l += ' (' + n.jtype + ')';
705 angular.forEach( node.fields, function (f) {
706 $scope.selected_source_fields.push( f );
709 $scope.currentPathLabel = '';
712 // console.log($scope.selected_source);
715 $scope.changeCoreSource = function (new_core) {
716 console.log('changeCoreSource: '+new_core);
717 function change_core () {
718 if (new_core) $scope.coreSource = new_core;
719 $scope.coreSourceChosen = true;
721 $scope.class_tree.pop();
722 $scope.class_tree.push(
723 egCore.idl.classTree.setTop($scope.coreSource)
726 while ($scope.selected_source_fields.length) {
727 $scope.selected_source_fields.pop();
730 while ($scope.available_field_transforms.length) {
731 $scope.available_field_transforms.pop();
734 $scope.currentPathLabel = '';
737 if ($scope.coreSourceChosen) {
738 egConfirmDialog.open(
739 egCore.strings.FOLDERS_TEMPLATE,
740 egCore.strings.SOURCE_SETUP_CONFIRM_EXIT,