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

declare(strict_types=1);

namespace 
AppHttpControllers;

use 
AppClassesValidator;
use 
AppModelsPolling;
use 
AppModelsVote;
use 
AppModelsVoteAnswer;
use 
IlluminateHttpRedirectResponse;
use 
IlluminateHttpRequest;
use 
IlluminateSupportArr;
use 
IlluminateViewView;

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

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

    
/**
     * Просмотр голосования
     *
     *
     * @return View|RedirectResponse
     */
    
public function view(int $idRequest $requestValidator $validator)
    {
        
$show $request->input('show');

        
/** @var Vote $vote */
        
$vote Vote::query()->find($id);

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

        if (
$vote->closed) {
            
abort(200__('votes.voting_closed'));
        }

        
$vote->answers VoteAnswer::query()
            ->
where('vote_id'$vote->id)
            ->
orderBy('id')
            ->
get();

        if (
$vote->answers->isEmpty()) {
            
abort(200__('votes.voting_not_answers'));
        }

        
$vote->poll $vote->pollings()
            ->
where('user_id'getUser('id'))
            ->
first();

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

            
$validator->equal($request->input('_token'), csrf_token(), __('validator.token'))
                ->
empty($vote->poll__('votes.voting_passed'))
                ->
notEmpty($poll__('votes.answer_not_chosen'));

            if (
$validator->isValid()) {
                
$answer $vote->answers()
                    ->
where('id'$poll)
                    ->
where('vote_id'$vote->id)
                    ->
first();
                
$validator->notEmpty($answer__('votes.answer_not_found'));
            }

            if (
$validator->isValid()) {
                
$vote->increment('count');
                
$answer->increment('result');

                
Polling::query()->create([
                    
'relate_type' => Vote::$morphName,
                    
'relate_id'   => $vote->id,
                    
'user_id'     => getUser('id'),
                    
'vote'        => $answer->answer,
                    
'created_at'  => SITETIME,
                ]);

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

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

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

        
$voted Arr::pluck($vote->answers'result''answer');
        
$max max($voted);

        
arsort($voted);

        
$info['voted'] = $voted;
        
$info['sum'] = $vote->count $vote->count 1;
        
$info['max'] = $max $max 1;

        return 
view('votes/view'compact('vote''show''info'));
    }

    
/**
     * Проголосовавшие
     */
    
public function voters(int $id): View
    
{
        
/** @var Vote $vote */
        
$vote Vote::query()->find($id);

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

        
$voters $vote->pollings()
            ->
limit(50)
            ->
with('user')
            ->
get();

        return 
view('votes/voters'compact('vote''voters'));
    }

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

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

    
/**
     * Результаты истории голосований
     */
    
public function viewHistory(int $id): View
    
{
        
/** @var Vote $vote */
        
$vote Vote::query()->find($id);

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

        if (! 
$vote->closed) {
            
abort(200__('votes.voting_not_archive'));
        }

        
$vote->answers VoteAnswer::query()
            ->
where('vote_id'$vote->id)
            ->
orderBy('id')
            ->
get();

        if (
$vote->answers->isEmpty()) {
            
abort(200__('votes.voting_not_answers'));
        }

        
$voted Arr::pluck($vote->answers'result''answer');
        
$max max($voted);

        
arsort($voted);

        
$info['voted'] = $voted;
        
$info['sum'] = $vote->count $vote->count 1;
        
$info['max'] = $max $max 1;

        return 
view('votes/view_history'compact('vote''info'));
    }

    
/**
     * Создание голосования
     *
     *
     * @return View|RedirectResponse
     */
    
public function create(Request $requestValidator $validator)
    {
        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($question5100, ['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(['answer' => __('votes.answer_wrong_length')]);
                    break;
                }
            }

            if (
$validator->isValid()) {
                
/** @var Vote $vote */
                
$vote Vote::query()->create([
                    
'title'       => $question,
                    
'description' => $description,
                    
'created_at'  => SITETIME,
                ]);

                
$prepareAnswers = [];
                foreach (
$answers as $answer) {
                    
$prepareAnswers[] = [
                        
'vote_id' => $vote->id,
                        
'answer'  => $answer,
                    ];
                }

                
VoteAnswer::query()->insert($prepareAnswers);

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

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

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

        return 
view('votes/create');
    }
}
Онлайн: 0
Реклама