-
Notifications
You must be signed in to change notification settings - Fork 84
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
Unique pages #61
base: main
Are you sure you want to change the base?
Unique pages #61
Changes from all commits
c6b8314
c694b06
7c6cdc6
bbe7b26
15bbd58
4c43501
d3f4eb5
78faf3a
97e203c
ca7ded7
2dd65c1
edc5760
f9b9908
6914305
d6148e3
48aceeb
dde9313
cda31c1
2448c13
c0d6650
5c29894
c4a1e1f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,247 @@ | ||
<?php | ||
|
||
namespace Backpack\PageManager\app\Http\Controllers\Admin; | ||
|
||
use App\UniquePages; | ||
use Illuminate\Http\Request; | ||
use Backpack\PageManager\app\TraitReflections; | ||
use Backpack\CRUD\app\Http\Controllers\CrudController; | ||
use Backpack\CRUD\app\Http\Controllers\CrudFeatures\SaveActions; | ||
|
||
class UniquePageCrudController extends CrudController | ||
{ | ||
use SaveActions; | ||
use UniquePages; | ||
use TraitReflections; | ||
|
||
public function setup() | ||
{ | ||
parent::setup(); | ||
|
||
$modelClass = config('backpack.pagemanager.unique_page_model_class', 'Backpack\PageManager\app\Models\Page'); | ||
|
||
$this->checkForTemplatesAndUniquePagesNotDistinct(); | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| BASIC CRUD INFORMATION | ||
|-------------------------------------------------------------------------- | ||
*/ | ||
$this->crud->setModel($modelClass); | ||
// Don't set route or entity names here. These depend on the page you are editing | ||
|
||
$this->crud->denyAccess(['list', 'create', 'delete']); | ||
|
||
if (config('backpack.pagemanager.unique_page_revisions')) { | ||
$this->crud->allowAccess('revisions'); | ||
} | ||
} | ||
|
||
/** | ||
* Edit the unique page retrieved by slug. | ||
* | ||
* @param string $slug | ||
* @return Response | ||
*/ | ||
public function uniqueEdit($slug) | ||
{ | ||
$model = $this->crud->model; | ||
$entry = $model::findBySlug($slug); | ||
|
||
if (! $entry) { | ||
$entry = $this->createMissingPage($slug); | ||
} | ||
|
||
$this->uniqueSetup($entry); | ||
|
||
return parent::edit($entry->id); | ||
} | ||
|
||
/** | ||
* Update the unique page. | ||
* | ||
* @param string $slug | ||
* @param int $id | ||
* @return \Illuminate\Http\RedirectResponse | ||
*/ | ||
public function update($slug, $id) | ||
{ | ||
$model = $this->crud->model; | ||
$entry = $model::findBySlugOrFail($slug); | ||
|
||
$this->uniqueSetup($entry); | ||
|
||
return parent::updateCrud(); | ||
} | ||
|
||
/** | ||
* Set the crud route. | ||
* | ||
* @param $slug | ||
*/ | ||
public function setRoute($slug) | ||
{ | ||
$this->crud->setRoute(config('backpack.base.route_prefix').'/unique/'.$slug); | ||
} | ||
|
||
/** | ||
* Populate the update form with basic fields that all pages need. | ||
* | ||
* @param Model $page | ||
*/ | ||
public function addDefaultPageFields($page) | ||
{ | ||
$fields = [ | ||
[ | ||
'name' => 'buttons', | ||
'type' => 'custom_html', | ||
'value' => $this->buttons($page), | ||
], | ||
[ | ||
'name' => 'template', | ||
'type' => 'hidden', | ||
], | ||
[ | ||
'name' => 'name', | ||
], | ||
[ | ||
'name' => 'title', | ||
], | ||
[ | ||
'name' => 'slug', | ||
'type' => 'hidden', | ||
], | ||
]; | ||
|
||
$this->crud->addFields($fields); | ||
} | ||
|
||
/** | ||
* Build the buttons html for the edit form. | ||
* | ||
* @param Model $page | ||
* @return string | ||
*/ | ||
public function buttons($page) | ||
{ | ||
$openButton = $page->getOpenButton(); | ||
$revisionsButton = view('crud::buttons.revisions', ['crud' => $this->crud, 'entry' => $page]); | ||
|
||
return $openButton.' '.$revisionsButton; | ||
} | ||
|
||
/** | ||
* Create missing unique page by slug. | ||
* | ||
* @param $slug | ||
* @return mixed | ||
*/ | ||
public function createMissingPage($slug) | ||
{ | ||
$slugs = $this->getUniqueSlugs(); | ||
|
||
if (! $slugs->has($slug)) { | ||
abort(404); | ||
} | ||
|
||
$page = $slugs->pull($slug); | ||
$model = $this->crud->model; | ||
|
||
return $model::create([ | ||
'template' => $page, | ||
'name' => camel_case($page), | ||
'title' => camel_case($page), | ||
'slug' => $slug, | ||
]); | ||
} | ||
|
||
/** | ||
* Display the revisions for specified resource. | ||
* | ||
* @param $slug | ||
* @param $id | ||
* @return Response | ||
*/ | ||
public function uniqueRevisions($slug, $id) | ||
{ | ||
$model = $this->crud->model; | ||
$entry = $model::findBySlugOrFail($slug); | ||
|
||
$this->uniqueSetup($entry); | ||
|
||
return parent::listRevisions($entry->id); | ||
} | ||
|
||
/** | ||
* Restore a specific revision for the specified resource. | ||
* | ||
* Used via AJAX in the revisions view | ||
* | ||
* @param string $slug | ||
* @param int $id | ||
* | ||
* @return JSON Response containing the new revision that was created from the update | ||
* @return HTTP 500 if the request did not contain the revision ID | ||
*/ | ||
public function restoreUniqueRevision($slug, $id) | ||
{ | ||
$model = $this->crud->model; | ||
$entry = $model::findBySlugOrFail($slug); | ||
|
||
$this->uniqueSetup($entry); | ||
|
||
return parent::restoreRevision($id); | ||
} | ||
|
||
/** | ||
* Setup the controller for the entry. | ||
* | ||
* @param $entry | ||
*/ | ||
protected function uniqueSetup($entry) | ||
{ | ||
$this->crud->entry = $entry; | ||
|
||
$this->setRoute($entry->slug); | ||
|
||
$this->addDefaultPageFields($entry); | ||
$this->crud->setEntityNameStrings($this->crud->makeLabel($entry->template), $this->crud->makeLabel($entry->template)); | ||
|
||
$this->{$entry->template}(); | ||
} | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| SaveActions overrides | ||
|-------------------------------------------------------------------------- | ||
*/ | ||
|
||
/** | ||
* Overrides trait version to remove 'save_and_back' and 'save_and_new'. | ||
* | ||
* @return [type] [description] | ||
*/ | ||
public function getSaveAction() | ||
{ | ||
$saveCurrent = [ | ||
'value' => $this->getSaveActionButtonName('save_and_back'), | ||
'label' => $this->getSaveActionButtonName('save_and_back'), | ||
]; | ||
|
||
return [ | ||
'active' => $saveCurrent, | ||
'options' => [], | ||
]; | ||
} | ||
|
||
/** | ||
* Override trait version to not update the session variable. | ||
* This way we preserve the user chosen save action and don't overwrite with. | ||
* | ||
* @param [type] $forceSaveAction [description] | ||
*/ | ||
public function setSaveAction($forceSaveAction = null) | ||
{ | ||
// do nothing to preserve session value for other crud | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,14 +58,4 @@ private function services() | |
'placeholder' => trans('backpack::pagemanager.content_placeholder'), | ||
]); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this meant to be part of the PR? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just moved As these traits get published and need to be edited by developers, this serves as an example how to define your templates/uniquePages. |
||
|
||
private function about_us() | ||
{ | ||
$this->crud->addField([ | ||
'name' => 'content', | ||
'label' => trans('backpack::pagemanager.content'), | ||
'type' => 'wysiwyg', | ||
'placeholder' => trans('backpack::pagemanager.content_placeholder'), | ||
]); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I forget why?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we have no "save actions" here and overwrite with
save_and_back
.This would geht default for this session in all Crud-Views.
As we don't want to overwrite behaviour when user can't choose, we need this (empty) implementation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added some more comment to make this clear ;)