ProductDoc::onAfterWrite()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.2559

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
ccs 3
cts 5
cp 0.6
crap 2.2559
1
<?php
2
3
namespace Dynamic\ProductCatalog\Docs;
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 ProductDoc 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
        'Name' => 'Varchar(255)',
18
        'Title' => 'Varchar(255)',
19
        'Content' => 'HTMLText',
20
        'FileLink' => 'Varchar(255)',
21
    );
22
23
    /**
24
     * @var array
25
     */
26
    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...
27
        'Image' => Image::class,
28
        'Download' => File::class,
29
    );
30
31
    /**
32
     * @var string
33
     */
34
    private static $table_name = 'ProductDoc';
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 $summary_fields = array(
0 ignored issues
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
40
        'Name' => 'Name',
41
        'Title' => 'Title',
42
    );
43
44
    /**
45
     * @var array
46
     */
47
    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...
48
        'Name',
49
        'Title',
50
    );
51
52
    /**
53
     * @var string
54
     */
55
    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...
56
57
    /**
58
     * @return \SilverStripe\Forms\FieldList
59
     */
60 6
    public function getCMSFields()
61
    {
62 6
        $fields = parent::getCMSFields();
63
64 6
        $fields->removeByName([
65 6
            'Products',
66
        ]);
67
68 6
        $file = UploadField::create('Download')
69 6
            ->setFolderName('Uploads/FileDownloads')
70
            //->setAllowedFileCategories('doc')
71
        ;
72
73 6
        $fields->addFieldsToTab('Root.Download', array(
74 6
            $file,
75 6
            TextField::create('FileLink')
76 6
                ->setDescription('URL of external file. will display on page if no Download is specified above.')
77 6
                ->setAttribute('placeholder', 'http://'),
78
        ));
79
80 6
        $fields->insertBefore(
81 6
            UploadField::create('Image')
82
                //->setFolderName('Uploads/ProductDocs/Images')
83 6
                ->setDescription('Preview image of file'),
84 6
            '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

84
            /** @scrutinizer ignore-type */ 'Content'
Loading history...
85
        );
86
87 6
        return $fields;
88
    }
89
90
    /**
91
     * if SetClass dropdown is set, create a new instance of the new class.
92
     */
93 10
    public function onAfterWrite()
94
    {
95 10
        parent::onAfterWrite();
96 10
        if (isset($_REQUEST['SetClass'])) {
97
            $object = $this->newClassInstance($_REQUEST['SetClass']);
98
            $object->write();
99
        }
100
    }
101
}
102