ProductFile   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 3
eloc 42
c 2
b 0
f 0
dl 0
loc 97
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getCMSFields() 0 32 1
A onAfterWrite() 0 6 2
1
<?php
2
3
namespace Dynamic\Products\Model;
4
5
use SilverStripe\AssetAdmin\Forms\UploadField;
6
use SilverStripe\Assets\File;
7
use SilverStripe\Assets\Image;
8
use SilverStripe\Forms\TextField;
9
use SilverStripe\ORM\DataObject;
10
11
class ProductFile extends DataObject
12
{
13
    /**
14
     * @var array
15
     */
16
    private static $db = array(
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
17
        'Title' => 'Varchar(255)',
18
        'Content' => 'HTMLText',
19
        'FileLink' => 'Varchar(255)',
20
    );
21
22
    /**
23
     * @var array
24
     */
25
    private static $has_one = array(
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
26
        'Image' => Image::class,
27
        'Download' => File::class,
28
    );
29
30
    /**
31
     * @var array
32
     */
33
    private static $owns = [
0 ignored issues
show
introduced by
The private property $owns is not used, and could be removed.
Loading history...
34
        'Image',
35
        'Download',
36
    ];
37
38
    /**
39
     * @var string
40
     */
41
    private static $table_name = 'ProductFile';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
42
43
    /**
44
     * @var array
45
     */
46
    private static $summary_fields = array(
0 ignored issues
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
47
        'Title' => 'Title',
48
    );
49
50
    /**
51
     * @var array
52
     */
53
    private static $searchable_fields = array(
0 ignored issues
show
introduced by
The private property $searchable_fields is not used, and could be removed.
Loading history...
54
        'Title',
55
    );
56
57
    /**
58
     * @var string
59
     */
60
    private static $default_sort = 'Title';
0 ignored issues
show
introduced by
The private property $default_sort is not used, and could be removed.
Loading history...
61
62
    /**
63
     * @return \SilverStripe\Forms\FieldList
64
     */
65
    public function getCMSFields()
66
    {
67
        $fields = parent::getCMSFields();
68
69
        $file = UploadField::create('Download')
70
            ->setAllowedMaxFileNumber(1)
71
            ->setFolderName('Uploads/ProductFiles/File')
72
            ->setAllowedFileCategories('document')
73
            ->setDescription('Product file for download')
74
        ;
75
76
        $fields->addFieldsToTab(
77
            'Root.Main',
78
            [
79
                $file,
80
                TextField::create('FileLink')
81
                    ->setDescription('URL of external file. Will display on page if no file is specified.')
82
                    ->setAttribute('placeholder', 'http://'),
83
            ],
84
            'Content'
85
        );
86
87
        $fields->insertBefore(
88
            UploadField::create('Image')
89
                ->setAllowedMaxFileNumber(1)
90
                ->setFolderName('Uploads/ProductDocs/Images')
91
                ->setAllowedFileCategories('image')
92
                ->setDescription('Optional preview image of file'),
93
            'Content'
0 ignored issues
show
Bug introduced by
'Content' of type string is incompatible with the type SilverStripe\Forms\FormField expected by parameter $item of SilverStripe\Forms\FieldList::insertBefore(). ( Ignorable by Annotation )

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

93
            /** @scrutinizer ignore-type */ 'Content'
Loading history...
94
        );
95
96
        return $fields;
97
    }
98
99
    /**
100
     * if SetClass dropdown is set, create a new instance of the new class.
101
     */
102
    public function onAfterWrite()
103
    {
104
        parent::onAfterWrite();
105
        if (isset($_REQUEST['SetClass'])) {
106
            $object = $this->newClassInstance($_REQUEST['SetClass']);
107
            $object->write();
108
        }
109
    }
110
}
111