Add analytics
[bus.git] / busui / owa / owa_template.php
blob:a/busui/owa/owa_template.php -> blob:b/busui/owa/owa_template.php
  <?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$
  //
   
  if (!class_exists('Template')) {
  require_once(OWA_INCLUDE_DIR.'template_class.php');
  }
   
  if (!class_exists('owa_lib')) {
  require_once(OWA_BASE_DIR.'/owa_lib.php');
  }
   
  if (!class_exists('owa_sanitize')) {
  require_once(OWA_BASE_CLASS_DIR.'sanitize.php');
  }
   
  /**
  * OWA Wrapper for template class
  *
  * @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.0.0
  */
   
  class owa_template extends Template {
   
  /**
  * Configuration
  *
  * @var array
  */
  var $config;
   
  var $theme_template_dir;
   
  var $module_local_template_dir;
   
  var $module_template_dir;
   
  var $e;
   
  var $period;
   
  /**
  * Params passed by calling caller
  *
  * @var array
  */
  var $caller_params;
   
  function owa_template($module = null, $caller_params = array()) {
   
  $this->caller_params = $caller_params;
   
  $c = &owa_coreAPI::configSingleton();
  $this->config = $c->fetch('base');
   
  $this->e = &owa_coreAPI::errorSingleton();
   
  // set template dirs
  if(!empty($caller_params['module'])):
  $this->_setTemplateDir($module);
  else:
  $this->_setTemplateDir('base');
  endif;
   
  $this->time_now = owa_lib::time_now();
   
  return;
  }
   
  function _setTemplateDir($module) {
   
  // set module template dir
  $this->module_template_dir = OWA_DIR.'modules'.DIRECTORY_SEPARATOR . $module . DIRECTORY_SEPARATOR.'templates'.DIRECTORY_SEPARATOR;
   
  // set module local template override dir
  $this->module_local_template_dir = $this->module_template_dir.'local'.DIRECTORY_SEPARATOR;
   
  // set theme template dir
  $this->theme_template_dir = OWA_THEMES_DIR.$this->config['theme'].DIRECTORY_SEPARATOR;
   
  return;
  }
   
  function getTemplatePath($module, $file) {
   
  $this->_setTemplateDir($module);
   
  if ($file == null) {
  owa_coreAPI::error('No template file was specified.');
  return false;
  } else {
  // check module's local modification template Directory
  if (file_exists($this->module_local_template_dir.$file)) {
  $fullfile = $this->module_local_template_dir.$file;
   
  // check theme's template Directory
  } elseif(file_exists($this->theme_template_dir.$file)) {
  $fullfile = $this->theme_template_dir.$file;
   
  // check module's template directory
  } elseif(file_exists($this->module_template_dir.$file)) {
  $fullfile = $this->module_template_dir.$file;
   
  // throw error
  } else {
  $this->e->err(sprintf('%s was not found in any template directory.', $file));
  return false;
  }
  return $fullfile;
  }
   
   
   
  }
   
  /**
  * Set the template file
  * @depricated
  * @param string $file
  */
  function set_template($file = null) {
   
  if (!$file):
  owa_coreAPI::error('No template file was specified.');
  return false;
  else:
  // check module's local modification template Directory
  if (file_exists($this->module_local_template_dir.$file)):
  $this->file = $this->module_local_template_dir.$file;
   
  // check theme's template Directory
  elseif(file_exists($this->theme_template_dir.$file)):
  $this->file = $this->theme_template_dir.$file;
   
  // check module's template directory
  elseif(file_exists($this->module_template_dir.$file)):
  $this->file = $this->module_template_dir.$file;
   
  // throw error
  else:
  $this->e->err(sprintf('%s was not found in any template directory.', $file));
  return false;
  endif;
   
  return true;
  endif;
  }
   
  function setTemplateFile($module, $file) {
   
  //choose file
  $filepath = $this->getTemplatePath($module, $file);
  //set template
  if ($filepath) {
  $this->file = $filepath;
  }
  }
   
  /**
  * Truncate string
  *
  * @param string $str
  * @param integer $length
  * @param string $trailing
  * @return string
  */
  function truncate ($str, $length=10, $trailing='...') {
   
  return owa_lib::truncate ($str, $length, $trailing);
  }
   
  function get_month_label($month) {
   
  return owa_lib::get_month_label($month);
  }
   
  /**
  * Chooses the right icon based on browser type
  *
  * @param unknown_type $browser_type
  * @return unknown
  */
  function choose_browser_icon($browser_type) {
   
  switch (strtolower($browser_type)) {
   
  case "ie":
  $file = 'msie.png';
  $name = 'Microsoft Internet Explorer';
  break;
  case "internet explorer":
  $file = 'msie.png';
  $name = 'Microsoft Internet Explorer';
  break;
  case "firefox":
  $file = 'firefox.png';
  $name = 'Firefox';
  break;
  case "safari":
  $file = 'safari.png';
  $name = 'Safari';
  break;
  case "opera":
  $file = 'opera.png';
  $name = 'Opera';
  break;
  case "netscape":
  $file = 'netscape.png';
  $name = 'Netscape';
  break;
  case "mozilla":
  $file = 'mozilla.png';
  $name = 'Mozilla';
  break;
  case "konqueror":
  $file = 'kon.png';
  $name = 'Konqueror';
  break;
  case "camino":
  $file = 'camino.png';
  $name = 'Camino';
  break;
  case "aol":
  $file = 'aol.png';
  $name = 'AOL';
  break;
  case "default browser":
  $file = 'default_browser.png';
  $name = 'Unknown Browser';
  break;
  default:
  $name = 'Unknown Browser';
  $file = 'default_browser.png';
   
  }
   
  return sprintf('<img alt="%s" align="baseline" src="%s">', $name, $this->makeImageLink('base/i/'.$file));
   
  }
   
  function getBrowserIcon($browser_family, $size = '128x128', $module = 'base') {
   
  if ($browser_family) {
  $browser_family = strtolower($browser_family);
  }
   
   
  if (file_exists(OWA_MODULES_DIR.$module.'/i/browsers/'.$size.'/'.$browser_family.'.png')) {
  return $this->makeImageLink('base/i/browsers/'.$size.'/'.$browser_family.'.png');
  } else {
  return $this->makeImageLink('base/i/browsers/'.$size.'/default.png');
  }
  }
   
   
  function makeLinkQueryString($query_params) {
   
  $new_query_params = array();
   
  //Load params passed by caller
  if (!empty($this->caller_params)):
  foreach ($this->caller_params as $name => $value) {
  if (!empty($value)):
  $new_query_params[$name] = $value;
  endif;
  }
  endif;
   
  // Load overrides
  if (!empty($query_params)):
  foreach ($query_params as $name => $value) {
  if (!empty($value)):
  $new_query_params[$name] = $value;
  endif;
  }
  endif;
   
  // Construct GET request
  if (!empty($new_query_params)):
  foreach ($new_query_params as $name => $value) {
  if (!empty($value)):
  $get .= $name . "=" . $value . "&";
  endif;
  }
  endif;
   
  return $get;
   
  }
   
  /**
  * Makes navigation links by checking whether or not the view
  * that is rendering the template is not the view being refered to in the link.
  *
  * @param array navigation array
  */
  function makeNavigation($nav, $id = '', $class = '', $li_template = '<LI class="%s"><a href="%s">%s</a></LI>', $li_class = '') {
   
  $ul = sprintf('<UL id="%s" class="%s">', $id, $class);
   
  if (!empty($nav)):
   
  $navigation = $ul;
   
  foreach($nav as $k => $v) {
   
  $navigation .= sprintf($li_template, $li_class, $this->makeLink(array('do' => $v['ref']), true), $v['anchortext']);
   
  }
   
  $navigation .= '</UL>';
   
  return $navigation;
  else:
  return false;
  endif;
   
  }
   
  function makeTwoLevelNav($links) {
  print_r($links);
  $navigation = '<UL id="report_top_level_nav_ul">';
   
  foreach($links as $k => $v) {
   
  if (!empty($v['subgroup'])):
  $sub_nav = $this->makeNavigation($v['subgroup']);
   
  $navigation .= sprintf('<LI class="drawer"><H2 class="nav_header"><a href="%s">%s</a></H2>%s</LI>',
  $this->makeLink(array('do' => $v['ref']), true),
  $v['anchortext'], $sub_nav);
  else:
   
  $navigation .= sprintf('<LI class="drawer"><H2 class="nav_header"><a href="%s">%s</a></H2></LI>',
  $this->makeLink(array('do' => $v['ref']), true),
  $v['anchortext']);
   
  endif;
   
  }
   
  $navigation .= '</UL>';
   
  return $navigation;
   
  }
   
  function daysAgo($time) {
   
  $now = mktime(23, 59, 59, $this->time_now['month'], $this->time_now['day'], $this->time_now['year']);
   
  $days = round(($now - $time) / (3600*24));
   
  switch ($days) {
   
  case 1:
  return $days . " day ago";
   
  default:
  return $days . " days ago";
  }
   
  }
   
  /**
  * @depricated
  * @todo remove
  */
  function getAuthStatus() {
   
  if (!class_exists('owa_auth')) {
  require_once(OWA_BASE_DIR.'/owa_auth.php');
  }
   
  $auth = &owa_auth::get_instance();
  return $auth->auth_status;
  }