forked from mylxsw/wizard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DocumentController.php
374 lines (328 loc) · 11.8 KB
/
DocumentController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
<?php
/**
* Wizard
*
* @link https://aicode.cc/
* @copyright 管宜尧 <[email protected]>
*/
namespace App\Http\Controllers;
use App\Events\DocumentCreated;
use App\Events\DocumentDeleted;
use App\Events\DocumentModified;
use App\Policies\ProjectPolicy;
use App\Repositories\Document;
use App\Repositories\DocumentHistory;
use App\Repositories\OperationLogs;
use App\Repositories\PageShare;
use App\Repositories\Project;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Http\Request;
class DocumentController extends Controller
{
/**
* 创建一个新文档页面
*
* @param Request $request
* @param $id
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function newPage(Request $request, $id)
{
$this->validate(
$request,
['type' => 'in:swagger,doc', 'pid' => 'integer|min:0']
);
/** @var Project $project */
$project = Project::where('id', $id)->firstOrFail();
$this->authorize('page-add', $project);
$type = $request->input('type', 'doc');
$pid = $request->input('pid', 0);
return view("doc.{$type}", [
'newPage' => true,
'project' => $project,
'type' => $type,
'pid' => $pid,
'navigator' => navigator((int)$id, $pid),
]);
}
/**
* 编辑文档页面
*
* @param $id
* @param $page_id
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function editPage($id, $page_id)
{
/** @var Document $pageItem */
$pageItem = Document::where('project_id', $id)->where('id', $page_id)->firstOrFail();
$this->authorize('page-edit', $pageItem);
$type = ((int)$pageItem->type === Document::TYPE_DOC ? 'doc' : 'swagger');
return view("doc.{$type}", [
'pageItem' => $pageItem,
'project' => $pageItem->project,
'newPage' => false,
'type' => $type,
'pid' => $pageItem->pid,
'navigator' => navigator((int)$id, (int)$pageItem->pid, [$pageItem->id]),
]);
}
/**
* 创建一个新文档
*
* @param Request $request
* @param $id
*
* @return array
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function newPageHandle(Request $request, $id)
{
$this->validate(
$request,
[
'project_id' => "required|integer|min:1|in:{$id}|project_exist",
'title' => 'required|between:1,255',
'type' => 'required|in:doc,swagger',
'pid' => 'integer|min:0',
],
[
'title.required' => __('document.validation.title_required'),
'title.between' => __('document.validation.title_between'),
]
);
$this->authorize('page-add', $id);
$pid = $request->input('pid', 0);
$projectID = $request->input('project_id');
$title = $request->input('title');
$content = $request->input('content');
$type = $request->input('type', 'doc');
$pageItem = Document::create([
'pid' => $pid,
'title' => $title,
'description' => '',
'content' => $content,
'project_id' => $projectID,
'user_id' => \Auth::user()->id,
'last_modified_uid' => \Auth::user()->id,
'type' => $type == 'doc' ? Document::TYPE_DOC : Document::TYPE_SWAGGER,
'status' => 1,
]);
// 记录文档变更历史
DocumentHistory::write($pageItem);
event(new DocumentCreated($pageItem));
return [
'redirect' => [
'edit' => wzRoute(
'project:doc:edit:show',
['id' => $projectID, 'page_id' => $pageItem->id]
),
'show' => route(
'project:home',
['id' => $projectID, 'p' => $pageItem->id]
)
],
'message' => __('common.operation_success'),
];
}
/**
* 更新文档内容
*
* @param Request $request
* @param $id
* @param $page_id
*
* @return array
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function editPageHandle(Request $request, $id, $page_id)
{
$this->validate(
$request,
[
'project_id' => "required|integer|min:1|in:{$id}|project_exist",
'page_id' => "required|integer|min:1|in:{$page_id}|page_exist:{$id}",
'pid' => "required|integer|min:0|page_exist:{$id},false",
'title' => 'required|between:1,255',
'last_modified_at' => 'required|date',
'force' => 'bool',
'history_id' => 'required|integer',
],
[
'title.required' => __('document.validation.title_required'),
'title.between' => __('document.validation.title_between'),
]
);
$pid = $request->input('pid', 0);
$projectID = $request->input('project_id');
$title = $request->input('title');
$content = $request->input('content');
$lastModifiedAt = Carbon::parse($request->input('last_modified_at'));
$history_id = $request->input('history_id');
$forceSave = $request->input('force', false);
/** @var Document $pageItem */
$pageItem = Document::where('id', $page_id)->firstOrFail();
$this->authorize('page-edit', $pageItem);
// 检查文档是否已经被别人修改过了,避免修改覆盖
if (!$forceSave
&& (!$pageItem->updated_at->equalTo($lastModifiedAt)
|| $history_id != $pageItem->history_id)
) {
return $this->buildFailedValidationResponse($request, [
'last_modified_at' => [
__('document.validation.doc_modified_by_user', [
'username' => $pageItem->lastModifiedUser->name,
'time' => $pageItem->updated_at
])
]
]);
}
$pageItem->pid = $pid;
$pageItem->project_id = $projectID;
$pageItem->title = $title;
$pageItem->content = $content;
// 只有文档内容发生修改才进行保存
if ($pageItem->isDirty()) {
$pageItem->last_modified_uid = \Auth::user()->id;
$pageItem->save();
// 记录文档变更历史
DocumentHistory::write($pageItem);
event(new DocumentModified($pageItem));
}
return [
'message' => __('common.operation_success'),
'redirect' => [
'edit' => wzRoute(
'project:doc:edit:show',
['id' => $projectID, 'page_id' => $pageItem->id]
),
'show' => route(
'project:home',
['id' => $projectID, 'p' => $pageItem->id]
)
]
];
}
/**
* 检查页面是否已经过期
*
* @param Request $request
* @param $id
* @param $page_id
*
* @return array
*/
public function checkPageExpired(Request $request, $id, $page_id)
{
$this->validate(
$request,
[
'l' => 'required|date',
]
);
$lastModifiedAt = Carbon::parse($request->input('l'));
/** @var Document $pageItem */
$pageItem = Document::where('id', $page_id)->firstOrFail();
// 检查文档是否已经被别人修改过了,避免修改覆盖
if (!$pageItem->updated_at->equalTo($lastModifiedAt)) {
return [
'message' => __('document.validation.doc_modified_by_user', [
'username' => $pageItem->lastModifiedUser->name,
'time' => $pageItem->updated_at
]),
'expired' => true,
];
}
return [
'message' => 'ok',
'expired' => false,
];
}
/**
* 删除文档
*
* @param Request $request
* @param $id
* @param $page_id
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
* @throws \Exception
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function deletePage(Request $request, $id, $page_id)
{
$pageItem = Document::where('id', $page_id)->where('project_id', $id)->firstOrFail();
$this->authorize('page-edit', $pageItem);
// 页面删除后,所有下级页面全部移动到该页面的上级
$pageItem->subPages()->update(['pid' => $pageItem->pid]);
// 更新删除文档的用户
$pageItem->last_modified_uid = \Auth::user()->id;
$pageItem->save();
// 删除文档
$pageItem->delete();
$this->alertSuccess(__('document.document_delete_success'));
event(new DocumentDeleted($pageItem));
return redirect(wzRoute('project:home', ['id' => $id]));
}
/**
* 以JSON形式返回文档
*
* @param Request $request
* @param $id
* @param $page_id
*
* @return array|mixed|string
*/
public function getPageJSON(Request $request, $id, $page_id)
{
$pageItem = Document::where('id', $page_id)->where('project_id', $id)->firstOrFail();
$onlyBody = $request->input('only_body', 0);
if ($onlyBody) {
return $pageItem->content;
}
return [
'id' => $pageItem->id,
'pid' => $pageItem->pid,
'title' => $pageItem->title,
'description' => $pageItem->description,
'content' => $pageItem->content,
'type' => $pageItem->type,
'user_id' => $pageItem->user_id,
'username' => $pageItem->user->name,
'last_modified_user_id' => $pageItem->lastModifiedUser->id,
'last_modified_username' => $pageItem->lastModifiedUser->name,
'created_at' => $pageItem->created_at->format('Y-m-d H:i:s'),
'updated_at' => $pageItem->updated_at->format('Y-m-d H:i:s'),
];
}
/**
* read mode
*
* @param $id
* @param $page_id
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function readMode($id, $page_id)
{
/** @var Project $project */
$project = Project::with([
'pages' => function (Relation $query) {
$query->select('id', 'pid', 'title', 'description', 'project_id', 'type', 'status');
}
])->findOrFail($id);
$page = Document::where('project_id', $id)->where('id', $page_id)->firstOrFail();
$type = $page->type == Document::TYPE_DOC ? 'markdown' : 'swagger';
return view('share-show', [
'project' => $project,
'pageItem' => $page,
'type' => $type,
'noheader' => true,
]);
}
}