Skip to content

Commit

Permalink
Merge branch 'v1.0.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
titrxw committed Mar 27, 2020
2 parents 4acb2f1 + 27c91a3 commit 003d9a1
Show file tree
Hide file tree
Showing 70 changed files with 2,139 additions and 494 deletions.
162 changes: 117 additions & 45 deletions app/Controller/Admin/ChapterController.php
Expand Up @@ -77,19 +77,19 @@ public function create(Request $request)
]);

$user = $request->getAttribute('user');
if (!$user->isManager && !$user->isFounder && !$user->isOperator) {
if (!$user->isOperator) {
throw new ErrorHttpException('您没有权限管理该文档');
}

$parentId = intval($request->post('parent_id'));
$isDir = $request->post('is_dir');
if (!empty($parentId)) {
$parentChapter = ChapterLogic::instance()->getById($parentId);
if (empty($parentChapter)) {
throw new ErrorHttpException('父章节不存在');
}
}

$isDir = $request->post('is_dir');
$documentId = intval($request->post('document_id'));
$maxSort = Chapter::query()->where('document_id', '=', $documentId)->where('parent_id', '=', $parentId)->max('sort');
$sort = intval($request->post('sort', ++$maxSort));
Expand All @@ -108,7 +108,8 @@ public function create(Request $request)
'user_id' => $user->id,
'document_id' => $documentId,
'chapter_id' => $chapter->id,
'operate' => UserOperateLog::CREATE
'operate' => UserOperateLog::CREATE,
'remark' => $user->username . '创建章节' . $chapter->name
]);

return $this->data($chapter->toArray());
Expand All @@ -129,16 +130,15 @@ public function update(Request $request)
]);

$user = $request->getAttribute('user');
if (!$user->isManager && !$user->isFounder && !$user->isOperator) {
if (!$user->isOperator) {
throw new ErrorHttpException('您没有权限管理该文档');
}

$parentId = $request->post('parent_id', null);
$chapterId = intval($request->post('chapter_id'));
$chapter = ChapterLogic::instance()->getById($chapterId);
$chapter = ChapterLogic::instance()->getById($request->post('chapter_id'));
if (empty($chapter)) {
throw new ErrorHttpException('章节不存在');
}
$parentId = $request->post('parent_id', null);
if (isset($parentId)) {
if ($parentId != 0) {
$parentChapter = ChapterLogic::instance()->getById($parentId);
Expand All @@ -157,7 +157,7 @@ public function update(Request $request)
'document_id' => $chapter->document_id,
'chapter_id' => $chapter->id,
'operate' => UserOperateLog::EDIT,
'remark' => '编辑文档标题'
'remark' => $user->username . '编辑章节' . $chapter->name . '基本信息'
]);

return $this->data('success');
Expand All @@ -173,19 +173,18 @@ public function sort(Request $request)
]);

$user = $request->getAttribute('user');
if (!$user->isManager && !$user->isFounder && !$user->isOperator) {
if (!$user->isOperator) {
throw new ErrorHttpException('您没有权限管理该文档');
}

$targetChapter = ChapterLogic::instance()->getById($request->post('target')['chapter_id']);
$chapter = ChapterLogic::instance()->getById($request->post('chapter_id'));

$position = $request->post('target')['position'];

if (empty($chapter)) {
throw new ErrorHttpException('要移动的章节不存在');
}

$position = $request->post('target')['position'];
$targetChapter = ChapterLogic::instance()->getById($request->post('target')['chapter_id']);

if ($position == 'move') {
$targetDocumentId = $request->post('target')['document_id'];
$documentPermission = DocumentPermissionLogic::instance()->getByDocIdAndUid($targetDocumentId, $user->id);
Expand All @@ -204,7 +203,11 @@ public function sort(Request $request)
//放入到目录节点中,但不存在排序
if ($position == 'inner' || $position == 'move') {
try {
ChapterLogic::instance()->moveByChapter($chapter, $targetChapter);
if (empty($targetChapter)) {
//找到该文档的根节点中的其中一个章节
$targetChapter = Chapter::query()->where('document_id', $chapter->document_id)->where('parent_id', '=', 0)->first();
}
$targetChapter && ChapterLogic::instance()->moveByChapter($chapter, $targetChapter);
} catch (\Throwable $e) {
throw new ErrorHttpException($e->getMessage());
}
Expand All @@ -223,12 +226,15 @@ public function sort(Request $request)
}
}

if ($position != 'move') {
$targetChapter && $targetChapter = ChapterLogic::instance()->getById($targetChapter->parent_id);
}
UserOperateLog::query()->create([
'user_id' => $user->id,
'document_id' => $chapter->document_id,
'chapter_id' => $chapter->id,
'operate' => UserOperateLog::EDIT,
'remark' => '移动文档'
'operate' => UserOperateLog::CHAPTER_MOVE,
'remark' => $user->username . '移动章节' . $chapter->name . '到' . !empty($targetChapter) ? $targetChapter->name : '根节点'
]);

return $this->data('success');
Expand All @@ -245,7 +251,7 @@ public function delete(Request $request)
]);

