|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Form widget for image upload in ImageBehavior |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/inblank/yii2-image |
|
6
|
|
|
* @copyright Copyright (c) 2017 Pavel Aleksandrov <[email protected]> |
|
7
|
|
|
* @license http://opensource.org/licenses/MIT |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace inblank\image; |
|
11
|
|
|
|
|
12
|
|
|
use yii\helpers\Html; |
|
13
|
|
|
use yii\widgets\ActiveForm; |
|
14
|
|
|
use yii\widgets\InputWidget; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class ImageUploadWidget |
|
18
|
|
|
* @property |
|
19
|
|
|
*/ |
|
20
|
|
|
class ImageUploadWidget extends InputWidget |
|
21
|
|
|
{ |
|
22
|
|
|
public $registerAssets = true; |
|
23
|
|
|
|
|
24
|
|
|
public $messages = [ |
|
25
|
|
|
'change' => 'Change Image', |
|
26
|
|
|
'clear' => 'Clear Image', |
|
27
|
|
|
'reset' => 'Reset Image', |
|
28
|
|
|
]; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @return string |
|
32
|
|
|
*/ |
|
33
|
|
|
public function run() |
|
34
|
|
|
{ |
|
35
|
|
|
if ($this->registerAssets) { |
|
36
|
|
|
ImageAsset::register($this->getView()); |
|
37
|
|
|
} |
|
38
|
|
|
$form = end(ActiveForm::$stack); |
|
39
|
|
|
if (!isset($form->options['enctype'])) { |
|
40
|
|
|
$form->options['enctype'] = 'multipart/form-data'; |
|
41
|
|
|
} |
|
42
|
|
|
$str = '<div class="upload-image-preview" style="background-image: url(' . $this->model->imageUrl . ')"></div>' . |
|
43
|
|
|
|
|
44
|
|
|
'<div class="upload-image-control">' . |
|
45
|
|
|
|
|
46
|
|
|
'<a class="btn btn-primary upload-image-change" title="' . $this->messages['change'] . '">' . |
|
47
|
|
|
Html::activeFileInput($this->model, $this->attribute, $this->options) . |
|
48
|
|
|
'<span class="glyphicon glyphicon-save"></span>' . |
|
49
|
|
|
'</a>' . |
|
50
|
|
|
|
|
51
|
|
|
'<a class="btn btn-danger upload-image-clear" title="' . $this->messages['clear'] . '">' . |
|
52
|
|
|
'<span class="glyphicon glyphicon-trash"></span>' . |
|
53
|
|
|
'</a>' . |
|
54
|
|
|
|
|
55
|
|
|
'<a class="btn btn-success upload-image-reset" title="' . $this->messages['reset'] . '">' . |
|
56
|
|
|
'<span class="glyphicon glyphicon-repeat"></span>' . |
|
57
|
|
|
'</a>' . |
|
58
|
|
|
|
|
59
|
|
|
'</div>'; |
|
60
|
|
|
|
|
61
|
|
|
$options = [ |
|
62
|
|
|
'class' => ['upload-image'], |
|
63
|
|
|
'data' => ['image-empty' => $this->model->imageDefaultUrl], |
|
64
|
|
|
]; |
|
65
|
|
|
if ($this->model->imageResizeStrategy === ImageBehavior::CROP) { |
|
66
|
|
|
$options['class'][] = 'upload-image-crop'; |
|
67
|
|
|
} else { |
|
68
|
|
|
$options['class'][] = 'upload-image-frame'; |
|
69
|
|
|
} |
|
70
|
|
|
if (!$this->model->{$this->attribute}) { |
|
71
|
|
|
$options['class'][] = 'upload-image-empty'; |
|
72
|
|
|
} else { |
|
73
|
|
|
$options['data']['image'] = $this->model->imageUrl; |
|
74
|
|
|
} |
|
75
|
|
|
return Html::tag('div', $str, $options); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|