|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nip\Form\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Nip\Records\AbstractModels\Record; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Trait HasModelTrait |
|
9
|
|
|
* @package Nip\Form\Traits |
|
10
|
|
|
*/ |
|
11
|
|
|
trait HasModelTrait |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var Record |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $model; |
|
17
|
|
|
|
|
18
|
|
|
public function initialized() |
|
19
|
|
|
{ |
|
20
|
|
|
parent::initialized(); |
|
21
|
|
|
$this->getDataFromModel(); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param $name |
|
26
|
|
|
* @return $this |
|
27
|
|
|
*/ |
|
28
|
|
|
public function addModelError($name) |
|
29
|
|
|
{ |
|
30
|
|
|
return $this->addError($this->getModelMessage($name)); |
|
|
|
|
|
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param $name |
|
35
|
|
|
* @param array $variables |
|
36
|
|
|
* @return mixed |
|
37
|
|
|
*/ |
|
38
|
|
|
public function getModelMessage($name, $variables = []) |
|
39
|
|
|
{ |
|
40
|
|
|
return $this->getModelManager()->getMessage('form.'.$name, $variables); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @return Record |
|
45
|
|
|
*/ |
|
46
|
|
|
public function getModel() |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->model; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param Record $model |
|
53
|
|
|
* @return $this |
|
54
|
|
|
*/ |
|
55
|
|
|
public function setModel(Record $model) |
|
56
|
|
|
{ |
|
57
|
|
|
$this->model = $model; |
|
58
|
|
|
return $this; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
protected function getDataFromModel() |
|
62
|
|
|
{ |
|
63
|
|
|
$elements = $this->getElements(); |
|
|
|
|
|
|
64
|
|
|
if (is_array($elements)) { |
|
65
|
|
|
foreach ($elements as $name => $element) { |
|
66
|
|
|
if (isset($this->getModel()->{$name})) { |
|
67
|
|
|
$element->getData($this->getModel()->{$name}, 'model'); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param $input |
|
75
|
|
|
* @param $name |
|
76
|
|
|
* @param array $variables |
|
77
|
|
|
* @return $this |
|
78
|
|
|
*/ |
|
79
|
|
|
public function addInputModelError($input, $name, $variables = []) |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->getElement($input)->addError($this->getModelMessage($name, $variables)); |
|
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param $name |
|
86
|
|
|
* @return mixed |
|
87
|
|
|
*/ |
|
88
|
|
|
public function getModelLabel($name) |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->getModelManager()->getLabel($name); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return \Nip\Records\RecordManager |
|
95
|
|
|
*/ |
|
96
|
|
|
public function getModelManager() |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->getModel()->getManager(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function process() |
|
102
|
|
|
{ |
|
103
|
|
|
$this->saveToModel(); |
|
104
|
|
|
$this->saveModel(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function saveToModel() |
|
108
|
|
|
{ |
|
109
|
|
|
$elements = $this->getElements(); |
|
110
|
|
|
if (is_array($elements)) { |
|
111
|
|
|
foreach ($elements as $name => $element) { |
|
112
|
|
|
$this->getModel()->{$name} = $element->getValue('model'); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function saveModel() |
|
118
|
|
|
{ |
|
119
|
|
|
$this->getModel()->save(); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
protected function _addModelFormMessage($form, $model) |
|
123
|
|
|
{ |
|
124
|
|
|
$this->_messageTemplates[$form] = $this->getModelMessage($model); |
|
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
return $this; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|