FileSetter::run()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 38
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 4
eloc 26
c 2
b 1
f 0
nc 8
nop 0
dl 0
loc 38
rs 9.504
1
<?php
2
namespace Itstructure\MFUploader\widgets;
3
4
use Yii;
5
use yii\helpers\{Html, Url};
6
use yii\widgets\InputWidget;
7
use Itstructure\MFUploader\Module;
8
use Itstructure\MFUploader\assets\FileSetterAsset;
9
10
/**
11
 * Class FileSetter
12
 *
13
 * Example 1 (for thumbnail):
14
 *
15
 * Container to display selected thumbnail.
16
 * <div id="thumbnail-container">
17
 *  <?php if (isset($thumbnailModel) && $thumbnailModel instanceof Mediafile): ?>
18
 *      <img src="<?php echo $thumbnailModel->getThumbUrl(Module::THUMB_ALIAS_DEFAULT) ?>">
19
 *  <?php endif; ?>
20
 * </div>
21
 *
22
 * <?php echo FileSetter::widget([
23
 *    'model' => $model,
24
 *    'attribute' => UploadModelInterface::FILE_TYPE_THUMB,
25
 *    'neededFileType' => UploadModelInterface::FILE_TYPE_THUMB,
26
 *    'buttonName' => Module::t('main', 'Set thumbnail'),
27
 *    'mediafileContainer' => '#thumbnail-container',
28
 *    'owner' => 'post',
29
 *    'ownerId' => {current owner id, post id, page id e.t.c.},
30
 *    'ownerAttribute' => UploadModelInterface::FILE_TYPE_THUMB,
31
 *    'subDir' => 'post'
32
 * ]); ?>
33
 *
34
 *
35
 * Example 2 (for image):
36
 *
37
 * $number - number of the file (Can be rendered by cycle)
38
 *
39
 * Container to display selected image.
40
 * <div class="media">
41
 *      <div id="mediafile-container-new<?php if (isset($number)): ?>-<?php echo $number; ?><?php endif; ?>">
42
 *      </div>
43
 *      <div class="media-body">
44
 *          <h4 id="title-container-new<?php if (isset($number)): ?>-<?php echo $number; ?><?php endif; ?>" class="media-heading"></h4>
45
 *          <div id="description-container-new<?php if (isset($number)): ?>-<?php echo $number; ?><?php endif; ?>"></div>
46
 *      </div>
47
 * </div>
48
 *
49
 * <?php echo FileSetter::widget([
50
 *    'model' => $model,
51
 *    'attribute' => UploadModelInterface::FILE_TYPE_IMAGE,
52
 *    'neededFileType' => UploadModelInterface::FILE_TYPE_IMAGE,
53
 *    'buttonName' => Module::t('main', 'Set image'),
54
 *    'options' => [
55
 *       'id' => Html::getInputId($model, UploadModelInterface::FILE_TYPE_IMAGE) . (isset($number) ? '-new-' . $number : '')
56
 *    ],
57
 *    'mediafileContainer' => '#mediafile-container-new' . (isset($number) ? '-' . $number : ''),
58
 *    'titleContainer' => '#title-container-new' . (isset($number) ? '-' . $number : ''),
59
 *    'descriptionContainer' => '#description-container-new' . (isset($number) ? '-' . $number : ''),
60
 *    'owner' => 'post',
61
 *    'ownerId' => {current owner id, post id, page id e.t.c.},
62
 *    'ownerAttribute' => UploadModelInterface::FILE_TYPE_IMAGE,
63
 *    'subDir' => 'post'
64
 * ]); ?>
65
 *
66
 * @property string|null $owner Owner name (post, article, page e.t.c.).
67
 * @property int|null $ownerId Owner id.
68
 * @property string|null $ownerAttribute Owner attribute (thumbnail, image e.t.c.).
69
 * @property string|null $neededFileType Needed file type for validation (thumbnail, image e.t.c.).
70
 * @property string $subDir Subdirectory to upload files.
71
 * @property string $template Template to display widget elements.
72
 * @property string $buttonHtmlTag Button html tag.
73
 * @property string $buttonName Button name.
74
 * @property array $buttonOptions Button html options.
75
 * @property string $resetButtonHtmlTag Reset button html tag.
76
 * @property string $resetButtonName Reset button name.
77
 * @property array $resetButtonOptions Reset button html options.
78
 * @property string $deleteBoxName Delete box name (text).
79
 * @property string $deleteBoxAttribute Delete box attribute.
80
 * @property array $deleteBoxOptions Delete box html options.
81
 * @property bool $deleteBoxDisplay Display or not delete box.
82
 * @property string $mediafileContainer In this container will be inserted selected mediafile.
83
 * @property string $titleContainer In this container will be inserted title of selected mediafile.
84
 * @property string $descriptionContainer In this container will be inserted description of selected mediafile.
85
 * @property string $callbackBeforeInsert JS function. That will be called before insert file data in to the input.
86
 * @property string $insertedData This data will be inserted in to the input field.
87
 * @property string $fileManagerUrl Url to get files by filemanager.
88
 *
89
 * @package Itstructure\MFUploader\widgets
90
 *
91
 * @author Andrey Girnik <[email protected]>
92
 */
