Passed
Push — master ( b7ad32...4866c6 )
by Jason
01:33
created

Product::Image()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Dynamic\Products\Page;
4
5
use Bummzack\SortableFile\Forms\SortableUploadField;
6
use Dynamic\Products\Model\Brochure;
7
use SilverStripe\Assets\Image;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\Forms\GridField\GridField;
10
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
11
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
12
use SilverStripe\Forms\TextField;
13
use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton;
14
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
15
16
class Product extends \Page
17
{
18
    /**
19
     * @var string
20
     */
21
    private static $table_name = 'Product';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
22
23
    /**
24
     * @var array
25
     */
26
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
27
        'Code' => 'Varchar(100)',
28
    ];
29
30
    /**
31
     * @var array
32
     */
33
    private static $many_many = [
0 ignored issues
show
introduced by
The private property $many_many is not used, and could be removed.
Loading history...
34
        'Images' => Image::class,
35
        'Brochures' => Brochure::class,
36
    ];
37
38
    /**
39
     * @var array
40
     */
41
    private static $many_many_extraFields = [
0 ignored issues
show
introduced by
The private property $many_many_extraFields is not used, and could be removed.
Loading history...
42
        'Images' => [
43
            'SortOrder' => 'Int',
44
        ],
45
        'Brochures' => [
46
            'SortOrder' => 'Int',
47
        ],
48
    ];
49
50
    /**
51
     *
52
     */
53
    private static $owns = [
0 ignored issues
show
introduced by
The private property $owns is not used, and could be removed.
Loading history...
54
        'Images',
55
    ];
56
57
    /**
58
     * @var array
59
     */
60
    private static $allowed_children = [];
0 ignored issues
show
introduced by
The private property $allowed_children is not used, and could be removed.
Loading history...
61
62
    /**
63
     * @return \SilverStripe\Forms\FieldList
64
     */
65
    public function getCMSFields()
66
    {
67
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
68
            $fields->insertBefore(
69
                'Content',
70
                TextField::create('Code', 'Product code')
71
            );
72
73
            // Images tab
74
            $images = SortableUploadField::create('Images')
75
                ->setSortColumn('SortOrder')
76
                ->setIsMultiUpload(true)
77
                ->setAllowedFileCategories('image')
78
                ->setFolderName('Uploads/Products/Images');
79
80
            $fields->addFieldsToTab('Root.Images', [
81
                $images,
82
            ]);
83
84
            if ($this->ID) {
85
                // Brochures
86
                $config = GridFieldConfig_RecordEditor::create();
87
                $config->addComponents([
88
                    new GridFieldOrderableRows('SortOrder'),
89
                    new GridFieldAddExistingSearchButton()
90
                ])
91
                    ->removeComponentsByType([
92
                        GridFieldAddExistingAutocompleter::class
93
                    ]);
94
95
                $brochures = GridField::create(
96
                    'Brochures',
97
                    'Brochures',
98
                    $this->Brochures()->sort('SortOrder'),
0 ignored issues
show
Bug introduced by
The method Brochures() does not exist on Dynamic\Products\Page\Product. 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

98
                    $this->/** @scrutinizer ignore-call */ 
99
                           Brochures()->sort('SortOrder'),
Loading history...
99
                    $config
100
                );
101
                $fields->addFieldsToTab('Root.Files.Brochures', array(
102
                    $brochures,
103
                ));
104
            }
105
        });
106
107
        return parent::getCMSFields();
108
    }
109
110
    /**
111
     * @return mixed
112
     */
113
    public function getImage()
114
    {
115
        if ($this->Images()->exists()) {
0 ignored issues
show
Bug introduced by
The method Images() does not exist on Dynamic\Products\Page\Product. 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

115
        if ($this->/** @scrutinizer ignore-call */ Images()->exists()) {
Loading history...
116
            $image = $this->Images()->first();
117
            if ($image->ImageID > 0) {
118
                return $image->Image();
119
            }
120
        }
121
122
        return false;
123
    }
124
125
    /**
126
     * @return mixed
127
     */
128
    public function getThumbnail()
129
    {
130
        if ($image = $this->getImage()) {
131
            if ($thumb = Image::get()->byID($image->ID)) {
132
                return $thumb->CMSThumbnail();
133
            }
134
        }
135
136
        return false;
137
    }
138
}
139