From 9db4ea8abd4c72b7360a8b0cab1dec0fa6bda9b9 Mon Sep 17 00:00:00 2001 From: Leorne Date: Fri, 6 Dec 2019 11:06:21 +0300 Subject: [PATCH] addition fields images --- backend/modules/card/views/user-card/view.php | 5 +- backend/modules/notes/views/notes/view.php | 5 +- common/models/FieldsValueNew.php | 61 +++++++++++++++++++ 3 files changed, 63 insertions(+), 8 deletions(-) diff --git a/backend/modules/card/views/user-card/view.php b/backend/modules/card/views/user-card/view.php index 9cf3277..4ec95e2 100755 --- a/backend/modules/card/views/user-card/view.php +++ b/backend/modules/card/views/user-card/view.php @@ -78,10 +78,7 @@ $this->params['breadcrumbs'][] = $this->title; 'attribute' => 'value', 'format' => 'raw', 'value' => function ($model) { - if ($model->type_file == 'file') { - return $model->value . ' (' . Html::a('Скачать', $model->value, ['target' => '_blank', 'download' => 'download']) . ')'; - } - return $model->value; + return $model->getValue(); } ], ], diff --git a/backend/modules/notes/views/notes/view.php b/backend/modules/notes/views/notes/view.php index 20388d7..dc27b37 100755 --- a/backend/modules/notes/views/notes/view.php +++ b/backend/modules/notes/views/notes/view.php @@ -46,10 +46,7 @@ $this->params['breadcrumbs'][] = $this->title; 'attribute' => 'value', 'format' => 'raw', 'value' => function ($model) { - if ($model->type_file == 'file') { - return $model->value . ' (' . Html::a('Скачать', $model->value, ['target' => '_blank', 'download' => 'download']) . ')'; - } - return $model->value; + return $model->getValue(); } ], ], diff --git a/common/models/FieldsValueNew.php b/common/models/FieldsValueNew.php index 6c085f5..7cab619 100644 --- a/common/models/FieldsValueNew.php +++ b/common/models/FieldsValueNew.php @@ -3,6 +3,9 @@ namespace common\models; use Yii; +use yii\base\ErrorException; +use yii\base\Exception; +use yii\helpers\Html; /** * This is the model class for table "fields_value_new". @@ -23,6 +26,7 @@ class FieldsValueNew extends \yii\db\ActiveRecord const TYPE_COMPANY = 2; const TYPE_BALANCE = 3; const TYPE_NOTE = 4; + /** * {@inheritdoc} */ @@ -67,4 +71,61 @@ class FieldsValueNew extends \yii\db\ActiveRecord return $this->hasOne(AdditionalFields::class, ['id' => 'field_id']); } + + + + + public function isImage() + { + if ($this->type_file === 'text') { + return false; + } + + try{ + $mime = mime_content_type(Yii::getAlias('@frontend/web' . $this->value)); + }catch (ErrorException $e){ + return false; + } + + $extension = explode('/', $mime)['0']; + if (($extension === 'image') || $extension === 'application') { + return true; + } else { + return false; + } + } + + public function getValue() + { + $test = $this->type_file === 'file' ? $this->getFileValue() : $this->getTextValue(); + return $test; + } + + public function getFileValue() + { + $filename = $this->getFileName(); + $filePath = Yii::getAlias('@frontend/web' . $this->value); + if($this->isImage()){ + $imageHTML = Html::img($this->value, ['width' => '200px', 'alt' => $filename]); + $downloadLinkHTML = ' (' . Html::a('Скачать', $this->value, ['target' => '_blank', 'download' => $filename]) . ')'; + $result = $imageHTML . $downloadLinkHTML; + }else{ + $result = $filename . ' (' . Html::a('Скачать', $this->value, ['target' => '_blank', 'download' => $filename]) . ')'; + } + return $result; + } + + public function getTextValue() + { + return $this->value; + } + + public function getFileName(){ + if($this->type_file === 'file'){ + $explode = explode('/', $this->value); + $filename = array_pop($explode); + return $filename; + } + return $this->value; + } }