Completed
Push — master ( 4770f2...b00f2c )
by Klochok
15:20
created

FileRender::getLink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
/*
4
 * HiPanel core package
5
 *
6
 * @link      https://hipanel.com/
7
 * @package   hipanel-core
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hipanel\widgets;
13
14
use hipanel\components\FileStorage;
15
use hipanel\helpers\ArrayHelper;
16
use hipanel\models\File;
17
use hipanel\widgets\filePreview\FilePreviewFactory;
18
use hipanel\widgets\filePreview\FilePreviewFactoryInterface;
19
use hipanel\widgets\filePreview\Dimensions;
20
use hipanel\widgets\filePreview\OutboundDimensions;
21
use hipanel\widgets\filePreview\types\PdfPreviewGenerator;
22
use hipanel\widgets\filePreview\UnsupportedMimeTypeException;
23
use hipanel\widgets\filePreview\InsetDimensions;
24
use hiqdev\assets\lightbox2\LightboxAsset;
25
use Yii;
26
use yii\base\InvalidConfigException;
27
use yii\base\Widget;
28
use yii\helpers\Html;
29
use yii\helpers\Url;
30
31
/**
32
 * @property mixed iconOptions
33
 */
34
class FileRender extends Widget
35
{
36
    /**
37
     * @var File
38
     */
39
    public $file;
40
41
    /**
42
     * @var int
43
     */
44
    public $thumbWidth = 64;
45
46
    /**
47
     * @var int
48
     */
49
    public $thumbHeight = 64;
50
51
    /**
52
     * @var string Name of the file storage component
53
     */
54
    public $fileStorageComponent = 'fileStorage';
55
56
    /**
57
     * @var FileStorage
58
     */
59
    protected $fileStorage;
60
61
    /**
62
     * @var array Options that will be passed to [[Html::a()]] for the image lightbox
63
     */
64
    public $lightboxLinkOptions = [];
65
66
    public $imageOptions = [];
67
68
    public $iconOptions = [];
69
70
    /**
71
     * @var array
72
     */
73
    private $extMatch = [
74
        'pdf' => '<div><i class="fa fa-file-pdf-o fa-2x"></i></div>',
75
        'doc' => '<div><i class="fa fa-file-word-o fa-2x"></i></div>',
76
        'docx' => '<div><i class="fa fa-file-word-o fa-2x"></i></div>',
77
        'xls' => '<div><i class="fa fa-file-excel-o fa-2x"></i></div>',
78
        'xlsx' => '<div><i class="fa fa-file-excel-o fa-2x"></i></div>',
79
    ];
80
81
    public function init()
82
    {
83
        parent::init();
84
85
        if (!($this->file instanceof File)) {
86
            throw new InvalidConfigException('The "file" property must instance of File class.');
87
        }
88
        $this->fileStorage = Yii::$app->get($this->fileStorageComponent);
89
    }
90
91
    public function run()
92
    {
93
        $this->registerClientScript();
94
        return $this->renderHtml();
95
    }
96
97
    private function registerClientScript()
98
    {
99
        $view = $this->getView();
100
        LightboxAsset::register($view);
101
        // Fix: Incorrect resizing of image #122
102
        $view->registerCss('.lightbox  .lb-image { max-width: inherit!important; }');
103
    }
104
105
    private function renderHtml()
106
    {
107
        $file = $this->file;
108
        $path = $this->fileStorage->get($file);
109
110
        /** @var FilePreviewFactoryInterface $factory */
111
        $factory = Yii::createObject(FilePreviewFactoryInterface::class);
112
        try {
113
            $generator = $factory->createGenerator($path);
114
            $dimensions = new InsetDimensions($generator->getDimensions(), new Dimensions($this->thumbWidth, $this->thumbWidth));
115
            $src = 'data: ' . $generator->getContentType() . ';base64,' . base64_encode($generator->asBytes($dimensions));
116
            if ($generator instanceof PdfPreviewGenerator) {
117
                return Html::a(Html::img($src, $this->imageOptions), $this->getLink(), ['target' => '_blank']);
118
            } else {
119
                $linkOptions = ArrayHelper::merge(['data-lightbox' => 'file-' . $file->id], $this->lightboxLinkOptions);
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<hipanel\models\File>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
120
                return Html::a(Html::img($src, $this->imageOptions), $this->getLink(), $linkOptions);
121
            }
122
        } catch (UnsupportedMimeTypeException $e) {
123
            return Html::a($this->getExtIcon($file->type), $this->getLink(true));
124
        }
125
    }
126
127
    private function getLink($download = false)
128
    {
129
        return Url::to($this->getRoute($download));
0 ignored issues
show
Documentation introduced by
$download is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
130
    }
131
132
    /**
133
     * @param string $download whether to return route to download page
134
     *
135
     * @return array
136
     */
137
    public function getRoute($download)
138
    {
139
        if ($download) {
140
            return ['/file/get', 'id' => $this->file->id];
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<hipanel\models\File>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
141
        }
142
143
        return ['/file/view', 'id' => $this->file->id];
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<hipanel\models\File>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
144
    }
145
146
    private function getExtIcon($ext)
147
    {
148
        $default = '<i class="fa fa-file-text-o fa-2x"></i>';
149
        if (array_key_exists($ext, $this->extMatch)) {
150
            return $this->extMatch[$ext];
151
        } else {
152
            return $default;
153
        }
154
    }
155
}
156