diff --git a/db/migrations/2019_10_15_000000_create_news_table.php b/db/migrations/2019_10_15_000000_create_news_table.php new file mode 100644 index 00000000..d6b93265 --- /dev/null +++ b/db/migrations/2019_10_15_000000_create_news_table.php @@ -0,0 +1,137 @@ +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, + ]); + } + } +} diff --git a/includes/pages/admin_news.php b/includes/pages/admin_news.php index 21245eb9..75d8291e 100644 --- a/includes/pages/admin_news.php +++ b/includes/pages/admin_news.php @@ -1,7 +1,6 @@ input('action')) { case 'edit': - $user_source = User::find($news['UID']); + $user_source = $news->user; if ( !auth()->can('admin_news_html') - && strip_tags($news['Text']) != $news['Text'] + && strip_tags($news->text) != $news->text ) { $html .= warning( __('This message contains HTML. After saving the post some formatting will be lost!'), @@ -42,11 +41,11 @@ function admin_news() $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_text('eBetreff', __('Subject'), $news['Betreff']), - form_textarea('eText', __('Message'), $news['Text']), - form_checkbox('eTreffen', __('Meeting'), $news['Treffen'] == 1, 1), + form_text('eBetreff', __('Subject'), $news->title), + form_textarea('eText', __('Message'), $news->text), + form_checkbox('eTreffen', __('Meeting'), $news->is_meeting === true, 1), form_submit('submit', __('Save')) ], page_link_to('admin_news', ['action' => 'save', 'id' => $news_id]) @@ -65,24 +64,10 @@ function admin_news() $text = strip_tags($text); } - DB::update(' - UPDATE `News` SET - `Datum`=?, - `Betreff`=?, - `Text`=?, - `UID`=?, - `Treffen`=? - WHERE `ID`=? - ', - [ - time(), - strip_tags($request->postData('eBetreff')), - $text, - $user->id, - $request->has('eTreffen') ? 1 : 0, - $news_id - ] - ); + $news->title = strip_tags($request->postData('eBetreff')); + $news->text = $text; + $news->is_meeting = $request->has('eTreffen'); + $news->save(); engelsystem_log('News updated: ' . $request->postData('eBetreff')); success(__('News entry updated.')); @@ -90,8 +75,8 @@ function admin_news() break; case 'delete': - DB::delete('DELETE FROM `News` WHERE `ID`=? LIMIT 1', [$news_id]); - engelsystem_log('News deleted: ' . $news['Betreff']); + $news->delete(); + engelsystem_log('News deleted: ' . $news->title); success(__('News entry deleted.')); redirect(page_link_to('news')); break; diff --git a/includes/pages/user_news.php b/includes/pages/user_news.php index 39ce24a5..f67896da 100644 --- a/includes/pages/user_news.php +++ b/includes/pages/user_news.php @@ -1,6 +1,7 @@ orderBy('created_at', 'DESC') + ->limit($display_news) + ->offset($page * $display_news) + ->get(); + foreach ($news as $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 .= '