Skip to content

Commit

Permalink
Merge pull request #81 from drewroberts/pdbreen/feature/79
Browse files Browse the repository at this point in the history
Update BlogController and PageController logic for #79 and #80
  • Loading branch information
drewroberts authored Apr 14, 2021
2 parents b5f0116 + 1fc8657 commit 57b1c77
Show file tree
Hide file tree
Showing 15 changed files with 219 additions and 52 deletions.
5 changes: 5 additions & 0 deletions resources/views/page/amp.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
@extends('support::amp')

@section('content')
{{-- DO NOT REMOVE - page identity tag --}}
<!-- P:{{ $page->id }} C:{{ $child_page->id ?? '0' }} GC:{{ $grand_child_page->id ?? '0' }} -->


{{-- Place holder content - safe to replace --}}
<ul>
<li>Page: {{ $page->name }}</li>
<li>Child: {{ $child_page->name ?? 'NONE' }}</li>
Expand Down
5 changes: 5 additions & 0 deletions resources/views/page/base.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
@extends('support::base')

@section('content')
{{-- DO NOT REMOVE - page identity tag --}}
<!-- P:{{ $page->id }} C:{{ $child_page->id ?? '0' }} GC:{{ $grand_child_page->id ?? '0' }} -->


{{-- Place holder content - safe to replace --}}
<ul>
<li>Page: {{ $page->name }}</li>
<li>Child: {{ $child_page->name ?? 'NONE' }}</li>
Expand Down
4 changes: 4 additions & 0 deletions resources/views/post/amp.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
@extends('support::amp')

@section('content')
{{-- DO NOT REMOVE - identity tag --}}
<!-- T:{{ $topic->id ?? '0' }} S:{{ $series->id ?? '0' }} P:{{ $post->id }} -->

{{-- Place holder content - safe to replace --}}
<ul>
<li>Topic: {{ $topic->name ?? 'NONE' }}</li>
<li>Series: {{ $series->name ?? 'NONE' }}</li>
Expand Down
4 changes: 4 additions & 0 deletions resources/views/post/base.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
@extends('support::base')

@section('content')
{{-- DO NOT REMOVE - identity tag --}}
<!-- T:{{ $topic->id ?? '0' }} S:{{ $series->id ?? '0' }} P:{{ $post->id }} -->

{{-- Place holder content - safe to replace --}}
<ul>
<li>Topic: {{ $topic->name ?? 'NONE' }}</li>
<li>Series: {{ $series->name ?? 'NONE' }}</li>
Expand Down
4 changes: 4 additions & 0 deletions resources/views/series/amp.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
@extends('support::amp')

@section('content')
{{-- DO NOT REMOVE - identity tag --}}
<!-- T:{{ $topic->id }} S:{{ $series->id }} -->

{{-- Place holder content - safe to replace --}}
<ul>
<li>Topic: {{ $topic->name }}</li>
<li>Series: {{ $series->name }}</li>
Expand Down
4 changes: 4 additions & 0 deletions resources/views/series/base.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
@extends('support::base')

@section('content')
{{-- DO NOT REMOVE - identity tag --}}
<!-- T:{{ $topic->id }} S:{{ $series->id }} -->

{{-- Place holder content - safe to replace --}}
<ul>
<li>Topic: {{ $topic->name }}</li>
<li>Series: {{ $series->name }}</li>
Expand Down
4 changes: 4 additions & 0 deletions resources/views/topic/amp.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
@extends('support::amp')

@section('content')
{{-- DO NOT REMOVE - identity tag --}}
<!-- T:{{ $topic->id }} -->

{{-- Place holder content - safe to replace --}}
<ul>
<li>Topic: {{ $topic->name }}</li>
</ul>
Expand Down
4 changes: 4 additions & 0 deletions resources/views/topic/base.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
@extends('support::base')

@section('content')
{{-- DO NOT REMOVE - identity tag --}}
<!-- T:{{ $topic->id }} -->

