Skip to content

Instantly share code, notes, and snippets.

@hugofabricio
Created October 4, 2013 21:35
Show Gist options
  • Select an option

  • Save hugofabricio/6833182 to your computer and use it in GitHub Desktop.

Select an option

Save hugofabricio/6833182 to your computer and use it in GitHub Desktop.
<?php
/**
*
* TeamsController
*
*/
App::uses('AppController', 'Controller');
class TeamsController extends AppController {
/**
* Chama os módulos definidos
*/
public $uses = array(
'Team',
'Task',
'TeamTask'
);
/**
* Painel Index
*/
public function painel_index()
{
// Retorna todos os resultados
$results = $this->paginate();
// Set
$this->set(
array(
'title_for_layout' => 'Equipes',
'action' => 'add',
'button' => 'Adicionar Equipe',
'class' => 'success',
'is_save' => false,
'results' => $results
)
);
}
/**
* Painel Add
*/
public function painel_add()
{
if ($this->request->is('post')):
$this->Team->create();
// Salva a prova
if ($this->Team->save($this->request->data)):
$this->Session->setFlash('Adicionado com sucesso.', SUCCESS);
$this->redirect(array('action' => 'index', 'painel' => true));
else:
$this->Session->setFlash('Verifique os erros encontrado no formulário de preenchimento.', WARNING);
endif;
endif;
// Set
$this->set(
array(
'title_for_layout' => 'Adicionar Equipe',
'action' => 'index',
'button' => 'Cancelar',
'class' => 'danger',
'is_save' => true
)
);
}
/**
* Painel Edit
*/
public function painel_edit($id = null)
{
// Equipe
$this->Team->id = $id;
// Se o registro não for encontrado
if (!$this->Team->exists()):
throw new NotFoundException('Equipe não encontrada.');
endif;
// Efetua ação de edição
if ($this->request->is('post') || $this->request->is('put')):
if ($this->Team->save($this->request->data)):
$this->Session->setFlash('Alterado com sucesso.', SUCCESS);
$this->redirect(array('action' => 'index', 'painel' => true));
else:
$this->Session->setFlash('Verifique os erros encontrado no formulário de preenchimento.', WARNING);
endif;
else:
$this->request->data = $this->Team->read(null, $id);
endif;
// Set
$this->set(
array(
'title_for_layout' => 'Editar Equipe',
'action' => 'index',
'button' => 'Cancelar',
'class' => 'danger',
'is_save' => true
)
);
}
/**
* Função para alterar o status
*/
public function painel_status($id = null)
{
// Equipe
$this->Team->id = $id;
// Se o registro não for encontrado
if (!$this->Team->exists()):
throw new NotFoundException('Equipe não encontrada.');
endif;
// Altera o status para o oposto do atual
if ($this->Team->field('is_active') == true):
$this->Team->saveField('is_active', false);
else:
$this->Team->saveField('is_active', true);
endif;
// Mensagem de sucesso
$this->Session->setFlash('Status alterado com sucesso.', SUCCESS);
// Redireciona
$this->redirect(array('action' => 'index', 'painel' => true));
}
/**
* Painel Delete
*/
public function painel_delete($id = null)
{
// Equipe
$this->Team->id = $id;
// Se o registro não for encontrado
if (!$this->Team->exists()):
throw new NotFoundException('Equipe não encontrada.');
endif;
// Se existir efetua a ação de exclusão
if ($this->Team->delete()):
$this->Session->setFlash('Excluído com sucesso.', SUCCESS);
$this->redirect(array('action' => 'index', 'painel' => true));
endif;
// Mensagem de erro
$this->Session->setFlash('Falha ao excluir.', WARNING);
// Redireciona
$this->redirect(array('action' => 'index', 'painel' => true));
}
/**
* Painel Index
*/
public function painel_points($id = null)
{
// Equipe
$this->Team->recursive = 0;
$this->Team->id = $id;
// Se o registro não for encontrado
if (!$this->Team->exists()):
throw new NotFoundException('Equipe não encontrada.');
endif;
// Retorna todos os resultados
$results = $this->paginate('TeamTask', array('TeamTask.team_id' => $id));
$team = $this->Team->read(null, $id);
// Set
$this->set(
array(
'title_for_layout' => 'Pontuação ' . $team['Team']['acronym'],
'action' => 'add_points/' . $team['Team']['id'],
'button' => 'Adicionar Pontuação',
'class' => 'success',
'results' => $results,
'team' => $team
)
);
}
/**
* Painel Add Points
*/
public function painel_add_points($id = null)
{
// Busca todas as provas ativas
$tasks = $this->TeamTask->Task->find('list', array('conditions' => array('Task.is_active' => true)));
// Busca todas as equipes ativas
$teams = $this->TeamTask->Team->find('list', array('conditions' => array('Team.is_active' => true)));
if ($this->request->is('post')):
$checkTask = $this->TeamTask->find('first', array('conditions' => array('team_id' => $this->request->data['TeamTask']['team_id'], 'task_id' => $this->request->data['TeamTask']['task_id'])));
if (!$checkTask):
$this->TeamTask->create();
// Salva a prova
if ($this->TeamTask->save($this->request->data)):
$this->Session->setFlash('Adicionado com sucesso.', SUCCESS);
$this->redirect(array('action' => 'points', $this->request->data['TeamTask']['team_id'], 'painel' => true));
else:
$this->Session->setFlash('Verifique os erros encontrado no formulário de preenchimento.', WARNING);
endif;
else:
$this->Session->setFlash('Prova já consta para esta equipe.', DANGER);
endif;
endif;
// Set
$this->set(
array(
'title_for_layout' => 'Adicionar Pontuação',
'action' => 'index',
'button' => 'Cancelar',
'class' => 'danger',
'is_save' => true,
'teams' => $teams,
'tasks' => $tasks
)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment