Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 31 additions & 15 deletions models/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*/
class Article extends \yii\db\ActiveRecord
{
public $imageFile;

/**
* {@inheritdoc}
*/
Expand All @@ -37,29 +39,30 @@ public static function tableName()
public function rules()
{
return [
[['description', 'content'], 'string'],
[['description', 'content'], 'string'],
[['title'], 'required'],
[['date'], 'date', 'format'=>'php:Y-m-d'],
[['date'], 'default', 'value'=>date('Y-m-d')],
[['date'], 'date', 'format' => 'php:Y-m-d'],
[['date'], 'default', 'value' => date('Y-m-d')],
[['viewed', 'status', 'user_id', 'category_id'], 'integer'],
[['title', 'image'], 'string', 'max' => 255],
[['title',], 'string', 'max' => 255],
[['imageFile'], 'file', 'extensions' => 'jpg,png']
];
}


public function attributeLabels()
{
return [
'id' =>"Номер",
'title' =>"Заголовок",
'description' =>"контекст",
'content' =>'контент',
'date' =>'Дата',
'image' =>'картика',
'viewed' =>'просмотры',
'status' =>'статус',
'user_id' =>'привязка юзера',
'category_id' =>'привязка категории',
'id' => "Номер",
'title' => "Заголовок",
'description' => "контекст",
'content' => 'контент',
'date' => 'Дата',
'imageFile' => 'картика',
'viewed' => 'просмотры',
'status' => 'статус',
'user_id' => 'привязка юзера',
'category_id' => 'привязка категории',
];
}

Expand Down Expand Up @@ -98,12 +101,25 @@ public function getComments()
{
return $this->hasMany(Comment::class, ['article_id' => 'id']);
}

public function getCategory(){
return $this->hasOne(Category::class, ['id' => 'category_id']);
}

public function saveImage($image){

public function saveImage($image)
{
$this->image = $image;
return $this->save(false);
}

public function upload($image)
{
$this->image = $image;
$path = Yii::getAlias("@web") . 'upload/';
$this->image = strtolower(md5(uniqid($image->baseName))) . '.' . $image->extension;

$image->saveAs($path . $this->image);
$this->save(false);
}
}
1 change: 1 addition & 0 deletions models/ArticleSeacrh.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function scenarios()
*/
public function search($params)
{
// $query = Article::find()->with('category');
$query = Article::find();

// add conditions that should always apply here
Expand Down
17 changes: 11 additions & 6 deletions models/Category.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

namespace app\models;

use app\models\Article;

use Yii;
Expand Down Expand Up @@ -41,20 +42,25 @@ public function attributeLabels()
'id' => 'ID',
];
}

public function getArticle()
{
return $this->hasMany(Article::className(), ['category_id' => 'id']);
return $this->hasMany(Article::class, ['category_id' => 'id']);
}

public function getArticlesCount(){
public function getArticlesCount()
{
return $this->getArticle()->count();
}

public static function getAll(){
public static function getAll()
{
return Category::find()->all();
}
public static function getArticlesByCategory($category_id){
$query = Article::find()->where(["category_id"=> $category_id]);

public static function getArticlesByCategory($category_id)
{
$query = Article::find()->where(["category_id" => $category_id]);
$count = $query->count();
$pagination = new Pagination(['totalCount' => $count, 'pageSize' => 3]);

Expand All @@ -68,6 +74,5 @@ public static function getArticlesByCategory($category_id){
return $data;



}
}
7 changes: 7 additions & 0 deletions modules/admin/controllers/ArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public function actionIndex()
*/
public function actionView($id)
{
$model = $this->findModel($id);
return $this->render('view', [
'model' => $this->findModel($id),
]);
Expand Down Expand Up @@ -96,7 +97,13 @@ public function actionUpdate($id)
{
$model = $this->findModel($id);




if ($this->request->isPost && $model->load($this->request->post()) && $model->save()) {
$model->imageFile = UploadedFile::getInstance($model, 'imageFile');

$model->upload($model->imageFile);
return $this->redirect(['view', 'id' => $model->id]);
}

Expand Down
4 changes: 3 additions & 1 deletion modules/admin/views/article/_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@

<?= $form->field($model, 'date')->textInput() ?>

<?= $form->field($model, 'image')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'imageFile')->fileInput() ?>

<?= $form->field($model, 'viewed')->textInput() ?>

<?= $form->field($model, 'status')->textInput() ?>

<?= $form->field($model, 'user_id')->textInput() ?>

<!-- --><?php //= Html::dropDownList('category', $model->category->id,[])?>

<?= $form->field($model, 'category_id')->textInput() ?>

<div class="form-group">
Expand Down
12 changes: 6 additions & 6 deletions modules/admin/views/article/_search.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
'method' => 'get',
]); ?>

<?= $form->field($model, 'id') ?>
<!-- --><?php //= $form->field($model, 'id') ?>

<?= $form->field($model, 'title') ?>

<?= $form->field($model, 'description') ?>

<?= $form->field($model, 'content') ?>

<?= $form->field($model, 'date') ?>
<!-- --><?php //= $form->field($model, 'description') ?>
<!---->
<!-- --><?php //= $form->field($model, 'content') ?>
<!---->
<!-- --><?php //= $form->field($model, 'date') ?>

<?php // echo $form->field($model, 'image') ?>

Expand Down
27 changes: 21 additions & 6 deletions modules/admin/views/article/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@
<?= Html::a('Create Article', ['create'], ['class' => 'btn btn-success']) ?>
</p>

<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<?php echo $this->render('_search', ['model' => $searchModel]); ?>

<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
// 'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],

'id',
'title',
Expand All @@ -36,14 +35,30 @@
'date',
//'image',
//'viewed',
//'status',
// 'status',
[
'attribute' => 'status',
'value' => function ($data) {
return !$data->status ?
'<p class="text-info">Активен</p>' :
'<p class="text-success">Завершён</p>';
},
'format' => 'raw',

],
//'user_id',
//'category_id',
// 'category_id',
[
"attribute" => 'category_id',
'value' => function ($data) {
return !$data->category ? "Категория" : $data->category->title;
},
],
[
'class' => ActionColumn::className(),
'urlCreator' => function ($action, Article $model, $key, $index, $column) {
return Url::toRoute([$action, 'id' => $model->id]);
}
}
],
],
]); ?>
Expand Down
4 changes: 3 additions & 1 deletion modules/admin/views/category/_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@

<?php $form = ActiveForm::begin(); ?>

<?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>
<!-- --><?php //= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>

<?=$form->field($model, 'title')->
dropDownList(\yii\helpers\ArrayHelper::map(\app\models\Category::find()->all(), 'id', 'title'))?>
<div class="form-group">
<?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
</div>
Expand Down