cxfoot/models/common/cms/CommonPostListForm.php
2023-10-24 14:54:18 +08:00

135 lines
4.3 KiB
PHP

<?php
/**
* @author Any
* @description KISS
* @date 2021年7月29日
* @version 1.0.0
*
* _____LOG_____
*
*/
namespace app\models\common\cms;
use app\models\cms\Post;
use app\models\cms\PostMeta;
use app\models\cms\RichText;
use app\models\Model;
use yii\data\Pagination;
use app\components\SiteHelper;
use app\models\Cat;
class CommonPostListForm extends Model
{
public $keywords;
public $limit;
public $page;
public $cat_id;
public $cx_mch_id;
public $status;
public $is_top;
public $post_id;
public $url_alias;
public $scene;
public function rules()
{
return [
[['keywords', 'url_alias'], 'trim'],
[['keywords', 'url_alias'], 'string'],
[['limit', 'page', 'cx_mch_id', 'status', 'is_top' ,'post_id', 'scene', 'cat_id'], 'integer'],
[['page'], 'default', 'value' => 1],
[['limit'], 'default', 'value' => 20],
[['cx_mch_id'], 'required'],
[['post_id'], 'required', 'on' => 'detail'],
];
}
public function search()
{
if(!$this->validate()){
return $this->getModelError();
}
$cat_query = PostMeta::find()
->where([
'type' => PostMeta::TYPE_CAT,
'is_delete' => 0,
'cx_mch_id' => $this->cx_mch_id,
])
->select('post_id,value as cat_id');
$query = Post::find()->alias('p')
->leftJoin(['rt' => RichText::tableName()],'rt.id=p.rich_text_id')
->leftJoin(['pm' => $cat_query], 'pm.post_id=p.id')
->where([
'p.cx_mch_id' => $this->cx_mch_id,
'p.is_delete' => 0,
'p.scene' => $this->scene,
])
->andFilterWhere([
'OR',
['LIKE', 'rt.title', $this->keywords],
['LIKE', 'rt.author', $this->keywords],
['LIKE', 'rt.summary', $this->keywords],
])
->andFilterWhere([
'p.is_top' => $this->is_top,
'p.status' => $this->status,
'p.id' => $this->post_id,
'p.url_alias' => $this->url_alias,
'pm.cat_id' => $this->cat_id
])
->select('rt.author,rt.title,rt.summary,rt.content,rt.cover_url,p.id,p.rich_text_id,p.sort,p.is_top,p.status,p.created_at,p.published_at,p.viewed_num,p.allow_read');
$count_query = clone $query;
$count = $count_query->count();
$pagination = new Pagination(['pageSize' => $this->limit, 'totalCount' => $count, 'page' => $this->page - 1]);
$list = $query->offset($pagination->offset)->limit($pagination->limit)->orderBy(['p.sort' => SORT_ASC, 'p.created_at' => SORT_DESC])->asArray()->all();
foreach ($list as $index => $item){
$item['created_at_cn'] = date('Y-m-d H:i:s',$item['created_at']);
$item['published_at_cn'] = date('Y-m-d H:i:s',$item['published_at']);
$item['status_cn'] = Post::getStatus($item['status']);
$item['allow_read_cn'] = $item['allow_read'] == 1 ? '开放阅读' : '密码访问';
$item['cat_list'] = $this->getCatList($item['id']);
if($this->scenario == 'detail')
$item['content'] = SiteHelper::repairContent($item['content']);
$list[$index] = $item;
}
//是否已经全部加载
$end_flag = $this->page > $pagination->pageCount ? true : false;
return [
'code' => 0,
'msg' => 'ok',
'data' => $list,
'count' => $count,
'page_size' => $this->limit,
'page_count' => $pagination->pageCount,
'page_no' => $this->page,
'end_flag' => $end_flag
];
}
private function getCatList($post_id)
{
$list = PostMeta::find()->alias('pm')
->leftJoin(['c' => Cat::tableName()],'c.id=pm.value')
->where([
'pm.is_delete' => 0,
'pm.type' => PostMeta::TYPE_CAT,
'pm.post_id' => $post_id,
'c.is_delete' => 0,
'c.type' => Cat::TYPE_POST_DEFAULT,
])
->select('c.name,c.id as cat_id')->asArray()->all();
return $list;
}
}