Passed
Branchfeature/useWidgetsNamespaces (54f503)
by Robin
04:17
created

AppCustomSection::isVisibleForRecord()   C

Complexity

Conditions 12
Paths 51

Size

Total Lines 57
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 34
c 0
b 0
f 0
dl 0
loc 57
rs 6.9666
cc 12
nc 51
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
// -------------------------------------------------------------------------
4
// OVIDENTIA http://www.ovidentia.org
5
// Ovidentia is free software; you can redistribute it and/or modify
6
// it under the terms of the GNU General Public License as published by
7
// the Free Software Foundation; either version 2, or (at your option)
8
// any later version.
9
//
10
// This program is distributed in the hope that it will be useful, but
11
// WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
// See the GNU General Public License for more details.
14
//
15
// You should have received a copy of the GNU General Public License
16
// along with this program; if not, write to the Free Software
17
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
18
// USA.
19
// -------------------------------------------------------------------------
20
/**
21
 * @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
22
 * @copyright Copyright (c) 2022 by SI4YOU ({@link https://www.siforyou.com})
23
 */
24
namespace Capwelton\LibApp\Set;
25
26
use bab_charset;
27
use Capwelton\LibOrm\MySql\ORMMySqlIterator;
0 ignored issues
show
Bug introduced by
The type Capwelton\LibOrm\MySql\ORMMySqlIterator 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
29
use function Capwelton\LibOrm\ORM_StringField;
0 ignored issues
show
introduced by
The function Capwelton\LibOrm\ORM_StringField was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
30
31
/**
32
 * @property string $name
33
 * @property string $sectionname
34
 * @property string $description
35
 * @property string $object
36
 * @property string $view
37
 * @property string $fields
38
 * @property string $classname
39
 * @property string $sizePolicy
40
 * @property string $fieldsLayout
41
 * @property bool $foldable
42
 * @property bool $folded
43
 * @property int $rank
44
 * @property string $visibilityCriteria
45
 *
46
 * @property AppCustomContainer $container
47
 * @method AppCustomContainer container()
48
 * @method Func_App App()
49
 */
50
class AppCustomSection extends AppTraceableRecord
51
{
52
    
53
    const FIELDS_LAYOUT_VERTICAL_LABEL = 'verticalLabel';
54
    
55
    const FIELDS_LAYOUT_HORIZONTAL_LABEL = 'horizontalLabel';
56
    
57
    const FIELDS_LAYOUT_WIDE_HORIZONTAL_LABEL = 'wideHorizontalLabel';
58
    
59
    const FIELDS_LAYOUT_VERY_WIDE_HORIZONTAL_LABEL = 'veryWideHorizontalLabel';
60
    
61
    const FIELDS_LAYOUT_NO_LABEL = 'noLabel';
62
    
63
    /**
64
     * @return string[]
65
     */
66
    public static function getFieldsLayouts()
67
    {
68
        $App = app_App();
69
        static $fieldsLayouts = null;
70
        
71
        if(! isset($fieldsLayouts)){
72
            $fieldsLayouts = array(
73
                self::FIELDS_LAYOUT_VERTICAL_LABEL => $App->translate('Vertical label'),
74
                self::FIELDS_LAYOUT_HORIZONTAL_LABEL => $App->translate('Horizontal label'),
75
                self::FIELDS_LAYOUT_WIDE_HORIZONTAL_LABEL => $App->translate('Horizontal label (wide)'),
76
                self::FIELDS_LAYOUT_VERY_WIDE_HORIZONTAL_LABEL => $App->translate('Horizontal label (very wide)'),
77
                self::FIELDS_LAYOUT_NO_LABEL => $App->translate('No label')
78
            );
79
        }
80
        return $fieldsLayouts;
81
    }
82
    
83
    /**
84
     * @return string[]
85
     */
86
    public function getRawFields()
87
    {
88
        if(empty($this->fields)){
89
            return array();
90
        }
91
        return explode(',', $this->fields);
92
    }
93
    
94
    /**
95
     * Return an array containing the fields in the section and their associated parameters.
96
     * [
97
     * [
98
     * 'fieldname' => the_field_name,
99
     * 'parameters => [
100
     * 'type' => the_field_type,
101
     * 'label' => the_field_label,
102
     * 'classname' => the field_classname,
103
     * ...
104
     * ]
105
     * ],
106
     * ...
107
     * ]
108
     *
109
     * @return string[][]
110
     */
111
    public function getFields()
112
    {
113
        if(empty($this->fields)){
114
            return array();
115
        }
116
        $fields = json_decode($this->fields, true);
117
        
118
        if(json_last_error() === JSON_ERROR_NONE){
119
            if(bab_charset::getIso() != bab_charset::UTF_8){
120
                $fields = app_utf8Decode($fields);
121
            }
122
            return $fields;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $fields also could return the type string which is incompatible with the documented return type array<mixed,string[]>.
Loading history...
123
        }
124
        
125
        $fields = array();
126
        $rawFields = $this->getRawFields();
127
        foreach ($rawFields as $rawField){
128
            if(empty($rawField)){
129
                continue;
130
            }
131
            $params = array();
132
            
133
            if(strpos($rawField, ':') === false){
134
                $fieldName = $rawField;
135
            }
136
            else{
137
                list ($fieldName, $parameters) = explode(':', $rawField);
138
                $parameters = explode(';', $parameters);
139
                foreach ($parameters as $parameter){
140
                    list ($key, $value) = explode('=', $parameter);
141
                    $params[$key] = $value;
142
                }
143
            }
144
            
145
            $fields[$fieldName] = array(
146
                'block' => '',
147
                'fieldname' => $fieldName,
148
                'parameters' => $params
149
            );
150
        }
151
        return $fields;
152
    }
153
    
154
    /**
155
     * Return an array containing the description of the specified field in the section and its associated parameters or
156
     * null if the field is not present in the section.
157
     * [
158
     * 'fieldname' => the_field_name,
159
     * 'parameters => [
160
     * 'type' => the_field_type,
161
     * 'label' => the_field_label,
162
     * 'classname' => the field_classname,
163
     * ...
164
     * ]
165
     * ],
166
     *
167
     * @param string $searchedFieldName
168
     * @return NULL|array[]
169
     */
170
    public function getField($searchedFieldName)
171
    {
172
        if(empty($this->fields)){
173
            return null;
174
        }
175
        
176
        $fields = $this->getFields();
177
        return isset($fields[$searchedFieldName]) ? $fields[$searchedFieldName] : null;
0 ignored issues
show
Bug Best Practice introduced by
The expression return IssetNode ? $fiel...archedFieldName] : null also could return the type string[] which is incompatible with the documented return type array<mixed,array>|null.
Loading history...
178
    }
