Move busui to seperate repository
[bus.git] / resultSetManager.php


















































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
<?php
 
//
// Open Web Analytics - An Open Source Web Analytics Framework
//
// Copyright 2006 Peter Adams. All rights reserved.
//
// Licensed under GPL v2.0 http://www.gnu.org/copyleft/gpl.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// $Id$
//
 
require_once(OWA_BASE_CLASS_DIR.'pagination.php');
require_once(OWA_BASE_CLASS_DIR.'timePeriod.php');
require_once(OWA_DIR.'owa_template.php');
 
/**
 * Result Set Manager
 *
 * Responsible for creating a data result set from various metrics and dimensions
 * 
 * @author      Peter Adams <peter@openwebanalytics.com>
 * @copyright   Copyright &copy; 2006 Peter Adams <peter@openwebanalytics.com>
 * @license     http://www.gnu.org/copyleft/gpl.html GPL v2.0
 * @category    owa
 * @package     owa
 * @version             $Revision$            
 * @since               owa 1.3.0
 */
 
class owa_resultSetManager extends owa_base {
 
        /**
         * The params of the caller, either a report or graph
         *
         * @var array
         */
        var $params = array();
                
        /**
         * The lables for calculated measures
         *
         * @var array
         */
        var $labels = array();
                
        /**
         * Data Access Object
         *
         * @var object
         */
        var $db;
        
        /**
         * The dimensions to groupby
         *
         * @var array
         */
        var $dimensions = array();
        
        /**
         * The Number of Dimensions to groupby
         *
         * @var integer
         */
        var $dimensionCount;
        
        /**
         * The table/column or denormalized dimensions 
         * associated with this metric
         *
         * @var array
         */
        var $denormalizedDimensions = array();
        
        var $_default_offset = 0;
        var $page;
        var $limit;
        var $order;
        var $format;
        var $constraint_operators = array('==','!=','>=', '<=', '>', '<', '=~', '!~', '=@','!@');
        var $related_entities = array();
        var $related_dimensions = array();
        var $related_metrics = array();
        var $resultSet;
        var $base_table;
        var $metrics = array();
        var $metricsByTable = array();
        var $childMetrics = array();
        var $calculatedMetrics = array();
        var $query_params = array();
        var $baseEntity;
        var $metricObjectsByEntityMap = array();
        var $errors = array();
        var $formatters = array();
        
        function __construct($db = '') {
                
                if ($db) {
                        $this->db = $db;        
                } else {
                        $this->db = owa_coreAPI::dbSingleton();
                }
                
                $this->formatters = array(
                        //'yyyymmdd' => array($this, 'dateFormatter'),
                        'timestamp'             => array($this, 'formatSeconds'),
                        'percentage'    => array($this, 'formatPercentage'), 
                        'integer'               => array($this, 'numberFormatter'),
                        'currency'              => array($this, 'formatCurrency')
                );
                
                return parent::__construct();
        }
        
                
        function setConstraint($name, $value, $operator = '') {
                
                if (empty($operator)) {
                        $operator = '=';
                }
                
                if (!empty($value)) {
                        $this->params['constraints'][] = array('operator' => $operator, 'value' => $value, 'name' => $name);
                }
        }
        
        function setConstraints($array) {
                
                if (is_array($array)) {
                        
                        if (is_array($this->params['constraints'])) {
                                $this->params['constraints'] = array_merge($array, $this->params['constraints']);
                        } else {
                                $this->params['constraints'] = $array;
                        }
                }
        }
        
        function constraintsStringToArray($string) {
                
                if ($string) {
                        //print_r($string);
                        // add string to query params array for use in URLs.
                        $this->query_params['constraints'] = $string;
                        
                        $constraints = explode(',', $string);
                        //print_r($constraints);
                        $constraint_array = array();
                        
                        foreach($constraints as $constraint) {
                                
                                foreach ($this->constraint_operators as $operator) {
                                        
                                        if (strpos($constraint, $operator)) {
                                                list ($name, $value) = split($operator, $constraint);
                                                
                                                $constraint_array[] = array('name' => $name, 'value' => $value, 'operator' => $operator);
                                        
 
                                                break;
                                        }
                                }
                        }
                        //print_r($constraint_array);
                        return $constraint_array;
                }
        }
        
        function getConstraints() {
        
                return $this->params['constraints'];
        }
        
