@@ -10,17 +10,44 @@ composer require rkit/fileapi-widget-yii2
1010
1111## Usage
1212
13- ### Basic usage
13+ ``` php
14+ use rkit\fileapi\Widget as FileApi;
15+ …
16+ <?= $form->field($model, $attribute, ['template' => "{label}\n{error}\n{input}\n{hint}"])
17+ ->widget(FileApi::className(), [
18+ 'template' => '@app/modules/admin/views/shared/files/image-template',
19+ 'callbacks' => [
20+ 'select' => new JsExpression('function (evt, ui) {
21+ …
22+ }'),
23+ 'filecomplete' => [new JsExpression('function (evt, uiEvt) {
24+ if (uiEvt.result.error) {
25+ …
26+ } else {
27+ …
28+ }
29+ }'),
30+ ]],
31+ 'settings' => [
32+ 'url' => yii\helpers\Url::toRoute('/url/to/upload'),
33+ 'imageSize' => ['minWidth' => 1000],
34+ 'accept' => 'image/*',
35+ 'duplicate' => true
36+ ]
37+ ]);
38+ ?>
39+ ```
40+
41+ ### Basic usage with [ FileMananger] ( https://github.com/rkit/filemanager-yii2 )
1442
15- 1 . Form
43+ 1 . ** Form**
1644
1745 ``` php
1846 use rkit\fileapi\Widget as FileApi;
1947 …
20-
21- $form->field($model, $attribute, ['template' => "{label}\n{error}\n{input}\n{hint}"])
48+ <?= $form->field($model, $attribute, ['template' => "{label}\n{error}\n{input}\n{hint}"])
2249 ->widget(FileApi::className(), [
23- 'template' => '@app/path/to/ template',
50+ 'template' => '@app/modules/admin/views/shared/files/image- template',
2451 'callbacks' => [
2552 'select' => new JsExpression('function (evt, ui) {
2653 var ufile = ui.files[0],
@@ -44,13 +71,18 @@ composer require rkit/fileapi-widget-yii2
4471 ]],
4572 'settings' => [
4673 'url' => yii\helpers\Url::toRoute([$attribute . '-upload']),
74+ 'imageSize' => $model->getFileRules($attribute)['imageSize'],
75+ 'accept' => implode(',', $model->getFileRules($attribute)['mimeTypes']),
4776 'duplicate' => true
4877 ]
4978 ])
50- );
79+ ->hint($model->getFileRulesDescription($attribute), [
80+ 'class' => 'fileapi-rules'
81+ ]);
82+ ?>
5183 ```
5284
53- 2 . Template
85+ 2 . ** Template**
5486
5587 ``` php
5688 <div id =" <?= $selector; ?>" class =" fileapi" >
@@ -98,4 +130,83 @@ composer require rkit/fileapi-widget-yii2
98130 </div >
99131 ```
100132
101- ### Gallery
133+ ### Gallery with [ FileMananger] ( https://github.com/rkit/filemanager-yii2 )
134+
135+ 1 . ** Form**
136+
137+ ``` php
138+ use rkit\fileapi\Widget as FileApi;
139+ …
140+
141+ <?= $form->field($model, $attribute, ['template' => "{error}\n{input}\n{hint}"])
142+ ->widget(FileApi::className(), [
143+ 'template' => '@app/modules/admin/views/shared/files/gallery-template',
144+ 'preview' => false,
145+ 'callbacks' => [
146+ 'select' => new JsExpression('function (evt, ui) {
147+ if (ui && ui.other.length && ui.other[0].errors) {
148+ alert("'.Yii::t('app', 'Incorrect file format').': " + ui.other.map(function(v) { return v.name; }));
149+ }
150+ }'),
151+ 'filecomplete' => new JsExpression('function (evt, uiEvt) {
152+ if (uiEvt.result.error) {
153+ alert(uiEvt.result.error);
154+ } else {
155+ $(".field-' . Html::getInputId($model, $attribute) . '").find(".help-block").empty();
156+ $(".field-' . Html::getInputId($model, $attribute) . '").removeClass("has-error");
157+ $(this).find(".fileapi-files").append(uiEvt.result);
158+ }
159+ }'),
160+ ],
161+ 'settings' => [
162+ 'url' => yii\helpers\Url::toRoute([$attribute . '-upload']),
163+ 'imageSize' => $model->getFileRules($attribute)['imageSize'],
164+ 'multiple' => true,
165+ 'duplicate' => true
166+ ]
167+ ])
168+ ->hint($model->getFileRulesDescription($attribute), [
169+ 'class' => 'fileapi-rules'
170+ ]
171+ ); ?>
172+ ```
173+
174+ 2 . ** Template**
175+
176+ ``` php
177+ <?php
178+ use yii\helpers\Html;
179+ $files = $model->getFiles($attribute); $items = [];
180+ ?>
181+ <div id =" <?= $selector; ?>" class =" fileapi" >
182+ <div class =" btn btn-default btn-small fileapi-fileapi-wrapper" >
183+ <div class =" fileapi-browse" data-fileapi =" active.hide" >
184+ <span class =" glyphicon glyphicon-picture" ></span >
185+ <span ><?= Yii::t('app', 'Upload')?></span >
186+ <input type =" file" name =" <?= $inputName ?>" >
187+ </div >
188+ </div >
189+ <ul class =" fileapi-files" >
190+ <?php foreach ($files as $file):?>
191+ <?= $this->render('gallery-item', [
192+ 'file' => $file,
193+ 'model' => $model,
194+ 'attribute' => $attribute
195+ ]); ?>
196+ <?php endforeach?>
197+ </ul >
198+ <?= Html::hiddenInput(Html::getInputName($model, $attribute) . '[]', null, ['id' => Html::getInputId($model, $attribute)]) ?>
199+ </div >
200+ ```
201+
202+ 3 . ** Item**
203+
204+ ``` php
205+ <li >
206+ <a href =" <?= $file->path()?>" target =" _blank" ><img src =" <?= $model->thumb('gallery', '80x80', $file->path())?>" ></a >
207+ <a class =" btn btn-lg" ><span class =" glyphicon glyphicon-remove remove-item" data-remove-item =" li" ></span ></a >
208+ <?= Html::textInput(Html::getInputName($model, $attribute) . '[files][' . $file->id .']', $file->title, [
209+ 'class' => 'form-control',
210+ ])?>
211+ </li >
212+ ```
0 commit comments