From a4850cb3d7886db727b4c98a98a94c51f008526f Mon Sep 17 00:00:00 2001
From: Mike Macgirvin <mike@macgirvin.com>
Date: Sat, 10 Jul 2010 00:45:18 -0700
Subject: [PATCH] added pager

---
 boot.php          | 69 +++++++++++++++++++++++++++++++++++++++++++++--
 mod/directory.php | 20 +++++++++++---
 view/style.css    | 24 ++++++++++++++++-
 3 files changed, 107 insertions(+), 6 deletions(-)

diff --git a/boot.php b/boot.php
index ddc2fa381..6607c737e 100644
--- a/boot.php
+++ b/boot.php
@@ -28,7 +28,7 @@ class App {
 	public  $argv;
 	public  $argc;
 	public  $module;
-
+	public  $pager;
 	private $scheme;
 	private $hostname;
 	private $path;
@@ -38,6 +38,7 @@ class App {
 
 		$this->config = array();
 		$this->page = array();
+		$this->pager= array();
 
 		$this->scheme = ((isset($_SERVER['HTTPS']) 
 				&& ($_SERVER['HTTPS']))	?  'https' : 'http' );
@@ -49,9 +50,9 @@ class App {
 
                 if(substr($_SERVER['QUERY_STRING'],0,2) == "q=")
 			$_SERVER['QUERY_STRING'] = substr($_SERVER['QUERY_STRING'],2);
-//		$this->cmd = trim($_SERVER['QUERY_STRING'],'/');
 		$this->cmd = trim($_GET['q'],'/');
 
+
 		$this->argv = explode('/',$this->cmd);
 		$this->argc = count($this->argv);
 		if((array_key_exists('0',$this->argv)) && strlen($this->argv[0])) {
@@ -60,6 +61,10 @@ class App {
 		else {
 			$this->module = 'home';
 		}
+		$this->pager['page'] = ((x($_GET,'page')) ? $_GET['page'] : 1);
+		$this->pager['itemspage'] = 50;
+		$this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
+		$this->pager['total'] = 0;
 	}
 
 	function get_baseurl($ssl = false) {
@@ -73,6 +78,15 @@ class App {
 		$this->path = ltrim(trim($p),'/');
 	} 
 
+	function set_pager_total($n) {
+		$this->pager['total'] = intval($n);
+	}
+	function set_pager_itemspage($n) {
+		$this->pager['itemspage'] = intval($n);
+		$this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
+
+	} 
+
 	function init_pagehead() {
 		if(file_exists("view/head.tpl"))
 			$s = file_get_contents("view/head.tpl");
@@ -358,3 +372,54 @@ function hex2bin($s) {
 	return(pack("H*",$s));
 }
 
+
+function paginate(&$a) {
+	$o = '';
+	$stripped = ereg_replace("(&page=[0-9]*)","",$_SERVER['QUERY_STRING']);
+	$stripped = str_replace('q=','',$stripped);
+	$stripped = trim($stripped,'/');
+	$url = $a->get_baseurl() . '/' . $stripped;
+
+
+	  if($a->pager['total'] > $a->pager['itemspage']) {
+		$o .= '<div class="pager">';
+    		if($a->pager['page'] != 1)
+			$o .= '<span class="pager_prev">'."<a href=\"$url".'&page='.($a->pager['page'] - 1).'">prev</a></span> ';
+
+		$o .=  "<span class=\"pager_first\"><a href=\"$url"."&page=1\">first</a></span> ";
+
+    		$numpages = $a->pager['total'] / $a->pager['itemspage'];
+
+		$numstart = 1;
+    		$numstop = $numpages;
+
+    		if($numpages > 14) {
+      			$numstart = (($pagenum > 7) ? ($pagenum - 7) : 1);
+      			$numstop = (($pagenum > ($numpages - 7)) ? $numpages : ($numstart + 14));
+    		}
+   
+		for($i = $numstart; $i <= $numstop; $i++){
+      			if($i == $pagenum)
+				$o .= '<span class="pager_current">'.(($i < 10) ? '&nbsp;'.$i : $i);
+			else
+				$o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? '&nbsp;'.$i : $i)."</a>";
+			$o .= '</span> ';
+		}
+
+		if(($a->pager['total'] % $a->pager['itemspage']) != 0) {
+			if($i == $a->pager['page'])
+				$o .= '<span class="pager_current">'.(($i < 10) ? '&nbsp;'.$i : $i);
+			else
+				$o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? '&nbsp;'.$i : $i)."</a>";
+			$o .= '</span> ';
+		}
+
+		$lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages);
+		$o .= "<span class=\"pager_last\"><a href=\"$url"."&page=$lastpage\">last</a></span> ";
+
+    		if(($a->pager['total'] - ($a->pager['itemspage'] * $a->pager['page'])) > 0)
+			$o .= '<span class="pager_next">'."<a href=\"$url"."&page=".($a->pager['page'] + 1).'">next</a></span>';
+		$o .= '</div>'."\r\n";
+	}
+	return $o;
+}
\ No newline at end of file
diff --git a/mod/directory.php b/mod/directory.php
index aec34f910..2dd4b15d9 100644
--- a/mod/directory.php
+++ b/mod/directory.php
@@ -1,8 +1,10 @@
 <?php
-
+function directory_init(&$a) {
+	$a->set_pager_itemspage(60);
+}
 
 function directory_content(&$a) {
-
+dbg(2);
 	$search = ((x($_GET,'search')) ? notags(trim($_GET['search'])) : '');
 
 	$tpl .= file_get_contents('view/directory_header.tpl');
@@ -16,7 +18,17 @@ function directory_content(&$a) {
 		$search = dbesc($search);
 	$sql_extra = ((strlen($search)) ? " AND MATCH (`profile`.`name`, `user`.`nickname`, `locality`,`region`,`country-name`,`gender`,`marital`,`sexual`,`about`,`romance`,`employer`,`school`) AGAINST ('$search' IN BOOLEAN MODE) " : "");
 
-	$r = q("SELECT `profile`.*, `profile`.`uid` AS `profile_uid`, `user`.`nickname`, `user`.`timezone` FROM `profile` LEFT JOIN `user` ON `user`.`uid` = `profile`.`uid` WHERE `is-default` = 1 AND `publish` = 1 $sql_extra ORDER BY `name` ASC");
+
+	$r = q("SELECT COUNT(*) AS `total` FROM `profile` LEFT JOIN `user` ON `user`.`uid` = `profile`.`uid` WHERE `is-default` = 1 AND `publish` = 1 AND `user`.`blocked` = 0 $sql_extra ");
+	if(count($r))
+		$a->set_pager_total($r[0]['total']);
+
+
+
+	$r = q("SELECT `profile`.*, `profile`.`uid` AS `profile_uid`, `user`.`nickname`, `user`.`timezone` FROM `profile` LEFT JOIN `user` ON `user`.`uid` = `profile`.`uid` WHERE `is-default` = 1 AND `publish` = 1 AND `user`.`blocked` = 0 $sql_extra ORDER BY `name` ASC LIMIT %d , %d ",
+		intval($a->pager['start']),
+		intval($a->pager['itemspage'])
+	);
 	if(count($r)) {
 
 		$tpl = file_get_contents('view/directory_item.tpl');
@@ -62,6 +74,8 @@ function directory_content(&$a) {
 
 		}
 		$o .= "<div class=\"directory-end\" ></div>\r\n";
+		$o .= paginate($a);
+
 	}
 	else
 		notice("No entries (some entries may be hidden).");
diff --git a/view/style.css b/view/style.css
index 701a7d0c5..3105c5f77 100644
--- a/view/style.css
+++ b/view/style.css
@@ -499,4 +499,26 @@ input#dfrn-url {
 
 #directory-search-end {
 	clear: both;
-}
\ No newline at end of file
+}
+
+.pager {
+  padding: 10px;
+  text-align: center;
+  font-size: 1.0em;
+}
+
+
+.pager_first,
+.pager_last,
+.pager_prev,
+.pager_next,
+.pager_n {
+  border: 1px solid black;
+  background: #EEE;
+  padding: 4px;
+}
+.pager_current {
+  border: 1px solid black;
+  background: #E33;
+  padding: 4px;
+}