Skip to content

Commit bd7ce8d

Browse files
committed
Refactor Document class and add brief field
1 parent 08527b6 commit bd7ce8d

File tree

5 files changed

+523
-339
lines changed

5 files changed

+523
-339
lines changed

src/controllers/Document/Create.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,21 @@ protected function handlePost(Router &$router, DocumentCreateModel &$model) {
6969

7070
try {
7171

72-
$success = Document::create(
73-
$user_id, $options_bitmask, $title, $content
74-
);
72+
$document = new Document(null);
73+
$document->setContent($content);
74+
$document->setOptions($options_bitmask);
75+
$document->setTitle($title);
76+
$document->setUserId($user_id);
77+
$document->commit();
78+
$model->error = false;
7579

7680
} catch (QueryException $e) {
7781

7882
// SQL error occurred. We can show a friendly message to the user while
7983
// also notifying this problem to staff.
8084
Logger::logException($e);
81-
82-
}
83-
84-
if (!$success) {
8585
$model->error = 'INTERNAL_ERROR';
86-
} else {
87-
$model->error = false;
86+
8887
}
8988

9089
Logger::logEvent(

src/controllers/Document/Edit.php

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,14 @@ public function &run(Router &$router, View &$view, array &$args) {
4545
if ($model->document === null) {
4646
$model->error = 'NOT_FOUND';
4747
} else {
48-
$flags = $model->document->getOptionsBitmask();
49-
5048
$model->comments = Comment::getAll(
5149
Comment::PARENT_TYPE_DOCUMENT,
5250
$model->document_id
5351
);
5452

5553
$model->content = $model->document->getContent(false);
56-
$model->markdown = ($flags & Document::OPTION_MARKDOWN);
57-
$model->published = ($flags & Document::OPTION_PUBLISHED);
54+
$model->markdown = $model->document->isMarkdown();
55+
$model->published = $model->document->isPublished();
5856
$model->title = $model->document->getTitle();
5957

6058
if ($router->getRequestMethod() == 'POST') {
@@ -63,7 +61,7 @@ public function &run(Router &$router, View &$view, array &$args) {
6361
}
6462

6563
$view->render($model);
66-
$model->_responseCode = ($model->acl_allowed ? 200 : 403);
64+
$model->_responseCode = ($model->acl_allowed ? 200 : 401);
6765
return $model;
6866
}
6967

@@ -102,31 +100,19 @@ protected function handlePost(Router &$router, DocumentEditModel &$model) {
102100
$model->document->setTitle($model->title);
103101
$model->document->setMarkdown($model->markdown);
104102
$model->document->setContent($model->content);
105-
$model->document->setPublished($publish);
106-
107-
$model->document->setEditedCount(
108-
$model->document->getEditedCount() + 1
109-
);
110-
$model->document->setEditedDateTime(
111-
new DateTime( 'now', new DateTimeZone( 'Etc/UTC' ))
112-
);
103+
$model->document->setPublished($publish ? true : false);
113104

114-
$success = $model->document->save();
105+
$model->document->incrementEdited();
106+
$model->document->commit();
107+
$model->error = false;
115108

116109
} catch (QueryException $e) {
117110

118111
// SQL error occurred. We can show a friendly message to the user while
119112
// also notifying this problem to staff.
120113
Logger::logException($e);
121-
122-
$success = false;
123-
124-
}
125-
126-
if (!$success) {
127114
$model->error = 'INTERNAL_ERROR';
128-
} else {
129-
$model->error = false;
115+
130116
}
131117

132118
Logger::logEvent(
@@ -136,7 +122,7 @@ protected function handlePost(Router &$router, DocumentEditModel &$model) {
136122
json_encode([
137123
'error' => $model->error,
138124
'document_id' => $model->document_id,
139-
'options_bitmask' => $model->document->getOptionsBitmask(),
125+
'options_bitmask' => $model->document->getOptions(),
140126
'title' => $model->document->getTitle(),
141127
'content' => $model->document->getContent(false),
142128
])

src/controllers/Document/Index.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function &run(Router &$router, View &$view, array &$args) {
5757
if ($model->documents) {
5858
$i = count($model->documents) - 1;
5959
while ($i >= 0) {
60-
if (!($model->documents[$i]->getOptionsBitmask()
60+
if (!($model->documents[$i]->getOptions()
6161
& Document::OPTION_PUBLISHED)) {
6262
unset($model->documents[$i]);
6363
}
@@ -80,7 +80,7 @@ public function &run(Router &$router, View &$view, array &$args) {
8080
$document->getEditedDateTime()
8181
),
8282
'id' => $document->getId(),
83-
'options_bitmask' => $document->getOptionsBitmask(),
83+
'options_bitmask' => $document->getOptions(),
8484
'title' => $document->getTitle(),
8585
'user' => $document->getUser(),
8686
];

0 commit comments

Comments
 (0)