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 bind() |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function initialized() |
24
|
|
|
{ |
25
|
|
|
parent::initialized(); |
26
|
|
|
$this->getDataFromModel(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param $name |
31
|
|
|
* @return $this |
32
|
|
|
*/ |
33
|
|
|
public function addModelError($name) |
34
|
|
|
{ |
35
|
|
|
return $this->addError($this->getModelMessage($name)); |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param $name |
40
|
|
|
* @param array $variables |
41
|
|
|
* @return mixed |
42
|
|
|
*/ |
43
|
|
|
public function getModelMessage($name, $variables = []) |
44
|
|
|
{ |
45
|
|
|
return $this->getModelManager()->getMessage('form.' . $name, $variables); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return Record |
50
|
|
|
*/ |
51
|
|
|
public function getModel() |
52
|
|
|
{ |
53
|
|
|
return $this->model; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param Record $model |
58
|
|
|
* @return $this |
59
|
|
|
*/ |
60
|
|
|
public function setModel(Record $model) |
61
|
|
|
{ |
62
|
|
|
$this->model = $model; |
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function getDataFromModel() |
67
|
|
|
{ |
68
|
|
|
$elements = $this->getElements(); |
|
|
|
|
69
|
|
|
if (is_array($elements)) { |
70
|
|
|
foreach ($elements as $name => $element) { |
71
|
|
|
if (isset($this->getModel()->{$name})) { |
72
|
|
|
$element->getData($this->getModel()->{$name}, 'model'); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param $input |
80
|
|
|
* @param $name |
81
|
|
|
* @param array $variables |
82
|
|
|
* @return $this |
83
|
|
|
*/ |
84
|
|
|
public function addInputModelError($input, $name, $variables = []) |
85
|
|
|
{ |
86
|
|
|
return $this->getElement($input)->addError($this->getModelMessage($name, $variables)); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param $name |
91
|
|
|
* @return mixed |
92
|
|
|
*/ |
93
|
|
|
public function getModelLabel($name) |
94
|
|
|
{ |
95
|
|
|
return $this->getModelManager()->getLabel($name); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return \Nip\Records\RecordManager |
100
|
|
|
*/ |
101
|
|
|
public function getModelManager() |
102
|
|
|
{ |
103
|
|
|
return $this->getModel()->getManager(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function process() |
107
|
|
|
{ |
108
|
|
|
$this->saveToModel(); |
109
|
|
|
$this->saveModel(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function saveToModel() |
113
|
|
|
{ |
114
|
|
|
$elements = $this->getElements(); |
115
|
|
|
if (!is_array($elements)) { |
116
|
|
|
return; |
117
|
|
|
} |
118
|
|
|
foreach ($elements as $name => $element) { |
119
|
|
|
if ($element instanceof \Nip_Form_Element_File) { |
120
|
|
|
continue; |
121
|
|
|
} |
122
|
|
|
$this->getModel()->{$name} = $element->getValue('model'); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function saveModel() |
127
|
|
|
{ |
128
|
|
|
$this->getModel()->save(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
protected function _addModelFormMessage($form, $model) |
132
|
|
|
{ |
133
|
|
|
$this->_messageTemplates[$form] = $this->getModelMessage($model); |
|
|
|
|
134
|
|
|
|
135
|
|
|
return $this; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|