|
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\Form\FormInterface; |
|
13
|
|
|
use Slick\Mvc\Form\EntityForm; |
|
14
|
|
|
use Slick\Mvc\Service\Entity\EntityUpdateService; |
|
15
|
|
|
use Slick\Orm\Entity; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Form Aware Methods |
|
19
|
|
|
* |
|
20
|
|
|
* @package Slick\Mvc\Controller |
|
21
|
|
|
* @author Filipe Silva <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
trait FormAwareMethods |
|
24
|
|
|
{ |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var EntityUpdateService |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $updateService; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @return FormInterface|EntityForm |
|
33
|
|
|
*/ |
|
34
|
|
|
abstract function getForm(); |
|
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Get invalid form data message |
|
38
|
|
|
* |
|
39
|
|
|
* @return string |
|
40
|
|
|
*/ |
|
41
|
|
|
protected function getInvalidFormDataMessage() |
|
42
|
|
|
{ |
|
43
|
|
|
$singleName = $this->getEntityNameSingular(); |
|
44
|
|
|
$message = "The {$singleName} could not be saved. Please check the " . |
|
45
|
|
|
"errors and try again."; |
|
46
|
|
|
return $this->translate($message); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Get invalid form data message |
|
51
|
|
|
* |
|
52
|
|
|
* @param \Exception $caught |
|
53
|
|
|
* |
|
54
|
|
|
* @return string |
|
55
|
|
|
*/ |
|
56
|
|
|
protected function getGeneralErrorMessage(\Exception $caught) |
|
57
|
|
|
{ |
|
58
|
|
|
$singleName = $this->getEntityNameSingular(); |
|
59
|
|
|
$message = "There was an error when saving {$singleName} data: %s"; |
|
60
|
|
|
return sprintf($this->translate($message), $caught->getMessage()); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get entity singular name used on controller actions |
|
65
|
|
|
* |
|
66
|
|
|
* @return string |
|
67
|
|
|
*/ |
|
68
|
|
|
abstract protected function getEntityNameSingular(); |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Get update service |
|
72
|
|
|
* |
|
73
|
|
|
* @return EntityUpdateService |
|
74
|
|
|
*/ |
|
75
|
|
|
public function getUpdateService() |
|
76
|
|
|
{ |
|
77
|
|
|
if (null == $this->updateService) { |
|
78
|
|
|
$this->setUpdateService( |
|
79
|
|
|
new EntityUpdateService($this->getNewEntity()) |
|
80
|
|
|
); |
|
81
|
|
|
} |
|
82
|
|
|
return $this->updateService; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Set update service |
|
87
|
|
|
* |
|
88
|
|
|
* @param EntityUpdateService $updateService |
|
89
|
|
|
* |
|
90
|
|
|
* @return EntityCreateMethods |
|
|
|
|
|
|
91
|
|
|
*/ |
|
92
|
|
|
public function setUpdateService(EntityUpdateService $updateService) |
|
93
|
|
|
{ |
|
94
|
|
|
$this->updateService = $updateService; |
|
95
|
|
|
return $this; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Get a new entity |
|
100
|
|
|
* |
|
101
|
|
|
* @return Entity |
|
102
|
|
|
*/ |
|
103
|
|
|
protected function getNewEntity() |
|
104
|
|
|
{ |
|
105
|
|
|
$class = $this->getEntityClassName(); |
|
106
|
|
|
return new $class(); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Gets the entity FQ class name |
|
111
|
|
|
* |
|
112
|
|
|
* @return string |
|
113
|
|
|
*/ |
|
114
|
|
|
abstract public function getEntityClassName(); |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Returns the translation for the provided message |
|
118
|
|
|
* |
|
119
|
|
|
* @param string $message |
|
120
|
|
|
* @param string $domain |
|
121
|
|
|
* @param string $locale |
|
122
|
|
|
* |
|
123
|
|
|
* @return string |
|
124
|
|
|
*/ |
|
125
|
|
|
abstract public function translate( |
|
126
|
|
|
$message, $domain = null, $locale = null |
|
127
|
|
|
); |
|
128
|
|
|
} |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.