Files

180 lines
4.5 KiB
PHP

<?php
namespace app\admin\controller\course;
use app\common\controller\Backend;
use think\Db;
/**
* 内容分组
*
* @icon fa fa-circle-o
*/
class GroupBind extends Backend
{
protected $modelValidate = false;
/**
* Group模型对象
* @var \app\admin\model\course\Group
*/
protected $model = null;
public function _initialize()
{
parent::_initialize();
$this->model = new \app\admin\model\course\GroupBind();
$this->courseModel = new \app\admin\model\course\Course;
}
/**
* 查看
*/
public function index($json = true)
{
//当前是否为关联查询
$this->relationSearch = true;
//设置过滤方法
$this->request->filter(['strip_tags', 'trim']);
//如果发送的来源是Selectpage,则转发到Selectpage
if ($this->request->request('keyField')) {
return $this->selectpage();
}
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
$list = $this->model
->with(['course'])
->where($where)
->order($sort, $order)
->limit($limit)
->paginate($limit);
$rows = $list->toArray();
if(!empty($rows['data'])){
foreach ($rows['data'] as &$item){
$item['course'] = $this->courseModel->structrue($item['course'],'decode');
}
}
$result = array("total" => $rows['total'], "rows" => $rows['data']);
if($json){
return $this->success("获取成功",$result);
}else{
return $result;
}
}
/**
* 设置排序
* @param $id
* @param $type
* @return mixed
*/
public function setSort($id,$type = 'up')
{
$self = $this->model->get($id);
if(!$self){
$this->error(__('No Results were found'));
}
$direction = $type == 'up' ? '>' : '<';
$sort = $type == 'up' ? 'asc' : 'desc';
$brother = $this->model->where(['group_id'=>$self['group_id']])
->where('sort',$direction,$self['sort'])
->order('sort',$sort)
->find();
if(!$brother){
$this->error(__('无继续调节空间'));
}
$this->model->where([
'id'=>$self['id']
])->update([
'sort'=>$brother['sort']
]);
$this->model->where([
'id'=>$brother['id']
])->update([
'sort'=>$self['sort']
]);
return $this->success("操作成功");
}
/**
* 删除
*
* @param $ids
* @return void
* @throws DbException
* @throws DataNotFoundException
* @throws ModelNotFoundException
*/
public function del($ids = null)
{
if (false === $this->request->isPost()) {
$this->error(__("Invalid parameters"));
}
$ids = $ids ?: $this->request->post("ids");
if (empty($ids)) {
$this->error(__('Parameter %s can not be empty', 'ids'));
}
$pk = $this->model->getPk();
$adminIds = $this->getDataLimitAdminIds();
if (is_array($adminIds)) {
$this->model->where($this->dataLimitField, 'in', $adminIds);
}
$list = $this->model->where($pk, 'in', $ids)->select();
$count = 0;
Db::startTrans();
try {
foreach ($list as $item) {
$count += $item->delete();
}
Db::commit();
} catch (PDOException|Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if ($count) {
$this->success("操作成功");
}
$this->error(__('No rows were deleted'));
}
/**
* 分组绑定内容
* @param $groupId
* @param $courseIds
* @return mixed
*/
public function groupBindCourse($groupId=null,$courseIds=null){
$courseIds = $courseIds ?: $this->request->post("course_ids/a");
$groupId = $groupId ?: $this->request->post("group_id");
$this->model->setGroupBindCourse($groupId,$courseIds);
return $this->success("操作成功");
}
/**
* 获取分组绑定的内容ID
* @param $id 分组ID
* @return mixed
*/
public function getBindCourseIds($id)
{
$id = $id ?: $this->request->post("id");
$row = $this->model->getGroupBindCourse($id);
$this->success("操作成功",$row);
}
}