| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- <?php
- namespace app\model\api;
- use think\Model;
- /**
- * 教育模型
- */
- class EducationModel extends Model
- {
- // 设置当前模型对应的数据表名称
- protected $name = 'education';
- // 设置当前模型的数据库连接
- protected $connection = 'database';
- /**
- * 当前所在模块的CSS样式类
- */
- const CURS_TYLE = 'current';
- const CATE_TREE_PREFIX = '├──';
- const CATE_TABLE_NAME = 'table_education_cate';
- public static function getCateInstance()
- {
- return new self(strtr(self::CATE_TABLE_NAME, ['wp_' => '']));
- }
- /**
- * 获取导航栏
- *
- * @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')
- ->getField('id,pid,name,sort');
- $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')
- ->getField('id,pid,name,sort');
- 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);
- }
- }
|