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

declare(strict_types=1);

namespace 
AppHttpControllersAdmin;

use 
AppClassesValidator;
use 
AppModelsUser;
use 
AppModelsVote;
use 
AppModelsVoteAnswer;
use 
IlluminateHttpRedirectResponse;
use 
IlluminateHttpRequest;
use 
IlluminateSupportFacadesDB;
use 
IlluminateViewView;

class 
VoteController extends AdminController
{
    
/**
     * Главная страница
     */
    
public function index(): View
    
{
        
$votes Vote::query()
            ->
where('closed'0)
            ->
orderByDesc('created_at')
            ->
with('topic')
            ->
paginate(setting('allvotes'));

        return 
view('admin/votes/index'compact('votes'));
    }

    
/**
     * Архив голосований
     */
    
public function history(): View
    
{
        
$votes Vote::query()
            ->
where('closed'1)
            ->
orderByDesc('created_at')
            ->
with('topic')
            ->
paginate(setting('allvotes'));

        return 
view('admin/votes/history'compact('votes'));
    }

    
/**
     * Редактирование голосования
     *
     *
     * @return View|RedirectResponse
     */
    
public function edit(int $idRequest $requestValidator $validator)
    {
        
$vote Vote::query()->where('id'$id)->first();

        if (! 
$vote) {
            
abort(404__('votes.voting_not_exist'));
        }

        if (
$request->isMethod('post')) {
            
$question $request->input('question');
            
$description $request->input('description');
            
$answers = (array) $request->input('answers');

            
$answers array_unique(array_diff($answers, ['']));

            
$validator->equal($request->input('_token'), csrf_token(), __('validator.token'))
                ->
length($question3100, ['question' => __('validator.text')])
                ->
length($description51000, ['description' => __('validator.text')], false)
                ->
between(count($answers), 210, ['answer' => __('votes.answer_not_enough')]);

            foreach (
$answers as $answer) {
                if (
utfStrlen($answer) > 50) {
                    
$validator->addError(['answers' => __('votes.answer_wrong_length')]);
                    break;
                }
            }

            if (
$validator->isValid()) {
                
$vote->update([
                    
'title'       => $question,
                    
'description' => $description,
                ]);

                
$countAnswers $vote->answers()->count();

                foreach (
$answers as $answerId => $answer) {
                    
/** @var VoteAnswer $ans */
                    
$ans $vote->answers()->firstOrNew(['id' => $answerId]);

                    if (
$ans->exists) {
                        
$ans->update(['answer' => $answer]);
                    } elseif (
$countAnswers 10) {
                        
$ans->fill(['answer' => $answer])->save();
                        
$countAnswers++;
                    }
                }

                
setFlash('success'__('votes.voting_success_changed'));

                return 
redirect('admin/votes/edit/' $vote->id);
            }

            
setInput($request->all());
            
setFlash('danger'$validator->getErrors());
        }

        
$vote->getAnswers $vote->answers->pluck('answer''id')->all();

        return 
view('admin/votes/edit'compact('vote'));
    }

    
/**
     * Удаление голосования
     */
    
public function delete(int $idRequest $request): RedirectResponse
    
{
        
$vote Vote::query()->where('id'$id)->first();

        if (! 
$vote) {
            
abort(404__('votes.voting_not_exist'));
        }

        if (! 
isAdmin(User::BOSS)) {
            
abort(403__('errors.forbidden'));
        }

        if (
$request->input('_token') === csrf_token()) {
            
DB::transaction(static function () use ($vote) {
                
$vote->delete();
                
$vote->answers()->delete();
                
$vote->pollings()->delete();
            });

            
setFlash('success'__('votes.voting_success_deleted'));
        } else {
            
setFlash('danger'__('validator.token'));
        }

        return 
redirect('admin/votes');
    }

    
/**
     * Открытие-закрытие голосования
     */
    
public function close(int $idRequest $request): RedirectResponse
    
{
        
$vote Vote::query()->where('id'$id)->first();

        if (! 
$vote) {
            
abort(404__('votes.voting_not_exist'));
        }

        if (
$request->input('_token') === csrf_token()) {
            
$status __('votes.voting_success_open');
            
$closed $vote->closed 1;

            
$vote->update([
                
'closed' => $closed,
            ]);

            if (
$closed) {
                
$vote->pollings()->delete();
                
$status __('votes.voting_success_closed');
            }

            
setFlash('success'$status);
        } else {
            
setFlash('danger'__('validator.token'));
        }

        if (empty(
$closed)) {
            return 
redirect('admin/votes');
        }

        return 
redirect('admin/votes/history');
    }

    
/**
     * Пересчет голосов
     */
    
public function restatement(Request $request): RedirectResponse
    
{
        if (! 
isAdmin(User::BOSS)) {
            
abort(403__('errors.forbidden'));
        }

        if (
$request->input('_token') === csrf_token()) {
            
restatement('votes');

            
setFlash('success'__('main.success_recounted'));
        } else {
            
setFlash('danger'__('validator.token'));
        }

        return 
redirect('admin/votes');
    }
}
Онлайн: 7
Реклама