Location: PHPKode > projects > EXtensible Production Suite (XPS) > xpsdev/core/controls/database_control.php
<?php



class Database_Control
{
	private $Database;
	private $Message_Control;
	private $Setting_Control;
    private $debug;
    public	$errors = '';
	
 	public function __construct($SC, $MC, $debug=false)
 	{
        $this->debug = $debug;
        if($debug) echo 'Database_Control::__construct<br />';
 		require_once( MODELS . '/database.php');
		$this->Message_Control = $MC;
		$this->Setting_Control = $SC;
		
		
		$this->Database = new Database($this->create_dsn(), 
									   $this->Setting_Control->get_setting('db_username', 'core'),
									   $this->Setting_Control->get_setting('db_password'),
									   $this->Setting_Control->get_setting('db_type', 'core'),
									   $this->Setting_Control->get_setting('db_table_prefix', 'core'));
 		
		$this->errors = $this->Database->ERROR;
	 }

	private function create_dsn()
	{
		$params = array(
						'db_host' => $this->Setting_Control->get_setting('db_host', 'core'),
						'db_type' => $this->Setting_Control->get_setting('db_type', 'core'),
						'db_name' => $this->Setting_Control->get_setting('db_name', 'core')
					  );
		$dsn = '';
	
		switch($params['db_type'])
		{
			case 'mysql':
				$dsn = 'mysql:dbname='.$params['db_name'].';host='.$params['db_host'].';';
				break;
			
			case 'oracle':
				$dsn = 'oci:dbname=//'.$params['db_host'].':'.$this->Setting_Control->get_setting('oracle_port').'/'.$this->Setting_Control->get_setting('oracle_listener');
				break;
				
			default:
				die($this->Message_Control->get_message('database_cannot_connect'));
	 
		}
		return $dsn;
	}
	/**
	* Builds the page number list for navigation
	*
	* @access	public
	* @param	$id The identification value for the SQL statement
	* @param	$params The parameters to be bound to the SQL statement
	* @return	$anchor_string a list of anchor tags
	*/
	public function get_page_numbers($id, $file_location='core', $params=array())
	{

		$count = $this->Database->get_count($file_location, $params);	 	

		$num_pages = ceil(($count / $params['LIMIT'])); 
		$current_page = (($params['OFFSET'] / $params['LIMIT'])+1);

	 	$upper_bound = '';
	 	$lower_bound = '';

		$upper_bound = $current_page + 2;
		$lower_bound = $current_page - 2;		 
 	 
 	 	if($num_pages > 5)
 	 	{
			if($upper_bound > $num_pages)
			{
				$overflow = ($upper_bound - $num_pages);
				$lower_bound -= $overflow;
				$upper_bound = $num_pages;
			}
			elseif($lower_bound < 1)
			{
				$overflow = (1 - $lower_bound);
			 	$upper_bound += $overflow;
				$lower_bound = 1;
			}
		} 
		else
		{
			$lower_bound = 1;
			$upper_bound = $num_pages;
		}		 
		
		
		if(isset($params['js_funct']))
		{
			$js_funct = $params['js_funct'];
		}
		else
		{
			$js_funct = 'launchCoreModule';
		}
		
		if(isset($params['mod_id']))
		{
			$mod_id = $params['mod_id'];
		}
		else
		{
			$mod_id = 'UserAdmin';
		}
		
		if(isset($params['ORDER_BY']))
		{
			$order_by = $params['ORDER_BY'];
		}
		else
		{
			$order_by = 'User_ID';
		}
		
		$theme = $this->Setting_Control->get_setting('current_theme');
		
		$first_icon		= "<IMG title=\"First Page (1)\" style=\"border:none;\" src=\"./themes/".$theme."/images/first.png\" /></acronym>";
		$previous_icon	= "<IMG title=\"Previous Page\" style=\"border:none;\" src=\"./themes/".$theme."/images/previous.png\" /></acronym>";
		$next_icon		= "<IMG title=\"Next Page\" style=\"border:none;\" src=\"./themes/".$theme."/images/next.png\" /></acronym>";
		$last_icon		= "<IMG title=\"Last Page (".$num_pages.")\" style=\"border:none;\" src=\"./themes/".$theme."/images/last.png\" /></acronym>";
						
		$anchor_string = '';
	 	if($num_pages > 1)
	 	{
			if($current_page != 1)
		 	{
		 		$anchor_string .= "<a href=\"javascript:void(0)\" onclick=\"$js_funct('$mod_id','&ORDER_BY=$order_by&OFFSET=0')\">".$first_icon."</a>&nbsp;";
				$anchor_string .= "<a href=\"javascript:void(0)\" onclick=\"$js_funct('$mod_id','&ORDER_BY=$order_by&OFFSET=".(($current_page-2) * $params['LIMIT'])."')\">".$previous_icon."</a>&nbsp;";
		 	}
			else
			{
				$anchor_string .= "<IMG style=\"visibility:hidden; width:16px;\" src=\"./themes/".$theme."/images/transparent.png\" />&nbsp;";
				$anchor_string .= "<IMG style=\"visibility:hidden; width:16px;\" src=\"./themes/".$theme."/images/transparent.png\" />&nbsp;";
			}
		
		 	for($x=$lower_bound; $x<=$upper_bound; $x++)
		 	{
		 		if($x != $current_page)
		 		{
		 		 	$anchor_string .= "<a class=\"page_number\" href=\"javascript:void(0)\" onclick=\"$js_funct('$mod_id','&ORDER_BY=$order_by&OFFSET=".(($x-1) * $params['LIMIT'])."')\">".$x."</a>&nbsp;";
				}
				else
				{
				 	$anchor_string .= '<a class="current_page_number">'. $x .'</a>&nbsp;';
				}
		 	}
			
			
			if($current_page != $num_pages)
			{
				$anchor_string .= "<a href=\"javascript:void(0)\" onclick=\"$js_funct('$mod_id','&ORDER_BY=$order_by&OFFSET=".(($current_page) * $params['LIMIT'])."')\">".$next_icon."</a>&nbsp;";
				$anchor_string .= "<a href=\"javascript:void(0)\" onclick=\"$js_funct('$mod_id','&ORDER_BY=$order_by&OFFSET=".(($num_pages - 1) *$params['LIMIT'])."')\">".$last_icon."</a>&nbsp;";
			}
			else
			{
				$anchor_string .= "<IMG style=\"visibility:hidden; width:16px;\" src=\"./themes/".$theme."/images/transparent.png\" />&nbsp;";
				$anchor_string .= "<IMG style=\"visibility:hidden; width:16px;\" src=\"./themes/".$theme."/images/transparent.png\" />&nbsp;";
			}
		}
	 	return $anchor_string;
	}
	
	/**
	* Creates a unique id based on the current time
	*
	* @access	public
	* @author	Travis Rennemann <hide@address.com>
	* @return	$guid the generated unique id
	*/
	public function get_unique_id()
	{
		$time = mktime();
        $guid = $_SESSION['USER_ID'] . '-' . gmstrftime('%y%m%d-%H%M-%S', $time);

        while( stristr( $this->guid_list, $guid))
		{
            $time++;
            $guid = $_SESSION['USER_ID'] . '-' . gmstrftime('%y%m%d-%H%M-%S', $time);
        }
        $this->guid_list .= $guid;
        return $guid;
	}

	/**
	* Executs an INSERT, UPATE, or DELETE statement in the database
	* @access public
	* @param $statement the PDO prepared statement
	* @author Brian Cook <hide@address.com>
	* @return boolean
	*/
	public function execute_sql($id, $file_location, $params=array())
	{
	 	$bool = $this->Database->execute_sql($id, $file_location, $params);
	 	return $bool;
	}
	
	public function execute_sql_line($line)
	{
	 	return $this->Database->execute_sql_line($line);
	} 	
	
	/**
	* Gets a recordset.
	*
	* @access public
	* @param $id the identification value for the SQL statement.
	* @param $params an associative array of table names for keys with values.
	* @author Brian Cook <hide@address.com>
	* @return recordset the database recordset.
	*/
	public function get_recordset($id, $file_location='core', $params=array())
	{
		$return = $this->Database->get_recordset($id, $file_location, $params);
		return $return;
	}
	
	//might be used for file i/o
	public function get_stream_contents($id, $params=array())
	{
		return $this->Database->fetch_statement($id, $params);
	}
	
	/*************************************
	* Retrieves the x,y coordinates of a widget
	*
	* @access	public
	* @param	$id the id of the widget
	* @author	Brian Cook <hide@address.com>
	* @return	$position the x,y coordinates of the widget
	*************************************/
	public function get_position($id)
	{
		$params = array();
		$params['WIDGET_ID'] = $id;
		return $this->Database->get_position($params);
	}
	
	/*************************************
	* Sets the x,y coordinates of a widget
	* 
	* @access	public
	* @param	$id the id of the widget
	* @param	$x the x coordinate of the widget
	* @param	$y the y coordinate of the widget
	* @return	boolean
	*************************************/
	public function set_position($id, $x, $y)
	{
		$params = array();
		$params['WIDGET_ID'] =  $id;
		$params['POSITION_X'] = $x;
		$params['POSITION_Y'] = $y;

		return $this->Database->set_position($params);
	}
}
?>
Return current item: EXtensible Production Suite (XPS)