<?php

class links_PageNav
{

	var $total;
	var $perpage;
	var $current;
	var $url;
	var $options;
	var $mydirname;

	var $start_name = 'p';
	var $single_navi_type = 1; // 1:lid, 2:serial number

	/**
	 * Constructor
	 *
	 * @param   int     $numrows    Total number of items
	 * @param   int     $per_page   Number of items per page
	 * @param   array   $extra_arg
	 * @param   array   $options
	 **/
	function __construct($numrows, $per_page, $extra_arg, $options, $mydirname)
	{
		$this->total     = intval($numrows);
		$this->perpage   = intval($per_page);
		$this->current   = (isset($extra_arg['p']) && !empty($extra_arg['p'])) ? intval($extra_arg['p']): 0;
		$this->options   = $options;
		$this->mydirname = $mydirname;

		unset($extra_arg['p']);

		if (isset($extra_arg['start_name'])) { 
			$this->start_name = htmlspecialchars($extra_arg['start_name'], ENT_QUOTES, _CHARSET);
			unset($extra_arg['start_name']);
		}

		$this->_construct_url($extra_arg);
	}

	function _construct_url($extra_arg)
	{
		$uri = XOOPS_URL . '/';

		if ($this->options['url_approach'] < 3) {
			$uri .= 'modules/';
		}
		$uri .= $this->mydirname;
		$uri .= (isset($this->options['admin']) && !empty($this->options['admin'])) ? '/admin': '';

		if ($this->options['url_approach'] > 0) {
			//segment approach
			if ($this->options['url_approach'] == 1) {
				$uri .= '/index.php';
			}

			if ($extra_arg['c'] != 'index') { 
				$uri .= '/' . $extra_arg['c'];
			}
			if ($extra_arg['m'] != 'index') {
				$uri .= '/' . $extra_arg['m'];
			}
			unset($extra_arg['c']);
			unset($extra_arg['m']);

			foreach ($extra_arg as $k => $v) {
				$uri .= '/'. $v;
			}
			$uri .= '/';

		} else {
			//query approach
			$uri .= '/index.php?';
			if ($extra_arg['c'] != 'index') {
				$uri .= 'c='. $extra_arg['c'];
			}
			if ($extra_arg['m'] != 'index') {
				$joint = (substr($uri, -1)=='?') ? '': '&amp;'; 
				$uri .= $joint . 'm=' . $extra_arg['m'];
			}
			unset($extra_arg['c']);
			unset($extra_arg['m']);

			foreach ($extra_arg as $k => $v) {
				$uri .= '&amp;'. $k .'='. $v;
			}

			$uri .= '&amp;'. $this->start_name .'=';
		}

		$this->url = $uri;
	}

	/**
	 * Create text navigation
	 *
	 **/
	function renderNav($offset = 4)
	{
		$ret = '';
		if ( $this->total <= $this->perpage ) {
			return $ret;
		}
		$total_pages = ceil($this->total / $this->perpage);
		if ( $total_pages > 1 ) {
			$url_suffix = ($this->options['url_approach'] > 0 && !empty($this->options['url_suffix'])) ? 
				$this->options['url_suffix']: 
				'';

			$prev = $this->current - $this->perpage;
			if ( $prev >= 0 ) {
				$ret .= '<a href="'.$this->url . $prev . $url_suffix .'" class="navi_prev">&lt;</a> ';//&laquo;
			}
			$counter = 1;
			$current_page = intval(floor(($this->current + $this->perpage) / $this->perpage));
			while ( $counter <= $total_pages ) {
				if ( $counter == $current_page ) {
					$ret .= '<a href="#" class="navi_here">'.$counter.'</a> ';
				} elseif ( ($counter > $current_page-$offset && $counter < $current_page + $offset ) || $counter == 1 || $counter == $total_pages ) {
					if ($counter == $total_pages && $current_page < $total_pages - $offset) {
						$ret .= '... ';
					}
					$ret .= '<a href="'. $this->url . (($counter - 1) * $this->perpage) . $url_suffix;
					$ret .= '" class="navi_page">'.$counter.'</a> ';
					if ($counter == 1 && $current_page > 1 + $offset) {
						$ret .= '... ';
					}
				}
				$counter++;
			}
			$next = $this->current + $this->perpage;
			if ($this->total > $next) {
				$ret .= '<a href="'. $this->url.$next . $url_suffix .'" class="navi_next">&gt;</a> ';//&raquo;
			}
		}
		return $ret;
	}

