Issues (39)

src/RegistryPage.php (8 issues)

1
<?php
2
3
namespace SilverStripe\Registry;
4
5
use Page;
0 ignored issues
show
The type Page 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...
6
use SilverStripe\Control\Controller;
7
use SilverStripe\Core\ClassInfo;
8
use SilverStripe\Forms\DropdownField;
9
use SilverStripe\Forms\NumericField;
10
use SilverStripe\ORM\ArrayList;
11
use SilverStripe\ORM\DataList;
12
use SilverStripe\ORM\DataObject;
13
use SilverStripe\ORM\FieldType\DBDatetime;
14
use SilverStripe\View\SSViewer;
15
use SilverStripe\View\ArrayData;
16
17
class RegistryPage extends Page
18
{
19
    private static $description = 'Shows large series of data in a filterable, searchable, and paginated list';
0 ignored issues
show
The private property $description is not used, and could be removed.
Loading history...
20
21
    private static $table_name = 'RegistryPage';
0 ignored issues
show
The private property $table_name is not used, and could be removed.
Loading history...
22
23
    private static $icon_class = 'font-icon-p-data';
0 ignored issues
show
The private property $icon_class is not used, and could be removed.
Loading history...
24
25
    private static $db = [
0 ignored issues
show
The private property $db is not used, and could be removed.
Loading history...
26
        'DataClass' => 'Varchar(100)',
27
        'PageLength' => 'Int',
28
    ];
29
30
    /**
31
     * The default length of a page of registry entries
32
     *
33
     * @config
34
     * @var integer
35
     */
36
    private static $page_length_default = 10;
0 ignored issues
show
The private property $page_length_default is not used, and could be removed.
Loading history...
37
38
    public function fieldLabels($includerelations = true)
39
    {
40
        $labels = parent::fieldLabels($includerelations);
41
        $labels['DataClass'] = _t(__CLASS__ . '.DataClassFieldLabel', "Data Class");
42
        $labels['PageLength'] = _t(__CLASS__ . '.PageLengthFieldLabel', "Results page length");
43
44
        return $labels;
45
    }
46
47
    public function getDataClasses()
48
    {
49
        $map = array();
50
        foreach (ClassInfo::implementorsOf(RegistryDataInterface::class) as $class) {
51
            $map[$class] = singleton($class)->singular_name();
52
        }
53
        return $map;
54
    }
55
56
    public function getDataClass()
57
    {
58
        return $this->getField('DataClass');
59
    }
60
61
    public function getDataSingleton()
62
    {
63
        $class = $this->getDataClass();
64
        if (!$class) {
65
            return null;
66
        }
67
        return singleton($this->getDataClass());
68
    }
69
70
    public function getPageLength()
71
    {
72
        $length = $this->getField('PageLength');
73
        return $length ?: $this->config()->get('page_length_default');
74
    }
75
76
    public function getCMSFields()
77
    {
78
        $fields = parent::getCMSFields();
79
        $classDropdown = DropdownField::create('DataClass', $this->fieldLabel('DataClass'), $this->getDataClasses());
80
        $classDropdown->setEmptyString(_t(__CLASS__ . '.SelectDropdownDefault', 'Select one'));
81
        $fields->addFieldToTab('Root.Main', $classDropdown, 'Content');
82
        $fields->addFieldToTab(
83
            'Root.Main',
84
            NumericField::create('PageLength', $this->fieldLabel('PageLength')),
85
            'Content'
86
        );
87
        return $fields;
88
    }
89
90
    public function LastUpdated()
91
    {
92
        $elements = DataList::create($this->dataClass);
93
        $lastUpdated = DBDatetime::create('LastUpdated');
94
        $lastUpdated->setValue($elements->max('LastEdited'));
95
        return $lastUpdated;
96
    }
97
98
    /**
99
     * Modified version of Breadcrumbs, to cater for viewing items.
100
     */
101
    public function Breadcrumbs(
102
        $maxDepth = 20,
103
        $unlinked = false,
104
        $stopAtPageType = false,
105
        $showHidden = false,
106
        $delimiter = '&raquo;'
107
    ) {
108
        $page = $this;
109
        $pages = [];
110
111
        while ($page
112
            && (!$maxDepth || count($pages) < $maxDepth)
113
            && (!$stopAtPageType || $page->ClassName != $stopAtPageType)
114
        ) {
115
            if ($showHidden || $page->ShowInMenus || ($page->ID == $this->ID)) {
116
                $pages[] = $page;
117
            }
118
119
            $page = $page->Parent;
120
        }
121
122
        // Add on the item we're currently showing.
123
        $controller = Controller::curr();
124
        if ($controller) {
0 ignored issues
show
$controller is of type SilverStripe\Control\Controller, thus it always evaluated to true.
Loading history...
125
            $request = $controller->getRequest();
126
            if ($request->param('Action') === 'show') {
127
                $id = $request->param('ID');
128
                if ($id) {
129
                    $object = DataObject::get_by_id($this->getDataClass(), $id);
0 ignored issues
show
$id of type string is incompatible with the type boolean|integer expected by parameter $idOrCache of SilverStripe\ORM\DataObject::get_by_id(). ( Ignorable by Annotation )

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

129
                    $object = DataObject::get_by_id($this->getDataClass(), /** @scrutinizer ignore-type */ $id);
Loading history...
130
                    array_unshift($pages, $object);
131
                }
132
            }
133
        }
134
135
        $template = SSViewer::create('BreadcrumbsTemplate');
136
137
        return $template->process($this->customise(ArrayData::create([
138
            'Pages' => ArrayList::create(array_reverse($pages))
139
        ])));
140
    }
141
}
142