summaryrefslogtreecommitdiffstats
path: root/modules-available/news/page.inc.php
blob: e7b70c0fef8b58ef482efa716566e7629a33a54a (plain) (blame)
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
<?php

class Page_News extends Page
{
	/*
	 * Member variables needed to represent a news entry.
	 */

	/**
	 * @var int ID of the news entry attributed by the database.
	 */
	private $newsId = false;
	/**
	 * @var string Title of the entry.
	 */
	private $newsTitle = false;
	/**
	 * @var string Content as text. (TODO: html-Support?)
	 */
	private $newsContent = false;
	/**
	 * @var int Unix epoch date of the news' creation.
	 */
	private $newsDate = false;
	private $helpContent = '';
	private $editHelp = false;
	private $hasSummernote = false;

	/**
	 * Implementation of the abstract doPreprocess function.
	 *
	 * Checks if the user is logged in and processes any
	 * action if one was specified in the request.
	 */
	protected function doPreprocess()
	{
		/* load summernote module if available */
		$this->hasSummernote = Module::isAvailable('summernote');

		// load user, we will need it later
		User::load();
		if (!User::isLoggedIn()) {
			Message::addError('main.no-permission');
			Util::redirect('?do=Main');
		}

		// check which action we need to do
		$action = Request::any('action', 'show');
		if ($action === 'clear') {
			// clear news input fields
			// TODO: is this the right way?
			$this->newsId = false;
			$this->newsTitle = false;
			$this->newsContent = false;
			$this->newsDate = false;
		} elseif ($action === 'show') {
			/* load latest things */
			$this->loadLatest('help');
			$this->loadLatest('news');

			/* and also the news (or help) with the given id */
			if (!$this->loadNews(Request::any('newsid'))) {
				Message::addError('news-empty');
			}

			if (Request::any('editHelp')) {
				$this->editHelp = true;
			}
		} elseif ($action === 'save') {
			// save to DB
			/* find out whether it's news or help */
			$pageType = Request::post('news-type');

			if ($pageType === 'news') {
				User::assertPermission("news.save");
				if (!$this->saveNews()) {
					// re-set the fields we got
					$this->newsTitle = Request::post('news-title', false, 'string');
					$this->newsContent = Request::post('news-content', false, 'string');
				} else {
					Message::addSuccess('news-save-success');
					$lastId = Database::lastInsertId();
					Util::redirect("?do=News&newsid=$lastId");
				}
			} elseif ($pageType === 'help') {
				User::assertPermission("help.save");
				if ($this->saveHelp()) {
					Message::addSuccess('help-save-success');
					$lastId = Database::lastInsertId();
					Util::redirect("?do=News&newsid=$lastId");
				}
			}
		} elseif ($action === 'delete') {
			// delete it
			$pageType = Request::post('news-type');

			if ($pageType === 'news') {
				User::assertPermission("news.delete");
				$this->delNews(Request::post('newsid'));
				Util::redirect('?do=News&editHelp=' . Request::any('editHelp'));
			} elseif ($pageType === 'help') {
				User::assertPermission("help.delete");
				$this->delNews(Request::post('newsid'));
				Util::redirect('?do=News&editHelp=' . Request::any('editHelp'));
			}
		} else {
			// unknown action, redirect user
			Message::addError('invalid-action', $action);
		}
	}

	/**
	 * Implementation of the abstract doRender function.
	 *
	 * Fetch the list of news from the database and paginate it.
	 */
	protected function doRender()
	{
		// fetch the list of the older news
		$lines = array();
		$paginate = new Paginate("SELECT newsid, dateline, title, content FROM vmchooser_pages WHERE type='news' ORDER BY dateline DESC", 10);
		$res = $paginate->exec();
		while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
			$row['date'] = date('d.m.Y H:i', $row['dateline']);

			if ($row['newsid'] == $this->newsId) {
				$row['active'] = 'active';
			}
			$row['content'] = strip_tags(str_replace('>', '> ', $row['content']));
			$lines[] = $row;
		}
		// fetch the list of the older helps
		$linesHelp = array();
		$paginateHelp = new Paginate("SELECT newsid, dateline, content FROM vmchooser_pages WHERE type='help' ORDER BY dateline DESC", 10);
		$resHelp = $paginateHelp->exec();
		while ($row = $resHelp->fetch(PDO::FETCH_ASSOC)) {
			$row['date'] = date('d.m.Y H:i', $row['dateline']);
			if ($row['newsid'] == $this->newsId) {
				$row['active'] = 'active';
			}
			$row['content'] = strip_tags(str_replace('>', '> ', $row['content']));
			$linesHelp[] = $row;
		}

