Passed
Branchfeature/useWidgetsNamespaces (b6928f)
by Robin
03:02
created

AppTableModelView::displayAdvancedForm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
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\Ui;
25
26
use Capwelton\Widgets\Widgets\Abstracts\WidgetItem;
0 ignored issues
show
Bug introduced by
The type Capwelton\Widgets\Widgets\Abstracts\WidgetItem 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\WidgetForm;
0 ignored issues
show
Bug introduced by
The type Capwelton\Widgets\Widgets\Form\WidgetForm 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\Item\WidgetFilter;
0 ignored issues
show
Bug introduced by
The type Capwelton\Widgets\Widgets\Item\WidgetFilter 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\Item\WidgetLabel;
0 ignored issues
show
Bug introduced by
The type Capwelton\Widgets\Widgets\Item\WidgetLabel 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\WidgetLink;
0 ignored issues
show
Bug introduced by
The type Capwelton\Widgets\Widgets\Item\WidgetLink 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\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...
33
use Capwelton\Widgets\Widgets\Table\WidgetTableModelView;
0 ignored issues
show
Bug introduced by
The type Capwelton\Widgets\Widget...le\WidgetTableModelView 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...
34
use Capwelton\Widgets\Widgets\Table\WidgetTableModelViewColumn;
0 ignored issues
show
Bug introduced by
The type Capwelton\Widgets\Widget...getTableModelViewColumn 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\LibApp\Ctrl\AppCtrlRecord;
36
use Capwelton\LibApp\Func_App;
37
use Capwelton\LibApp\Set\AppRecordSet;
38
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...
39
use Capwelton\LibOrm\Field\ORMFkField;
0 ignored issues
show
Bug introduced by
The type Capwelton\LibOrm\Field\ORMFkField 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...
40
use Capwelton\LibOrm\ORMRecordSet;
0 ignored issues
show
Bug introduced by
The type Capwelton\LibOrm\ORMRecordSet 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...
41
use function Capwelton\Widgets\widget_translate;
0 ignored issues
show
introduced by
The function Capwelton\Widgets\widget_translate was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
42
43
class AppTableModelView extends WidgetTableModelView
44
{
45
    
46
    /**
47
     * Filter form reset button
48
     */
49
    protected $reset = null;
50
    
51
    protected $highlightRows = null;
52
    
53
    /**
54
     * @var Func_App
55
     */
56
    private $app = null;
57
    
58
    /**
59
     * @var AppCtrlRecord
60
     */
61
    protected $recordController = null;
62
    
63
    /**
64
     * @var AppCtrlRecord
65
     */
66
    protected $recordControllerProxy = null;
67
    
68
    protected $filterBaseUrl = null;
69
    
70
    protected $displayAdvancedForm = true;
71
    
72
    /**
73
     * @param Func_App $app
74
     * @param string $id
75
     */
76
    public function __construct(Func_App $app = null, $id = null)
77
    {
78
        parent::__construct($id);
79
        $this->setApp($app);
80
        $this->displayAdvancedForm(true);
81
    }
82
    
83
    public function displayAdvancedForm($displayAdvancedForm = true)
84
    {
85
        $this->displayAdvancedForm = $displayAdvancedForm;
86
        return $this;
87
    }
88
    
89
    public function isAdvancedFormDisplayed()
90
    {
91
        return $this->displayAdvancedForm;
92
    }
93
    
94
    /**
95
     * Called before data rows
96
     *
97
     * @return void
98
     */
99
    protected function initHeaderRow(ORMRecordSet $set)
100
    {
101
        require_once $GLOBALS['babInstallPath'] . 'utilit/urlincl.php';
102
        
103
        \bab_functionality::includefile('Icons');
0 ignored issues
show
Bug introduced by
The method includefile() does not exist on bab_functionality. It seems like you code against a sub-type of bab_functionality such as Capwelton\LibApp\Func_App. ( Ignorable by Annotation )

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

103
        \bab_functionality::/** @scrutinizer ignore-call */ 
104
                            includefile('Icons');
Loading history...
104
        
105
        $W = bab_Widgets();
106
        
107
        $this->addHeaderSection();
108
        $this->setCurrentSection('header');
109
        
110
        $col = 0;
111
        
112
        $this->toggleCheckAllBox = null;
0 ignored issues
show
Bug Best Practice introduced by
The property toggleCheckAllBox does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
113
        $this->displayedIds = array();
0 ignored issues
show
Bug Best Practice introduced by
The property displayedIds does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
114
        
115
        $filterValues = $this->getFilterValues();
116
        
117
        foreach ($this->visibleColumns as $columnPath => $column){
118
            
119
            if(! isset($this->columnsDescriptions[$columnPath])){
120
                $this->columnsDescriptions[$columnPath] = $column->getDescription();
0 ignored issues
show
Bug Best Practice introduced by
The property columnsDescriptions does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
121
            }
122
            
123
            $columnLabel = $this->columnsDescriptions[$columnPath];
124
            
125
            $columnIcon = $column->getIcon();
126
            $isSortable = $column->isSortable();
127
            $isSearchable = $column->isSearchable() && $this->getFilterInputWidget($columnPath, $column->getField());
128
            
129
            $sizePolicy = 'widget-actions-target';
130
            
131
            if($this->isMultiSelect() && $columnPath === '_select_'){
132
                $this->toggleCheckAllBox = $W->CheckBox();
133
                $this->toggleCheckAllBox->setName('toggleCheckAllBox');
134
                $this->toggleCheckAllBox->setTitle(widget_translate('Select all on page'));
135
                $this->toggleCheckAllBox->setSizePolicy('condensed');
136
                $this->toggleCheckAllBox->setCheckedValue(true);
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type string expected by parameter $value of Widget_CheckBox::setCheckedValue(). ( Ignorable by Annotation )

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

136
                $this->toggleCheckAllBox->setCheckedValue(/** @scrutinizer ignore-type */ true);
Loading history...
137
                if(isset($_SESSION['toggleCheckAllBox'][$this->getId()]) && ! empty($_SESSION['toggleCheckAllBox'][$this->getId()]) && $_SESSION['toggleCheckAllBox'][$this->getId()] == 'on'){
138
                    $this->toggleCheckAllBox->setValue(true);
139
                }
140
                $isSearchable = false;
141
                $isSortable = false;
142
                $columnItem = $W->FlexItems($this->toggleCheckAllBox);
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

142
                /** @scrutinizer ignore-call */ 
143
                $columnItem = $W->FlexItems($this->toggleCheckAllBox);

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...
143
            }
144
            elseif(isset($this->sortParameterName) && self::getRecordSetField($set, $columnPath) && $isSortable){
145
                if(! isset($this->sortBaseUrl)){
146
                    $this->sortBaseUrl = \bab_url::request_gp();
0 ignored issues
show
Bug Best Practice introduced by
The property sortBaseUrl does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
147
                }
148
                
149
                if($this->sortField === $columnPath && $this->sortAscending){
150
                    $direction = ':down';
151
                }
152
                else{
153
                    $direction = ':up';
154
                }
155
                
156
                $url = \bab_url::mod($this->sortBaseUrl, $this->sortParameterName, $columnPath . $direction);
157
                if($anchor = $this->getAnchor()){
158
                    $url .= '#' . urlencode($anchor);
159
                }
160
                
161
                $columnItem = $W->Link($W->FlexItems($W->Label($columnLabel), $W->Items()
162
                    ->addClass('widget-table-column-sorting-icons'))
163
                    ->setAlignItems(WidgetFlexLayout::FLEX_ALIGN_CONTENT_CENTER), $url)
164
                    ->addClass('widget-table-column-sorting-action');
165
                if($this->sortAjaxAction){
166
                    $columnItem->setAjaxAction($this->sortAjaxAction->setParameter('sort', $columnPath . $direction), '');
167
                }
168
                $columnItem = $W->FlexItems($columnItem);
169
            }
170
            else{
171
                $columnItem = $W->FlexItems($W->Label($columnLabel), $W->Label(''));
172
            }
173
            
174
            $columnItem->setAlignItems(WidgetFlexLayout::FLEX_ALIGN_CONTENT_CENTER)->setJustifyContent(WidgetFlexLayout::FLEX_JUSTIFY_CONTENT_SPACE_BETWEEN);
175
            $columnItem->addClass('widget-table-column-header-item');
176
            
177
            if(isset($columnIcon)){
178
                $columnItem->addClass('icon', $columnIcon)->setSizePolicy(\Func_Icons::ICON_LEFT_16);
179
            }
180
            
181
            if($isSortable){
182
                $sizePolicy .= ' widget-table-column-sortable';
183
            }
184
            if($isSearchable){
185
                if(! isset($this->filterBaseUrl)){
186
                    $this->filterBaseUrl = $this->getRecordControllerProxy()->displayListFilter();
187
                }
188
                $url = $this->filterBaseUrl;
189
                $url->setParameter('col', $columnPath);
190
                $url->setParameter('key', $this->getId());
191
                $columnItem->addItem($W->Link('', $url->url())
192
                    ->setOpenMode(WidgetLink::OPEN_DIALOG)
193
                    ->addClass('widget-table-column-search-action')
194
                    ->setDialogClass('box blue'), 0);
0 ignored issues
show
Bug introduced by
The method setDialogClass() does not exist on Widget_Link. ( Ignorable by Annotation )

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

194
                    ->/** @scrutinizer ignore-call */ setDialogClass('box blue'), 0);

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...
195
                $sizePolicy .= ' widget-table-column-searchable';
196
                if(isset($filterValues[$columnPath]) && ! empty($filterValues[$columnPath])){
197
                    $sizePolicy .= ' widget-table-column-searched';
198
                }
199
            }
200
            $items = $W->Items()->setSizePolicy($sizePolicy);
201
            $items->addItem($columnItem);
202
            
203
            $this->addItem($items, 0, $col ++);
204
        }
205
    }