$user = $request->getAttribute('user');
if (!$user->isManager && !$user->isFounder && !$user->isOperator) {
if (!$user->isOperator) {
throw new ErrorHttpException('您没有权限管理该文档');
}

Expand All @@ -263,13 +269,18 @@ public function delete(Request $request)
if (empty($id)) {
continue;
}
ChapterLogic::instance()->deleteById($id);
UserOperateLog::query()->create([
'user_id' => $user->id,
'document_id' => $documentId,
'chapter_id' => $id,
'operate' => UserOperateLog::DELETE
]);
$chapter = ChapterLogic::instance()->getById($id);
if ($chapter) {
ChapterLogic::instance()->deleteById($id);

UserOperateLog::query()->create([
'user_id' => $user->id,
'document_id' => $documentId,
'chapter_id' => $id,
'operate' => UserOperateLog::DELETE,
'remark' => $user->username . '删除章节' . $chapter->name
]);
}
}
} catch (\Throwable $e) {
throw new ErrorHttpException($e->getMessage());
Expand All @@ -289,22 +300,21 @@ public function save(Request $request)
]);

$user = $request->getAttribute('user');
if (!$user->isManager && !$user->isFounder && !$user->isOperator) {
if (!$user->isOperator) {
throw new ErrorHttpException('您没有权限管理该文档');
}

$chapterId = intval($request->post('chapter_id'));
$chapter = ChapterLogic::instance()->getById($chapterId);
$chapter = ChapterLogic::instance()->getById(intval($request->post('chapter_id')));
if (empty($chapter)) {
throw new ErrorHttpException('章节不存在');
}

if (!empty($chapter->content)) {
$chapter->content->content = $request->post('content');
$chapter->content->content = $request->post('content');
$chapter->content->save();
} else {
ChapterContent::query()->create([
'chapter_id' => $chapterId,
'chapter_id' => $chapter->id,
'content' => $request->post('content')
]);
}
Expand All @@ -317,7 +327,7 @@ public function save(Request $request)
'document_id' => $chapter->document_id,
'chapter_id' => $chapter->id,
'operate' => UserOperateLog::EDIT,
'remark' => '编辑文档内容'
'remark' => $user->username . '编辑章节' . $chapter->name . '内容'
]);

