From f31b474e1f720e94b37cfedfe0febe453284f158 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 28 May 2019 17:57:29 +0200 Subject: [news] Modularize; add 'login-news' category TODO: Use date/time picker for expire time --- modules-available/news/page.inc.php | 287 +++++++++++++++++++----------------- 1 file changed, 154 insertions(+), 133 deletions(-) (limited to 'modules-available/news/page.inc.php') diff --git a/modules-available/news/page.inc.php b/modules-available/news/page.inc.php index 1e2e3eef..8e2b4e7e 100644 --- a/modules-available/news/page.inc.php +++ b/modules-available/news/page.inc.php @@ -2,6 +2,19 @@ class Page_News extends Page { + + private $hasSummernote = false; + + const TYPES = [ + // Dictionary::translate('type_news'); + 'news' => ['headline' => true], + // Dictionary::translate('type_help'); + 'help' => ['headline' => false], + // Dictionary::translate('type_login-news'); + 'login-news' => ['headline' => false], + ]; + + private $pageType = false; /* * Member variables needed to represent a news entry. */ @@ -15,16 +28,18 @@ class Page_News extends Page */ private $newsTitle = false; /** - * @var string Content as text. (TODO: html-Support?) + * @var string HTML news content */ private $newsContent = false; /** * @var int Unix epoch date of the news' creation. */ - private $newsDate = false; - private $helpContent = ''; - private $editHelp = false; - private $hasSummernote = false; + private $newsDateline = false; + /** + * @var int Unix epoch date when the news expires. + */ + private $newsExpires = false; + /** * Implementation of the abstract doPreprocess function. @@ -34,8 +49,6 @@ class Page_News extends Page */ protected function doPreprocess() { - /* load summernote module if available */ - $this->hasSummernote = Module::isAvailable('summernote'); // load user, we will need it later User::load(); @@ -45,62 +58,55 @@ class Page_News extends Page } // check which action we need to do - $action = Request::any('action', 'show'); - if ($action === 'show') { + if (!Request::isPost()) { + User::assertPermission('access-page'); - /* 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'); + $newsId = Request::get('newsid', false, 'int'); + $pageType = Request::get('type', false, 'string'); + if ($pageType === false && $newsId === false) { + Util::redirect('?do=news&type=news'); + } + $this->pageType = $pageType === false ? 'news' : $pageType; + $this->loadNews($newsId, $pageType); + + foreach (self::TYPES as $type => $entry) { + Dashboard::addSubmenu('?do=news&type=' . $type, Dictionary::translate('type_' . $type, true)); } - if (Request::any('editHelp')) { - $this->editHelp = true; + } else { + + $action = Request::post('action', false, 'string'); + $pageType = Request::post('type', false, 'string'); + if (!array_key_exists($pageType, self::TYPES)) { + Message::addError('invalid-type', $pageType); + Util::redirect('?do=news'); } - } 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'); + if ($action === 'save') { + // save to DB + User::assertPermission("$pageType.save"); + if (!$this->saveNews($pageType)) { + Message::addError('save-error'); } 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')); + } elseif ($action === 'delete') { + // delete it + User::assertPermission("$pageType.delete"); + $this->delNews(Request::post('newsid', false, 'int'), $pageType); + } else { + // unknown action, redirect user + Message::addError('invalid-action', $action); } - } else { - // unknown action, redirect user - Message::addError('invalid-action', $action); + + Util::redirect('?do=news&type=' . $pageType); } + + /* load summernote module if available */ + $this->hasSummernote = Module::isAvailable('summernote'); } /** @@ -111,152 +117,167 @@ class Page_News extends Page protected function doRender() { // fetch the list of the older news + $NOW = time(); $lines = array(); - $paginate = new Paginate("SELECT newsid, dateline, title, content FROM vmchooser_pages WHERE type='news' ORDER BY dateline DESC", 10); - $res = $paginate->exec(); + $res = Database::simpleQuery("SELECT newsid, dateline, expires, title, content FROM vmchooser_pages + WHERE type = :type ORDER BY dateline DESC LIMIT 20", ['type' => $this->pageType]); while ($row = $res->fetch(PDO::FETCH_ASSOC)) { - $row['date'] = date('d.m.Y H:i', $row['dateline']); + $row['dateline_s'] = Util::prettyTime($row['dateline']); + $row['expires_s'] = $this->formatExpires($row['expires']); + if ($row['expires'] < $NOW) { + $row['muted'] = 'text-muted'; + } if ($row['newsid'] == $this->newsId) { $row['active'] = 'active'; } - $row['content'] = strip_tags(str_replace('>', '> ', $row['content'])); + $row['content'] = substr(strip_tags(str_replace('>', '> ', $row['content'])), 0, 160); $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; - } + $validity = ceil(($this->newsExpires - $NOW) / 3600); + if ($this->newsExpires === false || $validity > 24 * 365 * 5) { + $validity = ''; + } $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, + 'withTitle' => self::TYPES[$this->pageType]['headline'], + 'newsTypeName' => Dictionary::translate('type_' . $this->pageType, true), + 'dateline_s' => Util::prettyTime($this->newsDateline), + 'expires_s' => $this->formatExpires($this->newsExpires), + 'currentContent' => $this->newsContent, + 'currentTitle' => $this->newsTitle, + 'type' => $this->pageType, + 'validity' => $validity, 'list' => $lines, - 'listHelp' => $linesHelp, 'hasSummernote' => $this->hasSummernote, ); - Permission::addGlobalTags($data['perms'], null, ['news.save', 'news.delete', 'help.save', 'help.delete']); + if (!User::hasPermission($this->pageType . '.save')) { + $data['save'] = [ + 'readonly' => 'readonly', + 'disabled' => 'disabled', + ]; + } + if (!User::hasPermission($this->pageType . '.delete')) { + $data['delete'] = [ + 'readonly' => 'readonly', + 'disabled' => 'disabled', + ]; + } + Render::addTemplate('page-news', $data); + } - $paginate->render('page-news', $data); + private function formatExpires($ts) + { + if ($ts - 86400 * 365 * 5 > time()) + return '-'; + return Util::prettyTime($ts); } /** * Loads the news with the given ID into the form. * * @param int $newsId ID of the news to be shown. + * @param string $pageType type if news id is not given. * * @return bool true if loading that news worked */ - private function loadNews($newsId) + private function loadNews($newsId, $pageType) { // 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( + $row = Database::queryFirst('SELECT newsid, title, content, dateline, expires, type FROM vmchooser_pages + WHERE newsid = :newsid LIMIT 1', [ 'newsid' => $newsId, - )); + ]); + if ($row === false) { + Message::addError('news-empty'); + } } else { - $row = Database::queryFirst("SELECT newsid, title, content, dateline, type FROM vmchooser_pages WHERE type='news' ORDER BY dateline DESC LIMIT 1"); + $row = Database::queryFirst("SELECT newsid, title, content, dateline, expires, type FROM vmchooser_pages + WHERE type = :type AND expires > UNIX_TIMESTAMP() ORDER BY dateline DESC LIMIT 1", [ + 'type' => $pageType, + ]); } + if ($row === false) + return false; // 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']; - } + $this->newsId = $row['newsid']; + $this->newsTitle = $row['title']; + $this->newsContent = $row['content']; + $this->newsDateline = (int)$row['dateline']; + $this->newsExpires = (int)$row['expires']; + $this->pageType = $row['type']; } + return true; } /** * Save the given $newsTitle and $newsContent as POST'ed into the database. */ - private function saveNews() + private function saveNews($pageType) { // check if news content were set by the user - $newsTitle = Request::post('news-title'); - $newsContent = Request::post('news-content'); - if ($newsContent !== '' && $newsTitle !== '') { + $newsTitle = Request::post('news-title', '', 'string'); + $newsContent = Request::post('news-content', false, 'string'); + $validity = Request::post('validity', false, 'string'); + if ($validity === false || $validity === '') { + $validity = 86400 * 3650; // 10 Years + } else { + $validity *= 3600; // Hours to seconds + } + if (!empty($newsContent)) { // 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( + // dup check first + $row = Database::queryFirst('SELECT newsid FROM vmchooser_pages + WHERE content = :content AND type = :type LIMIT 1', [ + 'content' => $newsContent, + 'type' => $pageType, + ]); + if ($row !== false) { + Database::exec('UPDATE vmchooser_pages SET dateline = :dateline, expires = :expires, title = :title + WHERE newsid = :newsid LIMIT 1', [ + 'newsid' => $row['newsid'], + 'dateline' => time(), + 'expires' => time() + $validity, + 'title' => $newsTitle, + ]); + return true; + } + // new one + Database::exec("INSERT INTO vmchooser_pages (dateline, expires, title, content, type) + VALUES (:dateline, :expires, :title, :content, :type)", array( 'dateline' => time(), + 'expires' => time() + $validity, 'title' => $newsTitle, 'content' => $newsContent, + 'type' => $pageType, )); 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; - } + Message::addError('main.empty-field'); + return false; } /** * Delete the news entry with ID $newsId. * * @param int $newsId ID of the entry to be deleted. + * @param string $pageType type of news to be deleted. Must match the ID, otherwise do nothing. */ - private function delNews($newsId) + private function delNews($newsId, $pageType) { // 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( + Database::exec('DELETE FROM vmchooser_pages WHERE newsid = :newsid AND type = :type LIMIT 1', array( 'newsid' => $newsId, + 'type' => $pageType, )); Message::addSuccess('news-del-success'); } -- cgit v1.2.3-55-g7522