model = new \app\admin\model\course\ColumnBind; $this->courseModel = new \app\admin\model\course\Course; } /** * 查看 */ public function index() { //当前是否为关联查询 $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(['bindcourse']) ->where($where) ->order("column_bind.sort asc, column_bind.createtime {$order}") ->select(); if(!empty($list)){ foreach ($list as $index => &$item){ if($item['type'] == 1){ if($item['bindcourse']['id']){ $item['bindcourse'] = $this->courseModel->structrue($item['bindcourse'],'decode'); }else{ unset($list[$index]); } } } //重新给数组排序 $list = array_values($list); } return $this->success("获取成功",$list); } /** * 编辑 * @param $ids * @return string * @throws DbException * @throws \think\Exception */ public function edit($ids = null) { $row = $this->model->get($ids); if (!$row) { $this->error(__('No Results were found')); } $adminIds = $this->getDataLimitAdminIds(); if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) { $this->error(__('You have no permission')); } $params = $this->request->post('row/a'); if (empty($params)) { $this->error(__('Parameter %s can not be empty', '')); } if(isset($params['dir']) && $row['dir']!=$params['dir']){ $params['sort'] = $this->model->defineSort($row['column_id'],$params['dir']); } $params = $this->preExcludeFields($params); $result = false; Db::startTrans(); try { //是否采用模型验证 if ($this->modelValidate) { $name = str_replace("\\model\\", "\\validate\\", get_class($this->model)); $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate; $row->validateFailException()->validate($validate); } $result = $row->allowField(true)->save($params); Db::commit(); } catch (ValidateException|PDOException|Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if (false === $result) { $this->error(__('No rows were updated')); } $this->success("提交成功"); } /** * 分组绑定内容 * @param $groupId * @param $courseIds * @return mixed */ public function columnBindCourse($columnId=null,$courseIds=null,$type=null,$title=null,$p_id=null){ $courseIds = $courseIds ?: $this->request->post("course_ids/a"); $columnId = $columnId ?: $this->request->post("column_id"); $type = $type ?: $this->request->post("type",1); $title = $title ?: $this->request->post("title",''); $p_id = $p_id ?: $this->request->post("p_id",0); $this->model->setColumnBindCourse($columnId,$courseIds,$p_id,$type,$title); return $this->success("操作成功"); } /** * 获取专栏绑定的内容ID * @param $columnId * @return mixed */ public function getBindCourseId($id = null){ $id = $id ?: $this->request->post("id/a"); $ids = $this->model->getBindCourseId($id); return $this->success("获取成功",$ids); } /** * 切换上下架 * @param $ids * @param $status * @param $timing 是否为定时切换事件 * @return void */ public function tryStatus($ids=null,$status=1){ 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')); } $status = $status ?: $this->request->post("status"); $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) { $row = [ 'try'=>$status ]; $this->model->where([ 'course_id'=>$item['course_id'] ])->update($row); $count++; } Db::commit(); } catch (PDOException|Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if ($count) { $this->success("操作成功"); } $this->error(__('未操作任何数据')); } /** * 取消绑定 * @param $groupId * @param $courseIds * @return void */ public function cancelBind($ids){ if (false === $this->request->isPost()) { $this->error(__("Invalid parameters")); } $ids = $ids ?: $this->request->post("ids/a"); //是否同步从课程列表中删除关联的课程(1=同时删除课程,0=仅从专栏目录中移除) $deleteCourse = (int)$this->request->post("delete_course", 0); if (empty($ids)) { $this->error(__('Parameter %s can not be empty', 'ids')); } $adminIds = $this->getDataLimitAdminIds(); if (is_array($adminIds)) { $this->model->where($this->dataLimitField, 'in', $adminIds); } $list = $this->model->where('id','in', $ids)->whereOr('p_id','in',$ids)->select(); //收集需要同步删除的课程ID(仅对实际绑定课程的目录项生效,目录节点 type!=1 跳过) $courseIdsToDelete = []; if ($deleteCourse && !empty($list)) { foreach ($list as $item) { if (isset($item['type']) && $item['type'] == 1 && !empty($item['course_id'])) { $courseIdsToDelete[] = (int)$item['course_id']; } } $courseIdsToDelete = array_values(array_unique($courseIdsToDelete)); } $count = 0; Db::startTrans(); try { foreach ($list as $item) { $count += $item->delete(); } //同步删除课程 if ($deleteCourse && !empty($courseIdsToDelete)) { $courseModel = new \app\admin\model\course\Course(); $courseList = $courseModel->where('id', 'in', $courseIdsToDelete)->select(); foreach ($courseList as $courseItem) { $courseItem->delete(); } } Db::commit(); } catch (PDOException|Exception $e) { Db::rollback(); $this->error($e->getMessage()); } //清理课程相关缓存 \app\common\library\cache\Clear::course(); if ($count) { $this->success("操作成功"); } $this->error(__('未移除任何数据')); } /** * 设置排序 * @param $id * @param $type * @return mixed */ public function setSort($id,$type = 'up',$p_id = 0) { $self = $this->model->get($id); if(!$self){ $this->error(__('No Results were found')); } $direction = $type == 'up' ? '<' : '>'; $sort = $type == 'up' ? 'desc' : 'asc'; $brother = $this->model->where(['column_id'=>$self['column_id'],'p_id'=>$p_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 $sortList 排序数据数组,每项包含 id 和 sort * @return mixed */ public function batchSort() { $sortList = $this->request->post("sort_list/a"); if (empty($sortList)) { $this->error(__('Parameter %s can not be empty', 'sort_list')); } $count = 0; Db::startTrans(); try { foreach ($sortList as $item) { if (!isset($item['id']) || !isset($item['sort'])) { continue; } $this->model->where([ 'id' => $item['id'] ])->update([ 'sort' => $item['sort'] ]); $count++; } Db::commit(); } catch (PDOException|Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if ($count) { return $this->success("操作成功"); } $this->error(__('未操作任何数据')); } }