Skip to content

Commit

Permalink
added pagination for all tweet filters
Browse files Browse the repository at this point in the history
  • Loading branch information
amwhalen committed Feb 1, 2013
1 parent 0a04ae8 commit af98496
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 23 deletions.
7 changes: 7 additions & 0 deletions .htaccess
Expand Up @@ -5,17 +5,24 @@ RewriteRule ^([0-9]+)/?$ index.php?id=$1 [L]

# Tweets by date
RewriteRule ^archive/([0-9]{4})/([0-9]{2})/([0-9]{2})/?$ index.php?year=$1&month=$2&day=$3 [L]
RewriteRule ^archive/([0-9]{4})/([0-9]{2})/([0-9]{2})/page/([0-9]+)/?$ index.php?year=$1&month=$2&day=$3&page=$4 [L]

RewriteRule ^archive/([0-9]{4})/([0-9]{2})/?$ index.php?year=$1&month=$2 [L]
RewriteRule ^archive/([0-9]{4})/([0-9]{2})/page/([0-9]+)/?$ index.php?year=$1&month=$2&page=$3 [L]

RewriteRule ^archive/([0-9]{4})/?$ index.php?year=$1 [L]
RewriteRule ^archive/([0-9]{4})/page/([0-9]+)/?$ index.php?year=$1&page=$2 [L]

# Tweets by client
RewriteRule ^client/(.*)/$ index.php?client=$1 [L]
RewriteRule ^client/(.*)/page/([0-9]+)/?$ index.php?client=$1&page=$2 [L]

# Paginated Index
RewriteRule ^page/([0-9]+)/?$ index.php?page=$1 [L]

# Favorites
RewriteRule ^favorites/?$ index.php?favorites=1 [L]
RewriteRule ^favorites/page/([0-9]+)/?$ index.php?favorites=1&page=$2 [L]

# Stats
RewriteRule ^stats/?$ index.php?method=stats [L]
27 changes: 19 additions & 8 deletions amt/controller.php
Expand Up @@ -32,6 +32,9 @@ public function index() {
$this->data['maxClients'] = $this->model->getMostPopularClientTotal();
$perPage = 50;

$current_page = (isset($_GET['page'])) ? $_GET['page']: 1;
$offset = ($current_page > 1) ? (($current_page-1) * $perPage) : 0;

// the big switch. this decides what to show on the page.
if (isset($_GET['id'])) {

Expand All @@ -48,7 +51,10 @@ public function index() {
$this->data['pageType'] = 'search';
$this->data['search'] = true;
$this->data['searchTerm'] = $searchTerm;
$this->data['tweets'] = $this->model->getSearchResults($searchTerm);
$this->data['tweets'] = $this->model->getSearchResults($searchTerm, $offset, $perPage);
$this->data['totalTweetsForSearch'] = $this->model->getSearchResults($searchTerm, $offset, $perPage, true);
$pageBaseUrl = $this->data['config']['system']['baseUrl'].'?q='.urlencode($searchTerm);
$this->data['pagination'] = $this->paginator->paginate($pageBaseUrl, $this->data['totalTweetsForSearch'], $current_page, $perPage, false);
$header = 'Search <small>'.$searchTerm.'</small>';
$this->data['header'] = $header;

Expand All @@ -59,7 +65,10 @@ public function index() {
$this->data['monthly_archive'] = true;
$this->data['archive_year'] = $_GET['year'];
$this->data['archive_month'] = $_GET['month'];
$this->data['tweets'] = $this->model->getTweetsByMonth($this->data['archive_year'], $this->data['archive_month'], 0, 1000);
$this->data['tweets'] = $this->model->getTweetsByMonth($this->data['archive_year'], $this->data['archive_month'], $offset, $perPage);
$this->data['totalTweetsByMonth'] = $this->model->getTweetsByMonthCount($this->data['archive_year'], $this->data['archive_month']);
$pageBaseUrl = $this->data['config']['system']['baseUrl'].'archive/'.urlencode($this->data['archive_year']).'/'.urlencode($this->data['archive_month']).'/';
$this->data['pagination'] = $this->paginator->paginate($pageBaseUrl, $this->data['totalTweetsByMonth'], $current_page, $perPage);
$this->data['header'] = date('F Y', strtotime($this->data['archive_year'].'-'.$this->data['archive_month'].'-01'));

} else if (isset($_GET['client'])) {
Expand All @@ -68,7 +77,10 @@ public function index() {
$this->data['pageType'] = 'client_archive';
$this->data['per_client_archive'] = true;
$this->data['client'] = $_GET['client'];
$this->data['tweets'] = $this->model->getTweetsByClient($this->data['client']);
$this->data['tweets'] = $this->model->getTweetsByClient($this->data['client'], $offset, $perPage);
$this->data['totalTweetsByClient'] = $this->model->getTweetsByClientCount($this->data['client']);
$pageBaseUrl = $this->data['config']['system']['baseUrl'].'client/'.urlencode($this->data['client']).'/';
$this->data['pagination'] = $this->paginator->paginate($pageBaseUrl, $this->data['totalTweetsByClient'], $current_page, $perPage);
$this->data['header'] .= 'Tweets from '.$this->data['client'];

} else if (isset($_GET['favorites'])) {
Expand All @@ -77,16 +89,15 @@ public function index() {
$this->data['pageType'] = 'favorites';
$this->data['favorite_tweets'] = true;
$this->data['client'] = $_GET['favorites'];
$this->data['tweets'] = $this->model->getFavoriteTweets();
$this->data['tweets'] = $this->model->getFavoriteTweets($offset, $perPage);
$pageBaseUrl = $this->data['config']['system']['baseUrl'].'favorites/';
$this->data['pagination'] = $this->paginator->paginate($pageBaseUrl, $this->data['totalFavoriteTweets'], $current_page, $perPage);
$this->data['header'] .= 'Favorite Tweets';

} else {

// default view: show all the tweets
$this->data['pageType'] = 'recent';
$current_page = (isset($_GET['page'])) ? $_GET['page']: 1;
$offset = ($current_page > 1) ? (($current_page-1) * $perPage) : 0;

$this->data['pageType'] = 'recent';
$this->data['all_tweets'] = true;
$this->data['tweets'] = $this->model->getTweets($offset, $perPage);
$this->data['pagination'] = $this->paginator->paginate($this->data['config']['system']['baseUrl'], $this->data['totalTweets'], $current_page, $perPage);
Expand Down
51 changes: 44 additions & 7 deletions amt/model.php
Expand Up @@ -75,11 +75,15 @@ public function getTweets($offset=0, $perPage=50) {

}

public function getSearchResults($k, $offset=0, $perPage=50) {
public function getSearchResults($k, $offset=0, $perPage=50, $count=false) {

if (trim($k) == '') return false;

$sql = 'select * from '.$this->table.' where 1 ';
if ($count) {
$sql = 'select count(*) as total from '.$this->table.' where 1 ';
} else {
$sql = 'select * from '.$this->table.' where 1 ';
}

// split out the quoted items
// $phrases[0] is an array of full pattern matches (quotes intact)
Expand All @@ -102,17 +106,29 @@ public function getSearchResults($k, $offset=0, $perPage=50) {
}
}
$sql = rtrim($sql, " or "); // remove that dangling "or"
$sql .= ') order by id desc limit :offset,:perPage';
$sql .= ') order by id desc';

if (!$count) {
$sql .= ' limit :offset,:perPage';
}

// bind each search term
$stmt = $this->db->prepare($sql);
foreach ($wordParams as $key=>$param) {
$stmt->bindValue($key, $param, PDO::PARAM_STR);
}
$stmt->bindValue(':offset', (int) $offset, PDO::PARAM_INT);
$stmt->bindValue(':perPage', (int) $perPage, PDO::PARAM_INT);
if (!$count) {
$stmt->bindValue(':offset', (int) $offset, PDO::PARAM_INT);
$stmt->bindValue(':perPage', (int) $perPage, PDO::PARAM_INT);
}
$stmt->execute();
return $stmt->fetchAll();

if ($count) {
$row = $stmt->fetch();
return $row['total'];
} else {
return $stmt->fetchAll();
}

}

Expand All @@ -138,9 +154,20 @@ public function getTweetsByMonth($year, $month, $offset=0, $perPage=50) {

}

public function getTweetsByMonthCount($year, $month) {

$stmt = $this->db->prepare('select count(*) as total from '.$this->table.' where year(created_at)=:year and month(created_at)=:month order by id desc');
$stmt->bindValue(':year', (int) $year, PDO::PARAM_INT);
$stmt->bindValue(':month', (int) $month, PDO::PARAM_INT);
$stmt->execute();
$row = $stmt->fetch();
return $row['total'];

}

public function getTweetsByClient($client, $offset=0, $perPage=50) {

$stmt = $this->db->prepare('select * from '.$this->table.' where source REGEXP "<a.*>:client</a>" order by id desc');
$stmt = $this->db->prepare('select * from '.$this->table.' where source REGEXP CONCAT("(<a.*>)?", :client, "(</a>)?") order by id desc limit :offset,:perPage');
$stmt->bindValue(':client', $client, PDO::PARAM_STR);
$stmt->bindValue(':offset', (int) $offset, PDO::PARAM_INT);
$stmt->bindValue(':perPage', (int) $perPage, PDO::PARAM_INT);
Expand All @@ -149,6 +176,16 @@ public function getTweetsByClient($client, $offset=0, $perPage=50) {

}

public function getTweetsByClientCount($client) {

$stmt = $this->db->prepare('select count(*) as total from '.$this->table.' where source REGEXP CONCAT("(<a.*>)?", :client, "(</a>)?")');
$stmt->bindValue(':client', $client, PDO::PARAM_STR);
$stmt->execute();
$row = $stmt->fetch();
return $row['total'];

}

public function getTwitterMonths() {

$stmt = $this->db->prepare('select year(created_at) as y, month(created_at) as m, count(*) AS total FROM '.$this->table.' GROUP BY year(created_at),month(created_at) order by created_at desc');
Expand Down
12 changes: 9 additions & 3 deletions amt/paginator.php
Expand Up @@ -13,18 +13,24 @@ class Paginator {
* @param int $perPage The total tweets per page.
* @return string The pagination links HTML.
*/
public function paginate($baseUrl, $total, $currentPage=1, $perPage=100) {
public function paginate($baseUrl, $total, $currentPage=1, $perPage=100, $niceUrl=true) {

if ($total == 0) {
return '';
}

$numPages = ceil($total / $perPage);

$pageMarker = ($niceUrl) ? 'page/' : '&page=';

$html = '<div class="amt-pagination"><ul class="pager">';

if ($currentPage > 1) {
$html .= '<li class="previous"><a href="' . $baseUrl . 'page/' . ($currentPage - 1) . '">&larr; Newer Tweets</a></li>';
$html .= '<li class="previous"><a href="' . $baseUrl . $pageMarker . ($currentPage - 1) . '">&larr; Newer Tweets</a></li>';
}

if ($currentPage < $numPages) {
$html .= '<li class="next"><a href="' . $baseUrl . 'page/' . ($currentPage + 1) . '">Older Tweets &rarr;</a></li>';
$html .= '<li class="next"><a href="' . $baseUrl . $pageMarker . ($currentPage + 1) . '">Older Tweets &rarr;</a></li>';
}

$html .= '</ul>';
Expand Down
18 changes: 13 additions & 5 deletions tests/controllerTest.php
Expand Up @@ -38,17 +38,16 @@ public function setUp() {
// model returns latest tweet
$this->model->expects($this->any())->method('getTweet')->will($this->returnValue($this->latestTweet));

// search
$this->model->expects($this->any())->method('getSearchResults')->will($this->returnValue($this->recentTweets));

// by client
$this->model->expects($this->any())->method('getTweetsByClient')->will($this->returnValue($this->recentTweets));
$this->model->expects($this->any())->method('getTweetsByClientCount')->will($this->returnValue(1));

// favorites
$this->model->expects($this->any())->method('getFavoriteTweets')->will($this->returnValue($this->recentTweets));

// search
// month
$this->model->expects($this->any())->method('getTweetsByMonth')->will($this->returnValue($this->recentTweets));
$this->model->expects($this->any())->method('getTweetsByMonthCount')->will($this->returnValue(1));

// months
$this->model->expects($this->any())->method('getTwitterMonths')->will($this->returnValue(array()));
Expand Down Expand Up @@ -92,8 +91,17 @@ public function testSearch() {
// preconditions
$_GET['q'] = 'aardvark';

$model = $this->model;

// search
// calls 0-6 are at the top of the index() controller method
$model->expects($this->at(7))->method('getSearchResults')->will($this->returnValue($this->recentTweets));
$model->expects($this->at(8))->method('getSearchResults')->will($this->returnValue(1));

$controller = new Controller($model, $this->view, $this->paginator);

ob_start();
$this->controller->index();
$controller->index();
$output = ob_get_clean();

$this->assertTrue($this->didFindString($output, 'amt-search'));
Expand Down

0 comments on commit af98496

Please sign in to comment.