Sindbad~EG File Manager

Current Path : /var/www/html/votacao.camarasap.rs.gov.br/src/Controller/
Upload File :
Current File : /var/www/html/votacao.camarasap.rs.gov.br/src/Controller/PanelController.php

<?php
declare(strict_types=1);

namespace App\Controller;

use Cake\Core\Configure;

/**
 * Panel Controller
 *
 * @method \App\Model\Entity\Panel[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = [])
 */
class PanelController extends AppController
{

    /**
     * Index method
     *
     * @param string $type Type of rendering data.
     * @param string $action Action to render data.
     * @return \Cake\Http\Response|null|void Renders view
     */
    public function index($type = 'page', $action = '')
    {

        if ($type == 'ajax') {
            $data = [];
            if ($action == 'timer') $data = ['time' => $this->_getTimerData(), 'datetime' => date('d/m/Y H:i:s')];
            if ($action == 'data') $data = $this->_getAllData();
            return $this->response
                ->withType('application/json')
                ->withStringBody(json_encode($data));
        }

        $time = $this->_getTimerData();
        $meeting = $this->_getActiveMeeting();
        $voting = $this->_getActiveVoting();

        $this->set(compact('voting', 'meeting', 'time'));
    }


    /**
     * Get active voting method
     *
     * @return array $voting Active voting.
     */
    private function _getActiveVoting() {
        $this->loadModel('Votings');
        $voting = $this->Votings
            ->find('all')
            ->contain([
                'Meetings', 
                'VotingVotes' => [
                    'Councilors' => [
                        'PoliticalParties'
                    ]
                ]
            ])
            ->where(['Votings.active' => 1])
            ->first();
        return $voting;
    }

    /**
     * Get active meeting method
     *
     * @return array $meeting Active meeting.
     */
    private function _getActiveMeeting() {
        $this->loadModel('Meetings');
        $meeting = $this->Meetings
            ->find('all')
            ->contain([
                'MeetingPresences' => [
                    'Councilors' => [
                        'PoliticalParties'
                    ]
                ]
            ])
            ->where(['Meetings.active' => 1])
            ->first();
        return $meeting;
    }

    /**
     * Get timer data
     *
     * @return array $timer Timer data.
     */
    private function _getTimerData() {
        $time = file_get_contents(__DIR__ . '/timer.db');
        $time = (time() > $time) ? 0 : $time - time();
        return ($time > 0) ? gmdate('i:s', $time) : '00:00';
    }

    private function _getAllData() {
        $this->loadModel('Votings');
    	$data = [];

    	$voting = $this->_getActiveVoting();
    	$meeting = $this->_getActiveMeeting();

    	$message = Configure::read('Sys.panelTitleText');
        $meetingName = '';
        if (!empty($meeting)) $message = $meetingName = $meeting->name;
        if (!empty($voting)) $message = $voting->name;
		if (!empty($meeting->message)) $message = $meeting->message;

		$presences = [];
		if (!empty($meeting)) {
			foreach ($meeting->meeting_presences as $presence) {
				$councilor = $presence->has('councilor') ? $presence->councilor->name : '';
                $politicalParty = $presence->has('councilor') && $presence->councilor->has('political_party') ? $presence->councilor->political_party->acronym : '';
				$image = $presence->has('councilor') ? $presence->councilor->image : '';

                $councilorVotes = null;

                if ($voting) {
                    $councilorVotes = $this->_getCouncilorVotes($voting->id, $presence->councilor->id);
                }

				$presences[] = [
					'name' => $councilor . ' - ' . $politicalParty,
					'image' => $image,
					'present' => $presence->present,
                    'vote' => ($councilorVotes !== null && !empty($councilorVotes->vote)) ? $councilorVotes->vote : 'Não votou'
				];
			}
		}

		$votingData = [
			'in_favor' => (!empty($voting)) ? $voting->getTotalVotes($voting->voting_votes, $voting::VOTE_OPTION_IN_FAVOR) : 0,
			'against' => !empty($voting) ? $voting->getTotalVotes($voting->voting_votes, $voting::VOTE_OPTION_AGAINST) : 0, 
			'abstention' => !empty($voting) ? $voting->getTotalVotes($voting->voting_votes, $voting::VOTE_OPTION_ABSTENTION) : 0
		];

		$data = [
            'meeting_name' => $meetingName,
			'presences' => $presences,
			'votes' => $votingData,
			'message' => $message
		];

		return $data;

    }

    private function _getCouncilorVotes($votingId, $councilorId) {
        $votingVote = $this->Votings->VotingVotes
            ->find()
            ->where(['voting_id' => $votingId, 'councilor_id' => $councilorId])
            ->first();
    
        return $votingVote;
    }

    /**
     * Before filter method
     *
     * @param \Cake\Event\EventInterface $event Event
     */
    public function beforeFilter(\Cake\Event\EventInterface $event)
    {
        parent::beforeFilter($event);
        $this->viewBuilder()->disableAutoLayout();
    }

}

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists