AppRecordFrame::addSections()   F
last analyzed

Complexity

Conditions 14
Paths 490

Size

Total Lines 74
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 49
dl 0
loc 74
rs 2.8083
c 2
b 1
f 0
cc 14
nc 490
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\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...
27
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...
28
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...
29
use Capwelton\LibApp\AppUiObject;
30
use Capwelton\LibApp\Func_App;
31
use Capwelton\LibApp\Set\AppRecord;
32
use Capwelton\LibApp\Set\AppRecordSet;
33
use Capwelton\LibApp\Set\AppCustomSection;
34
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...
35
use Capwelton\LibOrm\Field\ORMField;
0 ignored issues
show
Bug introduced by
The type Capwelton\LibOrm\Field\ORMField 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...
36
use Capwelton\LibOrm\ORMRecord;
0 ignored issues
show
Bug introduced by
The type Capwelton\LibOrm\ORMRecord 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
38
class AppRecordFrame extends AppUiObject
39
{
40
    /**
41
     * @var WidgetSection[]
42
     */
43
    protected $sections = array();
44
45
    /**
46
     * @var AppRecord
47
     */
48
    protected $record = null;
49
50
    /**
51
     * @var AppRecordSet
52
     */
53
    protected $recordSet = null;
54
55
56
    /**
57
     * @param Func_App      $app
58
     * @param AppRecord    $record
59
     * @param string        $id
60
     * @param WidgetLayout $layout
61
     */
62
    public function __construct(Func_App $app, AppRecord $record, $id = null, WidgetLayout $layout = null)
63
    {
64
        parent::__construct($app);
65
        $this->setRecord($record);
66
        $W = bab_Widgets();
67
        $this->setInheritedItem($W->Frame($id, $layout));
68
    }
69
70
71
    /**
72
     * Creates section in the editor.
73
     * If a section with the same header text (title) was already
74
     * created it is replaced by an empty section.
75
     *
76
     * @param string $headerText
77
     * @return WidgetSection
78
     */
79
    protected function addSection($id, $headerText, $layout = null)
80
    {
81
        $W = bab_Widgets();
82
        if (!isset($layout)) {
83
            $layout = $W->VBoxLayout()->setVerticalSpacing(0.6, 'em');
0 ignored issues
show
Bug introduced by
0.6 of type double is incompatible with the type integer expected by parameter $spacing of Widget_Layout::setVerticalSpacing(). ( Ignorable by Annotation )

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

83
            $layout = $W->VBoxLayout()->setVerticalSpacing(/** @scrutinizer ignore-type */ 0.6, 'em');
Loading history...
84
        }
85
        $this->sections[$id] = $W->Section(
86
            $headerText,
87
            $layout
88
        )->setFoldable(true);
89
90
        return $this->sections[$id];
91
    }
92
93
94
    /**
95
     * Retrieves a section in the editor.
96
     *
97
     * @param string $headerText
98
     * @return WidgetSection
99
     */
100
    protected function getSection($id)
101
    {
102
        if (!isset($this->sections[$id])) {
103
            return null;
104
        }
105
        return $this->sections[$id];
106
    }
107
108
109
    /**
110
     * @param string $textLabel
111
     * @param WidgetDisplayableInterface|string $value
112
     * @param AppCustomSection $section
113
     * @return WidgetDisplayableInterface
114
     */
115
    public function labelledWidget($textLabel, $value, AppCustomSection $section = null)
116
    {
117
        $W = bab_Widgets();
118
119
        if ($value instanceof WidgetDisplayableInterface) {
120
            $widget = $value;
121
        } else {
122
            $widget = $W->Label($value);
123
        }
124
125
        if (isset($section)) {
126
            if ($textLabel === '__') {
127
                $fieldLayout = AppCustomSection::FIELDS_LAYOUT_NO_LABEL;
128
            } else {
129
                $fieldLayout = $section->fieldsLayout;
130
            }
131
            switch ($fieldLayout) {
132
                case AppCustomSection::FIELDS_LAYOUT_HORIZONTAL_LABEL:
133
                    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...
134
                    $W->Label($textLabel)->addClass('app-horizontal-display-label', 'widget-strong')
135
                        ->setSizePolicy('widget-25pc'),
136
                    $widget->setSizePolicy('widget-75pc')
137
                )->addClass('widget-labelled-widget')
138
                ->setHorizontalSpacing(1, 'ex')
139
                ->setVerticalAlign('top');
140
141
                case AppCustomSection::FIELDS_LAYOUT_WIDE_HORIZONTAL_LABEL:
142
                    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...
143
                    $W->Label($textLabel)->addClass('app-horizontal-display-label', 'widget-strong')
144
                        ->setSizePolicy('widget-50pc'),
145
                    $widget->setSizePolicy('widget-50pc')
146
                )->addClass('widget-labelled-widget')
147
                ->setHorizontalSpacing(1, 'ex')
148
                ->setVerticalAlign('top');
149
150
                case AppCustomSection::FIELDS_LAYOUT_VERY_WIDE_HORIZONTAL_LABEL:
151
                    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...
152
                    $W->Label($textLabel)->addClass('app-horizontal-display-label', 'widget-strong')
153
                        ->setSizePolicy('widget-75pc'),
154
                    $widget->setSizePolicy('widget-25pc')
155
                )->addClass('widget-labelled-widget')
156
                ->setHorizontalSpacing(1, 'ex')
157
                ->setVerticalAlign('top');
158
159
160
                case AppCustomSection::FIELDS_LAYOUT_NO_LABEL:
161
                    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...
162
163
                case AppCustomSection::FIELDS_LAYOUT_VERTICAL_LABEL:
164
                default:
165
                    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...
166
            }
167
        }
168
    }
169
170
171
    /**
172
     *
173
     * @param string $textLabel
174
     * @param mixed $displayedValue
175
     * @param mixed $value
176
     * @param AppCustomSection $section
177
     * @return WidgetDisplayableInterface|null
178
     */
179
    public function labelledWidgetOptional($textLabel, $displayedValue, $value = null, AppCustomSection $section = null)
180
    {
181
        if (!isset($value)) {
182
            $value = $displayedValue;
183
        }
184
        if (!isset($value) || is_numeric($value) && $value == 0 || is_string($value) && trim($value) == '' || ($value instanceof WidgetLayout && count($value->getItems()) <= 0)) {
185
            return null;
186
        }
187
        $labelledWidget = $this->labelledWidget($textLabel, $displayedValue, $section);
188
        return $labelledWidget;
189
    }
190
191
192
    /**
193
     *
194
     * @param ORMField $field
195
     * @param ORMRecord $record
196
     * @param string $fieldName
197
     * @return mixed
198
     */
199
    protected function fieldOutput($field, $record, $fieldName)
200
    {
201
        if ($field instanceof ORMCurrencyField) {
202
            $App = $this->App();
203
            $value = $App->shortFormatWithUnit($this->record->$fieldName, $App->translate('_euro_'));
204
        } else {
205
            $value = $field->output($this->record->$fieldName);
206
        }
207
        if (is_array($value)) {
208
            $value = implode(', ', $value);
209
        }
210
        return $value;
211
    }
212
213
214
    /**
215
     *
216
     * @param AppCustomSection $customSection
217
     * @param array $displayField
218
     * @param string $textLabel
219
     * @param WidgetDisplayableInterface|string $value
220
     * @return WidgetDisplayableInterface
221
     */
222
    protected function getValueItem(AppCustomSection $customSection, $displayField, $textLabel, $value)
223
    {
224
        if ($value instanceof WidgetDisplayableInterface) {
225
            return $value;
226
        }
227
        $W = bab_Widgets();
228
        $parameters = $displayField['parameters'];
229
        if (isset($parameters['type'])) {
230
            if ($parameters['type'] === 'title1') {
231
                $item = $W->Title($value, 1);
232
            } elseif ($parameters['type'] === 'title2') {
233
                $item = $W->Title($value, 2);
234
            } elseif ($parameters['type'] === 'title3') {
235
                $item = $W->Title($value, 3);
236
            } else {
237
                $item = $W->Label($value);
238
            }
239
        } else {
240
            $item = $W->Label($value);
241
        }
242
        $labelledWidgetOptional = $this->labelledWidget( //Optional(
243
            $textLabel,
244
            $item,
245
//            $value,
246
            $customSection
247
        );
248
249
        if (isset($labelledWidgetOptional)) {
250
            $labelledWidgetOptional->addClass($displayField['fieldname']);
251
        }
252
253
        return $labelledWidgetOptional;
254
    }
255
256
257
    /**
258
     * @param string $view
259
     */
260
    protected function addSections($view)
261
    {
262
        $App = $this->App();
263
        $W = bab_Widgets();
264
265
        $recordClassName = $this->record->getClassName();
266
267
        $customSectionSet = $App->CustomSectionSet();
268
        $customSections = $customSectionSet->select(
269
            $customSectionSet->object->is($recordClassName)->_AND_(
270
                $customSectionSet->view->is($view)
271
            )
272
        );
273
        $customSections->orderAsc($customSectionSet->rank);
274
275
        $currentColumn = 0;
276
        $nbCol = 0;
277
        $row = $W->Items()->setSizePolicy('row');
278
        foreach ($customSections as $customSection) {
279
280
            list(, , $nbCol) = explode('-', $customSection->sizePolicy);
281
282
            if ($currentColumn + $nbCol > 12) {
283
                $this->addItem($row);
284
                $row = $W->Items()->setSizePolicy('row');
285
                $currentColumn = 0;
286
            }
287
            $currentColumn += $nbCol;
288
289
            $section = $this->getSection($customSection->id);
290
            if (!isset($section)) {
291
                $section = $this->addSection($customSection->id, $customSection->name);
292
                $section->addClass($customSection->classname);
293
                $section->setSizePolicy($customSection->sizePolicy);
294
295
                $section->setFoldable($customSection->foldable, $customSection->folded);
296
                $row->addItem($section);
297
            }
298
299
            $displayFields = $customSection->getFields();
300
301
            foreach ($displayFields as $displayField) {
302
                $value = null;
303
                $displayFieldName = $displayField['fieldname'];
304
                $parameters = $displayField['parameters'];
305
                $classname = isset($parameters['classname']) ? $parameters['classname'] : '';
306
                $label = isset($parameters['label']) ? $parameters['label'] : '';
307
                $displayFieldMethod = '_' . $displayFieldName;
308
309
                if (method_exists($this, $displayFieldMethod)) {
310
                    $value = $this->$displayFieldMethod($customSection, $label);
311
                } elseif ($this->recordSet->fieldExist($displayFieldName)) {
312
                    $field = $this->recordSet->getField($displayFieldName);
313
                    if ($label === '') {
314
                        $label = $field->getDescription();
315
                        if (substr($displayFieldName, 0, 1) !== '_') {
316
                            $label = $App->translate($label);
317
                        }
318
                    }
319
                    $value = $this->fieldOutput($field, $this->record, $displayFieldName);
320
                }
321
322
                if (isset($value)) {
323
                    $item = $this->getValueItem($customSection, $displayField, $label, $value);
324
                    if ($item) {
325
                        $item->addClass($classname);
326
                        $section->addItem($item);
327
                    }
328
                }
329
            }
330
        }
331
332
        if ($currentColumn + $nbCol > 0) {
333
            $this->addItem($row);
334
        }
335
    }
336
}