Skip to content

Commit

Permalink
Добавлены настройки главной страницы, вид отображения, кол. постов итд
Browse files Browse the repository at this point in the history
  • Loading branch information
visavi committed Dec 26, 2021
1 parent d419d96 commit e8c1164
Show file tree
Hide file tree
Showing 21 changed files with 337 additions and 76 deletions.
66 changes: 31 additions & 35 deletions app/Classes/Feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,78 +32,73 @@ public function __construct()
/**
* Get feed
*
* @param int $show
*
* @return HtmlString
*/
public function getFeed(int $show = 20): HtmlString
public function getFeed(): HtmlString
{
$topicsEnable = true;
$newsEnable = true;
$photosEnable = true;
$articlesEnable = true;
$downsEnable = true;
$itemsEnable = true;

$polls = [];
$collect = new Collection();


if ($topicsEnable) {
if (setting('feed_topics_show')) {
$topics = $this->getTopics();
$collect = $collect->merge($topics);

if ($this->user) {
$polls[Post::$morphName] = $this->getPolling($topics->pluck('last_post_id')->all(), Post::$morphName);
$ids = $topics->pluck('last_post_id')->all();
$polls[Post::$morphName] = $this->getPolling($ids, Post::$morphName);
}
}

if ($newsEnable) {
if (setting('feed_news_show')) {
$news = $this->getNews();
$collect = $collect->merge($news);

if ($this->user) {
$polls[News::$morphName] = $this->getPolling($news->pluck('id')->all(), News::$morphName);
$ids = $news->pluck('id')->all();
$polls[News::$morphName] = $this->getPolling($ids, News::$morphName);
}
}

if ($photosEnable) {
if (setting('feed_photos_show')) {
$photos = $this->getPhotos();
$collect = $collect->merge($photos);

if ($this->user) {
$polls[Photo::$morphName] = $this->getPolling($photos->pluck('id')->all(), Photo::$morphName);
$ids = $photos->pluck('id')->all();
$polls[Photo::$morphName] = $this->getPolling($ids, Photo::$morphName);
}
}

if ($articlesEnable) {
if (setting('feed_articles_show')) {
$articles = $this->getArticles();
$collect = $collect->merge($articles);

if ($this->user) {
$polls[Article::$morphName] = $this->getPolling($articles->pluck('id')->all(), Article::$morphName);
$ids = $articles->pluck('id')->all();
$polls[Article::$morphName] = $this->getPolling($ids, Article::$morphName);
}
}

if ($downsEnable) {
if (setting('feed_downs_show')) {
$downs = $this->getDowns();
$collect = $collect->merge($downs);

if ($this->user) {
$polls[Down::$morphName] = $this->getPolling($downs->pluck('id')->all(), Down::$morphName);
$ids = $downs->pluck('id')->all();
$polls[Down::$morphName] = $this->getPolling($ids, Down::$morphName);
}
}

if ($itemsEnable) {
if (setting('feed_items_show')) {
$collect = $collect->merge($this->getItems());
}

$posts = $collect
->sortByDesc('created_at')
->take(100);
->sortByDesc('created_at')
->take(setting('feed_total'));

$user = $this->user;
$posts = simplePaginate($posts, $show);
$posts = simplePaginate($posts, setting('feed_per_page'));
$allowDownload = $user || setting('down_guest_download');

return new HtmlString(view('widgets/_feed', compact('posts', 'polls', 'user', 'allowDownload')));
Expand Down Expand Up @@ -139,11 +134,11 @@ public function getTopics(): Collection
->select('topics.*', 'posts.created_at')
->join('posts', function ($join) {
$join->on('last_post_id', 'posts.id')
->where('posts.rating', '>', -3);
->where('posts.rating', '>', setting('feed_topics_rating'));
})
->orderByDesc('topics.updated_at')
->with('lastPost.user', 'lastPost.files', 'forum.parent')
->limit(20)
->limit(setting('feed_last_record'))
->get();
});
}
Expand All @@ -157,9 +152,9 @@ public function getNews(): Collection
{
return Cache::remember('NewsFeed', 600, static function () {
return News::query()
->where('rating', '>', -3)
->where('rating', '>', setting('feed_news_rating'))
->orderByDesc('created_at')
->limit(20)
->limit(setting('feed_last_record'))
->with('user')
->get();
});
Expand All @@ -174,9 +169,9 @@ public function getPhotos(): Collection
{
return Cache::remember('PhotoFeed', 600, static function () {
return Photo::query()
->where('rating', '>', -3)
->where('rating', '>', setting('feed_photos_rating'))
->orderByDesc('created_at')
->limit(20)
->limit(setting('feed_last_record'))
->with('user', 'files')
->get();
});
Expand All @@ -191,8 +186,9 @@ public function getArticles(): Collection
{
return Cache::remember('ArticleFeed', 600, static function () {
return Article::query()
->where('rating', '>', setting('feed_downs_rating'))
->orderByDesc('created_at')
->limit(20)
->limit(setting('feed_last_record'))
->with('user', 'files', 'category.parent')
->get();
});
Expand All @@ -208,9 +204,9 @@ public function getDowns(): Collection
return Cache::remember('DownFeed', 600, static function () {
return Down::query()
->where('active', 1)
->where('rating', '>', -3)
->where('rating', '>', setting('feed_downs_rating'))
->orderByDesc('created_at')
->limit(20)
->limit(setting('feed_last_record'))
->with('user', 'files', 'category.parent')
->get();
});
Expand All @@ -227,7 +223,7 @@ public function getItems(): Collection
return Item::query()
->where('expires_at', '>', SITETIME)
->orderByDesc('created_at')
->limit(20)
->limit(setting('feed_last_record'))
->with('user', 'files', 'category.parent')
->get();
});
Expand Down
3 changes: 2 additions & 1 deletion app/Http/Controllers/Forum/TopicController.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ public function create(int $id, Request $request, Validator $validator, Flood $f
if ($validator->isValid()) {
$msg = antimat($msg);

if ($post
if (
$post
&& $post->created_at + 600 > SITETIME
&& $user->id === $post->user_id
&& (utfStrlen($msg) + utfStrlen($post->text) <= setting('forumtextlength'))
Expand Down
7 changes: 4 additions & 3 deletions app/Http/Controllers/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ class PageController extends Controller
*/
public function index(string $page = 'index'): View
{
if ($page === 'menu' ||
! preg_match('|^[a-z0-9_\-]+$|i', $page) ||
! file_exists(resource_path('views/main/' . $page . '.blade.php'))
if (
$page === 'menu'
|| ! preg_match('|^[a-z0-9_\-]+$|i', $page)
|| ! file_exists(resource_path('views/main/' . $page . '.blade.php'))
) {
abort(404);
}
Expand Down
6 changes: 3 additions & 3 deletions app/Http/Middleware/AuthenticateCookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ public function handle(Request $request, Closure $next)
private function cookieAuth(Request $request): void
{
if (
$request->hasCookie('login') &&
$request->hasCookie('password') &&
$request->session()->missing('id')
$request->hasCookie('login')
&& $request->hasCookie('password')
&& $request->session()->missing('id')
) {
$login = $request->cookie('login');
$password = $request->cookie('password');
Expand Down
12 changes: 6 additions & 6 deletions app/Http/Middleware/CheckAccessSite.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public function handle(Request $request, Closure $next)

// Сайт закрыт для гостей
if (
setting('closedsite') === 1 &&
! getUser() &&
! $request->is('login', 'register', 'recovery', 'captcha')
setting('closedsite') === 1
&& ! getUser()
&& ! $request->is('login', 'register', 'recovery', 'captcha')
) {
setFlash('danger', __('main.not_authorized'));

Expand All @@ -41,9 +41,9 @@ public function handle(Request $request, Closure $next)

// Сайт закрыт для всех
if (
setting('closedsite') === 2 &&
! isAdmin() &&
! $request->is('login', 'captcha', 'closed')
setting('closedsite') === 2
&& ! isAdmin()
&& ! $request->is('login', 'captcha', 'closed')
) {
return redirect('closed');
}
Expand Down
1 change: 1 addition & 0 deletions app/Models/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public static function getActions(): array
'files',
'stickers',
'offers',
'feeds',
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function up(): void
*/
public function down(): void
{
Schema::table('errors', function (Blueprint $table) {
Schema::table('downs', function (Blueprint $table) {
$table->integer('rated')->default(0)->after('rating');
});
}
Expand Down
118 changes: 118 additions & 0 deletions database/upgrades/2021_12_25_041047_add_feed_to_settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

declare(strict_types=1);

use App\Models\Setting;
use Illuminate\Database\Migrations\Migration;

final class AddFeedToSettings extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Setting::query()->where('name', 'homepage_view')->updateOrCreate([], [
'name' => 'homepage_view',
'value' => 'feed',
]);

Setting::query()->where('name', 'feed_topics_show')->updateOrCreate([], [
'name' => 'feed_topics_show',
'value' => 1,
]);

Setting::query()->where('name', 'feed_news_show')->updateOrCreate([], [
'name' => 'feed_news_show',
'value' => 1,
]);

Setting::query()->where('name', 'feed_photos_show')->updateOrCreate([], [
'name' => 'feed_photos_show',
'value' => 1,
]);

Setting::query()->where('name', 'feed_articles_show')->updateOrCreate([], [
'name' => 'feed_articles_show',
'value' => 1,
]);

Setting::query()->where('name', 'feed_downs_show')->updateOrCreate([], [
'name' => 'feed_downs_show',
'value' => 1,
]);

Setting::query()->where('name', 'feed_items_show')->updateOrCreate([], [
'name' => 'feed_items_show',
'value' => 1,
]);

Setting::query()->where('name', 'feed_per_page')->updateOrCreate([], [
'name' => 'feed_per_page',
'value' => 20,
]);

Setting::query()->where('name', 'feed_last_record')->updateOrCreate([], [
'name' => 'feed_last_record',
'value' => 20,
]);

Setting::query()->where('name', 'feed_total')->updateOrCreate([], [
'name' => 'feed_total',
'value' => 100,
]);

Setting::query()->where('name', 'feed_topics_rating')->updateOrCreate([], [
'name' => 'feed_topics_rating',
'value' => -10,
]);

Setting::query()->where('name', 'feed_news_rating')->updateOrCreate([], [
'name' => 'feed_news_rating',
'value' => -10,
]);

Setting::query()->where('name', 'feed_photos_rating')->updateOrCreate([], [
'name' => 'feed_photos_rating',
'value' => -10,
]);

Setting::query()->where('name', 'feed_articles_rating')->updateOrCreate([], [
'name' => 'feed_articles_rating',
'value' => -10,
]);

Setting::query()->where('name', 'feed_downs_rating')->updateOrCreate([], [
'name' => 'feed_downs_rating',
'value' => -10,
]);


clearCache('settings');
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Setting::query()->where('name', 'homepage_view')->delete();
Setting::query()->where('name', 'feed_topics_show')->delete();
Setting::query()->where('name', 'feed_news_show')->delete();
Setting::query()->where('name', 'feed_photos_show')->delete();
Setting::query()->where('name', 'feed_articles_show')->delete();
Setting::query()->where('name', 'feed_downs_show')->delete();
Setting::query()->where('name', 'feed_items_show')->delete();
Setting::query()->where('name', 'feed_per_page')->delete();
Setting::query()->where('name', 'feed_last_record')->delete();
Setting::query()->where('name', 'feed_total')->delete();
Setting::query()->where('name', 'feed_topics_rating')->delete();
Setting::query()->where('name', 'feed_news_rating')->delete();
Setting::query()->where('name', 'feed_photos_rating')->delete();
Setting::query()->where('name', 'feed_articles_rating')->delete();
Setting::query()->where('name', 'feed_downs_rating')->delete();
}
}
19 changes: 19 additions & 0 deletions resources/lang/en/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
'files' => 'File upload',
'stickers' => 'Stickers',
'offers' => 'Suggestions and problems',
'feeds' => 'Post feed',

'adverts_count_links' => 'Qty. sponsored links',
'adverts_price' => 'Advertising price',
Expand Down Expand Up @@ -171,4 +172,22 @@
'site_open' => 'The site is open',
'site_closed_guest' => 'Closed to guests',
'site_closed_all' => 'Closed to everyone',

'homepage_view' => 'Homepage view',
'homepage_view_classic' => 'Classic view',
'homepage_view_feed' => 'Post feed',
'feed_topics_show' => 'Show forum posts',
'feed_news_show' => 'Show news',
'feed_photos_show' => 'Show photos',
'feed_articles_show' => 'Show articles',
'feed_downs_show' => 'Show downloads',
'feed_items_show' => 'Show boards',
'feed_per_page' => 'Number of posts per page',
'feed_last_record' => 'The number of recent records received',
'feed_total' => 'Total posts',
'feed_topics_rating' => 'Hide rated posts',
'feed_news_rating' => 'Hide rated news',
'feed_photos_rating' => 'Hide rated photos',
'feed_articles_rating' => 'Hide rated articles',
'feed_downs_rating' => 'Hide rated downloads',
];
Loading

0 comments on commit e8c1164

Please sign in to comment.