Location: PHPKode > projects > O - OpenSource GroupWare > osgw/osgw/classes/class.layout.php
<?php
/**************************************************************************
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   @Authors:	Ryan Thompson(hide@address.com)
***************************************************************************/

/*
	@description	This class handles are data relating to the user interface.
			

*/
class layout extends html
{

	var $sitename;		//Set in osgw/osgw.inc.php
	var $sitelogo;		//Set in osgw/osgw.inc.php
	var $user_theme = 'default';
	var $user_scheme = 'default';

	var $theme_dir;
	var $theme_url;
	var $scheme_url;
	
	var $service_theme;
	var $service_scheme;
	
	var $page_header;
	var $default_buttons = array(1=>NULL, 2=>NULL, 3=>NULL);
	
	var $image_ext = '.png';	//Changes to gif is Internet Explorer is used
	
/*
	var $colours;
	var $layout_dir;
	var $service_layout;
	
	
	var $footer;

*/
	/*!
		@function initialize_layout()
		@author Ryan Thompson
		@abstract Handles the creation of the header for all pages
		@version 0.1
		@params $header - Whether to add the page header.
		@return $page_header
		@since 02-12-2003
	*/
	function initialize_layout($header = FALSE, $user_id, $service)
	{
		
		$this->buttons();
		
		//If user is using IE we need to use the lower quality GIF images as IE doesn't handle PNG transperancy
		//PNG images are much better quality though.
		if(strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE'))
		{
			$this->image_ext = '.gif';
		} else {
			$this->image_ext = '.png';
		}
		
		$this->user_theme = $this->set_theme($user_id, $service);
		$this->user_scheme = $this->set_scheme($user_id, $service);
			
		$this->page_header = $this->create_title();
		$theme_data = $this->get_theme_data($this->user_theme);

		$this->set_default_buttons($theme_data);
		$navigation_bar = $this->create_navigation($user_id, $theme_data);
		
		//Only create the header if the page is NOT a global page. 
		//Login, forgot password, etc do not want a header. 
		if($header)
		{
			$this->page_header .= $this->create_header($user_id, $navigation_bar);
		}		
		
		
	}
	
	/*!
		@function buttons()
		@author Ryan Thompson
		@abstract Set default_buttons array
		@version 0.1
		@since 05-12-2003
	*/
	function buttons()
	{
		GLOBAL $O;
		$this->default_buttons[1]['title'] = 'home';
		$this->default_buttons[2]['title'] = 'help';
		$this->default_buttons[3]['title'] = 'logout';
		$this->default_buttons[1]['link'] = '/';
		$this->default_buttons[2]['link'] = "javascript:openpopup('http://". $O->url ."/help/index.php?app=". $O->current_service."')";
		$this->default_buttons[3]['link'] = "/logout.php";
		
		return;
	}
	/*!
		@function set_theme()
		@author Ryan Thompson
		@abstract Sets information relating to theme
		@version 0.1
		@params $user_id
		@return $theme
		@since 02-12-2003
	*/
	function set_theme($user_id, $service)
	{
		GLOBAL $db, $security, $O;
		
		$sql = "SELECT o_preferences.value, o_themes.directory FROM o_themes LEFT JOIN o_preferences ON 
			o_preferences.value=o_themes.theme_id WHERE o_preferences.service='gl' AND o_preferences.user_id='$user_id' 
			AND o_preferences.preference='theme'";
		
		$db->query($sql);
		$db->fetch_results();
		//If there is an error getting users theme we'll try the default
		//Also used for pages where user isn't logged in.
		
		if($db->num_rows == 0)
		{
		
			$db->record = $this->set_theme(1, $service);
		} else {
			$theme = $db->record['directory'];
		
			$this->theme_dir = "{$O->dir}/osgw/themes/{$theme}";	//Need this to get html templates relating to general page layout
			$this->service_theme = $this->set_service_location($service) ."/themes/{$theme}";	//Need this to retrieve data specific to the service
			$this->theme_url = $security->protocol() . "{$O->url}/osgw/themes/{$theme}";	//May not be needed
		}
		
		return $theme;
	}
	
	/*!
		@function get_scheme()
		@author Ryan Thompson
		@abstract Gets user scheme or Default if no user_id specified
		@version 0.1
		@params $user_id
		@return $scheme
		@since 02-12-2003
	*/	
	function set_scheme($user_id, $service)
	{
		GLOBAL $O, $db, $security;
		//We want to get the file name of the scheme not the human readable name or the identifier
		
		$sql = "SELECT o_preferences.value, o_schemes.file FROM o_schemes LEFT JOIN o_preferences ON 
			o_preferences.value=o_schemes.scheme_id WHERE o_preferences.service='gl' AND o_preferences.user_id='$user_id' 
			AND o_preferences.preference='scheme'";
		
		$db->query($sql);
		$db->fetch_results();
		if($db->num_rows == 0)
		{
			$scheme = 'default';
		} else {
			$scheme = $db->record['file'];
		}
		$this->scheme_url = $security->protocol() ."{$O->url}/osgw/themes/colours/$scheme.css";
		$this->service_scheme = $this->set_service_location($service, 'http') ."/themes/colours/$scheme.css";
	}
	
	/*!
		@function set_service_location()
		@author Ryan Thompson
		@abstract Set the location of the current Service (URL)
		@version 0.1
		@return $service_location
		@since 02-12-2003
	*/	
	function set_service_location($service, $fs = 'abs')
	{
		GLOBAL $O, $db, $security;
		
		$sql = "SELECT location FROM o_services WHERE code='$service'";
		$db->query($sql);
		$db->fetch_results();
		
		if($fs == 'abs')
		{
			$service_dir = "{$O->dir}";
		} else {
			$service_dir = $security->protocol() .$O->url;
		}
		//If there is no location then we use the generic information in osgw/themes
		//This is generally for home
		if($db->num_rows > 0)
		{
			$service_dir .= "/{$db->record['location']}";
		} else {
			$service_dir .= "/osgw";
		}
		return $service_dir;
		
	}		
	
	/*!
		@function create_title()
		@author Ryan Thompson
		@abstract Set the generic title - Every page has this.
		@version 0.1
		@return $page_title
		@since 02-12-2003
	*/	
	function create_title()
	{
		GLOBAL $O;
		$stylesheet = $this->stylesheets();
		$title = $this->get_file_data($this->theme_dir ."/title.thm");
		$header_title = str_replace('{STYLESHEETS}', $stylesheet, $title);
		$header_title = str_replace('{SITENAME}', $this->sitename, $header_title);
		$header_title = str_replace('{SERVICE}', $O->current_service, $header_title);
		$header_title = str_replace('{SITE_URL}', "http://".$O->url, $header_title);
		
		return $header_title;	
	}
	
	/*!
		@function get_scheme()
		@author Ryan Thompson
		@abstract Gets user scheme or Default if no user_id specified
		@version 0.1
		@params $user_id
		@return $scheme
		@since 02-12-2003
	*/
	function create_header($user_id, $navigation_bar)
	{
		GLOBAL $O, $user, $db, $date;
		$header_file = $this->get_file_data($this->theme_dir ."/header.thm");
		
		$image = $O->create_image("/osgw/graphics/{$this->sitelogo}", $this->sitename, 'sitelogo', 'left');
		
		$header = str_replace('{IMAGE}', $image, $header_file);

		$header = str_replace('{DATE}', date($date->header_date($user_id)), $header);
		$name = $user->name['last'] .", ". $user->name['first'];
		$header = str_replace('{USER_NAME}', $name, $header);
		
		$header = str_replace('{HEADER_BTN1}', $this->default_buttons[1], $header);
		$header = str_replace('{HEADER_BTN2}', $this->default_buttons[2], $header);
		$header = str_replace('{HEADER_BTN3}', $this->default_buttons[3], $header);
		$header = str_replace('{NAVBAR}', $navigation_bar, $header);
		return $header;
		
		
	}
	
	
	/*!
		@function stylesheets()
		@author Ryan Thompson
		@abstract Sets the stylesheet information (Three stylesheets are used)
		@version 0.2
		@return $stylesheets
		@since 02-12-2003
	*/
	function stylesheets()
	{

		GLOBAL $O, $security;
		

		$stylesheets = $this->link_stylesheet($this->theme_url ."/layout.css");
		$stylesheets .= $this->link_stylesheet($this->scheme_url);
		$stylesheets .= $this->link_stylesheet($this->service_scheme);
		
		return $stylesheets;
	}
	
	
	/*!
		@function get_file_data()
		@author Ryan Thompson
		@abstract Retrieves data file to be used as template for page
		@version 0.2
		@return $filename -- absolute path to file
		@since 02-12-2003
	*/
	function get_file_data($filename)
	{

		GLOBAL $O;
		$buffer = NULL;
		
		if(!file_exists($filename))
		{
			return FALSE;
		}
		$fp = @fopen($filename, 'r');
		if($fp)
		{

			while(!feof($fp))
			{
				$buffer .= fgets($fp, 2048);
			}
			fclose($fp);
		} else {
			//This has to be handled better. Shouldn't be viewable in production environment. 
			//Add to error handler.
			echo "ERROR - Can't get file $filename<br />";
			echo __FILE__ .' - '. __LINE__;
		}
		return $buffer;
	}
	
	/*!
		@function set_default_buttons()
		@author Ryan Thompson
		@abstract Sets the default buttons for a page (Home, Help, Logout)
		@version 0.2
		@return $this->default_buttons
		@since 02-12-2003
	*/
	function set_default_buttons($theme_data)
	{
		GLOBAL $O, $lang, $security;
		
			

			$button_size = $this->get_image_size($theme_data, 'buttons');
			 
			foreach($this->default_buttons AS $key => $value)
			{
				if($theme_data['button_text'] == 't')
				{
					$text = $lang->get_msg($value['title'], 'gl');
				}
				
				$image = $O->create_image(
									$this->get_image($button_size, $value['title']), 
									$lang->get_msg($value['title'], 'gl'), 
									'default_button', 
									'middle', 
									FALSE);
				
				$link = $O->create_link($value['link'], $image . $text, 'default_buttons');
				
				$this->default_buttons[$key] = $link;
			}
			
			return $this->default_buttons;
	}
	
	/*!
		@function get_image()
		@author Ryan Thompson
		@abstract Retrieves location or button image
		@version 0.2
		@params	$button_size
		@params $image_name
		@return $url
		@since 05-12-2003
	*/
	function get_image($button_size, $image_name)
	{
		
		$url = $this->theme_url ."/$button_size$image_name.png";

		return $url;	
	}
	
	/*!
		@function get_theme_data()
		@author Ryan Thompson
		@abstract Sets the default buttons for a page (Home, Help, Logout)
		@version 0.1
		@params $this->user_theme;
		@return $db->record
		@since 05-12-2003
	*/
	function get_theme_data($theme)
	{
		GLOBAL $db;
		
		$sql = "SELECT * FROM o_themes WHERE directory='$theme'";
		$db->query($sql);
		$db->fetch_results();
		
		return $db->record;
	}
	
	/*!
		@function get_image_size()
		@author Ryan Thompson
		@abstract Sets the prefix for image files to either 'b_' or NULL 
		@version 0.1
		@params $theme_data;
		@params $type  buttons or navigation
		@return $size
		@since 05-12-2003
	*/
	function get_image_size($theme_data, $type = 'buttons')
	{
		
		
		switch($type)
		{
			case 'buttons':
				$images = $theme_data['button_images'];
			break;
			case 'navigation':
				$images = $theme_data['navbar_images'];
			break;
		}
		
		if($images == 'large')
		{
			$value = 'b_';
		} else {
			$value = NULL;
		}
		return $value;
	}
	
	/*!
		@function create_navigation()
		@author Ryan Thompson
		@abstract Creates the Navigation bar
		@version 0.1
		@params $theme_data
		@return $navbar
		@since 05-12-2003
	*/
	function create_navigation($user_id, $theme_data)
	{
		GLOBAL $O, $security, $lang;
		
		$services = $security->get_accessible_services($user_id);
								
						if($theme_data['navbar_newline'] == 1)
						{
							//We have a start and end to for XHTML compatibility
							$newline_start = "<p>";
							$newline_end = "</p>";
						}
		//Stop if $security->get_accessible_services doesn't return an array 
		//Problem if user has no access to any services
		if(is_array($services))
		{
			foreach($services AS $value)
			{
				$service = NULL;
				$data_file = $O->dir ."/{$value['location']}/data.php";
				if(file_exists($data_file))
				{
					
					include($data_file);
					if($service['display'])
					{
						$service_text = $lang->get_msg('service_name', $service['code']);
						
						$image = "/{$value['location']}/themes/{$this->user_theme}/{$service['image']}";
						
						$image = $O->create_image(
									$image,									
									$service_text, 
									'navbar_link', 
									'middle', 
									FALSE);
					
						if($theme_data['navbar_text'] == 1)
						{
							$text = $service_text;	
						}
						$link = $O->create_link("/{$value['location']}", $image . $text, 'navbar_link');	
						$navigation_links .= $newline_start. $link .$newline_end."\n";

					}
				}
			
			}
		}
		return $navigation_links;
	}
	
	/*!
		@function service_header()
		@author Ryan Thompson
		@abstract Creates service header
		@version 0.1
		@params $service_name
		@params $navigation_link
		@params $sub_header
		@return $service_header
		@since 05-12-2003
	*/
	function service_header($service_name, $navigation_links, $sub_header = NULL)
	{
		$header = file($this->theme_dir."/service_header.thm");
		if(!is_null($sub_header))
		{
			$sub_header = ' - '. $sub_header;
		}
		$header = str_replace('{SERVICE_NAME}', $service_name, $header);
		$header = str_replace('{SUB_HEADER}', $sub_header, $header);
		$header = str_replace('{NAVIGATION}', $navigation_links, $header);
		foreach($header AS $value)
		{
			$service_header .= $value ."\n";
		}
		return $service_header;
	}
	
	/*!
		@function create_footer()
		@author Ryan Thompson
		@abstract Creates page footer
		@version 0.1
		@since 05-12-2003
	*/
	function create_footer()
	{
			
		GLOBAL $O, $db;

		$footer = $this->get_file_data($O->dir ."/osgw/themes/". $this->theme ."/footer.thm");
		$image_url = "/osgw".POWER;
		$alt = OSGW_NAME;
		
		$image = $O->create_image($image_url, $alt, 'footer_img', 'middle');
		$link = "<a href=\"". HOME ."\">$image</a>";
		
		$footer = str_replace('{POWER}', $link, $footer);
		$db->db_close();
		return $footer;
	}

}


?>
Return current item: O - OpenSource GroupWare