Код: Выделить всё
public function actionIndex34534()
{
if (isset($_GET['sort']) && in_array($_GET['sort'], ['created_at', 'like_cnt'])):
setcookie('nl-sort', $_GET['sort'], time() + 86400 * 30, '/');
return $this->redirect('/news-list/');
endif;
$sort = isset($_COOKIE['nl-sort']) && in_array($_COOKIE['nl-sort'], ['created_at', 'like_cnt']) ? $_COOKIE['nl-sort'] : 'created_at';
$uid = 0;
if (!\Yii::$app->user->isGuest)
$uid = \Yii::$app->user->identity->id;
$query = NewsTheme::find()
->where(['status' => NewsTheme::STATUS_ACTIVE])
->orderBy([$sort => SORT_DESC]);
$pages = new Pagination(['totalCount' => $query->count(), 'pageSize' => 5, 'forcePageParam' => false, 'pageSizeParam' => false]);
$posts = $query->offset($pages->offset)
->limit($pages->limit)
->all();
$programs = [];
$articles = [];
$authors = [];
$right_articles = [];
$popular_themes = [];
$pids = [];
$program_comments = [];
$author_ids = [];
$post_ids = [];
$program__last_liked = [];
foreach ($posts as $post):
$post_ids[] = $post->id;
$ptont = $post->programToNewsThemes;
//$atont = $post->articleToNewsThemes;
foreach ($ptont as $v):
$programs[$post->id][] = $v->program;
$pids[] = $v->program->id;
$program_comments[$v->program->id] = DevHelper::getComments('program', $v->program->id);
$liked = Like::find()
->where(['entity_type' => 'program', 'entity_id' => $v->program->id])
->orderBy(['id' => SORT_DESC])
->limit(3)
->all();
foreach ($liked as $l)
$program__last_liked[$l->entity_id][] = $l->user;
endforeach;
/*
foreach($atont as $v):
$articles[$post->id][] = $v->article;
endforeach;
*/
endforeach;
$articles = Publication::find()
->where(['=', 'status', Publication::STATUS_ACTIVE])
->andWhere(['=', 'is_interesting', 1])
->orderBy(new Expression('rand()'))
->all();
$author_query = Publication::find()
->where(['=', 'status', Publication::STATUS_ACTIVE])
->andWhere(['>', 'user_id', 0])
//->groupBy(['user_id'])
->limit(10)
->all();
foreach ($author_query as $aq):
$authors[] = $aq->user;
endforeach;
//$right_articles = ContentArticles::find()
$right_articles = Publication::find()
//->orderBy(new Expression('rand()'))
->orderBy(['id' => SORT_DESC])
->limit(3)
->all();
$popular_themes = NewsTheme::find()
->orderBy(['like_cnt' => SORT_DESC])
->limit(5)
->all();
//////recomend news_theme
$minus_from = strtotime(date('d.m.Y')) - 5 * 86400;
$minus_to = strtotime(date('d.m.Y')) + 5 * 86400 + 23 * 3600 + 59 * 60 + 59;
$rnt = NewsTheme::find()
->where(['>=', 'created_at', $minus_from])
->andWhere(['<=', 'created_at', $minus_to])
->orderBy(new Expression('rand()'))
->one();
$rp0 = $rnt->programToNewsThemes;
$rp = [];
foreach ($rp0 as $r)
$rp[] = $r->program;
usort($rp, function ($a, $b) {
return $a->like_cnt <= $b->like_cnt;
});
$rp = array_slice($rp, 0, 3);
/*
$liked_programs_minus_days = Like::find()
->where(['>=','created_at',$minus_from])
->andWhere(['<=','created_at',$minus_to])
->andWhere(['=','entity_type','program'])
->all();
$rprogram_ids = [];
foreach($liked_programs_minus_days as $v)
$rprogram_ids[$v->entity_id] = isset($rprogram_ids[$v->entity_id]) ? $rprogram_ids[$v->entity_id]++ : 1;
$rp = [];
if(count($rprogram_ids)):
$maxs = array_keys($rprogram_ids, max($rprogram_ids));
$maxs = array_slice($maxs, 0, 3);
if(count($maxs) && $maxs[0]):
$rp = Programs::find()
->where(['IN','id',$maxs])
//->orderBy(['like_cnt'=>SORT_DESC])
//->orderBy(new Expression('rand()'))
//->limit(3)
->all();
endif;
endif;
*/
//echo (int)\Yii::$app->user->isGuest;
//////////////////////
foreach ($rp as $v):
$pids[] = $v->id;
$program_comments[$v->id] = DevHelper::getComments('program', $v->id);
endforeach;
$program_images = [];
$pimg = ProgramsImages::find()
->where(['IN', 'program_id', $pids])
->orderBy(['priority' => SORT_ASC])
->all();
foreach ($pimg as $p)
$program_images[$p->program_id][] = $p['src'];
$my_reviews = [];
$cnt_reviews = [];
$rating_reviews = [];
foreach ($pids as $v):
$cnt_reviews[$v] = 0;
$rating_reviews[$v] = 0;
endforeach;
$rr = \common\models\Reviews::find()
->where(['IN', 'program_id', $pids])
->andWhere(['=', 'user_id', $uid])
->all();
foreach ($rr as $r)
$my_reviews[] = $r->program_id;
$rr = \common\models\Reviews::find()
->where(['IN', 'program_id', $pids])
->all();
foreach ($rr as $r):
$cnt_reviews[$r->program_id]++;
$r0 = $r->rating_common + $r->rating_convenience + $r->rating_functions + $r->rating_support;
$r0 = $r0 / 4;
$rating_reviews[$r->program_id] += $r0;
endforeach;
foreach ($rating_reviews as $k => $v):
$rating_reviews[$k] = $cnt_reviews[$k] ? number_format($v / $cnt_reviews[$k], 1, '.', ' ') : 0;
endforeach;
$my_likes = [];
if (!Yii::$app->user->isGuest):
$my_like = Like::find()->where(['user_id' => $uid,/*'entity_type'=>'news_theme'*/])->all();
foreach ($my_like as $ml)
$my_likes[$ml->entity_type][] = $ml->entity_id;
//$my_likes = array_unique($my_likes);
endif;
return $this->render('index', [
'posts' => $posts,
'my_likes' => $my_likes,
'programs' => $programs,
'articles' => $articles,
'authors' => $authors,
'right_articles' => $right_articles,
'popular_themes' => $popular_themes,
'rnt' => $rnt, //recomend news_theme
'rp' => $rp, //recomend programs
'program_images' => $program_images,
'program_comments' => $program_comments,
'my_reviews' => $my_reviews,
'cnt_reviews' => $cnt_reviews,
'rating_reviews' => $rating_reviews,
'program__last_liked' => $program__last_liked,
'sort' => $sort,
]);
}