135 lines
4.0 KiB
PHP
135 lines
4.0 KiB
PHP
<?php
|
||
|
||
namespace app\models\cms;
|
||
|
||
use Yii;
|
||
use app\models\Model;
|
||
|
||
|
||
/**
|
||
* This is the model class for table "{{%cms_rich_text}}".
|
||
*
|
||
* @property int $id ID
|
||
* @property int $cx_mch_id 平台商户ID
|
||
* @property string $title 标题
|
||
* @property string|null $author 作者
|
||
* @property string|null $summary 摘要
|
||
* @property string|null $cover_url 封面
|
||
* @property string $content 内容
|
||
* @property int $created_at 添加时间
|
||
* @property int $updated_at 更新时间
|
||
* @property int $is_delete 是否删除:0=否,1=是
|
||
* @property int $deleted_at 删除时间
|
||
*/
|
||
class RichText extends \yii\db\ActiveRecord
|
||
{
|
||
/**
|
||
* {@inheritdoc}
|
||
*/
|
||
public static function tableName()
|
||
{
|
||
return '{{%cms_rich_text}}';
|
||
}
|
||
|
||
/**
|
||
* {@inheritdoc}
|
||
*/
|
||
public function rules()
|
||
{
|
||
return [
|
||
[['title', 'author', 'content', 'summary', 'cover_url'], 'trim'],
|
||
[['cx_mch_id', 'created_at', 'updated_at', 'is_delete', 'deleted_at'], 'integer'],
|
||
[['title', 'content'], 'required'],
|
||
[['content'], 'string'],
|
||
[['title'], 'string', 'max' => 512],
|
||
[['author'], 'string', 'max' => 128],
|
||
[['summary', 'cover_url'], 'string', 'max' => 2048],
|
||
];
|
||
}
|
||
|
||
/**
|
||
* {@inheritdoc}
|
||
*/
|
||
public function attributeLabels()
|
||
{
|
||
return [
|
||
'id' => 'ID',
|
||
'cx_mch_id' => '平台商户ID',
|
||
'title' => '标题',
|
||
'author' => '作者',
|
||
'summary' => '摘要',
|
||
'cover_url' => '封面',
|
||
'content' => '内容',
|
||
'created_at' => '添加时间',
|
||
'updated_at' => '更新时间',
|
||
'is_delete' => '是否删除:0=否,1=是',
|
||
'deleted_at' => '删除时间',
|
||
];
|
||
}
|
||
|
||
|
||
public function beforeSave($insert) {
|
||
if(parent::beforeSave($insert)){
|
||
if($this->isNewRecord){
|
||
$this->created_at = time();
|
||
}
|
||
$this->updated_at = time();
|
||
if($this->is_delete == 1)
|
||
$this->deleted_at = time();
|
||
$this->htmlTagFilter();
|
||
return true;
|
||
} else {
|
||
return false;
|
||
}
|
||
}
|
||
|
||
public function htmlTagFilter()
|
||
{
|
||
$this->title = Model::htmlTagFilter($this->title);
|
||
$this->author = Model::htmlTagFilter($this->author);
|
||
$this->summary = Model::htmlTagFilter($this->summary);
|
||
$this->cover_url = Model::htmlTagFilter($this->cover_url);
|
||
$this->content = Model::htmlTagFilter($this->content);
|
||
}
|
||
|
||
|
||
public static function saveRichText($rich_text_id, $title, $content, $cover_url = null, $author = null, $summary = null, $cx_mch_id = 0)
|
||
{
|
||
$model = RichText::findOne([
|
||
'id' => $rich_text_id,
|
||
'cx_mch_id' => $cx_mch_id,
|
||
]);
|
||
if($model == null){
|
||
$model = new RichText();
|
||
$model->cx_mch_id = $cx_mch_id;
|
||
$model->is_delete = 0;
|
||
}
|
||
$model->title = $title;
|
||
$model->content = $content;
|
||
$model->author = $author;
|
||
$model->summary = $summary;
|
||
$model->cover_url = $cover_url;
|
||
if(!$model->save()){
|
||
return (new Model())->getModelError($model);
|
||
}
|
||
return Model::asReturnSuccess("ok", ['rich_text_id' => $model->id]);
|
||
}
|
||
|
||
public static function deleteRichText($rich_text_id, $cx_mch_id = 0)
|
||
{
|
||
$model = RichText::findOne([
|
||
'id' => $rich_text_id,
|
||
'cx_mch_id' => $cx_mch_id,
|
||
'is_delete' => 0
|
||
]);
|
||
if($model == null){
|
||
return Model::asReturnError("内容不存在或已经删");
|
||
}
|
||
$model->is_delete = 1;
|
||
if(!$model->save()){
|
||
return (new Model())->getModelError($model);
|
||
}
|
||
return Model::asReturnSuccess();
|
||
}
|
||
}
|