diff --git a/app/Database/migrations/2020_03_27_174238_create_pages.php b/app/Database/migrations/2020_03_27_174238_create_pages.php index f7d974c12..70b051e73 100644 --- a/app/Database/migrations/2020_03_27_174238_create_pages.php +++ b/app/Database/migrations/2020_03_27_174238_create_pages.php @@ -1,5 +1,6 @@ string('name'); $table->string('slug'); $table->string('icon'); - $table->unsignedSmallInteger('type'); + $table->unsignedSmallInteger('type')->default(PageType::PAGE); $table->boolean('public'); $table->boolean('enabled'); $table->mediumText('body'); diff --git a/app/Database/migrations/2020_06_09_141153_pages_add_link.php b/app/Database/migrations/2020_06_09_141153_pages_add_link.php new file mode 100644 index 000000000..7eb9bc803 --- /dev/null +++ b/app/Database/migrations/2020_06_09_141153_pages_add_link.php @@ -0,0 +1,35 @@ +string('body')->change()->nullable(); + $table->string('link') + ->default('') + ->nullable() + ->after('body'); + + $table->boolean('new_window')->default(false); + }); + } + + /** + * @return void + */ + public function down() + { + Schema::table('fares', function (Blueprint $table) { + $table->dropColumn('link'); + $table->dropColumn('new_window'); + }); + } +} diff --git a/app/Exceptions/UnknownPageType.php b/app/Exceptions/UnknownPageType.php new file mode 100644 index 000000000..40aee7143 --- /dev/null +++ b/app/Exceptions/UnknownPageType.php @@ -0,0 +1,46 @@ +page = $page; + parent::__construct( + 400, + 'Unknown page type "'.$page->type.'"' + ); + } + + /** + * Return the RFC 7807 error type (without the URL root) + */ + public function getErrorType(): string + { + return 'unknown-page-type'; + } + + /** + * Get the detailed error string + */ + public function getErrorDetails(): string + { + return $this->getMessage(); + } + + /** + * Return an array with the error details, merged with the RFC7807 response + */ + public function getErrorMetadata(): array + { + return [ + 'id' => $this->page->id, + 'type' => $this->page->type, + ]; + } +} diff --git a/app/Http/Composers/PageLinksComposer.php b/app/Http/Composers/PageLinksComposer.php index 094487c3e..0276bd0f2 100644 --- a/app/Http/Composers/PageLinksComposer.php +++ b/app/Http/Composers/PageLinksComposer.php @@ -10,7 +10,10 @@ class PageLinksComposer extends Composer { - protected $pageRepo; + private static $fields = ['id', 'name', 'slug', 'icon', 'type', 'link', 'new_window']; + + /** @var \App\Repositories\PageRepository */ + private $pageRepo; /** * PageLinksComposer constructor. @@ -37,7 +40,7 @@ public function compose(View $view) $w['public'] = true; } - $pages = $this->pageRepo->findWhere($w, ['id', 'name', 'slug', 'icon']); + $pages = $this->pageRepo->findWhere($w, static::$fields); } catch (Exception $e) { $pages = []; } diff --git a/app/Http/Controllers/Admin/PagesController.php b/app/Http/Controllers/Admin/PagesController.php index ef438bb63..a092cf34d 100644 --- a/app/Http/Controllers/Admin/PagesController.php +++ b/app/Http/Controllers/Admin/PagesController.php @@ -5,6 +5,7 @@ use App\Contracts\Controller; use App\Http\Requests\CreatePageRequest; use App\Http\Requests\UpdatePageRequest; +use App\Models\Enums\PageType; use App\Repositories\PageRepository; use Illuminate\Http\Request; use Laracasts\Flash\Flash; @@ -40,7 +41,9 @@ public function index(Request $request) */ public function create() { - return view('admin.pages.create'); + return view('admin.pages.create', [ + 'page_types' => PageType::select(false), + ]); } /** @@ -99,7 +102,8 @@ public function edit($id) } return view('admin.pages.edit', [ - 'page' => $page, + 'page' => $page, + 'page_types' => PageType::select(false), ]); } diff --git a/app/Http/Requests/UpdatePageRequest.php b/app/Http/Requests/UpdatePageRequest.php index 4c2a07218..86df290ca 100644 --- a/app/Http/Requests/UpdatePageRequest.php +++ b/app/Http/Requests/UpdatePageRequest.php @@ -19,7 +19,7 @@ public function rules(): array 'required', Rule::unique('pages')->ignore($this->id, 'id'), ], - 'body' => 'required', + 'body' => 'nullable', ]; } } diff --git a/app/Models/Enums/PageType.php b/app/Models/Enums/PageType.php index 46a35f24c..a92157039 100644 --- a/app/Models/Enums/PageType.php +++ b/app/Models/Enums/PageType.php @@ -6,6 +6,11 @@ class PageType extends Enum { - public const HTML = 0; - public const MARKDOWN = 1; + public const PAGE = 0; + public const LINK = 1; + + public static $labels = [ + self::PAGE => 'Page', + self::LINK => 'Link', + ]; } diff --git a/app/Models/Page.php b/app/Models/Page.php index f91f818c2..2e124fed8 100644 --- a/app/Models/Page.php +++ b/app/Models/Page.php @@ -3,6 +3,8 @@ namespace App\Models; use App\Contracts\Model; +use App\Exceptions\UnknownPageType; +use App\Models\Enums\PageType; /** * @property int id @@ -12,7 +14,9 @@ * @property int type * @property bool public * @property bool enabled + * @property bool new_window * @property string body + * @property string link */ class Page extends Model { @@ -25,17 +29,39 @@ class Page extends Model 'icon', 'public', 'body', + 'link', 'enabled', + 'new_window', ]; protected $casts = [ - 'type' => 'integer', - 'public' => 'boolean', - 'enabled' => 'boolean', + 'type' => 'integer', + 'public' => 'boolean', + 'enabled' => 'boolean', + 'new_window' => 'boolean', ]; public static $rules = [ 'name' => 'required|unique:pages,name', - 'body' => 'required', + 'body' => 'nullable', + 'type' => 'required', ]; + + /** + * Return the full URL to this page; determines if it's internal or external + * + * @throws \App\Exceptions\UnknownPageType + */ + public function getUrlAttribute(): string + { + if ($this->type === PageType::PAGE) { + return url(route('frontend.pages.show', ['slug' => $this->slug])); + } + + if ($this->type === PageType::LINK) { + return $this->link; + } + + throw new UnknownPageType($this); + } } diff --git a/resources/views/admin/flash/message.blade.php b/resources/views/admin/flash/message.blade.php index 13fcdf9fb..b165337e6 100644 --- a/resources/views/admin/flash/message.blade.php +++ b/resources/views/admin/flash/message.blade.php @@ -1,4 +1,4 @@ -@foreach (session('flash_notification', []) as $message) +@foreach (session('flash_notification', collect())->toArray() as $message) @if ($message['overlay']) @include('flash::modal', [ 'modalClass' => 'flash-modal', @@ -9,21 +9,17 @@
@endif @endforeach diff --git a/resources/views/admin/menu.blade.php b/resources/views/admin/menu.blade.php index b4652fd76..a80f77f03 100644 --- a/resources/views/admin/menu.blade.php +++ b/resources/views/admin/menu.blade.php @@ -71,7 +71,7 @@ @endability @ability('admin', 'pages') - + role="alert"> @if ($message['important']) + @endif - - - {{ $message['message'] }} + {!! $message['message'] !!}{{ $errors->first('type') }}
+{{ $errors->first('body') }}
{{ $errors->first('link') }}
+{{ $page['name'] }}