Skip to content

Commit

Permalink
версия 1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
antflk committed Nov 19, 2015
1 parent 51413b7 commit 10b1959
Show file tree
Hide file tree
Showing 30 changed files with 1,024 additions and 771 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
tmp/smarty/*
vendor
.idea
vde
dev/*
75 changes: 41 additions & 34 deletions Common/AnalogTypes.php
Original file line number Diff line number Diff line change
@@ -1,38 +1,45 @@
<?php
namespace NS\TecDocSite\Common;

class AnalogTypes {
/**
* Совпадение.
*/
const MATCH = 0;
/**
* OE-номер.
*/
const OE_NUMBER = 1;
/**
* Торговый номер.
*/
const NUMBER = 2;
/**
* Сопоставимый.
*/
const COMPARABLE = 3;
/**
* Замена.
*/
const REPLACEMENT = 4;
/**
* Заменяемое.
*/
const REMOVABLE = 5;
/**
* EAN
*/
const EAN = 6;
/**
* Любая.
*/
const ANY = 10;
/**
* Класс содержит описание типов аналогов
*
* Class AnalogTypes
* @package NS\TecDocSite\Common
*/
class AnalogTypes
{
/**
* Совпадение
*/
const MATCH = 0;
/**
* OE-номер
*/
const OE_NUMBER = 1;
/**
* Торговый номер
*/
const NUMBER = 2;
/**
* Сопоставимый
*/
const COMPARABLE = 3;
/**
* Замена
*/
const REPLACEMENT = 4;
/**
* Заменяемое
*/
const REMOVABLE = 5;
/**
* EAN
*/
const EAN = 6;
/**
* Любая
*/
const ANY = 10;

}
}
253 changes: 158 additions & 95 deletions Common/Paginator.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,100 +2,163 @@
namespace NS\TecDocSite\Common;

