Migrate news to model class

main
Michael Weimann 5 years ago committed by Igor Scheller
parent 8b5b9e2c65
commit 6534191d59

@ -0,0 +1,137 @@
<?php
declare(strict_types=1);
namespace Engelsystem\Migrations;
use Carbon\Carbon;
use Engelsystem\Database\Migration\Migration;
use Illuminate\Database\Schema\Blueprint;
use stdClass;
/**
* This migration creates the news table and copies the existing News table records to the new one.
*/
class CreateNewsTable extends Migration
{
use ChangesReferences, Reference;
/**
* Creates the news table, copies the data and drops the News table.
*/
public function up(): void
{
$hasPreviousNewsTable = $this->schema->hasTable('News');
if ($hasPreviousNewsTable) {
// rename because some SQL DBMS handle identifiers case insensitive
$this->schema->rename('News', 'PreviousNews');
}
$this->createNewNewsTable();
if ($hasPreviousNewsTable) {
$this->copyPreviousToNewNewsTable();
$this->changeReferences(
'PreviousNews',
'ID',
'news',
'id',
'unsignedInteger'
);
$this->schema->drop('PreviousNews');
}
}
/**
* Recreates the previous News table, copies back the data and drops the new news table.
*/
public function down(): void
{
// rename because some SQL DBMS handle identifiers case insensitive
$this->schema->rename('news', 'new_news');
$this->createPreviousNewsTable();
$this->copyNewToPreviousNewsTable();
$this->changeReferences(
'new_news',
'id',
'News',
'ID',
'unsignedInteger'
);
$this->schema->drop('new_news');
}
private function createNewNewsTable(): void
{
$this->schema->create('news', function (Blueprint $table) {
$table->increments('id');
$table->string('title', 150);
$table->text('text');
$table->boolean('is_meeting')->default(0);
$this->referencesUser($table, false);
$table->timestamps();
});
}
private function copyPreviousToNewNewsTable(): void
{
/** @var stdClass[] $previousNewsRecords */
$previousNewsRecords = $this->schema
->getConnection()
->table('PreviousNews')
->get();
foreach ($previousNewsRecords as $previousNews) {
$date = Carbon::createFromTimestamp($previousNews->Datum);
$this->schema->getConnection()->table('news')->insert([
'id' => $previousNews->ID,
'title' => $previousNews->Betreff,
'text' => $previousNews->Text,
'is_meeting' => $previousNews->Treffen,
'user_id' => $previousNews->UID,
'created_at' => $date,
'updated_at' => $date,
]);
}
}
private function createPreviousNewsTable(): void
{
$this->schema->create('News', function (Blueprint $table) {
$table->increments('ID');
$table->integer('Datum');
$table->string('Betreff', 150)
->default('');
$table->text('Text');
$table->boolean('Treffen');
$table->unsignedInteger('UID');
$table->foreign('UID')
->references('id')
->on('users');
});
}
private function copyNewToPreviousNewsTable(): void
{
/** @var stdClass[] $newsRecords */
$newsRecords = $this->schema
->getConnection()
->table('new_news')
->get();
foreach ($newsRecords as $newsRecord) {
$date = Carbon::createFromFormat('Y-m-d H:i:s', $newsRecord->created_at)
->getTimestamp();
$this->schema->getConnection()->table('News')->insert([
'ID' => $newsRecord->id,
'Datum' => $date,
'Betreff' => $newsRecord->title,
'Text' => $newsRecord->text,
'UID' => $newsRecord->user_id,
'Treffen' => $newsRecord->is_meeting,
]);
}
}
}