{{-- Place holder content - safe to replace --}}
<ul>
<li>Topic: {{ $topic->name }}</li>
</ul>
Expand Down
6 changes: 1 addition & 5 deletions src/Http/Controllers/BlogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ private function handleTopicRoute(Request $request, Topic $topic, ?string $slug2
private function handlePageRoute(Request $request, Page $page, ?string $slug2 = null, ?string $slug3 = null)
{
if (empty($slug2)) {
abort_unless($page->is_leaf, 404);

return app(PageController::class)($request, $page);
}

Expand All @@ -71,8 +69,6 @@ private function handlePageRoute(Request $request, Page $page, ?string $slug2 =
->where('slug', '=', $slug2)
->first()) {
if (empty($slug3)) {
abort_unless($childPage->is_leaf, 404);

return app(PageController::class)($request, $page, $childPage);
}

Expand All @@ -85,7 +81,7 @@ private function handlePageRoute(Request $request, Page $page, ?string $slug2 =
}
}

// Valid topic, but invalid nesting of series or post slugs
// Valid root page, but invalid nesting of pages
abort(404);
}
}
17 changes: 17 additions & 0 deletions src/Http/Controllers/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@ class PageController extends BaseController
{
public function __invoke(Request $request, Page $page, ?Page $childPage = null, Page $grandChildPage = null)
{
// If location based grand child is only child, redirect!!
if ($grandChildPage) {
if ($grandChildPage->location_based && $grandChildPage->is_only_child) {
return redirect(url($grandChildPage->path));
}
} elseif ($childPage) {
// If location based child is only child, redirect!!!
if ($childPage->location_based && $childPage->is_only_child) {
return redirect(url($childPage->path));
}
} else {
// If there is only location based root page, redirect home!
if ($page->is_only_root_location) {
return redirect(url($page->path));
}
}

$leafPage = $grandChildPage ?: ($childPage ?: $page);
LayoutManager::setLayout($leafPage->layout);

Expand Down
39 changes: 31 additions & 8 deletions src/Models/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
* @property Page parent
* @property bool is_leaf
* @property bool is_root
* @property bool is_only_child
* @property bool is_only_root_location
* @property string|null path
* @property int depth
* @property string content
Expand Down Expand Up @@ -81,6 +83,7 @@ class Page extends BaseModel
'parent_id',
'slug',
'title',
'location_based',
];

protected static function boot()
Expand Down Expand Up @@ -128,15 +131,35 @@ public function getRouteKeyName()
{
return 'slug';
}

public function layout()
{
return $this->belongsTo(app('layout'));
}

public function getPathAttribute(): ?string
{
return $this->is_leaf ? '/' . implode('/', $this->getParentPath()) : null;
if ($this->is_only_root_location) {
return '/';
}

$path = [];
$parent = $this;
while ($parent) {
// Start accumulating slugs when not location based or not only child
if ($path || ! $parent->location_based || ! $parent->is_only_child) {
$path[] = $parent->slug;
}
$parent = $parent->parent;
}

return implode('/', array_reverse($path));
}

public function getIsOnlyRootLocationAttribute(): bool
{
return ($this->location_based && $this->is_root &&
static::query()->whereNull('parent_id')->where('location_based', true)->count() === 1);
}

public function getIsRootAttribute(): bool
Expand All @@ -149,21 +172,21 @@ public function getIsLeafAttribute(): bool
return $this->children()->count() === 0;
}

public function getDepthAttribute(): int
public function getIsOnlyChildAttribute(): bool
{
return count($this->getParentPath());
return $this->parent && $this->parent->children->count() === 1;
}

private function getParentPath(): array
public function getDepthAttribute(): int
{
$path = [];
$depth = 0;
$parent = $this;
while ($parent) {
$path[] = $parent;
$depth++;
$parent = $parent->parent;
}

return array_reverse($path);
return $depth;
}

public function author()
Expand Down
Loading

0 comments on commit 57b1c77

Please sign in to comment.