DirectoryController.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. namespace addons\Course\backend\controllers;
  3. use addons\Course\common\models\AddonsCourseChapter;
  4. use addons\Course\common\models\AddonsCourseDirectorys;
  5. use common\enums\StatusEnum;
  6. /**
  7. * 默认控制器
  8. *
  9. * Class DefaultController
  10. * @package addons\Course\backend\controllers
  11. */
  12. class DirectoryController extends BaseController
  13. {
  14. public function actionList()
  15. {
  16. if (!\Yii::$app->request->isGet) {
  17. return $this->error('请求方式错误');
  18. }
  19. $mall_id = $this->mall_id;
  20. $course_id = \Yii::$app->request->get('course_id');
  21. // 列表形式,1:普通列表,2:树形列表
  22. $list_type = \Yii::$app->request->get('list_type', 1);
  23. if (empty($course_id) || empty($list_type)) {
  24. return $this->error('参数错误');
  25. }
  26. if (1 == $list_type) {
  27. $list = AddonsCourseDirectorys::getList($mall_id, $course_id, 'id,name,course_id,parent_id,sort');
  28. } elseif (2 == $list_type) {
  29. $list = AddonsCourseDirectorys::getDirTreeList($mall_id, $course_id, 'id,name,course_id,parent_id,sort');
  30. if (!empty($list)) {
  31. foreach ($list['tree'] as $lk => $lv) {
  32. $list['tree'][$lk]['label'] = $lv['name'];
  33. unset($list['tree'][$lk]['name']);
  34. if (!empty($lv['sub_dir'])) {
  35. foreach ($lv['sub_dir'] as $sk => $sv) {
  36. $list['tree'][$lk]['children'][] = $sv;
  37. $list['tree'][$lk]['children'][$sk]['label'] = $sv['name'];
  38. unset($list['tree'][$lk]['sub_dir'], $list['tree'][$lk]['children'][$sk]['name']);
  39. }
  40. }
  41. }
  42. }
  43. }
  44. return $this->success('success', $list);
  45. }
  46. /**
  47. * @name:
  48. * @msg: 添加目录
  49. * @param {int} course_id 课程 id
  50. * @param {int} parent_id 父级目录 id
  51. * @param {string} name 目录名称
  52. * @return {*}
  53. */
  54. public function actionAddDir()
  55. {
  56. if (!\Yii::$app->request->isPost) {
  57. return $this->error('请求方式错误');
  58. }
  59. $post = \Yii::$app->request->post();
  60. if (empty($post['dir_type']) || !in_array($post['dir_type'], [1, 2])) {
  61. return $this->error('参数错误');
  62. }
  63. if (1 == $post['dir_type']) {
  64. // 添加一级目录
  65. $rules = [
  66. [['course_id', 'name'], 'required'],
  67. ['course_id', 'integer'],
  68. ['name', 'string', 'max' => 50],
  69. ];
  70. } else {
  71. // 添加子级目录
  72. $rules = [
  73. [['course_id', 'name', 'parent_id'], 'required'],
  74. [['course_id', 'parent_id'], 'integer'],
  75. ['name', 'string', 'max' => 50],
  76. ['level', 'safe'],
  77. ];
  78. $post['level'] = 2;
  79. }
  80. $res = \Yii::$app->services->validate->validate($post, $rules);
  81. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  82. if (empty($post['parent_id'])) {
  83. $parent_dir_id = 0;
  84. $level = 1;
  85. } else {
  86. $parent_dir_id = $post['parent_id'];
  87. $level = 2;
  88. }
  89. $post['sort'] = AddonsCourseDirectorys::getLastSort($this->mall_id, $post['course_id'], $parent_dir_id ?? 0, $level);
  90. // dd($post, $post['sort'], $parent_dir_id);
  91. $res = AddonsCourseDirectorys::setData($this->mall_id, $post, 'addSubDir');
  92. if (false === $res) {
  93. return $this->error(AddonsCourseDirectorys::getStaticError());
  94. }
  95. return $this->success('添加成功');
  96. }
  97. /**
  98. * @name:
  99. * @msg: 目录改名称
  100. * @param {*}
  101. * @return {*}
  102. */
  103. public function actionRename()
  104. {
  105. if (!\Yii::$app->request->isPost) {
  106. return $this->error('请求方式错误');
  107. }
  108. $id = \Yii::$app->request->post('id');
  109. $name = \Yii::$app->request->post('name');
  110. $course_id = \Yii::$app->request->post('course_id');
  111. if (empty($id) || empty($name) || empty($course_id)) {
  112. return $this->error('参数错误');
  113. }
  114. $dir_names = AddonsCourseDirectorys::find()
  115. ->select('name')
  116. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'course_id' => $course_id])
  117. ->asArray()
  118. ->column();
  119. if (in_array($name, $dir_names)) {
  120. return $this->error('目录名称已经存在');
  121. }
  122. $params = [
  123. 'id' => $id,
  124. 'name' => $name,
  125. ];
  126. $res = AddonsCourseDirectorys::setData($this->mall_id, $params, 'rename');
  127. if (false === $res) {
  128. return $this->error(AddonsCourseDirectorys::getStaticError());
  129. }
  130. return $this->success('添加成功');
  131. }
  132. /**
  133. * @name:
  134. * @msg: 目录改变排序,只能相邻两个互换排序,不能拖动跳过多个进行排序
  135. * @param {int} id 目录 id
  136. * @param {int} type 操作类型,1:上移,2:下移
  137. * @return {*}
  138. */
  139. public function actionSortDir()
  140. {
  141. if (!\Yii::$app->request->isGet) {
  142. return $this->error('请求方式错误');
  143. }
  144. $id = \Yii::$app->request->get('id');
  145. $type = \Yii::$app->request->get('type', 1);
  146. if (empty($id) || (empty($type) && !in_array($type, [1, 2]))) {
  147. return $this->error('参数错误');
  148. }
  149. $res = AddonsCourseDirectorys::switchDirSort($id, $this->mall_id, $type);
  150. if (false === $res) {
  151. return $this->error('更改排序失败:' . AddonsCourseDirectorys::getStaticError());
  152. } else {
  153. return $this->success('更改排序成功');
  154. }
  155. }
  156. /**
  157. * @name:
  158. * @msg: 删除目录,目录下有章节时不能删除,目录下有子级目录时不能删除
  159. * @param {int} id 目录 id
  160. * @return {*}
  161. */
  162. public function actionDelete()
  163. {
  164. if (!\Yii::$app->request->isPost) {
  165. return $this->error('请求方式错误');
  166. }
  167. $id = \Yii::$app->request->post('id');
  168. if (empty($id)) {
  169. return $this->error('参数错误');
  170. }
  171. $dirs = AddonsCourseDirectorys::findOne(['mall_id' => $this->mall_id, 'parent_id' => $id, 'status' => StatusEnum::ENABLED]);
  172. if (!empty($dirs)) {
  173. return $this->error('该目录下尚有子级目录,不能删除');
  174. }
  175. $chapter = AddonsCourseChapter::findOne(['mall_id' => $this->mall_id, 'dir_id' => $id, 'status' => StatusEnum::ENABLED]);
  176. if (!empty($chapter)) {
  177. return $this->error('该目录下尚有课程章节,不能删除');
  178. }
  179. $res = AddonsCourseDirectorys::findOne(['mall_id' => $this->mall_id, 'id' => $id, 'status' => StatusEnum::ENABLED])->delete();
  180. if ($res) {
  181. return $this->success('删除成功');
  182. } else {
  183. return $this->error('删除失败');
  184. }
  185. }
  186. /**
  187. * @name:
  188. * @msg: 移动一个或者多个章节到一个目录里,不能跨课程移动!!!!!!
  189. * @param {integer} $course_id 课程 id
  190. * @param {integer|array} $chapter_ids 一个或者多个章节 id
  191. * @param {integer} $target_dir_id 目标目录 id,只能是一个
  192. * @return {*}
  193. */
  194. public function actionMoveChapters()
  195. {
  196. if (!\Yii::$app->request->isPost || !\Yii::$app->request->isAjax) {
  197. return $this->error('请求方式错误');
  198. }
  199. $course_id = \Yii::$app->request->post('course_id');
  200. // 章节id,一个或者多个,如果是多个放数组内
  201. $chapter_ids = \Yii::$app->request->post('chapter_id');
  202. // 目标目录 id,只能是移动到一个目录里
  203. $target_dir_id = \Yii::$app->request->post('target_dir_id');
  204. if (empty($course_id) || empty($chapter_ids) || (empty($target_dir_id) && $target_dir_id != 0)) {
  205. return $this->error('参数错误');
  206. }
  207. $res = AddonsCourseChapter::updateAll(['dir_id' => $target_dir_id], ['mall_id' => $this->mall_id, 'course_id' => $course_id, 'id' => $chapter_ids]);
  208. if (false !== $res) {
  209. return $this->success('移动成功');
  210. } else {
  211. return $this->error('移动失败');
  212. }
  213. }
  214. }