1
- <?php
2
-
1
+ <?php /* vim: set colorcolumn= expandtab shiftwidth=2 softtabstop=2 tabstop=4 smarttab: */
3
2
namespace BNETDocs \Controllers \News ;
4
3
5
4
use \BNETDocs \Libraries \Authentication ;
21
20
use \DateTimeZone ;
22
21
use \InvalidArgumentException ;
23
22
24
- class Edit extends Controller {
25
- public function &run (Router &$ router , View &$ view , array &$ args ) {
23
+ class Edit extends Controller
24
+ {
25
+ public function &run (Router &$ router , View &$ view , array &$ args )
26
+ {
26
27
$ data = $ router ->getRequestQueryArray ();
27
28
$ model = new NewsEditModel ();
28
29
$ model ->active_user = Authentication::$ user ;
@@ -41,6 +42,14 @@ public function &run(Router &$router, View &$view, array &$args) {
41
42
User::OPTION_ACL_NEWS_MODIFY
42
43
));
43
44
45
+ if (!$ model ->acl_allowed )
46
+ {
47
+ $ model ->_responseCode = 403 ;
48
+ $ model ->error = 'ACL_NOT_SET ' ;
49
+ $ view ->render ($ model );
50
+ return $ model ;
51
+ }
52
+
44
53
try { $ model ->news_post = new NewsPost ($ model ->news_post_id ); }
45
54
catch (NewsPostNotFoundException $ e ) { $ model ->news_post = null ; }
46
55
catch (InvalidArgumentException $ e ) { $ model ->news_post = null ; }
@@ -75,12 +84,16 @@ public function &run(Router &$router, View &$view, array &$args) {
75
84
return $ model ;
76
85
}
77
86
78
- protected function handlePost (Router &$ router , NewsEditModel &$ model ) {
79
- if (!$ model ->acl_allowed ) {
87
+ protected function handlePost (Router &$ router , NewsEditModel &$ model )
88
+ {
89
+ if (!$ model ->acl_allowed )
90
+ {
80
91
$ model ->error = 'ACL_NOT_SET ' ;
81
92
return ;
82
93
}
83
- if (!isset (Common::$ database )) {
94
+
95
+ if (!isset (Common::$ database ))
96
+ {
84
97
Common::$ database = DatabaseDriver::getDatabaseObject ();
85
98
}
86
99
@@ -99,16 +112,12 @@ protected function handlePost(Router &$router, NewsEditModel &$model) {
99
112
$ model ->content = $ content ;
100
113
$ model ->rss_exempt = $ rss_exempt ;
101
114
102
- if (empty ($ title )) {
103
- $ model ->error = 'EMPTY_TITLE ' ;
104
- } else if (empty ($ content )) {
105
- $ model ->error = 'EMPTY_CONTENT ' ;
106
- }
107
-
108
- $ user_id = $ model ->active_user ->getId ();
115
+ $ model ->error = (empty ($ title ) ? 'EMPTY_TITLE ' : (empty ($ content ) ? 'EMPTY_CONTENT ' : null ));
109
116
110
- try {
117
+ if ( $ model -> error ) return ;
111
118
119
+ try
120
+ {
112
121
$ model ->news_post ->setCategoryId ($ model ->category );
113
122
$ model ->news_post ->setTitle ($ model ->title );
114
123
$ model ->news_post ->setMarkdown ($ model ->markdown );
@@ -124,28 +133,25 @@ protected function handlePost(Router &$router, NewsEditModel &$model) {
124
133
);
125
134
126
135
$ success = $ model ->news_post ->save ();
127
-
128
- } catch (QueryException $ e ) {
129
-
136
+ $ model ->error = false ;
137
+ }
138
+ catch (QueryException $ e )
139
+ {
130
140
// SQL error occurred. We can show a friendly message to the user while
131
141
// also notifying this problem to staff.
132
142
Logger::logException ($ e );
133
143
134
144
$ success = false ;
135
-
136
- }
137
-
138
- if (!$ success ) {
139
145
$ model ->error = 'INTERNAL_ERROR ' ;
140
- } else {
141
- $ model ->error = false ;
142
146
}
143
147
144
- Logger::logEvent (
148
+ Logger::logEvent
149
+ (
145
150
EventTypes::NEWS_EDITED ,
146
- $ user_id ,
151
+ ( $ model -> active_user ? $ model -> active_user -> getId () : null ) ,
147
152
getenv ('REMOTE_ADDR ' ),
148
- json_encode ([
153
+ json_encode
154
+ ([
149
155
'error ' => $ model ->error ,
150
156
'news_post_id ' => $ model ->news_post_id ,
151
157
'category_id ' => $ model ->news_post ->getCategoryId (),
0 commit comments