<?
class db_manage
{//DigiHex Systems\\


	function split($query, $split_by=0, $url_start=false, $url_end=false, $get_var=false, $total_rows_query=false, $first_page_base=false, $custom_total_rows=false, $custom_start_offset=0) {
		global $X;

		if(empty($get_var)) $get_var='page';
		$_GET['split_by']=(int)$_GET['split_by'];
		if($_GET['split_by']>0 && $split_by>0) $split_by=$_GET['split_by'];

		if($split_by==0 || !is_numeric($split_by)) { /* ... */ } else {
			$this->GET_VAR=$get_var;
			$page=(int)$_GET[$get_var];
	      if(!is_numeric($page) || $page<1) $page=1;
			if(ADMIN===true) {
				$navi_cut='<span class="dots">...</span>';
			} else {
				$navi_cut='<li class="disabled"><span class="dots">...</span></li>';
			}
			$oscillate=3;

			$go='
				<span class="pagination_custom_page">
				<form action="'.URL.'" method="post" style="display:inline;" onsubmit="if(this.custom_page.value==\'\' || isNaN(this.custom_page.value)) { this.custom_page.focus(); return false;}">
					<span class="pagination_hash">#</span><input type="text" name="custom_page" class="input" style="width:32px; text-align:center" maxlength="4" autocomplete="off" />
					'.(ADMIN===true ? '
						'.$X->FORM->submit_link('<span class="ico w_yes"></span>',false,'button button_gfx_small').'
					':'
						<input type="submit" class="button" value="OK" />
					').'
				</form>
				</span>
			';
		}


		if(empty($custom_total_rows)) {
			if(!empty($total_rows_query)) {
		      $total_rows=$this->fast($total_rows_query);
			} else {
		      $total_rows=$this->num_rows($this->query($query));
			}
		} else {
			$total_rows=$custom_total_rows;
		}

		$pages=@ceil($total_rows/$split_by);


		if($_GET['return_rows_count']==1) {
			$_GET['db_split_initated']=true;
			$_GET['returned_rows_count']=$total_rows;
		}


		if($_GET['page']>$pages && ($pages>1 || !empty($first_page_base))) {
			$_GET['page']=$page=$pages;
			$corrected_page=$page;

			if($corrected_page==1) {
				if(!empty($first_page_base)) {
					$X->URL->go($first_page_base);
				} else {
					$X->URL->go($url_start.$corrected_page.$url_end);
				}
			}
		}


		if($pages<=1) {
			$sql=$X->DB->query($query);
			return array('query'=>$query, 'sql'=>$sql, 'total_rows'=>$total_rows, 'total'=>$total_rows, 'split_by'=>$split_by);
		}

		if($_GET['page']==1 && !empty($first_page_base)) $X->URL->go($first_page_base);


		if(isset($_POST['custom_page'])) {
			$page=(int)$_POST['custom_page'];
			if(empty($page)) $page=1;
			if($page<=1) $page=1;
			if($page>$pages) $page=$pages;

			if(!empty($url_start)) {
				$href=$url_start.$page.$url_end;
			} else {
				$href=$X->URL->get($get_var.'='.$page);
			}
			$X->URL->go($href);
		}

		if($page>1) { //first & prev
			/*
			if(!empty($url_start)) { //first
				$href=$url_start.(1).$url_end;
			} else {
				$href=$X->URL->get($get_var.'='.(1));
			}
			$navi.='<a href="'.$href.'" class="first"></a>';
			*/
			if(!empty($url_start)) { //prev
				if(($page-1)==1 && !empty($first_page_base)) {
					$href=$first_page_base;
				} else {
					$href=$url_start.($page-1).$url_end;
				}
			} else {
				$href=$X->URL->get($get_var.'='.($page-1));
			}
			if(ADMIN===true) {
				$navi.='<a href="'.$href.'" class="prev">&#9664;</a>';
			} else {
				$navi.='<li><a href="'.$href.'" class="prev">&laquo;</a></li>';
			}
		}


		if($pages>13) {
			if($page>=9 && $page<=$pages-8) {
				for($i=1; $i<=1; $i++) $navi.=$this->navi_link($page,$i,$url_start,$url_end,$first_page_base);
				$navi.=$navi_cut;
				for($i=$page-$oscillate; $i<=$page+$oscillate; $i++) $navi.=$this->navi_link($page,$i,$url_start,$url_end,$first_page_base);
				$navi.=$navi_cut;
				for($i=$pages; $i<=$pages; $i++) $navi.=$this->navi_link($page,$i,$url_start,$url_end,$first_page_base);
			} else {
				if($page<=8) {
					for($i=1; $i<=9; $i++) $navi.=$this->navi_link($page,$i,$url_start,$url_end,$first_page_base);
					$navi.=$navi_cut;
					for($i=$pages-2; $i<=$pages; $i++) $navi.=$this->navi_link($page,$i,$url_start,$url_end,$first_page_base);
				} else {
					for($i=1; $i<=3; $i++) $navi.=$this->navi_link($page,$i,$url_start,$url_end,$first_page_base);
					$navi.=$navi_cut;
					for($i=$pages-8; $i<=$pages; $i++) $navi.=$this->navi_link($page,$i,$url_start,$url_end,$first_page_base);
				}
			}
		} else {
			for($i=1; $i<=$pages; $i++) $navi.=$this->navi_link($page,$i,$url_start,$url_end,$first_page_base);
		}


		if($page<$pages) { //next & last
			if(!empty($url_start)) { //next
				$href=$url_start.($page+1).$url_end;
			} else {
				$href=$X->URL->get($get_var.'='.($page+1));
			}
			if(ADMIN===true) {

			} else {
				$navi.='<li><a href="'.$href.'" class="next">&raquo;</a></li>';
			}
			/*
			if(!empty($url_start)) { //last
				$href=$url_start.($pages).$url_end;
			} else {
				$href=$X->URL->get($get_var.'='.($pages));
			}
			$navi.='<a href="'.$href.'" class="last"></a>';
			*/
		}


		if(!empty($navi)) {
			if(ADMIN===true) {
				$navi = '<span class="pagination">'.$navi.'</span>';
			} else {
				$navi = '<ul class="pagination">'.$navi.'</ul>';
			}
		}
		$query = $query.' LIMIT '.((--$page*$split_by)-$custom_start_offset).','.$split_by.' ';
		$sql=$X->DB->query($query);

		return array('query'=>$query, 'sql'=>$sql, 'navi'=>$navi, 'total_rows'=>$total_rows, 'total'=>$total_rows, 'go'=>$go, 'split_by'=>$split_by, 'pages'=>$pages, 'corrected_page'=>$corrected_page);
	}


	function navi_link($page,$i,$url_start,$url_end=false,$first_page_base=false) {
		global $X;

		if($page==$i) { //sel
			if(ADMIN===true) {
				return '<span class="sel">'.$i.'</span>';
			} else {
				return '<li class="active"><span>'.$i.'</span></li>';
			}
		} else {
			$url = (!empty($url_start) ? $url_start.$i.$url_end : $X->URL->get($this->GET_VAR.'='.$i));
			if($i==1 && !empty($first_page_base)) $url=$first_page_base;

			if(ADMIN===true) {
				return '<a href="'.$url.'">'.$i.'</a>';
			} else {
				return '<li><a href="'.$url.'">'.$i.'</a></li>';
			}
		}
	}


	function set_order(&$table, $before=false, $after=false) {
		if(!empty($_GET['order_by'])) $table['order_by']=$_GET['order_by'];
		if(!empty($_GET['order_dir'])) $table['order_dir']=$_GET['order_dir'];
		if(empty($table['order_dir'])) $table['order_dir']='asc';
		if(empty($_GET['order_by'])) $_GET['order_by']=$table['order_by'];
		if(empty($_GET['order_dir'])) $_GET['order_dir']=$table['order_dir'];

		if($_GET['order_dir']!='asc' && $_GET['order_dir']!='desc' && $_GET['order_dir']!="") $_GET['order_dir']=$table['order_dir']='asc';

		return 'ORDER BY '.trim((!empty($before) ? $before.',':'').' '.$table['order_by'].' '.$table['order_dir'].' '.(!empty($after) ? ','.$after:''));
	}



}//DigiHex Systems\\
?>