Passed
Push — master ( 01c5da...60d466 )
by Jason
03:06 queued 01:31
created

ElementFileList::getCMSFields()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 18
nc 1
nop 0
dl 0
loc 28
ccs 0
cts 10
cp 0
crap 12
rs 9.6666
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 $singular_name = 'File List Element';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
25
26
    /**
27
     * @var string
28
     */
29
    private static $plural_name = 'File List Elements';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
30
31
    /**
32
     * @var string
33
     */
34
    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...
35
36
    /**
37
     * @var array
38
     */
39
    private static $has_many = [
0 ignored issues
show
introduced by
The private property $has_many is not used, and could be removed.
Loading history...
40
        'Files' => FileListObject::class,
41
    ];
42
43
    /**
44
     * @var array
45
     */
46
    private static $owns = [
0 ignored issues
show
introduced by
The private property $owns is not used, and could be removed.
Loading history...
47
        'Files',
48
    ];
49
50
    /**
51
     * @var bool
52
     */
53
    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...
54
55
    /**
56
     * @return FieldList
57
     */
58
    public function getCMSFields()
59
    {
60
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
61
            if ($this->ID) {
62
                $field = $fields->dataFieldByName('Files');
63
                $fields->removeByName('Files');
64
65
                $config = $field->getConfig();
66
                $config
67
                    ->addComponents([
68
                        new GridFieldOrderableRows('SortOrder')
69
                    ])
70
                    ->removeComponentsByType([
71
                        GridFieldAddExistingAutocompleter::class,
72
                        GridFieldDeleteAction::class
73
                    ]);
74
                if (class_exists(BulkUploader::class)) {
75
                    $config->addComponents([
76
                        new BulkUploader()
77
                    ]);
78
                    $config->getComponentByType(BulkUploader::class)
79
                        ->setUfSetup('setFolderName', 'Uploads/FileList');
80
                }
81
                $fields->addFieldToTab('Root.Main', $field);
82
            }
83
        });
84
85
        return parent::getCMSFields();
86
    }
87
88
    /**
89
     * @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...
90
     */
91
    public function getSummary()
92
    {
93
        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

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