	function single_renderNav($current_lid, $all_lid_arr, $num = 5)
	{
		// Nave Type
		$single_navi_type = $this->single_navi_type;
		$navi_type_path = realpath(dirname(__FILE__)) . '/';
		if (file_exists($navi_type_path . 'single_page_navi_type.inc.php')) {
			require $navi_type_path . 'single_page_navi_type.inc.php';
		}
		if (!($single_navi_type == 1 || $single_navi_type == 2)) {
			$single_navi_type = $this->single_navi_type;
		}

		$navi_html = "" ;
		$numrows = count( $all_lid_arr ) ;
		$pos = array_search( $current_lid , $all_lid_arr ) ;
		if( $numrows > 1 ) {
			$url_suffix = ($this->options['url_approach'] > 0 && !empty($this->options['url_suffix'])) ? 
				$this->options['url_suffix']: 
				'';
			
			//PREV and HEAD
			if ($all_lid_arr[0] != $current_lid) {
				$navi_html .= "<a href='". $this->url . $all_lid_arr[0] . $url_suffix;
				$navi_html .= "' class='navi_head' title='To the head'>&laquo;</a>&nbsp;&nbsp;";
				$navi_html .= "<a href='". $this->url . $all_lid_arr[$pos-1] . $url_suffix;
				$navi_html .= "' class='navi_prev' title='Prev'>&lt;</a>&nbsp;&nbsp;";
			}

			if( $numrows > $num ) {
				if ($pos > $num / 2) {
					if ($pos > round($numrows - ( $num / 2 ) - 1)) {
						$start = $numrows - $num + 1 ;
					} else {
						$start = round( $pos - ( $num / 2 ) ) + 1 ;
					}
				} else {
					$start = 1 ;
				}
			} else {
				$start = 1 ;
			}

			for ($i = $start; $i < $numrows + 1 && $i < $start + $num; $i++){
				if ($all_lid_arr[$i-1] == $current_lid) {
					if ($single_navi_type == 1) {
						$navi_html .= "<a href='#' class='navi_here'>". $all_lid_arr[$i-1] ."</a>&nbsp;&nbsp;";
					} elseif ($single_navi_type == 2) {
						$navi_html .= "<a href='#' class='navi_here'>". $i ."</a>&nbsp;&nbsp;";
					}
				} else {
					if ($single_navi_type == 1) {
						$navi_html .= "<a href='". $this->url . $all_lid_arr[$i-1] . $url_suffix;
						$navi_html .= "' class='navi_lid'>". $all_lid_arr[$i-1] ."</a>&nbsp;&nbsp;";
					} elseif ($single_navi_type == 2) {
						$navi_html .= "<a href='". $this->url . $all_lid_arr[$i-1] . $url_suffix;
						$navi_html .= "' class='navi_lid'>". $i ."</a>&nbsp;&nbsp;";
					}
				}
			}

			//NEXT and TAIL
			if ($all_lid_arr[$numrows-1] != $current_lid) {
				$navi_html .= "<a href='". $this->url . $all_lid_arr[$pos+1] . $url_suffix;
				$navi_html .= "' class='navi_next' title='Next'>&gt;</a>&nbsp;&nbsp;";
				$navi_html .= "<a href='". $this->url .$all_lid_arr[$numrows-1] . $url_suffix;
				$navi_html .= "' class='navi_tail' title='To the tail'> &raquo;</a>";
			}
		}

		return $navi_html ;
	}


}