LONTE SHELL EDITION


Dashboard -*- SHOW INFO -*- HASH identify -*- Config -*- Jumping

You Are Here : /var/www/virtual/tvarditsa.org/classes/
Upload File :
Current File : /var/www/virtual/tvarditsa.org/classes/pager.class.php

<?php
/**
 * VSite Framework for PHP
 * 
 * This product includes PHP, freely available from 
 * <{@link http://www.php.net/ http://www.php.net/}>
 * 
 * @package VSite_Framework
 * @subpackage DataBase
 * @copyright Copyright (c) 2003, Valentin Sheiretsky
 * @license http://www.vsite.org/license.txt BSD-style license
 */

if (!defined("_PAGER_CLASS_")) {
/**
 * @ignore
 */
define("_PAGER_CLASS_", 1);

$this_dir = dirname(__FILE__) . DIRECTORY_SEPARATOR;
if (!defined("_ADODB_CLASS_")) {
	include ($this_dir . 'adodb.class.php');
}

/**
 * ADODB data paging
 *
 * <code>
 * include (CLASSPATH.'pager.class.php');
 * 
 * $p = new Pager();
 * $p->set_offset (0);                  // Data Offset
 * $p->set_condition ('field = 1');     // Where Clause
 * $p->set_order ('field ASC');         // Data Order
 * $p->set_records (10);                // Data Limit
 * $p->set_groups (10);                 // Pages Limit
 * $rs = $p->create('table', '*');
 * 
 * if ($rs['total'] > 0)
 *   foreach ($rs['data'] as $field_name => $field_value) {
 *     // data loop
 *   }
 * </code>
 *
 * @version 2.0.11
 * @author Valentin Sheiretsky <valio@vsite.org>
 * @package VSite_Framework
 */
class Pager extends ADODB {

	/**#@+
	 * @access private
	 */
	var $_cfg		= null;
	var $_records	= 10;
	var $_groups	= 10;
	var $_offset	= 0;
	var $_where		= '';
	var $_order		= '';
	var $_info		= NULL;
	/**#@-*/

	/**
	 * Constructor
	 */
	function __construct() {
		parent::__construct();
         $this->Execute('set names cp1251');
	}

	/**
	 * @ignore
	 */
	function Pager () {
		$this->__construct();
	}

	/**
	 * Create Data Pager
	 *
	 * @param  string $table Table Name
	 * @param  string $fields Table fields
	 * @param  bool   $no_count Count Total Rows
	 * @param  bool   $p_slide Use Slide Navigation
	 * @return mixed  Data Array or False
	 */
	function create ($table, $fields, $no_count = false, $p_slide = true) {
		
		/* clear navigation */
		$this->_info = array();
		$this->_info['table'] = $table;
		$this->_info['fields'] = $fields;
		$this->_info['first'] = 0;
		$this->_info['last'] = 0;
		$this->_info['prev'] = -1;
		$this->_info['next'] = -1;
		$this->_info['prev_g'] = -1;
		$this->_info['next_g'] = -1;
 		$this->_info['pages'] = NULL;
		$this->_info['total'] = 0;
		$this->_info['data'] = NULL;
		$this->_info['offset'] = $this->_offset;
		$this->_info['records'] = $this->_records;
		
		if ($this->_records <= 0) $no_count = true;
		
		if (!$no_count) {
			/* count total records */
			$have_group = strpos(strtolower($this->_where), "group");
			if ($have_group === false) {
				$sql = 'SELECT COUNT(*) as total FROM '.$table;
				if ($this->_where != NULL) $sql .= ' WHERE ' . $this->_where;
				if ($this->debug) echo "<h5>COUNT: ".$sql."</h5>";
				if (($rs = $this->Execute($sql)) === false) return $this->_info;
			} else {
				$this->Execute('CREATE TEMPORARY TABLE adodb_pager_tmp (counter SMALLINT)');
				$sql = 'INSERT INTO adodb_pager_tmp SELECT 1 as total FROM '.$table;
				if ($this->_where != NULL) $sql .= ' WHERE ' . $this->_where;
				if ($this->debug) echo "<h5>TEMPSELECT: ".$sql."</h5>";
				$this->Execute($sql);
				$sql = 'SELECT COUNT(*) as total FROM adodb_pager_tmp';
				if ($this->debug) echo "<h5>TEMPCOUNT: ".$sql."</h5>";
				$rs = $this->Execute($sql);
				$this->Execute('DROP TABLE adodb_pager_tmp');
				if (($rs === false) || ($rs->EOF)) return $this->_info;
			}
			
			$this->_info['total'] = $rs->fields['total'];
			$rs->Close();
			if ($this->_info['total'] <= 0) return $this->_info;
		}
		
		/* get data */
		$sql = 'SELECT '.$fields.' FROM '.$table;
		if ($this->_where != '')
			$sql .= ' WHERE ' . $this->_where;
		if ($this->_order != '')
			$sql .= ' ORDER BY ' . $this->_order;
 
		if ($this->debug)
			echo "<h5>GET: ".$sql." LIMIT ".$this->_offset.", ".$this->_records."</h5>";
		
		$rs = $this->SelectLimit($sql, $this->_records, $this->_offset);
		
		if (($rs === false) || ($rs->EOF)) return $this->_info;
		
		$this->_info['data'] = $rs->GetRows();
		$rs->Close();
		
		if ($no_count) {
			$this->_info['total'] = count($this->_info['data']);
			$this->_info['pages'] = array(1 => $this->_offset);
			return $this->_info;
		}
		
		/* create navigation */
		
		// prev
		if ($this->_offset > 0) {
			if ($this->_offset >= $this->_records)
				$this->_info['prev'] = $this->_offset - $this->_records;
			else $this->_info['prev'] = 0;
		}
		
		// next
		$next = $this->_offset + $this->_records;
		if ($next < $this->_info['total'])
			$this->_info['next'] = $this->_offset + $this->_records;
		
		//last
		$this->_info['last'] = floor(
			($this->_info['total']-1) / $this->_records
		) * $this->_records;
		
		// prev groups
		$g_size = $this->_groups * $this->_records;
		$g_offset = $g_size * floor($this->_offset / $g_size);
		$prev_g = $g_offset - $g_size;
		if ($prev_g >= 0) $this->_info['prev_g'] = $prev_g;
		
		// next groups
		$next_g = $g_offset + $g_size;
		if ($next_g < $this->_info['total']) $this->_info['next_g'] = $next_g;
		
		if ($p_slide) {
			// slide - pages
			$p_b = ceil(($this->_groups / 2) - 1) * $this->_records;
			$s_page = $this->_offset - $p_b;
			if ($s_page < 0) $s_page = 0;
			$e_page = $this->_offset + ($g_size - $p_b) - $this->_records;
			if ($e_page > $this->_info['last']) $e_page = $this->_info['last'];
			while ($s_page <= $e_page) {
				$s_page_n = floor($s_page / $this->_records) + 1;
				$this->_info['pages'][$s_page_n] = $s_page;
				$s_page += $this->_records;
 			}
			if (sizeof($this->_info['pages']) <= 0) $this->_info['pages'] = NULL;
		} else {
			// page - pages
			$s_page = $g_offset;
			if ($this->_info['next_g'] > 0) $e_page = $this->_info['next_g'];
			else $e_page = $this->_info['total'];
			while ($s_page < $e_page) {
				$s_page_n = floor($s_page / $this->_records) + 1;
				$this->_info['pages'][$s_page_n] = $s_page;
				$s_page += $this->_records;
			}
			if (sizeof($this->_info['pages']) <= 0) $this->_info['pages'] = NULL;
		}
		
		return $this->_info;
	}

	/**
	 * Insert Data Into Table
	 *
	 * @param  string $table Table Name
	 * @param  array  $data Data Array
	 * @return mixed  Return Insert ID (int) or False
	 */
	function insert ($table, $data) {
		$sql = 'SELECT * FROM `'. $table .'` WHERE 0';
		if ($this->debug) echo "<h5>BLANK-RS: ".$sql."</h5>";
		$rs = $this->Execute($sql);
		$sql = $this->GetInsertSQL($rs, $data, $this->_magic_quotes_gpc);
		if ($this->debug) echo "<h5>INSERT: ".$sql."</h5>";
		if ($sql == '') return false;
		if (($status = $this->Execute($sql)) === false) return false;
		return $this->Insert_ID();
	}

	/**
	 * Update Data Into Table
	 *
	 * @param  string $table Table Name
	 * @param  array  $data Data Array
	 * @param  bool   $insert Insert Enabled by Default
	 * @return mixed  Return Insert ID (int) or Affected Rows (int) or False
	 */
	function update ($table, $data, $insert = true) {
		$sql = 'SELECT * FROM `'. $table .'`';
		if ($this->_where != NULL) $sql .= ' WHERE ' . $this->_where;
		if ($this->debug) echo "<h5>VERIFY: ".$sql."</h5>";
		$rs = $this->Execute($sql);
		if (($rs === false) || ($rs->EOF))
			if ($insert)
				$sql = $this->GetInsertSQL($rs, $data, $this->_magic_quotes_gpc);
			else
				return false;
		else {
			$sql = $this->GetUpdateSQL($rs, $data, false, $this->_magic_quotes_gpc);
			$insert = false;
		}
		
		if ($this->debug)
			if ($insert)
				echo "<h5>INSERT: ".$sql."</h5>";
			else
				echo "<h5>UPDATE: ".$sql."</h5>";
		
		if ($sql == '') return false;
		if (($status = $this->Execute($sql)) === false) return false;
		if ($insert) return $this->Insert_ID();
		return $this->Affected_Rows();
	}

	/**
	 * Delete Rows from Table
	 *
	 * @param  string $table Table Name
	 * @param  bool   $force_truncate Truncate Disabled by Default
	 * @return bool False on error
	 */
	function delete ($table, $force_truncate = false) {
		$sql = 'DELETE FROM `'. $table .'`';
		if ($this->_where != '') {
			$sql .= ' WHERE ' . $this->_where;
			if ($this->debug) echo "<h5>DELETE: ".$sql."</h5>";
		} else {
			if ($force_truncate) {
				$sql = 'TRUNCATE TABLE `'. $table .'`';
				if ($this->debug) echo "<h5>".$sql."</h5>";
			} else {
				if ($this->debug) echo "<h5>ERROR: TRUNCATE - DISABLED</h5>";
				return false;
			}
		}
		if (($status = $this->Execute($sql)) === false) return false;
		return true;
	}

	/**
	 * Set Data Offset
	 *
	 * @param int $offset
	 */
	function set_offset ($offset) {
		$this->_offset = intval($offset);
	}

	/**
	 * Get Data Offset
	 *
	 * @return int
	 */
	function get_offset () {
		return $this->_offset;
	}

	/**
	 * Set Data Condition
	 *
	 * @param string $where
	 */
	function set_condition ($where) {
		$this->_where = trim($where);
	}

	/**
	 * Get Data Condition
	 *
	 * @return string
	 */
	function get_condition () {
		return $this->_where;
	}

	/**
	 * Set Data Order
	 *
	 * @param string $order
	 */
	function set_order ($order) {
		$this->_order = trim($order);
	}

	/**
	 * Get Data Order
	 *
	 * @return string
	 */
	function get_order () {
		return $this->_order;
	}

	/**
	 * Set Number Rows
	 *
	 * @param int $offset
	 */
	function set_records ($records) {
		$this->_records = intval($records);
	}

	/**
	 * Get Number Rows
	 *
	 * @return int
	 */
	function get_records () {
		return $this->_records;
	}

	/**
	 * Set Number Pages
	 *
	 * @param int $groups
	 */
	function set_groups ($groups) {
		$this->_groups = intval($groups);
	}

	/**
	 * Get Number Pages
	 *
	 * @return int
	 */
	function get_groups () {
		return $this->_groups;
	}

	/**
	 * Destructor
	 */
	function __destruct() {
		parent::__destruct();
	}

}

} // END PAGER_CLASS
?>