1 <?php
2
3 namespace Balance\Mvc\Controller;
4
5 use Balance\Model\ModelException;
6 use Exception;
7 use Zend\Mvc\Controller\AbstractActionController;
8 use Zend\Stdlib\Parameters;
9
10 11 12
13 trait RemoveActionTrait
14 {
15 16 17 18 19
20 public function removeAction()
21 {
22
23 if (! $this instanceof AbstractActionController) {
24
25 throw new Exception('Invalid Controller');
26 }
27
28 if (! $this instanceof ModelAwareInterface) {
29
30 throw new Exception('Invalid Controller');
31 }
32
33 if (! $this instanceof RedirectRouteNameAwareInterface) {
34
35 throw new Exception('Invalid Controller');
36 }
37
38 $model = $this->getModel();
39
40 $params = $this->params()->fromRoute();
41
42 $params = array_diff_key($params, array_flip(array('controller', 'action')));
43
44 try {
45
46 $model->remove(new Parameters($params));
47
48 $this->flashMessenger()->addSuccessMessage('Os dados foram removidos com sucesso.');
49 } catch (ModelException $e) {
50
51 $this->flashMessenger()->addErrorMessage('Impossível remover os dados solicitados.');
52 }
53
54 return $this->redirect()->toRoute($this->getRedirectRouteName());
55 }
56 }
57