From d5465726e2ef5f0cf129943abd3701bbc643cb28 Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Fri, 19 Aug 2022 13:14:43 +0100 Subject: [PATCH] Added inbound references listing for pages --- app/Http/Controllers/ReferenceController.php | 47 ++++++++++++++++++++ resources/icons/popular.svg | 1 - resources/icons/reference.svg | 3 ++ resources/lang/en/entities.php | 5 +++ resources/views/pages/references.blade.php | 34 ++++++++++++++ routes/web.php | 2 + 6 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 app/Http/Controllers/ReferenceController.php create mode 100644 resources/icons/reference.svg create mode 100644 resources/views/pages/references.blade.php diff --git a/app/Http/Controllers/ReferenceController.php b/app/Http/Controllers/ReferenceController.php new file mode 100644 index 000000000..bed2c5f30 --- /dev/null +++ b/app/Http/Controllers/ReferenceController.php @@ -0,0 +1,47 @@ +permissions = $permissions; + } + + /** + * Display the references to a given page. + */ + public function page(string $bookSlug, string $pageSlug) + { + /** @var Page $page */ + $page = Page::visible()->whereSlugs($bookSlug, $pageSlug)->firstOrFail(); + + $baseQuery = $page->referencesTo() + ->where('from_type', '=', (new Page())->getMorphClass()) + ->with([ + 'from' => fn(Relation $query) => $query->select(Page::$listAttributes), + 'from.book' => fn(Relation $query) => $query->scopes('visible'), + 'from.chapter' => fn(Relation $query) => $query->scopes('visible') + ]); + + $references = $this->permissions->restrictEntityRelationQuery( + $baseQuery, + 'references', + 'from_id', + 'from_type' + )->get(); + + return view('pages.references', [ + 'page' => $page, + 'references' => $references, + ]); + } +} diff --git a/resources/icons/popular.svg b/resources/icons/popular.svg index ba1f918a5..2ac44f151 100644 --- a/resources/icons/popular.svg +++ b/resources/icons/popular.svg @@ -1,4 +1,3 @@ - \ No newline at end of file diff --git a/resources/icons/reference.svg b/resources/icons/reference.svg new file mode 100644 index 000000000..560ec5f37 --- /dev/null +++ b/resources/icons/reference.svg @@ -0,0 +1,3 @@ + + + diff --git a/resources/lang/en/entities.php b/resources/lang/en/entities.php index db1e8027b..a92b465b8 100644 --- a/resources/lang/en/entities.php +++ b/resources/lang/en/entities.php @@ -369,4 +369,9 @@ return [ 'convert_to_book_desc' => 'You can convert this chapter to a new book with the same contents. Any permissions set on this chapter will be copied to the new book but any inherited permissions, from the parent book, will not be copied which could lead to a change of access control.', 'convert_chapter' => 'Convert Chapter', 'convert_chapter_confirm' => 'Are you sure you want to convert this chapter?', + + // References + 'references' => 'References', + 'references_none' => 'There are no tracked references to this item.', + 'references_to_desc' => 'Shown below are all the known pages in the system that link to this item.', ]; diff --git a/resources/views/pages/references.blade.php b/resources/views/pages/references.blade.php new file mode 100644 index 000000000..3f35a1629 --- /dev/null +++ b/resources/views/pages/references.blade.php @@ -0,0 +1,34 @@ +@extends('layouts.simple') + +@section('body') + +
+ +
+ @include('entities.breadcrumbs', ['crumbs' => [ + $page->book, + $page->chapter, + $page, + $page->getUrl('/references') => [ + 'text' => trans('entities.references'), + 'icon' => 'reference', + ] + ]]) +
+ +
+

{{ trans('entities.references') }}

+

{{ trans('entities.references_to_desc') }}

+ + @if(count($references) > 0) +
+ @include('entities.list', ['entities' => $references->pluck('from'), 'showPath' => true]) +
+ @else +

{{ trans('entities.references_none') }}

+ @endif + +
+
+ +@stop diff --git a/routes/web.php b/routes/web.php index 00841365a..a16960283 100644 --- a/routes/web.php +++ b/routes/web.php @@ -20,6 +20,7 @@ use BookStack\Http\Controllers\PageExportController; use BookStack\Http\Controllers\PageRevisionController; use BookStack\Http\Controllers\PageTemplateController; use BookStack\Http\Controllers\RecycleBinController; +use BookStack\Http\Controllers\ReferenceController; use BookStack\Http\Controllers\RoleController; use BookStack\Http\Controllers\SearchController; use BookStack\Http\Controllers\SettingController; @@ -110,6 +111,7 @@ Route::middleware('auth')->group(function () { Route::get('/books/{bookSlug}/draft/{pageId}/delete', [PageController::class, 'showDeleteDraft']); Route::get('/books/{bookSlug}/page/{pageSlug}/permissions', [PageController::class, 'showPermissions']); Route::put('/books/{bookSlug}/page/{pageSlug}/permissions', [PageController::class, 'permissions']); + Route::get('/books/{bookSlug}/page/{pageSlug}/references', [ReferenceController::class, 'page']); Route::put('/books/{bookSlug}/page/{pageSlug}', [PageController::class, 'update']); Route::delete('/books/{bookSlug}/page/{pageSlug}', [PageController::class, 'destroy']); Route::delete('/books/{bookSlug}/draft/{pageId}', [PageController::class, 'destroyDraft']);