. */ namespace Fisharebest\Webtrees; use Fisharebest\Webtrees\Controller\PageController; use Fisharebest\Webtrees\Module\CkeditorModule; use PDO; require 'includes/session.php'; $controller = new PageController; $ctype = Filter::get('ctype', 'user|gedcom', 'user'); $action = Filter::get('action', 'delete', Filter::post('action', 'save')); $news_id = Filter::getInteger('news_id', 0, PHP_INT_MAX, Filter::postInteger('news_id')); $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); if (empty($news)) { $news = [ 'user_id' => $ctype === 'user' ? Auth::id() : null, 'gedcom_id' => $ctype === 'gedcom' ? $controller->tree()->getTreeId() : null, 'date' => WT_TIMESTAMP, 'subject' => Filter::post('subject'), 'body' => Filter::post('body'), ]; } // If we can't edit this item, go back to the home/my page if ($ctype === 'user' && $news['user_id'] != Auth::id() || $ctype === 'gedcom' && !Auth::isManager($controller->tree())) { header('Location: index.php?ctype=' . $ctype . '&ged=' . $controller->tree()->getNameUrl()); return; } switch ($action) { case 'delete': Database::prepare("DELETE FROM `##news` WHERE news_id = :news_id")->execute(['news_id' => $news_id,]); header('Location: index.php?ctype=' . $ctype . '&ged=' . $controller->tree()->getNameUrl()); return; case 'save': if ($news_id > 0) { Database::prepare( "UPDATE `##news` SET subject = :subject, body = :body, updated = CURRENT_TIMESTAMP WHERE news_id = :news_id" )->execute([ 'subject' => Filter::post('subject'), 'body' => Filter::post('body'), 'news_id' => $news_id, ]); } else { Database::prepare( "INSERT INTO `##news` (user_id, gedcom_id, subject, body, updated) VALUES (NULLIF(:user_id, ''), NULLIF(:gedcom_id, '') ,:subject ,:body, CURRENT_TIMESTAMP)" )->execute([ 'user_id' => $news['user_id'], 'gedcom_id' => $news['gedcom_id'], 'subject' => $news['subject'], 'body' => $news['body'], ]); } header('Location: index.php?ctype=' . $ctype . '&ged=' . $controller->tree()->getNameUrl()); return; } $controller->setPageTitle(I18N::translate('Add/edit a journal/news entry')); $controller->pageHeader(); if (Module::getModuleByName('ckeditor')) { CkeditorModule::enableEditor($controller); } ?>