<?php

include_once(dirname(__FILE__).'/Database.php');
include_once(dirname(__FILE__).'/Image.php');
include_once(dirname(__FILE__).'/Mail.php');
include_once(dirname(__FILE__).'/NetworkAddress.php');
include_once(dirname(__FILE__).'/PrefixMultiplier.php');
include_once(dirname(__FILE__).'/RSS.php');
include_once(dirname(__FILE__).'/UTF8.php');
include_once(dirname(__FILE__).'/VarDumper.php');
include_once(dirname(__FILE__).'/Error.php');
include_once(dirname(__FILE__).'/Base.php');
include_once(dirname(__FILE__).'/Application.php');
include_once(dirname(__FILE__).'/AppModule.php');
include_once(dirname(__FILE__).'/Config.php');
include_once(dirname(__FILE__).'/Page.php');
include_once(dirname(__FILE__).'/Locale.php');
include_once(dirname(__FILE__).'/Update.php');
include_once(dirname(__FILE__).'/Setup.php');
include_once(dirname(__FILE__).'/Table.php');

class PackageCommon
{
  var $Name;
  var $Version;
  var $ReleaseDate;
  var $License;
  var $Creator;
  var $Homepage;

  function __construct()
  {
    $this->Name = 'Common';
    $this->Version = '1.2';
    $this->ReleaseDate = strtotime('2016-01-22');
    $this->Creator = 'Chronos';
    $this->License = 'GNU/GPL';
    $this->Homepage = 'http://svn.zdechov.net/svn/PHPlib/Common/';
  }
}

class Paging
{
  var $TotalCount;
  var $ItemPerPage;
  var $Around;
  var $SQLLimit;
  var $Page;

  function __construct(System $System)
  {
    $this->ItemPerPage = $System->Config['Web']['ItemsPerPage'];
    $this->Around = $System->Config['Web']['VisiblePagingItems'];
  }

  function Show()
  {
    $QueryItems = GetQueryStringArray($_SERVER['QUERY_STRING']);

    $Result = '';
    if(array_key_exists('all', $QueryItems))
    {
      $PageCount = 1;
      $ItemPerPage = $this->TotalCount;
    } else
    {
      $ItemPerPage = $this->ItemPerPage;
      $Around = round($this->Around / 2);
      $PageCount = floor($this->TotalCount / $ItemPerPage) + 1;
    }

    if(!array_key_exists('Page', $_SESSION)) $_SESSION['Page'] = 0;
    if(array_key_exists('page', $_GET)) $_SESSION['Page'] = $_GET['page'] * 1;
    if($_SESSION['Page'] < 0) $_SESSION['Page'] = 0;
    if($_SESSION['Page'] >= $PageCount) $_SESSION['Page'] = $PageCount - 1;
    $CurrentPage = $_SESSION['Page'];

    $Result .= 'Počet položek: <strong>'.$this->TotalCount.'</strong> &nbsp; Stránky: ';

    $Result = '';
    if($PageCount > 1)
    {
      if($CurrentPage > 0)
      {
        $QueryItems['page'] = 0;
        $Result.= '<a href="?'.SetQueryStringArray($QueryItems).'">&lt;&lt;</a> ';
        $QueryItems['page'] = ($CurrentPage - 1);
        $Result.= '<a href="?'.SetQueryStringArray($QueryItems).'">&lt;</a> ';
      }
      $PagesMax = $PageCount - 1;
      $PagesMin = 0;
      if($PagesMax > ($CurrentPage + $Around)) $PagesMax = $CurrentPage + $Around;
      if($PagesMin < ($CurrentPage - $Around))
      {
        $Result.= ' ... ';
        $PagesMin = $CurrentPage - $Around;
      }
      for($i = $PagesMin; $i <= $PagesMax; $i++)
      {
        if($i == $CurrentPage) $Result.= '<strong>'.($i + 1).'</strong> ';
        else {
         $QueryItems['page'] = $i;
         $Result .= '<a href="?'.SetQueryStringArray($QueryItems).'">'.($i + 1).'</a> ';
        }
      }
      if($PagesMax < ($PageCount - 1)) $Result .= ' ... ';
      if($CurrentPage < ($PageCount - 1))
      {
        $QueryItems['page'] = ($CurrentPage + 1);
        $Result.= '<a href="?'.SetQueryStringArray($QueryItems).'">&gt;</a> ';
        $QueryItems['page'] = ($PageCount - 1);
        $Result.= '<a href="?'.SetQueryStringArray($QueryItems).'">&gt;&gt;</a>';
      }
    }
    $QueryItems['all'] = '1';
    if($PageCount > 1) $Result.= ' <a href="?'.SetQueryStringArray($QueryItems).'">Vše</a>';

    $Result = '<div style="text-align: center">'.$Result.'</div>';
    $this->SQLLimit = ' LIMIT '.$CurrentPage * $ItemPerPage.', '.$ItemPerPage;
    $this->Page = $CurrentPage;
    return($Result);
  }
}
