|
| 1 | +<?php |
| 2 | +App::uses('AppController', 'Controller'); |
| 3 | +/** |
| 4 | + * Photos Controller |
| 5 | + * |
| 6 | + * @property Photo $Photo |
| 7 | + */ |
| 8 | +class PhotosController extends AppController { |
| 9 | + |
| 10 | + |
| 11 | +/** |
| 12 | + * index method |
| 13 | + * |
| 14 | + * @return void |
| 15 | + */ |
| 16 | + public function index() { |
| 17 | + $this->Photo->recursive = 0; |
| 18 | + $this->set('photos', $this->paginate()); |
| 19 | + } |
| 20 | + |
| 21 | +/** |
| 22 | + * view method |
| 23 | + * |
| 24 | + * @param string $id |
| 25 | + * @return void |
| 26 | + */ |
| 27 | + public function view($id = null) { |
| 28 | + $this->Photo->id = $id; |
| 29 | + if (!$this->Photo->exists()) { |
| 30 | + throw new NotFoundException(__('Invalid photo')); |
| 31 | + } |
| 32 | + $this->set('photo', $this->Photo->read(null, $id)); |
| 33 | + } |
| 34 | + |
| 35 | +/** |
| 36 | + * add method |
| 37 | + * |
| 38 | + * @return void |
| 39 | + */ |
| 40 | + public function add() { |
| 41 | + if ($this->request->is('post')) { |
| 42 | + $this->request->data('Photo.user_id', $this->Auth->user('id')); |
| 43 | + if ($this->Photo->add($this->request->data)) { |
| 44 | + $this->Session->setFlash(__('The photo has been saved'), 'sucess'); |
| 45 | + $this->redirect(array('action' => 'index')); |
| 46 | + } else { |
| 47 | + $this->Session->setFlash(__('The photo could not be saved. Please, try again.'), 'error'); |
| 48 | + } |
| 49 | + } |
| 50 | + $users = $this->Photo->User->find('list'); |
| 51 | + $labels = $this->Photo->Label->find('list'); |
| 52 | + $this->set(compact('users', 'labels')); |
| 53 | + } |
| 54 | + |
| 55 | +/** |
| 56 | + * edit method |
| 57 | + * |
| 58 | + * @param string $id |
| 59 | + * @return void |
| 60 | + */ |
| 61 | + public function edit($id = null) { |
| 62 | + $this->Photo->id = $id; |
| 63 | + if (!$this->Photo->exists()) { |
| 64 | + throw new NotFoundException(__('Invalid photo')); |
| 65 | + } |
| 66 | + if ($this->request->is('post') || $this->request->is('put')) { |
| 67 | + if ($this->Photo->save($this->request->data)) { |
| 68 | + $this->Session->setFlash(__('The photo has been saved'), 'success'); |
| 69 | + $this->redirect(array('action' => 'index')); |
| 70 | + } else { |
| 71 | + $this->Session->setFlash(__('The photo could not be saved. Please, try again.'), 'error'); |
| 72 | + } |
| 73 | + } else { |
| 74 | + $this->request->data = $this->Photo->read(null, $id); |
| 75 | + } |
| 76 | + $users = $this->Photo->User->find('list'); |
| 77 | + $labels = $this->Photo->Label->find('list'); |
| 78 | + $this->set(compact('users', 'labels')); |
| 79 | + } |
| 80 | + |
| 81 | +/** |
| 82 | + * delete method |
| 83 | + * |
| 84 | + * @param string $id |
| 85 | + * @return void |
| 86 | + */ |
| 87 | + public function delete($id = null) { |
| 88 | + if (!$this->request->is('post')) { |
| 89 | + throw new MethodNotAllowedException(); |
| 90 | + } |
| 91 | + $this->Photo->id = $id; |
| 92 | + if (!$this->Photo->exists()) { |
| 93 | + throw new NotFoundException(__('Invalid photo')); |
| 94 | + } |
| 95 | + if ($this->Photo->delete()) { |
| 96 | + $this->Session->setFlash(__('Photo deleted'), 'sucess'); |
| 97 | + $this->redirect(array('action' => 'index')); |
| 98 | + } |
| 99 | + $this->Session->setFlash(__('Photo was not deleted'), 'error'); |
| 100 | + $this->redirect(array('action' => 'index')); |
| 101 | + } |
| 102 | +} |
0 commit comments