Skip to content

Commit

Permalink
feat: calendar for prompts/news/sales
Browse files Browse the repository at this point in the history
  • Loading branch information
ScuffedNewt committed Aug 15, 2023
1 parent 5cd820c commit 36dcdd7
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 0 deletions.
83 changes: 83 additions & 0 deletions app/Http/Controllers/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,87 @@ public function getCreditsPage() {
'extensions' => DB::table('site_extensions')->get(),
]);
}

/**
* Shows the site calendar page.
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function getCalendarPage() {
return view('pages.calendar');
}

/**
* Returns all events from news, sales and prompts formatted as json.
*
* @return \Illuminate\Http\JsonResponse
*/
public function getCalendarEvents() {
$start_at = request()->get('start');
$end_at = request()->get('end');

// get all prompts within time frame, if hide_before_start is set or hide_after_end is set, filter them out if they are not within the prompt time frame
$prompts = \App\Models\Prompt\Prompt::where('is_active', 1)->where('start_at', '>', $start_at)->where('end_at', '<', $end_at)
->where(function ($query) {
$query->where('hide_before_start', 0)->orWhere('hide_after_end', 0)
->orWhere(function ($query) {
$query->where('hide_before_start', 1)->where('start_at', '<=', now());
})
->orWhere(function ($query) {
$query->where('hide_after_end', 1)->where('end_at', '>=', now());
});
})->sortStart()->get();
// get all news as long as either created_at is within timeframe, or if post_at is set, if post_at is within timeframe
$news = \App\Models\News::visible()->where(function ($query) use ($start_at, $end_at) {
$query->where('created_at', '>', $start_at)->where('created_at', '<', $end_at)
->orWhere(function ($query) use ($start_at, $end_at) {
$query->where('post_at', '>', $start_at)->where('post_at', '<', $end_at);
});
})->get();
// get all sales as long as either created_at is within timeframe, or if post_at is set, if post_at is within timeframe
$sales = \App\Models\Sales\Sales::visible()->where(function ($query) use ($start_at, $end_at) {
$query->where('created_at', '>', $start_at)->where('created_at', '<', $end_at)
->orWhere(function ($query) use ($start_at, $end_at) {
$query->where('post_at', '>', $start_at)->where('post_at', '<', $end_at);
});
})->get();

$array = [];
foreach ($prompts as $prompt) {
$array[] = [
'id' => $prompt->id,
'title' => $prompt->name,
'url' => $prompt->url,
'start' => $prompt->start_at->toW3cString(),
'end' => $prompt->end_at->toW3cString(),
'color' => '#80c9e8',
];
}
// set start to start of 'post_at' day, if post_at is set, else use created_at
// set end to end of 'post_at' day, if post_at is set, else use created_at
foreach ($news as $news_item) {
$array[] = [
'id' => $news_item->id,
'title' => $news_item->title,
'url' => $news_item->url,
'start' => $news_item->post_at ? $news_item->post_at->startOfDay()->toW3cString() : $news_item->created_at->startOfDay()->toW3cString(),
'allDay' => "true",
'color' => '#ad283e',
];
}
foreach ($sales as $sale) {
$array[] = [
'id' => $sale->id,
'title' => $sale->title,
'url' => $sale->url,
'start' => $sale->post_at ? $sale->post_at->startOfDay()->toW3cString() : $sale->created_at->startOfDay()->toW3cString(),
// if there is a comment_open_at set, set end to that, else set end to start of next day
'end' => $sale->comments_open_at ? $sale->comments_open_at->endOfDay()->toW3cString() : null,
'allDay' => "true",
'color' => '#f5a623',
];
}

return response()->json($array);
}
}
18 changes: 18 additions & 0 deletions app/Models/News.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,22 @@ public function toFeedItem(): FeedItem {
'authorName' => $this->user->name,
]);
}

/**
* Returns news in a JSON format for the calendar.
*
* @return array
*/
public static function getCalendarEvents() {
$events = [];
foreach (self::visible()->get() as $news) {
$events[] = [
'title' => $news->title,
'start' => $news->post_at->toDateString(),
'url' => $news->url,
];
}

return $events;
}
}
6 changes: 6 additions & 0 deletions public/js/calendar.min.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions resources/views/layouts/_nav.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@
Activity
</a>
<div class="dropdown-menu" aria-labelledby="queueDropdown">
<a class="dropdown-item" href="{{ url('calendar') }}">
Calendar
</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="{{ url('submissions') }}">
Prompt Submissions
</a>
Expand Down
46 changes: 46 additions & 0 deletions resources/views/pages/calendar.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
@extends('world.layout')

@section('title')
Calendar
@endsection

@section('sidebar')
<ul>
<li class="sidebar-header"><a href="{{ url('/calendar') }}" class="card-link">Calendar</a></li>
<li class="sidebar-section">
<div class="sidebar-section-header">Items</div>
<div class="sidebar-item"><a href="{{ url('prompts') }}" class="{{ set_active('prompts') }}">Prompts</a></div>
<div class="sidebar-item"><a href="{{ url('news') }}" class="{{ set_active('news') }}">News</a></div>
<div class="sidebar-item"><a href="{{ url('sales') }}" class="{{ set_active('sales') }}">Sales</a></div>
</li>
</ul>
@endsection

@section('content')
{!! breadcrumbs(['World' => 'world', 'Calendar' => 'world/calendar']) !!}
<h1>Calendar</h1>

<div id='calendar'></div>

<script src="{{ asset('js/calendar.min.js') }}"></script>
<script>
document.addEventListener('DOMContentLoaded', function() {
const calendarEl = document.getElementById('calendar')
const calendar = new FullCalendar.Calendar(calendarEl, {
initialView: 'dayGridMonth',
lazyFetching: true,
timeZone: "{{ config('app.timezone') }}",
headerToolbar: {
left: 'prev,next',
center: 'title',
right: 'dayGridMonth multiMonthYear listWeek',
},
eventSources: [{
url: "{{ url('calendar/events') }}",
format: 'json',
}]
})
calendar.render()
})
</script>
@endsection
2 changes: 2 additions & 0 deletions routes/lorekeeper/browse.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@
**************************************************************************************************/
Route::get('credits', 'PageController@getCreditsPage');
Route::get('info/{key}', 'PageController@getPage');
Route::get('calendar', 'PageController@getCalendarPage');
Route::get('calendar/events', 'PageController@getCalendarEvents');

/**************************************************************************************************
Raffles
Expand Down

0 comments on commit 36dcdd7

Please sign in to comment.