forked from sya0710/yii2-gallery
/
Gallery.php
executable file
·155 lines (134 loc) · 5.49 KB
/
Gallery.php
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
namespace sya\gallery;
use Yii;
use yii\bootstrap\Html;
class Gallery extends \yii\widgets\InputWidget {
// Type upload
CONST TYPE_UPLOAD = 'upload';
CONST TYPE_URL = 'url';
CONST TYPE_PATH = 'path';
/**
* Thong tin cua image
* $columns = [
* 'status' => [
* 'title' => 'Status',
* 'displayType' => 'dropdown',
* 'items' => [
'An',
'Hien'
]
* ],
* 'sort' => [
'title' => 'Sort',
'displayType' => 'text'
],
* ]
*
* @var array
*/
public $columns = [];
// Ten model
private $moduleName;
public function init() {
$this->moduleName = \yii\helpers\StringHelper::basename(get_class($this->model));
$this->registerAssets();
}
public function run() {
$galleries = Html::getAttributeValue($this->model, $this->attribute);
return $this->render('gallery', [
'moduleName' => $this->moduleName,
'columns' => $this->columns,
'galleries' => $galleries,
]);
}
private function registerAssets(){
GalleryAssets::register($this->getView());
$this->getView()->registerJs('
function insertImagePath(element){
var data = $("#let_galleries").val();
if (data == ""){
$("#let_galleries").val($(element).attr("id"));
} else {
$("#let_galleries").val(data.concat("," + $(element).attr("id")));
}
}
function addImageByGallery(type){
var module = "' . $this->moduleName . '",
columns = \'' . \yii\helpers\Json::encode($this->columns) . '\',
image = $("#image").val();
$.ajax({
url: "' . \yii\helpers\Url::to(['/gallery/ajax/additemimage']) . '",
type: "post",
data: {type: type, module: module, columns: columns, image: image},
}).done(function (data) {
if (data.length > 0) {
$("#tableImage").append(data);
$("#sya_gallery").modal("hide");
}
});
}
// Ham xoa anh
function removeImage(element){
var confirmAlert = confirm("' . Yii::t('yii', 'Are you sure you want to delete this item?') . '");
if (confirmAlert == true){
$(element).parents("#imageItem").remove();
// Kiem tra co anh trong gallery hay khong
if ($("#tableImage").children().length == 0) {
$("#tableImage").append(\'' . Html::hiddenInput($this->moduleName . '[gallery]', '', ['class' => 'form-control']) . '\');
}
}
}
', \yii\web\View::POS_END);
$this->getView()->registerJs('
// Sap xep anh
$("#tableImage").sortable({});
// Dropzone drop and drag file
Dropzone.options.myAwesomeDropzone = {
uploadMultiple: true,
parallelUploads: 100,
maxFiles: 100,
paramName: "image",
params: {
type: "' . self::TYPE_UPLOAD . '",
module: "' . $this->moduleName . '",
columns: \'' . \yii\helpers\Json::encode($this->columns) . '\'
},
autoDiscover: false,
url: "' . \yii\helpers\Url::to(['/gallery/ajax/additemimage']) . '",
headers: {
"Accept": "*/*",
"' . \yii\web\Request::CSRF_HEADER . '": "' . Yii::$app->getRequest()->csrfToken . '",
"Access-Control-Allow-Origin": "*"
},
// Dropzone settings
init: function() {
this.on("sendingmultiple", function(image, xhr, formData) {
formData.processData = false;
formData.contentType = false;
formData.enctype = "multipart/form-data";
});
this.on("successmultiple", function(files, responseText, e) {
$("#tableImage").append(responseText);
});
}
}
// Forcus input
$(".custom_modal_gallery a[data-toggle=\'tab\']").on("shown.bs.tab", function (e) {
$("#insert_image").attr("data-type", $(e.target).attr("data-type"));
$(e.target).parents(".tabs-container").find("#image").focus();
})
$("#image").on("input",function(e){
$.ajax({
url: "' . \yii\helpers\Url::to(['/gallery/ajax/getimagepreview']) . '",
type: "post",
data: {image: $(this).val()},
}).done(function (data) {
$("#embed_url_settings").html(data);
});
});
$("#insert_image").click(function() {
addImageByGallery($(this).attr("data-type"));
});
', yii\web\View::POS_READY);
}
}