206
    
207
    protected function initRow($record, $row)
208
    {
209
        if($this->highlightRows == null){
210
            $this->highlightRows = $this->getHighlightedRows();
211
        }
212
        
213
        if(in_array($record->id, $this->highlightRows)){
214
            $this->addRowClass($row, "app_highlightRow");
215
        }
216
        $this->addRowClass($row, "app_highlightAvailable");
217
        return parent::initRow($record, $row);
218
    }
219
    
220
    /**
221
     * Forces the \Func_App object to which this object is 'linked'.
222
     *
223
     * @param Func_App $app
224
     * @return self
225
     */
226
    public function setApp(Func_App $app = null)
227
    {
228
        $this->app = $app;
229
        return $this;
230
    }
231
    
232
    /**
233
     * Get App object to use with this SET
234
     *
235
     * @return Func_App
236
     */
237
    public function App()
238
    {
239
        if(! isset($this->app)){
240
            // If the app object was not specified (through the setApp() method)
241
            // we try to select one according to the classname prefix.
242
            list ($prefix) = explode('_', get_class($this));
243
            $functionalityName = ucwords($prefix);
244
            $this->app = @\bab_functionality::get('App/' . $functionalityName);
0 ignored issues
show
Documentation Bug introduced by
@bab_functionality::get(...' . $functionalityName) is of type bab_functionality, but the property $app was declared to be of type Capwelton\LibApp\Func_App. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
245
            if(! $this->app){
246
                $this->app = @\bab_functionality::get('App');
247
            }
248
        }
249
        return $this->app;
250
    }
251
    
252
    protected function addCustomFields(AppRecordSet $recordSet)
253
    {
254
        $customFields = $recordSet->getCustomFields();
255
        
256
        foreach ($customFields as $customField){
257
            $fieldname = $customField->fieldname;
258
            $this->addColumn(app_TableModelViewColumn($recordSet->$fieldname, $customField->name)->setSortable(true)
259
                ->setExportable(true)
260
                ->setSearchable($customField->searchable)
261
                ->setVisible($customField->visible));
262
        }
263
    }
264
    
265
    /**
266
     * Get a generic filter panel
267
     * Use setPageLength to define the default number of items per page
268
     *
269
     * @param array $filter
270
     *            Filter values
271
     * @param string $name
272
     *            filter form name
273
     * @param string $anchor
274
     *            anchor in destination page of filter, the can be a WidgetTab id
275
     * @return WidgetFilter
276
     */
277
    public function filterPanel($filter = null, $name = null)
278
    {
279
        $W = bab_Widgets();
280
        
281
        $filterPanel = $W->Filter();
282
        $filterPanel->setLayout($W->VBoxLayout());
283
        if(isset($name)){
284
            $filterPanel->setName($name);
285
        }
286
        
287
        $pageLength = $this->getPageLength();
288
        if(null === $pageLength){
289
            $pageLength = 15;
290
        }
291
        
292
        $pageNumber = $this->getCurrentPage();
293
        if(null === $pageNumber){
294
            $pageNumber = 0;
295
        }
296
        
297
        $this->setPageLength(isset($filter['pageSize']) ? $filter['pageSize'] : $pageLength);
298
        $this->setCurrentPage(isset($filter['pageNumber']) ? $filter['pageNumber'] : $pageNumber);
299
        
300
        if(isset($name)){
301
            $this->sortParameterName = $name . '[filter][sort]';
0 ignored issues
show
Bug Best Practice introduced by
The property sortParameterName does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
302
        }
303
        else{
304
            $this->sortParameterName = 'filter[sort]';
305
        }
306
        
307
        if(isset($filter['sort'])){
308
            $this->setSortField($filter['sort']);
309
        }
310
        elseif(! isset($this->sortField)){
311
            
312
            if(method_exists($this, 'getDefaultSortField')){
313
                $this->setSortField($this->getDefaultSortField());
314
            }
315
            else{
316
                $columns = $this->getVisibleColumns();
317
                $sortField = key($columns);
318
                $this->setSortField($sortField);
319
            }
320
        }
321
        
322
        $form = $this->getFilterForm();
323
        
324
        if(isset($filter)){
325
            if(isset($name)){
326
                $path = array(
327
                    $name,
328
                    'filter'
329
                );
330
            }
331
            else{
332
                $path = array(
333
                    'filter'
334
                );
335
            }
336
            $form->setValues($filter, $path);
337
        }
338
        
339
        $filterPanel->setFilter($form);
340
        $filterPanel->setFiltered($this);
341
        
342
        return $filterPanel;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $filterPanel returns the type Widget_Filter which is incompatible with the documented return type Capwelton\Widgets\Widgets\Item\WidgetFilter.
Loading history...
343
    }
344
    
345
    /**
346
     * Add the filter fields to the filter form
347
     *
348
     * @param WidgetForm $form
349
     *
350
     */
351
    protected function handleAdvancedFilterFields(WidgetItem $form)
352
    {
353
    }
354
    
355
    protected function isFilterFieldSpecified($filter, $fieldPath)
356
    {
357
        if(isset($filter[$fieldPath])){
358
            $value = $filter[$fieldPath];
359
            if(is_array($value)){
360
                foreach ($value as $val){
361
                    $val = trim($val);
362
                    if(! empty($val)){
363
                        return true;
364
                    }
365
                }
366
                return false;
367
            }
368
            
369
            if(trim($value) !== ''){
370
                return true;
371
            }
372
        }
373
        
374
        return false;
375
    }
376
    
377
    protected function handleFilterInputWidget($name, ORMField $field = null)
378
    {
379
        if(null === $field){
380
            return null;
381
        }
382
        
383
        $W = bab_Widgets();
384
        
385
        if($field instanceof ORMFkField){
386
            $widget = $W->Select();
387
            $widget->addOption('', '');
388
            
389
            $App = $this->App();
390
            $setName = $field->getForeignSetName();
391
            
392
            $pos = strrpos($setName, '\\');
393
            if($pos === false){
394
                list (, $appSetName) = explode('_', $setName);
395
            }
396
            else{
397
                list (, $appSetName) = array(
398
                    substr($setName, 0, $pos),
399
                    substr($setName, $pos + 1)
400
                );
401
            }
402
            
403
            $set = $App->$appSetName();
404
            $set->setName($field->getName());
405
            $set->setDescription($field->getDescription());
406
            
407
            if($set === null){
408
                return null;
409
            }
410
            
411
            $values = $set->select();
412
            foreach ($values as $record){
413
                $widget->addOption($record->id, (string) $record->name);
414
            }
415
            
416
            return $widget;
417
        }
418
        
419
        return parent::handleFilterInputWidget($name, $field);
420
    }
421
    
422
    public function getFilterInputWidget($name, ORMField $field = null)
423
    {
424
        return $this->handleFilterInputWidget($name, $field);
425
    }
426
    
427
    public function getFilterLabelWidget($name, ORMField $field = null)
428
    {
429
        return $this->handleFilterLabelWidget($name, $field);
430
    }
431
    
432
    public function getFilterFormItem($name, ORMField $field = null)
433
    {
434
        return $this->handleFilterLabel($this->getFilterLabelWidget($name, $field), $this->getFilterInputWidget($name, $field));
0 ignored issues
show
Bug introduced by
It seems like $this->getFilterInputWidget($name, $field) can also be of type null; however, parameter $input of Capwelton\LibApp\Ui\AppT...ew::handleFilterLabel() does only seem to accept Capwelton\Widgets\Widget...getDisplayableInterface, maybe add an additional type check? ( Ignorable by Annotation )

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

434
        return $this->handleFilterLabel($this->getFilterLabelWidget($name, $field), /** @scrutinizer ignore-type */ $this->getFilterInputWidget($name, $field));
Loading history...
435
    }
436
    
437
    /**
438
     * Handle label and input widget merge in one item before adding to the filter form
439
     * default is a vertical box layout
440
     *
441
     * @param WidgetLabel $label
442
     * @param WidgetDisplayableInterface $input
443
     * @return WidgetItem
444
     */
445
    protected function handleFilterLabel(WidgetLabel $label, WidgetDisplayableInterface $input)
446
    {
447
        $W = bab_Widgets();
448
        
449
        return $W->FlowItems($W->LabelledWidget($label->getText(), $input));
0 ignored issues
show
Bug Best Practice introduced by
The expression return $W->FlowItems($W-...el->getText(), $input)) returns the type Widget_FlowLayout which is incompatible with the documented return type Capwelton\Widgets\Widgets\Abstracts\WidgetItem.
Loading history...
450
    }
451
    
452
    protected function getSearchItem()
453
    {
454
        $W = bab_Widgets();
455
        
456
        return $W->LineEdit()
457
            ->setName('search')
458
            ->addClass('widget-100pc');
459
    }
460
    
461
    /**
462
     * Get an advanced form filter
463
     *
464
     * @see WidgetFilter
465
     *
466
     * @param string $id
467
     * @param array $filter
468
     *
469
     * @return WidgetForm
470
     */
471
    public function getAdvancedFilterForm($id = null, $filter = null)
472
    {
473
        $App = $this->App();
474
        
475
        $W = bab_Widgets();
476
        
477
        $formItem = $this->getSearchItem();
478
        
479
        $activeFiltersFrame = $W->Frame(null, $W->FlowItems($W->VBoxItems($W->Label($App->translate('Search'))
480
            ->setAssociatedWidget($formItem), $formItem))
481
            ->setHorizontalSpacing(2, 'em')
482
            ->setVerticalSpacing(1, 'em')
483
            ->setVerticalAlign('bottom'));
484
        
485
        $addFilterButton = $W->Link($App->translate('More criteria'), $this->getRecordControllerProxy()
486
            ->displayListMoreCriteria($this->getId()))
487
            ->addClass('widget-actionbutton')
488
            ->setIcon(\Func_Icons::ACTIONS_LIST_ADD)
489
            ->setOpenMode(WidgetLink::OPEN_DIALOG)
490
            ->setDialogClass('box gradient blue app-morecriteria-dialog');
491
        
492
        $displayAdvancedForm = $this->isAdvancedFormDisplayed();
493
        $advancedFiltersFrame = $W->VBoxItems($addFilterButton)->addClass('widget-instant-container');
494
            
495
        if($displayAdvancedForm){
496
            $filterBox = $W->FlowLayout()
497
                ->addClass('widget-instant-form')
498
                ->setHorizontalSpacing(2, 'em')
499
                ->setVerticalSpacing(1, 'em')
500
                ->setVerticalAlign('bottom');
501
            $advancedFiltersFrame->addItem($filterBox);
502
        }
503
        
504
        $columns = $this->getVisibleColumns();
505
        
506
        foreach ($columns as $fieldName => $column){
507
            $field = $column->getField();
508
            if(! $column->isSearchable()){
509
                continue;
510
            }
511
            
512
            if(! ($field instanceof ORMField)){
513
                $field = null;
514
            }
515
            
516
            $label = $this->handleFilterLabelWidget($fieldName, $field);
517
            $input = $this->handleFilterInputWidget($fieldName, $field);
518
            
519
            if(isset($input) && isset($label)){
520
                
521
                $input->setName($fieldName);
522
                $label->setAssociatedWidget($input);
523
                
524
                $input->addClass('widget-100pc');
525
                
526
                $formItem = $this->handleFilterLabel($label, $input);
527
                
528
                // $formItem = $W->Section($label->getText(), $input, 7)->setFoldable(true, true);
529
                $formItem->addClass('compact', 'field_' . $fieldName);
530
                
531
                $mainSearch = (method_exists($column, 'isMainSearch') && $column->isMainSearch());
532
                
533
                if($mainSearch || $this->isFilterFieldSpecified($filter, $column->getFieldPath())){
534
                    
535
                    $removeColumnsAction = $W->Action();
536
                    $removeColumnsAction->setMethod('addon/widgets/configurationstorage', 'removeFilter', array(
537
                        'key' => $this->getId(),
538
                        'name' => $column->getFieldPath(),
539
                        'insession' => $this->isConfigurationStorageInSession()
540
                    ));
541
                    
542
                    // $formItem->setFoldable(false);
543
                    $activeFiltersFrame->addItem($W->FlexItems($formItem, $W->Link('', $removeColumnsAction)
544
                        ->setAjaxAction()
545
                        ->addClass('icon', \Func_Icons::STATUS_DIALOG_ERROR)
546
                        ->addAttribute('data-removefor', $column->getFieldPath()))
547
                        ->setAlignItems(WidgetFlexLayout::FLEX_ALIGN_ITEMS_FLEX_END));
548
                }
549
                elseif($displayAdvancedForm){
550
                    $formItem->setSizePolicy('col-lg-2 col-md-3 col-sm-6 col-xs-12');
551
                    $filterBox->addItem($formItem);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $filterBox does not seem to be defined for all execution paths leading up to this point.
Loading history...
552
                }
553
            }
554
        }
555
        
556
        if(! $this->submit){
557
            $this->submit = $W->SubmitButton();
0 ignored issues
show
Bug Best Practice introduced by
The property submit does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
558
            $this->submit->setLabel(Widget_translate('Apply filter'));
559
        }
560
        
561
        if($controller = $this->getRecordController()){
562
            $proxy = $controller->proxy();
563
            $this->reset = $W->Link($App->translate('Reset'), $proxy->resetFilters())
0 ignored issues
show
Bug introduced by
$proxy->resetFilters() of type true is incompatible with the type Widget_Action|bab_url|string expected by parameter $url of Func_Widgets::Link(). ( Ignorable by Annotation )

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

563
            $this->reset = $W->Link($App->translate('Reset'), /** @scrutinizer ignore-type */ $proxy->resetFilters())
Loading history...
564
                ->addClass('icon', \Func_Icons::ACTIONS_VIEW_REFRESH, 'widget-actionbutton')
565
                ->setTitle($App->translate('Reset filter to default values'))
566
                ->setAjaxAction();
567
            
568
            // $this->save = $W->Link(
569
            // $App->translate('Save'),
570
            // $proxy->confirmSaveCurrentFilter()
571
            // )->addClass('icon', \Func_Icons::ACTIONS_DOCUMENT_SAVE, 'widget-actionbutton')
572
            // ->setTitle($App->translate('Save current filter'))
573
            // ->setOpenMode(WidgetLink::OPEN_DIALOG_AND_RELOAD);
574
            
575
            // $this->select = null;
576
            
577
            $this->filterMenu = $W->Menu(null, $W->VBoxItems());
0 ignored issues
show
Bug Best Practice introduced by
The property filterMenu does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
578
            $this->filterMenu->setButtonClass('widget-actionbutton icon actions-list-filter');
579
            $this->filterMenu->setButtonLabel($App->translate('Saved filters'));
580
            $this->filterMenu->addClass(\Func_Icons::ICON_LEFT_16);
581
            
582
            $filterNames = $controller->getFilterNames();
583
            if(count($filterNames) > 0){
584
                // $this->select = $W->Menu(null, $W->VBoxItems());
585
                // $this->select->setButtonClass('widget-link icon actions-list-filter');
586
                // $this->select->setButtonLabel($App->translate('Saved filters'));
587
                // $this->select->addClass(\Func_Icons::ICON_LEFT_16);
588
                
589
                foreach ($filterNames as $filterName){
590
                    $filter = $W->getUserConfiguration($controller->getModelViewDefaultId() . '/filters/' . $filterName, 'widgets', false);
591
                    
592
                    // $this->select->addItem(
593
                    // $W->Link(
594
                    // $filterName,
595
                    // $proxy->setCurrentFilterName($filterName)
596
                    // )->setTitle($filter['description'])
597
                    // );
598
                    
599
                    $this->filterMenu->addItem($W->Link($filterName, $proxy->setCurrentFilterName($filterName))
600
                        ->setTitle($filter['description'])
601
                        ->setAjaxAction());
602
                }
603
                // $this->select->addSeparator();
604
                // $this->select->addItem(
605
                // $W->Link(
606
                // $App->translate('Manage filters'),
607
                // $proxy->manageFilters()
608
                // )->addClass('icon', \Func_Icons::ACTIONS_DOCUMENT_PROPERTIES)
609
                // ->setOpenMode(WidgetLink::OPEN_DIALOG_AND_RELOAD)
610
                // );
611
                
612
                $this->filterMenu->addSeparator();
613
                $this->filterMenu->addItem($W->Link($App->translate('Manage filters...'), $proxy->manageFilters())
614
                    ->addClass('icon', \Func_Icons::ACTIONS_DOCUMENT_PROPERTIES)
615
                    ->setOpenMode(WidgetLink::OPEN_DIALOG));
616
                $this->filterMenu->addSeparator();
617
            }
618
            
619
            $this->filterMenu->addItem($W->Link($App->translate('Save current filter...'), $proxy->confirmSaveCurrentFilter())
620
                ->addClass('icon', \Func_Icons::ACTIONS_DOCUMENT_SAVE)
621
                ->setOpenMode(WidgetLink::OPEN_DIALOG));
622
        }
623
        
624
        $form = $W->Form($id);
625
        $form->setReadOnly(true);
626
        $form->setName('filter');
627
        $form->colon();
628
        $form->checkUnsaved(false, null);
629
        
630
        $form->setLayout($W->FlowItems($activeFiltersFrame, $W->FlowItems($advancedFiltersFrame, $this->submit, $W->FlowItems($this->reset, $this->filterMenu) // $this->save,
631
        // $this->select,
632
        )
633
            ->setHorizontalSpacing(2, 'em')
634
            ->setVerticalSpacing(1, 'em'))
635
            ->setVerticalSpacing(1, 'em')
636
            ->setVerticalAlign('bottom')
637
            ->addClass(\Func_Icons::ICON_LEFT_16));
638
        
639
        $form->setHiddenValue('tg', $App->controllerTg);
640
        $form->setAnchor($this->getAnchor());
641
        
642
        return $form;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $form returns the type Widget_Form which is incompatible with the documented return type Capwelton\Widgets\Widgets\Form\WidgetForm.
Loading history...
643
    }
