ImageManagerInputWidget.php
4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
namespace backend\components\imagemanager\components;
use Yii;
use yii\widgets\InputWidget;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\helpers\Url;
use backend\components\imagemanager\models\ImageManager;
use backend\components\imagemanager\assets\ImageManagerInputAsset;
class ImageManagerInputWidget extends InputWidget {
/**
* @var null|integer The aspect ratio the image needs to be cropped in (optional)
*/
public $aspectRatio = null; //option info: https://github.com/fengyuanchen/cropper/#aspectratio
/**
* @var int Define the viewMode of the cropper
*/
public $cropViewMode = 1; //option info: https://github.com/fengyuanchen/cropper/#viewmode
/**
* @var bool Show a preview of the image under the input
*/
public $showPreview = true;
/**
* @var bool Show a confirmation message when de-linking a image from the input
*/
public $showDeletePickedImageConfirm = false;
/**
* @inheritdoc
*/
public function init() {
parent::init();
//set language
if (!isset(Yii::$app->i18n->translations['imagemanager'])) {
Yii::$app->i18n->translations['imagemanager'] = [
'class' => 'yii\i18n\PhpMessageSource',
'sourceLanguage' => 'en',
'basePath' => '@noam148/imagemanager/messages'
];
}
}
/**
* @inheritdoc
*/
public function run() {
//default
$ImageManager_id = null;
$mImageManager = null;
$sFieldId = null;
//start input group
$field = "<div class='image-manager-input'>";
$field .= "<div class='input-group'>";
//set input fields
if ($this->hasModel()) {
//get field id
$sFieldId = Html::getInputId($this->model, $this->attribute);
$sFieldNameId = $sFieldId . "_name";
//get attribute name
$sFieldAttributeName = Html::getAttributeName($this->attribute);
//get filename from selected file
$ImageManager_id = $this->model->{$sFieldAttributeName};
$ImageManager_fileName = null;
$mImageManager = ImageManager::findOne($ImageManager_id);
if ($mImageManager !== null) {
$ImageManager_fileName = $mImageManager->fileName;
}
//create field
$field .= Html::textInput($this->attribute, $ImageManager_fileName, ['class' => 'form-control', 'id' => $sFieldNameId, 'readonly' => true]);
$field .= Html::activeHiddenInput($this->model, $this->attribute, $this->options);
} else {
$field .= Html::textInput($this->name . "_name", null, ['readonly' => true]);
$field .= Html::hiddenInput($this->name, $this->value, $this->options);
}
//end input group
$sHideClass = $ImageManager_id === null ? 'hide' : '';
$field .= "<a href='#' class='input-group-addon btn btn-primary delete-selected-image " . $sHideClass . "' data-input-id='" . $sFieldId . "' data-show-delete-confirm='" . ($this->showDeletePickedImageConfirm ? "true" : "false") . "'><i class='glyphicon glyphicon-remove' aria-hidden='true'></i></a>";
$field .= "<a href='#' class='input-group-addon btn btn-primary open-modal-imagemanager' data-aspect-ratio='" . $this->aspectRatio . "' data-crop-view-mode='" . $this->cropViewMode . "' data-input-id='" . $sFieldId . "'>";
$field .= "<i class='glyphicon glyphicon-folder-open' aria-hidden='true'></i>";
$field .= "</a></div>";
//show preview if is true
if ($this->showPreview == true) {
$sHideClass = ($mImageManager == null) ? "hide" : "";
$sImageSource = isset($mImageManager->id) ? \Yii::$app->imagemanager->getImagePath($mImageManager->id, 500, 500, 'inset') : "";
$field .= '<div class="image-wrapper ' . $sHideClass . '">'
. '<img id="' . $sFieldId . '_image" alt="Thumbnail" class="img-responsive img-preview" src="' . $sImageSource . '">'
. '</div>';
}
//close image-manager-input div
$field .= "</div>";
echo $field;
$this->registerClientScript();
}
/**
* Registers js Input
*/
public function registerClientScript() {
$view = $this->getView();
ImageManagerInputAsset::register($view);
//set baseUrl from image manager
$sBaseUrl = Url::to(['/imagemanager/manager']);
//set base url
$view->registerJs("imageManagerInput.baseUrl = '" . $sBaseUrl . "';");
$view->registerJs("imageManagerInput.message = " . Json::encode([
'imageManager' => Yii::t('imagemanager','Image manager'),
'detachWarningMessage' => Yii::t('imagemanager', 'Are you sure you want to detach the image?'),
]) . ";");
}
}