<?php
/**
 * Created by PhpStorm.
 * User: leo
 * Date: 2018/2/24
 * Time: 下午12:48
 */
namespace backendApi\modules\v1\controllers;

use common\models\Article;
use common\models\ArticleCategory;
use common\models\forms\ArticleCategoryForm;
use common\models\forms\ArticleForm;
use Yii;

class ArticleController extends BaseController
{
    public $modelClass = Article::class;
    public function behaviors() {
        $behaviors = parent::behaviors();
        //$behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_JSON;
        return $behaviors;
    }

    /**
     * 文章分类
     * @return mixed
     * @throws \yii\web\HttpException
     */
    public function actionCategory(){
        $data = ArticleCategory::lists('', [], [
            'orderBy' => 'SORT ASC, CREATED_AT DESC',
        ]);
        return static::notice($data);
    }

    /**
     * 添加分类
     * @return mixed
     * @throws \yii\web\HttpException
     */
    public function actionCategoryAdd(){
        if(Yii::$app->request->isPost) {
            return parent::edit(ArticleCategoryForm::class, '文章分类添加成功');
        }
    }

    /**
     * 删除文章分类
     * @return mixed
     * @throws \yii\db\Exception
     * @throws \yii\web\HttpException
     */
    public function actionCategoryDelete(){
        $result = static::delete(ArticleCategory::class);
        return $result;
    }

    /**
     * 分类排序
     * @return mixed
     * @throws \yii\web\HttpException
     */
    public function actionCategorySort(){
        if(Yii::$app->request->get('id')){
            $formModel = new ArticleCategoryForm();
            $formModel->scenario = 'sort';
            if($formModel->load(Yii::$app->request->get(), '') && $formModel->sortTo()){
                return static::notice('排序成功');
            } else {
                return static::notice('排序失败', 400);
            }
        }
    }

    /**
     * 文章
     * @return mixed
     * @throws \yii\web\HttpException
     */
    public function actionIndex(){
        $data = Article::lists('', [], [
            'select' => 'ID,TITLE,CID,STATUS,CREATED_AT',
            'orderBy' => 'CREATED_AT',
        ]);
        // 全部分类
        $data['allCategory'] = ArticleCategory::getAllCategory();
        return static::notice($data);
    }

    /**
     * 添加文章
     * @return mixed
     * @throws \yii\web\HttpException
     */
    public function actionAdd(){
        if(Yii::$app->request->isPost) {
            return parent::edit(ArticleForm::class, '文章添加成功');
        }
        // 获取全部分类
        $allCategory = ArticleCategory::find()->where('STATUS=1')->asArray()->all();
        return static::notice(['allCategory'=>$allCategory]);
    }

    /**
     * 编辑文章
     * @return mixed
     * @throws \yii\base\Exception
     * @throws \yii\web\HttpException
     */
    public function actionEdit(){
        $id = Yii::$app->request->get('id');
        if(Yii::$app->request->isPost){
            return parent::edit(ArticleForm::class, '编辑成功');
        }
        $oneData = Article::findOneAsArray(['ID'=>$id]);
        $oneData['CONTENT'] = is_resource($oneData['CONTENT']) ? stream_get_contents($oneData['CONTENT']) : '';
        // 暂时先从文件中取内容
        $path = \Yii::getAlias('@common/runtime/articleContent/').$oneData['ID'];
        if(!file_exists($path)){
            $oneData['CONTENT'] = '';
        } else {
            $oneData['CONTENT'] = file_get_contents($path);
        }
        // 获取全部分类
        $allCategory = ArticleCategory::getAllCategory();
        return static::notice(['oneData'=>$oneData, 'allCategory'=>$allCategory]);
    }

    /**
     * 删除文章
     * @return mixed
     * @throws \yii\db\Exception
     * @throws \yii\web\HttpException
     */
    public function actionArticleDelete(){
        $result = static::delete(Article::class);
        return $result;
    }

}