index.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /**
  3. * @var $topic NewsTopic | NULL
  4. * @var $this \yii\web\View
  5. * @var $news_query \yii\db\ActiveQuery
  6. */
  7. use app\models\base\NewsTopic;
  8. use yii\web\View;
  9. use app\models\base\Story;
  10. use app\models\base\Tags;
  11. $this->registerJsFile(
  12. '@web/js/autocomplete/jquery.autocomplete.min.js',
  13. ['position'=>View::POS_END, 'defer' => 'defer'],
  14. 'autocomplete'
  15. );
  16. $this->params['breadcrumbs'] = [
  17. [
  18. "text"=>"НОВОСТИ",
  19. "url"=>"/news"
  20. ]
  21. ];
  22. if($topic instanceof NewsTopic){
  23. $this->params['breadcrumbs'][] =
  24. [
  25. "text"=>$topic->title,
  26. "url"=>$topic->url
  27. ];
  28. }
  29. $dateRange = \Yii::$app->request->get('daterange');
  30. $ssearch = \Yii::$app->request->get('q');
  31. $tag = \Yii::$app->request->get('tag',null);
  32. $story = \Yii::$app->request->get('story',null);
  33. if( !is_null($story) ){
  34. $storys = Story::findOne(['id'=>$story]);
  35. }
  36. if( !is_null($tag) ){
  37. $tags = Tags::findOne(['id'=>$tag]);
  38. }
  39. $sstring = !isset($tags)?'':'по тэгу:'.$tags->title;
  40. $sstring .= !isset($storys)?'':' по сюжету:'.$storys->title;
  41. $sstring .= ' '.$ssearch;
  42. if(!is_null($dateRange) && $dateRange!=""){
  43. $dateRange = explode(" — ",$dateRange);
  44. $dtb = date("Y-m-d",strtotime($dateRange[0]));
  45. $dte = isset($dateRange[1])?date("Y-m-d",strtotime($dateRange[1])):date("Y-m-d",strtotime($dateRange[0]));
  46. }
  47. $page = \Yii::$app->request->get('page');
  48. $addtpage = $page?". Страница ($page)":'';
  49. Yii::$app->og->title = "Страница поиска".$addtpage.' - '.$sstring;
  50. Yii::$app->og->description = "Поиск статей по сайту amic.ru".$addtpage.' - '.$sstring;
  51. Yii::$app->og->noindex = 'Y';
  52. ?>
  53. <div class="archive">
  54. <div class="row">
  55. <div class="col-md-12">
  56. <div class="search-header">
  57. <form action="/search" method="get" class="search_form">
  58. <h6>Поиск по запросу: </h6>
  59. <input type="hidden" value="<?=Yii::$app->request->get("index")?>" name="index">
  60. <input type="text" class="search_input" name="q" value="<?=Yii::$app->request->get("q")?>">
  61. <button>Найти</button>
  62. </form>
  63. </div>
  64. </div>
  65. </div>
  66. <div class="row">
  67. <div class="col-md-3">
  68. <div class="topics-list">
  69. <div class="topics-continer">
  70. <?php
  71. echo Yii::$app->cache->getOrSet("search_rubrics-{$topic->id}",function () use($topic){
  72. return \yii\widgets\ListView::widget([
  73. "dataProvider" => new \yii\data\ActiveDataProvider([
  74. "query"=>NewsTopic::find()->andWhere(["show"=>"Y","active"=>1,"topic_id"=>null])->orderBy(["order"=>SORT_ASC]), //"topic_id"=>$topic->id next lavel maybe
  75. "pagination" =>[
  76. "pageSize"=>0
  77. ],
  78. ]),
  79. 'options' => [
  80. 'tag' => "ul",
  81. 'class' => 'list-wrapper',
  82. 'id' => 'list-wrapper',
  83. ],
  84. "itemOptions"=>[
  85. "tag"=>"li"
  86. ],
  87. "itemView" => function($model) use($topic) {return \yii\helpers\Html::a($model->title,["news/{$model->url}"],['class'=>$model->id==$topic->id?"active":""]);},
  88. "layout" => "{items}"
  89. ]);
  90. },600);
  91. ?>
  92. </div>
  93. </div>
  94. </div>
  95. <div class="col-md-9">
  96. <div class="news-list">
  97. <?=$this->render("archive/filters")?>
  98. <div class="row align-items-center">
  99. <div class="col-auto">
  100. <select class="custom-select" aria-label="Даты" onchange="doSelect(this,document);">
  101. <option value="0">За всё время</option>
  102. <option value="1">За неделю</option>
  103. <option value="2">За месяц</option>
  104. <option value="3">За Год</option>
  105. <option value="4" onclick="toggleFiltersModal();return false;" <?=!is_null($dateRange)?'selected':''?>><?=($dateRange)?$dtb.'-'.$dte:'Выбрать даты'?></option>
  106. </select>
  107. </div>
  108. <div class="col-auto"><input type="text" class="form-control" name="tag" value="<?=!isset($tags)?'':$tags->title?>" placeholder="Тэг" id="tag"></div>
  109. <div class="col-auto"><input type="text" class="form-control" name="story" value="<?=!isset($storys)?'':$storys->title?>" placeholder="Сюжет" id="story"></div>
  110. </div>
  111. <?php
  112. if( !is_null($news_query) ){
  113. $cnt = $news_query->count();
  114. if( $cnt > 500*30 ) $cnt = 500*30;
  115. echo \yii\widgets\ListView::widget([
  116. "dataProvider" => new \yii\data\ActiveDataProvider([
  117. "query"=>$news_query,
  118. "totalCount"=>$cnt,
  119. "pagination" =>[
  120. "pageSize"=>30
  121. ],
  122. ]),
  123. 'options' => [
  124. 'tag' => "ul",
  125. 'class' => 'list-wrapper',
  126. 'id' => 'list-wrapper',
  127. ],
  128. "itemOptions"=>[
  129. "tag"=>"li"
  130. ],
  131. "itemView" => "archive/_archive_item",
  132. "layout" => "{items}{pager}"
  133. ]);
  134. }
  135. ?>
  136. </div>
  137. </div>
  138. </div>
  139. </div>
  140. <?
  141. $Js = <<< 'JS'
  142. jQuery(function () {
  143. $('#tag').autocomplete({
  144. serviceUrl: '/tags/search',
  145. minChars:3,
  146. noCache:true,
  147. deferRequestBy:3,
  148. triggerSelectOnValidInput:false,
  149. onSelect: function (suggestion) {
  150. window.location.href = window.location.href.split('?')[0]+'?tag='+suggestion.data;
  151. }
  152. });
  153. $('#story').autocomplete({
  154. serviceUrl: '/story/search',
  155. minChars:3,
  156. noCache:true,
  157. triggerSelectOnValidInput:false,
  158. onSelect: function (suggestion) {
  159. window.location.href = window.location.href.split('?')[0]+'?story='+suggestion.data;
  160. }
  161. });
  162. });
  163. JS;
  164. $this->registerJs($Js, View::POS_READY);
  165. ?>
  166. <script>
  167. function doSelect(e,w){
  168. var url = new URL(w.location.href);
  169. var q = url.searchParams.get("q");
  170. if( q == null ) q = '';
  171. switch (e.value*1) {
  172. case 0:
  173. w.location.href = w.location.href.split('?')[0]+'?q='+q;
  174. break;
  175. case 1:
  176. var now = new Date(Date.now());
  177. var begin = new Date(Date.now()-7*24*60*60*1000);
  178. var daterange = begin.getDate()+'.'+(begin.getMonth()+1)+'.'+begin.getFullYear()+'+—+'+now.getDate()+'.'+(now.getMonth()+1)+'.'+now.getFullYear();
  179. w.location.href = w.location.href.split('?')[0]+'?q='+q+'&daterange='+daterange;
  180. break;
  181. case 2:
  182. var now = new Date(Date.now());
  183. var begin = new Date(Date.now()-30*24*60*60*1000);
  184. var daterange = begin.getDate()+'.'+(begin.getMonth()+1)+'.'+begin.getFullYear()+'+—+'+now.getDate()+'.'+(now.getMonth()+1)+'.'+now.getFullYear();
  185. w.location.href = w.location.href.split('?')[0]+'?q='+q+'&daterange='+daterange;
  186. break;
  187. case 3:
  188. var now = new Date(Date.now());
  189. var begin = new Date(Date.now()-365*24*60*60*1000);
  190. var daterange = begin.getDate()+'.'+(begin.getMonth()+1)+'.'+begin.getFullYear()+'+—+'+now.getDate()+'.'+(now.getMonth()+1)+'.'+now.getFullYear();
  191. w.location.href = w.location.href.split('?')[0]+'?q='+q+'&daterange='+daterange;
  192. break;
  193. }
  194. }
  195. </script>
  196. <style>
  197. .autocomplete-suggestions { border: 1px solid #999; background: #FFF; overflow: auto; }
  198. .autocomplete-suggestion { padding: 2px 5px; white-space: nowrap; overflow: hidden; }
  199. .autocomplete-selected { background: #F0F0F0; }
  200. .autocomplete-suggestions strong { font-weight: normal; color: #3399FF; }
  201. .autocomplete-group { padding: 2px 5px; }
  202. .autocomplete-group strong { display: block; border-bottom: 1px solid #000; }
  203. </style>