AppRecordEditor::sectionContent()   F
last analyzed

Complexity

Conditions 20
Paths 2066

Size

Total Lines 76
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 52
c 1
b 0
f 0
dl 0
loc 76
rs 0
cc 20
nc 2066
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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;
0 ignored issues
show
Bug introduced by
The type Capwelton\Widgets\Widgets\Form\WidgetSelect was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
use Capwelton\Widgets\Widgets\Form\WidgetTextEdit;
0 ignored issues
show
Bug introduced by
The type Capwelton\Widgets\Widgets\Form\WidgetTextEdit was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
use Capwelton\Widgets\Widgets\Interfaces\WidgetDisplayableInterface;
0 ignored issues
show
Bug introduced by
The type Capwelton\Widgets\Widget...getDisplayableInterface was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
use Capwelton\Widgets\Widgets\Layout\WidgetFlexLayout;
0 ignored issues
show
Bug introduced by
The type Capwelton\Widgets\Widgets\Layout\WidgetFlexLayout was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
30
use Capwelton\Widgets\Widgets\Layout\WidgetLayout;
0 ignored issues
show
Bug introduced by
The type Capwelton\Widgets\Widgets\Layout\WidgetLayout was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
31
use Capwelton\Widgets\Widgets\Item\WidgetSection;
0 ignored issues
show
Bug introduced by
The type Capwelton\Widgets\Widgets\Item\WidgetSection was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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;
0 ignored issues
show
Bug introduced by
The type Capwelton\LibOrm\Exceptions\ORMException was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
37
use Capwelton\LibOrm\Field\ORMCurrencyField;
0 ignored issues
show
Bug introduced by
The type Capwelton\LibOrm\Field\ORMCurrencyField was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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(
0 ignored issues
show
Bug Best Practice introduced by
The expression return $W->FlowItems($W-...setVerticalAlign('top') returns the type Widget_FlowLayout which is incompatible with the documented return type Capwelton\Widgets\Widget...getDisplayableInterface.
Loading history...
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(
0 ignored issues
show
Bug Best Practice introduced by
The expression return $W->FlowItems($W-...setVerticalAlign('top') returns the type Widget_FlowLayout which is incompatible with the documented return type Capwelton\Widgets\Widget...getDisplayableInterface.
Loading history...
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;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $widget also could return the type Widget_Label which is incompatible with the documented return type Capwelton\Widgets\Widget...getDisplayableInterface.
Loading history...
142
143
                case AppCustomSection::FIELDS_LAYOUT_VERTICAL_LABEL:
144
                default:
145
                    return $W->LabelledWidget($textLabel, $widget);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $W->LabelledWidget($textLabel, $widget) returns the type Widget_LabelledWidget which is incompatible with the documented return type Capwelton\Widgets\Widget...getDisplayableInterface.
Loading history...
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);
0 ignored issues
show
Bug introduced by
The method FlexItems() does not exist on Func_Widgets. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

405
        $box = $W->/** @scrutinizer ignore-call */ FlexItems()->setGrowable(true)->setWrap(WidgetFlexLayout::FLEX_WRAP_WRAP);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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
}