644
    
645
    /**
646
     * Get a advanced filter panel
647
     * Use setPageLength to define the default number of items per page
648
     *
649
     * @param array $filter
650
     *            Filter values
651
     * @param string $name
652
     *            filter form name
653
     * @param string $anchor
654
     *            anchor in destination page of filter, the can be a WidgetTab id
655
     * @return WidgetFilter
656
     */
657
    public function advancedFilterPanel($filter = null, $name = null)
658
    {
659
        $W = bab_Widgets();
660
        
661
        $filterPanel = $W->Filter();
662
        $filterPanel->setLayout($W->VBoxLayout());
663
        if(isset($name)){
664
            $filterPanel->setName($name);
665
        }
666
        
667
        $pageLength = $this->getPageLength();
668
        if(null === $pageLength){
669
            $pageLength = 15;
670
        }
671
        
672
        $pageNumber = $this->getCurrentPage();
673
        if(null === $pageNumber){
674
            $pageNumber = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $pageNumber is dead and can be removed.
Loading history...
675
        }
676
        
677
        $this->setPageLength(isset($filter['pageSize']) ? $filter['pageSize'] : $pageLength);
678
        // $this->setCurrentPage(isset($filter['pageNumber']) ? $filter['pageNumber'] : $pageNumber);
679
        
680
        if(isset($name)){
681
            $this->sortParameterName = $name . '[filter][sort]';
0 ignored issues
show
Bug Best Practice introduced by
The property sortParameterName does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
682
        }
683
        else{
684
            $this->sortParameterName = 'filter[sort]';
685
        }
686
        
687
        if(isset($filter['sort'])){
688
            $this->setSortField($filter['sort']);
689
        }
690
        elseif(! isset($this->sortField)){
691
            
692
            if(method_exists($this, 'getDefaultSortField')){
693
                $this->setSortField($this->getDefaultSortField());
694
            }
695
            else{
696
                $columns = $this->getVisibleColumns();
697
                $sortField = key($columns);
698
                $this->setSortField($sortField);
699
            }
700
        }
701
        
702
        $form = $this->getAdvancedFilterForm(null, $filter);
703
        
704
        if(isset($filter)){
705
            if(isset($name)){
706
                $path = array(
707
                    $name,
708
                    'filter'
709
                );
710
            }
711
            else{
712
                $path = array(
713
                    'filter'
714
                );
715
            }
716
            $form->setValues($filter, $path);
717
        }
718
        
719
        $filterPanel->setFilter($form);
720
        $filterPanel->setFiltered($this);
721
        
722
        return $filterPanel;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $filterPanel returns the type Widget_Filter which is incompatible with the documented return type Capwelton\Widgets\Widgets\Item\WidgetFilter.
Loading history...
723
    }