@ -1,7 +1,6 @@
<?php <?php
use Engelsystem\Database\DB; use Engelsystem\Models\News\News;
use Engelsystem\Models\User\User;
/** /**
* @return string * @return string
@ -22,17 +21,17 @@ function admin_news()
return error('Incomplete call, missing News ID.', true); return error('Incomplete call, missing News ID.', true);
} }
$news = DB::selectOne('SELECT * FROM `News` WHERE `ID`=? LIMIT 1', [$news_id]); $news = News::find($news_id);
if (empty($news)) { if (empty($news)) {
return error('No News found.', true); return error('No News found.', true);
} }
switch ($request->input('action')) { switch ($request->input('action')) {
case 'edit': case 'edit':
$user_source = User::find($news['UID']); $user_source = $news->user;
if ( if (
!auth()->can('admin_news_html') !auth()->can('admin_news_html')
&& strip_tags($news['Text']) != $news['Text'] && strip_tags($news->text) != $news->text
) { ) {
$html .= warning( $html .= warning(
__('This message contains HTML. After saving the post some formatting will be lost!'), __('This message contains HTML. After saving the post some formatting will be lost!'),
@ -42,11 +41,11 @@ function admin_news()
$html .= form( $html .= form(
[ [
form_info(__('Date'), date('Y-m-d H:i', $news['Datum'])), form_info(__('Date'), $news->created_at->format('Y-m-d H:i')),
form_info(__('Author'), User_Nick_render($user_source)), form_info(__('Author'), User_Nick_render($user_source)),
form_text('eBetreff', __('Subject'), $news['Betreff']), form_text('eBetreff', __('Subject'), $news->title),
form_textarea('eText', __('Message'), $news['Text']), form_textarea('eText', __('Message'), $news->text),
form_checkbox('eTreffen', __('Meeting'), $news['Treffen'] == 1, 1), form_checkbox('eTreffen', __('Meeting'), $news->is_meeting === true, 1),
form_submit('submit', __('Save')) form_submit('submit', __('Save'))
], ],
page_link_to('admin_news', ['action' => 'save', 'id' => $news_id]) page_link_to('admin_news', ['action' => 'save', 'id' => $news_id])
@ -65,24 +64,10 @@ function admin_news()
$text = strip_tags($text); $text = strip_tags($text);
} }
DB::update(' $news->title = strip_tags($request->postData('eBetreff'));
UPDATE `News` SET $news->text = $text;
`Datum`=?, $news->is_meeting = $request->has('eTreffen');
`Betreff`=?, $news->save();
`Text`=?,
`UID`=?,
`Treffen`=?
WHERE `ID`=?
',
[
time(),
strip_tags($request->postData('eBetreff')),
$text,
$user->id,
$request->has('eTreffen') ? 1 : 0,
$news_id
]
);
engelsystem_log('News updated: ' . $request->postData('eBetreff')); engelsystem_log('News updated: ' . $request->postData('eBetreff'));
success(__('News entry updated.')); success(__('News entry updated.'));
@ -90,8 +75,8 @@ function admin_news()
break; break;
case 'delete': case 'delete':
DB::delete('DELETE FROM `News` WHERE `ID`=? LIMIT 1', [$news_id]); $news->delete();
engelsystem_log('News deleted: ' . $news['Betreff']); engelsystem_log('News deleted: ' . $news->title);
success(__('News entry deleted.')); success(__('News entry deleted.'));
redirect(page_link_to('news')); redirect(page_link_to('news'));
break; break;

@ -1,6 +1,7 @@
<?php <?php
use Engelsystem\Database\DB; use Engelsystem\Database\DB;
use Engelsystem\Models\News\News;
use Engelsystem\Models\User\User; use Engelsystem\Models\User\User;
/** /**
@ -42,20 +43,17 @@ function user_meetings()
$page = 0; $page = 0;
} }
$news = DB::select(sprintf(' $news = News::where('is_meeting', true)
SELECT * ->orderBy('created_at', 'DESC')
FROM `News` ->limit($display_news)
WHERE `Treffen`=1 ->offset($page * $display_news)
ORDER BY `Datum`DESC ->get();
LIMIT %u, %u',
$page * $display_news,
$display_news
));
foreach ($news as $entry) { foreach ($news as $entry) {
$html .= display_news($entry); $html .= display_news($entry);
} }
$dis_rows = ceil(count(DB::select('SELECT `ID` FROM `News`')) / $display_news); $dis_rows = ceil(News::where('is_meeting', true)->count() / $display_news);
$html .= '<div class="text-center">' . '<ul class="pagination">'; $html .= '<div class="text-center">' . '<ul class="pagination">';
for ($i = 0; $i < $dis_rows; $i++) { for ($i = 0; $i < $dis_rows; $i++) {
if ($request->has('page') && $i == $request->input('page', 0)) { if ($request->has('page') && $i == $request->input('page', 0)) {
@ -75,28 +73,28 @@ function user_meetings()
/** /**
* Renders the text content of a news entry * Renders the text content of a news entry
* *
* @param array $news * @param News $news
* @return string HTML * @return string HTML
*/ */
function news_text($news) function news_text(News $news): string
{ {
$text = ReplaceSmilies($news['Text']); $text = ReplaceSmilies($news->text);
$text = preg_replace("/\r\n\r\n/m", '<br><br>', $text); $text = preg_replace("/\r\n\r\n/m", '<br><br>', $text);
return $text; return $text;
} }
/** /**
* @param array $news * @param News $news
* @return string * @return string
*/ */
function display_news($news) function display_news(News $news): string
{ {
global $page; global $page;
$html = ''; $html = '';
$html .= '<div class="panel' . ($news['Treffen'] == 1 ? ' panel-info' : ' panel-default') . '">'; $html .= '<div class="panel' . ($news->is_meeting ? ' panel-info' : ' panel-default') . '">';
$html .= '<div class="panel-heading">'; $html .= '<div class="panel-heading">';
$html .= '<h3 class="panel-title">' . ($news['Treffen'] == 1 ? '[Meeting] ' : '') . ReplaceSmilies($news['Betreff']) . '</h3>'; $html .= '<h3 class="panel-title">' . ($news->is_meeting ? '[Meeting] ' : '') . ReplaceSmilies($news->title) . '</h3>';
$html .= '</div>'; $html .= '</div>';
$html .= '<div class="panel-body">' . news_text($news) . '</div>'; $html .= '<div class="panel-body">' . news_text($news) . '</div>';
@ -104,21 +102,21 @@ function display_news($news)
if (auth()->can('admin_news')) { if (auth()->can('admin_news')) {
$html .= '<div class="pull-right">' $html .= '<div class="pull-right">'
. button_glyph( . button_glyph(
page_link_to('admin_news', ['action' => 'edit', 'id' => $news['ID']]), page_link_to('admin_news', ['action' => 'edit', 'id' => $news->id]),
'edit', 'edit',
'btn-xs' 'btn-xs'
) )
. '</div>'; . '</div>';
} }
$html .= '<span class="glyphicon glyphicon-time"></span> ' . date('Y-m-d H:i', $news['Datum']) . '&emsp;'; $html .= '<span class="glyphicon glyphicon-time"></span> ' . $news->created_at->format('Y-m-d H:i') . '&emsp;';
$html .= User_Nick_render(User::find($news['UID'])); $html .= User_Nick_render(User::find($news->user_id));
if ($page != 'news_comments') { if ($page != 'news_comments') {
$html .= '&emsp;<a href="' . page_link_to('news_comments', ['nid' => $news['ID']]) . '">' $html .= '&emsp;<a href="' . page_link_to('news_comments', ['nid' => $news->id]) . '">'
. '<span class="glyphicon glyphicon-comment"></span> ' . '<span class="glyphicon glyphicon-comment"></span> '
. __('Comments') . ' &raquo;</a> ' . __('Comments') . ' &raquo;</a> '
. '<span class="badge">' . '<span class="badge">'
. count(DB::select('SELECT `ID` FROM `NewsComments` WHERE `Refid`=?', [$news['ID']])) . count(DB::select('SELECT `ID` FROM `NewsComments` WHERE `Refid`=?', [$news->id]))
. '</span>'; . '</span>';
} }
$html .= '</div>'; $html .= '</div>';
@ -135,13 +133,13 @@ function user_news_comments()
$request = request(); $request = request();
$html = '<div class="col-md-12"><h1>' . user_news_comments_title() . '</h1>'; $html = '<div class="col-md-12"><h1>' . user_news_comments_title() . '</h1>';
$nid = $request->input('nid');
if ( if (
$request->has('nid') $request->has('nid')
&& preg_match('/^\d{1,}$/', $request->input('nid')) && preg_match('/^\d{1,}$/', $nid)
&& count(DB::select('SELECT `ID` FROM `News` WHERE `ID`=? LIMIT 1', [$request->input('nid')])) > 0 && News::where('id', $request->input('nid'))->count() > 0
) { ) {
$nid = $request->input('nid'); $news = News::find('id');
$news = DB::selectOne('SELECT * FROM `News` WHERE `ID`=? LIMIT 1', [$nid]);
if ($request->hasPostData('submit') && $request->has('text')) { if ($request->hasPostData('submit') && $request->has('text')) {
$text = $request->input('text'); $text = $request->input('text');
DB::insert(' DB::insert('
@ -212,18 +210,12 @@ function user_news()
$text = strip_tags($text); $text = strip_tags($text);
} }
DB::insert(' News::create([
INSERT INTO `News` (`Datum`, `Betreff`, `Text`, `UID`, `Treffen`) 'title' => strip_tags($request->postData('betreff')),
VALUES (?, ?, ?, ?, ?) 'text' => $text,
', 'user_id' => $user->id,
[ 'is_meeting' => !!$isMeeting,
time(), ]);
strip_tags($request->postData('betreff')),
$text,
$user->id,
$isMeeting,
]
);
engelsystem_log('Created news: ' . $request->postData('betreff') . ', treffen: ' . $isMeeting); engelsystem_log('Created news: ' . $request->postData('betreff') . ', treffen: ' . $isMeeting);
success(__('Entry saved.')); success(__('Entry saved.'));
@ -236,20 +228,17 @@ function user_news()
$page = 0; $page = 0;
} }
$news = DB::select(sprintf(' $news = News::query()
SELECT * ->orderBy('created_at', 'DESC')
FROM `News` ->limit($display_news)
ORDER BY `Datum` ->offset($page * $display_news)
DESC LIMIT %u, %u ->get();
',
$page * $display_news,
$display_news
));
foreach ($news as $entry) { foreach ($news as $entry) {
$html .= display_news($entry); $html .= display_news($entry);
} }
$dis_rows = ceil(count(DB::select('SELECT `ID` FROM `News`')) / $display_news); $dis_rows = ceil(News::query()->count() / $display_news);
$html .= '<div class="text-center">' . '<ul class="pagination">'; $html .= '<div class="text-center">' . '<ul class="pagination">';
for ($i = 0; $i < $dis_rows; $i++) { for ($i = 0; $i < $dis_rows; $i++) {
if ($request->has('page') && $i == $request->input('page', 0)) { if ($request->has('page') && $i == $request->input('page', 0)) {

@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace Engelsystem\Models\News;
use Engelsystem\Models\User\UsesUserModel;
use Illuminate\Database\Eloquent\Model;
/**
* This class represents a news item.
*/
class News extends Model
{
use UsesUserModel;
protected $casts = [
'is_meeting' => 'boolean',
];
protected $attributes = [
'is_meeting' => false,
];
protected $fillable = [
'title',
'text',
'is_meeting',
'user_id',
];
}

@ -4,6 +4,8 @@ namespace Engelsystem\Models\User;
use Carbon\Carbon; use Carbon\Carbon;
use Engelsystem\Models\BaseModel; use Engelsystem\Models\BaseModel;
use Engelsystem\Models\News\News;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne; use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Query\Builder as QueryBuilder; use Illuminate\Database\Query\Builder as QueryBuilder;
@ -95,4 +97,12 @@ class User extends BaseModel
->hasOne(State::class) ->hasOne(State::class)
->withDefault(); ->withDefault();
} }
/**
* @return HasMany
*/
public function news(): HasMany
{
return $this->hasMany(News::class);
}
} }

@ -0,0 +1,80 @@
<?php
declare(strict_types=1);
use Engelsystem\Models\News\News;
use Engelsystem\Models\User\User;
use Engelsystem\Test\Unit\HasDatabase;
use Engelsystem\Test\Unit\TestCase;
/**
* This class provides tests for the News model.
*/
class NewsTest extends TestCase
{
use HasDatabase;
/**
* @var array
*/
private $newsData;
/**
* @var User
*/
private $user;
/**
* @return void
*/
protected function setUp(): void
{
parent::setUp();
$this->initDatabase();
$this->user = User::make([
'name' => 'lorem',
'password' => '',
'email' => 'foo@bar.batz',
'api_key' => '',
]);
$this->user->save();
$this->newsData = [
'title' => 'test title',
'text' => 'test text',
'user_id' => $this->user->id
];
}
/**
* Tests that creating a News item with default values works.
*
* @return void
*/
public function testCreateDefault(): void
{
$news = News::create($this->newsData);
$this->assertSame(1, $news->id);
$this->assertSame($this->newsData['title'], $news->title);
$this->assertSame($this->newsData['text'], $news->text);
$this->assertFalse($news->is_meeting);
}
/**
* Tests that creating a News item with all fill values works.
*
* @return void
*/
public function testCreate(): void
{
$news = News::create(
$this->newsData + ['is_meeting' => true,]
);
$this->assertSame(1, $news->id);
$this->assertSame($this->newsData['title'], $news->title);
$this->assertSame($this->newsData['text'], $news->text);
$this->assertTrue($news->is_meeting);
}
}

@ -3,6 +3,7 @@
namespace Engelsystem\Test\Unit\Models; namespace Engelsystem\Test\Unit\Models;
use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts; use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
use Engelsystem\Models\News\News;
use Engelsystem\Models\User\Contact; use Engelsystem\Models\User\Contact;
use Engelsystem\Models\User\HasUserModel; use Engelsystem\Models\User\HasUserModel;
use Engelsystem\Models\User\PersonalData; use Engelsystem\Models\User\PersonalData;
@ -25,6 +26,15 @@ class UserTest extends TestCase
'api_key' => '', 'api_key' => '',
]; ];
/**
* Prepare test
*/
protected function setUp(): void
{
parent::setUp();
$this->initDatabase();
}
/** /**
* @return array * @return array
*/ */
@ -93,11 +103,51 @@ class UserTest extends TestCase
} }
/** /**
* Prepare test * @covers User::news()
*
* @dataProvider hasManyRelationsProvider
*
* @param string $class Class name of the related models
* @param string $name Name of the accessor for the related models
* @param array $data List of the related models
*/ */
protected function setUp(): void public function testHasManyRelations(string $class, string $name, array $data): void
{ {
parent::setUp(); $user = new User($this->data);
$this->initDatabase(); $user->save();
$relatedModelIds = [];
foreach ($data as $d) {
$stored = $class::create($d + ['user_id' => $user->id]);
$relatedModelIds[] = $stored->id;
}
$this->assertEquals($relatedModelIds, $user->{$name}->modelKeys());
}
/**
* @return array
*/
public function hasManyRelationsProvider(): array
{
return [
'news' => [
News::class,
'news',
[
[
'title' => 'Hey hoo',
'text' => 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit.',
'is_meeting' => false,
],
[
'title' => 'Huuhuuu',
'text' => 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit.',
'is_meeting' => true,
],
]
]
];
} }
} }

Loading…
Cancel
Save