Files
core/app/Http/Controllers/Admin/Contents/EditContent.php
T

321 lines
10 KiB
PHP

<?php
namespace App\Http\Controllers\Admin\Contents;
use App\CMS\Enumerations\ContentStatuses;
use App\Cms\Handlers\Interactions\Interaction;
use App\CMS\Helpers\CurrentTemplate;
use App\Cms\Interactions\Admin\Content\EditContent as EditContentInteraction;
use App\Events\AttemptToEditInvalidContent;
use App\Events\DeleteContent;
use App\Events\MakeDraftFromContent;
use App\Events\PublishContent;
use App\Events\SetHomepage;
use App\Http\Controllers\Admin\Controller;
use App\Http\Requests\EditContentRequest;
use App\Http\Requests\Request;
use App\Models\Content\Content;
use App\Models\Content\ContentHistory;
use App\Models\FileManager\File;
use App\Models\Module;
use App\Models\Templates\Template;
use Illuminate\Support\Facades\Event;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use Modules\Categories\Entities\Category;
use Pingpong\Modules\Facades\Module as ModuleInstance;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class EditContent extends Controller
{
/**
* @var ContentHistory
*/
private $history;
/**
* @var File
*/
private $file;
/**
* EditContent constructor.
*
* Load ContentHistory and File into this controller
*
* @param ContentHistory $history
* @param File $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);
// get module ID
$module = Module::where('handler', $page->module_handler)
->firstOrFail();
$pageHeading = trans('admin/contents.edit', ['name' => e($page->title)]);
if (is_callable([$module->handler, 'getCreateNewTitle'])) {
$pageHeading = call_user_func([$module->handler, 'getEditTitle'], $page);
}
// get all Blade templates for rendering page
$templates = Template::where('module_id', $module->id)
->select(['template_name', 'name', 'description'])
->get();
// get module for Categories - PingPong module
$categoryModule = ModuleInstance::get('categories');
// add categories, if supported
$categories = [];
if ($module->support_categories && !is_null($categoryModule) && $categoryModule->enabled()) {
$categories = Category::where('module_id', $module->id)
->get();
}
// modify data for edit form according to installed template
call_user_func_array([CurrentTemplate::handler(), 'beforePopulatingEditForm'], [&$page]);
$history = $this->history->getStructuredHistoryOf($page);
// render view
return view('admin.content.edit', [
'pageHeading' => $pageHeading,
'contentStatuses' => $statuses,
'page' => $page,
'module' => $module,
'categories' => $categories,
'rootPages' => $page->getOverview()
->where('contents.id', '!=', $page->id)
->get(),
'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(Request $request, $id)
{
try {
/**
* @var $page Content
*/
$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'));
}
// edit page
list($page, $revision, $changedColumns) = Interaction::handle($request, new EditContentInteraction(), $page);
// was something changed?
if ($changedColumns == 0) {
return redirect()
->route('content.edit', ['id' => $page->id])
->with('error', trans('admin/contents.nothingToEdit'));
}
// something was changed, inform user
return redirect()
->route('content.overview')
->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();
// 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'));
}
// change url to something unique
$oldURL = $page->url;
$page->url .= uniqid('_deleted_');
$page->save();
// delete page
$page->delete();
// let module to delete some extra content created during page's life
call_user_func([$page->module_handler, 'delete'], $page);
// trigger delete event
Event::fire(new DeleteContent($page));
// redirect back to overview with info for user
return redirect()
->route('content.overview')
->with('success', trans('admin/contents.pageDeleted', [
'name' => $page->title,
'url' => $oldURL
]));
}
/**
* 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()
->back()
->with('error', trans('admin/contents.invalidPage'));
}
// disable any other existing homepage
Content::where('is_homepage', 1)
->where('language', LaravelLocalization::getCurrentLocale())
->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()
->back()
->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()
->back()
->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;
}
}