Issues (12)

src/Block/FileBlock.php (9 issues)

1
<?php
2
3
namespace SilverStripe\ElementalFileBlock\Block;
4
5
use DNADesign\Elemental\Models\BaseElement;
6
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...
7
use SilverStripe\Assets\File;
8
use SilverStripe\Assets\Image_Backend;
9
use SilverStripe\Core\Manifest\ModuleResourceLoader;
10
use SilverStripe\Forms\FieldList;
11
use SilverStripe\ORM\FieldType\DBHTMLText;
12
13
class FileBlock extends BaseElement
14
{
15
    private static $has_one = [
0 ignored issues
show
The private property $has_one is not used, and could be removed.
Loading history...
16
        'File' => File::class,
17
    ];
18
19
    private static $owns = [
0 ignored issues
show
The private property $owns is not used, and could be removed.
Loading history...
20
        'File',
21
    ];
22
23
    private static $singular_name = 'file block';
0 ignored issues
show
The private property $singular_name is not used, and could be removed.
Loading history...
24
25
    private static $plural_name = 'file blocks';
0 ignored issues
show
The private property $plural_name is not used, and could be removed.
Loading history...
26
27
    private static $icon = 'font-icon-block-file';
0 ignored issues
show
The private property $icon is not used, and could be removed.
Loading history...
28
29
    private static $table_name = 'S_EB_FileBlock';
0 ignored issues
show
The private property $table_name is not used, and could be removed.
Loading history...
30
31
    public function getCMSFields()
32
    {
33
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
34
            /** @var UploadField $uploadField */
35
            $uploadField = $fields->fieldByName('Root.Main.File');
0 ignored issues
show
Are you sure the assignment to $uploadField is correct as $fields->fieldByName('Root.Main.File') targeting SilverStripe\Forms\FieldList::fieldByName() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
36
            $uploadField->setIsMultiUpload(false);
37
        });
38
39
        return parent::getCMSFields();
40
    }
41
42
    public function getType()
43
    {
44
        return _t(__CLASS__ . '.BlockType', 'File');
45
    }
46
47
    public function getSummary()
48
    {
49
        if ($this->File() && $this->File()->exists()) {
0 ignored issues
show
The method File() does not exist on SilverStripe\ElementalFileBlock\Block\FileBlock. 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

49
        if ($this->/** @scrutinizer ignore-call */ File() && $this->File()->exists()) {
Loading history...
50
            return $this->getSummaryThumbnail() . $this->File()->Title;
51
        }
52
        return '';
53
    }
54
55
    /**
56
     * Return file title and thumbnail for summary section of ElementEditor
57
     *
58
     * @return array
59
     */
60
    protected function provideBlockSchema()
61
    {
62
        $blockSchema = parent::provideBlockSchema();
63
        if ($this->File() && $this->File()->exists() && $this->File()->getIsImage()) {
64
            $blockSchema['fileURL'] = $this->File()->CMSThumbnail()->getURL();
65
            $blockSchema['fileTitle'] = $this->File()->getTitle();
66
        }
67
        return $blockSchema;
68
    }
69
70
    /**
71
     * Return a thumbnail of the file, if it's an image. Used in GridField preview summaries.
72
     *
73
     * @return DBHTMLText
74
     */
75
    public function getSummaryThumbnail()
76
    {
77
        $data = [];
78
79
        if ($this->File() && $this->File()->exists()) {
80
            if ($this->File()->getIsImage()) {
81
                // Stretch to maximum of 36px either way then trim the extra off
82
                if ($this->File()->getOrientation() === Image_Backend::ORIENTATION_PORTRAIT) {
83
                    $data['Image'] = $this->File()->ScaleWidth(36)->CropHeight(36);
84
                } else {
85
                    $data['Image'] = $this->File()->ScaleHeight(36)->CropWidth(36);
86
                }
87
            } else {
88
                $data = [
89
                    'Image' => ModuleResourceLoader::resourceURL(
90
                        'silverstripe/framework:client/images/app_icons/document_92.png'
91
                    ),
92
                    'IsPlaceholder' => true
93
                ];
94
            }
95
        }
96
97
        return $this->customise($data)->renderWith(__CLASS__ . '/SummaryThumbnail');
98
    }
99
}
100