179
    
180
    /**
181
     * Appends the field to the list of contained fields.
182
     *
183
     * @param string $fieldName
184
     * @param array $parameters
185
     */
186
    public function addField($fieldName, $parameters = null)
187
    {
188
        $fields = $this->getFields();
189
        if(! isset($parameters)){
190
            $fields[$fieldName] = array(
191
                'block' => ''
192
            );
193
            $parameters = array();
194
        }
195
        $fields[$fieldName]['fieldname'] = $fieldName;
196
        $fields[$fieldName]['parameters'] = $parameters;
197
        if(bab_charset::getIso() != bab_charset::UTF_8){
198
            $fields = app_utf8Encode($fields);
199
        }
200
        $this->fields = json_encode($fields);
201
        return $this;
202
    }
203
    
204
    /**
205
     * @param string $removedFieldName
206
     */
207
    public function removeField($removedFieldName)
208
    {
209
        $fields = $this->getFields();
210
        unset($fields[$removedFieldName]);
211
        if(bab_charset::getIso() != bab_charset::UTF_8){
212
            $fields = app_utf8Encode($fields);
213
        }
214
        $this->fields = json_encode($fields);
215
        return $this;
216
    }
217
    
218
    /**
219
     * Update the field, or create it if it does not exist
220
     *
221
     * @param string $updatedFieldName
222
     * @param array $parameters
223
     * @return self
224
     */
225
    public function updateField($updatedFieldName, $parameters = null)
226
    {
227
        $isFieldsGroup = false;
228
        $fields = $this->getFields();
229
        if(strpos($updatedFieldName, '_fieldsGroup') !== false){
230
            $isFieldsGroup = true;
231
            list (, $groupPos) = explode('_fieldsGroup', $updatedFieldName);
232
            if(! isset($groupPos) || empty($groupPos)){
233
                $groupPos = 0;
234
            }
235
        }
236
        if(! isset($fields[$updatedFieldName])){
237
            if($isFieldsGroup){
238
                while (isset($fields[$updatedFieldName . $groupPos])){
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $groupPos does not seem to be defined for all execution paths leading up to this point.
Loading history...
239
                    $groupPos ++;
240
                }
241
                $updatedFieldName .= $groupPos;
242
            }
243
            $this->addField($updatedFieldName, $parameters);
244
            return $this;
245
        }
246
        if(! isset($parameters)){
247
            $fields[$updatedFieldName] = array(
248
                'block' => ''
249
            );
250
            $parameters = array();
251
        }
252
        $fields[$updatedFieldName]['fieldname'] = $updatedFieldName;
253
        $fields[$updatedFieldName]['parameters'] = $parameters;
254
        if(bab_charset::getIso() != bab_charset::UTF_8){
255
            $fields = app_utf8Encode($fields);
256
        }
257
        $this->fields = json_encode($fields);
258
        return $this;
259
    }
260
    
261
    public function addFieldToGroup($fieldGroupName, $fieldName, $parameters = null)
262
    {
263
        $fields = $this->getFields();
264
        if(! isset($fields[$fieldGroupName])){
265
            return $this;
266
        }
267
        $groupFields = isset($fields[$fieldGroupName]['fields']) ? $fields[$fieldGroupName]['fields'] : array();
268
        if(! isset($parameters)){
269
            $groupFields[$fieldName] = array(
270
                'block' => ''
271
            );
272
            $parameters = array();
273
        }
274
        $groupFields[$fieldName]['fieldname'] = $fieldName;
275
        $groupFields[$fieldName]['parameters'] = $parameters;
276
        $fields[$fieldGroupName]['fields'] = $groupFields;
277
        if(bab_charset::getIso() != bab_charset::UTF_8){
278
            $fields = app_utf8Encode($fields);
279
        }
280
        $this->fields = json_encode($fields);
281
        return $this;
282
    }
