|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace maxmirazh33\image; |
|
4
|
|
|
|
|
5
|
|
|
use yii\base\InvalidConfigException; |
|
6
|
|
|
use yii\web\JsExpression; |
|
7
|
|
|
use yii\widgets\InputWidget; |
|
8
|
|
|
use Yii; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class for uploadable and cropable image widget |
|
12
|
|
|
* |
|
13
|
|
|
* Usage: |
|
14
|
|
|
* ``` |
|
15
|
|
|
* ... |
|
16
|
|
|
* echo $form->field($model, 'image')->widget('maxmirazh33\image\Widget'); |
|
17
|
|
|
* ... |
|
18
|
|
|
* ``` |
|
19
|
|
|
*/ |
|
20
|
|
|
class Widget extends InputWidget |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var bool need crop |
|
24
|
|
|
*/ |
|
25
|
|
|
private $crop = false; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var array JCrop settings |
|
29
|
|
|
*/ |
|
30
|
|
|
public $jcropSettings = []; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var array default JCrop settings |
|
34
|
|
|
*/ |
|
35
|
|
|
private $jcropDefaultSettings = [ |
|
36
|
|
|
'bgColor' => '#ffffff', |
|
37
|
|
|
'minSize' => [100, 100], |
|
38
|
|
|
'keySupport' => false, // Important param to hide jCrop radio button. |
|
39
|
|
|
'setSelect' => [0, 0, 9999, 9999], |
|
40
|
|
|
'boxWidth' => 568, |
|
41
|
|
|
'boxHeight' => 400, |
|
42
|
|
|
]; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @inheritdoc |
|
46
|
|
|
*/ |
|
47
|
|
|
public function init() |
|
48
|
|
|
{ |
|
49
|
|
|
if (!$this->hasModel() && $this->name === null) { |
|
50
|
|
|
throw new InvalidConfigException("'model' and 'attribute' properties must be specified."); |
|
51
|
|
|
} |
|
52
|
|
|
parent::init(); |
|
53
|
|
|
|
|
54
|
|
|
$this->registerTranslations(); |
|
55
|
|
|
Asset::register($this->getView()); |
|
56
|
|
|
|
|
57
|
|
|
foreach ($this->model->behaviors as $b) { |
|
58
|
|
|
if ($b instanceof Behavior) { |
|
59
|
|
|
foreach ($b->attributes as $attr => $options) { |
|
60
|
|
|
Behavior::ensureAttribute($attr, $options); |
|
61
|
|
|
if ($attr == $this->attribute) { |
|
62
|
|
|
if ($b->needCrop($attr)) { |
|
63
|
|
|
$this->crop = true; |
|
64
|
|
|
CropAsset::register($this->getView()); |
|
65
|
|
|
$this->jcropSettings = array_merge($this->jcropDefaultSettings, $this->jcropSettings); |
|
66
|
|
|
$this->jcropSettings['onSelect'] = new JsExpression('function (c) { |
|
67
|
|
|
setCoords("' . $this->getSelector() . '", c); |
|
68
|
|
|
}'); |
|
69
|
|
|
if (isset($options['width'], $options['height'])) { |
|
70
|
|
|
$this->jcropSettings['aspectRatio'] = $options['width'] / $options['height']; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
break; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
break; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Register widget translations. |
|
83
|
|
|
*/ |
|
84
|
|
|
public function registerTranslations() |
|
85
|
|
|
{ |
|
86
|
|
|
if (!isset(Yii::$app->i18n->translations['maxmirazh33/image']) && !isset(Yii::$app->i18n->translations['maxmirazh33/*'])) { |
|
87
|
|
|
Yii::$app->i18n->translations['maxmirazh33/image'] = [ |
|
88
|
|
|
'class' => 'yii\i18n\PhpMessageSource', |
|
89
|
|
|
'basePath' => '@maxmirazh33/image/messages', |
|
90
|
|
|
'fileMap' => [ |
|
91
|
|
|
'maxmirazh33/image' => 'image.php' |
|
92
|
|
|
], |
|
93
|
|
|
'forceTranslation' => true |
|
94
|
|
|
]; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @inheritdoc |
|
100
|
|
|
*/ |
|
101
|
|
|
public function run() |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->render( |
|
104
|
|
|
'view', |
|
105
|
|
|
[ |
|
106
|
|
|
'selector' => $this->getSelector(), |
|
107
|
|
|
'model' => $this->model, |
|
108
|
|
|
'attribute' => $this->attribute, |
|
109
|
|
|
'crop' => $this->crop, |
|
110
|
|
|
'jcropSettings' => $this->jcropSettings, |
|
111
|
|
|
] |
|
112
|
|
|
); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @return string Widget selector |
|
117
|
|
|
* @throws \ReflectionException |
|
118
|
|
|
*/ |
|
119
|
|
|
public function getSelector() |
|
120
|
|
|
{ |
|
121
|
|
|
$object = new \ReflectionClass($this->model); |
|
122
|
|
|
return mb_strtolower($object->getShortName()) . '-' . $this->attribute; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|