4 function wrReportParserInit() {
6 $wgParser->setHook('bahnberichtformular', 'wrReportFormRender');
7 $wgParser->setHook('bahnberichte', 'wrReportTableRender');
12 function wrReportSandboxParse($wikiText) {
13 global $wgTitle, $wgUser;
14 $myParser = new Parser();
15 $myParserOptions = new ParserOptions();
16 $myParserOptions->initialiseFromUser($wgUser);
17 $result = $myParser->parse($wikiText, $wgTitle, $myParserOptions);
18 return $result->getText();
22 $wrReportMarkerList = array();
25 /// Returns a marker for a text and back-replaces the text in wrReportAfterTidy
26 function replaceByMarker($text, $marker = 'marker') {
27 $marker = $marker . mt_rand(1e5, 1e7);
28 global $wrReportMarkerList;
29 $wrReportMarkerList[$marker] = $text;
34 /// \brief Returns a form to enter a report (string containing HTML).
36 /// All parameters have to be UTF-8 encoded.
37 /// \param $page_title Name of the sledding run.
38 /// \return UTF-8 encoded HTML form
39 function wrRenderReportForm($hide_save_button = TRUE, $page_id = NULL, $page_title = NULL, $date_report = NULL, $condition = NULL, $description = NULL, $author_name = NULL, $page_title_list = NULL) {
40 if ($page_id) $page_id = (int) $page_id;
41 if ($page_title) $page_title = htmlspecialchars($page_title);
43 $tagnamen = array('Heute', 'Gestern', 'Vorgestern', 'Vor 3 Tagen', 'Vor 4 Tagen');
45 $date_selected = FALSE;
46 for ($tag=0; $tag!=5; ++$tag) {
47 $datum = time() - $tag*24*60*60;
48 $datum_f = strftime("%Y-%m-%d", $datum);
49 $date_options .= '<option value="' . $datum_f . '"';
50 // var_dump($date_report);
51 // var_dump($datum_f);
52 if ((is_null($date_report) && $tag == 0) || (!is_null($date_report) && $date_report == $datum_f)) {
53 $date_options .= ' selected="selected"';
54 $date_selected = TRUE;
56 $date_options .= '>' . htmlspecialchars($tagnamen[$tag]) . ' (' . strftime('%d.%m.', $datum) . ')</option>' . "\n";
58 if (!$date_selected) {
59 $date_options = '<option value="' . $date_report . '" selected="selected">' . htmlspecialchars($date_report) . "</option>\n" . $date_options;
63 $condition_options = '';
64 $condition_array = array(1 => 'Sehr gut', 2 => 'Gut', 3 => 'Mittelmäßig', 4 => 'Schlecht', 5 => 'Geht nicht');
65 $condition_options .= '<option value="">(keine Beurteilung)</option>' . "\n";
66 foreach ($condition_array as $condition_num => $condition_text) {
67 $condition_options .= "<option value=\"$condition_num\"";
68 if ($condition == $condition_num) $condition_options .= ' selected="selected"';
69 $condition_options .= '>' . htmlspecialchars(utf8_encode($condition_text)) . "</option>\n";
71 $description = htmlspecialchars($description);
74 $buttons = '<button name="action" type="submit" value="preview">Vorschau';
75 if ($hide_save_button) $buttons .= ' & Speichern';
76 $buttons .= '</button>';
77 if (!$hide_save_button) $buttons .= '<button name="action" type="submit" value="store">Speichern</button>';
80 <form action="/wiki/Spezial:Bahnberichte" method="post">
81 <table class="wrreportform" summary="Formular zum Eintragen eines Rodelbahnberichtes">
82 <tr class="oddrow"><th>Rodelbahn</th><td>$page_title<input type="hidden" name="page_id" value="$page_id"/><input type="hidden" name="page_title" value="$page_title"/></td></tr>
83 <tr class="evenrow"><th>Datum des Rodelns</th>
85 <select name="date_report">
90 <tr class="oddrow"><th>Bahnzustand*</th><td><select name="condition">
93 <tr class="evenrow"><th>Meinung</th><td><textarea name="description" cols="50" rows="7">$description</textarea></td></tr>
94 <tr class="oddrow"><th>Autor/in</th><td><input name="author_name" maxlength="30" size="30" value="$author_name" /></td></tr>
95 <tr class="evenrow"><th>Bericht abschicken</th><td>$buttons</td></tr>
104 /// \brief sub-function of wrRenderReportTable
105 /// \return UTF-8 encoded titles of HTML table
106 function wrRenderReportTableTitle($page_id, $page_title, $date_report, $date_entry, $date_invalid, $condition, $description, $author_name) {
108 if ($page_id) $out .= '<th>ID</th>';
109 if ($page_title) $out .= '<th>Bahn</th>';
110 if ($date_report) $out .= '<th>Datum</th>';
111 if ($date_entry) $out .= '<th>Datum Eintrag</th>';
112 if ($date_invalid) $out .= '<th>Datum Ungültig</th>';
113 if ($condition) $out .= '<th>Zustand</th>';
114 if ($description) $out .= '<th>Beschreibung</th>';
115 if ($author_name) $out .= '<th>Autor</th>';
116 return utf8_encode($out . "</tr>\n");
120 /// sub-function of wrRenderReportTable
121 function wrRenderReportTableRow($page_id, $page_title, $date_report, $date_entry, $date_invalid, $condition, $description, $author_name) {
123 if (!is_null($page_id)) $out .= '<td>' . $page_id . '</td>';
124 if (!is_null($page_title)) $out .= '<td>' . wrReportSandboxParse('[[' . $page_title . ']]') . '</td>';
125 $out .= '<td>' . $date_report . '</td>';
126 if (!is_null($date_entry)) $out .= '<td>' . date('Y-m-d, H:i', strtotime($date_entry)) . '</td>';
127 if (!is_null($date_invalid)) $out .= '<td>' . date('Y-m-d, H:i', strtotime($date_invalid)) . '</td>';
128 $out .= '<td>' . $condition . '</td>';
129 $out .= '<td>' . $description . '</td>';
130 $out .= '<td>' . $author_name . '</td>';
136 /// \brief It returns a table (string containing HTML) showing the results.
138 /// Is called when the tag <bahnberichte/> is encounted and renders a table /
139 /// \param $page_title If the name is specified (UTF-8 encoded), only reports of this "page" (e.g. 'Birgitzer Alm') is shown. Default: NULL
140 /// \return UTF-8 encoded HTML result table
141 function wrRenderReportTable($page_title = NULL) {
143 $dbr = wfGetDB(DB_SLAVE);
144 $conditions = array();
145 if ($page_title) $conditions['page_title'] = $page_title;
146 $res = $dbr->select('wrreport', array('id', 'page_title', 'date_report', 'date_entry', 'date_invalid', 'condition', 'description', 'author_name', 'author_username'), $conditions, $fname = 'Database::select', $options = array('ORDER BY' => 'date_report, date_entry'));
147 $admin = is_null($page_title);
148 if ($res->numRows() > 0) {
149 $out .= "<table class=\"wrreporttable\">\n";
150 $out .= wrRenderReportTableTitle($admin, $admin, TRUE, $admin, $admin, TRUE, TRUE, TRUE);
151 while ($row = $dbr->fetchObject($res)) {
152 $author = $row->author_name;
153 if ($row->author_username) $author .= ' (' . $row->author_username . ')';
154 $out .= wrRenderReportTableRow($admin ? $row->id : NULL, $admin ? $row->page_title : NULL, $row->date_report, $admin ? $row->date_entry : NULL, $admin ? $row->date_invalid : NULL, $row->condition, $row->description, $author);
156 $out .= "</table>\n";
158 $out .= "<p>Es wurden keine Bahnberichte in der Datenbank gefunden.</p>\n";
160 $dbr->freeResult($res);
165 /// Is called when the tag <<bahnberichtformular /> is encounted.
166 function wrReportFormRender($input, $args, $parser) {
167 return replaceByMarker(wrRenderReportForm(TRUE, $parser->getTitle()->getArticleID(), $parser->getTitle()->getText()));
172 function wrReportTableRender($input, $args, &$parser) {
173 return wrRenderReportTable($parser->getTitle());
178 /// Specal Page to show reports
179 class WrReport extends SpecialPage {
180 function WrReport() {
181 SpecialPage::SpecialPage('WrReport');
182 wfLoadExtensionMessages('WrReport');
186 /// Possibilities for $par:
187 /// - action == 'view' (default)
188 /// - action == 'preview': Preview new report
189 /// - action == 'store': Store new report
190 /// - action == 'success': Show a success message
191 function execute($par) {
192 global $wgRequest, $wgOut;
196 # Get request data from, e.g.
197 $action = $wgRequest->getText('action');
198 if ($action === '') $action = 'view';
200 if ($action == 'view') {
201 $wgOut->addHTML(wrRenderReportTable());
204 elseif ($action == 'preview' || $action == 'store') {
205 $page_id = $wgRequest->getText('page_id');
206 $page_title = $wgRequest->getText('page_title');
207 $date_report = $wgRequest->getText('date_report');
208 $condition = $wgRequest->getText('condition');
209 $description = $wgRequest->getText('description');
210 $author_name = $wgRequest->getText('author_name');
211 if ($action == 'store') {
212 // TODO: check conditions/permissions
213 $dbr = wfGetDB(DB_MASTER);
217 'page_id' => (int) $page_id,
218 'page_title' => $page_title,
219 'date_report' => $date_report,
220 // 'date_entry' => '', // use database default
221 // 'date_invalid' => '', // use database default
222 'condition' => $condition,
223 'description' => $description,
224 'author_name' => $author_name,
225 'author_username' => '',
226 'author_ip' => $_SERVER['REMOTE_ADDR']
229 $wgOut->addHTML('<p>Bahnbericht gespeichert! :-)</p>');
231 if ($action == 'preview') {
232 $wgOut->addHTML("<h3>Vorschau (noch nicht gespeichert)</h3>\n");
233 $wgOut->addHTML("<table class=\"wrreporttable\">\n");
234 $wgOut->addHTML(wrRenderReportTableTitle(FALSE, TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, TRUE));
235 $wgOut->addHTML(wrRenderReportTableRow(NULL, $page_title, $date_report, NULL, NULL, $condition, $description, $author_name));
236 $wgOut->addHTML("</table>\n\n\n");
237 $wgOut->addHTML(utf8_encode("<h3>Speichern oder Ändern</h3>\n"));
238 $wgOut->addHTML(wrRenderReportForm(FALSE, $page_id, $page_title, $date_report, $condition, $description, $author_name));
243 else die('Wrong action');
248 function wrReportAfterTidy(&$parser, &$text) {
249 // find markers in $text
250 // replace markers with actual output
251 global $wrReportMarkerList;
252 foreach ($wrReportMarkerList as $marker => $html) $text = str_replace($marker, $html, $text);