From fff821e7f3492be15c2f6510ccf04d89a79241a6 Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Wed, 10 Jun 2020 18:25:36 -0400 Subject: [PATCH] Add ability to use a link instead of a page #750 --- .../2020_03_27_174238_create_pages.php | 3 +- .../2020_06_09_141153_pages_add_link.php | 35 ++++++++++ app/Exceptions/UnknownPageType.php | 46 +++++++++++++ app/Http/Composers/PageLinksComposer.php | 7 +- .../Controllers/Admin/PagesController.php | 8 ++- app/Http/Requests/UpdatePageRequest.php | 2 +- app/Models/Enums/PageType.php | 9 ++- app/Models/Page.php | 34 ++++++++-- resources/views/admin/flash/message.blade.php | 22 +++---- resources/views/admin/menu.blade.php | 2 +- resources/views/admin/pages/fields.blade.php | 64 ++++++++++++++++++- resources/views/layouts/default/nav.blade.php | 2 +- 12 files changed, 205 insertions(+), 29 deletions(-) create mode 100644 app/Database/migrations/2020_06_09_141153_pages_add_link.php create mode 100644 app/Exceptions/UnknownPageType.php 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') -
  • pages
  • +
  • pages/link
  • @endability @ability('admin', 'maintenance') diff --git a/resources/views/admin/pages/fields.blade.php b/resources/views/admin/pages/fields.blade.php index fe7063f9d..4250db595 100644 --- a/resources/views/admin/pages/fields.blade.php +++ b/resources/views/admin/pages/fields.blade.php @@ -45,15 +45,55 @@
    -  Content +  Type
    +
    + {{ Form::label('type', 'Page Type') }} + {{ Form::select('type', \App\Models\Enums\PageType::select(false), null, [ + 'id' => 'content-type-select', + 'class' => 'form-control select2', + ]) + }} +

    {{ $errors->first('type') }}

    +
    +
    +
    +
    +
    + + +
    +
    +
    +
    +  Content +
    +
    +
    {{ Form::textarea('body', null, ['id' => 'editor', 'class' => 'editor']) }}

    {{ $errors->first('body') }}

    + +
    @@ -66,7 +106,6 @@ {{ Form::hidden('id') }} @endif - {{ Form::hidden('type', \App\Models\Enums\PageType::HTML) }} {{ Form::button('Save', ['type' => 'submit', 'class' => 'btn btn-success']) }} Cancel
    @@ -76,5 +115,26 @@ diff --git a/resources/views/layouts/default/nav.blade.php b/resources/views/layouts/default/nav.blade.php index 0bbb35940..f2d98043e 100644 --- a/resources/views/layouts/default/nav.blade.php +++ b/resources/views/layouts/default/nav.blade.php @@ -36,7 +36,7 @@ @foreach($page_links as $page)