public function actionDeleteFile() { $modelID = Yii::$app->request->post('modelId'); $modelName = Yii::$app->request->post('modelName'); $attribute = Yii::$app->request->post('attribute'); $language = Yii::$app->request->post('language'); if (Yii::$app->request->isAjax && $modelID && $modelName && $attribute) { $error = true; /** @var $model ActiveRecord */ if ($language) { $model = $modelName::find()->where(['model_id' => $modelID, 'language' => $language])->one(); } else { $model = $modelName::findOne($modelID); } if ($model) { $fileId = $model->{$attribute}; $model->{$attribute} = null; if ($model->save(false)) { FPM::deleteFile($fileId); $error = false; } } return Json::encode(['error' => $error]); } return false; }
/** * @param $sub * @param $module * @param $size * @param $id * @param $baseName * @param $extension * * @return int * @throws InvalidConfigException * @throws NotFoundHttpException * @throws \Exception */ public function actionProcess($sub, $module, $size, $id, $baseName, $extension) { $directory = (string) floor($id / FPM::getFilesPerDirectory()); if ($sub !== $directory) { throw new NotFoundHttpException(Module::t('exception', 'Wrong generated link')); } $fileName = FPM::getOriginalDirectory($id) . DIRECTORY_SEPARATOR . FPM::getOriginalFileName($id, $baseName, $extension); if (file_exists($fileName)) { $data = FPM::transfer()->getData($id); if (strtolower($baseName) !== strtolower($data->base_name)) { throw new NotFoundHttpException(Module::t('exception', 'File not found')); } $config = isset(FPM::m()->imageSections[$module][$size]) ? FPM::m()->imageSections[$module][$size] : null; if (!is_array($config)) { throw new NotFoundHttpException(Module::t('exception', 'Incorrect request')); } $thumbnailFile = FPM::getThumbnailDirectory($id, $module, $size) . DIRECTORY_SEPARATOR . FPM::getThumbnailFileName($id, $baseName, $extension); FileHelper::createDirectory(FPM::getThumbnailDirectory($id, $module, $size)); if (isset($config['action'])) { switch ($config['action']) { case FPM::ACTION_ADAPTIVE_THUMBNAIL: Image::thumbnail($fileName, $config['width'], $config['height'])->save($thumbnailFile)->show($extension); break; case FPM::ACTION_THUMBNAIL: Image::thumbnail($fileName, $config['width'], $config['height'], ManipulatorInterface::THUMBNAIL_INSET)->save($thumbnailFile)->show($extension); break; case FPM::ACTION_CROP: Image::crop($fileName, $config['width'], $config['height'], $config['startX'], $config['startY'])->save($thumbnailFile)->show($extension); break; case FPM::ACTION_CANVAS_THUMBNAIL: Image::canvasThumbnail($fileName, $config['width'], $config['height'])->save($thumbnailFile)->show($extension); break; case FPM::ACTION_FRAME: Image::frame($fileName, 50, 'F00')->save($thumbnailFile)->show($extension); break; case FPM::ACTION_COPY: if (FPM::m()->symLink) { symlink($fileName, $thumbnailFile); } else { copy($fileName, $thumbnailFile); } \Yii::$app->response->sendFile($thumbnailFile); break; default: throw new InvalidConfigException(Module::t('exception', 'Action is incorrect')); break; } } else { throw new InvalidConfigException(Module::t('exception', 'Action not defined')); } } else { throw new NotFoundHttpException(Module::t('exception', 'File not found')); } }
/** * Formats the value as an link tag using FPM module. * @param mixed $value the value to be formatted * @param array $options * @return string the formatted result */ public function asFile($value, $options = []) { if (!$value) { return $this->nullDisplay; } $file = FPM::transfer()->getData($value); if (in_array($file->extension, ['jpg', 'png', 'gif', 'tif', 'bmp'])) { $linkLabel = FPM::image($file->id, 'admin', 'file'); } else { $linkLabel = FPM::getOriginalFileName($file->id, $file->base_name, $file->extension); } return Html::a($linkLabel, FPM::originalSrc($value), ['target' => '_blank']); }
/** * @param $id * * @throws \Exception * @throws \yii\base\InvalidConfigException */ public function delete($id) { $model = FPM::transfer()->getData($id); $config = FPM::m()->imageSections; foreach ($config as $moduleKey => $module) { foreach ($module as $sizeKey => $size) { $fileName = FPM::getThumbnailDirectory($id, $moduleKey, $sizeKey) . FPM::getThumbnailFileName($id, $model->base_name, $model->extension); if (is_file($fileName)) { unlink($fileName); } } } }
/** * @param $id * * @return bool * @throws \Exception */ public function deleteFile($id) { if (!(int) $id) { return false; } $directory = \metalguardian\fileProcessor\helpers\FPM::getOriginalDirectory($id); $model = $this->getData($id); $fileName = $directory . DIRECTORY_SEPARATOR . \metalguardian\fileProcessor\helpers\FPM::getOriginalFileName($id, $model->base_name, $model->extension); if (is_file($fileName)) { $result = unlink($fileName) && $this->deleteData($id) ? true : false; } else { $result = false; } return $result; }
public function run() { if (!$this->model || !$this->attribute) { return null; } $extraData = $this->model->isNewRecord ? ['sign' => $this->model->sign] : ['id' => $this->model->id]; $previewImages = []; $previewImagesConfig = []; $existModelImages = EntityToFile::find()->where('entity_model_name = :emn', [':emn' => $this->model->formName()]); if ($this->saveAttribute !== null) { $existModelImages->andWhere('attribute = :attr', [':attr' => $this->saveAttribute]); } $existModelImages = $this->model->isNewRecord ? $existModelImages->andWhere('temp_sign = :ts', [':ts' => $this->model->sign]) : $existModelImages->andWhere('entity_model_id = :id', [':id' => $this->model->id]); $existModelImages = $existModelImages->orderBy('position DESC')->all(); /** * @var \common\models\EntityToFile $file */ foreach ($existModelImages as $image) { $fileName = $image->file->base_name . '.' . $image->file->extension; $previewImages[] = Html::img(FPM::originalSrc($image->file_id), ['class' => 'file-preview-image', 'id' => 'preview-image-' . $image->file_id]); $previewImagesConfig[] = ['caption' => $fileName, 'width' => '120px', 'url' => ImagesUploadModel::deleteImageUrl(['id' => $image->id]), 'key' => $image->id]; } $output = Html::hiddenInput('urlForSorting', ImagesUploadModel::sortImagesUrl(), ['id' => 'urlForSorting']); $output .= Html::hiddenInput('aspectRatio', $this->aspectRatio, ['class' => 'aspect-ratio']); $output .= FileInput::widget(['model' => $this->model, 'attribute' => $this->attribute, 'options' => ['multiple' => $this->multiple, 'accept' => 'image/*'], 'pluginOptions' => ['dropZoneEnabled' => false, 'browseClass' => 'btn btn-success', 'browseIcon' => '<i class="glyphicon glyphicon-picture"></i> ', 'removeClass' => "btn btn-danger", 'removeIcon' => '<i class="glyphicon glyphicon-trash"></i> ', 'uploadClass' => "btn btn-info", 'uploadIcon' => '<i class="glyphicon glyphicon-upload"></i> ', 'uploadUrl' => Url::to($this->uploadUrl), 'allowedFileTypes' => ['image'], 'allowedPreviewTypes' => ['image'], 'uploadExtraData' => $extraData, 'initialPreview' => $previewImages, 'initialPreviewConfig' => $previewImagesConfig, 'overwriteInitial' => false, 'showRemove' => false, 'otherActionButtons' => $this->render('_crop_button'), 'fileActionSettings' => ['indicatorSuccess' => $this->render('_success_buttons_template')]], 'pluginEvents' => ['fileuploaded' => 'function(event, data, previewId, index) { var elem = $("#"+previewId).find(".file-actions .file-upload-indicator .kv-file-remove"); var cropElem = $("#"+previewId).find(".file-actions .crop-link"); var img = $("#"+previewId).find("img"); //id for cropped image replace img.attr("id", "preview-image-"+data.response.imgId); elem.attr("data-url", data.response.deleteUrl); elem.attr("data-key", data.response.id); cropElem.attr("href", data.response.cropUrl); //Resort images saveSort(); //Fix crop url for old images fixMultiUploadImageCropUrl(); }', 'fileloaded' => 'function(file, previewId, index, reader) { //Fix url for old images fixMultiUploadImageCropUrl(); }']]); $output .= '<br>'; $output .= $this->render('_modal'); return $output; }
/** * @param $attribute * @param array $element * @param ActiveRecord $model * @param string|null $language * * @return string */ public function renderUploadedFile($model, $attribute, $element, $language = null) { $content = ''; if ($element['type'] == static::INPUT_FILE && isset($model->{$attribute}) && $model->{$attribute}) { $file = FPM::transfer()->getData($model->{$attribute}); $content .= Html::beginTag('div', ['class' => 'file-name']); $content .= Html::button(\Yii::t('app', 'Delete file'), ['class' => 'delete-file', 'data' => ['modelName' => $model->className(), 'modelId' => $language ? $model->model_id : $model->id, 'attribute' => $attribute, 'language' => $language]]); if (in_array($file->extension, ['jpg', 'png', 'gif', 'tif', 'bmp'])) { $linkLabel = FPM::image($file->id, 'admin', 'file'); } else { $linkLabel = FPM::getOriginalFileName($file->id, $file->base_name, $file->extension); } $content .= Html::a($linkLabel, FPM::originalSrc($model->{$attribute}), ['target' => '_blank']); $content .= Html::endTag('div'); } return $content; }
/** * @inheritdoc */ public function safeUp() { $this->createTable($this->tableName, ['id' => $this->primaryKey(), 'entity_model_name' => $this->string()->notNull(), 'entity_model_id' => $this->integer()->notNull(), 'file_id' => $this->integer()->notNull(), 'temp_sign' => $this->string()->notNull()->defaultValue(''), 'position' => $this->integer()->notNull()->defaultValue(0), 'attribute' => $this->string()->defaultValue(null)], 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB'); $this->addForeignKey('fk_entity_file_id_to_fpm_file_table', $this->tableName, 'file_id', \metalguardian\fileProcessor\helpers\FPM::getTableName(), 'id', 'CASCADE', 'CASCADE'); }
<?php use backend\modules\imagesUpload\models\ImagesUploadModel; use metalguardian\fileProcessor\helpers\FPM; use yii\helpers\Html; \backend\assets\CropperAsset::register($this); ?> <div class="container"> <div class="img-container col-sm-6"> <img src="<?php echo FPM::originalSrc($id) . '?' . time(); ?> "/> </div> <div class="img-preview col-sm-6"></div> <div class="col-sm-12"> <?php echo Html::hiddenInput('dataX', null, ['id' => 'dataX']); ?> <?php echo Html::hiddenInput('dataY', null, ['id' => 'dataY']); ?> <?php echo Html::hiddenInput('dataHeight', null, ['id' => 'dataHeight']); ?> <?php echo Html::hiddenInput('dataWidth', null, ['id' => 'dataWidth']); ?> <?php
/** * Delete all previously generated image thumbs * * @param File $model */ protected function clearImageThumbs(File $model) { $fp = \Yii::$app->getModule('fileProcessor'); if ($fp) { $imageSections = $fp->imageSections; foreach ($imageSections as $moduleName => $config) { foreach ($config as $size => $data) { $thumbnailFile = FPM::getThumbnailDirectory($model->id, $moduleName, $size) . DIRECTORY_SEPARATOR . FPM::getThumbnailFileName($model->id, $model->base_name, $model->extension); if (is_file($thumbnailFile)) { unlink($thumbnailFile); } } } } }
echo $this->render('@app/themes/basic/layouts/_header', ['breadcrumbsLinks' => $model->getBreadcrumbsLinks()]); ?> <div class="row blog-post-page"> <div class="col-md-9 blog-box"> <!-- Start Single Post Area --> <div class="blog-post gallery-post"> <!-- Post Thumb --> <div class="post-head"> <?php if (isset($titleImage->file_id)) { ?> <?php echo FPM::image($titleImage->file_id, 'article', 'title', ['alt' => $model->label]); ?> <?php } ?> </div> <!-- Start Single Post Content --> <div class="post-content"> <div class="post-type"><i class="icon-pencil-4"></i></div> <h2><?php echo $model->label; ?> </h2> <ul class="post-meta"> <li><?php
public function down() { $this->dropTable(\metalguardian\fileProcessor\helpers\FPM::getTableName()); }
<?php foreach ($models as $article) { ?> <li> <?php $titleImage = $article->titleImage; ?> <?php $url = $article->getViewUrl(); ?> <div class="widget-thumb"> <a href="<?php echo $url; ?> "><?php echo FPM::image($titleImage->file_id, 'article', 'thumb', ['alt' => $article->label]); ?> </a> </div> <div class="widget-content"> <h5><a href="<?php echo $url; ?> "><?php echo $article->label; ?> </a></h5> <span><?php echo Yii::$app->formatter->asDate($article->date, 'd MMM yyyy'); ?> </span>
/** * * @param $file * * @return int */ protected function getValue($file) { $fileId = \metalguardian\fileProcessor\helpers\FPM::transfer()->saveUploadedFile($file); return $fileId; }
/** * @param $id */ private function delete($id) { if ($id) { FPM::deleteFile($id); } }
/** * @inheritdoc */ public static function tableName() { return \metalguardian\fileProcessor\helpers\FPM::getTableName(); }