-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDefinition.php
207 lines (161 loc) · 4.7 KB
/
Definition.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
namespace WebFace;
use Silex\Application;
use Symfony\Component\Form\Exception\InvalidConfigurationException;
use WebFace\Entity\Entity;
use WebFace\Entity\EntityManager;
use WebFace\Form\FormBuilder;
use WebFace\ListControl\FilterBuilder;
use WebFace\ListControl\ListBuilder;
class Definition
{
/** @var Application */
protected $app;
/** @var ListBuilder */
protected $listBuilder;
/** @var FormBuilder */
protected $formBuilder;
/** @var FilterBuilder */
protected $filterBuilder;
/** @var Entity */
protected $entity;
/** @var EntityManager */
protected $entityManager;
protected $fieldsDefinition = array();
public function __construct(Application $app, $table)
{
$this->app = $app;
$this->table = $table;
$this->setFieldsDefinition();
$this->setFieldsDefinitionDefaults();
}
public function defineDependencies()
{
$this->entity = new Entity($this->app, $this->table, $this->getFieldsDefinition());
$this->formBuilder = new FormBuilder($this->app, $this);
$this->listBuilder = new ListBuilder($this->app);
$this->filterBuilder = new FilterBuilder($this->app, $this->getFilterFieldsDefinition(), $this->formBuilder);
$this->entityManager = new EntityManager($this->app, $this->table);
}
public function getTable()
{
return $this->table;
}
/**
* @return ListBuilder
*/
public function getListBuilder()
{
return $this->listBuilder;
}
/**
* @return FormBuilder
*/
public function getFormBuilder()
{
return $this->formBuilder;
}
/**
* @return FilterBuilder
*/
public function getFilterBuilder()
{
return $this->filterBuilder;
}
/**
* @return EntityManager
*/
public function getEntityManager()
{
return $this->entityManager;
}
/**
* @return Entity
*/
public function getEntity()
{
return $this->entity;
}
public function getCustomFieldsDefinition(array $fields)
{
$fieldsDefinition = array();
foreach ($fields as $field) {
if (!isset($this->fieldsDefinition[$field])) {
throw new InvalidConfigurationException('Field "' . $field . '" does not exist in method getFields()');
}
$fieldsDefinition[$field] = $this->fieldsDefinition[$field];
}
return $fieldsDefinition;
}
public function getListFieldsDefinition()
{
return $this->getCustomFieldsDefinition($this->getListFields());
}
public function getFormFieldsDefinition($data = null)
{
return $this->getCustomFieldsDefinition($this->getFormFields($data));
}
public function getFilterFieldsDefinition()
{
return $this->getCustomFieldsDefinition($this->getFilterFields());
}
public function setFieldsDefinitionDefaults()
{
foreach ($this->fieldsDefinition as $fieldName => $fieldDefinition) {
// setDefaults
switch ($fieldDefinition['type']) {
case 'relation':
$config = &$fieldDefinition['config'];
switch ($config['relation_type']) {
case 'belongs_to':
if (!isset($config['relation_field'])) {
$config['relation_field'] = 'id';
}
break;
case 'has_many':
if (!isset($config['relation_field'])) {
$config['relation_field'] = 'id';
}
if (!isset($config['relation_table'])) {
$config['relation_table'] = $fieldName;
}
break;
}
break;
}
$this->fieldsDefinition[$fieldName] = $fieldDefinition;
}
}
public function setFieldsDefinition()
{
$this->fieldsDefinition = array();
}
public function getFieldsDefinition()
{
return $this->fieldsDefinition;
}
public function getListFields()
{
return array();
}
public function getListOrder()
{
return 'id DESC';
}
public function getListPerPageCount()
{
return 20;
}
public function getFormFields($data = null)
{
return array();
}
public function getFormGroups()
{
return array('default' => 'Основное');
}
public function getFilterFields()
{
return array();
}
}