162 lines
4.9 KiB
PHP
162 lines
4.9 KiB
PHP
<?php
|
|
namespace App\Http\Controllers\Admin\Contents;
|
|
|
|
use App\CMS\Enumerations\ContentStatuses;
|
|
use App\Events\DeleteContent;
|
|
use App\Events\MakeDraftFromContent;
|
|
use App\Events\PublishContent;
|
|
use App\Events\SetHomepage;
|
|
use App\Http\Controllers\Admin\Controller;
|
|
use App\Models\Content\Content;
|
|
use Illuminate\Support\Facades\Event;
|
|
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
|
|
class ContentActions extends Controller
|
|
{
|
|
/**
|
|
* 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 delete($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
|
|
]));
|
|
}
|
|
} |