|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of slick/mvc package |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Slick\Mvc\Controller; |
|
11
|
|
|
|
|
12
|
|
|
use Slick\Common\Log; |
|
13
|
|
|
use Slick\Filter\StaticFilter; |
|
14
|
|
|
use Slick\Mvc\Exception\Service\EntityNotFoundException; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Entity View Methods |
|
18
|
|
|
* |
|
19
|
|
|
* @package Slick\Mvc\Controller |
|
20
|
|
|
* @author Filipe Silva <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
trait EntityViewMethods |
|
23
|
|
|
{ |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* For entity retrieval |
|
27
|
|
|
*/ |
|
28
|
|
|
use EntityBasedMethods; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Add a warning flash message |
|
32
|
|
|
* |
|
33
|
|
|
* @param string $message |
|
34
|
|
|
* @return self |
|
35
|
|
|
*/ |
|
36
|
|
|
abstract public function addWarningMessage($message); |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Get missing entity warning message |
|
40
|
|
|
* |
|
41
|
|
|
* @param mixed $entityId |
|
42
|
|
|
* |
|
43
|
|
|
* @return string |
|
44
|
|
|
*/ |
|
45
|
|
|
protected function getMissingEntityMessage($entityId) |
|
46
|
|
|
{ |
|
47
|
|
|
$singleName = $this->getEntityNameSingular(); |
|
48
|
|
|
return "The {$singleName} with ID {$entityId} was not found."; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Redirect callback after missing entity detection |
|
53
|
|
|
* |
|
54
|
|
|
* @return $this|\Slick\Mvc\ControllerInterface|static |
|
55
|
|
|
*/ |
|
56
|
|
|
protected function redirectFromMissingEntity() |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->redirect($this->getBasePath()); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Handles the request to view an entity |
|
63
|
|
|
* |
|
64
|
|
|
* @param int $entityId |
|
65
|
|
|
*/ |
|
66
|
|
|
public function show($entityId = 0) |
|
67
|
|
|
{ |
|
68
|
|
|
$entityId = StaticFilter::filter('text', $entityId); |
|
69
|
|
|
try { |
|
70
|
|
|
$entity = $this->getEntity($entityId); |
|
71
|
|
|
$this->set($this->getEntityNameSingular(), $entity); |
|
72
|
|
|
} catch (EntityNotFoundException $caught) { |
|
73
|
|
|
Log::logger()->addNotice($caught->getMessage()); |
|
74
|
|
|
$this->addWarningMessage( |
|
75
|
|
|
$this->getMissingEntityMessage($entityId) |
|
76
|
|
|
); |
|
77
|
|
|
$this->redirectFromMissingEntity(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
} |