guild/common/models/FieldsValueNew.php

132 lines
3.1 KiB
PHP
Raw Normal View History

2019-06-25 12:37:09 +03:00
<?php
namespace common\models;
use Yii;
2019-12-06 11:06:21 +03:00
use yii\base\ErrorException;
use yii\base\Exception;
use yii\helpers\Html;
2019-06-25 12:37:09 +03:00
/**
* This is the model class for table "fields_value_new".
*
* @property int $id
* @property int $field_id
* @property int $item_id
* @property int $item_type
* @property int $order
* @property string $value
2019-07-26 12:32:43 +03:00
* @property string $option
2019-07-26 15:31:39 +03:00
* @property string $type_file
2019-06-25 12:37:09 +03:00
*/
class FieldsValueNew extends \yii\db\ActiveRecord
{
const TYPE_PROFILE = 0;
const TYPE_PROJECT = 1;
const TYPE_COMPANY = 2;
const TYPE_BALANCE = 3;
2019-12-05 15:05:33 +03:00
const TYPE_NOTE = 4;
2019-12-06 11:06:21 +03:00
2019-06-25 12:37:09 +03:00
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'fields_value_new';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['field_id', 'item_id', 'item_type'], 'required'],
[['field_id', 'item_id', 'item_type', 'order'], 'integer'],
2019-07-26 12:32:43 +03:00
[['value', 'type_file'], 'string'],
2019-06-25 12:37:09 +03:00
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'field_id' => 'Field ID',
'item_id' => 'Item ID',
'item_type' => 'Item Type',
'order' => 'Order',
'value' => 'Value',
2019-07-26 16:02:35 +03:00
'type_file' => 'Тип файла'
2019-06-25 12:37:09 +03:00
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getField()
{
return $this->hasOne(AdditionalFields::class, ['id' => 'field_id']);
}
2019-06-25 18:28:20 +03:00
2019-12-06 11:06:21 +03:00
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;
}
2019-06-25 12:37:09 +03:00
}