93
class FileSetter extends InputWidget
94
{
95
    /**
96
     * Owner name (post, article, page e.t.c.).
97
     *
98
     * @var string|null
99
     */
100
    public $owner = null;
101
102
    /**
103
     * Owner id.
104
     *
105
     * @var int|null
106
     */
107
    public $ownerId = null;
108
109
    /**
110
     * Owner attribute (thumbnail, image e.t.c.).
111
     *
112
     * @var string|null
113
     */
114
    public $ownerAttribute = null;
115
116
    /**
117
     * Needed file type for validation (thumbnail, image e.t.c.).
118
     *
119
     * @var string|null
120
     */
121
    public $neededFileType = null;
122
123
    /**
124
     * Subdirectory to upload files.
125
     *
126
     * @var string
127
     */
128
    public $subDir = '';
129
130
    /**
131
     * Template to display widget elements.
132
     *
133
     * @var string
134
     */
135
    public $template = '<div class="input-group">{input}<span class="input-group-btn">{button}{reset-button}</span><span class="delete-box">{delete-box}</span></div>';
136
137
    /**
138
     * Button html tag.
139
     *
140
     * @var string
141
     */
142
    public $buttonHtmlTag = 'button';
143
144
    /**
145
     * Button name.
146
     *
147
     * @var string
148
     */
149
    public $buttonName = 'Browse';
150
151
    /**
152
     * Button html options.
153
     *
154
     * @var array
155
     */
156
    public $buttonOptions = [];
157
158
    /**
159
     * Reset button html tag.
160
     *
161
     * @var string
162
     */
163
    public $resetButtonHtmlTag = 'button';
164
165
    /**
166
     * Reset button name.
167
     *
168
     * @var string
169
     */
170
    public $resetButtonName = 'Clear';
171
172
    /**
173
     * Reset button html options.
174
     *
175
     * @var array
176
     */
177
    public $resetButtonOptions = [];
178
179
    /**
180
     * Delete box name (text).
181
     *
182
     * @var string
183
     */
184
    public $deleteBoxName = 'Delete';
185
186
    /**
187
     * Delete box attribute.
188
     *
189
     * @var string
190
     */
191
    public $deleteBoxAttribute = 'delete[]';
192
193
    /**
194
     * Delete box html options.
195
     *
196
     * @var array
197
     */
198
    public $deleteBoxOptions = [];
199
200
    /**
201
     * Display or not delete box.
202
     *
203
     * @var bool
204
     */
205
    public $deleteBoxDisplay = false;
206
207
    /**
208
     * Optional, if set, in container will be inserted selected mediafile.
209
     *
210
     * @var string|null
211
     */
212
    public $mediafileContainer = null;
213
214
    /**
215
     * Optional, if set, in container will be inserted title of selected mediafile.
216
     *
217
     * @var string|null
218
     */
219
    public $titleContainer = null;
220
221
    /**
222
     * Optional, if set, in container will be inserted description of selected mediafile.
223
     *
224
     * @var string|null
225
     */
226
    public $descriptionContainer = null;
227
228
    /**
229
     * JS function. That will be called before insert file data in to the input.
230
     *
231
     * @var string
232
     */
233
    public $callbackBeforeInsert = '';
234
235
    /**
236
     * This data will be inserted in to the input field.
237
     *
238
     * @var string
239
     */
240
    public $insertedData = self::INSERTED_DATA_ID;
241
    
242
    /**
243
     * Src to get files by filemanager.
244
     *
245
     * @var string
246
     */
247
    public $fileManagerUrl  = Module::URL_FILE_MANAGER;
248
249
    /**
250
     * Data, which will be inserted in to the file input.
251
     */
252
    const INSERTED_DATA_ID = 'id';
253
    const INSERTED_DATA_URL = 'url';
254
255
    /**
256
     * @inheritdoc
257
     */
258
    public function init()
259
    {
260
        parent::init();
261
262
        if (empty($this->options['class'])) {
263
            $this->options['class'] = 'form-control';
264
        }
265
266
        if (empty($this->buttonOptions['id'])) {
267
            $this->buttonOptions['id'] = $this->options['id'] . '-btn';
268
        }
269
270
        if (empty($this->buttonOptions['class'])) {
271
            $this->buttonOptions['class'] = 'btn btn-default';
272
        }
273
274
        if (empty($this->resetButtonOptions['class'])) {
275
            $this->resetButtonOptions['class'] = 'btn btn-default';
276
        }
277
278
        $this->buttonOptions['role'] = 'filemanager-load';
279
        $this->resetButtonOptions['role'] = 'clear-input';
280
        $this->resetButtonOptions['data-clear-element-id'] = $this->options['id'];
281
        $this->resetButtonOptions['data-mediafile-container'] = $this->mediafileContainer;
282
    }
283
284
    /**
285
     * Run widget.
286
     */
287
    public function run()
288
    {
289
        $replace = [];
290
291
        if ($this->hasModel()) {
292
            $replace['{input}'] = Html::activeHiddenInput($this->model, $this->attribute, $this->options);
293
        } else {
294
            $replace['{input}'] = Html::hiddenInput($this->name, $this->value, $this->options);
295
        }
296
297
        $replace['{button}'] = Html::tag($this->buttonHtmlTag, $this->buttonName, $this->buttonOptions);
298
        $replace['{reset-button}'] = Html::tag($this->resetButtonHtmlTag, $this->resetButtonName, $this->resetButtonOptions);
299
        $replace['{delete-box}'] = $this->deleteBoxDisplay ? Html::checkbox($this->deleteBoxAttribute, false, $this->deleteBoxOptions).' '.$this->deleteBoxName : '';
300
301
        FileSetterAsset::register($this->view);
302
303
        if (!empty($this->callbackBeforeInsert)) {
304
            $this->view->registerJs('
305
                $("#' . $this->options['id'] . '").on("fileInsert", ' . $this->callbackBeforeInsert . ');'
306
            );
307
        }
308
309
        $modal = $this->renderFile('@'.Module::MODULE_NAME.'/views/layouts/modal.php', [
310
            'inputId' => $this->options['id'],
311
            'btnId' => $this->buttonOptions['id'],
312
            'fileManagerUrl' => Url::to([$this->fileManagerUrl]),
313
            'mediafileContainer' => $this->mediafileContainer,
314
            'titleContainer' => $this->titleContainer,
315
            'descriptionContainer' => $this->descriptionContainer,
316
            'insertedData' => $this->insertedData,
317
            'owner' => $this->owner,
318
            'ownerId' => $this->ownerId,
319
            'ownerAttribute' => $this->ownerAttribute,
320
            'neededFileType' => $this->neededFileType,
321
            'subDir' => $this->subDir,
322
        ]);
323
324
        return strtr($this->template, $replace) . $modal;
325
    }
326
327
    /**
328
     * Give ability of configure view to the module class.
329
     *
330
     * @return \yii\base\View|\yii\web\View
331
     */
332
    public function getView()
333
    {
334
        $module = Yii::$app->controller->module;
335
336
        if (method_exists($module, 'getView')) {
337
            return $module->getView();
338
        }
339
340
        return parent::getView();
341
    }
342
}
343