Slightly renamed functions wrReportTableRender* functions.
[philipp/winterrodeln/mediawiki_extensions/wrreport.git] / wrreport.body.php
1 <?php
2 // File encoding: utf-8
3 // Classes for the wrreport extension.
4
5
6 // Special page
7 // ------------
8
9 /// Specal Page to show reports
10 class SpecialWrReport extends SpecialPage {
11         function __construct() {
12                 parent::__construct('wrreport');
13         }
14
15
16         /// \param $par Possibilities:
17         /// - action == 'view' (default)
18         /// - action == 'preview': Preview new report
19         /// - action == 'store': Store new report
20         /// - action == 'deletepreview': Preview the deleted record
21         /// - action == 'delete': Delete an existing report
22         /// - action == 'showerror': Shows the error and exits
23         /// \param $override_action If not NULL (default), it overrides the action in $par
24         /// \param $errorMsg UFT-8 encoded error message (in WikiText) to show on top of the page or NULL (default):
25         function execute($par, $override_action = NULL, $errorMsg = NULL) {
26                 // $request = $this->getRequest();
27                 // $output = $this->getOutput(); // see http://www.mediawiki.org/wiki/Manual:Special_pages
28                 // $param = $request->getText('param');
29         
30                 global $wgRequest, $wgOut, $wgParser;
31
32                 $this->setHeaders();
33
34                 // Get request data
35                 $action = $wgRequest->getText('action');
36                 if (!$action) {
37                         if ($wgRequest->getVal('preview')) $action = 'preview';
38                         elseif ($wgRequest->getVal('store')) $action = 'store';
39                         elseif ($wgRequest->getVal('deletepreview')) $action = 'deletepreview';
40                         elseif ($wgRequest->getVal('delete')) $action = 'delete';
41                         else $action = 'view';
42                 }
43                 if ($override_action) $action = $override_action;
44
45                 // Show error message
46                 if ($errorMsg || $action == 'showerror') {
47                         $wgOut->addWikiText('<div class="errorbox">' . $errorMsg . "</div>\n");
48                         if ($action == 'showerror') return;
49                 }
50
51                 // Action view
52                 if ($action == 'view') {
53                         $conditions = array('date_invalid > now()');
54                         $rows = wrReportGetReports($conditions);
55                         if (count($rows) == 0) $wgOut->addHTML(wfMessage('wrreport-reports-none')->text());
56                         else {
57                                 $wgOut->addWikiText(''); // this is necessary because otherwise $wgParser is not properly initialized but $wgParser is needed in the next line
58                                 $wgOut->addHTML(wrReportTableRender($rows, WRREPORT_DETAIL, wrReportUserMayDelete(), $wgParser));
59                         }
60                 }
61
62                 // Action deletepreview or delete
63                 elseif ($action == 'deletepreview' || $action == 'delete') {
64                         $reportid = (int) $wgRequest->getText('reportid');
65                         if ($reportid == 0) {
66                                 $this->execute($par, 'showerror', wfMessage('wrreport-deletereport-noreport')->text());
67                                 return;
68                         }
69                         $rows = wrReportGetReports(array('id' => $reportid));
70                         if (count($rows) != 1) {
71                                 $this->execute($par, 'showerror', wfMessage('wrreport-deletereport-invalid')->text());
72                                 return;
73                         }
74                         $row = $rows[0];
75                         if (!is_null($row['delete_date'])) {
76                                 $this->execute($par, 'showerror', wfMessage('wrreport-deletereport-alreadydeleted')->text());
77                                 return;
78                         }
79                         $delete_reason_public = $wgRequest->getText('delete_reason_public');
80                         $delete_person_name = $wgRequest->getText('delete_person_name');
81                         $delete_invisible = $wgRequest->getText('delete_invisible') ? TRUE : FALSE;
82                         if ($action == 'delete') {
83                                 // page
84                                 $title = Title::newFromId($row['page_id']);
85
86                                 // user
87                                 global $wgUser;
88                                 $delete_person_userid = $wgUser->getId();
89                                 if ($delete_person_userid == 0) $delete_person_userid = NULL; // to store a NULL value in the database if no user is logged in instead of 0.
90                                 $delete_person_username = $wgUser->getName();
91
92                                 // Check permissions - see also function wrReportUserMayDelete, that does also check permissions but does not return an error message.
93                                 $errorMsg = NULL;
94                                 global $wgWrReportDeleteMode;
95                                 if ($wgWrReportDeleteMode == 'deny') $errorMsg = wfMessage('wrreport-deletereport-deny')->text();
96                                 elseif ($wgWrReportDeleteMode == 'loggedin' && !$wgUser->isLoggedIn()) $errorMsg = wfMessage('wrreport-deletereport-loggedin')->text();
97                                 elseif (!$delete_person_name || !$delete_reason_public) $errorMsg = wfMessage('wrreport-deletereport-incomplete')->text();
98                                 if ($errorMsg) {
99                                         $this->execute($par, 'deletepreview', $errorMsg);
100                                         return;
101                                 }
102
103                                 // "Delete" (update) entry
104                                 $dbr = wfGetDB(DB_MASTER);
105                                 $dbr->update(
106                                         'wrreport',
107                                         array(
108                                                 'delete_date' => date('c'),
109                                                 'delete_person_name' => $delete_person_name,
110                                                 'delete_person_ip' => $_SERVER['REMOTE_ADDR'],
111                                                 'delete_person_userid' => $delete_person_userid,
112                                                 'delete_person_username' => $delete_person_username,
113                                                 'delete_reason_public' => $delete_reason_public,
114                                                 'delete_invisible' => $delete_invisible ? 't' : 'f'
115                                         ),
116                                         array('id' => $reportid)
117                                 );
118
119                                 // Purge cache
120                                 $title->invalidateCache();
121                                 wrRecacheRegions();
122
123                                 // Show success message
124                                 $wgOut->addWikiText(wfMessage('wrreport-deletereport-success', '[[' . $row['page_title'] . '#' . wfMessage('wrreport-reports-sectionname')->text() . '|' . $row['page_title'] . ']]')->text());
125                         }
126                         if ($action == 'deletepreview') {
127                                 $wgOut->addWikiText(wfMessage('wrreport-deletereport-preview-before')->text());
128                                 $format = WRREPORT_COMPACT_PAGE;
129                                 $wgOut->addHTML(wrReportTableRender(array($row), $format, FALSE, $wgParser));
130                                 $wgOut->addWikiText(wfMessage('wrreport-deletereport-preview-after')->text());
131                                 $row['delete_date'] = date('c');
132                                 $row['delete_reason_public'] = $delete_reason_public;
133                                 $row['delete_person_name'] = $delete_person_name;
134                                 $row['delete_invisible'] = $delete_invisible;
135                                 $wgOut->addHTML(wrReportTableRender(array($row), $format, FALSE, $wgParser));
136                                 $wgOut->addWikiText(wfMessage('wrreport-deletereport-preview-form')->text());
137                                 $wgOut->addHTML(wrDeleteReportFormRender($reportid, $delete_person_name, $delete_reason_public, $delete_invisible));
138                                 $wgOut->addWikiText(wfMessage('wrreport-deletereport-preview-bottom')->text());
139                         }
140                 }
141
142                 // Action preview or store
143                 elseif ($action == 'preview' || $action == 'store') {
144                         $page_title = $wgRequest->getText('page_title');
145                         $date_report = $wgRequest->getText('date_report');
146                         $condition = $wgRequest->getText('condition');
147                         $description = $wgRequest->getText('description');
148                         $author_name = $wgRequest->getText('author_name');
149
150                         // condition
151                         $condition = (int) $condition; // force to be nummeric. -1 ... "keine Bewertung", 0 ... "Bitte eingeben", 1 to 5 ... "Sehr gut" to "Geht nicht"
152                         if ($condition < -1 or $condition > 5) $condition = 0; // invalid condition: Tread like 0.
153                         $condition_sql = NULL;
154                         if ($condition >= 1 and $condition <= 5) $condition_sql = $condition;
155
156                         // author_name
157                         $author_name = trim($author_name);
158
159                         // page_id
160                         $title = Title::newFromText($page_title);
161                         $page_id = $title->getArticleId();
162                         if ($page_id == 0) $page_id = NULL;
163
164                         // user_id
165                         global $wgUser;
166                         $author_userid = $wgUser->getId();
167                         if ($author_userid == 0) $author_userid = NULL; // to store a NULL value in the database if no user is logged in instead of 0.
168                         $author_username = $wgUser->getName();
169
170                         if ($action == 'store') {
171                                 // check conditions/permissions
172                                 $errorMsg = NULL;
173                                 global $wgWrReportMode;
174                                 global $wgWrReportBlackListAll;
175                                 global $wgWrReportBlackListStrangers;
176                                 if ($wgWrReportMode == 'summer') $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-summer')->text());
177                                 elseif ($wgWrReportMode == 'deny') $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-deny')->text());
178                                 elseif ($wgWrReportMode == 'loggedin' && !$wgUser->isLoggedIn()) $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-loggedin')->text());
179                                 elseif (!$page_id) $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-pagenotfound')->text());
180                                 elseif (in_array($page_title, $wgWrReportBlackListAll)) $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-blacklist')->text());
181                                 elseif (!$wgUser->isLoggedIn() && in_array($page_title, $wgWrReportBlackListStrangers)) $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-blackliststrangers')->text());
182                                 elseif ($condition == 0) $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-choosecondition')->text());
183                                 elseif (!$wgUser->isLoggedIn()) {
184                                         if (!$description) $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-enterdescription')->text());
185                                         elseif (!(stripos($description, 'http') === FALSE)) $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-nohttp')->text());
186                                         elseif (!$author_name) $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-enterauthor')->text());
187                                 }
188
189                                 // check author name
190                                 if (!$errorMsg) {
191                                         $author_name_id = $wgUser->idFromName(strtolower($author_name));
192                                         if ($wgUser->isLoggedIn()) {
193                                                 if ($author_name_id != 0 && $author_name_id != $wgUser->getId())
194                                                         $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-authorused')->text());
195                                         } else {
196                                                 if ($author_name_id != 0)
197                                                         $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-authorlogin')->text());
198                                         }
199                                 }
200
201                                 // Chech whether identical reports are present
202                                 if (!$errorMsg) {
203                                         $dbr = wfGetDB(DB_SLAVE);
204                                         $cond = 'condition';
205                                         global $wgDBtype;
206                                         if ($wgDBtype == "mysql") $cond = "`$cond`"; // "condition" is a reserved word in mysql
207                                         $sqlConditions = array('page_id' => $page_id, 'date_report' => $date_report, $cond => $condition_sql, 'description' => $description, 'author_name' => $author_name);
208                                         $res = $dbr->select('wrreport', 'id', $sqlConditions);
209                                         if ($res->numRows() == 1) $errorMsg = htmlspecialchars(wfMessage('wrreport-newreport-alreadysaved')->text());
210                                         $dbr->freeResult($res);
211                                 }
212
213                                 // Show error if any
214                                 if ($errorMsg) {
215                                         $this->execute($par, 'preview', $errorMsg);
216                                         return;
217                                 }
218
219                                 // Save entry
220                                 $dbr = wfGetDB(DB_MASTER);
221                                 $dbr->insert(
222                                         'wrreport',
223                                         array(
224                                                 'page_id' => $page_id,
225                                                 'page_title' => $page_title,
226                                                 'date_report' => $date_report,
227                                                 'date_entry' => date('c'),
228                                                 'date_invalid' => date('c', strtotime('+9 days')),
229                                                 $cond => $condition_sql,
230                                                 'description' => $description,
231                                                 'author_name' => $author_name,
232                                                 'author_ip' => $_SERVER['REMOTE_ADDR'],
233                                                 'author_userid' => $author_userid,
234                                                 'author_username' => $author_username
235                                                 // 'delete_*' => // use database defaults (NULL)
236                                         )
237                                 );
238
239                                 // Purge cache
240                                 $title->invalidateCache();
241                                 wrRecacheRegions();
242                                 wrUpdateWrReportCacheTable($page_id);
243
244                                 // Show success message
245                                 $wgOut->addWikiText(wfMessage('wrreport-newreport-success', '[[' . $page_title . '#' . wfMessage('wrreport-reports-sectionname')->text() . '|' . $page_title . ']]')->text());
246                                 // We could redirect to result with the following line but we don't want to.
247                                 // $wgOut->redirect($title->getFullURL() . '#' . wfMessage('wrreport-reports-sectionname')->text());
248                         }
249                         if ($action == 'preview') {
250                                 $wgOut->addWikiText(wfMessage('wrreport-newreport-preview-top')->text());
251                                 $format = WRREPORT_COMPACT_PAGE;
252                                 $row = array_fill_keys(wrReportGetColumnNames(), NULL);
253                                 $row['page_id'] = $page_id;
254                                 $row['page_title'] = $page_title;
255                                 $row['date_report'] = $date_report;
256                                 $row['condition'] = $condition_sql;
257                                 $row['description'] = $description;
258                                 $row['author_name'] = $author_name;
259                                 $row['author_userid'] = $author_userid;
260                                 $row['author_username'] = $author_username;
261
262                                 $wgOut->addHTML(wrReportTableRender(array($row), $format, FALSE, $wgParser));
263                                 $wgOut->addWikiText(wfMessage('wrreport-newreport-preview-middle')->text());
264                                 $wgOut->addHTML(wrReportFormRender(FALSE, $page_title, $date_report, $condition, $description, $author_name));
265                                 $wgOut->addWikiText(wfMessage('wrreport-newreport-preview-bottom')->text());
266                         }
267
268                 }
269
270                 else die('Wrong action');
271         }
272 }
273
274
275 ?>