1
|
|
|
<?php |
2
|
|
|
//------------------------------------------------------------------------- |
3
|
|
|
// OVIDENTIA http://www.ovidentia.org |
4
|
|
|
// Ovidentia is free software; you can redistribute it and/or modify |
5
|
|
|
// it under the terms of the GNU General Public License as published by |
6
|
|
|
// the Free Software Foundation; either version 2, or (at your option) |
7
|
|
|
// any later version. |
8
|
|
|
// |
9
|
|
|
// This program is distributed in the hope that it will be useful, but |
10
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of |
11
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
12
|
|
|
// See the GNU General Public License for more details. |
13
|
|
|
// |
14
|
|
|
// You should have received a copy of the GNU General Public License |
15
|
|
|
// along with this program; if not, write to the Free Software |
16
|
|
|
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
17
|
|
|
// USA. |
18
|
|
|
//------------------------------------------------------------------------- |
19
|
|
|
/** |
20
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL) |
21
|
|
|
* @copyright Copyright (c) 2022 by SI4YOU ({@link https://www.siforyou.com}) |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
namespace Capwelton\LibApp\Ui; |
25
|
|
|
|
26
|
|
|
use Capwelton\Widgets\Widgets\Form\WidgetSelect; |
|
|
|
|
27
|
|
|
use Capwelton\Widgets\Widgets\Form\WidgetTextEdit; |
|
|
|
|
28
|
|
|
use Capwelton\Widgets\Widgets\Interfaces\WidgetDisplayableInterface; |
|
|
|
|
29
|
|
|
use Capwelton\Widgets\Widgets\Layout\WidgetFlexLayout; |
|
|
|
|
30
|
|
|
use Capwelton\Widgets\Widgets\Layout\WidgetLayout; |
|
|
|
|
31
|
|
|
use Capwelton\Widgets\Widgets\Item\WidgetSection; |
|
|
|
|
32
|
|
|
use Capwelton\LibApp\Func_App; |
33
|
|
|
use Capwelton\LibApp\Set\AppRecordSet; |
34
|
|
|
use Capwelton\LibApp\Set\AppRecord; |
35
|
|
|
use Capwelton\LibApp\Set\AppCustomSection; |
36
|
|
|
use Capwelton\LibOrm\Exceptions\ORMException; |
|
|
|
|
37
|
|
|
use Capwelton\LibOrm\Field\ORMCurrencyField; |
|
|
|
|
38
|
|
|
|
39
|
|
|
class AppRecordEditor extends AppEditor |
40
|
|
|
{ |
41
|
|
|
public function __construct(Func_App $app, AppRecord $record = null, $id = null, WidgetLayout $layout = null) |
42
|
|
|
{ |
43
|
|
|
$this->record = $record; |
44
|
|
|
parent::__construct($app, $id, $layout); |
45
|
|
|
} |
46
|
|
|
/** |
47
|
|
|
* @var AppRecordSet |
48
|
|
|
*/ |
49
|
|
|
public $recordSet = null; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return AppRecordSet |
53
|
|
|
*/ |
54
|
|
|
protected function getRecordSet() |
55
|
|
|
{ |
56
|
|
|
if(!isset($this->recordSet)){ |
57
|
|
|
$this->recordSet = parent::getRecordSet(); |
58
|
|
|
} |
59
|
|
|
return $this->recordSet; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Creates section in the editor. |
64
|
|
|
* If a section with the same header text (title) was already |
65
|
|
|
* created it is replaced by an empty section. |
66
|
|
|
* |
67
|
|
|
* @param string $headerText |
68
|
|
|
* @return WidgetSection |
69
|
|
|
*/ |
70
|
|
|
protected function addSection($id, $headerText, $layout = null) |
71
|
|
|
{ |
72
|
|
|
$W = bab_Widgets(); |
73
|
|
|
if (!isset($layout)) { |
74
|
|
|
$layout = $W->VBoxLayout(); |
75
|
|
|
} |
76
|
|
|
$this->sections[$id] = $W->Section( |
77
|
|
|
$headerText, |
78
|
|
|
$layout |
79
|
|
|
)->setFoldable(true); |
80
|
|
|
|
81
|
|
|
return $this->sections[$id]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Retrieves a section in the editor. |
87
|
|
|
* |
88
|
|
|
* @param string $headerText |
89
|
|
|
* @return WidgetSection |
90
|
|
|
*/ |
91
|
|
|
protected function getSection($id) |
92
|
|
|
{ |
93
|
|
|
if (!isset($this->sections[$id])) { |
94
|
|
|
return null; |
95
|
|
|
} |
96
|
|
|
return $this->sections[$id]; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param string $textLabel |
102
|
|
|
* @param mixed $value |
103
|
|
|
* @return WidgetDisplayableInterface |
104
|
|
|
*/ |
105
|
|
|
public function labelledWidget($textLabel, $value, AppCustomSection $section = null) |
106
|
|
|
{ |
107
|
|
|
$W = bab_Widgets(); |
108
|
|
|
|
109
|
|
|
if ($value instanceof WidgetDisplayableInterface) { |
110
|
|
|
$widget = $value; |
111
|
|
|
} else { |
112
|
|
|
$widget = $W->Label($value); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if (isset($section)) { |
116
|
|
|
if ($textLabel === '__') { |
117
|
|
|
$fieldLayout = AppCustomSection::FIELDS_LAYOUT_NO_LABEL; |
118
|
|
|
} else { |
119
|
|
|
$fieldLayout = $section->fieldsLayout; |
120
|
|
|
} |
121
|
|
|
switch ($fieldLayout) { |
122
|
|
|
case AppCustomSection::FIELDS_LAYOUT_HORIZONTAL_LABEL: |
123
|
|
|
return $W->FlowItems( |
|
|
|
|
124
|
|
|
$W->Label($textLabel)->addClass('crm-horizontal-display-label', 'widget-strong') |
125
|
|
|
->setSizePolicy('widget-25pc'), |
126
|
|
|
$widget->setSizePolicy('widget-75pc') |
127
|
|
|
)->addClass('widget-labelled-widget') |
128
|
|
|
->setHorizontalSpacing(1, 'ex') |
129
|
|
|
->setVerticalAlign('top'); |
130
|
|
|
|
131
|
|
|
case AppCustomSection::FIELDS_LAYOUT_WIDE_HORIZONTAL_LABEL: |
132
|
|
|
return $W->FlowItems( |
|
|
|
|
133
|
|
|
$W->Label($textLabel)->addClass('crm-horizontal-display-label', 'widget-strong') |
134
|
|
|
->setSizePolicy('widget-50pc'), |
135
|
|
|
$widget |
136
|
|
|
)->addClass('widget-labelled-widget') |
137
|
|
|
->setHorizontalSpacing(1, 'ex') |
138
|
|
|
->setVerticalAlign('top'); |
139
|
|
|
|
140
|
|
|
case AppCustomSection::FIELDS_LAYOUT_NO_LABEL: |
141
|
|
|
return $widget; |
|
|
|
|
142
|
|
|
|
143
|
|
|
case AppCustomSection::FIELDS_LAYOUT_VERTICAL_LABEL: |
144
|
|
|
default: |
145
|
|
|
return $W->LabelledWidget($textLabel, $widget); |
|
|
|
|
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
|
151
|
|
|
protected function labelledWidgetOptional($textLabel, $displayedValue, $value = null, AppCustomSection $section = null) |
152
|
|
|
{ |
153
|
|
|
if (!isset($value)) { |
154
|
|
|
$value = $displayedValue; |
155
|
|
|
} |
156
|
|
|
if (!isset($value) || is_numeric($value) && $value == 0 || is_string($value) && trim($value) == '' || ($value instanceof WidgetLayout && count($value->getItems()) <= 0)) { |
157
|
|
|
return null; |
158
|
|
|
} |
159
|
|
|
return $this->labelledWidget($textLabel, $displayedValue, $section); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
|
163
|
|
|
|
164
|
|
|
protected function fieldOutput($field, $record, $fieldName) |
165
|
|
|
{ |
166
|
|
|
if ($field instanceof ORMCurrencyField) { |
167
|
|
|
$App = $this->App(); |
168
|
|
|
$value = $App->shortFormatWithUnit($this->record->$fieldName, $App->translate('_euro_')); |
169
|
|
|
} else { |
170
|
|
|
$value = $field->output($this->record->$fieldName); |
171
|
|
|
} |
172
|
|
|
if (is_array($value)) { |
173
|
|
|
$value = implode(', ', $value); |
174
|
|
|
} |
175
|
|
|
return $value; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
|
179
|
|
|
protected function getValueItem(AppCustomSection $customSection, $displayField, $label, $value) |
180
|
|
|
{ |
181
|
|
|
return $this->labelledWidget( |
182
|
|
|
$label, |
183
|
|
|
// $item, |
184
|
|
|
$value, |
185
|
|
|
$customSection |
186
|
|
|
); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
|
190
|
|
|
public function addSections($view) |
191
|
|
|
{ |
192
|
|
|
echo '<!-- VIEW (' . $view . ') -->'; |
193
|
|
|
$App = $this->App(); |
194
|
|
|
$W = bab_Widgets(); |
195
|
|
|
|
196
|
|
|
$this->addItem($W->Hidden()->setName('id')); |
197
|
|
|
|
198
|
|
|
$recordSet = $this->getRecordSet(); |
199
|
|
|
$recordClassName = $this->record->getClassName(); |
200
|
|
|
|
201
|
|
|
$customContainerSet = $App->CustomContainerSet(); |
202
|
|
|
$customContainers = $customContainerSet->select( |
203
|
|
|
$customContainerSet->object->is($recordClassName)->_AND_($customContainerSet->view->is($view)) |
204
|
|
|
); |
205
|
|
|
$customContainers->orderAsc($customContainerSet->rank); |
206
|
|
|
|
207
|
|
|
$customSectionSet = $App->CustomSectionSet(); |
208
|
|
|
|
209
|
|
|
$groupFieldsLayout = AppCustomSection::FIELDS_LAYOUT_VERTICAL_LABEL; |
210
|
|
|
|
211
|
|
|
foreach ($customContainers as $customContainer) { |
212
|
|
|
|
213
|
|
|
if (isset($this->record) && !$customContainer->isVisibleForRecord($this->record)) { |
214
|
|
|
continue; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
$container = $W->Items() |
218
|
|
|
->addClass($customContainer->classname) |
219
|
|
|
->setSizePolicy($customContainer->sizePolicy); |
220
|
|
|
|
221
|
|
|
$this->addItem($container); |
222
|
|
|
|
223
|
|
|
$customSections = $customSectionSet->select( |
224
|
|
|
$customSectionSet->container->is($customContainer->id) |
225
|
|
|
); |
226
|
|
|
$customSections->orderAsc($customSectionSet->rank); |
227
|
|
|
|
228
|
|
|
$currentColumn = 0; |
229
|
|
|
$nbCol = 0; |
230
|
|
|
$row = $W->Items()->setSizePolicy('row'); |
231
|
|
|
|
232
|
|
|
foreach ($customSections as $customSection) { |
233
|
|
|
|
234
|
|
|
$originalFieldsLayout = $customSection->fieldsLayout; |
235
|
|
|
|
236
|
|
|
if (isset($this->record) && !$customSection->isVisibleForRecord($this->record)) { |
237
|
|
|
continue; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
list(, , $nbCol) = explode('-', $customSection->sizePolicy); |
241
|
|
|
|
242
|
|
|
if ($currentColumn + $nbCol > 12) { |
243
|
|
|
$container->addItem($row); |
244
|
|
|
$row = $W->Items()->setSizePolicy('row'); |
245
|
|
|
$currentColumn = 0; |
246
|
|
|
} |
247
|
|
|
$currentColumn += $nbCol; |
248
|
|
|
|
249
|
|
|
$section = $this->getSection($customSection->id); |
250
|
|
|
if (!isset($section)) { |
251
|
|
|
$section = $this->addSection($customSection->id, $customSection->name); |
252
|
|
|
$section->addClass($customSection->classname); |
253
|
|
|
$section->setSizePolicy($customSection->sizePolicy); |
254
|
|
|
|
255
|
|
|
$section->setFoldable($customSection->foldable, $customSection->folded); |
256
|
|
|
$row->addItem($section); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
$displayFields = $customSection->getFields(); |
260
|
|
|
|
261
|
|
|
foreach ($displayFields as $displayFieldId => $displayField) { |
262
|
|
|
$isGroupField = strpos($displayFieldId, '_fieldsGroup') !== false; |
263
|
|
|
$widget = null; |
264
|
|
|
$item = null; |
265
|
|
|
$displayFieldName = $displayField['fieldname']; |
266
|
|
|
$parameters = $displayField['parameters']; |
267
|
|
|
$classname = isset($parameters['classname']) ? $parameters['classname'] : ''; |
268
|
|
|
$label = isset($parameters['label']) ? $parameters['label'] : ''; |
269
|
|
|
$sizePolicy = 'customsection-field-box '; |
270
|
|
|
$sizePolicy .= isset($parameters['sizePolicy']) ? $parameters['sizePolicy'] : ''; |
271
|
|
|
$sizePolicy .= isset($parameters['transparentBackground']) && $parameters['transparentBackground'] ? 'customsection-field-box-transparentBackground' : ''; |
272
|
|
|
$displayFieldMethod = '_' . $displayFieldName; |
273
|
|
|
|
274
|
|
|
if (!$isGroupField && method_exists($this, $displayFieldMethod)) { |
275
|
|
|
$widget = $this->$displayFieldMethod($customSection, $label); |
276
|
|
|
$item = $widget; |
277
|
|
|
} |
278
|
|
|
else { |
279
|
|
|
if($isGroupField){ |
280
|
|
|
list(, $groupId) = explode('_fieldsGroup', $displayFieldId); |
281
|
|
|
if (method_exists($this, '__fieldsGroup')) { |
282
|
|
|
$customSection->fieldsLayout = $groupFieldsLayout; |
283
|
|
|
$item = $this->__fieldsGroup($customSection, $groupId, $label); |
284
|
|
|
$customSection->fieldsLayout = $originalFieldsLayout; |
285
|
|
|
} |
286
|
|
|
$sizePolicy .= ' customsection-fieldsgroup-box'; |
287
|
|
|
} |
288
|
|
|
else{ |
289
|
|
|
try{ |
290
|
|
|
$field = $recordSet->getField($displayFieldName); |
291
|
|
|
if ($label === '' || $label === '__') { |
292
|
|
|
$label = $field->getDescription(); |
293
|
|
|
if (substr($displayFieldName, 0, 1) !== '_') { |
294
|
|
|
$label = $App->translate($label); |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
$widget = $field->getWidget(); |
298
|
|
|
if ($widget instanceof WidgetTextEdit || $widget instanceof WidgetSelect) { |
299
|
|
|
$widget->addClass('widget-100pc'); |
300
|
|
|
} |
301
|
|
|
$item = $this->getValueItem($customSection, $displayField, $label, $widget); |
302
|
|
|
} catch (ORMException $e) { |
303
|
|
|
$item = null; |
304
|
|
|
} |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
if ($item) { |
309
|
|
|
$item->setSizePolicy($sizePolicy); |
310
|
|
|
$item->addClass($classname); |
311
|
|
|
$section->addItem($item); |
312
|
|
|
} |
313
|
|
|
} |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
if ($currentColumn + $nbCol > 0) { |
317
|
|
|
$container->addItem($row); |
318
|
|
|
} |
319
|
|
|
} |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
public function sectionContent($customSectionId) |
323
|
|
|
{ |
324
|
|
|
$App = $this->App(); |
325
|
|
|
|
326
|
|
|
$customSectionSet = $App->CustomSectionSet(); |
327
|
|
|
$customSection = $customSectionSet->get($customSectionId); |
328
|
|
|
|
329
|
|
|
if (isset($this->record) && !$customSection->isVisibleForRecord($this->record)) { |
330
|
|
|
return null; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
$W = bab_Widgets(); |
334
|
|
|
|
335
|
|
|
$section = $W->VBoxLayout(); |
336
|
|
|
|
337
|
|
|
$displayFields = $customSection->getFields(); |
338
|
|
|
|
339
|
|
|
$wantedFieldsLayout = AppCustomSection::FIELDS_LAYOUT_HORIZONTAL_LABEL; |
340
|
|
|
$groupFieldsLayout = AppCustomSection::FIELDS_LAYOUT_VERTICAL_LABEL; |
341
|
|
|
$customSection->fieldsLayout = $wantedFieldsLayout; |
342
|
|
|
|
343
|
|
|
foreach ($displayFields as $displayFieldId => $displayField) { |
344
|
|
|
$isGroupField = strpos($displayFieldId, '_fieldsGroup') !== false; |
345
|
|
|
$widget = null; |
346
|
|
|
$item = null; |
347
|
|
|
$displayFieldName = $displayField['fieldname']; |
348
|
|
|
$parameters = $displayField['parameters']; |
349
|
|
|
$classname = isset($parameters['classname']) ? $parameters['classname'] : ''; |
350
|
|
|
$label = isset($parameters['label']) && !empty($parameters['label']) && $parameters['label'] !== '__' ? $parameters['label'] : null; |
351
|
|
|
$displayFieldMethod = '_' . $displayFieldName; |
352
|
|
|
$sizePolicy = 'customsection-field-box '; |
353
|
|
|
$sizePolicy .= isset($parameters['sizePolicy']) ? $parameters['sizePolicy'] : ''; |
354
|
|
|
if (!$isGroupField && method_exists($this, $displayFieldMethod)) { |
355
|
|
|
$item = $this->$displayFieldMethod($customSection, $label); |
356
|
|
|
} |
357
|
|
|
else{ |
358
|
|
|
if($isGroupField){ |
359
|
|
|
list(, $groupId) = explode('_fieldsGroup', $displayFieldId); |
360
|
|
|
if (method_exists($this, '__fieldsGroup')) { |
361
|
|
|
$customSection->fieldsLayout = $groupFieldsLayout; |
362
|
|
|
$item = $this->__fieldsGroup($customSection, $groupId, $label); |
363
|
|
|
$customSection->fieldsLayout = $wantedFieldsLayout; |
364
|
|
|
} |
365
|
|
|
$sizePolicy .= ' customsection-fieldsgroup-box'; |
366
|
|
|
} |
367
|
|
|
else{ |
368
|
|
|
try { |
369
|
|
|
$field = $this->recordSet->getField($displayFieldName); |
370
|
|
|
if (!isset($label) || empty($label)) { |
371
|
|
|
$label = $field->getDescription(); |
372
|
|
|
if (substr($displayFieldName, 0, 1) !== '_') { |
373
|
|
|
$label = $App->translate($label); |
374
|
|
|
} |
375
|
|
|
} |
376
|
|
|
$widget = $field->getWidget(); |
377
|
|
|
if ($widget instanceof WidgetTextEdit) { |
378
|
|
|
$widget->addClass('widget-100pc widget-autoresize'); |
379
|
|
|
|
380
|
|
|
} |
381
|
|
|
if ($widget instanceof WidgetSelect) { |
382
|
|
|
$widget->addClass('widget-100pc'); |
383
|
|
|
} |
384
|
|
|
$item = $this->getValueItem($customSection, $displayField, $label, $widget); |
385
|
|
|
} catch (ORMException $e) { |
386
|
|
|
$item = null; |
387
|
|
|
} |
388
|
|
|
} |
389
|
|
|
} |
390
|
|
|
if ($item) { |
391
|
|
|
$item->addClass($classname); |
392
|
|
|
$item->setSizePolicy($sizePolicy); |
393
|
|
|
$section->addItem($item); |
394
|
|
|
} |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
return $section; |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
protected function __fieldsGroup(AppCustomSection $customSection, $groupId = 0, $label = null) |
401
|
|
|
{ |
402
|
|
|
$W = bab_Widgets(); |
403
|
|
|
$App = $this->App(); |
404
|
|
|
|
405
|
|
|
$box = $W->FlexItems()->setGrowable(true)->setWrap(WidgetFlexLayout::FLEX_WRAP_WRAP); |
|
|
|
|
406
|
|
|
$groupField = $customSection->getField('_fieldsGroup'.$groupId); |
407
|
|
|
$recordSet = $this->getRecordSet(); |
408
|
|
|
|
409
|
|
|
if(isset($groupField['fields'])){ |
410
|
|
|
foreach ($groupField['fields'] as $field){ |
411
|
|
|
$item = null; |
412
|
|
|
|
413
|
|
|
$labelField = isset($field['parameters']['label']) ? $field['parameters']['label'] : ''; |
414
|
|
|
$itemClass = isset($field['parameters']['classname']) ? $field['parameters']['classname'] : ''; |
415
|
|
|
$displayFieldName = $field['fieldname']; |
416
|
|
|
$displayFieldMethod = '_' . $displayFieldName; |
417
|
|
|
if (method_exists($this, $displayFieldMethod)) { |
418
|
|
|
$item = $this->$displayFieldMethod($customSection, $labelField); |
419
|
|
|
} |
420
|
|
|
else if ($recordSet->fieldExist($displayFieldName)) { |
421
|
|
|
$field = $recordSet->getField($displayFieldName); |
422
|
|
|
if ($labelField === '') { |
423
|
|
|
$labelField = $field->getDescription(); |
424
|
|
|
if (substr($displayFieldName, 0, 1) !== '_') { |
425
|
|
|
$labelField = $App->translate($labelField); |
426
|
|
|
} |
427
|
|
|
} |
428
|
|
|
$item = $field->getWidget(); |
429
|
|
|
if ($item instanceof WidgetTextEdit || $item instanceof WidgetSelect) { |
430
|
|
|
$item->addClass('widget-100pc'); |
431
|
|
|
} |
432
|
|
|
$item = $this->getValueItem($customSection, $field, $labelField, $item); |
433
|
|
|
} |
434
|
|
|
if(isset($item)){ |
435
|
|
|
$item->addClass('app-customsection-groupedfield'); |
436
|
|
|
$item->addClass($itemClass); |
437
|
|
|
$box->addItem($item); |
438
|
|
|
} |
439
|
|
|
} |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
return $box; |
443
|
|
|
} |
444
|
|
|
} |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths