sibtracker/system/classes/navigation.class.php
2019-05-18 13:46:03 +08:00

125 lines
3.5 KiB
PHP

<?php
if( !defined( "DATALIFEENGINE" ) ) die( "Hacking attempt!" );
class Navigation {
var $URL = "";
var $Page = "";
var $Total = "";
var $Tpl = "";
var $Compile = "content";
// Îòäà¸ò ñãåíåðèðîâàííóþ ññûëêó èëè Url ïî íåîáõîäèìîñòè
function CNPage( $page, $title = "", $return = "" ){
$link = str_replace( "{page}", $page, $this->URL );
if( $return == "url" ) return $link;
if( $title ) $page = $title;
return "<a href=\"{$link}\">{$page}</a> ";
}
// Ãåíåðèðóåò ëèñò ññûëîê
function LPage( $start, $end ){
while( $start < $end )
{
if( $start == $this->Page )
$navigation .= "<span>".$start."</span>\n";
else
$navigation .= $this->CNPage( $start );
$start++;
}
return $navigation;
}
// Ôîðìèðîâàíèå íàâèãàöèè ïðîèñõîäèò â ýòîé ôóíêöèè
function BuildNavigation(){
global $tpl;
// Åñëè íîëü ñòðàíèö, òî íàâèãàöèþ íå ôîðìèðóåì
if( $this->Total < 2 ) return;
// Îò÷èùàåì ñòàðóþ ñãåíåðèðîâàííóþ íàâèãàöèþ
//$tpl->result[ $this->Compile ] = "";
// ×òîáû íå áûëî íåäîðàçóìåíèé
if( $this->Page < 1 ) $this->Page = 1;
if( $this->Page > $this->Total ) $this->Page = $this->Total;
$first = $this->Page - 4; // Ñêîëüêî ñòðàíèö âûâîäèòñÿ ñëåâà îò àêòèâíîé
$last = $this->Page + 5; // Ñêîëüêî ñòðàíèö âûâîäèòñÿ ñïðàâà îò àêòèâíîé
$PPage = $this->CNPage( $this->Page - 1 , "", "url" ); // Ïðåäûäóùàÿ ñòðàíèöà
$NPage = $this->CNPage( $this->Page + 1 , "", "url" ); // Ñëåäóþùàÿ ñòðàíèöà
$PageMin = 8; // Ìèíèìàëüíî çíà÷åíèå äëÿ íà÷àëà ïàãèíàöèè
$PageMax = $this->Total - 8; // Ìàêñèìàëüíî çíà÷åíèå äëÿ íà÷àëà ïàãèíàöèè
$divider = "... "; // Íà ÷òî çàìåíÿåì ïðîáåëû
// Ïîäãðóçêà øàáëîíà
$tpl->load_template( $this->Tpl );
// Ïðåäûäóùàÿ ñòðàíèöà
if( $this->Page > 1 )
$tpl->set_block( "'\[prev-link\](.*?)\[/prev-link\]'si", "<a href=\"{$PPage}\">\\1</a>" );
else
$tpl->set_block( "'\[prev-link\](.*?)\[/prev-link\]'si", "<span>\\1</span>" );
// Ñëóäåþùàÿ ñòðàíèöà
if( $this->Page < $this->Total )
$tpl->set_block( "'\[next-link\](.*?)\[/next-link\]'si", "<a href=\"{$NPage}\">\\1</a>" );
else
$tpl->set_block( "'\[next-link\](.*?)\[/next-link\]'si", "<span>\\1</span>" );
// Åñëè ñòðàíèö ìåíüøå ÷åì 10
if( $this->Total < 10 )
{
$navigation = $this->LPage( 1, $this->Total + 1 );
$navigation = $navigation;
}
else
{
// Ïî ñåðåäèíå
if( ( $this->Page >= $PageMin - 2 ) and ( $this->Page <= $PageMax + 2 ) )
{
$navigation = $this->LPage( $first, $last );
$navigation = $this->CNPage( "1" ).$divider.$navigation.$divider.$this->CNPage( $this->Total );
}
// Åñëè ñòðàíèöà íå áîëüøå 8 - îé
elseif( $this->Page < $PageMin )
{
$navigation = $this->LPage( 1, $PageMin + 1 );
$navigation = $navigation.$divider.$this->CNPage( $this->Total );
}
// Åñëè ñòðàíèöà áîëüøå ïðåä 8 - ìè ïîñëåäíåé
elseif( $this->Page > $PageMax )
{
$navigation = $this->LPage( $PageMax, $this->Total + 1 );
$navigation = $this->CNPage( "1" ).$divider.$navigation;
}
}
// Íàçíà÷åíèå òåãîâ äëÿ øàáëîíà
$tpl->set( "{pages}", $navigation );
$tpl->set( "{page}", $this->Page );
$tpl->set( "{count_page}", $this->Total );
if( $this->Total < 2 )
{
$tpl->set_block( "'\[page\](.*?)\[/page\]'si", "" );
}
else
{
$tpl->set( "[page]", "" );
$tpl->set( "[/page]", "" );
}
// Ôîðìèðîâàíèå øàáëîíà
$tpl->compile( $this->Compile );
// Î÷èñòêà êëàññà øàáëîíîâ îò íàâèãàöèè
$tpl->clear();
// Î÷èñòêà íàâèãàöèè
unset( $navigation );
}
}
?>