309 lines
9.8 KiB
PHP
309 lines
9.8 KiB
PHP
<?php
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Enumerations\ContentStatuses;
|
|
use App\Enumerations\ModulesHandlerToId;
|
|
use App\Events\AttemptToEditInvalidContent;
|
|
use App\Events\DeleteContent;
|
|
use App\Events\EditContent as EditContentEvent;
|
|
use App\Events\MakeDraftFromContent;
|
|
use App\Events\PublishContent;
|
|
use App\Events\SetHomepage;
|
|
use App\Http\Requests\EditContentRequest;
|
|
use App\Http\Requests\Request;
|
|
use App\Models\Content;
|
|
use App\Models\ContentHistory;
|
|
use App\Models\File;
|
|
use App\Models\Template;
|
|
use Illuminate\Support\Facades\Event;
|
|
use Illuminate\Support\Str;
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
|
|
class EditContent extends Controller
|
|
{
|
|
/**
|
|
* @var ContentHistory
|
|
*/
|
|
private $history;
|
|
/**
|
|
* @var File
|
|
*/
|
|
private $file;
|
|
|
|
public function __construct(ContentHistory $history, File $file)
|
|
{
|
|
$this->history = $history;
|
|
$this->file = $file;
|
|
}
|
|
|
|
/**
|
|
* Display form for editing static page
|
|
*
|
|
* @param $id
|
|
*
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
*/
|
|
public function getEdit($id)
|
|
{
|
|
// select all content statuses
|
|
$statuses = ContentStatuses::getWithTranslations();
|
|
|
|
// try to find requested page
|
|
$page = Content::findOrFail($id);
|
|
$page->module_settings = json_decode($page->module_settings);
|
|
|
|
$allModules = ModulesHandlerToId::getWithTranslations();
|
|
$moduleId = $allModules[$page->module_handler];
|
|
$templates = Template::where('module_id', $moduleId)
|
|
->select(['template_name', 'name', 'description'])
|
|
->get();
|
|
|
|
$history = $this->history->getStructuredHistoryOf($page);
|
|
|
|
// render view
|
|
return view('admin.content.edit', [
|
|
'contentStatuses' => $statuses,
|
|
'page' => $page,
|
|
'rootPages' => $page->rootPages(),
|
|
'templates' => $templates,
|
|
'extraData' => call_user_func([$page->module_handler, 'loadExtraData'], $page),
|
|
'formSectionNames' => call_user_func([$page->module_handler, 'getExtendedFormSections']),
|
|
'formSections' => call_user_func([$page->module_handler, 'getExtendedFormInputsEdit']),
|
|
'historySize' => count($history['changesets'])
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Edit static page and save changes into history.
|
|
*
|
|
* @param EditContentRequest $request
|
|
* @param $id
|
|
*
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function postEdit(EditContentRequest $request, $id)
|
|
{
|
|
try {
|
|
$page = Content::findOrFail($id);
|
|
} catch (NotFoundHttpException $e) {
|
|
|
|
// log attempt of invalid edit
|
|
Event::fire(new AttemptToEditInvalidContent($id));
|
|
|
|
// page for editing not found
|
|
return redirect()
|
|
->route('content.overview')
|
|
->with('error', trans('admin/contents.invalidPage'));
|
|
}
|
|
|
|
$request->url = Str::slug($request->url);
|
|
$request->parentPage = $request->parentPage ? $request->parentPage : null;
|
|
$request->titlePhotoFilename = $page->title_photo;
|
|
|
|
// upload file - if provided
|
|
if ($request->hasFile('titlePhoto')) {
|
|
$file = $this->file->upload($request->file('titlePhoto'), $request);
|
|
|
|
$request->titlePhotoFilename = $file['filename'];
|
|
}
|
|
|
|
// array of fields in database and fields from request
|
|
$fields = [
|
|
'title' => 'title',
|
|
'url' => 'url',
|
|
'template' => 'template',
|
|
'published_from' => 'publishedFrom',
|
|
'published_to' => 'publishedTo',
|
|
'meta_keywords' => 'metaKeywords',
|
|
'meta_description' => 'metaDescription',
|
|
'content' => 'pageContent',
|
|
'parent_id' => 'parentPage',
|
|
'status' => 'status',
|
|
'title_photo' => 'titlePhotoFilename'
|
|
];
|
|
|
|
// save new values with history
|
|
$data = self::getFields($request, $fields);
|
|
list($revision, $changedColumns) = $page->updateWithHistory($fields, $data);
|
|
|
|
$changedColumns += call_user_func_array([$page->module_handler, 'edit'], [&$page, $request, $revision]);
|
|
|
|
// was something changed?
|
|
if ($changedColumns == 0) {
|
|
return redirect()
|
|
->route('content.edit', ['id' => $page->id])
|
|
->with('error', trans('admin/contents.nothingToEdit'));
|
|
}
|
|
|
|
// fire event with
|
|
Event::fire(new EditContentEvent($page, $revision));
|
|
|
|
// something was changed, inform user
|
|
return redirect()
|
|
->route('content.edit', ['id' => $page->id])
|
|
->with('success', trans('admin/contents.successfullyEdited'));
|
|
}
|
|
|
|
/**
|
|
* Method for changing page status
|
|
* This method is called in static page overview from quick actions. It checks if status is valid, if page exists
|
|
* and updates status.
|
|
*
|
|
* @param $id
|
|
* @param $status
|
|
*
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function changeStatus($id, $status)
|
|
{
|
|
// get possible statuses
|
|
$statuses = ContentStatuses::getWithTranslations();
|
|
unset($statuses[Content::STATUS_DELETED]);
|
|
|
|
// check if given status is valid
|
|
if (!isset($statuses[$status])) {
|
|
return redirect()
|
|
->route('content.overview')
|
|
->with('error', trans('admin/contents.invalidStatus'));
|
|
}
|
|
|
|
try {
|
|
// try to find this page
|
|
$page = Content::findOrFail($id);
|
|
} catch (NotFoundHttpException $e) {
|
|
// page not found
|
|
return redirect()
|
|
->route('content.overview')
|
|
->with('error', trans('admin/contents.invalidPage'));
|
|
}
|
|
|
|
// try to change the page
|
|
list ($revision, $changedColumns) = $page->updateWithHistory(['status' => 'status'], ['status' => $status]);
|
|
|
|
// was something changed?
|
|
if ($changedColumns == 0) {
|
|
return redirect()
|
|
->route('content.edit', ['id' => $page->id])
|
|
->with('error', trans('admin/contents.nothingToEdit'));
|
|
}
|
|
|
|
// fire events
|
|
if ($status != Content::STATUS_DRAFT) {
|
|
Event::fire(new PublishContent($page, $revision));
|
|
} else {
|
|
Event::fire(new MakeDraftFromContent($page, $revision));
|
|
}
|
|
|
|
// redirect back to overview with info for user
|
|
return redirect()
|
|
->route('content.overview')
|
|
->with('success', trans('admin/contents.statusChanged'));
|
|
}
|
|
|
|
/**
|
|
* Delete page
|
|
*
|
|
* @param $id
|
|
*
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function deletePage($id)
|
|
{
|
|
try {
|
|
// try to find this page
|
|
$page = Content::findOrFail($id);
|
|
} catch (NotFoundHttpException $e) {
|
|
// page not found
|
|
return redirect()
|
|
->route('content.overview')
|
|
->with('error', trans('admin/contents.invalidPage'));
|
|
}
|
|
|
|
list ($revision, $changedColumns) = $page->updateWithHistory(['status' => 'status'], ['status' => Content::STATUS_DELETED]);
|
|
|
|
// was something changed?
|
|
if ($changedColumns == 0) {
|
|
return redirect()
|
|
->route('content.edit', ['id' => $page->id])
|
|
->with('error', trans('admin/contents.nothingToEdit'));
|
|
}
|
|
|
|
// trigger delete event
|
|
Event::fire(new DeleteContent($page, $revision));
|
|
|
|
// redirect back to overview with info for user
|
|
return redirect()
|
|
->route('content.overview')
|
|
->with('success', trans('admin/contents.pageDeleted', [
|
|
'name' => $page->title,
|
|
'url' => $page->url
|
|
]));
|
|
}
|
|
|
|
/**
|
|
* Sets page as homepage
|
|
*
|
|
* @param $id
|
|
*
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function setHomepage($id)
|
|
{
|
|
try {
|
|
// try to find this page
|
|
/**
|
|
* @var $page Content
|
|
*/
|
|
$page = Content::findOrFail($id);
|
|
} catch (NotFoundHttpException $e) {
|
|
// page not found
|
|
return redirect()
|
|
->route('content.overview')
|
|
->with('error', trans('admin/contents.invalidPage'));
|
|
}
|
|
|
|
// disable any other existing homepage
|
|
Content::where('is_homepage', 1)
|
|
->update(['is_homepage' => 0]);
|
|
|
|
// set new homepage
|
|
list ($revision, $changedColumns) = $page->updateWithHistory(['is_homepage' => 'is_homepage'], ['is_homepage' => 1]);
|
|
|
|
// was something changed?
|
|
if ($changedColumns == 0) {
|
|
return redirect()
|
|
->route('content.overview')
|
|
->with('error', trans('admin/contents.nothingToEdit'));
|
|
}
|
|
|
|
// trigger delete event
|
|
Event::fire(new SetHomepage($page, $revision));
|
|
|
|
// redirect back to overview with info for user
|
|
return redirect()
|
|
->route('content.overview')
|
|
->with('success', trans('admin/contents.homepageSetup', [
|
|
'name' => $page->title,
|
|
'url' => $page->url
|
|
]));
|
|
}
|
|
|
|
/**
|
|
* Convert object of all request data into array of only wanted fields
|
|
*
|
|
* @param Request $request
|
|
* @param $fields
|
|
*
|
|
* @return array
|
|
*/
|
|
public static function getFields(Request $request, $fields)
|
|
{
|
|
$requestData = [];
|
|
|
|
foreach ($fields as $field) {
|
|
$requestData[$field] = $request->$field;
|
|
}
|
|
|
|
return $requestData;
|
|
}
|
|
} |