        function applyConstraints() {
                
                $nconstraints = array();
                
                foreach ($this->getConstraints() as $k => $constraint) {
                        
                        $dim = $this->lookupDimension($constraint['name'], $this->baseEntity);
                        
                        //$dimEntity = owa_coreAPI::entityFactory($dim['entity']);
                        
                        
                        $col = $dim['column'];
                        $constraint['name'] = $col;
                        $nconstraints[$col] = $constraint;
                        $this->db->multiWhere($nconstraints);
                        //print_r($nconstraints);
                
                }
                
        }
        
        
        function chooseBaseEntity() {
                
                $metric_imps = array();
                
                // load metric implementations
                foreach ($this->metrics as $metric_name) {
                        
                        $metric_imps = array_merge($this->getMetricEntities($metric_name), $metric_imps);
                        
                        
                }
                //print_r($metric_imps);
                owa_coreAPI::debug('pre-reduce set of entities to choose from: '.print_r($metric_imps, true));
                
                $entities = array();
                // reduce entities      
                foreach ($metric_imps as $mimp) {
                        
                        if (empty($entities)) {
                                $entities = $mimp;
                        }
                        
                        $entities = $this->reduceTables($mimp, $entities);
                        
                        if (empty($entities)) {
                                return $this->addError('illegal metric combination');
                        }
                }
                
                owa_coreAPI::debug('post-reduce set of entities to choose from: '.print_r($entities, true));
                
                // check summary level of entities
                $niceness = array();
                
                foreach ($entities as $entity) {
                        
                        $niceness[$entity] = owa_coreAPI::entityFactory($entity)->getSummaryLevel();
                }
                //sort by summary level
                arsort($niceness);
                
                owa_coreAPI::debug('Entities summary levels: '.print_r($niceness, true));
                
                $entity_count = count($niceness);
                $i = 1;
                //check entities for dimension relations
                foreach ($niceness as $entity_name => $summary_level) {
                        
                        $error = false;
                        
                        //cycle through each dimension frm dim list and those found in constraints.
                        $dims = array_unique(array_merge($this->dimensions, $this->getDimensionsFromConstraints()));
                        
                        owa_coreAPI::debug(sprintf('Dimensions: %s',print_r($this->dimensions, true)));
                        
                        owa_coreAPI::debug(sprintf('Checking the following dimensions for relation to %s: %s',$entity_name, print_r($dims, true)));
                        
                        foreach ($dims as $dimension) {
                                
                                $check = $this->isDimensionRelated($dimension, $entity_name);
                                
                                // is the realtionship check fails then move onto the next entity.
                                if (!$check) {
                                        $error = true;
                                        owa_coreAPI::debug("$dimension is not related to $entity_name. Moving on to next entity...");
                                        break;
                                } else {
                                        owa_coreAPI::debug("Dimension: $dimension is related to $entity_name.");
                                }
                        }
                        
                        // is no error then everythig is related and we are good to go.
                        if (!$error) {
                                owa_coreAPI::debug('optimal base entity is: '.$entity_name);
                                $this->baseEntity = owa_coreAPI::entityFactory($entity_name);
                                return $this->baseEntity;
                        }
                        
                        if ($i === $entity_count) {
                                $this->addError('illegal dimension combination: '.$dimension);
                        } else {
                                $i++;
                        }
                }
        }
        
        function getDimensionsFromConstraints() {
                
                $dims = array();
                
                $constraints = $this->getConstraints();
                //print_r($constraints);
                if (!empty($constraints)) {
                                
                        foreach ($constraints as $carray) {
                                
                                $dims[] = $carray['name'];
                        }
                }
                
                return $dims;
        }
                
        function isDimensionRelated($dimension_name, $entity_name) {
                
                $entity = owa_coreAPI::entityFactory($entity_name);
                
                $dimension = $this->lookupDimension($dimension_name, $entity);
                
                if ($dimension['denormalized'] === true) {
                        $this->related_dimensions[$dimension['name']] = $dimension;
                        owa_coreAPI::debug("Dimension: $dimension_name is denormalized into $entity_name");
                        return true;
                } else {
                
                        $fk = $this->getDimensionForeignKey($dimension, $entity);
                        
                        if ($fk) {
                                owa_coreAPI::debug("Dimension: $dimension_name is related to $entity_name");
                                $this->related_dimensions[$dimension['name']] = $dimension;
                                return true;
                        } else {
                                owa_coreAPI::debug("Could not find a foreign key for $dimension_name in $entity_name");
                        }
                }
        }
        