return $this->data('success');
Expand All @@ -334,25 +344,23 @@ public function content(Request $request)
'document_id.required' => '文档id必填',
]);
$user = $request->getAttribute('user');
if (!$user->isManager && !$user->isFounder && !$user->isOperator) {
if (!$user->isOperator) {
throw new ErrorHttpException('您没有权限管理该文档');
}

$chapterId = intval($request->post('chapter_id'));
$chapter = ChapterLogic::instance()->getById($chapterId);

$chapter = ChapterLogic::instance()->getById(intval($request->post('chapter_id')));
if (empty($chapter)) {
throw new ErrorHttpException('章节不存在');
}

$creator = UserOperateLogic::instance()->getByChapterAndOperate($chapterId, UserOperateLog::CREATE);
$creator = UserOperateLogic::instance()->getByChapterAndOperate($chapter->id, UserOperateLog::CREATE);
if ($creator) {
$author = $creator->user;
} else {
$author = $chapter->document->user;
}
$result = [
'chapter_id' => $chapterId,
'chapter_id' => $chapter->id,
'name' => $chapter->name,
'content' => $chapter->content->content,
'author' => [
Expand All @@ -367,15 +375,18 @@ public function content(Request $request)

/**
* 设置章节目录默认显示文章内容
* @param Request $request
* @return array
*/
public function defaultShow(Request $request) {
public function defaultShow(Request $request)
{
$this->validate($request, [
'chapter_id' => 'required',
'show_chapter_id' => 'required',
]);

$user = $request->getAttribute('user');
if (!$user->isManager && !$user->isFounder && !$user->isOperator) {
if (!$user->isOperator) {
throw new ErrorHttpException('您没有权限管理该文档');
}

Expand All @@ -385,31 +396,36 @@ public function defaultShow(Request $request) {
$showChapterId = intval($request->post('show_chapter_id'));
$showChapter = ChapterLogic::instance()->getById($showChapterId);

if (($chapterId && empty($chapter))|| empty($showChapter)) {
if (($chapterId && empty($chapter)) || empty($showChapter)) {
throw new ErrorHttpException('您要操作的章节或是目录不存在');
}

if ($chapter && empty($chapter->is_dir)) {
throw new ErrorHttpException('此操作只能设置目录的默认显示');
}
if (!empty($showChapter->is_dir)) {
throw new ErrorHttpException('设置显示的章节不能为目录');
}

if ($chapterId == 0) {
//找到已存在的顶级默认文章
$defaultShowChapter = Chapter::query()->where('document_id', '=', $showChapter->document_id)->where('parent_id', '=', 0)->where('default_show_chapter_id', '!=', 0)->first();
if ($defaultShowChapter) {
$defaultShowChapter->default_show_chapter_id = 0;
$defaultShowChapter->save();
}
$chapter = $showChapter;
}

if (!empty($showChapter->is_dir)) {
throw new ErrorHttpException('设置显示的章节不能为目录');
}

$chapter->default_show_chapter_id = $showChapterId;
$chapter->save();

UserOperateLog::query()->create([
'user_id' => $user->id,
'document_id' => $chapter->document_id,
'chapter_id' => $chapter->id,
'chapter_id' => $showChapterId,
'operate' => UserOperateLog::EDIT,
'remark' => '设置文档默认显示'
'remark' => $user->username . '设置章节' . $chapter->name . '默认显示'
]);

return $this->data('success');
Expand All @@ -435,4 +451,60 @@ public function search(Request $request)
throw new ErrorHttpException($e->getMessage());
}
}

public function copy(Request $request)
{
$params = $this->validate($request, [
'parent_id' => 'required',
'name' => 'required',
'document_id' => 'required|integer|min:1',
'chapter_id' => 'required|integer|min:1',
]);

$user = $request->getAttribute('user');
if (!$user->isOperator) {
throw new ErrorHttpException('您没有权限管理该文档');
}
$parentChapter = null;
if ($params['parent_id']) {
$parentChapter = ChapterLogic::instance()->getById($params['parent_id']);
if (!$parentChapter) {
throw new ErrorHttpException('目标章节不存在');
}
}
$chapter = ChapterLogic::instance()->getById($params['chapter_id']);
if (!$chapter) {
throw new ErrorHttpException('章节不存在');
}

$maxSort = Chapter::query()->where('document_id', '=', $params['document_id'])->where('parent_id', '=', $params['parent_id'])->max('sort');
$sort = intval($request->post('sort', ++$maxSort));

$newChapter = new Chapter();
$newChapter->parent_id = $params['parent_id'];
$newChapter->name = $params['name'];
$newChapter->document_id = $params['document_id'];
$newChapter->sort = $sort;
$newChapter->is_dir = $chapter->is_dir;
$newChapter->save();

$chapterContent = ChapterContent::query()->where('chapter_id', '=', $params['chapter_id'])->first();
if ($chapterContent) {
$newChapterContent = new ChapterContent();
$newChapterContent->chapter_id = $newChapter->id;
$newChapterContent->content = $chapterContent->content;
$newChapterContent->layout = $chapterContent->layout;
$newChapterContent->save();
}

UserOperateLog::query()->create([
'user_id' => $user->id,
'document_id' => $chapter->document_id,
'chapter_id' => $chapter->id,
'operate' => UserOperateLog::CHAPTER_COPY,
'remark' => $user->username . '复制章节' . $chapter->name . '到' . !empty($parentChapter) ? $parentChapter->name : '根节点'
]);

return $this->data($newChapter->toArray());
}
}

0 comments on commit 003d9a1

Please sign in to comment.