summaryrefslogtreecommitdiff
path: root/includes
diff options
context:
space:
mode:
authorIgor Scheller <igor.scheller@igorshp.de>2019-11-10 21:30:26 +0100
committerIgor Scheller <igor.scheller@igorshp.de>2019-11-10 22:10:11 +0100
commitb878740f80ce7cfe2a0bc53956e3f7e4e0aa2f78 (patch)
tree4003b59c5ea2a731f8881711b95e2b736ae04626 /includes
parent6534191d59521e1e5a88638b1c6a1c77f74dd12a (diff)
News: Bug fixes, cleanup, comments & formatting
Use more magically available methods and properties Fixed atom feed and stats not using the new model
Diffstat (limited to 'includes')
-rw-r--r--includes/pages/admin_news.php5
-rw-r--r--includes/pages/user_atom.php37
-rw-r--r--includes/pages/user_news.php25
3 files changed, 30 insertions, 37 deletions
diff --git a/includes/pages/admin_news.php b/includes/pages/admin_news.php
index 75d8291e..1d49af80 100644
--- a/includes/pages/admin_news.php
+++ b/includes/pages/admin_news.php
@@ -1,13 +1,12 @@
<?php
-use Engelsystem\Models\News\News;
+use Engelsystem\Models\News;
/**
* @return string
*/
function admin_news()
{
- $user = auth()->user();
$request = request();
if (!$request->has('action')) {
@@ -45,7 +44,7 @@ function admin_news()
form_info(__('Author'), User_Nick_render($user_source)),
form_text('eBetreff', __('Subject'), $news->title),
form_textarea('eText', __('Message'), $news->text),
- form_checkbox('eTreffen', __('Meeting'), $news->is_meeting === true, 1),
+ form_checkbox('eTreffen', __('Meeting'), $news->is_meeting, 1),
form_submit('submit', __('Save'))
],
page_link_to('admin_news', ['action' => 'save', 'id' => $news_id])
diff --git a/includes/pages/user_atom.php b/includes/pages/user_atom.php
index a491fea7..9a4d65a5 100644
--- a/includes/pages/user_atom.php
+++ b/includes/pages/user_atom.php
@@ -1,7 +1,8 @@
<?php
-use Engelsystem\Database\DB;
use Engelsystem\Http\Exceptions\HttpForbidden;
+use Engelsystem\Models\News;
+use Illuminate\Database\Eloquent\Collection;
/**
* Publically available page to feed the news to feed readers
@@ -23,15 +24,11 @@ function user_atom()
throw new HttpForbidden('Not allowed', ['content-type' => 'text/text']);
}
- $news = DB::select('
- SELECT *
- FROM `News`
- ' . (!$request->has('meetings') ? '' : 'WHERE `Treffen` = 1 ') . '
- ORDER BY `ID`
- DESC LIMIT ' . (int)config('display_news')
- );
-
- $output = make_atom_entries_from_news($news);
+ $news = $request->has('meetings') ? News::whereIsMeeting((bool)$request->get('meetings', false)) : News::query();
+ $news
+ ->limit((int)config('display_news'))
+ ->orderByDesc('updated_at');
+ $output = make_atom_entries_from_news($news->get());
header('Content-Type: application/atom+xml; charset=utf-8');
header('Content-Length: ' . strlen($output));
@@ -39,12 +36,14 @@ function user_atom()
}
/**
- * @param array[] $news_entries
+ * @param News[]|Collection $news_entries
* @return string
*/
function make_atom_entries_from_news($news_entries)
{
$request = app('request');
+ $updatedAt = isset($news_entries[0]) ? $news_entries[0]->updated_at->format('Y-m-d\TH:i:sP') : '0000:00:00T00:00:00+00:00';
+
$html = '<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>' . config('app_name') . '</title>
@@ -55,7 +54,7 @@ function make_atom_entries_from_news($news_entries)
$request->getRequestUri()
))
. '</id>
- <updated>' . date('Y-m-d\TH:i:sP', $news_entries[0]['Datum']) . '</updated>' . "\n";
+ <updated>' . $updatedAt . '</updated>' . "\n";
foreach ($news_entries as $news_entry) {
$html .= make_atom_entry_from_news($news_entry);
}
@@ -64,21 +63,21 @@ function make_atom_entries_from_news($news_entries)
}
/**
- * @param array $news_entry
+ * @param News $news
* @return string
*/
-function make_atom_entry_from_news($news_entry)
+function make_atom_entry_from_news(News $news)
{
return '
<entry>
- <title>' . htmlspecialchars($news_entry['Betreff']) . '</title>
- <link href="' . page_link_to('news_comments', ['nid' => $news_entry['ID']]) . '"/>
+ <title>' . htmlspecialchars($news->title) . '</title>
+ <link href="' . page_link_to('news_comments', ['nid' => $news->id]) . '"/>
<id>' . preg_replace(
'#^https?://#',
'',
- page_link_to('news_comments', ['nid' => $news_entry['ID']])
+ page_link_to('news_comments', ['nid' => $news->id])
) . '</id>
- <updated>' . date('Y-m-d\TH:i:sP', $news_entry['Datum']) . '</updated>
- <summary type="html">' . htmlspecialchars($news_entry['Text']) . '</summary>
+ <updated>' . $news->updated_at->format('Y-m-d\TH:i:sP') . '</updated>
+ <summary type="html">' . htmlspecialchars($news->text) . '</summary>
</entry>' . "\n";
}
diff --git a/includes/pages/user_news.php b/includes/pages/user_news.php
index f67896da..643d9d04 100644
--- a/includes/pages/user_news.php
+++ b/includes/pages/user_news.php
@@ -1,7 +1,7 @@
<?php
use Engelsystem\Database\DB;
-use Engelsystem\Models\News\News;
+use Engelsystem\Models\News;
use Engelsystem\Models\User\User;
/**
@@ -43,7 +43,7 @@ function user_meetings()
$page = 0;
}
- $news = News::where('is_meeting', true)
+ $news = News::whereIsMeeting(true)
->orderBy('created_at', 'DESC')
->limit($display_news)
->offset($page * $display_news)
@@ -53,7 +53,7 @@ function user_meetings()
$html .= display_news($entry);
}
- $dis_rows = ceil(News::where('is_meeting', true)->count() / $display_news);
+ $dis_rows = ceil(News::whereIsMeeting(true)->count() / $display_news);
$html .= '<div class="text-center">' . '<ul class="pagination">';
for ($i = 0; $i < $dis_rows; $i++) {
if ($request->has('page') && $i == $request->input('page', 0)) {
@@ -110,7 +110,7 @@ function display_news(News $news): string
}
$html .= '<span class="glyphicon glyphicon-time"></span> ' . $news->created_at->format('Y-m-d H:i') . '&emsp;';
- $html .= User_Nick_render(User::find($news->user_id));
+ $html .= User_Nick_render($news->user);
if ($page != 'news_comments') {
$html .= '&emsp;<a href="' . page_link_to('news_comments', ['nid' => $news->id]) . '">'
. '<span class="glyphicon glyphicon-comment"></span> '
@@ -137,9 +137,8 @@ function user_news_comments()
if (
$request->has('nid')
&& preg_match('/^\d{1,}$/', $nid)
- && News::where('id', $request->input('nid'))->count() > 0
+ && $news = News::find($nid)
) {
- $news = News::find('id');
if ($request->hasPostData('submit') && $request->has('text')) {
$text = $request->input('text');
DB::insert('
@@ -180,7 +179,7 @@ function user_news_comments()
$html .= form([
form_textarea('text', __('Message'), ''),
form_submit('submit', __('Save'))
- ], page_link_to('news_comments', ['nid' => $news['ID']]));
+ ], page_link_to('news_comments', ['nid' => $news->id]));
} else {
$html .= __('Invalid request.');
}
@@ -199,25 +198,21 @@ function user_news()
$html = '<div class="col-md-12"><h1>' . news_title() . '</h1>' . msg();
- $isMeeting = $request->postData('treffen');
+ $isMeeting = $request->postData('treffen', false);
if ($request->has('text') && $request->has('betreff') && auth()->can('admin_news')) {
- if (!$request->has('treffen')) {
- $isMeeting = 0;
- }
-
$text = $request->postData('text');
if (!auth()->can('admin_news_html')) {
$text = strip_tags($text);
}
- News::create([
+ $news = News::create([
'title' => strip_tags($request->postData('betreff')),
'text' => $text,
'user_id' => $user->id,
- 'is_meeting' => !!$isMeeting,
+ 'is_meeting' => (bool)$isMeeting,
]);
- engelsystem_log('Created news: ' . $request->postData('betreff') . ', treffen: ' . $isMeeting);
+ engelsystem_log('Created news: ' . $news->title . ', is meeting: ' . ($news->is_meeting ? 'yes' : 'no'));
success(__('Entry saved.'));
redirect(page_link_to('news'));
}