From eed7e156508147bab407c8851bba15f70d4c63e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Kv=C3=A1=C4=8Dek?= Date: Fri, 11 Mar 2016 15:47:27 +0100 Subject: [PATCH] FEATURE #1507: adapt static pages for new modular content management --- modules/StaticPages/Handlers/StaticPage.php | 57 ++++- .../Http/Controllers/Admin/Controller.php | 9 - .../Controllers/Admin/CreateNewStaticPage.php | 70 ------ .../Http/Controllers/Admin/EditStaticPage.php | 213 ------------------ .../Controllers/Admin/StaticPageOverview.php | 22 -- .../Controllers/StaticPagesController.php | 12 - .../Requests/CreateNewStaticPageRequest.php | 26 --- .../Http/Requests/EditStaticPageRequest.php | 30 --- modules/StaticPages/Http/routes.php | 53 ----- .../Providers/StaticPagesServiceProvider.php | 7 +- .../StaticPages/Resources/lang/en/admin.php | 35 +-- .../Resources/lang/en/adminMenu.php | 4 +- 12 files changed, 62 insertions(+), 476 deletions(-) delete mode 100644 modules/StaticPages/Http/Controllers/Admin/Controller.php delete mode 100644 modules/StaticPages/Http/Controllers/Admin/CreateNewStaticPage.php delete mode 100644 modules/StaticPages/Http/Controllers/Admin/EditStaticPage.php delete mode 100644 modules/StaticPages/Http/Controllers/Admin/StaticPageOverview.php delete mode 100644 modules/StaticPages/Http/Controllers/StaticPagesController.php delete mode 100644 modules/StaticPages/Http/Requests/CreateNewStaticPageRequest.php delete mode 100644 modules/StaticPages/Http/Requests/EditStaticPageRequest.php diff --git a/modules/StaticPages/Handlers/StaticPage.php b/modules/StaticPages/Handlers/StaticPage.php index abb34bd5..6bf9e3ae 100644 --- a/modules/StaticPages/Handlers/StaticPage.php +++ b/modules/StaticPages/Handlers/StaticPage.php @@ -1,8 +1,10 @@ dropdown(trans('staticpages::adminMenu.staticPages'), function ($submenu) use ($user) { - $submenu->route('staticpage.new', trans('staticpages::adminMenu.new'), [], 1, ['icon' => 'fa fa-plus-square-o']); - $submenu->route('staticpage.overview', trans('staticpages::adminMenu.overview'), [], 20, ['icon' => 'fa fa-list']); + $submenu->route('content.new', trans('staticpages::adminMenu.new'), ['moduleId' => self::getModuleId()], 10, ['icon' => 'fa fa-plus']); + $submenu->route('content.overview', trans('staticpages::adminMenu.overview'), ['moduleId' => self::getModuleId()], 20, ['icon' => 'fa fa-list']); }, 15, ['icon' => 'fa fa-newspaper-o']); } + public static function getModuleId() + { + $allModules = ModulesHandlerToId::getWithTranslations(); + $moduleId = $allModules[config('staticpages.handler')]; + + return $moduleId; + } + /** * Render given module into visitor area. * @@ -32,4 +42,47 @@ class StaticPage extends Handler { return Controller::VisitorsView($content->template, ['page' => $content]); } + + /** + * Does given module manage content? Or is it just module with widgets/something internal/something absolutely else? + * + * @return bool + */ + public static function managesContent() + { + return true; + } + + /** + * Save module-specific values into database + * + * @param Content $content + * @param Request $request + * + * @return mixed + */ + public static function createNew(Content &$content, Request $request) + { + // There is nothing more we can save. Static pages are only text saved into database. + // This funcionality is already covered in saving content page alone. + } + + /** + * Method used for editing existing content page. + * + * Reason why it is separated from createNew() method is simple - we need to save history of changes. + * + * @param Content $content + * @param Request $request + * @param string $revisionId + * + * @return mixed + */ + public static function edit(Content &$content, Request $request, $revisionId) + { + // There is nothing more we can save. Static pages are only text saved into database. + // This funcionality is already covered in saving content page alone. + + return 0; + } } \ No newline at end of file diff --git a/modules/StaticPages/Http/Controllers/Admin/Controller.php b/modules/StaticPages/Http/Controllers/Admin/Controller.php deleted file mode 100644 index 8e937831..00000000 --- a/modules/StaticPages/Http/Controllers/Admin/Controller.php +++ /dev/null @@ -1,9 +0,0 @@ -select(['template_name', 'name', 'description']) - ->get(); - - // render view - return view('staticpages::admin.new', [ - 'contentStatuses' => $statuses, - 'templates' => $templates, - 'selectedStatus' => Content::STATUS_DRAFT - ]); - } - - /** - * Save new static page - * - * @param CreateNewStaticPageRequest $request - * - * @return \Illuminate\Http\RedirectResponse - */ - public function postNew(CreateNewStaticPageRequest $request) - { - $page = new Content(); - - $page->user_id = \Auth::user()->id; - $page->title = $request->title; - $page->url = $request->url; - $page->published_from = $request->publishedFrom; - $page->published_to = $request->publishedTo; - $page->meta_keywords = $request->meta_description; - $page->meta_description = $request->meta_description; - $page->content = $request->pageContent; - $page->status = $request->status; - $page->module_handler = config('staticpages.handler'); - $page->template = $request->template; - - $page->save(); - - Event::fire(new \Modules\StaticPages\Events\CreateNewStaticPage($page)); - - return redirect() - ->route('staticpage.new') - ->with('success', trans('staticpages::admin.successfullyCreated')); - } -} \ No newline at end of file diff --git a/modules/StaticPages/Http/Controllers/Admin/EditStaticPage.php b/modules/StaticPages/Http/Controllers/Admin/EditStaticPage.php deleted file mode 100644 index 14f3c313..00000000 --- a/modules/StaticPages/Http/Controllers/Admin/EditStaticPage.php +++ /dev/null @@ -1,213 +0,0 @@ -history = $history; - } - - /** - * 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::where('module_handler', config('staticpages.handler')) - ->findOrFail($id); - - $allModules = ModulesHandlerToId::getWithTranslations(); - $moduleId = $allModules[config('staticpages.handler')]; - $templates = Template::where('module_id', $moduleId) - ->select(['template_name', 'name', 'description']) - ->get(); - - $history = $this->history->getStructuredHistoryOf($page); - - // render view - return view('staticpages::admin.edit', [ - 'contentStatuses' => $statuses, - 'page' => $page, - 'templates' => $templates, - 'historySize' => count($history['changesets']) - ]); - } - - /** - * Edit static page and save changes into history. - * - * @param EditStaticPageRequest $request - * @param $id - * - * @return \Illuminate\Http\RedirectResponse - */ - public function postEdit(EditStaticPageRequest $request, $id) - { - try { - $page = Content::where('module_handler', config('staticpages.handler')) - ->findOrFail($id); - } catch (NotFoundHttpException $e) { - - // page for editing not found - return redirect() - ->route('staticpage.overview') - ->with('error', trans('staticpages::admin.invalidPage')); - } - - // 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', - 'status' => 'status' - ]; - - // save new values with history - list($revision, $changedColumns) = $page->updateWithHistory($fields, $request->all()); - - // was something changed? - if ($changedColumns == 0) { - return redirect() - ->route('staticpage.edit', ['id' => $page->id]) - ->with('error', trans('staticpages::admin.nothingToEdit')); - } - - // fire event with - Event::fire(new EditStaticPageEvent($page, $revision)); - - // something was changed, inform user - return redirect() - ->route('staticpage.edit', ['id' => $page->id]) - ->with('success', trans('staticpages::admin.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('staticpage.overview') - ->with('error', trans('staticpages::admin.invalidStatus')); - } - - try { - // try to find this page - $page = Content::where('module_handler', config('staticpages.handler')) - ->findOrFail($id); - } catch (NotFoundHttpException $e) { - // page not found - return redirect() - ->route('staticpage.overview') - ->with('error', trans('staticpages::admin.invalidPage')); - } - - // try to change the page - list ($revision, $changedColumns) = $page->updateWithHistory(['status' => 'status'], ['status' => $status]); - - // was something changed? - if ($changedColumns == 0) { - return redirect() - ->route('staticpage.edit', ['id' => $page->id]) - ->with('error', trans('staticpages::admin.nothingToEdit')); - } - - // fire events - if ($status != Content::STATUS_DRAFT) { - Event::fire(new PublishStaticPage($page, $revision)); - } else { - Event::fire(new MakeDraftFromStaticPage($page, $revision)); - } - - // redirect back to overview with info for user - return redirect() - ->route('staticpage.overview') - ->with('success', trans('staticpages::admin.statusChanged')); - } - - /** - * Delete page - * - * @param $id - * - * @return \Illuminate\Http\RedirectResponse - */ - public function deletePage($id) - { - try { - // try to find this page - $page = Content::where('module_handler', config('staticpages.handler')) - ->findOrFail($id); - } catch (NotFoundHttpException $e) { - // page not found - return redirect() - ->route('staticpage.overview') - ->with('error', trans('staticpages::admin.invalidPage')); - } - - list ($revision, $changedColumns) = $page->updateWithHistory(['status' => 'status'], ['status' => Content::STATUS_DELETED]); - - // was something changed? - if ($changedColumns == 0) { - return redirect() - ->route('staticpage.edit', ['id' => $page->id]) - ->with('error', trans('staticpages::admin.nothingToEdit')); - } - - // trigger delete event - Event::fire(new DeleteStaticPage($page, $revision)); - - // redirect back to overview with info for user - return redirect() - ->route('staticpage.overview') - ->with('success', trans('staticpages::admin.pageDeleted', [ - 'name' => $page->title, - 'url' => $page->url - ])); - } -} \ No newline at end of file diff --git a/modules/StaticPages/Http/Controllers/Admin/StaticPageOverview.php b/modules/StaticPages/Http/Controllers/Admin/StaticPageOverview.php deleted file mode 100644 index 3abc63f6..00000000 --- a/modules/StaticPages/Http/Controllers/Admin/StaticPageOverview.php +++ /dev/null @@ -1,22 +0,0 @@ -getByType(config('staticpages.handler'), true); - - // add filtering by status - if (!is_null($status) && in_array($status, ContentStatuses::getSimple())) { - $pages = $pages->where('status', $status); - } - $pages = $pages->paginate($perPage); - - return view('staticpages::admin.overview', ['pages' => $pages]); - } -} \ No newline at end of file diff --git a/modules/StaticPages/Http/Controllers/StaticPagesController.php b/modules/StaticPages/Http/Controllers/StaticPagesController.php deleted file mode 100644 index 7b6c8bc0..00000000 --- a/modules/StaticPages/Http/Controllers/StaticPagesController.php +++ /dev/null @@ -1,12 +0,0 @@ - 'required', - 'url' => 'required|unique:contents,url', - 'template' => 'required|exists:templates,template_name', - 'published_from' => 'date', - 'published_to' => 'date', - 'title_photo' => 'exists:files,id', - 'module_settings' => 'json', - 'status' => 'required|in:draft,protected,public' - ]; - } -} diff --git a/modules/StaticPages/Http/Requests/EditStaticPageRequest.php b/modules/StaticPages/Http/Requests/EditStaticPageRequest.php deleted file mode 100644 index 8e7ecb18..00000000 --- a/modules/StaticPages/Http/Requests/EditStaticPageRequest.php +++ /dev/null @@ -1,30 +0,0 @@ - 'required', - 'url' => 'required|unique:contents,url,'.$this->id, - 'template' => 'required|exists:templates,template_name', - 'published_from' => 'date', - 'published_to' => 'date', - 'title_photo' => 'exists:files,id', - 'status' => 'required|in:'.$statuses - ]; - } -} diff --git a/modules/StaticPages/Http/routes.php b/modules/StaticPages/Http/routes.php index b0adb65b..b3d9bbc7 100644 --- a/modules/StaticPages/Http/routes.php +++ b/modules/StaticPages/Http/routes.php @@ -1,54 +1 @@ 'web', - 'prefix' => 'page', - 'namespace' => 'Modules\StaticPages\Http\Controllers' - ], function () { - Route::get('/{url}', 'StaticPagesController@index'); -}); - -// routes group for admin area -Route::group([ - 'middleware' => ['web', 'auth'], - 'prefix' => 'admin/pages', - 'namespace' => 'Modules\StaticPages\Http\Controllers\Admin' - ], function () { - - // list of all static pages - Route::get('overview', 'StaticPageOverview@getOverview') - ->name('staticpage.overview'); - - // form for creating new static page - Route::get('new', 'CreateNewStaticPage@getNew') - ->name('staticpage.new'); - - // form for editing existing static page - Route::get('edit/{id}', 'EditStaticPage@getEdit') - ->name('staticpage.edit'); - - // post method for creating new static page - Route::post('new', 'CreateNewStaticPage@postNew') - ->name('staticpage.newPost'); - - // post method for creating new static page - Route::post('edit/{id}', 'EditStaticPage@postEdit') - ->name('staticpage.editPost'); - - // routes for quick actions - - // change status of page (protected, public or draft) - Route::get('status/{id}/{status}', 'EditStaticPage@changeStatus') - ->name('staticpage.changeStatus'); - - - // delete page - Route::get('delete/{id}', 'EditStaticPage@deletePage') - ->name('staticpage.delete'); - - // display change history of given page - // Route::get('history/{id}', 'StaticPageHistory@getHistory')->name('staticpage.history'); - - - // Route::get('', '')->name('staticpage.'); -}); \ No newline at end of file diff --git a/modules/StaticPages/Providers/StaticPagesServiceProvider.php b/modules/StaticPages/Providers/StaticPagesServiceProvider.php index b4b6531e..609c1100 100644 --- a/modules/StaticPages/Providers/StaticPagesServiceProvider.php +++ b/modules/StaticPages/Providers/StaticPagesServiceProvider.php @@ -48,7 +48,7 @@ class StaticPagesServiceProvider extends ServiceProvider protected function install() { $handler = config('staticpages.handler'); - $installedModules = Modules::getWithTranslations(); + $installedModules = ModulesHandlerToId::getWithTranslations(); if (!isset($installedModules[$handler])) { // insert module into database @@ -121,13 +121,10 @@ class StaticPagesServiceProvider extends ServiceProvider */ $registrator = $this->app->make('registerTemplateDir'); try { - $allModules = ModulesHandlerToId::getWithTranslations(); - $moduleId = $allModules[config('staticpages.handler')]; - // setup some global variables $registrator->setBaseDirectory('resources/views/modules/staticpages/visitor'); $registrator->setNamespace('staticpages'); - $registrator->setModuleId($moduleId); + $registrator->setModuleId(call_user_func([config('staticpages.handler'), 'getModuleId'])); // register path with current template name $registrator->register(config('app.theme'), 'staticpages::admin.templatePath'); diff --git a/modules/StaticPages/Resources/lang/en/admin.php b/modules/StaticPages/Resources/lang/en/admin.php index a799cdfa..c7ee3368 100644 --- a/modules/StaticPages/Resources/lang/en/admin.php +++ b/modules/StaticPages/Resources/lang/en/admin.php @@ -1,35 +1,6 @@ 'List of all static pages', - 'new' => 'Create new static page', - 'content' => 'Page content', - 'save' => 'Save new page', - 'basicInfo' => 'Basic information', - 'metaOptions' => 'Meta information', - 'publicationOptions' => 'Publication setup', - 'field_title' => 'Page title', - 'field_url' => 'URL', - 'field_meta_keywords' => 'Keywords', - 'field_meta_description' => 'Page description', - 'field_published_from' => 'Published from', - 'field_published_to' => 'Published to', - 'successfullyCreated' => 'Page successfully created', - 'noPagesAvailable' => 'No static pages available. Try to create one first.', - 'viewPage' => 'View page on web', - 'edit' => 'Edit page :name', - 'deletePage' => 'Delete page (ireversible!)', - 'makePublic' => 'Publish page', - 'makeProtected' => 'Publish page only for logged users', - 'makeDraft' => 'Disable page and make it draft', - 'invalidStatus' => 'Cannot change status to something undefined.', - 'statusChanged' => 'Status successfully changed.', - 'invalidPage' => 'Specified page does not exist.', - 'pageDeleted' => 'Page :name (:url) successfully deleted.', - 'nothingToEdit' => 'You did not submit any changes. Nothing was edited.', - 'successfullyEdited' => 'Edit successfull', - 'showHistory' => 'History (:count)', - 'templatePath' => 'Static pages', - 'appearance' => 'Appearance', - 'template' => 'Template of this page' - + 'overview' => 'List of all static pages', + 'templatePath' => 'Static pages', + 'new' => 'nova stranka' ]; \ No newline at end of file diff --git a/modules/StaticPages/Resources/lang/en/adminMenu.php b/modules/StaticPages/Resources/lang/en/adminMenu.php index 8ca1327d..ae842272 100644 --- a/modules/StaticPages/Resources/lang/en/adminMenu.php +++ b/modules/StaticPages/Resources/lang/en/adminMenu.php @@ -1,6 +1,6 @@ 'Static pages', - 'new' => 'Create new', - 'overview' => 'Overview' + 'new' => 'Create new', + 'overview' => 'Overview' ]; \ No newline at end of file