|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Charcoal\Admin\Widget; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
|
|
7
|
|
|
use \Pimple\Container; |
|
8
|
|
|
|
|
9
|
|
|
// From `charcoal-app` |
|
10
|
|
|
use \Charcoal\App\Template\WidgetFactory; |
|
11
|
|
|
|
|
12
|
|
|
/// From `charcoal-ui` |
|
13
|
|
|
use \Charcoal\Ui\Form\FormInterface; |
|
14
|
|
|
//use \Charcoal\Ui\Form\FormTrait; |
|
15
|
|
|
use \Charcoal\Ui\Layout\LayoutAwareInterface; |
|
16
|
|
|
use \Charcoal\Ui\Layout\LayoutAwareTrait; |
|
17
|
|
|
|
|
18
|
|
|
use \Charcoal\Admin\AdminWidget; |
|
19
|
|
|
//use \Charcoal\Admin\Ui\FormInterface; |
|
20
|
|
|
use \Charcoal\Admin\Ui\FormTrait; |
|
21
|
|
|
use \Charcoal\Admin\Ui\FormGroupInterface; |
|
22
|
|
|
use \Charcoal\Admin\Widget\LayoutWidget; |
|
23
|
|
|
|
|
24
|
|
|
// Local namespace dependencies |
|
25
|
|
|
use \Charcoal\Admin\Widget\FormGroupWidget; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* |
|
29
|
|
|
*/ |
|
30
|
|
|
class FormWidget extends AdminWidget implements |
|
31
|
|
|
FormInterface, |
|
32
|
|
|
LayoutAwareInterface |
|
33
|
|
|
{ |
|
34
|
|
|
use FormTrait; |
|
35
|
|
|
use LayoutAwareTrait; |
|
36
|
|
|
|
|
37
|
|
|
protected $sidebars = []; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var WidgetFactory $widgetFactory |
|
41
|
|
|
*/ |
|
42
|
|
|
private $widgetFactory; |
|
43
|
|
|
|
|
44
|
|
|
public function setDependencies(Container $container) |
|
45
|
|
|
{ |
|
46
|
|
|
//$this->setFormGroupBuilder($container['form/group/builder']); |
|
|
|
|
|
|
47
|
|
|
$this->setLayoutBuilder($container['layout/builder']); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param array|null $data |
|
52
|
|
|
* @return FormGroupInterface |
|
53
|
|
|
*/ |
|
54
|
|
|
public function createGroup(array $data = null) |
|
55
|
|
|
{ |
|
56
|
|
|
$widget_type = (isset($data['widget_type']) ? $data['widget_type'] : 'charcoal/admin/widget/formGroup'); |
|
57
|
|
|
$group = $this->widgetFactory()->create($widget_type, [ |
|
58
|
|
|
'logger' => $this->logger |
|
59
|
|
|
]); |
|
60
|
|
|
$group->setForm($this); |
|
61
|
|
|
if ($data) { |
|
|
|
|
|
|
62
|
|
|
$group->setData($data); |
|
63
|
|
|
} |
|
64
|
|
|
return $group; |
|
65
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param array $data |
|
70
|
|
|
* @return FormPropertyInterface |
|
71
|
|
|
*/ |
|
72
|
|
|
public function createFormProperty(array $data = null) |
|
73
|
|
|
{ |
|
74
|
|
|
$p = new FormPropertyWidget([ |
|
75
|
|
|
'logger'=>$this->logger |
|
76
|
|
|
]); |
|
77
|
|
|
if ($data !== null) { |
|
78
|
|
|
$p->setData($data); |
|
79
|
|
|
} |
|
80
|
|
|
return $p; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* |
|
85
|
|
|
*/ |
|
86
|
|
|
public function setSidebars(array $sidebars) |
|
87
|
|
|
{ |
|
88
|
|
|
$this->sidebars = []; |
|
89
|
|
|
foreach ($sidebars as $sidebarIdent => $sidebar) { |
|
90
|
|
|
$this->addSidebar($sidebarIdent, $sidebar); |
|
91
|
|
|
} |
|
92
|
|
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param array|FormSidebarWidget $sidebar |
|
97
|
|
|
* @throws InvalidArgumentException |
|
98
|
|
|
* @return FormWidget Chainable |
|
99
|
|
|
*/ |
|
100
|
|
View Code Duplication |
public function addSidebar($sidebarIdent, $sidebar) |
|
|
|
|
|
|
101
|
|
|
{ |
|
102
|
|
|
if (!is_string($sidebarIdent)) { |
|
103
|
|
|
throw new InvalidArgumentException( |
|
104
|
|
|
'Sidebar ident must be a string' |
|
105
|
|
|
); |
|
106
|
|
|
} |
|
107
|
|
|
if (($sidebar instanceof FormSidebarWidget)) { |
|
108
|
|
|
$this->sidebars[$sidebarIdent] = $sidebar; |
|
109
|
|
|
} else if (is_array($sidebar)) { |
|
110
|
|
|
$s = new FormSidebarWidget([ |
|
111
|
|
|
'logger'=>$this->logger |
|
112
|
|
|
]); |
|
113
|
|
|
$s->setForm($this); |
|
114
|
|
|
$s->setData($sidebar); |
|
115
|
|
|
$this->sidebars[$sidebarIdent] = $s; |
|
116
|
|
|
} else { |
|
117
|
|
|
throw new InvalidArgumentException( |
|
118
|
|
|
'Sidebar must be a FormSidebarWidget object or an array' |
|
119
|
|
|
); |
|
120
|
|
|
} |
|
121
|
|
|
return $this; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @return FormSidebarWidget |
|
126
|
|
|
*/ |
|
127
|
|
View Code Duplication |
public function sidebars() |
|
|
|
|
|
|
128
|
|
|
{ |
|
129
|
|
|
$sidebars = $this->sidebars; |
|
130
|
|
|
if (!is_array($this->sidebars)) { |
|
131
|
|
|
yield null; |
|
132
|
|
|
} else { |
|
133
|
|
|
uasort($sidebars, ['self', 'sortSidebarsByPriority']); |
|
134
|
|
|
foreach ($sidebars as $sidebar) { |
|
135
|
|
|
/*if ($sidebar->widget_type() != '') { |
|
|
|
|
|
|
136
|
|
|
$GLOBALS['widget_template'] = $sidebar->widget_type(); |
|
137
|
|
|
} else { |
|
138
|
|
|
$GLOBALS['widget_template'] = 'charcoal/admin/widget/form.sidebar'; |
|
139
|
|
|
}*/ |
|
140
|
|
|
$GLOBALS['widget_template'] = 'charcoal/admin/widget/form.sidebar'; |
|
141
|
|
|
yield $sidebar->ident() => $sidebar; |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* To be called with uasort(). |
|
148
|
|
|
* |
|
149
|
|
|
* @param FormGroupInterface $a Item "a" to compare, for sorting. |
|
150
|
|
|
* @param FormGroupInterface $b Item "b" to compaer, for sorting. |
|
151
|
|
|
* @return integer Sorting value: -1, 0, or 1 |
|
152
|
|
|
*/ |
|
153
|
|
View Code Duplication |
protected static function sortSidebarsByPriority(FormGroupInterface $a, FormGroupInterface $b) |
|
|
|
|
|
|
154
|
|
|
{ |
|
155
|
|
|
$a = $a->priority(); |
|
156
|
|
|
$b = $b->priority(); |
|
157
|
|
|
|
|
158
|
|
|
if ($a == $b) { |
|
159
|
|
|
return 1; |
|
160
|
|
|
// Should be 0? |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
return ($a < $b) ? (-1) : 1; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @return WidgetFactory |
|
168
|
|
|
*/ |
|
169
|
|
|
private function widgetFactory() |
|
170
|
|
|
{ |
|
171
|
|
|
if ($this->widgetFactory === null) { |
|
172
|
|
|
$this->widgetFactory = new WidgetFactory(); |
|
173
|
|
|
} |
|
174
|
|
|
return $this->widgetFactory; |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.