Added important news (#1096)

This commit is contained in:
xuwhite 2023-02-13 21:19:45 +01:00 committed by GitHub
parent 713f8222e4
commit 6fcb656299
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 176 additions and 51 deletions

View File

@ -16,11 +16,12 @@ class NewsFactory extends Factory
public function definition(): array
{
return [
'title' => $this->faker->text(50),
'text' => $this->faker->realText(),
'is_meeting' => $this->faker->boolean(),
'is_pinned' => $this->faker->boolean(.1),
'user_id' => User::factory(),
'title' => $this->faker->text(50),
'text' => $this->faker->realText(),
'is_meeting' => $this->faker->boolean(),
'is_pinned' => $this->faker->boolean(.1),
'is_important' => $this->faker->boolean(.1),
'user_id' => User::factory(),
];
}
}

View File

@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
namespace Engelsystem\Migrations;
use Engelsystem\Database\Migration\Migration;
class AddSetNewsFlagImportantPermissions extends Migration
{
/**
* Run the migration
*/
public function up(): void
{
$db = $this->schema->getConnection();
$db->table('privileges')
->insert(['name' => 'news.important', 'description' => 'Make News Important']);
$newsImportant = $db->table('privileges')
->where('name', 'news.important')
->get(['id'])
->first();
$buerocrat = 80;
$db->table('group_privileges')
->insertOrIgnore([
['group_id' => $buerocrat, 'privilege_id' => $newsImportant->id],
]);
}
/**
* Reverse the migration
*/
public function down(): void
{
$db = $this->schema->getConnection();
$db->table('privileges')
->where(['name' => 'news.important'])
->delete();
}
}

View File

@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace Engelsystem\Migrations;
use Engelsystem\Database\Migration\Migration;
use Illuminate\Database\Schema\Blueprint;
class AddIsImportantToNews extends Migration
{
/**
* Run the migration
*/
public function up(): void
{
$this->schema->table('news', function (Blueprint $table): void {
$table->boolean('is_important')->default(false)->after('is_pinned');
});
}
/**
* Reverse the migration
*/
public function down(): void
{
$this->schema->table('news', function (Blueprint $table): void {
$table->dropColumn('is_important');
});
}
}

View File

@ -1,6 +1,7 @@
<?php
use Engelsystem\Models\AngelType;
use Engelsystem\Models\News;
use Engelsystem\Models\Shifts\Shift;
use Engelsystem\ShiftsFilter;
@ -55,9 +56,14 @@ function public_dashboard_controller()
}
}
$important_news = News::whereIsImportant(true)
->orderBy('updated_at')
->limit(1)
->get();
return [
__('Public Dashboard'),
public_dashboard_view($stats, $free_shifts),
public_dashboard_view($stats, $free_shifts, $important_news),
];
}

View File

@ -1,15 +1,27 @@
<?php
use Engelsystem\Models\News;
use Illuminate\Support\Collection;
/**
* Public dashboard (formerly known as angel news hub)
*
* @param array $stats
* @param array[] $free_shifts
* @param array $stats
* @param array[] $free_shifts
* @param News[]|Collection $important_news
* @return string
*/
function public_dashboard_view($stats, $free_shifts)
function public_dashboard_view($stats, $free_shifts, $important_news)
{
$needed_angels = '';
$news = '';
if ($important_news->isNotEmpty()) {
$first_news = $important_news->first();
$news = div('alert alert-warning text-center', [
'<a href="' . url('/news/' . $first_news->id) . '"><strong>' . $first_news->title . '</strong></a>',
]);
}
if (count($free_shifts) > 0) {
$shift_panels = [
'<div class="row">',
@ -42,6 +54,7 @@ function public_dashboard_view($stats, $free_shifts)
stats(__('Angels currently working'), $stats['angels-working'], 'default'),
stats(__('Hours to be worked'), $stats['hours-to-work'], 'default'),
], 'statistics'),
$news,
$needed_angels,
], 'public-dashboard'),
]),

View File

@ -137,6 +137,10 @@ table a > .icon-icon_angel {
}
}
.public-dashboard .alert a {
text-decoration: inherit;
}
.dashboard-card {
position: relative;
font-size: 20px;

View File

@ -2910,6 +2910,9 @@ msgstr "+"
msgid "news.is_meeting"
msgstr "[Treffen]"
msgid "news.edit.is_important"
msgstr "Wichtig"
msgid "news.read_more"
msgstr "Weiterlesen"

View File

@ -187,6 +187,9 @@ msgstr "Subject"
msgid "news.edit.is_meeting"
msgstr "Meeting"
msgid "news.edit.is_important"
msgstr "Important"
msgid "news.edit.is_pinned"
msgstr "Pin to top"

View File

@ -43,6 +43,7 @@
<div class="col-md-6">
{{ f.checkbox('is_meeting', __('news.edit.is_meeting'), is_meeting) }}
{{ f.checkbox('is_pinned', __('news.edit.is_pinned'), is_pinned) }}
{% if has_permission_to('news.important') %} {{ f.checkbox('is_important', __('news.edit.is_important'), is_important) }}{% endif %}
</div>
</div>
@ -67,7 +68,7 @@
<div class="col-md-12">
<h2>{{ __('form.preview') }}</h2>
<div class="card {% if news.is_meeting %}bg-info{% else %}{% if theme.type =='light' %}bg-light{% else %}bg-secondary{% endif %}{% endif %} mb-4">
<div class="card {% if news.is_important %}bg-warning{% elseif news.is_meeting %}bg-info{% elseif theme.type =='light' %}bg-light{% else %}bg-secondary{% endif %} mb-4">
<div class="card-header {% if news.is_meeting and theme.type == 'dark' %}text-white{% endif %}">
{% if news.is_meeting %}{{ __('news.is_meeting') }}{% endif %}
{{ news.title }}

View File

@ -48,7 +48,7 @@
{% endblock %}
{% macro news(news, show_comments_link, is_overview) %}
<div class="card {% if news.is_meeting %}bg-info{% else %}{% if theme.type =='light' %}bg-light{% else %}bg-dark{% endif %}{% endif %} mb-4">
<div class="card {% if news.is_important %}bg-warning{% elseif news.is_meeting %}bg-info{% elseif theme.type =='light' %}bg-light{% else %}bg-dark{% endif %} mb-4">
{% if is_overview|default(false) %}
<div class="card-header {% if news.is_meeting and theme.type == 'dark' %}text-white{% endif %}">
<a href="{{ url('news/' ~ news.id) }}" class="text-inherit">

View File

@ -46,9 +46,10 @@ class NewsController extends BaseController
return $this->response->withView(
'pages/news/edit.twig',
[
'news' => $news,
'is_meeting' => $news ? $news->is_meeting : $isMeetingDefault,
'is_pinned' => $news ? $news->is_pinned : false,
'news' => $news,
'is_meeting' => $news ? $news->is_meeting : $isMeetingDefault,
'is_pinned' => $news ? $news->is_pinned : false,
'is_important' => $news ? $news->is_important : false,
],
);
}
@ -61,12 +62,13 @@ class NewsController extends BaseController
$news = $this->news->findOrNew($newsId);
$data = $this->validate($request, [
'title' => 'required',
'text' => 'required',
'is_meeting' => 'optional|checked',
'is_pinned' => 'optional|checked',
'delete' => 'optional|checked',
'preview' => 'optional|checked',
'title' => 'required',
'text' => 'required',
'is_meeting' => 'optional|checked',
'is_pinned' => 'optional|checked',
'is_important' => 'optional|checked',
'delete' => 'optional|checked',
'preview' => 'optional|checked',
]);
if (!is_null($data['delete'])) {
@ -93,6 +95,10 @@ class NewsController extends BaseController
$news->is_meeting = !is_null($data['is_meeting']);
$news->is_pinned = !is_null($data['is_pinned']);
if ($this->auth->can('news.important')) {
$news->is_important = !is_null($data['is_important']);
}
if (!is_null($data['preview'])) {
return $this->showEdit($news);
}
@ -107,10 +113,11 @@ class NewsController extends BaseController
$this->log->info(
'Updated {pinned}{type} "{news}": {text}',
[
'pinned' => $news->is_pinned ? 'pinned ' : '',
'type' => $news->is_meeting ? 'meeting' : 'news',
'news' => $news->title,
'text' => $news->text,
'pinned' => $news->is_pinned ? 'pinned ' : '',
'important' => $news->is_important ? 'important ' : '',
'type' => $news->is_meeting ? 'meeting' : 'news',
'news' => $news->title,
'text' => $news->text,
]
);

View File

@ -134,6 +134,7 @@ class NewsController extends BaseController
->with('user')
->withCount('comments')
->orderByDesc('is_pinned')
->orderByDesc('is_important')
->orderByDesc('updated_at')
->limit($perPage)
->offset(($page - 1) * $perPage)

View File

@ -18,19 +18,21 @@ use Illuminate\Support\Str;
* @property string $text
* @property bool $is_meeting
* @property bool $is_pinned
* @property bool $is_important
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
*
* @property-read Collection|NewsComment[] $comments
* @property-read int|null $comments_count
*
* @method static QueryBuilder|LogEntry[] whereId($value)
* @method static QueryBuilder|LogEntry[] whereTitle($value)
* @method static QueryBuilder|LogEntry[] whereText($value)
* @method static QueryBuilder|LogEntry[] whereIsMeeting($value)
* @method static QueryBuilder|LogEntry[] whereIsPinned($value)
* @method static QueryBuilder|LogEntry[] whereCreatedAt($value)
* @method static QueryBuilder|LogEntry[] whereUpdatedAt($value)
* @method static QueryBuilder|News[] whereId($value)
* @method static QueryBuilder|News[] whereTitle($value)
* @method static QueryBuilder|News[] whereText($value)
* @method static QueryBuilder|News[] whereIsMeeting($value)
* @method static QueryBuilder|News[] whereIsPinned($value)
* @method static QueryBuilder|News[] whereIsImportant($value)
* @method static QueryBuilder|News[] whereCreatedAt($value)
* @method static QueryBuilder|News[] whereUpdatedAt($value)
*/
class News extends BaseModel
{
@ -42,15 +44,17 @@ class News extends BaseModel
/** @var array<string, string> */
protected $casts = [ // phpcs:ignore
'user_id' => 'integer',
'is_meeting' => 'boolean',
'is_pinned' => 'boolean',
'user_id' => 'integer',
'is_meeting' => 'boolean',
'is_pinned' => 'boolean',
'is_important' => 'boolean',
];
/** @var array<string, bool> Default attributes */
protected $attributes = [ // phpcs:ignore
'is_meeting' => false,
'is_pinned' => false,
'is_meeting' => false,
'is_pinned' => false,
'is_important' => false,
];
/** @var array<string> */
@ -59,6 +63,7 @@ class News extends BaseModel
'text',
'is_meeting',
'is_pinned',
'is_important',
'user_id',
];

View File

@ -23,10 +23,10 @@ use Illuminate\Database\Query\Builder as QueryBuilder;
* @property-read AngelType $angelType
* @property-read User $confirmUser
*
* @method static QueryBuilder|AngelType[] whereId($value)
* @method static QueryBuilder|AngelType[] whereAngelTypeId($value)
* @method static QueryBuilder|AngelType[] whereConfirmUserId($value)
* @method static QueryBuilder|AngelType[] whereSupporter($value)
* @method static QueryBuilder|UserAngelType[] whereId($value)
* @method static QueryBuilder|UserAngelType[] whereAngelTypeId($value)
* @method static QueryBuilder|UserAngelType[] whereConfirmUserId($value)
* @method static QueryBuilder|UserAngelType[] whereSupporter($value)
*/
class UserAngelType extends Pivot
{

View File

@ -158,11 +158,12 @@ class NewsControllerTest extends ControllerTest
{
$this->request->attributes->set('news_id', 1);
$this->request = $this->request->withParsedBody([
'title' => 'New title',
'text' => 'New text',
'is_meeting' => '1',
'is_pinned' => '1',
'preview' => '1',
'title' => 'New title',
'text' => 'New text',
'is_meeting' => '1',
'is_pinned' => '1',
'is_important' => '1',
'preview' => '1',
]);
$this->response->expects($this->once())
->method('withView')
@ -174,11 +175,16 @@ class NewsControllerTest extends ControllerTest
// Contains new text
$this->assertTrue($news->is_meeting);
$this->assertTrue($news->is_pinned);
$this->assertTrue($news->is_important);
$this->assertEquals('New title', $news->title);
$this->assertEquals('New text', $news->text);
return $this->response;
});
$this->auth->expects($this->atLeastOnce())
->method('can')
->with('news.important')
->willReturn(true);
/** @var NewsController $controller */
$controller = $this->app->make(NewsController::class);
@ -192,6 +198,7 @@ class NewsControllerTest extends ControllerTest
$this->assertEquals('**foo**', $news->text);
$this->assertFalse($news->is_meeting);
$this->assertFalse($news->is_pinned);
$this->assertFalse($news->is_important);
}
/**

View File

@ -39,11 +39,12 @@ class NewsControllerTest extends ControllerTest
'user_id' => 1,
],
[
'title' => 'baz',
'text' => 'baz',
'is_meeting' => true,
'is_pinned' => true,
'user_id' => 1,
'title' => 'baz',
'text' => 'baz',
'is_meeting' => true,
'is_pinned' => true,
'is_important' => true,
'user_id' => 1,
],
[
'title' => 'Lorem',

View File

@ -28,7 +28,7 @@ class PrivilegeTest extends ModelTest
$model->groups()->attach($group2);
/** @var Privilege $savedModel */
$savedModel = Privilege::first();
$savedModel = Privilege::whereName('Some Privilege')->first();
$this->assertEquals('Some Privilege', $savedModel->name);
$this->assertEquals('Some long description', $savedModel->description);
$this->assertEquals($group1->name, $savedModel->groups[0]->name);