Passed
Push — master ( 8f6961...722018 )
by Jason
01:37
created

ElementFileList::fieldLabels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 7
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Dynamic\Elements\FileList\Elements;
4
5
use Colymba\BulkUpload\BulkUploader;
0 ignored issues
show
Bug introduced by
The type Colymba\BulkUpload\BulkUploader 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 DNADesign\Elemental\Models\BaseElement;
7
use Dynamic\Elements\FileList\Model\FileListObject;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
10
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
11
use SilverStripe\ORM\FieldType\DBField;
12
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
13
14
class ElementFileList extends BaseElement
15
{
16
    /**
17
     * @var string
18
     */
19
    private static $icon = 'font-icon-block-file-list';
0 ignored issues
show
introduced by
The private property $icon is not used, and could be removed.
Loading history...
20
21
    /**
22
     * @var string
23
     */
24
    private static $table_name = 'ElementFileList';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
25
26
    /**
27
     * @var array
28
     */
29
    private static $has_many = [
0 ignored issues
show
introduced by
The private property $has_many is not used, and could be removed.
Loading history...
30
        'Files' => FileListObject::class,
31
    ];
32
33
    /**
34
     * @var array
35
     */
36
    private static $owns = [
0 ignored issues
show
introduced by
The private property $owns is not used, and could be removed.
Loading history...
37
        'Files',
38
    ];
39
40
    /**
41
     * @var bool
42
     */
43
    private static $inline_editable = false;
0 ignored issues
show
introduced by
The private property $inline_editable is not used, and could be removed.
Loading history...
44
45
    /**
46
     * @param bool $includerelations
47
     * @return array
48
     */
49
    public function fieldLabels($includerelations = true)
50
    {
51
        $labels = parent::fieldLabels($includerelations);
52
53
        $labels['Files'] = _t(__CLASS__.'.FilesLabel', 'Files');
54
55
        return $labels;
56
    }
57
58
    /**
59
     * @return FieldList
60
     */
61
    public function getCMSFields()
62
    {
63
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
64
            if ($this->ID) {
65
                $field = $fields->dataFieldByName('Files');
66
                $fields->removeByName('Files');
67
68
                $config = $field->getConfig();
69
                $config
70
                    ->addComponents([
71
                        new GridFieldOrderableRows('SortOrder')
72
                    ])
73
                    ->removeComponentsByType([
74
                        GridFieldAddExistingAutocompleter::class,
75
                        GridFieldDeleteAction::class
76
                    ]);
77
                if (class_exists(BulkUploader::class)) {
78
                    $config->addComponents([
79
                        new BulkUploader()
80
                    ]);
81
                    $config->getComponentByType(BulkUploader::class)
82
                        ->setUfSetup('setFolderName', 'Uploads/FileList');
83
                }
84
                $fields->addFieldToTab('Root.Main', $field);
85
            }
86
        });
87
88
        return parent::getCMSFields();
89
    }
90
91
    /**
92
     * @return DBHTMLText
0 ignored issues
show
Bug introduced by
The type Dynamic\Elements\FileList\Elements\DBHTMLText 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...
93
     */
94
    public function getSummary()
95
    {
96
        if ($this->Files()->count() == 1) {
0 ignored issues
show
Bug introduced by
The method Files() does not exist on Dynamic\Elements\FileList\Elements\ElementFileList. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

96
        if ($this->/** @scrutinizer ignore-call */ Files()->count() == 1) {
Loading history...
97
            $label = ' file';
98
        } else {
99
            $label = ' files';
100
        }
101
        return DBField::create_field('HTMLText', $this->Files()->count() . $label)->Summary(20);
102
    }
103
104
    /**
105
     * @return array
106
     */
107
    protected function provideBlockSchema()
108
    {
109
        $blockSchema = parent::provideBlockSchema();
110
        $blockSchema['content'] = $this->getSummary();
111
        return $blockSchema;
112
    }
113
114
    /**
115
     * @return string
116
     */
117
    public function getType()
118
    {
119
        return _t(__CLASS__.'.BlockType', 'File List');
120
    }
121
}
122