root/trunk/owa_template.php

Revision 546, 17.5 kB (checked in by padams, 5 days ago)

moved truncate function from owa_template to owa_lib for better reuse.
fixed broken feeds report and widgets
fixed broken traffic metrics
fixed broken timePeriod diference measures

<
Line 
1<?php
2
3//
4// Open Web Analytics - An Open Source Web Analytics Framework
5//
6// Copyright 2006 Peter Adams. All rights reserved.
7//
8// Licensed under GPL v2.0 http://www.gnu.org/copyleft/gpl.html
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16// $Id$
17//
18
19require_once(OWA_INCLUDE_DIR.'template_class.php');
20require_once(OWA_BASE_DIR.'/owa_lib.php');
21require_once(OWA_BASE_DIR.'/owa_auth.php');
22
23/**
24 * OWA Wrapper for template class
25 *
26 * @author      Peter Adams <peter@openwebanalytics.com>
27 * @copyright   Copyright &copy; 2006 Peter Adams <peter@openwebanalytics.com>
28 * @license     http://www.gnu.org/copyleft/gpl.html GPL v2.0
29 * @category    owa
30 * @package     owa
31 * @version             $Revision$           
32 * @since               owa 1.0.0
33 */
34
35class owa_template extends Template {
36       
37        /**
38         * Configuration
39         *
40         * @var array
41         */
42        var $config;
43       
44        var $theme_template_dir;
45       
46        var $module_local_template_dir;
47       
48        var $module_template_dir;
49       
50        var $e;
51       
52        var $period;
53       
54        /**
55         * Params passed by calling caller
56         *
57         * @var array
58         */
59        var $caller_params;
60       
61        function owa_template($module = null, $caller_params = array()) {
62               
63                $this->caller_params = $caller_params;
64                       
65                $c = &owa_coreAPI::configSingleton();
66                $this->config = $c->fetch('base');
67               
68                $this->e = &owa_coreAPI::errorSingleton();
69               
70                // set template dirs
71                if(!empty($caller_params['module'])):
72                        $this->_setTemplateDir($module);
73                else:
74                        $this->_setTemplateDir('base');
75                endif;
76               
77                $this->time_now = owa_lib::time_now();
78               
79                return;
80        }
81       
82        function _setTemplateDir($module) {
83       
84                // set module template dir
85                $this->module_template_dir = OWA_DIR.'modules'.DIRECTORY_SEPARATOR . $module . DIRECTORY_SEPARATOR.'templates'.DIRECTORY_SEPARATOR;
86               
87                // set module local template override dir
88                $this->module_local_template_dir = $this->module_template_dir.'local'.DIRECTORY_SEPARATOR;
89               
90                // set theme template dir
91                $this->theme_template_dir = OWA_THEMES_DIR.$this->config['theme'].DIRECTORY_SEPARATOR;
92               
93                return;
94        }
95       
96        /**
97     * Set the template file
98     *
99     * @param string $file
100     */
101        function set_template($file = null) {
102       
103                if ($file == null):
104                        $this->e->error('No template file was specified.');
105                        return false;
106                else:
107                        // check module's local modification template Directory
108                        if (file_exists($this->module_local_template_dir.$file)):
109                                $this->file = $this->module_local_template_dir.$file;
110                               
111                        // check theme's template Directory
112                        elseif(file_exists($this->theme_template_dir.$file)):
113                                $this->file = $this->theme_template_dir.$file;
114                               
115                        // check module's template directory
116                        elseif(file_exists($this->module_template_dir.$file)):
117                                $this->file = $this->module_template_dir.$file;
118                       
119                        // throw error
120                        else:
121                                $this->e->err(sprintf('%s was not found in any template directory.', $file));
122                                return false;
123                        endif;
124       
125                return true;
126        endif;
127    }
128       
129        /**
130         * Truncate string
131         *
132         * @param string $str
133         * @param integer $length
134         * @param string $trailing
135         * @return string
136         */
137        function truncate ($str, $length=10, $trailing='...')  {
138         
139      return owa_lib::truncate ($str, $length, $trailing); 
140        }
141       
142        function get_month_label($month) {
143               
144                return owa_lib::get_month_label($month);
145        }
146       
147        /**
148         * Chooses the right icon based on browser type
149         *
150         * @param unknown_type $browser_type
151         * @return unknown
152         */
153        function choose_browser_icon($browser_type) {
154               
155                switch (strtolower($browser_type)) {
156                       
157                        case "ie":
158                                $file = 'msie.png';
159                                $name = 'Microsoft Internet Explorer';
160                                break;
161                        case "internet explorer":
162                                $file = 'msie.png';
163                                $name = 'Microsoft Internet Explorer';
164                                break;
165                        case "firefox":
166                                $file = 'firefox.png';
167                                $name = 'Firefox';
168                                break;
169                        case "safari":
170                                $file = 'safari.png';
171                                $name = 'Safari';
172                                break;
173                        case "opera":
174                                $file = 'opera.png';
175                                $name = 'Opera';
176                                break;
177                        case "netscape":
178                                $file = 'netscape.png';
179                                $name = 'Netscape';
180                                break;
181                        case "mozilla":
182                                $file = 'mozilla.png';
183                                $name = 'Mozilla';
184                                break;
185                        case "konqueror":
186                                $file = 'kon.png';
187                                $name = 'Konqueror';
188                                break;
189                        case "camino":
190                                $file = 'camino.png';
191                                $name = 'Camino';
192                                break; 
193                        case "aol":
194                                $file = 'aol.png';
195                                $name = 'AOL';
196                                break; 
197                        case "default browser":
198                                $file = 'default_browser.png';
199                                $name = 'Unknown Browser';
200                                break; 
201                        default:
202                                $file = 'default_browser.png';
203                       
204                }
205                       
206                return sprintf('<img alt="%s" align="baseline" src="%s">', $name, $this->makeImageLink($file));
207               
208        }
209       
210       
211        function makeLinkQueryString($query_params) {
212               
213                $new_query_params = array();
214               
215                //Load params passed by caller
216                if (!empty($this->caller_params)):
217                        foreach ($this->caller_params as $name => $value) {
218                                if (!empty($value)):
219                                        $new_query_params[$name] = $value;     
220                                endif;
221                        }
222                endif;
223
224                // Load overrides
225                if (!empty($query_params)):
226                        foreach ($query_params as $name => $value) {
227                                if (!empty($value)):
228                                        $new_query_params[$name] = $value;     
229                                endif;
230                        }
231                endif;
232               
233                // Construct GET request
234                if (!empty($new_query_params)):
235                        foreach ($new_query_params as $name => $value) {
236                                if (!empty($value)):
237                                        $get .= $name . "=" . $value . "&";     
238                                endif;
239                        }
240                endif;
241               
242                return $get;
243               
244        }
245       
246        /**
247         * Makes navigation links by checking whether or not the view
248         * that is rendering the template is not the view being refered to in the link.
249         *
250         * @param array navigation array
251         */
252        function makeNavigation($nav, $id = '', $class = '', $li_template = '<LI class="%s"><a href="%s">%s</a></LI>', $li_class = '') {
253               
254                $ul = sprintf('<UL id="%s" class="%s">', $id, $class);
255               
256                if (!empty($nav)):
257               
258                        $navigation = $ul;
259                       
260                        foreach($nav as $k => $v) {
261                                                                               
262                                $navigation .= sprintf($li_template, $li_class, $this->makeLink(array('do' => $v['ref']), true), $v['anchortext']);
263                               
264                        }
265                       
266                        $navigation .= '</UL>';
267                       
268                        return $navigation;
269                else:
270                        return false;
271                endif;
272               
273        }
274       
275               
276        function makeTwoLevelNav($links) {
277       
278                $navigation = '<UL id="report_top_level_nav_ul">';
279
280                foreach($links as $k => $v) {
281               
282                        if (!empty($v['subgroup'])):
283                                $sub_nav = $this->makeNavigation($v['subgroup']);       
284                               
285                                $navigation .= sprintf('<LI class="drawer"><H2 class="nav_header"><a href="%s">%s</a></H2>%s</LI>', 
286                                                                                                $this->makeLink(array('do' => $v['ref']), true), 
287                                                                                                $v['anchortext'], $sub_nav);
288                        else:
289                       
290                                $navigation .= sprintf('<LI class="drawer"><H2 class="nav_header"><a href="%s">%s</a></H2></LI>', 
291                                                                                                $this->makeLink(array('do' => $v['ref']), true), 
292                                                                                                $v['anchortext']);
293                               
294                        endif; 
295                       
296                }
297               
298                $navigation .= '</UL>';
299                       
300                return $navigation;
301       
302        }
303       
304        function daysAgo($time) {
305               
306                $now = mktime(23, 59, 59, $this->time_now['month'], $this->time_now['day'], $this->time_now['year']);
307               
308                $days = round(($now - $time) / (3600*24));
309               
310                switch ($days) {
311                       
312                        case 1:
313                                return $days . " day ago";
314               
315                        default:
316                                return $days . " days ago";
317                }
318               
319        }
320       
321        function getAuthStatus() {
322               
323                $auth = &owa_auth::get_instance();
324               
325                return $auth->auth_status;
326        }
327       
328        function makeWikiLink($page) {
329               
330                return sprintf($this->config['owa_wiki_link_template'], $page);
331        }
332       
333        /**
334         * Returns Namespace value to template
335         *
336         * @return string
337         */
338        function getNs() {
339               
340                return $this->config['ns'];
341        }
342       
343        function graphLink($params, $add_state = false) {
344               
345                return $this->makeLink($params, $add_state, $this->config['action_url']);
346        }
347       
348       
349        /**
350         * Makes Links, adds state to links optionaly.
351         *
352         * @param array $params
353         * @param boolean $add_state
354         * @return string
355         */
356        function makeLink($params = array(), $add_state = false, $url = '', $xml = false) {
357                //print_r($this->get('params'));
358                $all_params = array();
359                //print_r($this->caller_params['link_state']);
360                //Loads link state passed by caller
361                if ($add_state == true):
362                        if (!empty($this->caller_params['link_state'])):
363                                $all_params = array_merge($all_params, $this->caller_params['link_state']);
364                        endif;
365                       
366                        // add in period properties if available
367                        $period = $this->get('timePeriod');
368                       
369                        if (!empty($period)):
370                                $all_params = array_merge($all_params, $period->getPeriodProperties());
371                                //print_r($all_params);
372                        endif;
373                       
374                endif;
375               
376               
377                // Load overrides
378                if (!empty($params)):
379                        $params = array_filter($params);
380                        //print_r($params);
381                        $all_params = array_merge($all_params, $params);
382                endif;
383               
384                //print_r($all_params);
385                //print_r($this->get('timePeriod')->getPeriodProperties());
386               
387                $get = '';
388               
389                if (!empty($all_params)):
390               
391                        $count = count($all_params);
392                       
393                        $i = 0;
394                       
395                        foreach ($all_params as $n => $v) {
396                               
397                                $get .= $this->config['ns'].$n.'='.$v;
398                               
399                                $i++;
400                               
401                                if ($i < $count):
402                                        $get .= "&";
403                                endif;
404                        }
405                endif;
406               
407                if (empty($url)):
408                        $url = $this->config['main_url'];
409                endif;
410               
411                $link = sprintf($this->config['link_template'], $url, $get);
412               
413                if ($xml == true):
414                        $link = $this->escapeForXml($link);
415                endif;
416               
417                return $link;
418               
419        }
420       
421        function escapeForXml($string) {
422       
423                $string = str_replace(array('&', '"', "'", '<', '>' ), array('&amp;' , '&quot;', '&apos;' , '&lt;' , '&gt;'), $string);
424                // removes non-ascii chars
425                $string = owa_lib::escapeNonAsciiChars($string);
426                return $string;
427        }
428       
429        function makeAbsoluteLink($params = array(), $add_state = false, $url = '', $xml = false) {
430               
431                if (empty($url)):
432                        $url = $this->config['main_absolute_url'];
433                endif;
434               
435                return $this->makeLink($params, $add_state, $url, $xml);
436               
437        }
438       
439        function makeImageLink($name, $absolute = false) {
440               
441                if ($absolute == true):
442                        $url = $this->config['images_absolute_url'];
443                else:
444                        $url = $this->config['images_url'];
445                endif;
446               
447                return $url.$name;
448               
449        }
450       
451        function includeTemplate($file) {
452       
453                $this->set_template($file);
454                include($this->file);
455                return;
456       
457        }
458       
459        function setTemplate($file) {
460               
461                $this->set_template($file);
462                return $this->file;
463               
464        }
465        /*
466
467        function ofc($url, $use_swfobject = true, $id, $base = '') {
468       
469                if (empty($width)):
470                        $width = '100%';
471                endif;
472               
473                $base = $this->config['public_url'].'includes/ofc-1.9/';
474                //
475                // I think we may use swfobject for all browsers,
476                // not JUST for IE...
477                //
478                //$ie = strstr(getenv('HTTP_USER_AGENT'), 'MSIE');
479               
480                //
481                // escape the & and stuff:
482                //
483                $url = urlencode($url);
484               
485                //
486                // output buffer
487                //
488                $out = array();
489               
490                //
491                // check for http or https:
492                //
493                if (isset ($_SERVER['HTTPS']))
494                {
495                        if (strtoupper ($_SERVER['HTTPS']) == 'ON')
496                        {
497                                $protocol = 'https';
498                        }
499                        else
500                        {
501                                $protocol = 'http';
502                        }
503                }
504                else
505                {
506                        $protocol = 'http';
507                }
508               
509                //
510                // if there are more than one charts on the
511                // page, give each a different ID
512                //
513                global $open_flash_chart_seqno;
514                $obj_id = 'chart';
515               
516                $div_name = 'ofc_chart_object_'.$id;
517               
518                //$out[] = '<script type="text/javascript" src="'. $base .'js/ofc.js"></script>';
519               
520                if( !isset( $open_flash_chart_seqno ) )
521                {
522                        $open_flash_chart_seqno = 1;
523                        $out[] = '<script type="text/javascript" src="'. $base .'swfobject.js"></script>';
524                }
525                else
526                {
527                        $open_flash_chart_seqno++;
528                        $obj_id .= '_'. $open_flash_chart_seqno;
529                        $div_name .= '_'. $open_flash_chart_seqno;
530                }
531               
532                if( $use_swfobject )
533                {
534                // Using library for auto-enabling Flash object on IE, disabled-Javascript proof 
535                $out[] = '<div id="'. $div_name .'" class="owa_ofcChart"></div>';
536                $out[] = '<script type="text/javascript">';
537                $out[] = 'var so = new SWFObject("'. $base .'actionscript/open-flash-chart.swf", "'. $obj_id .'", "'. $width . '", "' . $height . '", "9", "#FFFFFF");';
538                //$out[] = 'so.addVariable("width", "' . $width . '");';
539                //$out[] = 'so.addVariable("height", "' . $height . '");';
540                $out[] = 'so.addVariable("data", "'. $url . '");';
541                $out[] = 'so.addParam("allowScriptAccess", "sameDomain");';
542                $out[] = 'so.write("'. $div_name .'");';
543                $out[] = '</script>';
544                $out[] = '<noscript>';
545                }
546       
547                $out[] = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="' . $protocol . '://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" ';
548                $out[] = 'width="' . $width . '" height="' . $height . '" id="ie_'. $obj_id .'" align="middle">';
549                $out[] = '<param name="allowScriptAccess" value="sameDomain" />';
550                $out[] = '<param name="movie" value="'. $base .'open-flash-chart.swf?width='. $width .'&height='. $height . '&data='. $url .'" />';
551                $out[] = '<param name="quality" value="high" />';
552                $out[] = '<param name="bgcolor" value="#FFFFFF" />';
553                $out[] = '<embed src="'. $base .'actionscript/open-flash-chart.swf?data=' . $url .'" quality="high" bgcolor="#FFFFFF" width="'. $width .'" height="'. $height .'" name="'. $obj_id .'" align="middle" allowScriptAccess="sameDomain" ';
554                $out[] = 'type="application/x-shockwave-flash" pluginspage="' . $protocol . '://www.macromedia.com/go/getflashplayer" id="'. $obj_id .'"/>';
555                $out[] = '</object>';
556       
557                if ( $use_swfobject ) {
558                $out[] = '</noscript>';
559                }
560               
561                return implode("\n",$out);
562        }
563       
564*/
565       
566        function getWidget($do, $params = array(), $wrapper = true, $add_state = true) {
567               
568                $final_params = array();
569               
570                if (empty($params)):
571                        $params = array();
572                endif;
573               
574                $params['do'] = $do;
575       
576                if ($wrapper === true):
577                        $params['initial_view'] = true;
578                        $params['wrapper'] = true;
579                elseif ($wrapper === 'inpage'):
580                        $params['initial_view'] = true;
581                        $params['wrapper'] = 'inpage';
582                else:
583                        $params['wrapper'] = false;
584                endif;
585
586                // add state params into request params
587                if ($add_state === true):
588                        $final_params = array_merge($final_params, $this->caller_params['link_state']);
589                endif;
590               
591                // apply overides made via the template
592                $final_params = array_merge($final_params, array_filter($params));
593               
594                return owa_coreAPI::performAction($do, $final_params);
595        }
596       
597        function getInpageWidget($do, $params = array()) {
598       
599                return owa_template::getWidget($do, $params, 'inpage');
600       
601        }
602       
603        function getSparkline($metric, $metric_col, $period = '', $height = 25, $width = 250, $map = array(), $add_state = true) {
604       
605                $map['metric'] = $metric;
606                $map['metric_col'] = $metric_col;
607                $map['period'] = $period;
608                $map['height'] = $height;
609                $map['width'] = $width;
610                return owa_template::getWidget('base.widgetSparkline', $map, false, $add_state);
611       
612        }
613               
614        function makeJson($array) {
615               
616                $json = '{';
617               
618                foreach ($array as $k => $v) {
619                       
620                        if (is_object($v)) {
621                                if (method_exists($v, 'toString')) {
622                                        $v = $v->toString();
623                                } else {
624                                        $v = '';
625                                }
626                               
627                        }
628                       
629                        $json .= sprintf('%s: "%s", ', $k, $v);
630                       
631                }
632               
633               
634                $json = substr($json, 0, -2);
635               
636                $json .= '}';
637               
638                return $json;
639       
640        }
641       
642        function headerActions() {
643       
644                return;
645        }
646       
647        function footerActions() {
648       
649                return;
650        }
651       
652        function makePagination($pagination, $map = array(), $add_state = true, $template = '') {
653               
654                $pages = '';
655                //print_r($pagination);
656                if ($pagination['max_page_num'] > 1) {
657       
658                        $pages = '<div class="owa_pagination"><UL>';
659                       
660                        for ($i = 1; $i <= $pagination['max_page_num'];$i++) {
661                               
662                                if ($pagination['page_num'] != $i) {
663                                        $new_map = array();
664                                        $new_map = $map;
665                                        $new_map['page'] = $i;
666                                        $link = sprintf('<LI class="owa_reportPaginationControl"><a href="%s">%s</a></LI>', 
667                                                                                                                $this->makeLink($new_map, $add_state), 
668                                                                                                                $i);
669                               
670                                } else {
671                                       
672                                        $link = sprintf('<LI class="owa_reportPaginationControl">%s</LI>', $i);
673                                }
674                                                                                                               
675                                $pages .= $link;
676                        }
677                       
678                        $pages .= '</UL></div>';
679                                       
680                       
681               
682                }
683               
684                return $pages;
685        }
686       
687        function get($name) {
688       
689                if (array_key_exists($name, $this->vars)) {
690                        return $this->vars[$name];
691                } else {
692                        return false;
693                }
694               
695        }
696