724
    
725
    /**
726
     * @param AppCtrlRecord $recordController
727
     * @return self
728
     */
729
    public function setRecordController(AppCtrlRecord $recordController)
730
    {
731
        $this->recordController = $recordController;
732
        return $this;
733
    }
734
    
735
    /**
736
     * @return AppCtrlRecord
737
     */
738
    public function getRecordController()
739
    {
740
        return $this->recordController;
741
    }
742
    
743
    /**
744
     * @return AppCtrlRecord
745
     */
746
    public function getRecordControllerProxy()
747
    {
748
        if(! isset($this->recordControllerProxy)){
749
            $this->recordControllerProxy = $this->getRecordController()->proxy();
750
        }
751
        return $this->recordControllerProxy;
752
    }
753
}
754
755
/**
756
 * @param string|ORMField $field
757
 * @param string|null $description
758
 *
759
 * @return WidgetTableModelViewColumn
760
 */
761
function app_TableModelViewColumn($field, $description = null)
762
{
763
    if(null === $field){
0 ignored issues
show
introduced by
The condition null === $field is always false.
Loading history...
764
        return null;
765
    }
766
    
767
    if(! isset($description) && $field instanceof ORMField){
768
        $App = $field->getParentSet()->App();
769
        $description = $App->translate($field->getDescription());
770
    }
771
    
772
    return new WidgetTableModelViewColumn($field, $description);
773
}