Issues (13)

src/models/IconSVG.php (8 issues)

1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: magnum34
5
 * Date: 21.07.2019
6
 * Time: 21:29
7
 */
8
9
namespace Magnum34\SilverStripeSVGGO\Models;
10
11
use SilverStripe\Assets\File;
12
use SilverStripe\Core\Convert;
13
use SilverStripe\Forms\LiteralField;
14
use SilverStripe\Forms\RequiredFields;
15
use SilverStripe\Forms\TextField;
16
use SilverStripe\ORM\DataObject;
17
use SilverStripe\Assets\Image;
18
use SilverStripe\AssetAdmin\Forms\UploadField;
0 ignored issues
show
The type SilverStripe\AssetAdmin\Forms\UploadField 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...
19
use SilverStripe\ORM\FieldType\DBHTMLText;
20
use SilverStripe\Security\Permission;
21
22
23
class IconSVG extends DataObject
24
{
25
26
    private static $db = [
0 ignored issues
show
The private property $db is not used, and could be removed.
Loading history...
27
        'Title' => 'Varchar(255)',
28
    ];
29
30
    private static $has_one = [
0 ignored issues
show
The private property $has_one is not used, and could be removed.
Loading history...
31
        'Image' => File::class,
32
    ];
33
34
    private static $owns = [
0 ignored issues
show
The private property $owns is not used, and could be removed.
Loading history...
35
        'Image'
36
    ];
37
38
    protected $cssClass;
39
40
41
    public function getCMSFields()
42
    {
43
        $fields = parent::getCMSFields();
44
        $fields->addFieldToTab('Root.Main', TextField::create('Title', 'Title'));
45
        $fields->addFieldToTab('Root.Main',
46
            UploadField::create('Image', 'Image Icon')
47
                ->setAllowedExtensions(['svg', 'png', 'jpg', 'jpeg'])
48
                ->setRightTitle('(only .svg , .jpg, .png, .jpeg)'));
49
50
        return $fields;
51
    }
52
53
    public function getCMSValidator()
54
    {
55
        return new RequiredFields([
56
            'Title'
57
        ]);
58
    }
59
60
61
    private static $summary_fields = [
0 ignored issues
show
The private property $summary_fields is not used, and could be removed.
Loading history...
62
        'Title' => 'Title',
63
        'Thumbnail' => 'Thumbnail'
64
    ];
65
66
    /**
67
     * @param string $class
68
     * @return $this
69
     */
70
    public function setCSSClass($class)
71
    {
72
        $this->cssClass = $class;
73
        return $this;
74
    }
75
76
    /**
77
     * Gets the classes for this link.
78
     *
79
     * @return array|string
80
     */
81
    public function getClasses()
82
    {
83
        $classes = explode(' ', $this->cssClass);
84
        $this->extend('updateClasses', $classes);
85
        $classes = implode(' ', $classes);
86
        return $classes;
87
    }
88
89
90
    public function getThumbnail()
91
    {
92
        if ($this->Image()->ID) {
0 ignored issues
show
The method Image() does not exist on Magnum34\SilverStripeSVGGO\Models\IconSVG. 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

92
        if ($this->/** @scrutinizer ignore-call */ Image()->ID) {
Loading history...
93
94
            if ($this->Image()->getExtension() == 'svg') {
95
                $obj = DBHTMLText::create();
96
                $obj->setValue(file_get_contents(BASE_PATH . $this->Image()->Link()));
97
                return ($obj);
98
            } else {
99
                return $this->Image()->CMSThumbnail();
100
            }
101
        } else {
102
            return '(No Image)';
103
        }
104
    }
105
106
    public function getTitleThumbnail()
107
    {
108
109
        if ($this->Image()->ID) {
110
            $image = $this->Image()->Link();
111
            return LiteralField::create('TitleThumbnail', "<p>$this->Title</p><img src=\"$image\" width=\"50\" height=\"50\" ");
112
113
        } else {
114
            return $this->Title;
115
        }
116
    }
117
118
119
    /**
120
     * Renders an HTML anchor tag for this link
121
     *
122
     * @return DBHTMLText|string
123
     */
124
    public function forTemplate()
125
    {
126
        $link = $this->Image()->URL;
127
        $svg = $this->Image()->SVGRAW();
128
        $extraClass = $this->getClasses();
129
        if ($this->Image->isSVG()) {
0 ignored issues
show
The method isSVG() does not exist on null. ( Ignorable by Annotation )

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

129
        if ($this->Image->/** @scrutinizer ignore-call */ isSVG()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug Best Practice introduced by
The property Image does not exist on Magnum34\SilverStripeSVGGO\Models\IconSVG. Since you implemented __get, consider adding a @property annotation.
Loading history...
130
            return "<div class=\"$extraClass\">$svg</div>";
131
        } else {
132
            return "<img class=\"$extraClass \" src=\"$link\" alt=\"icon avatar\">";
133
        }
134
135
    }
136
137
138
    public function canView($member = null)
139
    {
140
        return Permission::check('CMS_ACCESS_CMSMain', 'any', $member);
141
    }
142
143
    public function canCreate($member = null, $context = array())
144
    {
145
        return Permission::check('CMS_ACCESS_CMSMain', 'any', $member);
146
    }
147
148
    public function canEdit($member = null)
149
    {
150
        return Permission::check('CMS_ACCESS_CMSMain', 'any', $member);
151
    }
152
153
    public function canDelete($member = null)
154
    {
155
        return Permission::check('CMS_ACCESS_CMSMain', 'any', $member);
156
    }
157
}
158