|
|
@@ -8,144 +8,106 @@ use think\Model;
|
|
|
* 教育模型
|
|
|
*/
|
|
|
class EducationModel extends Model
|
|
|
- {
|
|
|
- // 设置当前模型对应的数据表名称
|
|
|
-// protected $name = 'table_education_cate';
|
|
|
-
|
|
|
- // 设置当前模型的数据库连接
|
|
|
-// protected $connection = 'infant_service';
|
|
|
+{
|
|
|
+ protected $table = 'education_cate';
|
|
|
|
|
|
- /**
|
|
|
- * 当前所在模块的CSS样式类
|
|
|
- */
|
|
|
- const CURS_TYLE = 'current';
|
|
|
- const CATE_TREE_PREFIX = '├──';
|
|
|
- const CATE_TABLE_NAME = 'table_education_cate';
|
|
|
|
|
|
- public static function getCateInstance()
|
|
|
+ public static function getInstance()
|
|
|
{
|
|
|
- return new self(strtr(self::CATE_TABLE_NAME, ['wp_' => '']));
|
|
|
+ return new self;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 获取导航栏
|
|
|
+ * 获取所有分类
|
|
|
*
|
|
|
- * @return array
|
|
|
+ * @return mixed
|
|
|
*/
|
|
|
- public static function getNav(array $params, $isRoot = false)
|
|
|
+ public function getCate()
|
|
|
{
|
|
|
- $nav = [];
|
|
|
- foreach (self::getModules() as $module => $desc) {
|
|
|
- if (($desc[1] && $isRoot) || !$desc[1]) {
|
|
|
- $nav[$module] = [
|
|
|
- 'title' => $desc[0],
|
|
|
- 'url' => url($module, $params),
|
|
|
- ];
|
|
|
+ $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 $tree;
|
|
|
}
|
|
|
- $nav[ACTION_NAME]['class'] = self::CURS_TYLE;
|
|
|
- return $nav;
|
|
|
- }
|
|
|
+
|
|
|
+// public function getGrList($userId)
|
|
|
+// {
|
|
|
+// return $this->where('user', $userId)->order('create_time', 'desc')->select()->toArray();
|
|
|
+// }
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
- * 获取所有模块
|
|
|
- * 按理说这里应该从数据库的配置表来动态取,但是扩展的可能性不大,所以直接写死了
|
|
|
+ * 获取所有文章列表
|
|
|
*
|
|
|
+ * @param int|null $excludeCourseId 需要排除的课程 ID
|
|
|
* @return array
|
|
|
*/
|
|
|
- public static function getModules()
|
|
|
+ public function getCourseList($excludeCourseId = null)
|
|
|
{
|
|
|
- return [
|
|
|
- 'catelist' => ['分类管理', 1],
|
|
|
- 'lists' => ['课程列表', 0],
|
|
|
- 'grlists' => ['分组列表', 0],
|
|
|
- 'aboutme' => ['讲师介绍', 0],
|
|
|
- ];
|
|
|
- }
|
|
|
+ $query = $this->where(['status' => 'Y']);
|
|
|
|
|
|
- /**
|
|
|
- * 获取所有分类
|
|
|
- *
|
|
|
- * @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']];
|
|
|
+ if ($excludeCourseId !== null) {
|
|
|
+ $query->where('id', '<>', $excludeCourseId);
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- return $this->formatCateTree($tree);
|
|
|
+ $query->order('sort', 'asc');
|
|
|
+
|
|
|
+ return $query->select()->toArray();
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
* 获取顶级类
|
|
|
*/
|
|
|
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;
|
|
|
- }
|
|
|
+ $items = $this->where(['status' => 'Y', 'pid' => 0])
|
|
|
+ ->order('sort', 'asc')
|
|
|
+ ->field('id,pid,name,sort')
|
|
|
+ ->select()
|
|
|
+ ->toArray();
|
|
|
|
|
|
- public function getSubcatesByTopCate($cates, &$subcates = array())
|
|
|
- {
|
|
|
- foreach ($cates as $cate) {
|
|
|
- $subcates[] = $cate['id'];
|
|
|
- if (isset($cate['son'])) {
|
|
|
- $this->getSubcatesByTopCate($cate['son'], $subcates);
|
|
|
- }
|
|
|
- }
|
|
|
- return $subcates;
|
|
|
+ return $items;
|
|
|
}
|
|
|
|
|
|
- public static function getPureCateName($dirtyName)
|
|
|
- {
|
|
|
- return strtr($dirtyName, [self::CATE_TREE_PREFIX => '']);
|
|
|
- }
|
|
|
|
|
|
/**
|
|
|
- * 格式化分类树
|
|
|
- *
|
|
|
- * @param $tree
|
|
|
- * @return mixed
|
|
|
+ *获取指定ID列表
|
|
|
*/
|
|
|
- public function formatCateTree($tree, $parents = '')
|
|
|
+ public function getSubcatesByTopCate($cates, &$subcates = array())
|
|
|
{
|
|
|
- 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;
|
|
|
+ foreach ($cates as $cate) {
|
|
|
+ $subcates[] = $cate['id'];
|
|
|
+ if (isset($cate['son'])) {
|
|
|
+ $this->getSubcatesByTopCate($cate['son'], $subcates);
|
|
|
+ }
|
|
|
}
|
|
|
+ return $subcates;
|
|
|
}
|
|
|
|
|
|
- return $tem;
|
|
|
- }
|
|
|
+// /**
|
|
|
+// * 去除分类名称中的前缀
|
|
|
+// *
|
|
|
+// * @param string $dirtyName 脏分类名称
|
|
|
+// * @return string
|
|
|
+// */
|
|
|
+// public static function getPureCateName($dirtyName)
|
|
|
+// {
|
|
|
+// return strtr($dirtyName, [self::CATE_TREE_PREFIX => '']);
|
|
|
+// }
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* 修改分类
|
|
|
@@ -170,11 +132,12 @@ class EducationModel extends Model
|
|
|
*/
|
|
|
public function addCate(array $data)
|
|
|
{
|
|
|
- $data['status'] = 'Y';
|
|
|
- if ($this->allowField(true)->save($data)) {
|
|
|
- return $this->id;
|
|
|
- }
|
|
|
- return false;
|
|
|
+ $data['status'] = 'Y';
|
|
|
+ if ($this->save($data)) {
|
|
|
+ return $this->id;
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|