diff options
author | Simon Rettberg | 2016-05-03 19:03:09 +0200 |
---|---|---|
committer | Simon Rettberg | 2016-05-03 19:03:09 +0200 |
commit | 50404f3b23b7fd6aeae4c9d2f6df0ea25e984e66 (patch) | |
tree | 05e99fdffa696434960d7c77966c0bc36d6339e8 /modules-available/news | |
parent | Second half of merge.... (diff) | |
download | slx-admin-50404f3b23b7fd6aeae4c9d2f6df0ea25e984e66.tar.gz slx-admin-50404f3b23b7fd6aeae4c9d2f6df0ea25e984e66.tar.xz slx-admin-50404f3b23b7fd6aeae4c9d2f6df0ea25e984e66.zip |
WIP
Diffstat (limited to 'modules-available/news')
-rw-r--r-- | modules-available/news/config.json | 4 | ||||
-rw-r--r-- | modules-available/news/lang/de/templates/page-news.json | 13 | ||||
-rw-r--r-- | modules-available/news/lang/en/module.json | 12 | ||||
-rw-r--r-- | modules-available/news/lang/en/templates/page-news.json | 12 | ||||
-rw-r--r-- | modules-available/news/lang/pt/module.json | 13 | ||||
-rw-r--r-- | modules-available/news/page.inc.php | 167 | ||||
-rw-r--r-- | modules-available/news/templates/page-news.html | 57 |
7 files changed, 278 insertions, 0 deletions
diff --git a/modules-available/news/config.json b/modules-available/news/config.json new file mode 100644 index 00000000..6c189b63 --- /dev/null +++ b/modules-available/news/config.json @@ -0,0 +1,4 @@ +{ + "category":"main.content", + "enabled":"true" +} diff --git a/modules-available/news/lang/de/templates/page-news.json b/modules-available/news/lang/de/templates/page-news.json new file mode 100644 index 00000000..7801584d --- /dev/null +++ b/modules-available/news/lang/de/templates/page-news.json @@ -0,0 +1,13 @@ +{ + "lang_activeNews": "Aktive News", + "lang_content": "Inhalt", + "lang_date": "Datum", + "lang_delete": "L\u00f6schen", + "lang_editNews": "News bearbeiten", + "lang_latestUpdate": "Letzte Aktualisierung", + "lang_newsIntro": "Hier haben Sie die M\u00f6glichkeit, die von bwLehrpool-Clients angezeigten News zu editieren.", + "lang_newsOld": "Alte News", + "lang_save": "Speichern", + "lang_show": "Ansehen", + "lang_title": "Titel" +}
\ No newline at end of file diff --git a/modules-available/news/lang/en/module.json b/modules-available/news/lang/en/module.json new file mode 100644 index 00000000..5acbe58d --- /dev/null +++ b/modules-available/news/lang/en/module.json @@ -0,0 +1,12 @@ +{ + "lang_activeNews": "News", + "lang_content": "Content", + "lang_date": "Date", + "lang_delete": "Delete", + "lang_latestUpdate": "Last Update", + "lang_newsOld": "Old News", + "lang_save": "Save", + "lang_show": "Show", + "lang_title": "Title", + "module_name": "News" +}
\ No newline at end of file diff --git a/modules-available/news/lang/en/templates/page-news.json b/modules-available/news/lang/en/templates/page-news.json new file mode 100644 index 00000000..df63658c --- /dev/null +++ b/modules-available/news/lang/en/templates/page-news.json @@ -0,0 +1,12 @@ +{ + "lang_content": "Content", + "lang_date": "Date", + "lang_delete": "Delete", + "lang_editNews": "Edit news", + "lang_latestUpdate": "Latest update", + "lang_newsIntro": "Here you have the possibility to edit the news displayed to the bwLehrpool clients.", + "lang_newsOld": "Old News", + "lang_save": "Save", + "lang_show": "Show", + "lang_title": "Title" +}
\ No newline at end of file diff --git a/modules-available/news/lang/pt/module.json b/modules-available/news/lang/pt/module.json new file mode 100644 index 00000000..e1997fce --- /dev/null +++ b/modules-available/news/lang/pt/module.json @@ -0,0 +1,13 @@ +{ + "lang_activeNews": "Novidades", + "lang_content": "Conte\u00fado", + "lang_date": "Data", + "lang_delete": "Excluir", + "lang_latestUpdate": "\u00daltima Atualiza\u00e7\u00e3o", + "lang_newsIntro": "Aqui voc\u00ea tem a possibilidade de editar as novidades que s\u00e3o mostradas para os clientes do bwLehrpool.", + "lang_newsOld": "Antigas", + "lang_save": "Salvar", + "lang_show": "Mostrar", + "lang_title": "T\u00edtulo", + "module_name": "Novidades" +}
\ No newline at end of file diff --git a/modules-available/news/page.inc.php b/modules-available/news/page.inc.php new file mode 100644 index 00000000..9bbadc4f --- /dev/null +++ b/modules-available/news/page.inc.php @@ -0,0 +1,167 @@ +<?php + +class Page_News extends Page +{ + /** + * Member variables needed to represent a news entry. + * + * @var newsId int ID of the news entry attributed by the database. + * @var string Title of the entry. + * $newsContent string Content as text. (TODO: html-Support?) + * $newsDate string Unix epoch date of the news' creation. + */ + private $newsId = false; + private $newsTitle = false; + private $newsContent = false; + private $newsDate = 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 user, we will need it later + User::load(); + + // only admins should be able to edit news + if (!User::hasPermission('superadmin')) { + Message::addError('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') { + // show news + if (!$this->loadNews(Request::any('newsid'))) { + Message::addError('news-empty'); + } + } elseif ($action === 'save') { + // save to DB + if (!$this->saveNews()) { + // re-set the fields we got + Request::post('news-title') ? $this->newsTitle = Request::post('news-title') : $this->newsTitle = false; + Request::post('news-content') ? $this->newsContent = Request::post('news-content') : $this->newsContent = false; + } else { + Message::addSuccess('news-save-success'); + Util::redirect('?do=News'); + } + } elseif ($action === 'delete') { + // delete it + $this->delNews(Request::post('newsid')); + } else { + // unknown action, redirect user + Message::addError('invalid-action', $action); + Util::redirect('?do=News'); + } + } + + /** + * 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 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"; + $lines[] = $row; + } + $paginate->render('page-news', array( + 'token' => Session::get('token'), + 'latestDate' => ($this->newsDate ? date('d.m.Y H:i', $this->newsDate) : '--'), + 'latestContent' => $this->newsContent, + 'latestTitle' => $this->newsTitle, + 'list' => $lines )); + + } + /** + * Loads the news with the given ID into the form. + * + * @param int $newsId ID of the news to be shown. + * @return boolean 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 FROM news WHERE newsid = :newsid LIMIT 1", array( + 'newsid' => $newsId + )); + } else { + $row = Database::queryFirst("SELECT newsid, title, content, dateline FROM news ORDER BY dateline DESC LIMIT 1"); + } + + // fetch the news to be shown + if ($row !== false) { + $this->newsId = $row['newsid']; + $this->newsTitle = $row['title']; + $this->newsContent = $row['content']; + $this->newsDate = $row['dateline']; + } + return $row !== false; + } + + /** + * 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 news (dateline, title, content) VALUES (:dateline, :title, :content)", array( + 'dateline' => time(), + 'title' => $newsTitle, + 'content' => $newsContent + )); + return true; + } else { + Message::addError('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('value-invalid', 'newsid', $newsId); + } else { + // check passed - do delete + Database::exec("DELETE FROM news WHERE newsid = :newsid LIMIT 1", array( + 'newsid' => $newsId + )); + Message::addSuccess('news-del-success'); + } + Util::redirect('?do=News'); + } + +}
\ No newline at end of file diff --git a/modules-available/news/templates/page-news.html b/modules-available/news/templates/page-news.html new file mode 100644 index 00000000..8e400498 --- /dev/null +++ b/modules-available/news/templates/page-news.html @@ -0,0 +1,57 @@ +<div class="panel panel-default"> + <div class="panel-heading"> + {{lang_editNews}} + </div> + <div class="panel-body"> + <p>{{lang_newsIntro}}</p> + <form action="?do=News&action=save" method="post"> + <div class="form-group"> + <label for="news-title-id">{{lang_title}}</label> + <input type="text" name="news-title" id ="news-title-id" class="form-control" placeholder="{{welcome}}" value="{{latestTitle}}"> + </div> + <div class="form-group"> + <label for="news-content-id">{{lang_content}}</label> + <textarea name="news-content" id ="news-content-id" class="form-control" rows="5" cols="30" placeholder="">{{latestContent}}</textarea> + </div> + <p>{{lang_latestUpdate}}: {{latestDate}}</p> + <button class="btn btn-primary btn-sm" type="submit">{{lang_save}}</button> + <input type="hidden" name="token" value="{{token}}"> + </form> + </div> +</div> + +<div class="panel panel-default"> + <div class="panel-heading"> + {{lang_newsOld}} + </div> + <div class="panel-body"> + <div class="table-responsive"> + <form method="post" action="?do=News&action=delete"> + <input type="hidden" name="token" value="{{token}}"> + <table class="table table-stripped table-condensed"> + <thead> + <tr> + <th>{{lang_date}}</th> + <th>{{lang_title}}</th> + <th>{{lang_content}}</th> + <th></th> + </tr> + </thead> + <tbody> + {{#list}} + <tr {{#active}}class="active"{{/active}}> + <td class="text-left nowrap">{{date}}</td> + <td class="slx-ellipsis">{{title}}</td> + <td class="slx-ellipsis">{{content}}</td> + <td> + <a class="btn btn-primary btn-xs" href="?do=news&newsid={{newsid}}&action=show"><span class="glyphicon glyphicon-share-alt"></span> {{lang_show}}</a> + <button class="btn btn-danger btn-xs" type="submit" name="newsid" value="{{newsid}}"><span class="glyphicon glyphicon-remove"></span> {{lang_delete}}</button> + </td> + </tr> + {{/list}} + </tbody> + </table> + </form> + </div> + </div> +</div> |