upgrade blog and file upload

parent 8125d08a
......@@ -5,16 +5,24 @@ use Yii;
use yii\base\Behavior;
use yii\db\ActiveRecord;
use yii\web\UploadedFile;
use yii\imagine\Image;
class FileUploadBehavior extends Behavior
{
public $path;
public $folder;
public $file;
public $unlinkFile = false;
public $field;
public $thumb = false;
public $thumbSize = ['100', '100'];
public function events()
{
return [
......@@ -38,17 +46,49 @@ class FileUploadBehavior extends Behavior
$field = $this->field;
$this->owner->$field = $this->folder . date('dmYHis-') . uniqid() . '.' . $this->file->extension;
$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 && file_exists($this->getAbsolutePath() . $this->owner->$field))
if($this->owner->$field)
{
unlink($this->getAbsolutePath() . $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;
}
}
......
......@@ -13,12 +13,29 @@ use common\modules\blog\models\Post;
*/
class PostController extends BaseController
{
public static function actionsTitles(){
public static function actionsTitles()
{
return [
'Index' => 'Блог',
'View' => 'Просмотр записи',
];
}
/**
* Displays all Post models.
* @return mixed
*/
public function actionIndex()
{
$this->meta_title = Yii::t('menu', 'Blog') . ' - ' . \Yii::$app->params['name'];
$models = Post::find()->where(['active' => 1])->all();
return $this->render('index', [
'models' => $models,
]);
}
/**
* Displays a single Post model.
* @param string $url
......@@ -26,8 +43,14 @@ class PostController extends BaseController
*/
public function actionView($url)
{
$model = $this->findModelByUrl($url);
$this->meta_title = $model->metaTag->title . ' - ' . \Yii::$app->params['name'];
$this->meta_description = $model->metaTag->description;
$this->meta_keywords = $model->metaTag->keywords;
return $this->render('view', [
'model' => $this->findModelByUrl($url),
'model' => $model,
]);
}
......
......@@ -84,6 +84,8 @@ class Post extends \common\components\ActiveRecordModel
],
'file' => [
'class' => 'common\components\activeRecordBehaviors\FileUploadBehavior',
'thumb' => true,
'thumbSize' => ['280', '141'],
'path' => '@frontend/web',
'folder' => '/uploads/blog/',
'field' => 'preview'
......@@ -170,4 +172,11 @@ class Post extends \common\components\ActiveRecordModel
{
return $this->hasMany(PostTagAssign::className(), ['post_id' => 'id']);
}
public function getThumbnailUrl()
{
$path = pathinfo($this->preview);
return $path['dirname'] . '/' . $path['filename'] . '.thumb.' . $path['extension'];
}
}
......@@ -82,4 +82,8 @@ use common\modules\content\widgets\MetaTagsWidget;
<?php ActiveForm::end(); ?>
</div>
\ No newline at end of file
</div>
<script type="text/javascript">
$('.field-post-tags ul').addClass('primary');
</script>
\ No newline at end of file
<?php
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\grid\GridView;
use common\modules\blog\models\Post;
......@@ -24,6 +25,10 @@ $this->params['breadcrumbs'][] = $this->title;
'columns' => [
// ['class' => 'yii\grid\SerialColumn'],
[
'attribute' => 'created_at',
'format' => ['date', 'php:d.m.Y H:i:s']
],
[
'attribute' => 'url',
'format' => 'raw',
......@@ -40,6 +45,22 @@ $this->params['breadcrumbs'][] = $this->title;
return Post::$active_title[$model->active];
}
],
[
'header' => 'Теги',
'format' => 'raw',
'value' => function($model)
{
if($model->postTags)
{
$b = '<span class="label label-primary">';
$e = '</span>';
return $b . implode($e.' '.$b, array_keys(ArrayHelper::map($model->postTags, 'name', 'id'))) . $e;
}
return null;
}
],
[
'class' => 'common\components\ColorActionColumn',
......
<?php
use yii\helpers\Url;
use common\modules\blog\models\Post;
use common\modules\blog\models\PostTag;
use common\models\Settings;
?>
<div class="col-md-4 col-xs-4 col-sm-12">
<aside class="sidebar">
<div class="sidebar_module">
<h2>Категории</h2>
<div class="sidebar_module_body">
<?php $tags = PostTag::find()->orderBy('RAND()')->limit(7)->all();
foreach ($tags as $tag) : ?>
<a href="#" class="cat_link_mod"><?=$tag->name?></a>
<?php endforeach; ?>
</div>
</div>
<!-- <div class="sidebar_module">
<div class="sidebar_module_body">
<a href="#" class="sidebar_btn">
Предложить тему
<div class="blog_toltip_right">Предложите свою тему и мы напишем</div>
</a>
<a href="#" class="sidebar_btn">Послать статью</a>
</div>
</div> -->
<?php $posts = Post::find();
if($model)
{
$posts = $posts->where(['!=', 'id', $model->id]);
}
$posts = $posts->andWhere(['active' => 1])->orderBy('RAND()')->limit(3)->all();
if($posts) : ?>
<div class="sidebar_module">
<h2>Популярные</h2>
<div class="sidebar_module_body">
<?php foreach ($posts as $post) : ?>
<div class="article_itm">
<?php if($post->preview) : ?>
<img src="<?=$post->getThumbnailUrl()?>" alt="">
<?php endif; ?>
<a href="<?=Url::to(['/blog/'.$post->url])?>" class="article_itm_link"><?=$post->lang->title?></a>
</div>
<?php endforeach; ?>
</div>
</div>
<?php endif; ?>
<div class="sidebar_module">
<h2>Все секреты в наших соц сетях!</h2>
<div class="sidebar_module_body">
<ul class="sidebar_social">
<?php $s = Settings::getValue('social-link-fb'); if($s): ?><li><a href="<?=$s?>" class="sidebar_social_fb"></a></li><? endif; ?>
<!-- <li><a href="#" class="sidebar_social_ok"></a></li> -->
<?php $s = Settings::getValue('social-link-twitter'); if($s): ?><li><a href="<?=$s?>" class="sidebar_social_tw"></a></li><? endif; ?>
<?php $s = Settings::getValue('social-link-google'); if($s): ?><li><a href="<?=$s?>" class="sidebar_social_gp"></a></li><? endif; ?>
<?php $s = Settings::getValue('social-link-youtube'); if($s): ?><li><a href="<?=$s?>" class="sidebar_social_yt"></a></li><? endif; ?>
<?php $s = Settings::getValue('social-link-vk'); if($s): ?><li><a href="<?=$s?>" class="sidebar_social_vk"></a></li><? endif; ?>
<?php $s = Settings::getValue('social-link-instagram'); if($s): ?><li><a href="<?=$s?>" class="sidebar_social_inst"></a></li><? endif; ?>
</ul>
</div>
</div>
</aside>
</div>
\ No newline at end of file
<?php
use yii\widgets\ActiveForm;
use yii\helpers\Html;
use common\modules\bids\models\Bid;
?>
<div class="subsc_blog">
<h2 class="subsc_blog_title">Понравилось? Подпишись на обновления!</h2>
<div class="subsc_blog_txt">Мы страемся публиковать в данном разделе только полезный и уникальный контент для рынка. По этому подпишись и ты будешь первым, кто получит уведомление о свежей публикации</div>
<?php
$model = new Bid;
$model->scenario = Bid::SCENARIO_SUBSCRIBE;
$form = ActiveForm::begin([
'action' => '/',
'options' => [
'class' => 'subsc_blog_form bids-form',
],
]); ?>
<?php echo Html::hiddenInput('scenario', $model->scenario, ['class' => 'not_clear']); ?>
<?php echo Html::hiddenInput('Bid[form]', Bid::FORM_SUBSCRIBE, ['class' => 'not_clear']); ?>
<?php echo $form->field($model, 'email', [
'template' => '<div class="row"><div class="col-sm-4">{input}</div></div>',
'errorOptions' => []
])->textInput([
'placeholder' => 'E-mail*'
]); ?>
<?php echo Html::submitButton('Подписаться', ['class' => 'save-button']); ?>
<?php ActiveForm::end(); ?>
</div>
\ No newline at end of file
<?php
use yii\helpers\Html;
use yii\helpers\Url;
/* @var $this yii\web\View */
/* @var $model common\modules\blog\models\Post */
?>
<div class="blog_container">
<div class="container">
<div class="row">
<div class="col-md-8 col-xs-8 col-sm-12">
<section class="blog">
<h1><?=Yii::t('menu', 'Blog')?></h1>
<?php foreach ($models as $model) : ?>
<article class="article_short">
<a href="<?=Url::to(['/blog/'.$model->url])?>" class="article_short_title"><?=$model->lang->title?></a>
<div class="article_short_head">
<span class="article_short_date"><?=date('d.m.Y', $model->created_at)?></span>
<!-- <span class="article_short_view">
180
<div class="blog_toltip_left">Количество просмотров</div>
</span> -->
<!-- <ul class="article_short_social">
<li>
<a href="#"><img src="/images/icon/sh_social_vk.png" height="30" width="30" alt=""></a>
<a href="#"><img src="/images/icon/sh_social_fb.png" height="30" width="30" alt=""></a>
<a href="#"><img src="/images/icon/sh_social_tw.png" height="30" width="30" alt=""></a>
<a href="#"><img src="/images/icon/sh_social_gp.png" height="30" width="30" alt=""></a>
<a href="#"><img src="/images/icon/sh_social_t.png" height="30" width="30" alt=""></a>
</li>
</ul> -->
</div>
<div class="article_short_tags">
<?php foreach ($model->postTags as $tag) : ?>
<a href="#"># <?=$tag->name?></a>
<?php endforeach; ?>
</div>
<div class="article_short_txt">
<?php if($model->preview) :
echo Html::img($model->preview);
endif; ?>
<?=$model->lang->text?>
</div>
</article>
<?php endforeach; ?>
<?=$this->render('_subscribe')?>
</section>
</div>
<?=$this->render('_sidebar')?>
</div>
</div>
</div>
<div class="end_keys_neft"></div>
<?=$this->render('@app/views/layouts/footer');?>
\ No newline at end of file
<?php
use yii\helpers\Html;
use yii\widgets\DetailView;
/* @var $this yii\web\View */
/* @var $model common\modules\blog\models\Post */
$this->title = $model->id;
$this->params['breadcrumbs'][] = ['label' => 'Posts', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="post-view">
<h1><?= Html::encode($this->title) ?></h1>
<p>
<?= Html::a('Update', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Delete', ['delete', 'id' => $model->id], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => 'Are you sure you want to delete this item?',
'method' => 'post',
],
]) ?>
</p>
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'id',
'url:url',
'active',
'created_at',
'updated_at',
],
]) ?>
<div class="blog_container">
<div class="container">
<div class="row">
<div class="col-md-8 col-xs-8 col-sm-12">
<section class="blog">
<h1><?=$model->lang->title?></h1>
<article class="article_short">
<div class="article_short_head">
<span class="article_short_date"><?=date('d.m.Y', $model->created_at)?></span>
<!-- <span class="article_short_view">
180
<div class="blog_toltip_left">Количество просмотров</div>
</span> -->
<!-- <ul class="article_short_social">
<li>
<a href="#"><img src="/images/icon/sh_social_vk.png" height="30" width="30" alt=""></a>
<a href="#"><img src="/images/icon/sh_social_fb.png" height="30" width="30" alt=""></a>
<a href="#"><img src="/images/icon/sh_social_tw.png" height="30" width="30" alt=""></a>
<a href="#"><img src="/images/icon/sh_social_gp.png" height="30" width="30" alt=""></a>
<a href="#"><img src="/images/icon/sh_social_t.png" height="30" width="30" alt=""></a>
</li>
</ul> -->
</div>
<div class="article_short_tags">
<?php foreach ($model->postTags as $tag) : ?>
<a href="#"># <?=$tag->name?></a>
<?php endforeach; ?>
</div>
<div class="article_short_txt">
<?php if($model->preview) :
echo Html::img($model->preview);
endif; ?>
<?=$model->lang->text?>
</div>
</article>
<?=$this->render('_subscribe')?>
</section>
</div>
<?=$this->render('_sidebar', ['model' => $model])?>
</div>
</div>
</div>
<div class="end_keys_neft"></div>
<?=$this->render('@app/views/layouts/footer');?>
......@@ -153,6 +153,7 @@ return [
'faq/view/all' => 'faq/faq/all',
'faq/<url>' => 'faq/faq/view',
'faq' => 'faq/faq/index',
'blog' => 'blog/post/index',
'blog/<url>' => 'blog/post/view',
'school' => 'school/course/index',
'school/course/<id>' => 'school/course/view',
......
......@@ -12,7 +12,7 @@ use common\modules\languages\widgets\Languages;
<ul>
<li><?=Html::a(\Yii::t('menu', 'About'), ['/about']);?></li>
<li><?=Html::a(\Yii::t('menu', 'Portfolio'), ['/portfolio']);?></li>
<li><?=Html::a(\Yii::t('menu', 'Blog'), ['/#']);?></li>
<li><?=Html::a(\Yii::t('menu', 'Blog'), ['/blog']);?></li>
<!-- <li><?=Html::a(\Yii::t('menu', 'Analytics school'), ['/school']);?></li> -->
<li><?=Html::a(\Yii::t('menu', 'Contacts'), ['/contacts']);?></li>
</ul>
......@@ -31,7 +31,7 @@ use common\modules\languages\widgets\Languages;
<ul>
<li><?=Html::a(\Yii::t('menu', 'About'), ['/about']);?></li>
<li><?=Html::a(\Yii::t('menu', 'Portfolio'), ['/portfolio']);?></li>
<li><?=Html::a(\Yii::t('menu', 'Blog'), ['/#']);?></li>
<li><?=Html::a(\Yii::t('menu', 'Blog'), ['/blog']);?></li>
<!-- <li><?=Html::a(\Yii::t('menu', 'Analytics school'), ['/school']);?></li> -->
<li><?=Html::a(\Yii::t('menu', 'Contacts'), ['/contacts']);?></li>
</ul>
......
......@@ -57,7 +57,8 @@ a.login_form_link, a.login_form_popup_link, a.reg_popup_link, a.reg_form_link{
.sh_ft .popup_text, .reg_form .popup_text{
margin-top: 10px;
}
.subsc_form input {
.subsc_form input,
.subsc_blog_form input {
display: block;
width: 273px;
height: 49px;
......
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