tweetmonitor/common/display_lib.php

124 lines
4.7 KiB
PHP
Raw Permalink Normal View History

2016-02-09 13:23:43 +00:00
<?php
function linkify($text) {
$text = preg_replace("~[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]~",
"<a href=\"\\0\">\\0</a>", $text);
$text = preg_replace("/\B@(\w+(?!\/))\b/i",
'<a href="https://twitter.com/\\1">@\\1</a>', $text);
$text = preg_replace("/\B(?<![=\/])#([\w]+[a-z]+([0-9]+)?)/i",
'<a href="https://twitter.com/#!/search/%23\\1">#\\1</a>', $text);
return $text;
}
function twitter_time($time) {
$delta = time() - strtotime($time);
if ($delta < 60) {
return 'less than a minute ago';
} else if ($delta < 120) {
return 'about a minute ago';
} else if ($delta < (60 * 60)) {
return floor($delta / 60) . ' minutes ago';
} else if ($delta < (120 * 60)) {
return 'about an hour ago';
} else if ($delta < (24 * 60 * 60)) {
return floor($delta / 3600) . ' hours ago';
} else if ($delta < (48 * 60 * 60)) {
return '1 day ago';
} else {
return number_format(floor($delta / 86400)) . ' days ago';
}
}
function pagination($query,$per_page=8,$page=1,$url='?')
{
$query = "SELECT COUNT(*) as `num` FROM {$query}";
$row = mysql_fetch_array(mysql_query($query));
$total = $row['num'];
$adjacents = "2";
$prevlabel = "&lsaquo; Prev";
$nextlabel = "Next &rsaquo;";
$page = ($page == 0 ? 1 : $page);
$start = ($page - 1) * $per_page;
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total/$per_page);
$lpm1 = $lastpage - 1; // //last page minus 1
$pagination = "";
if($lastpage > 0){
$pagination .= "<ul class='pagination'>";
$pagination .= "<li class='page_info'>Page {$page} of {$lastpage}</li>";
if ($page > 1) $pagination.= "<li><a href='{$url}page={$prev}'>{$prevlabel}</a></li>";
if ($lastpage < 7 + ($adjacents * 2)){
for ($counter = 1; $counter <= $lastpage; $counter++){
if ($counter == $page)
$pagination.= "<li><a class='current'>{$counter}</a></li>";
else
$pagination.= "<li><a href='{$url}page={$counter}'>{$counter}</a></li>";
}
} elseif($lastpage > 5 + ($adjacents * 2)){
if($page < 1 + ($adjacents * 2)) {
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++){
if ($counter == $page)
$pagination.= "<li><a class='current'>{$counter}</a></li>";
else
$pagination.= "<li><a href='{$url}page={$counter}'>{$counter}</a></li>";
}
$pagination.= "<li class='dot'>...</li>";
$pagination.= "<li><a href='{$url}page={$lpm1}'>{$lpm1}</a></li>";
$pagination.= "<li><a href='{$url}page={$lastpage}'>{$lastpage}</a></li>";
} elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)) {
$pagination.= "<li><a href='{$url}page=1'>1</a></li>";
$pagination.= "<li><a href='{$url}page=2'>2</a></li>";
$pagination.= "<li class='dot'>...</li>";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++) {
if ($counter == $page)
$pagination.= "<li><a class='current'>{$counter}</a></li>";
else
$pagination.= "<li><a href='{$url}page={$counter}'>{$counter}</a></li>";
}
$pagination.= "<li class='dot'>..</li>";
$pagination.= "<li><a href='{$url}page={$lpm1}'>{$lpm1}</a></li>";
$pagination.= "<li><a href='{$url}page={$lastpage}'>{$lastpage}</a></li>";
} else {
$pagination.= "<li><a href='{$url}page=1'>1</a></li>";
$pagination.= "<li><a href='{$url}page=2'>2</a></li>";
$pagination.= "<li class='dot'>..</li>";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++) {
if ($counter == $page)
$pagination.= "<li><a class='current'>{$counter}</a></li>";
else
$pagination.= "<li><a href='{$url}page={$counter}'>{$counter}</a></li>";
}
}
}
if ($page < $counter - 1) $pagination.= "<li><a href='{$url}page={$next}'>{$nextlabel}</a></li>";
$pagination.= "</ul>";
}
return $pagination;
}
?>