<?php
/*
BioMa Common.php template file

This file should be located in the include/ directory
*/

/*
NOTICE OF COPYRIGHT

BioMa - Biodiversity Manager (Manager for biodiversity database)
        http://bioma.sourceforge.net

Copyright (C) 2005 BioMa Development Group

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.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details:

          http://www.gnu.org/copyleft/gpl.html
*/

/** 
* Provides various common functions and variables that most classes use
*
* @author Mats Mortensen <matsm@stud.cs.uit.no>
* @copyright Copyright (C) 2006 BioMa Development Group
* @access public
*
*/
class Common{

	/**
	* Variable that holds an instance of the XTemplate class
	* @access private
	* @name $_xtpl
	*/
	protected $_xtpl;
	
	/**
	* Holds an instance of the Language class
	* @access private
	* @name $_lang
	*/
	protected $_lang;
	
	/**
	* Holds the dir string to the current language directory
	* @access private
	* @name $_langDir
	*/
	protected $_langDir;
	
	/**
	* String containing the url to the Bioma service directory
	* @access private
	* @name $_serviceDir
	*/
	protected $_serviceDir;

	/**
	* String containing the path to the template dir for the class
	* @access private
	* @name $_templateDir
	*/
	protected $_templateDir;
	
	/**
	* String holding the an id used as index for storing items for this class in
	* the $_SESSION array (ie. $_SESSION[$this->_sessionID])
	* @access private
	* @name $_sessionID
	*/
	protected $_sessionID;
	
	public function Common($xtpl, $lang, $langdir, $tpldir, $servicedir, $sessionid){
		$this->_xtpl = $xtpl;
		$this->_lang = $lang;
		$this->_langDir = $langdir;
		$this->_templateDir = $tpldir;
		$this->_serviceDir = $servicedir;
		$this->_sessionID = $sessionid;
	}
	
	/**
	* Helper function to save data in a temporary session buffer
	* $_SESSION[$name][$bufferID] is an array of arrays. 
	* $_SESSION[$name][$bufferID][0] = array('key1' => 'data1', 'key2' => 'data2')
	* $_SESSION[$name][$bufferID][1] = array('key1' => 'data3', 'key2' => 'data4')
	* @access protected
	* @author Mats Mortensen <matsm@stud.cs.uit.no>
	* @return void
	*/
	protected function addToSessionBuffer($bufferID, $value, $bufKey, $items){
		$name = $this->_sessionID;
		
		/* If buffer not created => create it */
		if(!isset($_SESSION[$name][$bufferID]) or !$_SESSION[$name][$bufferID])
			$_SESSION[$name][$bufferID] = array();

		/* Scan through array and check that no duplicate names exist */
		foreach($_SESSION[$name][$bufferID] as $buffer){
			/* If duplicate found, just return without adding the field to session buffer */
			if($value == $buffer[$bufKey])
				return;
		}
		
		/* Create new array in dedicated session array to hold the values to be saved */
		$_SESSION[$name][$bufferID][] = array();
		$key = count($_SESSION[$name][$bufferID]) - 1;
		$buffer = &$_SESSION[$name][$bufferID][$key];
		
		/* Add the keys and values from $items array into the newly created array */
		foreach($items as $itemkey => $itemvalue)
			$buffer[$itemkey] = $itemvalue;
			
	}
	
	/**
	* Helper function delete data from a temporary session buffer
	* @access protected
	* @author Mats Mortensen <matsm@stud.cs.uit.no>
	* @return void
	*/
	protected function deleteFromSessionBuffer($removeID, $bufferID){
		$name = $this->_sessionID;
		$newbuf = array();
		
		foreach($_SESSION[$name][$bufferID] as $key => $value){
			if($key != $removeID){
				$newbuf[] = $_SESSION[$name][$bufferID][$key];
			}
		}
		$_SESSION[$name][$bufferID] = $newbuf;
	}
	
	/**
	*  Helper function that turns two regular arrays into an associative array
	*  The two regular arrays must be on the form:
	*  $keys = $arr[0], $values = $arr[1]
	*  $keys = array('key1', 'key2', etc), $values = array('data1', 'data2', etc)
	* @access protected
	* @author Mats Mortensen <matsm@stud.cs.uit.no>
	* @return void
	*/
	protected function makeAssociative($arr){
		$keys = $arr[0];
		$values = $arr[1];
		$result = array();
		for($i=0;$i<count($keys);$i++){
			$result[$keys[$i]] = $values[$i];
		}
		return $result;
	}
}
