Created
March 5, 2011 15:07
-
-
Save devi/856418 to your computer and use it in GitHub Desktop.
Kohana helper collection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php defined('SYSPATH') or die('No direct script access.'); | |
class HELPER { | |
/* | |
* helper to load post from database | |
* | |
* @param int 4 digit ex. 2010 | |
* @param int without leading zero | |
* @param string slug/permalink, if set then limit/offset will be ignore | |
* @param int post status ex. draft,published,preview | |
* @param int limit | |
* @param int offset | |
* @return mixed if query failed return FALSE else return Database_MySQL_Result | |
*/ | |
public static function load_post($year, $month = NULL, $slug = NULL, $status = 0, $limit = 0, $offset = 10) | |
{ | |
if ( ! $year) | |
{ | |
return FALSE; | |
} | |
$format = '%Y'; | |
if ($month !== NULL) | |
{ | |
$format .= '-%c'; | |
$year = $year.'-'.$month; | |
} | |
$post = DB::select()->from('posts') | |
->where('status', '=', $status) | |
->and_where('FROM_UNIXTIME("published_on", \''.$format.'\')', '=', ':format') | |
->param(':format', $year); | |
if ($slug !== NULL) | |
{ | |
$post->and_where('slug', '=', ':slug')->param(':slug', $slug); | |
} | |
else | |
{ | |
$post->order_by('published_on', 'DESC')->limit($limit)->offset($offset); | |
} | |
$post->as_object()->execute(); | |
return ($post AND count($post) > 0) ? $post : FALSE; | |
} | |
/* | |
* Extend HTML::style | |
* | |
* @param string File name | |
* @param array Default attributes | |
* @param string Spesific browser logic/condition | |
* @param mixed Protocol to pass to URL::base() | |
* @param boolean Include the index page | |
* @return string | |
*/ | |
public static function style($file, array $attributes = NULL, $browser = NULL, $protocol = NULL, $index = FALSE) | |
{ | |
$style = HTML::style($file,$attributes, $protocol, $index); | |
if ($browser) | |
return '<!--[if '.$browser.' ]>'.$style.'<![endif]-->'; | |
return $style; | |
} | |
/* | |
* Extend HTML::script | |
* | |
* @param string File name | |
* @param array Default attributes | |
* @param string Spesific browser logic/condition | |
* @param mixed Protocol to pass to URL::base() | |
* @param boolean Include the index page | |
* @return string | |
*/ | |
public static function script($file, array $attributes = NULL, $browser = NULL, $protocol = NULL, $index = FALSE) | |
{ | |
$script = HTML::script($file, $attributes, $protocol, $index); | |
if ($browser) | |
return '<!--[if '.$browser.' ]>'.$script.'<![endif]-->'; | |
return $script; | |
} | |
/** | |
* Builds & returns a variable containing the HTML code to display pagination links based on given params | |
* | |
* @param int $page - the current page | |
* @param int $totalitems - the number of items to paginate (not total number of pages) | |
* @param int $limit - the number of items per page | |
* @param int $adjacents - the number of page links to put adjacent to the current page | |
* @param string $targetpage - URL to the web page requiring pagination links e.g. /module/controller/action/id/abc123123/ | |
* @param string $pagestring - the URL params to pass the new page value e.g. page/ (the number is inserted at the end of the string) | |
* @param string $cssClass - the class of the containing UL tag for the returned pagination | |
* @return void | |
* @author Stranger Studios, adapted by Rich Milns | |
* @see http://www.strangerstudios.com/sandbox/pagination/diggstyle.php | |
*/ | |
public static function digg($page = 1, $totalitems, $limit = 15, $adjacents = 1, $targetpage = "/", $pagestring = "page/", $cssClass='pagination') | |
{ | |
//defaults | |
if(!$adjacents) $adjacents = 1; | |
if(!$limit) $limit = 15; | |
if(!$page) $page = 1; | |
if(!$targetpage) $targetpage = "/"; | |
//other vars | |
$prev = $page - 1; //previous page is page - 1 | |
$next = $page + 1; //next page is page + 1 | |
$lastpage = ceil($totalitems / $limit); //lastpage is = total items / items per page, rounded up. | |
$lpm1 = $lastpage - 1; //last page minus 1 | |
/* | |
Now we apply our rules and draw the pagination object. | |
We're actually saving the code to a variable in case we want to draw it more than once. | |
*/ | |
$pagination = ""; | |
if($lastpage > 1) | |
{ | |
$pagination .= "<ul class=\"" . $cssClass . "\">".PHP_EOL; | |
//previous button | |
if ($page > 1) | |
$pagination .= "<li><a href=\"$targetpage$pagestring$prev\" class=\"prevnext\">« previous</a></li>".PHP_EOL; | |
else | |
$pagination .= "<li><a href=\"#\" class=\"prevnext disablelink\">« previous</a></li>".PHP_EOL; | |
//pages | |
if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up | |
{ | |
for ($counter = 1; $counter <= $lastpage; $counter++) | |
{ | |
if ($counter == $page) | |
$pagination .= "<li><a href=\"#\" class=\"currentpage\">$counter</a></li>".PHP_EOL; | |
else | |
$pagination .= "<li><a href=\"" . $targetpage . $pagestring . $counter . "\">$counter</a></li>".PHP_EOL; | |
} | |
} | |
elseif($lastpage >= 7 + ($adjacents * 2)) //enough pages to hide some | |
{ | |
//close to beginning; only hide later pages | |
if($page < 1 + ($adjacents * 3)) | |
{ | |
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++) | |
{ | |
if ($counter == $page) | |
$pagination .= "<li><a href=\"#\" class=\"currentpage\">$counter</a></li>".PHP_EOL; | |
else | |
$pagination .= "<li><a href=\"" . $targetpage . $pagestring . $counter . "\">$counter</a></li>".PHP_EOL; | |
} | |
$pagination .= "<li>...</li>".PHP_EOL; | |
$pagination .= "<li><a href=\"" . $targetpage . $pagestring . $lpm1 . "\">$lpm1</a></li>".PHP_EOL; | |
$pagination .= "<li><a href=\"" . $targetpage . $pagestring . $lastpage . "\">$lastpage</a></li>".PHP_EOL; | |
} | |
//in middle; hide some front and some back | |
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)) | |
{ | |
$pagination .= "<li><a href=\"" . $targetpage . $pagestring . "1\">1</a></li>".PHP_EOL; | |
$pagination .= "<li><a href=\"" . $targetpage . $pagestring . "2\">2</a></li>".PHP_EOL; | |
$pagination .= "<li>...</li>".PHP_EOL; | |
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++) | |
{ | |
if ($counter == $page) | |
$pagination .= "<li><a href=\"#\" class=\"currentpage\">$counter</a></li>".PHP_EOL; | |
else | |
$pagination .= "<li><a href=\"" . $targetpage . $pagestring . $counter . "\">$counter</a></li>".PHP_EOL; | |
} | |
$pagination .= "<li>...</li>".PHP_EOL; | |
$pagination .= "<li><a href=\"" . $targetpage . $pagestring . $lpm1 . "\">$lpm1</a></li>".PHP_EOL; | |
$pagination .= "<li><a href=\"" . $targetpage . $pagestring . $lastpage . "\">$lastpage</a></li>".PHP_EOL; | |
} | |
//close to end; only hide early pages | |
else | |
{ | |
$pagination .= "<li><a href=\"" . $targetpage . $pagestring . "1\">1</a></li>".PHP_EOL; | |
$pagination .= "<li><a href=\"" . $targetpage . $pagestring . "2\">2</a></li>".PHP_EOL; | |
$pagination .= "<li>...</li>"; | |
for ($counter = $lastpage - (1 + ($adjacents * 3)); $counter <= $lastpage; $counter++) | |
{ | |
if ($counter == $page) | |
$pagination .= "<li><a href=\"#\" class=\"currentpage\">$counter</a></li>".PHP_EOL; | |
else | |
$pagination .= "<li><a href=\"" . $targetpage . $pagestring . $counter . "\">$counter</a></li>".PHP_EOL; | |
} | |
} | |
} | |
//next button | |
if ($page < $counter - 1) | |
$pagination .= "<li><a href=\"" . $targetpage . $pagestring . $next . "\" class=\"prevnext\">next »</a></li>".PHP_EOL; | |
else | |
$pagination .= "<li><a href=\"#\" class=\"prevnext disablelink\">next »</a></li>".PHP_EOL; | |
$pagination .= "</ul>".PHP_EOL; | |
} | |
return $pagination; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
doesn't work