public function actionIndex($slug)
 {
     $this->layout = 'category';
     $query = News::find();
     $category = Categories::findOne(['slug' => $slug]);
     if (!$category) {
         $this->redirect("/");
     }
     if ('all' != $slug) {
         $catArr = [];
         $catArr[] = $category->id;
         if ($subcatList = $category->child) {
             foreach ($subcatList as $subcat) {
                 $catArr[] = $subcat->id;
             }
         }
         $query->join('INNER JOIN', NewsHasCategory::tableName(), NewsHasCategory::tableName() . ".news_id = " . News::tableName() . ".id");
         $query->where([NewsHasCategory::tableName() . '.category_id' => $catArr]);
     }
     $query->orderBy(['id' => SORT_DESC]);
     $provider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 26]]);
     $breadCrumbs = [];
     $breadCrumbs[] = ['label' => 'Главная', 'url' => '/'];
     $breadCrumbs[] = ['label' => 'Категории', 'url' => '/category/all'];
     $breadCrumbs[] = ['label' => isset($category) ? $category->name : "Все новости"];
     Yii::$app->view->title = isset($category) ? $category->name : "Все новости";
     Yii::$app->view->registerMetaTag(['name' => 'keywords', 'content' => (isset($category) ? $category->name : "Все новости") . ' новости, свежие новости, новости украины, новости сегодня, агрегатор новостей']);
     Yii::$app->view->registerMetaTag(['name' => 'description', 'content' => 'Свежие новости со всех Украинских сайтов в одном сайте. Агрегатор новостей'], 'description');
     Yii::$app->view->registerMetaTag(['name' => 'og:title', 'content' => Yii::$app->view->title], 'og:title');
     return $this->render('index', ['provider' => $provider, 'slug' => $slug, 'category' => isset($category) ? $category->name : "Все новости", 'breadcrumbs' => $breadCrumbs]);
 }
 public function up()
 {
     $this->addColumn('news', 'unpublishDate', Schema::TYPE_DATETIME);
     $this->addColumn('news', 'checkedDate', Schema::TYPE_DATETIME);
     $this->addColumn('news', 'checked', Schema::TYPE_INTEGER . ' UNSIGNED NOT NULL DEFAULT 0');
     $this->addColumn('news', 'creator', Schema::TYPE_INTEGER . ' UNSIGNED NOT NULL DEFAULT 0');
     $this->addColumn('news', 'textPreview', $this->text());
     $this->addColumn('news', 'publishTimestamp', $this->integer()->unsigned()->notNull()->defaultValue(0));
     $this->createIndex('publishTimestamp', 'news', 'publishTimestamp');
     $this->execute("UPDATE `news` SET `publishTimestamp` = UNIX_TIMESTAMP(STR_TO_DATE(`publishDate`, '%Y-%m-%d %H:%i:%s'))");
     $this->alterColumn('news', 'author', Schema::TYPE_STRING);
     $newsList = new \yii\db\Query();
     $newsList = $newsList->from(\Yii::$app->params['oldDb'] . '.joom_content');
     $newsListCount = $newsList->count();
     echo "    > migrating news... Almost have {$newsListCount} news...\r\n";
     echo "    > prepare news...\r\n";
     $i = 0;
     foreach ($newsList->each(25) as $news) {
         $i++;
         echo "    > news {$i} from {$newsListCount}... \r\n";
         $newNews = new News(['id' => $news['id']]);
         $newNews->creator = $news['created_by'];
         $newNews->author = $news['created_by_alias'];
         $newNews->title = $news['title'];
         $newNews->textPreview = $news['introtext'];
         $newNews->text = $news['fulltext'];
         $newNews->link = $news['alias'];
         $newNews->published = $news['access'] == '1' ? 1 : 0;
         $newNews->deleted = $news['state'] == '-2' ? 1 : 0;
         $newNews->publishDate = $news['publish_up'];
         $newNews->favorite = $news['featured'];
         $newNews->hits = $news['hits'];
         $newNews->meta_description = $news['metadesc'];
         $newNews->meta_keywords = $news['metakey'];
         $newNews->category = $news['catid'];
         $newNews->unpublishDate = $news['publish_down'];
         $newNews->checkedDate = $news['checked_out_time'];
         $newNews->checked = $news['checked_out'];
         $newNews->image = $newNews->getImagePreview();
         $newNews->publishTimestamp = $news['publish_up'] ? strtotime($news['publish_up']) : 0;
         $newNews->publishTimestamp = $newNews->publishTimestamp < 0 ? 0 : $newNews->publishTimestamp;
         $prepared[] = $newNews->attributes;
         if (count($prepared) >= 50 || $newsListCount - $i == 0) {
             $this->batchInsert(News::tableName(), array_keys($prepared[0]), $prepared);
             $prepared = [];
         }
     }
 }
Exemple #3
0
 public function actionIndex()
 {
     Yii::$app->view->title = 'Свежие новости';
     Yii::$app->view->registerMetaTag(['name' => 'keywords', 'content' => 'новости, свежие новости, новости украины, новости сегодня, агрегатор новостей']);
     Yii::$app->view->registerMetaTag(['name' => 'description', 'content' => 'Свежие новости со всех Украинских сайтов в одном сайте. Агрегатор новостей'], 'description');
     Yii::$app->view->registerMetaTag(['name' => 'og:title', 'content' => Yii::$app->view->title], 'og:title');
     $this->layout = 'front';
     $notInIds = [];
     $sliders = News::find()->where("created_at BETWEEN NOW() - INTERVAL '6 hour' AND NOW() AND thumb  IS NOT NULL")->orderBy(["cnt" => SORT_DESC])->limit(8)->all();
     foreach ($sliders as $item) {
         $notInIds[] = $item->id;
     }
     $topNews = News::find()->where("created_at BETWEEN NOW() - INTERVAL '6 hour' AND NOW() AND thumb  IS NOT NULL  AND id NOT IN(" . implode(",", $notInIds) . ")")->orderBy(["cnt" => SORT_DESC])->limit(4)->all();
     foreach ($topNews as $item) {
         $notInIds[] = $item->id;
     }
     $hotNews = News::find()->where("thumb  IS NOT NULL AND id NOT IN(" . implode(",", $notInIds) . ")")->orderBy(["id" => SORT_DESC])->limit(4)->all();
     foreach ($hotNews as $item) {
         $notInIds[] = $item->id;
     }
     $category = Categories::findOne(['slug' => 'ato']);
     $atoNews = News::find()->joinWith(['newsHasCategories'])->where(NewsHasCategory::tableName() . ".category_id = {$category->id} AND " . News::tableName() . ".id NOT IN(" . implode(",", $notInIds) . ")")->orderBy(["id" => SORT_DESC])->limit(7)->all();
     foreach ($atoNews as $item) {
         $notInIds[] = $item->id;
     }
     $category = Categories::findOne(['slug' => 'economic']);
     $ecoNews = News::find()->joinWith(['newsHasCategories'])->where(NewsHasCategory::tableName() . ".category_id = {$category->id} AND " . News::tableName() . ".id NOT IN(" . implode(",", $notInIds) . ")")->orderBy(["id" => SORT_DESC])->limit(7)->all();
     foreach ($ecoNews as $item) {
         $notInIds[] = $item->id;
     }
     $category = Categories::findOne(['slug' => 'sport']);
     $sportNews = News::find()->joinWith(['newsHasCategories'])->where(NewsHasCategory::tableName() . ".category_id = {$category->id} AND " . News::tableName() . ".id NOT IN(" . implode(",", $notInIds) . ")")->orderBy(["id" => SORT_DESC])->limit(3)->all();
     foreach ($sportNews as $item) {
         $notInIds[] = $item->id;
     }
     $category = Categories::findOne(['slug' => 'politics']);
     $politicsNews = News::find()->joinWith(['newsHasCategories'])->where(NewsHasCategory::tableName() . ".category_id = {$category->id} AND " . News::tableName() . ".id NOT IN(" . implode(",", $notInIds) . ")")->orderBy(["id" => SORT_DESC])->limit(3)->all();
     return $this->render('index', ['topNews' => $topNews, 'hotNews' => $hotNews, 'atoNews' => $atoNews, 'ecoNews' => $ecoNews, 'sportNews' => $sportNews, 'politicsNews' => $politicsNews, 'sliders' => $sliders]);
 }