예제 #1
0
 /**
  * @return News
  */
 private function getVideo()
 {
     if (empty($this->video)) {
         $this->video = News::find()->where(['categoryID' => 49])->orderBy('publishDate DESC')->limit(1)->one();
     }
     return $this->video;
 }
예제 #2
0
 /**
  * @param $id
  * @return null|static
  * @throws NotFoundHttpException
  */
 protected function findModel($id)
 {
     if (($model = News::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
예제 #3
0
 public function actionGet($newsID)
 {
     $news = News::findOne($newsID);
     if (empty($news)) {
         throw new NotFoundHttpException("Не найдена новость с идентификатором {$newsID}");
     }
     if (\Yii::$app->request->isAjax) {
         \Yii::$app->session->setFlash('updated', 'Список комментариев обновлён!');
     }
     return $this->renderPartial('comments', ['news' => $news, 'comments' => new ActiveDataProvider(['query' => $news->getComments()->with('votes'), 'sort' => ['defaultOrder' => ['date' => SORT_ASC]]])]);
 }
예제 #4
0
 public function actionGet_news()
 {
     // \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
     $response = [];
     // return $response;
     $news_data = News::get_news_1();
     $response['home-news'] = $news_data;
     $fp = fopen('json/news.json', 'w');
     fwrite($fp, \yii\helpers\Json::encode($response));
     fclose($fp);
     return $this->redirect(['/']);
 }
예제 #5
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = News::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to return any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'category_id' => $this->category_id, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]);
     $query->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'description', $this->description])->andFilterWhere(['like', 'content', $this->content]);
     return $dataProvider;
 }
예제 #6
0
 private function getNews()
 {
     $news = News::find();
     if ($this->category) {
         $news->andWhere(['categoryID' => $this->category]);
     }
     if ($this->orderBy) {
         $news->orderBy($this->orderBy);
     }
     if ($this->limit) {
         $news->limit($this->limit);
     }
     return $news->with('category')->all();
 }
예제 #7
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = News::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to return any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id]);
     $query->andFilterWhere(['like', 'header', $this->header])->andFilterWhere(['like', 'content', $this->content])->andFilterWhere(['like', 'image', $this->image]);
     return $dataProvider;
 }
예제 #8
0
파일: News.php 프로젝트: hsleonis/basetech
 public static function get_news_1()
 {
     $options = [];
     $datas = [];
     $news_data_r = News::find()->where(['status' => 1])->all();
     if (!empty($news_data_r)) {
         $i = 0;
         foreach ($news_data_r as $key => $value) {
             $options[$i]['id'] = $value->id;
             $options[$i]['title'] = $value->title;
             $options[$i]['slug'] = $value->slug;
             $options[$i]['desc'] = $value->desc;
             $options[$i]['ext_url'] = $value->ext_url;
             $i++;
         }
     }
     return $options;
 }
예제 #9
0
 public function getPossibleNews($count, $outcludeIDs = [])
 {
     $categoryPossibleNews = ArrayHelper::getColumn(News::find()->select('id')->andWhere(['categoryID' => $this->id])->asArray()->all(), 'id');
     $randomNewsCount = ($c = count($categoryPossibleNews)) && $c > $count ? $count * 2 : $c;
     $randomNews = $randomNewsCount <= 1 ? [0] : array_rand($categoryPossibleNews, $randomNewsCount > $c ? $c : $randomNewsCount);
     $randomNewsIDs = [];
     if (!is_array($randomNews)) {
         $randomNews = [$randomNews];
     }
     foreach ($randomNews as $key) {
         $randomNewsIDs[] = $categoryPossibleNews[$key];
     }
     if (!empty($outcludeIDs)) {
         if (!is_array($outcludeIDs)) {
             $outcludeIDs = [$outcludeIDs];
         }
         $randomNewsIDs = array_diff($randomNewsIDs, $outcludeIDs);
     }
     return News::find()->andWhere(['in', 'id', $randomNewsIDs])->with('category')->limit($count)->all();
 }
