Skip to content

Instantly share code, notes, and snippets.

@andrebian
Created August 14, 2013 13:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrebian/6230847 to your computer and use it in GitHub Desktop.
Save andrebian/6230847 to your computer and use it in GitHub Desktop.
<?php
App::uses('AppController', 'Controller');
/**
* Usuarios Controller
*
* @property Usuario $Usuario
*/
class UsuariosController extends AppController {
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$this->redirect($this->Auth->redirectUrl());
} else {
$this->Session->setFlash('Usuário e/ou senha incorreto(s)');
}
}
}
public function logout() {
$this->redirect($this->Auth->logout());
}
/**
* index method
*
* @return void
*/
public function index() {
$this->Usuario->recursive = 0;
$this->set('usuarios', $this->paginate());
}
/**
* view method
*
* @throws NotFoundException
* @param string $id
* @return void
*/
public function view($id = null) {
if (!$this->Usuario->exists($id)) {
throw new NotFoundException(__('Invalid usuario'));
}
$options = array('conditions' => array('Usuario.' . $this->Usuario->primaryKey => $id));
$this->set('usuario', $this->Usuario->find('first', $options));
}
public function download() {
App::uses('CakePdf', 'Plugin/CakePdf/Pdf');
$CakePdf = new CakePdf();
$CakePdf->template('teste', 'default');
$CakePdf->viewVars(array(
'nome' => 'Thiago'
));
$CakePdf->output();
$path = WWW_ROOT . DS . 'files' . DS;
$path .= uniqid() . '.pdf';
if ($CakePdf->write($path)) {
$this->response->file($path, array(
'download' => true,
'name' => 'teste_' . uniqid(),
));
return $this->response;
}
}
/**
* add method
*
* @return void
*/
public function add() {
if ($this->request->is('post')) {
$this->Usuario->create();
if ($this->Usuario->save($this->request->data)) {
$this->Session->setFlash(__('The usuario has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The usuario could not be saved. Please, try again.'));
}
}
}
/**
* edit method
*
* @throws NotFoundException
* @param string $id
* @return void
*/
public function edit($id = null) {
if (!$this->Usuario->exists($id)) {
throw new NotFoundException(__('Invalid usuario'));
}
if ($this->request->is('post') || $this->request->is('put')) {
if ($this->Usuario->save($this->request->data)) {
$this->Session->setFlash(__('The usuario has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The usuario could not be saved. Please, try again.'));
}
} else {
$options = array('conditions' => array('Usuario.' . $this->Usuario->primaryKey => $id));
$this->request->data = $this->Usuario->find('first', $options);
}
}
/**
* delete method
*
* @throws NotFoundException
* @throws MethodNotAllowedException
* @param string $id
* @return void
*/
public function delete($id = null) {
$this->Usuario->id = $id;
if (!$this->Usuario->exists()) {
throw new NotFoundException(__('Invalid usuario'));
}
$this->request->onlyAllow('post', 'delete');
if ($this->Usuario->delete()) {
$this->Session->setFlash(__('Usuario deleted'));
$this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('Usuario was not deleted'));
$this->redirect(array('action' => 'index'));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment