DEV: Упростим контроллер

This commit is contained in:
Evg 2023-10-19 03:38:52 +03:00
parent 59b3c5be08
commit 415fc47cb5
2 changed files with 19 additions and 26 deletions

View file

@ -8,16 +8,8 @@ use App\Models\HomeModel;
class HomeController extends Controller
{
protected $limit = 15;
public function index($sheet)
{
$latest_answers = HomeModel::latestAnswers();
$topics_user = HomeModel::subscription();
$pagesCount = HomeModel::feedCount($topics_user, $sheet);
$posts = HomeModel::feed($this->pageNumber, $this->limit, $topics_user, $sheet);
$items = HomeModel::latestItems(3); // (LIMIT)
// Topics signed by the participant. If a guest, then default.
// Темы на которые подписан участник. Если гость, то дефолтные.
$topics = \App\Models\FacetModel::advice();
@ -27,15 +19,15 @@ class HomeController extends Controller
[
'meta' => Home::metadata($sheet),
'data' => [
'pagesCount' => ceil($pagesCount / $this->limit),
'pagesCount' => HomeModel::feedCount($sheet),
'pNum' => $this->pageNumber,
'sheet' => $sheet,
'type' => 'main',
'latest_answers' => $latest_answers,
'topics_user' => $topics_user,
'posts' => $posts,
'topics' => $topics,
'items' => $items,
'type' => 'main',
'latest_answers' => HomeModel::latestAnswers(),
'topics_user' => HomeModel::subscription(),
'posts' => HomeModel::feed($this->pageNumber, $sheet),
'items' => HomeModel::latestItems(),
],
],
);
@ -45,10 +37,9 @@ class HomeController extends Controller
// Бесконечный скролл
public function scroll()
{
$type = Request::get('type') == 'all' ? 'all' : 'main.feed';
$type = Request::get('type') == 'all' ? 'all' : 'main.feed';
$topics_user = HomeModel::subscription();
$posts = HomeModel::feed($this->pageNumber, $this->limit, $topics_user, $type);
$posts = HomeModel::feed($this->pageNumber, $type);
$this->insert(
'/content/post/type-post',
@ -56,7 +47,7 @@ class HomeController extends Controller
'data' => [
'pages' => $this->pageNumber,
'sheet' => 'main.feed',
'posts' => $posts,
'posts' => $posts, // $posts = empty($posts) ? 'null' : $posts;
]
]

View file

@ -9,14 +9,16 @@ use DB;
class HomeModel extends \Hleb\Scheme\App\Models\MainModel
{
public static $limit = 15;
// Posts on the central page
// Посты на центральной странице
public static function feed($page, $limit, $topics_user, $type)
public static function feed($page, $type)
{
$user_id = UserData::getUserId();
$result = [];
foreach ($topics_user as $ind => $row) {
foreach (self::subscription() as $ind => $row) {
$result[$ind] = $row['facet_id'];
}
@ -42,7 +44,7 @@ class HomeModel extends \Hleb\Scheme\App\Models\MainModel
$nsfw = UserData::getUserNSFW() ? "" : "AND post_nsfw = 0";
$start = ($page - 1) * $limit;
$start = ($page - 1) * self::$limit;
$sql = "SELECT DISTINCT
post_id,
post_title,
@ -91,13 +93,13 @@ class HomeModel extends \Hleb\Scheme\App\Models\MainModel
LEFT JOIN votes_post
ON votes_post_item_id = post_id AND votes_post_user_id = :uid2
WHERE $ignoring post_type != 'page' AND post_draft = 0 $nsfw $string $display $sort LIMIT :start, :limit";
return DB::run($sql, ['uid' => $user_id, 'uid2' => $user_id, 'start' => $start, 'limit' => $limit])->fetchAll();
return DB::run($sql, ['uid' => $user_id, 'uid2' => $user_id, 'start' => $start, 'limit' => self::$limit])->fetchAll();
}
public static function feedCount($topics_user, $type)
public static function feedCount($type)
{
$result = [];
foreach ($topics_user as $ind => $row) {
foreach (self::subscription() as $ind => $row) {
$result[$ind] = $row['facet_id'];
}
@ -136,7 +138,7 @@ class HomeModel extends \Hleb\Scheme\App\Models\MainModel
INNER JOIN users ON id = post_user_id
WHERE $ignoring post_draft = 0 $nsfw $string $display";
return DB::run($sql)->rowCount();
return ceil(DB::run($sql)->rowCount() / self::$limit);
}
public static function display($type)
@ -201,7 +203,7 @@ class HomeModel extends \Hleb\Scheme\App\Models\MainModel
return DB::run($sql)->fetchAll();
}
public static function latestItems($limit)
public static function latestItems($limit = 3)
{
$sql = "SELECT item_id, item_title, item_slug, item_domain FROM items WHERE item_published = 1 AND item_is_deleted = 0 ORDER BY item_id DESC LIMIT :limit";