        function getMetricEntities($metric_name) {
                
                //get the class implementations
                $s = owa_coreAPI::serviceSingleton();
                $classes = $s->getMetricClasses($metric_name);
                
                $entities = array();
                
                // cycles through metric classes and get their entity names
                foreach ($classes as $name => $map) {
                        $m = owa_coreAPI::metricFactory($map['class'], $map['params']);
                        
                        // check to see if this is a calculated metric
                                if ($m->isCalculated()) {
                                        
                                        foreach ($m->getChildMetrics() as $cmetric_name) {
                                                $this->addCalculatedMetric($m);
                                                $entities = array_merge($this->getMetricEntities($cmetric_name), $entities);
                                        }
                                        
                                } else {
                                        $this->metricObjectsByEntityMap[$m->getEntityName()][$metric_name] = $m;
                                        $entities[$metric_name][] = $m->getEntityName();        
                                }
                        
                }
                
                return $entities;
        }
        
        function reduceTables($new, $old) {
                
                return array_intersect($new, $old);
        }
        
        function getDimensionForeignKey($dimension, $entity) {
 
                if ($dimension) {
                        //$entity = ;
                        $dim = $dimension;
                        $fk = array();
                        // check for foreign key column by name if dimension specifies one
                        if (array_key_exists('foreign_key_name', $dim) && !empty($dim['foreign_key_name'])) {
                                // get foreign key col by 
                                if ($entity->isForeignKeyColumn($dim['foreign_key_name'])){
                                        $fk = array('col' => $dim['foreign_key_name'], 'entity' => $entity);
                                }
                                
                        } else {
                                // if not check for foreign key by entity name
                            //check to see if the metric's entity has a foreign key to the dimenesion table.
                                $fk = array(); 
                                
                                $fkcol = $entity->getForeignKeyColumn($dim['entity']);
                                owa_coreAPI::debug("Foreign Key check: ". print_r($fkcol, true));
                                if ($fkcol) {
                                        $fk['col'] = $fkcol;
                                        $fk['entity'] = $entity;
                                } 
                        }
 
                        return $fk;
                }
        }
                
        function lookupDimension($name, $entity) {
                
                // check dimensions
                if (array_key_exists($name, $this->related_dimensions)) {
                        //return $this->related_dimensions[$name];
                }
                //print_r($this->metrics[0]);
                // check for denormalized 
                
                $service = owa_coreAPI::serviceSingleton();
                $dim = $service->getDenormalizedDimension($name, $entity->getName());
                
                if ($dim) {
                        //apply table aliasing to dimension column
                        $dim['column'] = $entity->getTableAlias().'.'.$dim['column'];
                } else {
                
                        // check for normalized dim
                        if (array_key_exists($name, $this->related_dimensions)) {
                                $dim = $this->related_dimensions[$name];
                        } else {
                                
                                $dim = $service->getDimension($name);
                                
                                if ($dim) {
                                        $dimEntity = owa_coreAPI::entityFactory($dim['entity']);
                                        // alias needs to use fk name in case there are two joins on the
                                        // same table. This is also used in addRelation method
                                        $alias = $dimEntity->getTableAlias().'_via_'.$dim['foreign_key_name'];
                                        //$dim['column'] = $dimEntity->getTableAlias().'.'.$dim['column'];
                                        $dim['column'] = $alias.'.'.$dim['column'];
                                } else {
                                        $msg = "$name is not a registered dimension.";
                                        owa_coreAPI::debug($msg);
                                        $this->addError($msg);
                                }
                                
                        }
                }
                
                return $dim;
        }
        
        function setLimit($value) {
                
                if (!empty($value)) {
                
                        $this->limit = $value;
                }
        }
        
        function setOrder($value) {
                
                if (!empty($value)) {
                        $this->params['order'] = $value;
                }
        }
        
        function getOrder() {
        
                if (array_key_exists('order', $this->params)) {
                        return $this->params['order'];
                }
        }
        
        function setSort($column, $order) {
                
                //$this->params['orderby'][] = array($this->getColumnName($column), $order);
        }
        
        function setSorts($array) {
                
                if (is_array($array)) {
                        
                        if (!empty($this->params['orderby'])) {
                                $this->params['orderby'] = array_merge($array, $this->params['orderby']);
 
                        } else {
                                $this->params['orderby'] = $array;
                        }
                }
        }
        
