Created
August 14, 2013 13:02
-
-
Save andrebian/6230834 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
App::uses('AppController', 'Controller'); | |
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()); | |
} | |
public function index() { | |
$this->Usuario->recursive = 0; | |
$this->set('usuarios', $this->paginate()); | |
} | |
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; | |
} | |
} | |
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.')); | |
} | |
} | |
} | |
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); | |
} | |
} | |
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