Completed
Push — openstreetmap ( b0b563...f189bb )
by Greg
22:39 queued 13:08
created

editnews.php (1 issue)

Severity
1
<?php
2
/**
3
 * webtrees: online genealogy
4
 * Copyright (C) 2018 webtrees development team
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
 * GNU General Public License for more details.
13
 * You should have received a copy of the GNU General Public License
14
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15
 */
16
namespace Fisharebest\Webtrees;
17
18
use Fisharebest\Webtrees\Controller\PageController;
19
use Fisharebest\Webtrees\Module\CkeditorModule;
20
use PDO;
21
22
require 'includes/session.php';
23
24
$controller = new PageController;
25
26
$ctype   = Filter::get('ctype', 'user|gedcom', 'user');
27
$action  = Filter::get('action', 'delete', Filter::post('action', 'save'));
28
$news_id = Filter::getInteger('news_id', 0, PHP_INT_MAX, Filter::postInteger('news_id'));
29
30
$news = Database::prepare("SELECT user_id, gedcom_id, UNIX_TIMESTAMP(updated) AS date, subject, body FROM `##news` WHERE news_id = :news_id")->execute(['news_id' => $news_id])->fetchOneRow(PDO::FETCH_ASSOC);
31
32
if (empty($news)) {
33
	$news = [
34
		'user_id'   => $ctype === 'user' ? Auth::id() : null,
35
		'gedcom_id' => $ctype === 'gedcom' ? $controller->tree()->getTreeId() : null,
36
		'date'      => WT_TIMESTAMP,
37
		'subject'   => Filter::post('subject'),
38
		'body'      => Filter::post('body'),
39
	];
40
}
41
// If we can't edit this item, go back to the home/my page
42
if ($ctype === 'user' && $news['user_id'] != Auth::id() || $ctype === 'gedcom' && !Auth::isManager($controller->tree())) {
0 ignored issues
show
Consider adding parentheses for clarity. Current Interpretation: {currentAssign}, Probably Intended Meaning: {alternativeAssign}
Loading history...
43
	header('Location: index.php?ctype=' . $ctype . '&ged=' . $controller->tree()->getNameUrl());
44
45
	return;
46
}
47
48
switch ($action) {
49
case 'delete':
50
	Database::prepare("DELETE FROM `##news` WHERE news_id = :news_id")->execute(['news_id' => $news_id,]);
51
52
	header('Location: index.php?ctype=' . $ctype . '&ged=' . $controller->tree()->getNameUrl());
53
54
	return;
55
56
case 'save':
57
	if ($news_id > 0) {
58
		Database::prepare(
59
			"UPDATE `##news` SET subject = :subject, body = :body, updated = CURRENT_TIMESTAMP WHERE news_id = :news_id"
60
		)->execute([
61
			'subject' => Filter::post('subject'),
62
			'body'    => Filter::post('body'),
63
			'news_id' => $news_id,
64
		]);
65
	} else {
66
		Database::prepare(
67
			"INSERT INTO `##news` (user_id, gedcom_id, subject, body, updated) VALUES (NULLIF(:user_id, ''), NULLIF(:gedcom_id, '') ,:subject ,:body, CURRENT_TIMESTAMP)"
68
		)->execute([
69
			'user_id'   => $news['user_id'],
70
			'gedcom_id' => $news['gedcom_id'],
71
			'subject'   => $news['subject'],
72
			'body'      => $news['body'],
73
		]);
74
	}
75
76
	header('Location: index.php?ctype=' . $ctype . '&ged=' . $controller->tree()->getNameUrl());
77
78
	return;
79
}
80
81
$controller->setPageTitle(I18N::translate('Add/edit a journal/news entry'));
82
$controller->pageHeader();
83
84
if (Module::getModuleByName('ckeditor')) {
85
	CkeditorModule::enableEditor($controller);
86
}
87
88
?>
89
<h2><?= $controller->getPageTitle() ?></h2>
90
91
<form method="post">
92
	<input type="hidden" name="ged" value="<?= $controller->tree()->getNameUrl() ?>">
93
	<input type="hidden" name="action" value="save">
94
95
	<table>
96
		<tr>
97
			<th>
98
				<label for="subject">
99
					<?= I18N::translate('Title') ?>
100
				</label>
101
			</th>
102
		<tr>
103
		<tr>
104
			<td>
105
				<input type="text" id="subject" name="subject" size="50" dir="auto" autofocus value="<?= e($news['subject']) ?>">
106
			</td>
107
		</tr>
108
		<tr>
109
			<th>
110
				<label for="body">
111
					<?= I18N::translate('Content') ?>
112
				</label>
113
			</th>
114
		</tr>
115
		<tr>
116
			<td>
117
				<textarea id="body" name="body" class="html-edit form-control" rows="10" dir="auto"><?= e($news['body']) ?></textarea>
118
			</td>
119
		</tr>
120
		<tr>
121
			<td>
122
				<input type="submit" value="<?= I18N::translate('save') ?>">
123
			</td>
124
		</tr>
125
	</table>
126
</form>
127