        function sortStringToArray($string) {
                
                if ($string) {
                
                        // add string to query params array for use in URLs.
                        $this->query_params['sort'] = $string;
                
                        $sorts = explode(',', $string);
                        
                        $sort_array = array();
                        
                        foreach ($sorts as $sort) {
                                
                                if (strpos($sort, '-')) {
                                        $column = substr($sort, 0, -1);
                                        $order = 'DESC';
                                } else {
                                        $column = $sort;
                                        $order = 'ASC';
                                }
                
                                //$col_name = $this->getColumnName($column);
                                $check = $this->isSortValid($column);
                                
                                if ($check) {
                                                                
                                        $col_name = $column;
                                        
                                        if ($col_name) {
                                                $sort_array[$sort][0] = $col_name;
                                                $sort_array[$sort][1] = $order;
                                                
                                        } else {
                                                $this->addError("$column is not a valid column to sort on");
                                        }       
                                }       
                        }
                        
                        return $sort_array;
                }
        }
        
        function isSortValid($needle) {
                
                $haystack = array_merge($this->metrics, $this->dimensions);
                return in_array($needle, $haystack);
        }
        
        function setPage($value) {
                
                if (!empty($value)) {
                
                        $this->page = $value;
                        
                        if (!empty($this->pagination)) {
                                $this->pagination->setPage($value);
                        }                       
                }
        }
        
        function setOffset($value) {
                
                if (!empty($value)) {
                        $this->params['offset'] = $value;
                }
        }
        
        function setFormat($value) {
                if (!empty($value)) {
                        $this->format;
                        $this->params['result_format'] = $value;
                }
        }
        
        function setPeriod($value) {
                if (!empty($value)) {
                        $this->params['period'] = $value;
                }
        }
        
        function setTimePeriod($period_name = '', $startDate = null, $endDate = null, $startTime = null, $endTime = null) {
                
                $map = false;
                
                if ($startDate && $endDate) {
                        $period_name = 'date_range';
                        $map = array('startDate' => $startDate, 'endDate' => $endDate);
                        $dimension_name = 'date';
                        $format = 'yyyymmdd';
                } elseif ($startTime && $endTime) {
                        $period_name = 'time_range';
                        $map = array('startTime' => $startTime, 'endTime' => $endTime);
                        $dimension_name = 'timestamp';
                        $format = 'timestamp';
                } else {
                        owa_coreAPI::debug('no start/end params passed to owa_metric::setTimePeriod');
                        $dimension_name = 'date';
                        $format = 'yyyymmdd';
                }
        
                // add to query params array for use in URL construction                
                if ($map) {
                        $this->query_params = array_merge($map, $this->query_params);
                } else {
                        $this->query_params['period'] = $period_name;
                }
                
                $p = owa_coreAPI::supportClassFactory('base', 'timePeriod');
                
                $p->set($period_name, $map);
                
                $this->setPeriod($p);
                
                $start = $p->startDate->get($format);
                $end = $p->endDate->get($format);
                
                $this->setConstraint($dimension_name, array('start' => $start, 'end' => $end), 'BETWEEN');
 
                
        }
        
        function setStartDate($date) {
        
                if (!empty($date)) {
                        $this->params['startDate'] = $date;
                }
        }
        
        function setEndDate($date) {
                if (!empty($date)) {
                        $this->params['endDate'] = $date;
                }
        }
                
        function applyMetaDataToResults($results) {
                
                $new_rows = array();
                
                foreach ($results as $row) {
                        
                        $new_rows[] = $this->applyMetaDataToSingleResultRow($row);
                }
                
                return $new_rows;
        }
        
        function applyMetaDataToSingleResultRow($row) {
                
                $new_row = array();
                
                foreach ($row as $k => $v) {
                                
                        if (in_array($k, $this->dimensions)) {
                                $type = 'dimension';
                                $dim = $this->lookupDimension($k, $this->baseEntity);
                                $data_type = $dim['data_type'];
                        } elseif (in_array($k, $this->metrics)){
                                $type = 'metric';
                                $data_type = $this->getMetric($k)->getDataType();
                        }
                        
                        
                        
                        $new_row[$k] = array(
                                'result_type' => $type, 
                                'name'            => $k, 
                                'value'           => $v,
                                'formatted_value' => $this->formatValue($data_type, $v),
                                'label' => $this->getLabel($k), 'data_type' => $data_type);     
                }
                
                return $new_row;
        }
        
        function formatValue($type, $value) {
                
                if (array_key_exists($type, $this->formatters)) {
                        
                        $formatter = $this->formatters[$type];
                        
                        if (!empty($formatter)) {
                                
                                $value = call_user_func($formatter, $value);
                        }
                }
                 
                
                return $value;
        }
        
        function numberFormatter($value) {
                
                return number_format($value);
        }
        
        function formatSeconds($value) {
                
                return date("G:i:s",mktime(0,0,($value)));
        }
        
        function formatPercentage($value) {
                
                return number_format($value * 100, 2).'%';
        }
        
        function formatCurrency($value) {
        
                return owa_lib::formatCurrency( $value, owa_coreAPI::getSetting( 'base', 'currencyLocal' ) );
        }
        
        /**
         * Sets an individual label
         * return the key so that it can be nested
         * @return $key string
         */
        function addLabel($key, $label) {
                
                $this->labels[$key] = $label;
                return $key;
        }
        
        function getLabel($key = '') {
                
                if (array_key_exists($key, $this->labels)) {
                        return $this->labels[$key];
                } else {
                        //owa_coreAPI::debug("No label found for $key.");
                }
                
        }
        
        /**
         * Retrieve the labels of the measures
         *
         */
        function getLabels() {
        
                return $this->labels;
        }
        
        /**
         * Sets an individual label
         * return the key so that it can be nested
         * @return $key string
         */
        function setLabel($label) {
                
                $this->labels[$this->getName()] = $label;
        }
        
        /**
         * Set the labels of the measures
         *
         */
        function setLabels($array) {
        
                $this->labels = $array;
        }
                
        function getPeriod() {
        
                return $this->params['period'];
        }
 
        function getLimit() {
                
                return $this->limit;
        }
        
        /**
         * Adds a dimension to the dimension map
         * 
         * Retrieves dimension info from service layer and checks to see if 
         * dimension is denromalized or if it is a valid relation
         */
        function setDimension($name) {
                
                if ($name) {
                        $this->dimensions[] = $name;
                }
        }
        
        function setDimensions($array) {
                
                if ($array) {
                
                        foreach($array as $name) {
                                
                                $this->setDimension($name);
                        }       
                }
        }
        
        function dimensionsStringToArray($string) {
                
                // add string to query params array for use in URLs.
                $this->query_params['dimensions'] = $string;
                return explode(',', $string);
        }
        
        function metricsStringToArray($string) {
                
                // add string to query params array for use in URLs.
                $this->query_params['metrics'] = $string;
                return explode(',', $string);
        }
 
        
        function dimensionsArrayToString($array) {
                
                return implode(',', $array);
        }
        
        /**
         * Applies dimensional sql to dao object
         */
        function applyDimensions() {
                
                foreach ($this->dimensions as $dimension_name) {
                        $dim = $this->lookupDimension($dimension_name, $this->baseEntity);
                        // add column name to select statement
                        $this->db->selectColumn($dim['column'], $dim['name']);
                        // add groupby
                        $this->db->groupBy($dim['column']);
                        $this->addLabel($dim['name'], $dim['label']);
                }
        }
        
        function applyJoins() {
                
                foreach($this->related_dimensions as $dim) {
                        $this->addRelation($dim);
                }               
        }
                
        function addRelation($dim) {
                        
                        // if denomalized, skip
                        if ($dim['denormalized'] === true) {
                                return;
                        }
                        
                        // have already determined base enttiy at this point so use that.
                        $fk = $this->getDimensionForeignKey($dim, $this->baseEntity);
                        //print_r($fk);
                        //print $fk;
                        if ($fk) {
                                
                                // create dimension entity
                                $dimEntity = owa_coreAPI::entityFactory($dim['entity']);
                                // get foreign key column
                                //$bm = $this->getBaseMetric();
                                //$fpk_col = $bm->entity->getProperty($fk);
                                $fpk_col = $fk['entity']->getProperty($fk['col']);
                                //$fpk_col = $this->baseEntity->getProperty($fk['col']);
 
                                //print_r($fk['col']);
                                $fpk = $fpk_col->getForeignKey();
                                // add join
                                //print_r($fpk);
                                // needed to make joins unique in cases where there are 
                                // two joins onthe same table using different foreign keys.
                                $alias = $dimEntity->getTableAlias().'_via_'.$dim['foreign_key_name'];  
                                //$this->db->join(OWA_SQL_JOIN, $dimEntity->getTableName(), $dimEntity->getTableAlias(), $fk['entity']->getTableAlias().'.'.$fk['col'], $dimEntity->getTableAlias().'.'.$fpk[1]);
                                $this->db->join(OWA_SQL_JOIN, $dimEntity->getTableName(), $alias, $fk['entity']->getTableAlias().'.'.$fk['col'], $alias.'.'.$fpk[1]);
                                
                                //$this->addColumn($dim['name'], $dimEntity->getTableAlias().'.'.$dim['column']);
                                $this->addColumn($dim['name'], $alias.'.'.$dim['column']);
 
                        } else {
                                // add error result set
                                owa_coreAPI::debug(sprintf('%s metric does not have relation to dimension %s', $fk['entity']->getName(), $dim['name'])); 
                        }
                
        }
        
