fix blog

parent e945554e
<?php
namespace common\components\activeRecordBehaviors;
use Yii;
use yii\base\Behavior;
use yii\db\ActiveRecord;
use yii\web\UploadedFile;
class FileUploadBehavior extends Behavior
{
public $path;
public $folder;
public $file;
public $unlinkFile = false;
public $field;
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()))
{
mkdir($this->getAbsolutePath(), 0777, true);
}
$field = $this->field;
$this->owner->$field = $this->folder . date('dmYHis-') . uniqid() . '.' . $this->file->extension;
$this->file->saveAs($this->getAbsolutePath() . $this->owner->$field);
}
private function deleteFile()
{
$field = $this->field;
if($this->owner->$field && 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, 'file');
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, 'file');
if($this->file)
{
$this->uploadFile();
}
return true;
}
public function Delete($event)
{
$this->deleteFile();
return true;
}
}
......@@ -54,6 +54,8 @@ class Post extends \common\components\ActiveRecordModel
[['active', 'created_at', 'updated_at'], 'integer'],
[['url'], 'string', 'max' => 255],
[['url'], 'unique'],
[['preview', 'unlinkFile'], 'safe'],
[['file'], 'file', 'skipOnEmpty' => true, 'extensions' => 'png, jpg, jpeg, gif'],
];
}
......@@ -66,6 +68,12 @@ class Post extends \common\components\ActiveRecordModel
'langs' => [
'class' => 'common\modules\blog\components\PostLangBehavior',
],
'file' => [
'class' => 'common\components\activeRecordBehaviors\FileUploadBehavior',
'path' => '@frontend/web',
'folder' => '/uploads/blog/',
'field' => 'preview'
],
];
}
......@@ -78,6 +86,9 @@ class Post extends \common\components\ActiveRecordModel
'id' => 'ID',
'url' => 'Ссылка',
'active' => 'Видимость',
'file' => 'Изображение',
'preview' => 'Изображение',
'unlinkFile' => 'Удалить изображение',
'created_at' => 'Дата добавления',
'updated_at' => 'Дата обновления',
];
......
......@@ -3,6 +3,8 @@
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use common\modules\blog\models\Post;
/* @var $this yii\web\View */
/* @var $model common\modules\blog\models\Post */
/* @var $form yii\widgets\ActiveForm */
......@@ -10,7 +12,11 @@ use yii\widgets\ActiveForm;
<div class="post-form">
<?php $form = ActiveForm::begin(); ?>
<?php $form = ActiveForm::begin([
'options' => [
'enctype' => 'multipart/form-data'
]
]); ?>
<?= $form->field($model, 'url')->textInput(['maxlength' => 250])->hint('Для создания ЧПУ («Человеку Понятный Урл») укажите латинскими буквами путь, например, razdel/podrazdel/nazvanie_stranici.html') ?>
......@@ -23,6 +29,15 @@ use yii\widgets\ActiveForm;
'label' => ' '
], false); ?>
<?php if($model->preview)
{
echo Html::img(\Yii::$app->params['frontUrl'] . '/uploads/blog/' . $model->preview);
} ?>
<?= $form->field($model, 'file')->fileInput() ?>
<?= $form->field($model, 'unlinkFile')->checkbox(); ?>
<ul class="nav nav-pills">
<?php $c = 0; foreach ($langs as $i => $content) : $c++; ?>
<li class="<?=($c==1?'active':'')?>"><a href="#lang-<?=$content->lang->url?>" data-toggle="tab"><?=$content->lang->name?></a></li>
......
......@@ -24,7 +24,14 @@ $this->params['breadcrumbs'][] = $this->title;
'columns' => [
// ['class' => 'yii\grid\SerialColumn'],
'url:url',
[
'attribute' => 'url',
'format' => 'raw',
'value' => function($model)
{
return Html::a($model->url, \Yii::$app->params['frontUrl'] . '/blog/' . $model->url, ['target' => '_blank', 'title' => 'Просмотреть как страницу видит пользователь', 'data-toggle'=>"tooltip"]);
}
],
[
'attribute' => 'active',
'filter' => Post::$active_title,
......
......@@ -112,18 +112,9 @@ class ContentAdminController extends AdminController
try
{
$model->attributes = Yii::$app->request->post('CoContent');
$model->image = UploadedFile::getInstance($model, 'image');
if($model->validate())
if($model->save())
{
if($model->image)
{
$model->upload();
$model->image = null;
}
$model->save();
$transaction->commit();
return $this->redirect(['manage']);
}
......@@ -191,23 +182,9 @@ class ContentAdminController extends AdminController
try
{
$model->attributes = Yii::$app->request->post('CoContent');
$model->image = UploadedFile::getInstance($model, 'image');
if($model->validate())
if($model->save())
{
if($model->image)
{
if($model->preview)
{
$model->deletePreview();
}
$model->upload();
$model->image = null;
}
$model->save();
$transaction->commit();
return $this->redirect(['manage']);
}
......
......@@ -32,10 +32,6 @@ class CoContent extends \common\components\ActiveRecordModel
const CUSTOM_DARK = 'dark';
const CUSTOM_WHITE = 'white';
const PHOTO_FOLDER = '/uploads/content/';
public $image;
public static $cutom_list = [
self::CUSTOM_DARK => 'Темный',
self::CUSTOM_WHITE => 'Светлый',
......@@ -61,6 +57,12 @@ class CoContent extends \common\components\ActiveRecordModel
'langs' => [
'class' => 'common\modules\content\components\CoContentLangBehavior',
],
'file' => [
'class' => 'common\components\activeRecordBehaviors\FileUploadBehavior',
'path' => '@frontend/web',
'folder' => '/uploads/content/',
'field' => 'preview'
],
'timestamp' => [
'class' => TimestampBehavior::className(),
'createdAtAttribute' => 'created_at',
......@@ -100,7 +102,7 @@ class CoContent extends \common\components\ActiveRecordModel
{
return [
[['active'], 'integer'],
[['image'], 'file', 'skipOnEmpty' => true, 'extensions' => 'png, jpg, jpeg, gif'],
[['file'], 'file', 'skipOnEmpty' => true, 'extensions' => 'png, jpg, jpeg, gif'],
[['url'], 'required'],
[['url'], 'string', 'max' => 250],
[['category_id', 'priority', 'custom'], 'safe'],
......@@ -118,7 +120,7 @@ class CoContent extends \common\components\ActiveRecordModel
'category_id' => Yii::t('content', 'Category ID'),
'url' => Yii::t('content', 'Url'),
'name' => Yii::t('content', 'Name'),
'image' => 'Превью',
'file' => 'Превью',
'custom' => 'Заголовок',
'priority' => 'Приоритет в Sitemap',
'title' => Yii::t('content', 'Title'),
......@@ -176,36 +178,4 @@ class CoContent extends \common\components\ActiveRecordModel
{
return $this->hasMany(CoContentLang::className(), ['content_id' => 'id']);
}
private function getPath()
{
return Yii::getAlias('@frontend/web') . self::PHOTO_FOLDER;
}
public function upload()
{
if ($this->validate())
{
if(!file_exists($this->getPath()))
{
mkdir($this->getPath(), 0777, true);
}
$this->preview = date('dmYHis-') . uniqid() . '.' . $this->image->extension;
$this->image->saveAs($this->getPath() . $this->preview);
return true;
}
else
{
return false;
}
}
public function deletePreview()
{
if(file_exists($this->getPath() . $this->preview))
{
unlink($this->getPath() . $this->preview);
}
}
}
......@@ -47,10 +47,10 @@ $blocks = \common\modules\content\models\CoBlocks::find()->all();
<?php if($model->preview)
{
echo Html::img(\Yii::$app->params['frontUrl'] . CoContent::PHOTO_FOLDER . $model->preview);
echo Html::img(\Yii::$app->params['frontUrl'] . $model->preview);
} ?>
<?= $form->field($model, 'image')->fileInput() ?>
<?= $form->field($model, 'file')->fileInput() ?>
<ul class="nav nav-pills">
<?php $c = 0; foreach ($langs as $i => $content) : $c++; ?>
......
......@@ -28,7 +28,7 @@ use common\modules\content\models\CoCategory;
'attribute' => 'url',
'format' => 'raw',
'value' => function($data) {
return Html::a($data->url, Yii::$app->params['frontUrl'].($data->url!='/'?'/':'').$data->url, ['target' => '_blank', 'title' => 'Просмотреть как страницу видит пользователь', 'data-toggle'=>"tooltip"]);$data->category->name;
return Html::a($data->url, Yii::$app->params['frontUrl'].($data->url!='/'?'/':'').$data->url, ['target' => '_blank', 'title' => 'Просмотреть как страницу видит пользователь', 'data-toggle'=>"tooltip"]);
}
],
[
......
<?php
use yii\db\Schema;
use yii\db\Migration;
class m160210_053816_add_column_posts extends Migration
{
// Use safeUp/safeDown to run migration code within a transaction
public function safeUp()
{
$this->addColumn('posts', 'preview', Schema::TYPE_STRING.'(60) DEFAULT NULL');
}
public function safeDown()
{
$this->dropColumn('posts', 'preview');
}
}
......@@ -16,7 +16,7 @@ $models = CoContent::find()
<?php foreach ($models as $model) : ?>
<div class="col-md-6 col-xs-6 col-sm-12">
<div class="keys_block_small">
<img src="<?=CoContent::PHOTO_FOLDER . $model->preview?>" height="338" width="455">
<img src="<?=$model->preview?>" height="338" width="455">
<div class="keys_small_title" <?if($model->custom==CoContent::CUSTOM_WHITE){?>style="color:#fff;"<?}?>><?=$model->lang->title?></div>
<div class="keys_small_foot">
<?=Html::a('<span>Подробнее</span>', ['/'.$model->url], ['class' => 'keys_small_btn_more'])?>
......
......@@ -30,9 +30,9 @@ $cases = CoContent::find()->where([
<span class="others_project__subtitle"><?=$model->lang->title?></span>
<p class="others_project__txt"><?=$model->lang->text?></p>
<a href="<?=Url::to(['/' . $model->url])?>" class="others_project__bt">Подробнее</a>
<a href="<?=Url::to(['/case'])?>" class="others_project__link">Все проекты (<?=$cases->count();?>)</a>
<a href="<?=Url::to(['/portfolio'])?>" class="others_project__link">Все проекты (<?=$cases->count();?>)</a>
</div>
<img src="<?=CoContent::PHOTO_FOLDER . $model->preview?>" height="476" width="489">
<img src="<?=$model->preview?>" height="476" width="489">
</div>
<?php endforeach; ?>
......
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