fastadmin/application/admin/model/Channel.php

115 lines
2.7 KiB
PHP

<?php
namespace app\admin\model;
use think\Model;
use think\Session;
class Channel extends Model
{
// 表名
protected $name = 'channel';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
// 追加属性
protected $append = [
];
/**
* 根据后台入口和管理员等级来自动读取
* 1, 如果是从站点后台登录,所有管理员只能读取本站点信息
* 2, 如果是从总后台登录,管理员读取属于自己的站点的信息,超级管理员无限制
*/
protected function base($query)
{
if (Session::has("user_site_id")) {
$query->where(['site_id'=> Session::get("user_site_id")]);
}
}
public function Sites()
{
return $this->belongsTo('Sites', 'site_id')->field('id,name');
}
public function Archives()
{
//return $this->belongsTo('Archives', 'id', 'channel_id')->field('channel_id,count(1) as items');
return $this->hasOne('Archives','channel_id','id')->field('channel_id,count(1) as items');
}
/**
* 读取分类类型
* @return array
*/
public static function getTypeList()
{
//$typelist = config('site.categorytype');
return ['channel'=>__('Channel'), 'list'=>__('List'), 'redirect'=>__('Redirect')];
}
public static function getFlagList()
{
return ['hot' => __('Hot'), 'recommend' => __('Recommend')];
}
public function getFlagTextAttr($value, $data)
{
if(!isset($data['flag'])) return '';
$value = $value ? $value : $data['flag'];
$valueArr = explode(',', $value);
$list = $this->getFlagList();
return implode(',', array_intersect_key($list, array_flip($valueArr)));
}
/**
* 读取分类列表
* @param string $type 指定类型
* @param string $status 指定状态
* @return array
*/
public static function getChannelArray($type = NULL, $status = NULL)
{
$list = collection(self::where(function($query) use($type, $status) {
if (!is_null($type))
{
$query->where('type', '=', $type);
}
if (!is_null($status))
{
$query->where('status', '=', $status);
}
})->order('weigh', 'desc')->select())->toArray();
return $list;
}
/*
protected static function init()
{
self::afterInsert(function ($row) {
$pk = $row->getPk();
$row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
});
}
*/
}