From 75d0b2191e80b0193691de8a7ceda0c03932caf9 Mon Sep 17 00:00:00 2001 From: Leorne Date: Fri, 6 Dec 2019 11:11:31 +0300 Subject: [PATCH] FieldsValueNew some refactoring --- common/models/FieldsValueNew.php | 86 +++++++++++++++++++------------- 1 file changed, 51 insertions(+), 35 deletions(-) diff --git a/common/models/FieldsValueNew.php b/common/models/FieldsValueNew.php index 7cab619..ffaf54a 100644 --- a/common/models/FieldsValueNew.php +++ b/common/models/FieldsValueNew.php @@ -72,18 +72,67 @@ class FieldsValueNew extends \yii\db\ActiveRecord } + /** + * get value for view + * @return string + */ + public function getValue() + { + $test = $this->type_file === 'file' ? $this->getFileValue() : $this->getTextValue(); + return $test; + } + /** + * @return string + */ + private 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; + } + /** + * @return string + */ + private function getTextValue() + { + return $this->value; + } + /** + * @return mixed|string + */ + public function getFileName() + { + if ($this->type_file === 'file') { + $explode = explode('/', $this->value); + $filename = array_pop($explode); + return $filename; + } + return $this->value; + } + + /** + * File is image? + * @return bool + */ public function isImage() { if ($this->type_file === 'text') { return false; } - try{ + try { $mime = mime_content_type(Yii::getAlias('@frontend/web' . $this->value)); - }catch (ErrorException $e){ + } catch (ErrorException $e) { return false; } @@ -95,37 +144,4 @@ class FieldsValueNew extends \yii\db\ActiveRecord } } - 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; - } }