<?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__).'/View.php');
include_once(dirname(__FILE__).'/Model.php');
include_once(dirname(__FILE__).'/ModelDesc.php');
include_once(dirname(__FILE__).'/Controller.php');
include_once(dirname(__FILE__).'/System.php');
include_once(dirname(__FILE__).'/Module.php');
include_once(dirname(__FILE__).'/ModuleManager.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__).'/Table.php');
include_once(dirname(__FILE__).'/Process.php');
include_once(dirname(__FILE__).'/Generics.php');
include_once(dirname(__FILE__).'/BigInt.php');
include_once(dirname(__FILE__).'/Int128.php');
include_once(dirname(__FILE__).'/Pdf.php');
include_once(dirname(__FILE__).'/Modules/Setup.php');
include_once(dirname(__FILE__).'/Modules/ModuleManager.php');

class PackageCommon
{
  public string $Name;
  public string $Version;
  public int $ReleaseDate;
  public string $License;
  public string $Creator;
  public string $Homepage;

  function __construct()
  {
    $this->Name = 'Common';
    $this->Version = '1.2';
    $this->ReleaseDate = strtotime('2020-03-29');
    $this->Creator = 'Chronos';
    $this->License = 'GNU/GPLv3';
    $this->Homepage = 'https://svn.zdechov.net/PHPlib/Common/';
  }
}

class Paging
{
  public int $TotalCount;
  public int $ItemPerPage;
  public int $Around;
  public string $SQLLimit;
  public int $Page;

  function __construct()
  {
    global $System;

    $this->ItemPerPage = $System->Config['Web']['ItemsPerPage'];
    $this->Around = $System->Config['Web']['VisiblePagingItems'];
  }

  function Show(): string
  {
    $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;
  }
}
