forked from Open-Web-Analytics/Open-Web-Analytics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
owa_view.php
791 lines (600 loc) · 18 KB
/
owa_view.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
<?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_CLASSES_DIR.'owa_template.php');
require_once(OWA_BASE_CLASSES_DIR.'owa_requestContainer.php'); // ??
/**
* Abstract View Class
*
* @author Peter Adams <[email protected]>
* @copyright Copyright © 2006 Peter Adams <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GPL v2.0
* @category owa
* @package owa
* @version $Revision$
* @since owa 1.0.0
*/
class owa_view extends owa_base {
/**
* Main view template object
*
* @var object
*/
var $t;
/**
* Body content template object
*
* @var object
*/
var $body;
/**
* Sub View object
*
* @var object
*/
var $subview;
/**
* Rednered subview
*
* @var string
*/
var $subview_rendered;
/**
* CSS file for main template
*
* @var unknown_type
*/
var $css_file;
/**
* The priviledge level required to access this view
* @depricated
* @var string
*/
var $priviledge_level;
/**
* Type of page
*
* @var unknown_type
*/
var $page_type;
/**
* Request Params
*
* @var unknown_type
*/
var $params;
/**
* Authorization object
*
* @var object
*/
var $auth;
var $module; // set by factory.
var $data;
var $default_subview;
var $is_subview;
var $js = array();
var $css = array();
var $postProcessView = false;
var $renderJsInline;
/**
* Constructor
*
*/
function __construct($params = null) {
parent::__construct($params);
$this->t = new owa_template();
$this->body = new owa_template($this->module);
$this->setTheme();
$this->setCss("base/css/owa.css");
}
/**
* Assembles the view using passed model objects
*
* @param unknown_type $data
* @return unknown
*/
function assembleView($data) {
$this->e->debug('Assembling view: '.get_class($this));
// set view name in template class. used for navigation.
if (array_key_exists('view', $this->data)) {
$this->body->caller_params['view'] = $this->data['view'];
}
if (array_key_exists('params', $this->data)):
$this->body->set('params', $this->data['params']);
endif;
if (array_key_exists('subview', $this->data)):
$this->body->caller_params['subview'] = $this->data['subview'];
endif;
// Assign status msg
if (array_key_exists('status_msg', $this->data)):
$this->t->set('status_msg', $this->data['status_msg']);
endif;
// get status msg from code passed on the query string from a redirect.
if (array_key_exists('status_code', $this->data)):
$this->t->set('status_msg', $this->getMsg($this->data['status_code']));
endif;
// set error msg directly if passed from constructor
if (array_key_exists('error_msg', $this->data)):
$this->t->set('error_msg', $this->data['error_msg']);
endif;
// authentication status
if (array_key_exists('auth_status', $this->data)):
$this->t->set('authStatus', $this->data['auth_status']);
endif;
// get error msg from error code passed on the query string from a redirect.
if (array_key_exists('error_code', $this->data)):
$this->t->set('error_msg', $this->getMsg($this->data['error_code']));
endif;
// load subview
if (!empty($this->data['subview']) || !empty($this->default_subview)):
// Load subview
$this->loadSubView($this->data['subview']);
endif;
// construct main view. This might set some properties of the subview.
if (method_exists($this, 'render')) {
$this->render($this->data);
} else {
// old style
$this->construct($this->data);
}
//array of errors usually used for field validations
if (array_key_exists('validation_errors', $this->data)):
$this->body->set('validation_errors', $this->data['validation_errors']);
endif;
// pagination
if (array_key_exists('pagination', $this->data)):
$this->body->set('pagination', $this->data['pagination']);
endif;
//$this->_setLinkState();
// assemble subview
if (!empty($this->data['subview'])):
// set view name in template. used for navigation.
$this->subview->body->caller_params['view'] = $this->data['subview'];
// Set validation errors
$this->subview->body->set('validation_errors', $this->get('validation_errors'));
// pagination
if (array_key_exists('pagination', $this->data)):
$this->subview->body->set('pagination', $this->data['pagination']);
endif;
if (array_key_exists('params', $this->data)):
$this->subview->body->set('params', $this->data['params']);
$this->subview->body->set('do', $this->data['params']['do']);
endif;
// Load subview
$this->renderSubView($this->data);
// assign subview to body template
$this->body->set('subview', $this->subview_rendered);
endif;
// assign validation errors
if (!empty($this->data['validation_errors'])) {
$ves = new owa_template('base');
$ves->set_template('error_validation_summary.tpl');
$ves->set('validation_errors', $this->data['validation_errors']);
$validation_errors_summary = $ves->fetch();
$this->t->set('error_msg', $validation_errors_summary);
}
// fire post method
$this->post();
// assign css and js ellements if the view is not a subview.
// subview css/js have been merged/pulls from subview and assigned here.
if ($this->is_subview != true) {
if (!empty($this->css)) {
$this->t->set('css', $this->css);
}
if (!empty($this->js)) {
$this->t->set('js', $this->js);
}
}
//Assign body to main template
$this->t->set('config', $this->config);
//Assign body to main template
$this->t->set('body', $this->body);
if ($this->postProcessView === true){
return $this->postProcess();
} else {
// Return fully asembled View
return $this->t->fetch();
}
}
/**
* Abstract Alternative rendering method reuires the setting of $this->postProcessView to fire
*
*/
function postProcess() {
return false;
}
/**
* Post method fired right before view is rendered and returned
* as output
*/
function post() {
return false;
}
/**
* Sets the theme to be used by a view
*
*/
function setTheme() {
$this->t->set_template($this->config['report_wrapper']);
return;
}
/**
* Abstract method for assembling a view
* @depricated
* @param array $data
*/
function construct($data) {
return;
}
/**
* Assembles subview
*
* @param array $data
*/
function loadSubView($subview) {
if (empty($subview)):
if (!empty($this->default_subview)):
$subview = $this->default_subview;
$this->data['subview'] = $this->default_subview;
else:
return $this->e->debug("No Subview was specified by caller.");
endif;
endif;
$this->subview = owa_coreAPI::subViewFactory($subview);
//print_r($subview.'///');
$this->subview->setData($this->data);
}
/**
* Assembles subview
*
* @param array $data
*/
function renderSubView($data) {
// Stores subview as string into $this->subview
$this->subview_rendered = $this->subview->assembleSubView($data);
// pull css and js elements needed by subview
$this->css = array_merge($this->css, $this->subview->css);
$this->js = array_merge($this->js, $this->subview->js);
}
/**
* Assembles the view using passed model objects
*
* @param unknown_type $data
* @return unknown
*/
function assembleSubView($data) {
// construct main view. This might set some properties of the subview.
if (method_exists($this, 'render')) {
$this->render($data);
} else {
// old style
$this->construct($data);
}
$this->t->set_template('wrapper_subview.tpl');
//Assign body to main template
$this->t->set('body', $this->body);
// Return fully asembled View
$page = $this->t->fetch();
return $page;
}
function setCss($path, $version = null, $deps = array(), $ie_only = false) {
if ( ! $version ) {
$version = OWA_VERSION;
}
$uid = $path;
$url = sprintf('%s?version=%s', owa_coreAPI::getSetting('base', 'modules_url').$path, $version);
$this->css[$uid]['url'] = $url;
// build file system path just in case we need to concatenate the JS into a single file.
$fs_path = OWA_MODULES_DIR.$path;
$this->css[$uid]['path'] = $fs_path;
$this->css[$uid]['deps'] = $deps;
$this->css[$uid]['version'] = $version;
$this->css[$uid]['ie_only'] = $ie_only;
}
function setJs($name, $path, $version ='', $deps = array(), $ie_only = false) {
if (empty($version)) {
$version = OWA_VERSION;
}
$uid = $name.$version;
$url = sprintf('%s?version=%s', owa_coreAPI::getSetting('base', 'modules_url').$path, $version);
$this->js[$uid]['url'] = $url;
// build file system path just in case we need to concatenate the JS into a single file.
$fs_path = OWA_MODULES_DIR.$path;
$this->js[$uid]['path'] = $fs_path;
$this->js[$uid]['deps'] = $deps;
$this->js[$uid]['version'] = $version;
$this->js[$uid]['ie_only'] = $ie_only;
}
function concatinateJs() {
$js_libs = '';
foreach ($this->js as $lib) {
$js_libs .= file_get_contents($lib['path']);
$js_libs .= "\n\n";
}
$this->body->set('js_includes', $js_libs);
return;
}
/**
* Sets flag to tell view to render the JS inline as <SCRIPT> blocks
* TODO: not yet implemented
*/
function renderJsInline() {
$this->renderJsInLine = true;
return;
}
/**
* Sets the Priviledge Level required to access this view
*
* @param string $level
*/
function _setPriviledgeLevel($level) {
$this->priviledge_level = $level;
return;
}
/**
* Sets the page type of this view. Used for tracking.
*
* @param string $page_type
*/
function _setPageType($page_type) {
$this->page_type = $page_type;
return;
}
/**
* Sets properties that are needed to maintain state across most
* report and widget requests. This is used by many template functions.
*
*/
function _setLinkState( $p = array() ) {
// array of params to check
if ( ! $p ) {
$p = $this->get('params');
}
// control array - will check for these params. If they exist it will return.
$sp = array(
'period' => null,
'startDate' => null,
'endDate' => null,
'siteId' => null,
'startTime' => null,
'endTime' => null
);
// result array
$link_params = array();
if ( ! empty( $p ) ) {
$link_params = array_intersect_key($p, $sp);
}
// needed for forwards compatability with
if ( array_key_exists('site_id', $link_params ) && ! array_key_exists('siteId', $link_params) ) {
$link_params['siteId'] = $link_params['site_id'];
}
$this->t->caller_params['link_state'] = $link_params;
$this->body->caller_params['link_state'] = $link_params;
if(!empty($this->subview)) {
$this->subview->body->caller_params['link_state'] = $link_params;
}
}
function get($name) {
if (array_key_exists($name, $this->data)) {
return $this->data[$name];
} else {
return false;
}
}
function set($name, $value) {
$this->data[$name] = $value;
}
function setSubViewProperty($name, $value) {
$this->subview->set($name, $value);
}
function getSubViewProperty($name) {
return $this->subview->get($name);
}
function setData($data) {
$this->data = $data;
}
function setTitle($title, $suffix = '') {
$this->t->set('page_title', $title);
$this->t->set('titleSuffix', $suffix);
}
function setContentTypeHeader($type = 'html') {
owa_lib::setContentTypeHeader($type);
}
}
/**
* Generic HTMl Table View
*
* Will produce a generic html table
*
*/
class owa_genericTableView extends owa_view {
function __construct() {
return parent::__construct();
}
function render($data) {
$this->t->set_template('wrapper_blank.tpl');
$this->body->set_template('generic_table.tpl');
if (!empty($data['labels'])):
$this->body->set('labels', $data['labels']);
$this->body->set('col_count', count($data['labels']));
else:
$this->body->set('labels', '');
$this->body->set('col_count', count($data['rows'][0]));
endif;
if (!empty($data['rows'])):
$this->body->set('rows', $data['rows']);
$this->body->set('row_count', count($data['rows']));
else:
$this->body->set('rows', '');
$this->body->set('row_count', 0);
endif;
if (array_key_exists('table_class', $data)):
$this->body->set('table_class', $data['table_class']);
else:
$this->body->set('table_class', 'data');
endif;
if (array_key_exists('header_orientation', $data)):
$this->body->set('header_orientation', $data['header_orientation']);
else:
$this->body->set('header_orientation', 'col');
endif;
if (array_key_exists('table_footer', $data)):
$this->body->set('table_footer', $data['table_footer']);
else:
$this->body->set('table_footer', '');
endif;
if (array_key_exists('table_caption', $data)):
$this->body->set('table_caption', $data['table_caption']);
else:
$this->body->set('table_caption', '');
endif;
if (array_key_exists('is_sortable', $data)) {
if ($data['is_sortable'] != true) {
$this->body->set('sort_table_class', '');
}
} else {
$this->body->set('sort_table_class', 'tablesorter');
}
if (array_key_exists('table_row_template', $data)):
$this->body->set('table_row_template', $data['table_row_template']);
else:
;
endif;
// show the no data error msg
if (array_key_exists('show_error', $data)):
$this->body->set('show_error', $data['show_error']);
else:
$this->body->set('show_error', true);
endif;
$this->body->set('table_id', str_replace('.', '-', $data['params']['do']).'-table');
}
}
/**
* @depricated
*/
class owa_sparklineJsView extends owa_view {
function __construct() {
return parent::__construct();
}
function render($data) {
// load template
$this->t->set_template('wrapper_blank.tpl');
$this->body->set_template('sparklineJs.tpl');
// set
$this->body->set('widget', $data['widget']);
$this->body->set('type', $data['type']);
$this->body->set('height', $data['height']);
$this->body->set('width', $data['width']);
$this->body->set('values', $data['series']['values']);
$this->body->set('dom_id', $data['dom_id'].rand());
//$this->setJs("includes/jquery/jquery.sparkline.js");
return;
}
}
class owa_mailView extends owa_view {
// post office
var $po;
var $postProcessView = true;
function __construct() {
// make this a service
require_once(OWA_BASE_CLASS_DIR.'mailer.php');
$this->po = new owa_mailer;
return parent::__construct();
}
function postProcess() {
$this->po->setHtmlBody( $this->t->fetch() );
if ( $this->get( 'plainTextView' ) ) {
$this->po->setAltBody( owa_coreAPI::displayView( $this->get( 'plain_text_view' ) ) );
}
return $this->po->sendMail();
}
function setMailSubject($sbj) {
$this->po->setSubject( $sbj );
}
function addMailToAddress($email, $name = '') {
if (empty($name)) {
$name = $email;
}
$this->po->addAddress($email, $name);
}
}
class owa_adminView extends owa_view {
var $postProcessView = true;
function __construct() {
return parent::__construct();
}
function post() {
$this->setJs('owa.css');
$this->setJs('owa.admin.css');
}
}
class owa_jsonView extends owa_view {
function __construct() {
if (!class_exists('Services_JSON')) {
require_once(OWA_INCLUDE_DIR.'JSON.php');
}
return parent::__construct();
}
function render() {
// load template
$this->t->set_template('wrapper_blank.tpl');
$this->body->set_template('json.php');
$json = new Services_JSON();
// set
// look for jsonp callback
$callback = $this->get('jsonpCallback');
// if not found look on the request scope.
if ( ! $callback ) {
$callback = owa_coreAPI::getRequestParam('jsonpCallback');
}
if ( $callback ) {
$body = sprintf("%s(%s);", $callback, $json->encode( $this->get( 'json' ) ) );
} else {
$body = $json->encode( $this->get( 'json' ) );
}
$this->body->set('json', $body);
}
}
class owa_jsonResultsView extends owa_view {
function __construct() {
if (!class_exists('Services_JSON')) {
require_once(OWA_INCLUDE_DIR.'JSON.php');
}
return parent::__construct();
}
function render() {
// load template
$this->t->set_template('wrapper_blank.tpl');
$this->body->set_template('json.php');
$json = new Services_JSON();
// set
$this->body->set('json', $json->encode($this->get('data')));
}
}
class owa_adminPageView extends owa_view {
function render() {
// Set Page title
$this->t->set('page_title', $this->get('title'));
// Set Page headline
$this->body->set('title', $this->get('title'));
$this->body->set('titleSuffix', $this->get('titleSuffix'));
$this->body->set_template('genericAdminPage.php');
$this->setJs('owa.reporting', 'base/js/owa.reporting-combined-min.js');
$this->setCss("base/css/owa.reporting-css-combined.css");
}
}
?>