Вход Регистрация
Файл: app/Http/Controllers/WallController.php
Строк: 100
<?php

declare(strict_types=1);

namespace 
AppHttpControllers;

use 
AppClassesValidator;
use 
AppModelsFlood;
use 
AppModelsIgnore;
use 
AppModelsWall;
use 
IlluminateHttpJsonResponse;
use 
IlluminateHttpRedirectResponse;
use 
IlluminateHttpRequest;
use 
IlluminateViewView;

class 
WallController extends Controller
{
    
/**
     * Главная страница
     */
    
public function index(string $login): View
    
{
        
$user getUserByLogin($login);

        if (! 
$user) {
            
abort(404__('validator.user'));
        }

        
$newWall getUser('newwall');

        
$messages Wall::query()
            ->
where('user_id'$user->id)
            ->
orderByDesc('created_at')
            ->
with('user''author')
            ->
paginate(setting('wallpost'));

        if (
$newWall && getUser('id') === $user->id) {
            
$user->update([
                
'newwall' => 0,
            ]);
        }

        return 
view('walls/index'compact('messages''user''newWall'));
    }

    
/**
     * Добавление сообщения
     */
    
public function create(string $loginRequest $requestValidator $validatorFlood $flood): RedirectResponse
    
{
        if (! 
getUser()) {
            
abort(403__('main.not_authorized'));
        }

        
$user getUserByLogin($login);

        if (! 
$user) {
            
abort(404__('validator.user'));
        }

        if (
$request->isMethod('post')) {
            
$msg $request->input('msg');

            
$validator->equal($request->input('_token'), csrf_token(), __('validator.token'))
                ->
length($msg5setting('comment_length'), ['msg' => __('validator.text')])
                ->
false($flood->isFlood(), ['msg' => __('validator.flood', ['sec' => $flood->getPeriod()])]);

            
$ignoring Ignore::query()
                ->
where('user_id'$user->id)
                ->
where('ignore_id'getUser('id'))
                ->
first();

            
$validator->empty($ignoring__('ignores.you_are_ignoring'));

            if (
$validator->isValid()) {
                if (
getUser() && getUser('id') !== $user->id) {
                    
$user->increment('newwall');
                }

                
Wall::query()->create([
                    
'user_id'    => $user->id,
                    
'author_id'  => getUser('id'),
                    
'text'       => antimat($msg),
                    
'created_at' => SITETIME,
                ]);

                
$flood->saveState();
                
sendNotify($msg'/walls/' $user->login__('index.wall_posts_login', ['login' => $user->getName()]));

                
setFlash('success'__('main.record_added_success'));
            } else {
                
setInput($request->all());
                
setFlash('danger'$validator->getErrors());
            }

            return 
redirect('walls/' $user->login);
        }
    }

    
/**
     * Удаление сообщений
     */
    
public function delete(string $loginRequest $requestValidator $validator): JsonResponse
    
{
        
$id int($request->input('id'));
        
$user getUserByLogin($login);

        if (! 
$user) {
            
abort(404__('validator.user'));
        }

        
$validator
            
->true($request->ajax(), __('validator.not_ajax'))
            ->
equal($request->input('_token'), csrf_token(), __('validator.token'))
            ->
notEmpty($id__('validator.deletion'))
            ->
notEmpty($user__('validator.user'))
            ->
true(isAdmin() || getUser('id') === $user->id__('main.deleted_only_admins'));

        if (
$validator->isValid()) {
            
Wall::query()
                ->
where('id'$id)
                ->
where('user_id'$user->id)
                ->
delete();

            return 
response()->json(['success' => true]);
        }

        return 
response()->json([
            
'success' => false,
            
'message' => current($validator->getErrors()),
        ]);
    }
}
Онлайн: 0
Реклама