        // remove
        function addMetric($metric_name, $child = false) {
                
                $ret = false;
                
                $m = $this->getMetric($metric_name);
                
                if (!$m) {
                        $m = owa_coreAPI::metricFactory($metric_name);
                
                        if ($m) {
                        
                                
                                // necessary if the metric was first added as a child but later added as a parent.
                                if (!$child) {
                                        
                                        if (array_key_exists($metric_name, $this->childMetrics)) {
                                                unset ($this->childMetrics[$metric_name]);
                                        }
                                } else {
                                        // add child metrics to child metric maps
                                        // check to see if it wasn't already added as a non-child metric.
                                        if (!array_key_exists($metric_name, $this->metrics)){
                                                $this->childMetrics[$metric_name] = $metric_name;
                                        }
                                }
                        
                                // check to see if this is a calculated metric
                                if ($m->isCalculated()) {
                                        
                                        return $this->addCalculatedMetric($m);
                                }
                        
                                if ($this->checkForFactTableRelation($m)) {
                                        
                                        $this->metrics[$metric_name] = $m;
                                        $this->metricsByTable[$m->getTableName()] = $metric_name;
                                        $this->addSelect($m->getSelect());
                                        $this->addLabel($m->getName(), $m->getLabel());
                                        
                                        $ret = true;
                                }
                        
                        } else {
                                $this->addError("$metric_name is not a metric.");
                        }
                } else {
                        $ret =  true;
                }
                
                
                
                return $ret;            
        }
        
        function addCalculatedMetric($calc_metric_obj) {
                
                // add label of calculated metric obj
                $this->addLabel($calc_metric_obj->getName(),$calc_metric_obj->getLabel());
                // add to calculated metric map
                $this->calculatedMetrics[$calc_metric_obj->getName()] = $calc_metric_obj; 
                
        }
        
        function getCalculatedMetricByName($name) {
                
                return $this->calculatedMetrics[$name];
        }
        
        function addSelect($select_array) {
        
                $this->params['selects'][] = $select_array; 
        }
        
        function getSelects() {
        
                if (array_key_exists('selects', $this->params)) {
                        return $this->params['selects'];
                }
        }
        
        function applySelects() {
                //print_r($this->metrics);
                foreach($this->metrics as $k => $metric_name) {
                        
                        if (!array_key_exists($metric_name, $this->calculatedMetrics)) {
                        
                                $m = $this->metricObjectsByEntityMap[$this->baseEntity->getName()][$metric_name];
                                                
                                $select = $m->getSelect();
                                //print_r ($select);
                                $this->db->selectColumn($select[0], $select[1]);
                        } else {
                                $m = $this->getCalculatedMetricByName($metric_name);
                        }
                        
                        $this->addLabel($m->getName(), $m->getLabel());
                }
                
                // add selects for calculated metrics
                if (!empty($this->calculatedMetrics)) {
 
                        // loop through calculated metric objects
                        foreach ($this->calculatedMetrics as $cmetric) {
                                //create child metrics
                                foreach( $cmetric->getChildMetrics() as $child_name) {
                                        // check to see if the metric has already been added
                                        if (!in_array($child_name, $this->metrics)) {
                                        
                                                $child = $this->metricObjectsByEntityMap[$this->baseEntity->getName()][$child_name];
                                                $select = $child->getSelect();
                                                //print_r ($select[0]);
                                                $this->db->selectColumn($select[0], $select[1]);
                                                // needed so we can remove this temp metric later
                                                $this->childMetrics[] = $child_name;
                                                owa_coreAPI::debug("Added $child_name to ChildMetrics array");
                                        }
                                }
                        }
                }
        }
        
