''])); } /** * 获取导航栏 * * @return array */ public static function getNav(array $params, $isRoot = false) { $nav = []; foreach (self::getModules() as $module => $desc) { if (($desc[1] && $isRoot) || !$desc[1]) { $nav[$module] = [ 'title' => $desc[0], 'url' => url($module, $params), ]; } } $nav[ACTION_NAME]['class'] = self::CURS_TYLE; return $nav; } /** * 获取所有模块 * 按理说这里应该从数据库的配置表来动态取,但是扩展的可能性不大,所以直接写死了 * * @return array */ public static function getModules() { return [ 'catelist' => ['分类管理', 1], 'lists' => ['课程列表', 0], 'grlists' => ['分组列表', 0], 'aboutme' => ['讲师介绍', 0], ]; } /** * 获取所有分类 * * @return mixed */ public function getCate() { $items = $this->table(self::CATE_TABLE_NAME) ->where(['status' => 'Y']) ->order('sort', 'asc') ->column('id,pid,name,sort','id'); $tree = array(); //格式化好的树 foreach ($items as $key => $item) { $items[$key]['oriName'] = $item['name']; if (isset ($items [$item ['pid']])) { $items [$item ['pid']] ['son'] [$item ['id']] = &$items [$item ['id']]; } else { $tree [$item ['id']] = &$items [$item ['id']]; } } return $this->formatCateTree($tree); } /** * 获取顶级类 */ public function getTopCate() { $items = $this->table(self::CATE_TABLE_NAME) ->where(['status' => 'Y', 'pid' => 0]) ->order('sort', 'asc') ->column('id,pid,name,sort','id'); return $items; } public function getSubcatesByTopCate($cates, &$subcates = array()) { foreach ($cates as $cate) { $subcates[] = $cate['id']; if (isset($cate['son'])) { $this->getSubcatesByTopCate($cate['son'], $subcates); } } return $subcates; } public static function getPureCateName($dirtyName) { return strtr($dirtyName, [self::CATE_TREE_PREFIX => '']); } /** * 格式化分类树 * * @param $tree * @return mixed */ public function formatCateTree($tree, $parents = '') { static $tem = array(); static $prefix = 0; foreach ($tree as $key => $val) { if (!isset($tem [$val ['id']])) $tem [$val ['id']] = $val; if ($prefix) { $tem [$val ['id']]['name'] = str_repeat(self::CATE_TREE_PREFIX, $prefix) . $tem [$val ['id']]['name']; $tem [$val ['id']]['parents'] = ($parents ? $parents . '->' : '') . $val['name']; } if (isset($val ['son']) && is_array($val ['son'])) { $prefix += 1; $this->formatCateTree($val ['son'], ($parents ? $parents . '->' : '') . $val['name']); $prefix -= 1; } } return $tem; } /** * 修改分类 * * @param array $data 修改的信息 * @param $id 类目ID * @return bool */ public function editCate(array $data, $id) { if ($this->allowField(true)->save($data, ['id' => $id])) { return true; } return false; } /** * 增加一个分类 * * @param array $data * @return bool|mixed */ public function addCate(array $data) { $data['status'] = 'Y'; if ($this->allowField(true)->save($data)) { return $this->id; } return false; } /** * 删除一个分类 * * @param array $data * @return bool|mixed */ public function delCate($id) { return $this->editCate(['id' => $id, 'status' => 'N'], $id); } public function getCourseListByCates($cates = array(), $pageSize = 3, $page = 1, $userInfo=null) { $firstRow = (($page < 1) ? 0 : ($page - 1)) * $pageSize; $where = array( 'course_audit' => 1, 'course_status' => 1, 'course_only_for_employee' => 0, 'course_cate_id' => array('in', $cates) ); $fields = 'course_id, course_cate_id, course_topic, course_intro, course_cover, course_speaker_intro, course_play_count, '; if ($userInfo){ // 假如当前用户是从业人员,那就删除条件 if ($userInfo["is_employee"]){ unset($where["course_only_for_employee"]); } // 根据用户类型选择不同字段 switch ($userInfo["user_type"]){ case "1": $fields .= "course_price_2 as course_price"; break; case "2": $fields .= "course_price_3 as course_price"; break; default : $fields .= "course_price"; } }else{ $fields .= "course_price"; } $result = array(); if ($cates) { $result = $this->where( $where ) ->order('course_id desc') ->limit($firstRow, $pageSize) ->column($fields); } return (array)$result; } public function getCourseListLikeTopic($topic, $pageSize = 50, $page = 1) { $firstRow = (($page < 1) ? 0 : ($page - 1)) * $pageSize; $result = array(); if ($topic) { $result = $this->where( array( 'course_audit' => 1, 'course_status' => 1, 'course_topic' => array('like', '%' . $topic . '%') ) ) ->order('course_id desc') ->limit($firstRow, $pageSize) ->column( 'course_id, course_cate_id, course_topic, course_price, course_intro, course_cover, course_speaker_intro, course_play_count, gr_id' ); } return (array)$result; } }