		$data = array(
			'token' => Session::get('token'),
			'latestDate' => ($this->newsDate ? date('d.m.Y H:i', $this->newsDate) : '--'),
			'latestContent' => $this->newsContent,
			'latestTitle' => $this->newsTitle,
			'latestHelp' => $this->helpContent,
			'editHelp' => $this->editHelp,
			'list' => $lines,
			'listHelp' => $linesHelp,
			'hasSummernote' => $this->hasSummernote,
		);
		Permission::addGlobalTags($data['perms'], null, ['news.save', 'news.delete', 'help.save', 'help.delete']);

		$paginate->render('page-news', $data);
	}

	/**
	 * Loads the news with the given ID into the form.
	 *
	 * @param int $newsId ID of the news to be shown.
	 *
	 * @return bool true if loading that news worked
	 */
	private function loadNews($newsId)
	{
		// check to see if we need to request a specific newsid
		if ($newsId !== false) {
			$row = Database::queryFirst('SELECT newsid, title, content, dateline, type FROM vmchooser_pages WHERE newsid = :newsid LIMIT 1', array(
				'newsid' => $newsId,
			));
		} else {
			$row = Database::queryFirst("SELECT newsid, title, content, dateline, type FROM vmchooser_pages WHERE type='news' ORDER BY dateline DESC LIMIT 1");
		}

		// fetch the news to be shown
		if ($row !== false) {
			if ($row['type'] == 'news') {
				$this->newsId = $row['newsid'];
				$this->newsTitle = $row['title'];
				$this->newsContent = $row['content'];
				$this->newsDate = $row['dateline'];
				$this->editHelp = false;
			} else {
				$this->editHelp = true;
				$this->helpContent = $row['content'];
			}
		}

		return $row !== false;
	}

	private function loadLatest($type)
	{
		$row = Database::queryFirst("SELECT newsid, title, content, dateline, type FROM vmchooser_pages WHERE type=:type ORDER BY dateline DESC LIMIT 1", ['type' => $type]);
		if ($row !== false) {
			if ($row['type'] == 'news') {
				$this->newsId = $row['newsid'];
				$this->newsTitle = $row['title'];
				$this->newsContent = $row['content'];
				$this->newsDate = $row['dateline'];
			} else {
				$this->helpContent = $row['content'];
			}
		}
	}

	/**
	 * Save the given $newsTitle and $newsContent as POST'ed into the database.
	 */
	private function saveNews()
	{
		// check if news content were set by the user
		$newsTitle = Request::post('news-title');
		$newsContent = Request::post('news-content');
		if ($newsContent !== '' && $newsTitle !== '') {
			// we got title and content, save it to DB
			Database::exec("INSERT INTO vmchooser_pages (dateline, title, content, type) VALUES (:dateline, :title, :content, 'news')", array(
				'dateline' => time(),
				'title' => $newsTitle,
				'content' => $newsContent,
			));

			return true;
		} else {
			Message::addError('main.empty-field');

			return false;
		}
	}

	private function saveHelp()
	{
		$content = Request::post('help-content');
		if ($content !== '') {
			Database::exec("INSERT INTO vmchooser_pages (dateline, content, type) VALUES (:dateline, :content, 'help')", array(
				'dateline' => time(),
				'content' => $content,
			));

			return true;
		} else {
			Message::addError('main.empty-field');

			return false;
		}
	}

	/**
	 * Delete the news entry with ID $newsId.
	 *
	 * @param int $newsId ID of the entry to be deleted.
	 */
	private function delNews($newsId)
	{
		// sanity check: is newsId even numeric?
		if (!is_numeric($newsId)) {
			Message::addError('main.value-invalid', 'newsid', $newsId);
		} else {
			// check passed - do delete
			Database::exec('DELETE FROM vmchooser_pages WHERE newsid = :newsid LIMIT 1', array(
				'newsid' => $newsId,
			));
			Message::addSuccess('news-del-success');
		}
	}
}