Sindbad~EG File Manager
<?php
declare(strict_types=1);
namespace App\Controller;
use Cake\Network\Exception\InternalErrorException;
/**
* Meetings Controller
*
* @property \App\Model\Table\MeetingsTable $Meetings
* @method \App\Model\Entity\Meeting[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = [])
*/
class MeetingsController extends AppController
{
/**
* Index method
*
* @return \Cake\Http\Response|null|void Renders view
*/
public function index()
{
$where = [];
$this->paginate = [
'order' => ['id' => 'DESC'],
];
if (!empty($this->request->getQuery('name'))) {
$where['OR'] = [
'Meetings.name LIKE' => "%{$this->request->getQuery('name')}%",
];
}
$emptyDate = '';
$start_date = (!empty($this->request->getQuery('start_date'))) ? $this->request->getQuery('start_date') : $emptyDate;
$end_date = (!empty($this->request->getQuery('end_date'))) ? $this->request->getQuery('end_date') : $emptyDate;
if (!empty($start_date)) {
$where['Meetings.date >='] = $start_date;
}
if (!empty($end_date)) {
$where['Meetings.date <='] = $end_date;
}
$meetings = $this->Meetings->find('all')->where($where);
$meetings = $this->paginate($meetings);
$this->set(compact('meetings'));
}
/**
* View method
*
* @param string|null $id Meeting id.
* @return \Cake\Http\Response|null|void Renders view
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function view($id = null)
{
$meeting = $this->Meetings->get($id, [
'contain' => ['MeetingPresences' => ['Councilors' => ['PoliticalParties']], 'Votings' => ['VotingVotes']],
]);
$this->set(compact('meeting'));
}
/**
* Add method
*
* @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise.
*/
public function add($id = null)
{
$meeting = $this->Meetings->newEmptyEntity();
if ($this->request->is('post')) {
$meeting = $this->Meetings->patchEntity($meeting, $this->request->getData());
if ($meeting->active && $this->_checkActiveMeeting($id)) {
$this->Flash->error(__('An active meeting already exists. Only one meeting is allowed to be active.'));
return $this->redirect(['action' => 'index']);
}
if ($this->Meetings->save($meeting)) {
$this->Flash->success(__('The meeting has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The meeting could not be saved. Please, try again.'));
}
$councilors = $this->Meetings->MeetingPresences->Councilors->find(
'all',
[
'conditions' => ['Councilors.active' => 1],
'order' => ['Councilors.name' => 'ASC'],
'contain' => ['PoliticalParties']
])
->all();
$this->set(compact('meeting', 'councilors'));
}
/**
* Edit method
*
* @param string|null $id Meeting id.
* @param boolean $close To disable meeting.
* @return \Cake\Http\Response|null|void Redirects on successful edit, renders view otherwise.
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function edit($id = null, $close = false)
{
if ($id === null) {
$lastActiveMeeting = $this->Meetings->find()
->where(['active' => true])
->orderDesc('id')
->first();
$id = $lastActiveMeeting->id;
}
$meeting = $this->Meetings->get($id, [
'contain' => ['MeetingPresences' => ['Councilors' => ['PoliticalParties']]],
]);
if ($close) {
$meeting->active = 0;
if ($this->Meetings->save($meeting)) {
$this->Flash->success(__('The meeting has been saved.'));
return $this->redirect($this->referer());
}
$this->Flash->error(__('The meeting could not be saved. Please, try again.'));
}
if ($this->request->is(['patch', 'post', 'put'])) {
$meeting = $this->Meetings->patchEntity($meeting, $this->request->getData());
if ($meeting->active && $this->_checkActiveMeeting($id)) {
$this->Flash->error(__('An active meeting already exists. Only one meeting is allowed to be active.'));
return $this->redirect($this->referer());
}
$this->Meetings->MeetingPresences->deleteAll(['MeetingPresences.meeting_id' => $id]);
if ($this->Meetings->save($meeting)) {
$this->_deactivateActiveVoting();
$this->Flash->success(__('The meeting has been saved.'));
return $this->redirect($this->referer());
}
$this->Flash->error(__('The meeting could not be saved. Please, try again.'));
}
$this->set(compact('meeting'));
}
/**
* Delete method
*
* @param string|null $id Meeting id.
* @return \Cake\Http\Response|null|void Redirects to index.
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function delete($id = null)
{
$this->request->allowMethod(['post', 'delete']);
$meeting = $this->Meetings->get($id);
if ($this->Meetings->delete($meeting)) {
$this->Flash->success(__('The meeting has been deleted.'));
} else {
$this->Flash->error(__('The meeting could not be deleted. Please, try again.'));
}
return $this->redirect(['action' => 'index']);
}
/**
* Check active meeting
*
* @param int $id Current meeting id.
* @return boolean True if has another active meeting.
*/
private function _checkActiveMeeting($id = null) {
$conditions = ['Meetings.active' => 1];
if (!empty($id)) $conditions['Meetings.id <>'] = $id;
$meeting = $this->Meetings->find('all')->where($conditions)->first();
return (!empty($meeting)) ? true : false;
}
protected function _deactivateActiveVoting()
{
$this->loadModel('Votings');
$activeVoting = $this->Votings->find()
->where(['active' => true])
->first();
if ($activeVoting) {
$activeVoting->active = false;
$this->Votings->save($activeVoting);
}
}
/**
* Timer method
*
* @return \Cake\Http\Response|null|void Renders view
*/
public function timer()
{
if ($this->request->is(['patch', 'post', 'put'])) {
$minutes = $this->request->getData('minutes');
$timestamp = time() + ($minutes * 60);
file_put_contents(__DIR__ . '/timer.db', $timestamp);
$this->Flash->success(__('Timer updated successfully.'));
return $this->redirect(['controller' => 'Meetings', 'action' => 'edit']);
}
}
public function castVote()
{
$this->autoRender = false;
if ($this->request->is('ajax')) {
$action = $this->request->getData('action');
try {
$this->loadModel('Votings');
$this->loadModel('Meetings');
$activeVoting = $this->Votings->find()
->where(['active' => true])
->first();
$meeting = $this->Meetings->find()
->where(['active' => true])
->first();
if ($activeVoting) {
$activeVoting->active = false;
$message = ($action === 'approve') ? ' / APROVADO' : ' / REPROVADO';
$messageToPanel = $activeVoting->name . $message;
$meeting->message = $messageToPanel;
$this->Votings->save($activeVoting);
$this->Meetings->save($meeting);
$this->response = $this->response
->withType('application/json')
->withStringBody(json_encode(['success' => true, 'message' => $action == 'approve' ? 'A votação foi APROVADA com sucesso!' : 'A votação foi REPROVADA com sucesso!', 'messageInput' => $messageToPanel]));
} else {
$this->response = $this->response
->withType('application/json')
->withStringBody(json_encode(['success' => false, 'message' => 'Não há nenhuma votação ativa no momento!']));
}
} catch (InternalErrorException $e) {
// Responder com erro em caso de exceção
$this->response = $this->response
->withType('application/json')
->withStringBody(json_encode(['success' => false, 'message' => $e->getMessage()]));
}
}
}
}
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists