Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b619e48

Browse files
committed
Add ability to use a link instead of a page #750
1 parent e4b1c23 commit b619e48

File tree

12 files changed

+206
-29
lines changed

12 files changed

+206
-29
lines changed

app/Database/migrations/2020_03_27_174238_create_pages.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use App\Models\Enums\PageType;
34
use Illuminate\Database\Migrations\Migration;
45
use Illuminate\Database\Schema\Blueprint;
56
use Illuminate\Support\Facades\Schema;
@@ -22,7 +23,7 @@ public function up()
2223
$table->string('name');
2324
$table->string('slug');
2425
$table->string('icon');
25-
$table->unsignedSmallInteger('type');
26+
$table->unsignedSmallInteger('type')->default(PageType::PAGE);
2627
$table->boolean('public');
2728
$table->boolean('enabled');
2829
$table->mediumText('body');
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class PagesAddLink extends Migration
8+
{
9+
/**
10+
* Add a `link` column and make the body optional. Also add a "new_window" bool
11+
* which determines if we open this link in a new window or not
12+
*/
13+
public function up()
14+
{
15+
Schema::table('pages', function (Blueprint $table) {
16+
$table->string('body')->change()->nullable();
17+
$table->string('link')
18+
->default('')
19+
->nullable()
20+
->after('body');
21+
22+
$table->boolean('new_window')->default(false);
23+
});
24+
}
25+
26+
/**
27+
* @return void
28+
*/
29+
public function down()
30+
{
31+
Schema::table('fares', function (Blueprint $table) {
32+
$table->dropColumn('link');
33+
$table->dropColumn('new_window');
34+
});
35+
}
36+
}

app/Exceptions/UnknownPageType.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace App\Exceptions;
4+
5+
use App\Models\Page;
6+
7+
class UnknownPageType extends AbstractHttpException
8+
{
9+
private $page;
10+
11+
public function __construct(Page $page)
12+
{
13+
$this->page = $page;
14+
parent::__construct(
15+
400,
16+
'Unknown page type "'.$page->type.'"'
17+
);
18+
}
19+
20+
/**
21+
* Return the RFC 7807 error type (without the URL root)
22+
*/
23+
public function getErrorType(): string
24+
{
25+
return 'unknown-page-type';
26+
}
27+
28+
/**
29+
* Get the detailed error string
30+
*/
31+
public function getErrorDetails(): string
32+
{
33+
return $this->getMessage();
34+
}
35+
36+
/**
37+
* Return an array with the error details, merged with the RFC7807 response
38+
*/
39+
public function getErrorMetadata(): array
40+
{
41+
return [
42+
'id' => $this->page->id,
43+
'type' => $this->page->type,
44+
];
45+
}
46+
}

app/Http/Composers/PageLinksComposer.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010

1111
class PageLinksComposer extends Composer
1212
{
13-
protected $pageRepo;
13+
private static $fields = ['id', 'name', 'slug', 'icon', 'type', 'link', 'new_window'];
14+
15+
/** @var \App\Repositories\PageRepository */
16+
private $pageRepo;
1417

1518
/**
1619
* PageLinksComposer constructor.
@@ -37,7 +40,7 @@ public function compose(View $view)
3740
$w['public'] = true;
3841
}
3942

40-
$pages = $this->pageRepo->findWhere($w, ['id', 'name', 'slug', 'icon']);
43+
$pages = $this->pageRepo->findWhere($w, static::$fields);
4144
} catch (Exception $e) {
4245
$pages = [];
4346
}

app/Http/Controllers/Admin/PagesController.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\Contracts\Controller;
66
use App\Http\Requests\CreatePageRequest;
77
use App\Http\Requests\UpdatePageRequest;
8+
use App\Models\Enums\PageType;
89
use App\Repositories\PageRepository;
910
use Illuminate\Http\Request;
1011
use Laracasts\Flash\Flash;
@@ -40,7 +41,9 @@ public function index(Request $request)
4041
*/
4142
public function create()
4243
{
43-
return view('admin.pages.create');
44+
return view('admin.pages.create', [
45+
'page_types' => PageType::select(false),
46+
]);
4447
}
4548

4649
/**
@@ -99,7 +102,8 @@ public function edit($id)
99102
}
100103

101104
return view('admin.pages.edit', [
102-
'page' => $page,
105+
'page' => $page,
106+
'page_types' => PageType::select(false),
103107
]);
104108
}
105109

app/Http/Requests/UpdatePageRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function rules(): array
1919
'required',
2020
Rule::unique('pages')->ignore($this->id, 'id'),
2121
],
22-
'body' => 'required',
22+
'body' => 'nullable',
2323
];
2424
}
2525
}

app/Models/Enums/PageType.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
class PageType extends Enum
88
{
9-
public const HTML = 0;
10-
public const MARKDOWN = 1;
9+
public const PAGE = 0;
10+
public const LINK = 1;
11+
12+
public static $labels = [
13+
self::PAGE => 'Page',
14+
self::LINK => 'Link',
15+
];
1116
}

app/Models/Page.php

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace App\Models;
44

55
use App\Contracts\Model;
6+
use App\Exceptions\UnknownPageType;
7+
use App\Models\Enums\PageType;
68

79
/**
810
* @property int id
@@ -12,7 +14,9 @@
1214
* @property int type
1315
* @property bool public
1416
* @property bool enabled
17+
* @property bool new_window
1518
* @property string body
19+
* @property string link
1620
*/
1721
class Page extends Model
1822
{
@@ -25,17 +29,39 @@ class Page extends Model
2529
'icon',
2630
'public',
2731
'body',
32+
'link',
2833
'enabled',
34+
'new_window',
2935
];
3036

3137
protected $casts = [
32-
'type' => 'integer',
33-
'public' => 'boolean',
34-
'enabled' => 'boolean',
38+
'type' => 'integer',
39+
'public' => 'boolean',
40+
'enabled' => 'boolean',
41+
'new_window' => 'boolean',
3542
];
3643

3744
public static $rules = [
3845
'name' => 'required|unique:pages,name',
39-
'body' => 'required',
46+
'body' => 'nullable',
47+
'type' => 'required',
4048
];
49+
50+
/**
51+
* Return the full URL to this page; determines if it's internal or external
52+
*
53+
* @throws \App\Exceptions\UnknownPageType
54+
*/
55+
public function getUrlAttribute(): string
56+
{
57+
if ($this->type === PageType::PAGE) {
58+
return url(route('frontend.pages.show', ['slug' => $this->slug]));
59+
}
60+
61+
if ($this->type === PageType::LINK) {
62+
return $this->link;
63+
}
64+
65+
throw new UnknownPageType($this);
66+
}
4167
}

resources/views/admin/flash/message.blade.php

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@foreach (session('flash_notification', []) as $message)
1+
@foreach (session('flash_notification', collect())->toArray() as $message)
22
@if ($message['overlay'])
33
@include('flash::modal', [
44
'modalClass' => 'flash-modal',
@@ -9,21 +9,17 @@
99
<div class="alert
1010
alert-{{ $message['level'] }}
1111
{{ $message['important'] ? 'alert-important' : '' }}"
12-
role="alert"
13-
>
14-
<div class="alert-icon">
15-
<i class="now-ui-icons ui-2_like"></i>
16-
</div>
12+
role="alert">
1713
@if ($message['important'])
14+
<button type="button"
15+
class="close"
16+
data-dismiss="alert"
17+
aria-hidden="true"
18+
>&times;
19+
</button>
1820
@endif
19-
<button type="button"
20-
class="close"
21-
data-dismiss="alert"
22-
aria-hidden="true">&times;
23-
</button>
24-
25-
{{ $message['message'] }}
2621

22+
{!! $message['message'] !!}
2723
</div>
2824
@endif
2925
@endforeach

resources/views/admin/menu.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
@endability
7272

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

7777
@ability('admin', 'maintenance')

0 commit comments

Comments
 (0)