ListView.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace app\forks\ListView;
  3. use yii\helpers\ArrayHelper;
  4. use yii\helpers\Html;
  5. class ListView extends \yii\widgets\ListView
  6. {
  7. /**
  8. * Runs the widget.
  9. */
  10. public function run()
  11. {
  12. $content = preg_replace_callback('/{\\w+}/', function ($matches) {
  13. $content = $this->renderSection($matches[0]);
  14. return $content === false ? $matches[0] : $content;
  15. }, $this->layout);
  16. $options = $this->options;
  17. $tag = ArrayHelper::remove($options, 'tag', 'div');
  18. echo Html::tag($tag, $content, $options);
  19. }
  20. /**
  21. * Renders all data models.
  22. * @return string the rendering result
  23. */
  24. public function renderItems()
  25. {
  26. try{
  27. $pageStart = new \DateTime(\Yii::$app->request->get("page","now"));
  28. } catch (\Exception $exception){
  29. $pageStart = new \DateTime("now");
  30. }
  31. $pageStart->setTime(0,0,0);
  32. $pageEnd = (clone $pageStart)->setTime(23,59,59);
  33. $query = clone $this->dataProvider->query;
  34. $pageQuery = clone $query;
  35. $pageQuery->andFilterWhere(["BETWEEN",'dt_pub',$pageStart->format("Y-m-d H:i:s"),$pageEnd->format("Y-m-d H:i:s")]);
  36. if($pageQuery->count()==0){
  37. //Такой странички не существует, кидаем на первую
  38. $pageQuery = clone $query;
  39. $pageQuery->limit(20);
  40. }
  41. $models = $pageQuery->all();
  42. $rows = [];
  43. if(count($models)==0){
  44. return "<span>К сожалению, за ".$pageStart->format("d.m.Y")." ничего нет</span>";
  45. }
  46. foreach (array_values($models) as $index => $model) {
  47. $key = $model->id;
  48. if (($before = $this->renderBeforeItem($model, $key, $index)) !== null) {
  49. $rows[] = $before;
  50. }
  51. $rows[] = $this->renderItem($model, $key, $index);
  52. if (($after = $this->renderAfterItem($model, $key, $index)) !== null) {
  53. $rows[] = $after;
  54. }
  55. }
  56. return implode($this->separator, $rows);
  57. }
  58. public function renderPager()
  59. {
  60. $pager['view'] = $this->getView();
  61. $pager['dataProvider'] = $this->dataProvider;
  62. return DatePager::widget($pager);
  63. }
  64. }