Add ability to use a link instead of a page #750
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Enums\PageType;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
@@ -22,7 +23,7 @@ class CreatePages extends Migration
|
||||
$table->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');
|
||||
|
||||
36
app/Database/migrations/2020_06_09_141153_pages_add_link.php
Normal file
36
app/Database/migrations/2020_06_09_141153_pages_add_link.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class PagesAddLink extends Migration
|
||||
{
|
||||
/**
|
||||
* Add a `link` column and make the body optional. Also add a "new_window" bool
|
||||
* which determines if we open this link in a new window or not
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('pages', function (Blueprint $table) {
|
||||
$table->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');
|
||||
});
|
||||
}
|
||||
}
|
||||
46
app/Exceptions/UnknownPageType.php
Normal file
46
app/Exceptions/UnknownPageType.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exceptions;
|
||||
|
||||
use App\Models\Page;
|
||||
|
||||
class UnknownPageType extends AbstractHttpException
|
||||
{
|
||||
private $page;
|
||||
|
||||
public function __construct(Page $page)
|
||||
{
|
||||
$this->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,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,10 @@ use Illuminate\View\View;
|
||||
|
||||
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 @@ class PageLinksComposer extends Composer
|
||||
$w['public'] = true;
|
||||
}
|
||||
|
||||
$pages = $this->pageRepo->findWhere($w, ['id', 'name', 'slug', 'icon']);
|
||||
$pages = $this->pageRepo->findWhere($w, static::$fields);
|
||||
} catch (Exception $e) {
|
||||
$pages = [];
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ class UpdatePageRequest extends FormRequest
|
||||
'required',
|
||||
Rule::unique('pages')->ignore($this->id, 'id'),
|
||||
],
|
||||
'body' => 'required',
|
||||
'body' => 'nullable',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,11 @@ use App\Contracts\Enum;
|
||||
|
||||
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',
|
||||
];
|
||||
}
|
||||
|
||||
@@ -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 @@ use App\Contracts\Model;
|
||||
* @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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user