142 lines
4.2 KiB
PHP
142 lines
4.2 KiB
PHP
<?php
|
||
|
||
namespace app\models\cms;
|
||
|
||
use Yii;
|
||
use app\models\Model;
|
||
|
||
/**
|
||
* This is the model class for table "{{%cms_post}}".
|
||
*
|
||
* @property int $id ID
|
||
* @property int $cx_mch_id 平台商户ID
|
||
* @property int $scene 场景:0=默认
|
||
* @property int $rich_text_id 富文本ID
|
||
* @property int $sort 排序,默认100,升序
|
||
* @property int $status 状态:0=草稿,1=已发布,2=回收站
|
||
* @property int $is_top 是否置顶:0=否,1=是
|
||
* @property int $allow_read 是否允许阅读:0=否,1=是
|
||
* @property string|null $password 访问密码
|
||
* @property string|null $url_alias URL别名
|
||
* @property int $viewed_num 浏览量
|
||
* @property int $created_at 添加时间
|
||
* @property int $updated_at 更新时间
|
||
* @property int $published_at 发布时间
|
||
* @property int $is_delete 是否删除:0=否,1=是
|
||
* @property int $deleted_at 删除时间
|
||
*/
|
||
class Post extends \yii\db\ActiveRecord
|
||
{
|
||
const STATUS_DRAFT = 0;
|
||
const STATUS_PUBLISHED = 1;
|
||
const STATUS_TRASH = 2;
|
||
|
||
const SCENE_DEFAULT = 0;
|
||
|
||
/**
|
||
* {@inheritdoc}
|
||
*/
|
||
public static function tableName()
|
||
{
|
||
return '{{%cms_post}}';
|
||
}
|
||
|
||
/**
|
||
* {@inheritdoc}
|
||
*/
|
||
public function rules()
|
||
{
|
||
return [
|
||
[['cx_mch_id', 'scene', 'rich_text_id', 'sort', 'status', 'is_top', 'allow_read', 'viewed_num', 'created_at', 'updated_at', 'published_at', 'is_delete', 'deleted_at'], 'integer'],
|
||
[['rich_text_id'], 'required'],
|
||
[['password'], 'string', 'max' => 256],
|
||
[['url_alias'], 'string', 'max' => 512],
|
||
];
|
||
}
|
||
|
||
/**
|
||
* {@inheritdoc}
|
||
*/
|
||
public function attributeLabels()
|
||
{
|
||
return [
|
||
'id' => 'ID',
|
||
'cx_mch_id' => '平台商户ID',
|
||
'scene' => '场景:0=默认',
|
||
'rich_text_id' => '富文本ID',
|
||
'sort' => '排序,默认100,升序',
|
||
'status' => '状态:0=草稿,1=已发布,2=回收站',
|
||
'is_top' => '是否置顶:0=否,1=是',
|
||
'allow_read' => '是否允许阅读:0=否,1=是',
|
||
'password' => '访问密码',
|
||
'url_alias' => 'URL别名',
|
||
'viewed_num' => '浏览量',
|
||
'created_at' => '添加时间',
|
||
'updated_at' => '更新时间',
|
||
'published_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->password = Model::htmlTagFilter($this->password);
|
||
$this->url_alias = Model::htmlTagFilter($this->url_alias);
|
||
}
|
||
|
||
public static function statusLabels()
|
||
{
|
||
return [
|
||
'0' => '草稿',
|
||
'1' => '已发布',
|
||
'2' => '回收站',
|
||
];
|
||
}
|
||
|
||
public static function getStatus($status)
|
||
{
|
||
$labels = self::statusLabels();
|
||
return isset($labels[$status]) ? $labels[$status] : "未知";
|
||
}
|
||
|
||
/**
|
||
* @param integer $post_id 页面ID
|
||
* @param integer $cx_mch_id 平台商户ID
|
||
* return integer
|
||
*/
|
||
public static function setViewedNum($post_id, $cx_mch_id = 0)
|
||
{
|
||
$model = Post::findOne([
|
||
'id' => $post_id,
|
||
'cx_mch_id' => $cx_mch_id,
|
||
'is_delete' => 0
|
||
]);
|
||
if($model == null)
|
||
return 0;
|
||
$model->viewed_num = $model->viewed_num + 1;
|
||
$model->save();
|
||
return $model->viewed_num;
|
||
}
|
||
|
||
public function getRichText()
|
||
{
|
||
return $this->hasOne(RichText::className(), ['id' => 'rich_text_id'])->where(['is_delete' => 0]);
|
||
}
|
||
}
|