Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to add a link instead of a page #750 #757

Merged
merged 2 commits into from
Jun 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/Database/migrations/2020_03_27_174238_create_pages.php
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -22,7 +23,7 @@ public function up()
$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');
Expand Down
36 changes: 36 additions & 0 deletions app/Database/migrations/2020_06_09_141153_pages_add_link.php
Original file line number Diff line number Diff line change
@@ -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 changes: 46 additions & 0 deletions app/Exceptions/UnknownPageType.php
Original file line number Diff line number Diff line change
@@ -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,
];
}
}
7 changes: 5 additions & 2 deletions app/Http/Composers/PageLinksComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 = [];
}
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Requests/UpdatePageRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function rules(): array
'required',
Rule::unique('pages')->ignore($this->id, 'id'),
],
'body' => 'required',
'body' => 'nullable',
];
}
}
9 changes: 7 additions & 2 deletions app/Models/Enums/PageType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];
}
34 changes: 30 additions & 4 deletions app/Models/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace App\Models;

use App\Contracts\Model;
use App\Exceptions\UnknownPageType;
use App\Models\Enums\PageType;

/**
* @property int id
Expand All @@ -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
{
Expand All @@ -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);
}
}
22 changes: 9 additions & 13 deletions resources/views/admin/flash/message.blade.php
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -9,21 +9,17 @@
<div class="alert
alert-{{ $message['level'] }}
{{ $message['important'] ? 'alert-important' : '' }}"
role="alert"
>
<div class="alert-icon">
<i class="now-ui-icons ui-2_like"></i>
</div>
role="alert">
@if ($message['important'])
<button type="button"
class="close"
data-dismiss="alert"
aria-hidden="true"
>&times;
</button>
@endif
<button type="button"
class="close"
data-dismiss="alert"
aria-hidden="true">&times;
</button>

{{ $message['message'] }}

{!! $message['message'] !!}
</div>
@endif
@endforeach
Expand Down
2 changes: 1 addition & 1 deletion resources/views/admin/menu.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
@endability

@ability('admin', 'pages')
<li><a href="{!! url('/admin/pages') !!}"><i class="pe-7s-note"></i>pages</a></li>
<li><a href="{!! url('/admin/pages') !!}"><i class="pe-7s-note"></i>pages/links</a></li>
@endability

@ability('admin', 'maintenance')
Expand Down
64 changes: 62 additions & 2 deletions resources/views/admin/pages/fields.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,55 @@
<div class="col-12">
<div class="form-container">
<h6><i class="fas fa-sticky-note"></i>
&nbsp;Content
&nbsp;Type
</h6>
<div class="form-container-body">
<div class="row">
<div class="form-group col-12">
{{ Form::label('type', 'Page Type') }}
{{ Form::select('type', \App\Models\Enums\PageType::select(false), null, [
'id' => 'content-type-select',
'class' => 'form-control select2',
])
}}
<p class="text-danger">{{ $errors->first('type') }}</p>
</div>
</div>
</div>
</div>
</div>
</div>

<div class="row">
<div class="col-12">
<div class="form-container">
<h6><i class="fas fa-sticky-note"></i>
&nbsp;Content
</h6>
<div class="form-container-body">
<div id="type_content_page" class="row">
<div class="form-group col-12">
{{ Form::textarea('body', null, ['id' => 'editor', 'class' => 'editor']) }}
<p class="text-danger">{{ $errors->first('body') }}</p>
</div>
</div>

<div id="type_content_link" class="row">
<div class="form-group col-12">
{{ Form::text('link', null, ['class' => 'form-control', 'placeholder' => 'Link']) }}
<p class="text-danger">{{ $errors->first('link') }}</p>
</div>

<div class="form-group col-12">
<div class="checkbox">
<label class="checkbox-inline">
{{ Form::label('new_window', 'Open in new window:') }}
<input name="new_window" type="hidden" value="0"/>
{{ Form::checkbox('new_window') }}
</label>
</div>
</div>
</div>
</div>
</div>
</div>
Expand All @@ -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']) }}
<a href="{{ route('admin.roles.index') }}" class="btn btn-default">Cancel</a>
</div>
Expand All @@ -76,5 +115,26 @@
<script>
$(document).ready(function () {
CKEDITOR.replace('editor');
const select_id = "select#content-type-select";
function swap_content() {
const type = parseInt($(select_id + " option:selected").val());
console.log('content type change: ', type);
if (type === 0) {
$("#type_content_page").show();
$("#type_content_link").hide();
}
else if (type === 1) {
$("#type_content_page").hide();
$("#type_content_link").show();
}
}
$(select_id).change(async (e) => {
swap_content();
});
swap_content();
});
</script>
2 changes: 1 addition & 1 deletion resources/views/layouts/default/nav.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

@foreach($page_links as $page)
<li class="nav-item">
<a class="nav-link" href="{{ url(route('frontend.pages.show', ['slug' => $page->slug])) }}">
<a class="nav-link" href="{{ $page->url }}" target="{{ $page->new_window ? '_blank':'_self' }}">
<i class="{{ $page['icon'] }}"></i>
<p>{{ $page['name'] }}</p>
</a>
Expand Down