EducationModel.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. namespace app\model\api;
  3. use think\Model;
  4. /**
  5. * 教育模型
  6. */
  7. class EducationModel extends Model
  8. {
  9. // 设置当前模型对应的数据表名称
  10. protected $name = 'education';
  11. // 设置当前模型的数据库连接
  12. protected $connection = 'database';
  13. /**
  14. * 当前所在模块的CSS样式类
  15. */
  16. const CURS_TYLE = 'current';
  17. const CATE_TREE_PREFIX = '├──';
  18. const CATE_TABLE_NAME = 'table_education_cate';
  19. public static function getCateInstance()
  20. {
  21. return new self(strtr(self::CATE_TABLE_NAME, ['wp_' => '']));
  22. }
  23. /**
  24. * 获取导航栏
  25. *
  26. * @return array
  27. */
  28. public static function getNav(array $params, $isRoot = false)
  29. {
  30. $nav = [];
  31. foreach (self::getModules() as $module => $desc) {
  32. if (($desc[1] && $isRoot) || !$desc[1]) {
  33. $nav[$module] = [
  34. 'title' => $desc[0],
  35. 'url' => url($module, $params),
  36. ];
  37. }
  38. }
  39. $nav[ACTION_NAME]['class'] = self::CURS_TYLE;
  40. return $nav;
  41. }
  42. /**
  43. * 获取所有模块
  44. * 按理说这里应该从数据库的配置表来动态取,但是扩展的可能性不大,所以直接写死了
  45. *
  46. * @return array
  47. */
  48. public static function getModules()
  49. {
  50. return [
  51. 'catelist' => ['分类管理', 1],
  52. 'lists' => ['课程列表', 0],
  53. 'grlists' => ['分组列表', 0],
  54. 'aboutme' => ['讲师介绍', 0],
  55. ];
  56. }
  57. /**
  58. * 获取所有分类
  59. *
  60. * @return mixed
  61. */
  62. public function getCate()
  63. {
  64. $items = $this->table(self::CATE_TABLE_NAME)
  65. ->where(['status' => 'Y'])
  66. ->order('sort', 'asc')
  67. ->getField('id,pid,name,sort');
  68. $tree = array(); //格式化好的树
  69. foreach ($items as $key => $item) {
  70. $items[$key]['oriName'] = $item['name'];
  71. if (isset ($items [$item ['pid']])) {
  72. $items [$item ['pid']] ['son'] [$item ['id']] = &$items [$item ['id']];
  73. } else {
  74. $tree [$item ['id']] = &$items [$item ['id']];
  75. }
  76. }
  77. return $this->formatCateTree($tree);
  78. }
  79. /**
  80. * 获取顶级类
  81. */
  82. public function getTopCate()
  83. {
  84. $items = $this->table(self::CATE_TABLE_NAME)
  85. ->where(['status' => 'Y', 'pid' => 0])
  86. ->order('sort', 'asc')
  87. ->getField('id,pid,name,sort');
  88. return $items;
  89. }
  90. public function getSubcatesByTopCate($cates, &$subcates = array())
  91. {
  92. foreach ($cates as $cate) {
  93. $subcates[] = $cate['id'];
  94. if (isset($cate['son'])) {
  95. $this->getSubcatesByTopCate($cate['son'], $subcates);
  96. }
  97. }
  98. return $subcates;
  99. }
  100. public static function getPureCateName($dirtyName)
  101. {
  102. return strtr($dirtyName, [self::CATE_TREE_PREFIX => '']);
  103. }
  104. /**
  105. * 格式化分类树
  106. *
  107. * @param $tree
  108. * @return mixed
  109. */
  110. public function formatCateTree($tree, $parents = '')
  111. {
  112. static $tem = array();
  113. static $prefix = 0;
  114. foreach ($tree as $key => $val) {
  115. if (!isset($tem [$val ['id']]))
  116. $tem [$val ['id']] = $val;
  117. if ($prefix) {
  118. $tem [$val ['id']]['name'] = str_repeat(self::CATE_TREE_PREFIX, $prefix) . $tem [$val ['id']]['name'];
  119. $tem [$val ['id']]['parents'] = ($parents ? $parents . '->' : '') . $val['name'];
  120. }
  121. if (isset($val ['son']) && is_array($val ['son'])) {
  122. $prefix += 1;
  123. $this->formatCateTree($val ['son'], ($parents ? $parents . '->' : '') . $val['name']);
  124. $prefix -= 1;
  125. }
  126. }
  127. return $tem;
  128. }
  129. /**
  130. * 修改分类
  131. *
  132. * @param array $data 修改的信息
  133. * @param $id 类目ID
  134. * @return bool
  135. */
  136. public function editCate(array $data, $id)
  137. {
  138. if ($this->allowField(true)->save($data, ['id' => $id])) {
  139. return true;
  140. }
  141. return false;
  142. }
  143. /**
  144. * 增加一个分类
  145. *
  146. * @param array $data
  147. * @return bool|mixed
  148. */
  149. public function addCate(array $data)
  150. {
  151. $data['status'] = 'Y';
  152. if ($this->allowField(true)->save($data)) {
  153. return $this->id;
  154. }
  155. return false;
  156. }
  157. /**
  158. * 删除一个分类
  159. *
  160. * @param array $data
  161. * @return bool|mixed
  162. */
  163. public function delCate($id)
  164. {
  165. return $this->editCate(['id' => $id, 'status' => 'N'], $id);
  166. }
  167. }