|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Charcoal\Admin\Widget\FormGroup; |
|
4
|
|
|
|
|
5
|
|
|
use RuntimeException; |
|
6
|
|
|
|
|
7
|
|
|
// From 'charcoal-admin' |
|
8
|
|
|
use Charcoal\Admin\Ui\NestedWidgetContainerInterface; |
|
9
|
|
|
use Charcoal\Admin\Ui\NestedWidgetContainerTrait; |
|
10
|
|
|
use Charcoal\Admin\Ui\ObjectContainerInterface; |
|
11
|
|
|
use Charcoal\Admin\Ui\ObjectContainerTrait; |
|
12
|
|
|
|
|
13
|
|
|
// From 'charcoal-ui' |
|
14
|
|
|
use Charcoal\Ui\FormGroup\AbstractFormGroup; |
|
15
|
|
|
|
|
16
|
|
|
// From 'charcoal-config' |
|
17
|
|
|
use Charcoal\Config\ConfigurableInterface; |
|
18
|
|
|
|
|
19
|
|
|
// from 'charcoal-factory' |
|
20
|
|
|
use Charcoal\Factory\FactoryInterface; |
|
21
|
|
|
|
|
22
|
|
|
// from 'charcoal-translator' |
|
23
|
|
|
use Charcoal\Translator\Translation; |
|
24
|
|
|
|
|
25
|
|
|
// From 'pimple' |
|
26
|
|
|
use Pimple\Container; |
|
27
|
|
|
|
|
28
|
|
|
// from 'charcoal-attachment' |
|
29
|
|
|
use Charcoal\Attachment\Traits\ConfigurableAttachmentsTrait; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Attachment widget, as form group. |
|
33
|
|
|
*/ |
|
34
|
|
|
class AttachmentFormGroup extends AbstractFormGroup implements |
|
|
|
|
|
|
35
|
|
|
ConfigurableInterface, |
|
36
|
|
|
NestedWidgetContainerInterface, |
|
37
|
|
|
ObjectContainerInterface |
|
38
|
|
|
{ |
|
39
|
|
|
use ConfigurableAttachmentsTrait; |
|
40
|
|
|
use NestedWidgetContainerTrait; |
|
41
|
|
|
use ObjectContainerTrait { |
|
42
|
|
|
ObjectContainerTrait::createOrLoadObj as createOrCloneOrLoadObj; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var string |
|
47
|
|
|
*/ |
|
48
|
|
|
private $widgetId; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Store the widget factory instance for the current class. |
|
52
|
|
|
* |
|
53
|
|
|
* @var FactoryInterface |
|
54
|
|
|
*/ |
|
55
|
|
|
private $widgetFactory; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Whether notes should be display before or after the form fields. |
|
59
|
|
|
* |
|
60
|
|
|
* @var boolean |
|
61
|
|
|
*/ |
|
62
|
|
|
private $showNotesAbove = false; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Set the widget's data. |
|
66
|
|
|
* |
|
67
|
|
|
* @param array $data The widget data. |
|
68
|
|
|
* @return self |
|
69
|
|
|
*/ |
|
70
|
|
|
public function setData(array $data) |
|
71
|
|
|
{ |
|
72
|
|
|
/** |
|
73
|
|
|
* @todo Kinda hacky, but works with the concept of form. |
|
74
|
|
|
* Should work embeded in a form group or in a dashboard. |
|
75
|
|
|
*/ |
|
76
|
|
|
$data = array_merge($_GET, $data); |
|
77
|
|
|
|
|
78
|
|
|
parent::setData($data); |
|
79
|
|
|
|
|
80
|
|
|
/** Merge any available presets */ |
|
81
|
|
|
$data = $this->mergePresets($data); |
|
82
|
|
|
|
|
83
|
|
|
parent::setData($data); |
|
84
|
|
|
|
|
85
|
|
|
return $this; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Retrieve the default nested widget options. |
|
90
|
|
|
* |
|
91
|
|
|
* @return array |
|
92
|
|
|
*/ |
|
93
|
|
|
public function defaultWidgetData() |
|
94
|
|
|
{ |
|
95
|
|
|
return [ |
|
96
|
|
|
'type' => 'charcoal/admin/widget/attachment', |
|
97
|
|
|
'group' => $this['group'], |
|
98
|
|
|
'attachment_options' => $this['attachmentOptions'], |
|
99
|
|
|
'attachable_objects' => $this['attachableObjects'], |
|
100
|
|
|
'preset' => $this['preset'], |
|
101
|
|
|
'obj' => $this->obj(), |
|
102
|
|
|
'objId' => $this->objId(), |
|
103
|
|
|
'objType' => $this->objType() |
|
104
|
|
|
]; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Retrieve the widget's ID. |
|
109
|
|
|
* |
|
110
|
|
|
* @return string |
|
111
|
|
|
*/ |
|
112
|
|
|
public function widgetId() |
|
113
|
|
|
{ |
|
114
|
|
|
if (!$this->widgetId) { |
|
115
|
|
|
$this->widgetId = 'attachment_widget_'.uniqid(); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return $this->widgetId; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Set the widget's ID. |
|
123
|
|
|
* |
|
124
|
|
|
* @param string $widgetId The widget identifier. |
|
125
|
|
|
* @return self |
|
126
|
|
|
*/ |
|
127
|
|
|
public function setWidgetId($widgetId) |
|
128
|
|
|
{ |
|
129
|
|
|
$this->widgetId = $widgetId; |
|
130
|
|
|
|
|
131
|
|
|
return $this; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @return Translation|string|null |
|
136
|
|
|
*/ |
|
137
|
|
|
public function description() |
|
138
|
|
|
{ |
|
139
|
|
|
return $this->renderTemplate((string)parent::description()); |
|
|
|
|
|
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @return Translation|string|null |
|
144
|
|
|
*/ |
|
145
|
|
|
public function notes() |
|
146
|
|
|
{ |
|
147
|
|
|
return $this->renderTemplate((string)parent::notes()); |
|
|
|
|
|
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Show/hide the widget's notes. |
|
152
|
|
|
* |
|
153
|
|
|
* @param boolean|string $show Whether to show or hide notes. |
|
154
|
|
|
* @return self Chainable |
|
155
|
|
|
*/ |
|
156
|
|
|
public function setShowNotes($show) |
|
157
|
|
|
{ |
|
158
|
|
|
$this->showNotesAbove = ($show === 'above'); |
|
159
|
|
|
parent::setShowNotes($show); |
|
160
|
|
|
|
|
161
|
|
|
return $this; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @return boolean |
|
166
|
|
|
*/ |
|
167
|
|
|
public function showNotesAbove() |
|
168
|
|
|
{ |
|
169
|
|
|
return $this->showNotesAbove && $this->showNotes(); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @param Container $container The DI container. |
|
174
|
|
|
* @return void |
|
175
|
|
|
*/ |
|
176
|
|
|
protected function setDependencies(Container $container) |
|
177
|
|
|
{ |
|
178
|
|
|
parent::setDependencies($container); |
|
179
|
|
|
|
|
180
|
|
|
$this->setWidgetFactory($container['widget/factory']); |
|
181
|
|
|
$this->setModelFactory($container['model/factory']); |
|
182
|
|
|
|
|
183
|
|
|
if (isset($container['attachments/config'])) { |
|
184
|
|
|
$this->setConfig($container['attachments/config']); |
|
185
|
|
|
} else if (isset($container['config']['attachments'])) { |
|
186
|
|
|
$this->setConfig($container['config']['attachments']); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
// Satisfies Charcoal\View\ViewableInterface dependencies |
|
190
|
|
|
$this->setView($container['view']); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Set the widget factory. |
|
195
|
|
|
* |
|
196
|
|
|
* @param FactoryInterface $factory The factory to create widgets. |
|
197
|
|
|
* @return self |
|
198
|
|
|
*/ |
|
199
|
|
|
protected function setWidgetFactory(FactoryInterface $factory) |
|
200
|
|
|
{ |
|
201
|
|
|
$this->widgetFactory = $factory; |
|
202
|
|
|
|
|
203
|
|
|
return $this; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Retrieve the widget factory. |
|
208
|
|
|
* |
|
209
|
|
|
* @return FactoryInterface |
|
210
|
|
|
* @throws RuntimeException If the widget factory was not previously set. |
|
211
|
|
|
*/ |
|
212
|
|
View Code Duplication |
protected function widgetFactory() |
|
|
|
|
|
|
213
|
|
|
{ |
|
214
|
|
|
if ($this->widgetFactory === null) { |
|
215
|
|
|
throw new RuntimeException(sprintf( |
|
216
|
|
|
'Widget Factory is not defined for "%s"', |
|
217
|
|
|
get_class($this) |
|
218
|
|
|
)); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
return $this->widgetFactory; |
|
222
|
|
|
} |
|
223
|
|
|
} |
|
224
|
|
|
|