/** * Updates an existing Folders model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id * @return mixed */ public function actionUpdate($id) { FilemanagerAsset::register($this->view); $model = $this->findModel($id); if ($model->load(Yii::$app->request->post())) { $model->path = trim($model->path, '/'); if ($model->save()) { Yii::$app->session->setFlash('success', 'Folder successfully updated.'); return $this->redirect(['view', 'id' => $model->folder_id]); } } return $this->render('update', ['model' => $model]); }
public function init() { parent::init(); $script = ''; $view = $this->getView(); FilemanagerAsset::register($view); if (empty($this->containerOptions['id'])) { $this->containerOptions['id'] = $this->getId(); } $fileContent = $this->renderFileContent(); $opts = \yii\helpers\Json::encode(['multiple' => $this->multiple, 'maxFileCount' => $this->maxFileCount, 'folderId' => $this->folderId]); $script .= "\$('#{$this->containerOptions['id']}').filemanagerBrowse({$opts});"; $this->_browseClientFunc = 'fmBrowseInit_' . hash('crc32', $script); $view->registerJs("var {$this->_browseClientFunc}=function(){\n{$script}\n};\n{$this->_browseClientFunc}();"); echo Html::tag('div', $fileContent, ['id' => $this->containerOptions['id'], 'class' => 'fm-browse-selected']); }
<?php use dpodium\filemanager\FilemanagerAsset; use kartik\widgets\ActiveForm; use kartik\widgets\FileInput; use yii\helpers\Url; if ($uploadType == \dpodium\filemanager\components\Filemanager::TYPE_MODAL) { FilemanagerAsset::register($this); } $form = ActiveForm::begin(['action' => \Yii::$app->urlManager->createUrl(['/filemanager/files/upload']), 'id' => 'fm-upload-form', 'options' => ['enctype' => 'multipart/form-data']]); if (!empty($folderArray)) { echo $form->field($model, 'folder_id')->dropDownList($folderArray); } $script = <<<SCRIPT function (event, params) { params.form.append('uploadType', {$uploadType}); if(jQuery('select[name="Files[folder_id]"]').val() != undefined) { params.form.append('uploadTo', jQuery('select[name="Files[folder_id]"]').val()); } else { params.form.append('uploadTo', '{$model->folder_id}'); } } SCRIPT; echo $form->field($model, 'upload_file[]')->widget(FileInput::classname(), ['options' => ['multiple' => $multiple, 'accept' => implode(',', \Yii::$app->controller->module->acceptedFilesType)], 'pluginOptions' => ['uploadUrl' => Url::to(['/filemanager/files/upload']), 'browseClass' => 'btn btn-sm btn-success', 'uploadClass' => 'btn btn-sm btn-info', 'removeClass' => 'btn btn-sm btn-danger', 'maxFileCount' => $maxFileCount], 'pluginEvents' => ['filepreupload' => $script]]); ActiveForm::end();
public function actionUpload() { FilemanagerAsset::register($this->view); $model = new $this->module->models['files'](); $model->scenario = 'upload'; $folders = $this->module->models['folders']; $folderArray = ArrayHelper::map($folders::find()->all(), 'folder_id', 'category'); if (Yii::$app->request->isAjax) { if (!in_array(Yii::$app->request->post('uploadType'), [Filemanager::TYPE_FULL_PAGE, Filemanager::TYPE_MODAL])) { echo Json::encode(['error' => Yii::t('filemanager', 'Invalid value: {variable}', ['variable' => 'uploadType'])]); \Yii::$app->end(); } Yii::$app->response->getHeaders()->set('Vary', 'Accept'); $file = UploadedFile::getInstances($model, 'upload_file'); if (!$file) { echo Json::encode(['error' => Yii::t('filemanager', 'File not found.')]); \Yii::$app->end(); } $model->folder_id = Yii::$app->request->post('uploadTo'); $folder = $folders::find()->select(['path', 'storage'])->where('folder_id=:folder_id', [':folder_id' => $model->folder_id])->one(); if (!$folder) { echo Json::encode(['error' => Yii::t('filemanager', 'Invalid folder location.')]); \Yii::$app->end(); } $uploadStatus = true; $model->upload_file = $file[0]; $model->filename = $file[0]->name; list($width, $height) = getimagesize($file[0]->tempName); $model->dimension = $width && $height ? $width . 'X' . $height : null; // Too large size will cause memory exhausted issue when create thumbnail if (!is_null($model->dimension)) { if ($width > 2272 || $height > 1704) { echo Json::encode(['error' => Yii::t('filemanager', 'File dimension at most 2272 X 1704.')]); \Yii::$app->end(); } } $model->mime_type = $file[0]->type; $model->url = $folder->path; $extension = '.' . $file[0]->getExtension(); if (isset($this->module->storage['s3'])) { $model->object_url = '/'; $model->host = isset($this->module->storage['s3']['host']) ? $this->module->storage['s3']['host'] : null; $model->storage_id = $this->module->storage['s3']['bucket']; $this->saveModel($model, $extension, $folder->storage); $uploadStatus = $this->uploadToS3($model, $file[0], $extension); } else { $model->object_url = '/' . $folder->path . '/'; $model->storage_id = $this->module->directory; $this->saveModel($model, $extension, $folder->storage); $uploadStatus = $this->uploadToLocal($model, $file[0], $extension); } if (!$uploadStatus) { echo Json::encode(['error' => Yii::t('filemanager', 'Upload fail due to some reasons.')]); \Yii::$app->end(); } // if upload type = 1, render edit bar below file input container // if upload type = 2, switch active tab to Library for user to select file Yii::$app->response->format = Response::FORMAT_JSON; if (Yii::$app->request->post('uploadType') == Filemanager::TYPE_FULL_PAGE) { $fileType = $model->mime_type; if ($model->dimension) { $fileType = 'image'; } $html = Filemanager::renderEditUploadedBar($model->file_id, $model->object_url, $model->src_file_name, $fileType); return ['status' => 1, 'message' => 'Upload Success', 'type' => Yii::$app->request->post('uploadType'), 'html' => $html]; } else { return ['status' => 1, 'message' => 'Upload Success', 'type' => Yii::$app->request->post('uploadType')]; } return; } return $this->render('upload', ['model' => $model, 'folderArray' => $folderArray]); }
/** * Runs the widget. */ public function run() { $view = $this->getView(); FilemanagerAsset::register($view); parent::run(); }