|
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); |
|
|
|
|
|
|
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)); |
|
|
|
|
|
|
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]; |
|
|
|
|
|
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
return ['/file/view', 'id' => $this->file->id]; |
|
|
|
|
|
|
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
|
|
|
|
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@propertyannotation to your class or interface to document the existence of this variable.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.