Commit b25bf125 authored by andre's avatar andre

#1302

parent d34b3a86
<?php
namespace common\components\activeRecordBehaviors;
use Yii;
use yii\base\Behavior;
use yii\db\ActiveRecord;
use yii\web\UploadedFile;
use yii\imagine\Image;
/**
* Class MFileUploadBehavior
* @package common\components\activeRecordBehaviors
*
* Тоже самое что и FileUploadBehavior, только
* использует $this->field, т.е. атрибут хранящий файл, в методах save и insert
* соотв и в форме нужно указывать этот атрибут, а не 'file'
*/
class MFileUploadBehavior extends Behavior
{
public $path;
public $folder;
public $file;
public $unlinkFile = false;
public $field;
public $thumb = false;
public $thumbSize = ['100', '100'];
public function events()
{
return [
ActiveRecord::EVENT_BEFORE_UPDATE => 'Save',
ActiveRecord::EVENT_BEFORE_INSERT => 'Insert',
ActiveRecord::EVENT_BEFORE_DELETE => 'Delete',
];
}
private function getAbsolutePath()
{
return Yii::getAlias($this->path);
}
private function uploadFile()
{
if(!file_exists($this->getAbsolutePath() . $this->folder))
{
mkdir($this->getAbsolutePath() . $this->folder, 0777, true);
}
$field = $this->field;
$filename = date('dmYHis-') . uniqid();
$this->owner->$field = $this->folder . $filename . '.' . $this->file->extension;
$this->file->saveAs($this->getAbsolutePath() . $this->owner->$field);
if($this->thumb)
{
list($width, $height) = $this->thumbSize;
try
{
Image::$driver = [Image::DRIVER_GD2];
Image::thumbnail($this->path . $this->owner->$field, $width, $height)
->save($this->getAbsolutePath().$this->folder.$filename.'.thumb.'.$this->file->extension, ['quality' => 80]);
}
catch (Exception $e)
{
throw $e;
}
}
}
private function deleteFile()
{
$field = $this->field;
if($this->owner->$field)
{
// Удаление Thumbnail если есть
$path = pathinfo(Yii::getAlias($this->path . $this->owner->$field));
$thumb = $path['dirname'] . '/' . $path['filename'] . '.thumb.' . $path['extension'];
if(file_exists($thumb))
{
unlink($thumb);
}
// Удаление файла
if(file_exists($this->getAbsolutePath() . $this->owner->$field))
{
unlink($this->getAbsolutePath() . $this->owner->$field);
}
$this->owner->$field = null;
}
}
public function Save($event)
{
$this->file = UploadedFile::getInstance($this->owner, $this->field);
if($this->owner->unlinkFile || $this->file)
{
$this->deleteFile();
}
if($this->file)
{
$this->uploadFile();
}
return true;
}
public function Insert($event)
{
$this->file = UploadedFile::getInstance($this->owner, $this->field);
if($this->file)
{
$this->uploadFile();
}
return true;
}
public function Delete($event)
{
$this->deleteFile();
return true;
}
}
...@@ -25,6 +25,7 @@ class CaseContent extends CoContent ...@@ -25,6 +25,7 @@ class CaseContent extends CoContent
{ {
public $parentClass = 'common\modules\content\models\CoContent'; public $parentClass = 'common\modules\content\models\CoContent';
public function behaviors() public function behaviors()
{ {
return [ return [
...@@ -39,11 +40,17 @@ class CaseContent extends CoContent ...@@ -39,11 +40,17 @@ class CaseContent extends CoContent
'actions' => ['create', 'update', 'copy', 'createcase', 'createproject'] 'actions' => ['create', 'update', 'copy', 'createcase', 'createproject']
], ],
'file' => [ 'file' => [
'class' => 'common\components\activeRecordBehaviors\FileUploadBehavior', 'class' => 'common\components\activeRecordBehaviors\MFileUploadBehavior',
'path' => '@frontend/web', 'path' => '@frontend/web',
'folder' => '/uploads/content/', 'folder' => '/uploads/content/',
'field' => 'preview' 'field' => 'preview'
], ],
'file_mob' => [
'class' => 'common\components\activeRecordBehaviors\MFileUploadBehavior',
'path' => '@frontend/web',
'folder' => '/uploads/content/',
'field' => 'preview_mob'
],
'timestamp' => [ 'timestamp' => [
'class' => TimestampBehavior::className(), 'class' => TimestampBehavior::className(),
'createdAtAttribute' => 'created_at', 'createdAtAttribute' => 'created_at',
......
...@@ -90,7 +90,14 @@ $blocks = \common\modules\content\models\CoBlocks::find()->all(); ...@@ -90,7 +90,14 @@ $blocks = \common\modules\content\models\CoBlocks::find()->all();
echo Html::img(\Yii::$app->params['frontUrl'] . $model->preview); echo Html::img(\Yii::$app->params['frontUrl'] . $model->preview);
} ?> } ?>
<?= $form->field($model, 'file')->fileInput() ?> <?= $form->field($model, 'preview')->fileInput() ?>
<?php if($model->preview_mob)
{
echo Html::img(\Yii::$app->params['frontUrl'] . $model->preview_mob);
} ?>
<?= $form->field($model, 'preview_mob')->fileInput() ?>
<ul class="nav nav-pills"> <ul class="nav nav-pills">
<?php $c = 0; foreach ($model->getLangsHelper() as $i => $content) : $c++; ?> <?php $c = 0; foreach ($model->getLangsHelper() as $i => $content) : $c++; ?>
......
...@@ -141,6 +141,7 @@ class CoContent extends \common\components\ActiveRecordModel ...@@ -141,6 +141,7 @@ class CoContent extends \common\components\ActiveRecordModel
'url' => Yii::t('content', 'Url'), 'url' => Yii::t('content', 'Url'),
'name' => Yii::t('content', 'Name'), 'name' => Yii::t('content', 'Name'),
'file' => 'Превью', 'file' => 'Превью',
'preview_mob' => 'Превью для моб. версии',
'custom' => 'Заголовок', 'custom' => 'Заголовок',
'priority' => 'Приоритет в Sitemap', 'priority' => 'Приоритет в Sitemap',
'title' => Yii::t('content', 'Title'), 'title' => Yii::t('content', 'Title'),
......
<?php
use yii\db\Migration;
use yii\db\Schema;
/**
* Handles adding column to table `co_content`.
*/
class m160713_083810_add_column_to_co_content extends Migration
{
/**
* @inheritdoc
*/
public function safeUp()
{
$this->addColumn('co_content', 'preview_mob', Schema::TYPE_STRING.'(100) AFTER preview');
}
public function safeDown()
{
$this->dropColumn('co_content', 'preview_mob');
}
}
...@@ -44,7 +44,10 @@ $models = CaseContent::find() ...@@ -44,7 +44,10 @@ $models = CaseContent::find()
<? elseif($model->previewType->type == CasesPreviewType::PREVIEW_RECTANGLE): ?> <? elseif($model->previewType->type == CasesPreviewType::PREVIEW_RECTANGLE): ?>
<div class="col-md-12 col-xs-12 col-sm-12"> <div class="col-md-12 col-xs-12 col-sm-12">
<a href="<?=Url::to(['/'.$model->url])?>" class="keys_block_small big"> <a href="<?=Url::to(['/'.$model->url])?>" class="keys_block_small big">
<img src="<?=$model->preview?>" height="338" width="940"> <img class="img_lg" src="<?=$model->preview?>" height="338" width="940">
<? if($model->preview_mob):?>
<img class="img_sm" src="<?=$model->preview_mob?>" height="338" width="455">
<? endif ?>
<div class="keys_small_title" <?if($model->custom==CoContent::CUSTOM_WHITE){?>style="color:#fff;"<?}?>><?=$model->lang->title?></div> <div class="keys_small_title" <?if($model->custom==CoContent::CUSTOM_WHITE){?>style="color:#fff;"<?}?>><?=$model->lang->title?></div>
<div class="keys_small_foot"> <div class="keys_small_foot">
<div class="keys_small_btn_more"> <div class="keys_small_btn_more">
......
...@@ -21,7 +21,7 @@ AppAsset::register($this); ...@@ -21,7 +21,7 @@ AppAsset::register($this);
<?php $this->head() ?> <?php $this->head() ?>
<?php echo $this->render('head')?> <?php echo $this->render('head')?>
</head> </head>
<body class="responsiv_100pr"> <body <?/* class="responsiv_100pr"*/?>>
<?php echo $this->render('htmlcodes')?> <?php echo $this->render('htmlcodes')?>
<?php $this->beginBody() ?> <?php $this->beginBody() ?>
......
...@@ -15435,4 +15435,20 @@ h2.subsc_blog_title{ ...@@ -15435,4 +15435,20 @@ h2.subsc_blog_title{
.subsc_blog .save-button{ .subsc_blog .save-button{
margin-top: 0 !important; margin-top: 0 !important;
}
/*--*/
.img_sm {
display: none;
}
@media only screen and (max-width: 768px) {
.img_lg {
display: none;
}
.img_sm {
display: block;
}
} }
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment