Skip to content

Commit

Permalink
Merge branch 'dev' into updateHandleApiError
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurpar06 authored Jun 13, 2024
2 parents fba7374 + 287b3bf commit 128d52c
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 10 deletions.
14 changes: 14 additions & 0 deletions app/Events/NewsUpdated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Events;

use App\Contracts\Event;
use App\Models\News;

class NewsUpdated extends Event
{
public function __construct(
public News $news
) {
}
}
5 changes: 5 additions & 0 deletions app/Http/Controllers/Admin/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ public function news(Request $request): View
$attrs['user_id'] = Auth::user()->id;

$this->newsSvc->addNews($attrs);
} elseif ($request->isMethod('patch')) {
$attrs = $request->post();
$attrs['user_id'] = Auth::user()->id;

$this->newsSvc->updateNews($attrs);
} elseif ($request->isMethod('delete')) {
$id = $request->input('news_id');
$this->newsSvc->deleteNews($id);
Expand Down
20 changes: 20 additions & 0 deletions app/Notifications/NotificationEventsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Contracts\Listener;
use App\Events\AwardAwarded;
use App\Events\NewsAdded;
use App\Events\NewsUpdated;
use App\Events\PirepAccepted;
use App\Events\PirepFiled;
use App\Events\PirepPrefiled;
Expand Down Expand Up @@ -33,6 +34,7 @@ class NotificationEventsHandler extends Listener
public static $callbacks = [
AwardAwarded::class => 'onAwardAwarded',
NewsAdded::class => 'onNewsAdded',
NewsUpdated::class => 'onNewsUpdated',
PirepPrefiled::class => 'onPirepPrefile',
PirepStatusChange::class => 'onPirepStatusChange',
PirepAccepted::class => 'onPirepAccepted',
Expand Down Expand Up @@ -268,6 +270,24 @@ public function onNewsAdded(NewsAdded $event): void
Notification::send([$event->news], new Messages\Broadcast\NewsAdded($event->news));
}

/**
* Notify all users of a news event, but only the users which have opted in
*
* @param \App\Events\NewsUpdated $event
*/
public function onNewsUpdated(NewsUpdated $event): void
{
Log::info('NotificationEvents::onNewsAdded');
if (setting('notifications.mail_news', true)) {
$this->notifyAllUsers(new Messages\NewsAdded($event->news));
}

/*
* Broadcast notifications
*/
Notification::send([$event->news], new Messages\Broadcast\NewsAdded($event->news));
}

/**
* Notify all users that user has awarded a new award
*
Expand Down
1 change: 1 addition & 0 deletions app/Providers/RouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@ private function mapAdminRoutes()

Route::match([
'get',
'patch',
'post',
'delete',
], 'dashboard/news', ['uses' => 'DashboardController@news'])
Expand Down
34 changes: 33 additions & 1 deletion app/Services/NewsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

use App\Contracts\Service;
use App\Events\NewsAdded;
use App\Events\NewsUpdated;
use App\Models\News;
use App\Repositories\NewsRepository;
use Prettus\Validator\Exceptions\ValidatorException;

class NewsService extends Service
{
Expand All @@ -27,7 +30,36 @@ public function __construct(NewsRepository $newsRepo)
public function addNews(array $attrs)
{
$news = $this->newsRepo->create($attrs);
event(new NewsAdded($news));

if (array_key_exists('send_notifications', $attrs) && get_truth_state($attrs['send_notifications'])) {
event(new NewsAdded($news));
}

return $news;
}

/**
* Update a news
*
* @param array $attrs
*
* @throws ValidatorException
*
* @return ?News
*/
public function updateNews(array $attrs): ?News
{
$news = $this->newsRepo->find($attrs['id']);

if (!$news) {
return null;
}

$news = $this->newsRepo->update($attrs, $attrs['id']);

if (array_key_exists('send_notifications', $attrs) && get_truth_state($attrs['send_notifications'])) {
event(new NewsUpdated($news));
}

return $news;
}
Expand Down
78 changes: 72 additions & 6 deletions resources/views/admin/dashboard/news.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div id="pjax_news_wrapper">
<div class="card border-blue-bottom">
<div class="card border-blue-bottom" id="add_news">
<div class="content">
<div class="header">
<h4 class="title">Add News</h4>
Expand All @@ -15,10 +15,52 @@
<td>{!! Form::textarea('body', '', ['id' => 'news_editor', 'class' => 'editor']) !!}</td>
</tr>
<tr>
<td colspan="2" class="text-right">
{{ Form::button('<i class="fas fa-plus-circle"></i>&nbsp;add', ['type' => 'submit', 'class' => 'btn btn-success btn-s']) }}
</td>
</table>
<div style="display:flex; align-items: center; justify-content: space-between;">
<div class="checkbox">
<label class="checkbox-inline">
{{ Form::label('send_notifications', 'Send notifications:') }}
<input name="send_notifications" type="hidden" value="0"/>
{{ Form::checkbox('send_notifications') }}
</label>
</div>
<div>
{{ Form::button('<i class="fas fa-plus-circle"></i>&nbsp;add', ['type' => 'submit', 'class' => 'btn btn-success btn-s']) }}
</div>
</div>
{{ Form::close() }}
</div>
</div>
<div class="card border-blue-bottom" id="edit_news" style="display:none;">
<div class="content">
<div class="header">
<h4 class="title" id="edit_title">Edit News</h4>
</div>
{{ Form::open(['route' => 'admin.dashboard.news', 'method' => 'patch', 'class' => 'pjax_news_form']) }}
{{ Form::hidden('id', '', ['id' => 'edit_id']) }}
<table class="table">
<tr>
<td>{{ Form::label('subject', 'Subject:') }}</td>
<td>{{ Form::text('subject', '', ['id' => 'edit_subject', 'class' => 'form-control']) }}</td>
</tr>
<tr>
<td>{{ Form::label('body', 'Body:') }}</td>
<td>{!! Form::textarea('body', '', ['id' => 'edit_body', 'class' => 'editor']) !!}</td>
</tr>
</table>
<div style="display:flex; align-items: center; justify-content: space-between;">
<div class="checkbox">
<label class="checkbox-inline">
{{ Form::label('send_notifications', 'Send notifications:') }}
<input name="send_notifications" type="hidden" value="0"/>
{{ Form::checkbox('send_notifications') }}
</label>
</div>
<div>
<button type="button" class="btn btn-warning btn-s" onclick="closeEdit()">Cancel</button>
{{ Form::button('<i class="fas fa-pencil-alt"></i>&nbsp;edit', ['type' => 'submit', 'class' => 'btn btn-success btn-s']) }}
</div>
</div>
{{ Form::close() }}
</div>
</div>
Expand All @@ -43,7 +85,8 @@
<td>{!! $item->body!!}</td>
<td>{{ optional($item->user)->name_private }}</td>
<td>{{ $item->created_at->format('d.M.y') }}</td>
<td>
<td style="display: flex;gap: .5rem;">
<button class="btn btn-primary btn-xs text-small" onclick="editNews({{ $item->toJson() }})">Edit</button>
{{ Form::open(['route' => 'admin.dashboard.news', 'method' => 'delete', 'class' => 'pjax_news_form']) }}
{{ Form::hidden('news_id', $item->id) }}
{{ Form::button('Delete', ['type' => 'submit', 'class' => 'btn btn-danger btn-xs text-small', 'onclick' => "return confirm('Are you sure?')"]) }}
Expand All @@ -55,11 +98,34 @@
@endif
</div>
</div>
<script>
$(document).ready(function () { CKEDITOR.replace('news_editor'); });
if (typeof $('input').iCheck !== 'undefined') {
$('input').iCheck({
checkboxClass: 'icheckbox_square-blue',
radioClass: 'icheckbox_square-blue'
});
}
</script>
</div>
@section('scripts')
@parent
<script src="{{ public_asset('assets/vendor/ckeditor4/ckeditor.js') }}"></script>
<script>
$(document).ready(function () { CKEDITOR.replace('news_editor'); });
function editNews(news) {
CKEDITOR.replace('edit_body')
$('#edit_title').html('Edit News: ' + news.subject);
$('#edit_subject').val(news.subject)
CKEDITOR.instances.edit_body.setData(news.body)
$('#edit_id').val(news.id)
$('#add_news').hide();
$('#edit_news').show();
}
function closeEdit() {
$('#edit_news').hide();
$('#add_news').show()
}
</script>
@endsection
7 changes: 4 additions & 3 deletions tests/NewsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ public function testNewsNotifications(): void
$users_opt_out = User::factory()->count(5)->create(['opt_in' => false]);

$this->newsSvc->addNews([
'user_id' => $users_opt_out[0]->id,
'subject' => 'News Item',
'body' => 'News!',
'user_id' => $users_opt_out[0]->id,
'subject' => 'News Item',
'body' => 'News!',
'send_notifications' => true,
]);

Notification::assertSentTo($users_opt_in, NewsAdded::class);
Expand Down

0 comments on commit 128d52c

Please sign in to comment.