283
    
284
    /**
285
     * @param string $removedFieldName
286
     */
287
    public function removeFieldFromGroup($fieldGroupName, $removedFieldName)
288
    {
289
        $fields = $this->getFields();
290
        unset($fields[$fieldGroupName]['fields'][$removedFieldName]);
291
        if(bab_charset::getIso() != bab_charset::UTF_8){
292
            $fields = app_utf8Encode($fields);
293
        }
294
        $this->fields = json_encode($fields);
295
        return $this;
296
    }
297
    
298
    /**
299
     * Update the field, or create it if it does not exist
300
     *
301
     * @param string $updatedFieldName
302
     * @param array $parameters
303
     * @return self
304
     */
305
    public function updateFieldGroup($fieldGroupName, $updatedFieldName, $parameters = null)
306
    {
307
        $fields = $this->getFields();
308
        if(! isset($fields[$fieldGroupName])){
309
            return $this;
310
        }
311
        $groupFields = isset($fields[$fieldGroupName]['fields']) ? $fields[$fieldGroupName]['fields'] : array();
312
        if(! isset($groupFields[$updatedFieldName])){
313
            $this->addFieldToGroup($fieldGroupName, $updatedFieldName, $parameters);
314
            return $this;
315
        }
316
        if(! isset($parameters)){
317
            $groupFields[$updatedFieldName] = array(
318
                'block' => ''
319
            );
320
            $parameters = array();
321
        }
322
        $groupFields[$updatedFieldName]["parameters"]["label"] = ORM_StringField("")->input($groupFields[$updatedFieldName]["parameters"]["label"]);
323
        $groupFields[$updatedFieldName]['fieldname'] = $updatedFieldName;
324
        $groupFields[$updatedFieldName]['parameters'] = $parameters;
325
        $fields[$fieldGroupName]['fields'] = $groupFields;
326
        if(bab_charset::getIso() != bab_charset::UTF_8){
327
            $fields = app_utf8Encode($fields);
328
        }
329
        $this->fields = json_encode($fields);
330
        return $this;
331
    }
332
    
333
    /**
334
     * @param AppRecord $record
335
     *
336
     * @return bool
337
     */
338
    public function isVisibleForRecord(AppRecord $record)
339
    {
340
        if(empty($this->visibilityCriteria)){
341
            return true;
342
        }
343
        
344
        $App = \bab_functionality::get('App');
345
        $recordSet = $record->getParentSet();
346
        $arrCriteria = json_decode($this->visibilityCriteria, true);
347
        if(bab_charset::getIso() != bab_charset::UTF_8){
348
            $arrCriteria = app_utf8Decode($arrCriteria);
349
        }
350
        foreach ($arrCriteria as $fieldName => $condition){
351
            if(strpos($fieldName, '/') !== false){
352
                list ($oneField, $foreignField) = explode('/', $fieldName);
353
                $field = $recordSet->$oneField()->$foreignField;
354
            }
355
            else{
356
                $field = $recordSet->$fieldName;
357
            }
358
            foreach ($condition as $op => $value){
359
                if(! is_array($value)){
360
                    $criteria = $field->$op($value);
361
                }
362
                else{
363
                    foreach ($value as $foreignClassName => $foreignValues){
364
                        $foreignClassName = str_replace($App->classPrefix, '', $foreignClassName) . 'Set';
365
                        $foreignSet = $App->$foreignClassName();
366
                        $foreignField = '';
367
                        $foreignCondition = '';
368
                        foreach ($foreignValues as $foreignFieldName => $foreignConditions){
369
                            if($foreignFieldName === '_foreignField'){
370
                                $foreignField = $foreignConditions;
371
                            }
372
                            else{
373
                                foreach ($foreignConditions as $key => $val){
374
                                    $foreignCondition = $foreignSet->$foreignFieldName->$key($val);
375
                                }
376
                            }
377
                        }
378
                    }
379
                    if(method_exists($foreignSet, 'getDefaultCriteria')){
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $foreignSet does not seem to be defined for all execution paths leading up to this point.
Loading history...
380
                        $foreignCondition = $foreignCondition->_AND_($foreignSet->getDefaultCriteria());
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $foreignCondition does not seem to be defined for all execution paths leading up to this point.
Loading history...
381
                    }
382
                    $criteria = $field->in($foreignCondition, $foreignField);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $foreignField does not seem to be defined for all execution paths leading up to this point.
Loading history...
383
                }
384
            }
385
        }
386
        
387
        $criteria = $criteria->_AND_($recordSet->id->is($record->id));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $criteria does not seem to be defined for all execution paths leading up to this point.
Loading history...
388
        
389
        /**
390
         * @var ORMMySqlIterator
391
         */
392
        $records = $recordSet->select($criteria);
393
        
394
        return ($records->count() != 0);
395
    }
396
}
397