예제 #10
0
 public function search()
 {
     $ids = [];
     $activeDataProvider = new ActiveDataProvider(['sort' => false, 'pagination' => ['pageSize' => 10]]);
     $solr = new Solr(['rowsCount' => 10000]);
     $result = $solr->find($this->terms, ['id']);
     if ($result && ($result = json_decode($result))) {
         $this->resultsCount = $result->response->numFound;
         foreach ($result->response->docs as $oneResult) {
             $ids[] = $oneResult->id;
         }
     }
     $sort = null;
     $searchQuery = News::find()->where(['in', 'id', $ids]);
     if ($ids) {
         switch ($this->order) {
             case 'newest':
                 $sort = ['defaultOrder' => ['publishDate' => SORT_DESC]];
                 break;
             case 'oldest':
                 $sort = ['defaultOrder' => ['publishDate' => SORT_ASC]];
                 break;
             case 'popular':
                 $sort = ['defaultOrder' => ['hits' => SORT_DESC]];
                 break;
             case 'alphabet':
                 $sort = ['defaultOrder' => ['title' => SORT_ASC]];
                 break;
             case 'relative':
             default:
                 $searchQuery->orderBy(new Expression('FIELD(`id`, ' . implode(', ', $ids) . ')'));
                 break;
         }
     }
     $activeDataProvider->query = $searchQuery;
     if ($sort) {
         $activeDataProvider->setSort($sort);
     }
     return $activeDataProvider;
 }
예제 #11
0
 public function getNewses($name = '经济')
 {
     return $this->hasMany(News::className(), ['category_id' => 'id'])->where('title = :title', [':title' => $name]);
 }
예제 #12
0
 public static function IdToCategory()
 {
     $news = News::find()->where(['category_id' => 3])->one();
     //$category = $news->category;
     var_dump($news);
 }
예제 #13
0
파일: popular.php 프로젝트: BoBRoID/new.k-z
<?php

use frontend\helpers\ConfigurationHelper;
use frontend\models\News;
?>
<div class="box nsp blue sidebar">
    <div>
        <h3 class="header">
            <span>Популярное</span>
        </h3>
        <div class="content">
            <div class="nspMain popular-list">
                <div class="nspArts">
                    <div class="nspArtScroll1">
                        <div class="nspArtScroll2">
                            <div class="nspArtPage">
                                <?php 
foreach (News::getPopular(ConfigurationHelper::getValue('popularLength')) as $popularNews) {
    echo $this->render('_popular_news_item', ['news' => $popularNews]);
}
?>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
예제 #14
0
 public function actionNewsDetail($id)
 {
     $modelSocial = Social::findOne(1);
     $model = News::findOne($id);
     return $this->render('news-detail', ['model' => $model, 'modelSocial' => $modelSocial]);
 }
