123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- <?php
- namespace app\forks\ListView;
- use app\models\base\News;
- use Cassandra\Date;
- use yii\data\ActiveDataProvider;
- use yii\db\ActiveQueryInterface;
- use yii\db\QueryInterface;
- use yii\helpers\ArrayHelper;
- use yii\helpers\Html;
- use yii\helpers\Url;
- use yii\jui\Widget;
- class DatePager extends Widget
- {
- public ActiveDataProvider $dataProvider;
- public \DateTime $now;
- public $options = ['class' => 'pagination'];
- /**
- * @var array HTML attributes which will be applied to all link containers
- * @since 2.0.13
- */
- public $linkContainerOptions = [];
- /**
- * @var array HTML attributes for the link in a pager container tag.
- * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
- */
- public $pageCssClass;
- /**
- * @var string the CSS class for the "first" page button.
- */
- public $firstPageCssClass = 'first';
- /**
- * @var string the CSS class for the "last" page button.
- */
- public $lastPageCssClass = 'last';
- /**
- * @var string the CSS class for the "previous" page button.
- */
- public $prevPageCssClass = 'prev';
- /**
- * @var string the CSS class for the "next" page button.
- */
- public $nextPageCssClass = 'next';
- /**
- * @var string the CSS class for the active (currently selected) page button.
- */
- public $activePageCssClass = 'active';
- /**
- * @var string the CSS class for the disabled page buttons.
- */
- public $disabledPageCssClass = 'disabled';
- /**
- * @var array the options for the disabled tag to be generated inside the disabled list element.
- * In order to customize the html tag, please use the tag key.
- *
- * ```php
- * $disabledListItemSubTagOptions = ['tag' => 'div', 'class' => 'disabled-div'];
- * ```
- * @since 2.0.11
- */
- public $disabledListItemSubTagOptions = [];
- /**
- * @var int maximum number of page buttons that can be displayed. Defaults to 10.
- */
- public $maxButtonCount = 10;
- /**
- * @var string|bool the label for the "next" page button. Note that this will NOT be HTML-encoded.
- * If this property is false, the "next" page button will not be displayed.
- */
- public $nextPageLabel = '»';
- /**
- * @var string|bool the text label for the "previous" page button. Note that this will NOT be HTML-encoded.
- * If this property is false, the "previous" page button will not be displayed.
- */
- public $prevPageLabel = '«';
- /**
- * @var string|bool the text label for the "first" page button. Note that this will NOT be HTML-encoded.
- * If it's specified as true, page number will be used as label.
- * Default is false that means the "first" page button will not be displayed.
- */
- public $firstPageLabel = false;
- /**
- * @var string|bool the text label for the "last" page button. Note that this will NOT be HTML-encoded.
- * If it's specified as true, page number will be used as label.
- * Default is false that means the "last" page button will not be displayed.
- */
- public $lastPageLabel = false;
- /**
- * @var bool whether to register link tags in the HTML header for prev, next, first and last page.
- * Defaults to `false` to avoid conflicts when multiple pagers are used on one page.
- * @see https://www.w3.org/TR/html401/struct/links.html#h-12.1.2
- * @see registerLinkTags()
- */
- public $registerLinkTags = false;
- /**
- * @var bool Hide widget when only one page exist.
- */
- public $hideOnSinglePage = true;
- /**
- * @var bool whether to render current page button as disabled.
- * @since 2.0.12
- */
- public $disableCurrentPageButton = false;
- public $linkOptions = [];
- function __construct($config = [])
- {
- $this->now = new \DateTime("now",new \DateTimeZone('Asia/Novosibirsk'));
- parent::__construct($config);
- }
- /**
- * Executes the widget.
- * This overrides the parent implementation by displaying the generated page buttons.
- */
- public function run()
- {
- echo $this->renderPageButtons();
- }
- /**
- * Renders the page buttons.
- * @return string the rendering result
- */
- protected function renderPageButtons()
- {
- $firstPost = $this->dataProvider->query->orderBy(['dt_pub'=>SORT_ASC])->limit(1)->one();
- if(!$firstPost instanceof News) return '';
- if($firstPost->dt_pub > $this->now->format('Y-m-d 00:00:00')) return '';
- $firstPostDateTime = new \DateTime($firstPost->dt_pub, new \DateTimeZone('Asia/Novosibirsk'));
- $pageCount = $this->now->diff($firstPostDateTime)->days;
- if ($pageCount < 2 && $this->hideOnSinglePage) {
- return '';
- }
- $buttons = [];
- $currentPage = \Yii::$app->request->get("page", $this->now->format("Y-m-d"));
- try{
- $currentPageDate = new \DateTime($currentPage);
- } catch (\Exception $exception){
- $currentPageDate = new \DateTime("now");
- }
- $pages = $this->getRealPages($currentPageDate,$this->dataProvider->query);
- $currentPageIndex = array_search($currentPageDate->format("Y-m-d"),array_column($pages,'dt_pub'));
- if(false === $currentPageIndex){
- $currentPageIndex = 0;
- $currentPageDate = new \DateTime(ArrayHelper::getValue($pages,"0.dt_pub","now"));
- }
- // prev page
- if ($this->prevPageLabel !== false) {
- if (($page = (clone $currentPageDate)->modify("+1 day"))->format("Y-m-d") > $this->now->format("Y-m-d")) {
- $page = clone $this->now;
- }
- $buttons[] = $this->renderPageButton($this->prevPageLabel, $page->format('Y-m-d'), $this->prevPageCssClass, $currentPage <= 0, false);
- }
- foreach ($pages as $page)
- {
- $date = new \DateTime($page['dt_pub']);
- $buttons[] = $this->renderPageButton($date->format('d.m'), $date->format('Y-m-d'), null, $this->disableCurrentPageButton && $date->format('Y-m-d') == $currentPage, $date->format('Y-m-d') == $currentPage);
- }
- // internal pages
- // list($beginPage, $endPage) = $this->getPageRange($currentPageDate->format("Y-m-d"), $firstPostDateTime);
- // $diff = $beginPage->diff($endPage)->days;
- // for ($i = 0; $i <= $diff; ++$i) {
- // $date = (clone $beginPage)->modify("-{$i} days");
- // if($date->format("Y") >= $this->now->format("Y")){
- // $internalLabel = $date->format('d.m');
- // } else {
- // $internalLabel = $date->format('d.m.y');
- // }
- // $buttons[] = $this->renderPageButton($internalLabel, $date->format('Y-m-d'), null, $this->disableCurrentPageButton && $date->format('Y-m-d') == $currentPage, $date->format('Y-m-d') == $currentPage);
- // }
- // next page
- if ($this->nextPageLabel !== false) {
- if (($page = (clone $currentPageDate)->modify("-1 day"))->format("Y-m-d") < $firstPostDateTime->format("Y-m-d")) {
- $page = clone ($firstPostDateTime);
- }
- $buttons[] = $this->renderPageButton($this->nextPageLabel, $page->format('Y-m-d'), $this->nextPageCssClass, $currentPage <= 0, false);
- }
- $options = $this->options;
- $tag = ArrayHelper::remove($options, 'tag', 'ul');
- return Html::tag($tag, implode("\n", $buttons), $options);
- }
- /**
- * Renders a page button.
- * You may override this method to customize the generation of page buttons.
- * @param string $label the text label for the button
- * @param int $page the page number
- * @param string $class the CSS class for the page button.
- * @param bool $disabled whether this page button is disabled
- * @param bool $active whether this page button is active
- * @return string the rendering result
- */
- protected function renderPageButton($label, $page, $class, $disabled, $active)
- {
- $options = $this->linkContainerOptions;
- $linkWrapTag = ArrayHelper::remove($options, 'tag', 'li');
- Html::addCssClass($options, empty($class) ? $this->pageCssClass : $class);
- if ($active) {
- Html::addCssClass($options, $this->activePageCssClass);
- }
- if ($disabled) {
- Html::addCssClass($options, $this->disabledPageCssClass);
- $disabledItemOptions = $this->disabledListItemSubTagOptions;
- $tag = ArrayHelper::remove($disabledItemOptions, 'tag', 'span');
- return Html::tag($linkWrapTag, Html::tag($tag, $label, $disabledItemOptions), $options);
- }
- $linkOptions = $this->linkOptions;
- $linkOptions['data-page'] = $page;
- return Html::tag($linkWrapTag, Html::a($label, $this->createUrl($page), $linkOptions), $options);
- }
- public function createUrl($page)
- {
- $url = parse_url(Url::to());
- return Url::to([ArrayHelper::getValue($url, "path","/"),"page"=>$page]);
- }
- /**
- * @return array the begin and end pages that need to be displayed.
- */
- protected function getPageRange($currentPage, \DateTime $firstPostDateTime)
- {
- $currentPageDate = new \DateTime($currentPage);
- $beginPage = min((new \DateTime("now"))->format("Y-m-d"), $currentPageDate->modify("+".($this->maxButtonCount / 2)." days")->format("Y-m-d"));
- $beginPageDate = new \DateTime($beginPage);
- if (($endPageDate = (clone $beginPageDate)->modify("-".($this->maxButtonCount - 1)." days")) <= $firstPostDateTime->format("Y-m-d")) {
- $endPageDate = $firstPostDateTime->modify("+1 day");
- $beginPageDate = (clone $endPageDate)->modify("+". ($this->maxButtonCount + 1)." days");
- }
- return [$beginPageDate, $endPageDate];
- }
- public $_realPages = false;
- public function getRealPages(\DateTime $currentPageDate, QueryInterface $query)
- {
- if($this->_realPages === false){
- $this->_realPages = $query
- ->select(["dt_pub"=>"Date(dt_pub)",'cnt'=>"count(*)"])
- ->andWhere("dt_pub < NOW()")
- ->andWhere(['<','dt_pub',$currentPageDate->format('Y-m-d 23:59:59')])
- ->groupBy("Date(dt_pub)")
- ->orderBy(["dt_pub"=>SORT_DESC])
- ->limit(10)
- ->asArray()
- ->all();
- }
- return $this->_realPages;
- }
- }
|