        function getFormat() {
                
                if (array_key_exists('result_format', $this->params)) {
                        return $this->params['result_format'];
                }
        }
        
        function getColumnName($string) {
                
                //$string = trim($string);
                if (array_key_exists($string, $this->related_dimensions)) {
                        return $this->related_dimensions[$string]['column'];
                }
                
                if (array_key_exists($string, $this->related_metrics)) {
                        return $string;
                }
                
                
                //return $string;
                
        }
        
        /**
         * Sets a metric's column name into the all_columns map
         *
         * this is needed when combining metrics so that sort and
         * constraint column names can be looked up fro ma single map.
         */
        function addColumn($name, $col) {
                
                $this->all_columns[$name] = $col;
        }
        
        function addError($msg) {
                
                $this->errors[] = $msg;
                owa_coreAPI::debug($msg);
        }
        
        /**
         * Generates a result set for the metric
         *
         */
        function getResults() {
                
                // get paginated result set object
                $rs = owa_coreAPI::supportClassFactory('base', 'paginatedResultSet');
                
                $bm = $this->chooseBaseEntity();
                                
                if ($bm) {
                        
                        $bname = $bm->getName();
                
                        owa_coreAPI::debug("Using $bname as base entity for making result set.");
        
                        // set constraints
                        $this->applyJoins();
                        $this->applyConstraints();
                        $this->applySelects();
                
                        $this->db->selectFrom($bm->getTableName(), $bm->getTableAlias());
                        // generate aggregate results
                        $results = $this->db->getOneRow();
                        // merge into result set
                        if ($results) {
                                $rs->aggregates = array_merge($this->applyMetaDataToSingleResultRow($results), $rs->aggregates);
                        }
                        
                        // setup dimensional query
                        if (!empty($this->dimensions)) {
                                $this->applyJoins();
                                // apply dimensional SQL
                                $this->applyDimensions();
                                
                                $this->applySelects();
                        
                                $this->db->selectFrom($bm->getTableName(), $bm->getTableAlias());
                                
                                // pass limit to db object if one exists
                                if (!empty($this->limit)) {
                                        $rs->setLimit($this->limit);
                                }
                                // pass limit to db object if one exists
                                if (!empty($this->page)) {
                                        $rs->setPage($this->page);
                                }
                                
                                $this->applyConstraints();
                                
                                if (array_key_exists('orderby', $this->params)) {
                                        $sorts = $this->params['orderby'];
                                        // apply sort by
                                        if ($sorts) {
                                                foreach ($sorts as $sort) {
                                                        $this->db->orderBy($sort[0], $sort[1]);
                                                        $rs->sortColumn = $sort[0];
                                                        if (isset($sort[1])){
                                                                $rs->sortOrder = strtolower($sort[1]);
                                                        } else {
                                                                $rs->sortOrder = 'asc';
                                                        }
                                                }
                                        }
                                }                               
                                
                                // add labels
                                $rs->setLabels($this->getLabels());     
                        
                                // generate dimensonal results
                                $results = $rs->generate($this->db);
                                
                                $rs->resultsRows = $this->applyMetaDataToResults($results);
                        }
                        
                        // add labels
                        $rs->setLabels($this->getLabels());
                        
                        // add period info
                        
                        $rs->setPeriodInfo($this->params['period']->getAllInfo());
                        
                        $rs = $this->computeCalculatedMetrics($rs);
                        
                        // add urls
                        $urls = $this->makeResultSetUrls();
                        $rs->self = $urls['self'];
                        
                        if ($rs->more) {
                        
                                $rs->next = $urls['next'];
                        }
                        
                        if ($this->page >=2) {
                                $rs->previous = $urls['previous'];
                        }
                        
                        $rs->createResultSetHash();
                }
                
                $rs->errors = $this->errors;
                                
                return $rs;
        }
        
