newsList.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. use app\models\Authors;
  3. use yii\sphinx\Query;
  4. use yii\web\View;
  5. $this->title="Новости";
  6. //тест (заменить на новостьи дня и по календарику)
  7. /*
  8. $newsDataProvider = new \yii\data\ActiveDataProvider(
  9. [
  10. "query"=>\app\models\News::getMainView()->orderBy(["dt_pub"=>SORT_DESC])->limit(50),
  11. "pagination" =>[
  12. "pageSize"=>50
  13. ],
  14. ]
  15. );
  16. */
  17. if( isset( $_GET['search'] ) && trim( $_GET['search'] ) != ''){
  18. $news_query = \app\models\base\News::find()->joinWith('topics t');
  19. $news_query->alias('n')->andWhere([
  20. "n.id"=>(new Query())
  21. ->from('amic_news')
  22. ->select(['id'])
  23. ->addOptions(['max_matches' => 200])
  24. ->addOptions(['field_weights'=> ['title' => 10, 'body' => 8, 'lead'=>5] ])
  25. ->match($_GET['search'])
  26. ->orderBy(["dt_pub"=>SORT_DESC])
  27. ->limit(200)
  28. ->column()
  29. ]);
  30. $newsDataProvider1 = new \yii\data\ActiveDataProvider(
  31. [
  32. "query"=>$news_query,
  33. "pagination" =>[
  34. "pageSize"=>200
  35. ],
  36. ]
  37. );
  38. }
  39. if (Yii::$app->request->isGet && $get = Yii::$app->request->get('dt')){
  40. $b_date = date("Y-m-d 00:00:00", strtotime($get));
  41. $e_date = date("Y-m-d 23:59:59", strtotime($get));
  42. Yii::$app->session->set('dt-newslistadm', $get);
  43. }elseif( $get = Yii::$app->session->get('dt-newslistadm') ){
  44. $b_date = date("Y-m-d 00:00:00", strtotime($get));
  45. $e_date = date("Y-m-d 23:59:59", strtotime($get));
  46. }else{
  47. $b_date = date("Y-m-d 00:00:00");
  48. $e_date = date("Y-m-d 23:59:59");
  49. }
  50. $newsDataProvider = new \yii\data\ActiveDataProvider(
  51. [
  52. "query"=>\app\models\base\News::find()->andwhere(['between', 'dt_pub', $b_date , $e_date])->orderBy(["dt_pub"=>SORT_DESC])->limit(200),
  53. "pagination" =>[
  54. "pageSize"=>200
  55. ],
  56. ]
  57. );
  58. $cnt = $newsDataProvider->getCount();
  59. if( $cnt == 0 ){ //return; возможный вариант
  60. $newsDataProvider = new \yii\data\ActiveDataProvider(
  61. [
  62. "query"=>\app\models\base\News::find()->andwhere(['>', 'id', 0])->orderBy(["dt_pub"=>SORT_DESC])->limit(200),
  63. "pagination" =>[
  64. "pageSize"=>200
  65. ],
  66. ]
  67. );
  68. $cnt = $newsDataProvider->getCount();
  69. }
  70. $cache = Yii::$app->cache;
  71. $NewsItem = array();
  72. $prc = round($cnt*100/60);
  73. /**** ****/
  74. ?>
  75. <div class="row">
  76. <div class="info-box col-3 m-1">
  77. <span class="info-box-icon bg-info"><i class="far fa-copy"></i></span>
  78. <div class="info-box-content">
  79. <span class="info-box-text">Материалов</span>
  80. <span class="info-box-number"><?=$cnt?></span>
  81. <div class="progress">
  82. <div class="progress-bar bg-info" style="width: <?=$prc?>%"></div>
  83. </div>
  84. <span class="progress-description">
  85. <?=$prc?>% Среднее за день
  86. </span>
  87. </div>
  88. </div>
  89. <?
  90. $newsDataProvidera = new \yii\data\ActiveDataProvider(
  91. [
  92. "query"=>\app\models\base\News::find()->select(["SUBSTRING_INDEX(`editors`,',',1) as e"])->andwhere(['between', 'dt_pub', $b_date , $e_date])->groupBy(["e"]),
  93. "pagination" =>false
  94. ]
  95. );
  96. $cnt = $newsDataProvidera->getCount();
  97. $newsDataProvidera = new \yii\data\ActiveDataProvider(
  98. [
  99. "query"=>\app\models\base\News::find()->select('count(author)')->andwhere(['between', 'dt_pub', $b_date , $e_date])->andwhere(['<>', 'author', '5'])->groupBy(["author"]),
  100. "pagination" =>false
  101. ]
  102. );
  103. $cnt2 = $newsDataProvidera->getCount();
  104. $newsDataProvidera = new \yii\data\ActiveDataProvider(
  105. [
  106. "query"=>\app\models\base\News::find()->select('count(*)')->andwhere(['between', 'dt_pub', $b_date , $e_date])->andwhere(['<>', 'author', '5'])->andWhere(['show_author'=>'Y'])->groupBy(["author"]),
  107. "pagination" =>false
  108. ]
  109. );
  110. $cnt3 = $newsDataProvidera->getCount();
  111. ?>
  112. <div class="info-box col-3 m-1">
  113. <span class="info-box-icon bg-info"><i class="far fa-copy"></i></span>
  114. <div class="info-box-content">
  115. <span class="info-box-text">Авторов на ленте</span>
  116. <span class="info-box-number"><?=$cnt?> (подписаны материалы: <?=$cnt3?>)</span>
  117. <span class="info-box-number">
  118. <?=$cnt2?> Указаны авторы
  119. </span>
  120. </div>
  121. </div>
  122. </div>
  123. <div class="table-responsive-sm custom-range-badge">
  124. <table class="table table-hover table-Light">
  125. <thead class="bg-gray table-sm">
  126. <tr>
  127. <th scope="col" class="col-md-1 col-sm-2 col-xs-5">время</th>
  128. <th scope="col" class="col-md-1 col-sm-2 col-xs-5">фото</th>
  129. <th scope="col">заголовок</th>
  130. <th scope="col">статусы</th>
  131. <th scope="col">управление</th>
  132. </tr>
  133. </thead>
  134. <tbody>
  135. <?
  136. if( isset( $newsDataProvider1 ) && $newsDataProvider1){
  137. $items = array_reverse( $newsDataProvider1->getModels() );
  138. }else{
  139. $items = $newsDataProvider->getModels();
  140. }
  141. foreach( $items as $item ){
  142. // print_r($item->getTopics());
  143. $NewsItem[] = $item->id;
  144. $strTopics = '';
  145. foreach( $item->getTopics()->primaryModel->topics as $topic ){
  146. $strTopics = $strTopics." [".$topic->title."]";
  147. }
  148. $strbage = '';
  149. if( $item->verifed == 'Y' ) $strbage .= '<span class="badge badge-success font-weight-normal">проверено</span><br>';
  150. if( $item->top == 'Y' ) $strbage .= '<span class="badge badge-primary font-weight-normal">картина дня</span><br>';
  151. if( $item->NH == 'Y' ) $strbage .= '<span class="badge badge-info font-weight-normal">НЧ</span><br>';
  152. if( $item->comments == 'N' ) $strbage .= '<span class="badge badge-secondary font-weight-normal">no comments</span><br>';
  153. if( ($item->flags & 1) == 1 ) $strbage .= '<span class="badge badge-warning font-weight-normal">без главной</span><br>';
  154. if( $res = $cache->get("editNews_".$item->id) ){
  155. $strbage .= '<span class="badge badge-danger font-weight-normal author">'.unserialize($res)['author'].'</span>';
  156. }
  157. $author = Authors::getAuthor($item->author);
  158. $show = ($item->active == 'N' || strtotime($item->dt_pub) > time())?'preview/'.$item->uid:$item->id;
  159. $colorize = ($item->active == 'N' || strtotime($item->dt_pub) > time())?' class="table-secondary"':'';
  160. $colorize = ($item->active == 'Y' && strtotime($item->dt_pub) > time())?' class="table-info"':$colorize;
  161. $icon = $item->getImage()->getIcon();
  162. ?>
  163. <tr<?=$colorize?>>
  164. <th scope="row" class="text-nowrap"><?=date("H:i d-m-Y",strtotime($item->dt_pub))?></th>
  165. <td><img src="<?=$icon?>" onerror="this.src='/images/defaultsm.jpg'" width="94" height="auto"></td>
  166. <td><?=$item->title?><br><small><?=$strTopics?><br><?=str_replace(',',' ',$item->editors)?></small><small class="bg-olive px-1"><?=$author->name?></small></td>
  167. <td rel_id="<?=$item->id?>"><?=$strbage?></td>
  168. <td><div class="btn-group btn-group-toggle" data-toggle="a" role="group"><a href="update?id=<?=$item->id?>" title="Править" role="button" class="btn btn-secondary btn-sm"><i class="far fa-edit"></i></a><a href="newsshow?id=<?=$item->id?>" title="спрятать" role="button" class="btn btn-sm btn-secondary"><i class="fa <?=($item->active == 'N')?'fa-eye-slash':'fa fa-eye'?>"></i></a><a href="newskd?id=<?=$item->id?>" role="button" class="btn btn-sm <?=($item->top == 'N')?'btn-secondary':'btn-primary'?>">КД</a><a href="newsnh?id=<?=$item->id?>" role="button" class="btn <?=($item->NH == 'N')?'btn-secondary':'btn-primary'?> btn-sm">НЧ</a><a href="/news/<?=$show?>" title="открыть на сайте" role="button" class="btn btn-secondary btn-sm" target="_blank"><i class="far fa-window-restore"></i></a><?if(Yii::$app->user->can('deleteNews')){?><a href="delete?id=<?=$item->id?>" title="открыть на сайте" role="button" class="btn btn-secondary btn-sm" target="_blank" onclick="return confirm('Вы уверены?')"><i class="fa fa-trash"></i></a><?}?></div></td>
  169. </tr>
  170. <?
  171. // <pre>
  172. // print_r($item);
  173. // </pre>
  174. ?>
  175. <?
  176. }
  177. ?>
  178. </tbody>
  179. </table>
  180. </div>
  181. <?php
  182. $ni = serialize($NewsItem);
  183. $nij = json_encode($NewsItem);
  184. $script = <<< JS
  185. setInterval( function (){ testping() }, 10000 );
  186. function testping(){
  187. $.get( 'testping', { cmd: 'GetAuthors', data: '$ni' }, function(data){
  188. data = JSON.parse(data);
  189. if( data.status == 'ok' ){
  190. var dn = JSON.parse('$nij');
  191. $.each( dn, function( key, value ) {
  192. $("[rel_id='"+value+"'] .author").remove();
  193. $("[rel_id='"+value+"'] br:last-child").remove();
  194. });
  195. $.each( data['authors'], function( key, value ) {
  196. // $("[rel_id='"+key+"'] .author").remove();
  197. // $("[rel_id='"+key+"'] br:last-child").remove();
  198. $("[rel_id='"+key+"']").append("<span class='badge badge-danger font-weight-normal author'>"+value+"</span>");
  199. console.log(key);
  200. });
  201. }
  202. } );
  203. }
  204. JS;
  205. $this->registerJs( $script,View::POS_READY);
  206. //print_r($newsDataProvider->getModels());