addition fields images

This commit is contained in:
Leorne 2019-12-06 11:06:21 +03:00
parent 5cff041241
commit 9db4ea8abd
3 changed files with 63 additions and 8 deletions

View File

@ -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();
}
],
],

View File

@ -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();
}
],
],

View File

@ -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;
}
}