        function computeCalculatedMetrics($rs) {
                
                foreach ($this->calculatedMetrics as $cm) {
                        
                        // add aggregate metric
                        $formula = $cm->getFormula();
                        $div_by_zero = false;
                        
                        foreach ($cm->getChildMetrics() as $metric_name) {
                                
                                $ag_value = $rs->getAggregateMetric($metric_name);
                                
                                if (empty($ag_value) || $ag_value == 0) {
                                        $ag_value = 0;
                                        $div_by_zero = true;
                                }
                                
                                $formula = str_replace($metric_name, $ag_value, $formula);
                        }
                        
                        if ( ! $div_by_zero ) {
                                $value = $this->evalFormula($formula);
                        } else {
                                $value = 0;
                        }
                        
                        $rs->setAggregateMetric($cm->getName(), $value, $cm->getLabel(), $cm->getDataType(), $this->formatValue($cm->getDataType(), $value));
                        
                        // add dimensional metric
                        
                        if ($rs->getRowCount() > 0) {
                                
                                foreach ($rs->resultsRows as $k => $row) {
                                        
                                        // add aggregate metric
                                        $formula = $cm->getFormula();
                                        $row_div_by_zero = false;
                                        foreach ($cm->getChildMetrics() as $metric_name) {
                                                
                                                if (array_key_exists($metric_name, $row)) {
                                                        $row_value = $row[$metric_name]['value'];
                                                } else {
                                                        $row_value = '';
                                                }
                                                if (empty($row_value) || $row_value == 0) {
                                                        $row_value = 0;
                                                        $row_div_by_zero = true;
                                                }
                                        
                                                $formula = str_replace($metric_name, $row_value, $formula);     
                                        
                                        }
                                        
                                        if ( ! $row_div_by_zero ) {
                                                $value = $this->evalFormula($formula);
                                        } else {
                                                $value = 0;
                                        }
                                        
                                        $rs->appendRow($k, 'metric', $cm->getName(), $value, $cm->getLabel(), $cm->getDataType(), $this->formatValue($cm->getDataType(), $value));
                                }
                        }
                        
                        foreach ($this->childMetrics as $metric_name) {
                                
                                $rs->removeMetric($metric_name);
                        }
                        
                }
                
                return $rs;
        }
        
        function evalFormula($formula) {
                
                //safety first. should only be computing numbers.
                        $formula = str_replace('$','', $formula);
                        
                        // need parens and @ to handle divsion by zero errors
                        $formula = '$value = ('.$formula.');';
                        //print $formula;
                        // calc
                        @ eval($formula);
                        
                        if (!$value) {
                                $value = 0;
                        }
                        
                        return $value;
        }
        
        function getMetric($name) {
                
                if (in_array($name, $this->metrics)) {
                        return $this->metricObjectsByEntityMap[$this->baseEntity->getName()][$name];
                } 
        }
        
        function setQueryStringParam($name, $string) {
                
                        $this->query_params[$name] = $string;
        }
        
        function makeResultSetUrls() {
                
                $urls = array();
                // get api url
                $api_url = owa_coreAPI::getSetting('base', 'api_url');
                // get base query params
                $query_params = $this->query_params;
                // add api command
                $query_params['do'] = 'getResultSet';
                //add format
                if ($this->format) {
                        $query_params['format'] = $this->format;
                } else {
                        $query_params['format'] = 'json';
                }
                // add current page if any
                if ($this->page) {
                        $query_params['page'] = $this->page;
                }
                // add limit
                if ($this->limit) {
                        $query_params['resultsPerPage'] = $this->limit;
                }
                
                // build url for this result set
                $link_template = owa_coreAPI::getSetting('base', 'link_template');
                $q = $this->buildQueryString($query_params);
                $urls['self'] = sprintf($link_template, $api_url, $q);
                
                // build url for next page of result set
                $next_query_params = $query_params;
                if ($this->page) {
                        $next_query_params['page'] = $query_params['page'] + 1;
                } else {
                        $next_query_params['page'] = 2;
                } 
                
                $nq = $this->buildQueryString($next_query_params);
                $urls['next'] = sprintf($link_template, $api_url, $nq);
                
                // build previous url if page is greater than 2 
                if ($this->page >= 2) {
                        $previous_query_params = $query_params;
                        $previous_query_params['page'] = $query_params['page'] - 1;
                        $pq = $this->buildQueryString($previous_query_params);
                        $urls['previous'] = sprintf($link_template, $api_url, $pq);
                }
                
                $base_query_params = $this->query_params;
                $base_query_params['format'] = $this->format;
                
                // build pagination url template for use in constructing 
                $q = $this->buildQueryString($base_query_params);
                $url['base_url'] = sprintf($link_template, $api_url, $q);
                
                return $urls;
        }
        
        function buildQueryString($params, $seperator = '&') {
                
                $new = array();
                //get namespace
                $ns = owa_coreAPI::getSetting('base', 'ns');
                foreach ($params as $k => $v) {
                        
                        $new[$ns.$k] = $v;
                }
                
                return http_build_query($new,'', $seperator);
        }
 
}
 
?>