/**
* Класс, реализующий интерфейс paginator-a, преднозначенного для перемещения по страницам.
* Класс, реализующий интерфейс paginator-a, преднозначенного для перемещения по страницам
*
* Class Paginator
* @package NS\TecDocSite\Common
*/
class Paginator {

const ONE_PAGE = 1;

private $options = NULL;
private $displayedPages = array();
private $currentPage = 0;
private $pagesCount = 0;

public function __construct(PaginatorOptions $options = NULL) {
$this->options = isset($options) ? $options : new PaginatorOptions();
}

public function __destruct() {
unset($this->options);
unset($this->displayedPages);
}

public function deploy() {
$this->make();

$page = '';
if (self::ONE_PAGE < $this->getPagesCount()) {
$options = $this->getOptions();
$url = is_null($options->url) ? $_SERVER['REQUEST_URI'] : $options->url;
$parsedUrl = parse_url($url);
parse_str($parsedUrl['query'], $urlQueryArray);
unset($urlQueryArray[$options->argName]);
$newUrlData = array();
foreach ($urlQueryArray as $k => $oneUrlQueryItem) {
$newUrlData[] = "$k=$oneUrlQueryItem";
}
$url = $newUrlData ? '?' . implode('&', $newUrlData) : '';
$dataTemplate = array(
'displayedPages' => $this->getDisplayedPages(),
'options' => $options,
'argName' => $options->argName,
'url' => $url,
'currentPage' => $this->getCurrentPage(),
'pagesCount' => $this->getPagesCount()
);
$page = View::deploy($options->template, $dataTemplate);
}
return $page;
}

final protected function make() {
$options = $this->getOptions();

$startRecord = abs((int) $options->startRecord);
$maxRecords = ceil(($options->totalRecords - $options->recordsPageCount) / $options->recordsPageCount) * $options->recordsPageCount;
if ($startRecord > $maxRecords) {
$startRecord = $maxRecords;
}
$this->currentPage = floor($startRecord / $options->recordsPageCount) + 1;
$this->pagesCount = floor($maxRecords / $options->recordsPageCount) + 1;

$leftEdgePage = $this->currentPage - floor($options->displayedPagesCount / 2);
if ($leftEdgePage <= 1) {
$leftEdgePage = 1;
}

$rightEdgePage = $leftEdgePage + $options->displayedPagesCount - 1;
if ($rightEdgePage >= $this->pagesCount) {
$rightEdgePage = $this->pagesCount;
}

if (($rightEdgePage - $leftEdgePage) < $options->displayedPagesCount) {
$leftEdgePage = $rightEdgePage - $options->displayedPagesCount + 1;
if ($leftEdgePage <= 1) {
$leftEdgePage = 1;
}
}

for ($number = $leftEdgePage; $number <= $rightEdgePage; $number++) {
$this->displayedPages[$number] = ($number - 1) * $options->recordsPageCount;
}
}

final protected function getOptions() {
return $this->options;
}

final protected function getDisplayedPages() {
return $this->displayedPages;
}

final protected function getCurrentPage() {
return $this->currentPage;
}

final protected function getPagesCount() {
return $this->pagesCount;
}
class Paginator
{
/**
* Одна страница
*/
const ONE_PAGE = 1;
/**
* Настройки пагинатора
*
* @var PaginatorOptions|null
*/
private $options = null;
/**
* Отображаемые страницы
*
* @var array
*/
private $displayedPages = array();
/**
* Текущая страница
*
* @var int
*/
private $currentPage = 0;
/**
* Количество страниц
*
* @var int
*/
private $pagesCount = 0;

/**
* Конструктор класса
*
* @param PaginatorOptions|null $options
*/
public function __construct(PaginatorOptions $options = null)
{
$this->options = isset($options) ? $options : new PaginatorOptions();
}

/**
* Деструктор класса
*/
public function __destruct()
{
unset($this->options);
unset($this->displayedPages);
}

/**
* Возвращает интерфейс пагинатора в виде html
*
* @return string
*/
public function deploy()
{
$this->make();
$page = '';
if (self::ONE_PAGE < $this->getPagesCount()) {
$options = $this->getOptions();
$url = is_null($options->url) ? $_SERVER['REQUEST_URI'] : $options->url;
$parsedUrl = parse_url($url);
parse_str($parsedUrl['query'], $urlQueryArray);
unset($urlQueryArray[$options->argName]);
$newUrlData = array();
foreach ($urlQueryArray as $k => $oneUrlQueryItem) {
$newUrlData[] = "$k=$oneUrlQueryItem";
}
$url = $newUrlData ? '?' . implode('&', $newUrlData) : '';
$dataTemplate = array(
'displayedPages' => $this->getDisplayedPages(),
'options' => $options,
'argName' => $options->argName,
'url' => $url,
'currentPage' => $this->getCurrentPage(),
'pagesCount' => $this->getPagesCount()
);
$page = View::deploy($options->template, $dataTemplate);
}
return $page;
}

/**
* Инициализация опций
*/
final protected function make()
{
$options = $this->getOptions();
$startRecord = abs((int)$options->startRecord);
$maxRecords = ceil(($options->totalRecords - $options->recordsPageCount) / $options->recordsPageCount) * $options->recordsPageCount;
if ($startRecord > $maxRecords) {
$startRecord = $maxRecords;
}
$this->currentPage = floor($startRecord / $options->recordsPageCount) + 1;
$this->pagesCount = floor($maxRecords / $options->recordsPageCount) + 1;
$leftEdgePage = $this->currentPage - floor($options->displayedPagesCount / 2);
if ($leftEdgePage <= 1) {
$leftEdgePage = 1;
}
$rightEdgePage = $leftEdgePage + $options->displayedPagesCount - 1;
if ($rightEdgePage >= $this->pagesCount) {
$rightEdgePage = $this->pagesCount;
}
if (($rightEdgePage - $leftEdgePage) < $options->displayedPagesCount) {
$leftEdgePage = $rightEdgePage - $options->displayedPagesCount + 1;
if ($leftEdgePage <= 1) {
$leftEdgePage = 1;
}
}
for ($number = $leftEdgePage; $number <= $rightEdgePage; $number++) {
$this->displayedPages[$number] = ($number - 1) * $options->recordsPageCount;
}
}

/**
* Вовращает опции для инициализации пагинатора
*
* @return PaginatorOptions|null
*/
final protected function getOptions()
{
return $this->options;
}

/**
* Возвращает количество страниц
*
* @return int
*/
final protected function getPagesCount()
{
return $this->pagesCount;
}

/**
* Возвращает отображаемые страницы
*
* @return array
*/
final protected function getDisplayedPages()
{
return $this->displayedPages;
}

/**
* Возвращает текущую страницу
*
* @return int
*/
final protected function getCurrentPage()
{
return $this->currentPage;
}
}
Loading

0 comments on commit 10b1959

Please sign in to comment.