예제 #15
0
 public function actionNews($id, $ignoreLink = false)
 {
     $news = News::find()->andWhere(['id' => filter_var($id, FILTER_SANITIZE_NUMBER_INT)])->with('author')->one();
     if (!$news) {
         throw new NotFoundHttpException();
     }
     if ($news->fullLink != \Yii::$app->request->url && !$ignoreLink) {
         return $this->redirect($news->fullLink, 301);
     }
     if (\Yii::$app->request->isAjax) {
         \Yii::$app->response->format = 'json';
         switch (\Yii::$app->request->post('action')) {
             case 'voteComment':
                 $commentID = \Yii::$app->request->post('commentID');
                 $comment = Comment::findOne($commentID);
                 if (!$comment) {
                     throw new NotFoundHttpException("Комментарий с идентификатором {$commentID} не найден!");
                 }
                 $isGood = \Yii::$app->request->post('isGood') == 'true';
                 if (!empty($comment->myVote) && $comment->myVote->operation == ($isGood ? CommentVote::OPERATION_GOOD : CommentVote::OPERATION_BAD)) {
                     return ['result' => $comment->rating];
                 }
                 $comment->vote($isGood);
                 $comment = Comment::findOne($commentID);
                 return ['result' => $comment->rating];
                 break;
         }
     }
     if ($date = \Yii::$app->request->headers->get('If-Modified-Since', 0)) {
         if ($date = strtotime($date) && ($date >= $news->updated || $date >= $news->publishDate)) {
             header('HTTP/1.1 304 Not Modified');
             die;
         }
     }
     \Yii::$app->response->headers->add('Last-Modified', gmdate('D, d M Y H:i:s', $news->updated ? $news->updated : $news->publishDate) . ' GMT');
     if (!preg_match('/(googlebot|google.com\\/bot|yandex(\\w+|)bot|yandex\\.com\\/bots)/im', \Yii::$app->request->userAgent)) {
         (new Query())->createCommand(\Yii::$app->db)->setSql("UPDATE `news` SET `hits` = `hits` + 1 WHERE `id` = '{$news->id}'")->execute();
         NewsViews::addView($news->id);
     }
     $commentForm = new CommentForm();
     if (\Yii::$app->request->post('CommentForm')) {
         $commentForm->load(\Yii::$app->request->post());
         $commentForm->news = $news;
         if ($commentForm->save()) {
             \Yii::$app->session->setFlash('saved', 'Комментарий успешно добавлен!' . ($news->moderatedComments == 1 ? ' Он будет опубликован после проверки модератором.' : ''));
             $commentForm = new CommentForm();
         }
     }
     if (!empty($news->category)) {
         if (!empty($news->category->parentCategory)) {
             $this->getView()->params['breadcrumbs'][] = ['label' => $news->category->parentCategory->title, 'url' => yii\helpers\Url::toRoute(['/' . $news->category->parentCategory->fullLink], true)];
         }
         $this->getView()->params['breadcrumbs'][] = ['label' => $news->category->title, 'url' => yii\helpers\Url::toRoute(['/' . $news->category->fullLink], true)];
     }
     $today = strtotime(date('Y-m-d'));
     $interestNews = [];
     if ($news->category->showPopular) {
         $interestNews = NewsViews::find()->select('newsID')->where(['date' => $today])->andWhere(['not in', 'newsID', $news->id])->orderBy('views DESC')->limit(5)->asArray()->all();
         if (empty($interestNews)) {
             $interestNews = NewsViews::find()->select('newsID')->where(['date' => $today - 86400])->andWhere(['not in', 'newsID', $news->id])->orderBy('views DESC')->limit(5)->asArray()->all();
         }
         $interestNews = News::find()->andWhere(['in', 'id', ArrayHelper::getColumn($interestNews, 'newsID')])->andWhere(['not in', 'categoryID', '49'])->with('category')->limit(3)->all();
     }
     return $this->render('article', ['article' => $news, 'interest' => $interestNews, 'commentForm' => $commentForm, 'categoryNews' => $news->category->showSimilar ? $news->category->getPossibleNews(3, [$news->id]) : []]);
 }
 public function actionTest()
 {
     News::IdToCategory();
     die;
 }
예제 #17
0
<?php

$showedNewsIDs = [];
foreach ($showedNews as $news) {
    $showedNewsIDs[] = $news->id;
}
$lastMonth = time() - 86400 * 30;
$newsQuery = \frontend\models\News::find()->with('category')->where(['categoryID' => $category->id])->andWhere(['not in', 'id', $showedNewsIDs])->andWhere("publishDate >= '{$lastMonth}'")->limit('4')->orderBy('hits DESC');
if ($newsModels = $newsQuery->all()) {
    ?>
<div class="items-more">
    <h3>Это может быть интересно:</h3>
    <ol>
    <?php 
    foreach ($newsModels as $news) {
        echo \yii\helpers\Html::tag('li', \yii\helpers\Html::a($news->title, \yii\helpers\Url::to($news->fullLink)), ['class' => 'longHeader']);
    }
    ?>
    </ol>
</div>
<?php 
}