From e789b02a93a97251439fccfa8b35b4e0ac2afd7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Kv=C3=A1=C4=8Dek?= Date: Thu, 31 Mar 2016 21:12:54 +0200 Subject: [PATCH] FEATURE #1523: add subpages for content --- app/Console/Commands/SetupDatabase.php | 65 +++++++++++++++++++ .../Controllers/Admin/CreateNewContent.php | 8 ++- app/Http/Controllers/Admin/EditContent.php | 38 ++++++----- app/Http/Requests/CreateNewContentRequest.php | 3 +- app/Http/Requests/EditContentRequest.php | 3 +- app/Models/Content.php | 31 ++++++++- .../2016_03_31_175822_AddSubpages.php | 42 ++++++++++++ resources/lang/en/admin/contents.php | 3 +- resources/lang/en/admin/history.php | 1 + .../formParts/basicInformation.blade.php | 22 ++++++- 10 files changed, 192 insertions(+), 24 deletions(-) create mode 100644 app/Console/Commands/SetupDatabase.php create mode 100644 database/migrations/2016_03_31_175822_AddSubpages.php diff --git a/app/Console/Commands/SetupDatabase.php b/app/Console/Commands/SetupDatabase.php new file mode 100644 index 00000000..05f3ace2 --- /dev/null +++ b/app/Console/Commands/SetupDatabase.php @@ -0,0 +1,65 @@ +argument('host'); + $user = $this->argument('user'); + $password = $this->argument('password'); + $database = $this->argument('database'); + + // try to connect with this settings + // @todo add credentials verification + + // if pass, proceed to creating .env file + $parameters = [ + 'DB_HOST' => $host, + 'DB_USERNAME' => $user, + 'DB_PASSWORD' => $password, + 'DB_DATABASE' => $database + ]; + + $envPath = base_path('.env'); + $env = file_get_contents($envPath); + if (!file_exists($envPath)) + $env = file_get_contents($envPath.'.example'); + + foreach ($parameters as $parameter => $value) { + $env = preg_replace('/'.$parameter.'=(.*)/i', $parameter.'='.$value, $env); + } + + file_put_contents($envPath, $env); + + + } +} diff --git a/app/Http/Controllers/Admin/CreateNewContent.php b/app/Http/Controllers/Admin/CreateNewContent.php index 74de8fa4..10e1c17e 100644 --- a/app/Http/Controllers/Admin/CreateNewContent.php +++ b/app/Http/Controllers/Admin/CreateNewContent.php @@ -6,7 +6,6 @@ use App\Http\Requests\CreateNewContentRequest; use App\Models\Content; use App\Models\Module; use App\Models\Template; -use Illuminate\Support\Facades\Event; use Illuminate\Support\Str; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; @@ -39,6 +38,7 @@ class CreateNewContent extends Controller 'templates' => $templates, 'moduleId' => $moduleId, 'page' => $page, + 'rootPages' => $page->rootPages(), 'moduleName' => trans($module->name), 'formSectionNames' => call_user_func([$module->handler, 'getExtendedFormSections']), 'formSections' => call_user_func([$module->handler, 'getExtendedFormInputsCreate']), @@ -78,13 +78,15 @@ class CreateNewContent extends Controller $page->template = $request->template; $page->module_handler = $module->handler; + if ($request->parentPage) { + $page->parent_id = $request->parentPage; + } + // save module-specific stuff call_user_func_array([$module->handler, 'createNew'], [&$page, $request]); $page->save(); - Event::fire(new \Modules\StaticPages\Events\CreateNewStaticPage($page)); - return redirect() ->route('content.overview') ->with('success', trans('admin/contents.successfullyCreated')); diff --git a/app/Http/Controllers/Admin/EditContent.php b/app/Http/Controllers/Admin/EditContent.php index 25ac10f9..c6e22d7d 100644 --- a/app/Http/Controllers/Admin/EditContent.php +++ b/app/Http/Controllers/Admin/EditContent.php @@ -7,12 +7,7 @@ use App\Http\Requests\EditContentRequest; use App\Models\Content; use App\Models\ContentHistory; use App\Models\Template; -use Illuminate\Support\Facades\Event; use Illuminate\Support\Str; -use Modules\StaticPages\Events\DeleteStaticPage; -use Modules\StaticPages\Events\EditStaticPage as EditStaticPageEvent; -use Modules\StaticPages\Events\MakeDraftFromStaticPage; -use Modules\StaticPages\Events\PublishStaticPage; use Modules\StaticPages\Http\Requests\EditStaticPageRequest; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; @@ -56,6 +51,7 @@ class EditContent extends Controller return view('admin.content.edit', [ 'contentStatuses' => $statuses, 'page' => $page, + 'rootPages' => $page->rootPages(), 'templates' => $templates, 'formSectionNames' => call_user_func([$page->module_handler, 'getExtendedFormSections']), 'formSections' => call_user_func([$page->module_handler, 'getExtendedFormInputsCreate']), @@ -84,6 +80,7 @@ class EditContent extends Controller } $request->url = Str::slug($request->url); + $request->parentPage = $request->parentPage ? $request->parentPage : null; // array of fields in database and fields from request $fields = [ @@ -95,11 +92,12 @@ class EditContent extends Controller 'meta_keywords' => 'metaKeywords', 'meta_description' => 'metaDescription', 'content' => 'pageContent', + 'parent_id' => 'parentPage', 'status' => 'status' ]; // save new values with history - list($revision, $changedColumns) = $page->updateWithHistory($fields, $request->all()); + list($revision, $changedColumns) = $page->updateWithHistory($fields, $this->getFields($request, $fields)); $changedColumns += call_user_func_array([$page->module_handler, 'edit'], [&$page, $request, $revision]); @@ -111,7 +109,7 @@ class EditContent extends Controller } // fire event with - Event::fire(new EditStaticPageEvent($page, $revision)); + // Event::fire(new EditStaticPageEvent($page, $revision)); // something was changed, inform user return redirect() @@ -121,7 +119,6 @@ class EditContent 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. * @@ -164,11 +161,11 @@ class EditContent extends Controller } // fire events - if ($status != Content::STATUS_DRAFT) { - Event::fire(new PublishStaticPage($page, $revision)); - } else { - Event::fire(new MakeDraftFromStaticPage($page, $revision)); - } + // 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() @@ -205,7 +202,7 @@ class EditContent extends Controller } // trigger delete event - Event::fire(new DeleteStaticPage($page, $revision)); + // Event::fire(new DeleteStaticPage($page, $revision)); // redirect back to overview with info for user return redirect() @@ -253,7 +250,7 @@ class EditContent extends Controller } // trigger delete event -// Event::fire(new DeleteStaticPage($page, $revision)); + // Event::fire(new DeleteStaticPage($page, $revision)); // redirect back to overview with info for user return redirect() @@ -263,4 +260,15 @@ class EditContent extends Controller 'url' => $page->url ])); } + + private function getFields(EditContentRequest $request, $fields) + { + $requestData = []; + + foreach ($fields as $field) { + $requestData[$field] = $request->$field; + } + + return $requestData; + } } \ No newline at end of file diff --git a/app/Http/Requests/CreateNewContentRequest.php b/app/Http/Requests/CreateNewContentRequest.php index caf34a91..3d995ba6 100644 --- a/app/Http/Requests/CreateNewContentRequest.php +++ b/app/Http/Requests/CreateNewContentRequest.php @@ -19,7 +19,8 @@ class CreateNewContentRequest extends Request 'published_to' => 'date', 'title_photo' => 'exists:files,id', 'module_settings' => 'json', - 'status' => 'required|in:draft,protected,public' + 'status' => 'required|in:draft,protected,public', + 'parentPage' => 'exists:contents,id' ]; } } diff --git a/app/Http/Requests/EditContentRequest.php b/app/Http/Requests/EditContentRequest.php index eccee379..f92504b4 100644 --- a/app/Http/Requests/EditContentRequest.php +++ b/app/Http/Requests/EditContentRequest.php @@ -24,7 +24,8 @@ class EditContentRequest extends Request 'published_from' => 'date', 'published_to' => 'date', 'title_photo' => 'exists:files,id', - 'status' => 'required|in:' . $statuses + 'status' => 'required|in:' . $statuses, + 'parentPage' => 'exists:contents,id' ]; } } diff --git a/app/Models/Content.php b/app/Models/Content.php index 9070e2fd..70fa0972 100644 --- a/app/Models/Content.php +++ b/app/Models/Content.php @@ -43,7 +43,6 @@ class Content extends Model /** * Getter for homepage content page - * * @return Content */ public function getHomepage() @@ -68,8 +67,36 @@ class Content extends Model } /** - * Getter for overview of all content pages. + * Get list of all subpages * + * @return \Illuminate\Database\Eloquent\Relations\HasMany + */ + public function subpages() + { + return $this->getPageQuery() + ->where('parent_id', $this->id) + ->get(); + } + + /** + * Returns list of all root pages - those pages, which parent_id is null + * + * @return mixed + */ + public function rootPages() + { + $rootPages = $this->getPageQuery() + ->where('parent_id', null); + + if ($this->id) { + $rootPages = $rootPages->where('id', '!=', $this->id); + } + + return $rootPages->get(); + } + + /** + * Getter for overview of all content pages. * These content pages may be filtered by using some handler * * @param bool $handler diff --git a/database/migrations/2016_03_31_175822_AddSubpages.php b/database/migrations/2016_03_31_175822_AddSubpages.php new file mode 100644 index 00000000..93e95508 --- /dev/null +++ b/database/migrations/2016_03_31_175822_AddSubpages.php @@ -0,0 +1,42 @@ +integer('parent_id') + ->after('id') + ->nullable() + ->default(null) + ->index() + ->comment('ID of parent page'); + + $table->foreign('parent_id') + ->references('id') + ->on('contents') + ->onDelete('cascade') + ->onUpdate('cascade'); + }); + } + + /** + * Reverse the migrations. + * @return void + */ + public function down() + { + Schema::table('contents', function (Blueprint $table) { + $table->dropForeign('contents_parent_id_foreign'); + $table->dropColumn('parent_id'); + }); + } +} diff --git a/resources/lang/en/admin/contents.php b/resources/lang/en/admin/contents.php index cce837e0..2f6175fd 100644 --- a/resources/lang/en/admin/contents.php +++ b/resources/lang/en/admin/contents.php @@ -35,5 +35,6 @@ return [ 'template' => 'Template of this page', 'overview' => 'List of all content pages', 'homepageSetup' => 'Homepage successfully set up.', - 'setHomepage' => 'Set page as homepage' + 'setHomepage' => 'Set page as homepage', + 'noParentPage' => ' -- No parent page -- ' ]; \ No newline at end of file diff --git a/resources/lang/en/admin/history.php b/resources/lang/en/admin/history.php index e0235ded..0f40784e 100644 --- a/resources/lang/en/admin/history.php +++ b/resources/lang/en/admin/history.php @@ -9,6 +9,7 @@ return [ 'columnStatus' => 'Page visibility', 'columnTemplate' => 'Template', 'columnIs_homepage' => 'Homepage', + 'columnParentPage' => 'Parent page', 'emptyValue' => 'none', 'changedFromTo' => 'Changed from :from to :to', 'numOfChanges' => 'One change|:count changes', diff --git a/resources/views/admin/content/formParts/basicInformation.blade.php b/resources/views/admin/content/formParts/basicInformation.blade.php index 5069d967..d6bebc2d 100644 --- a/resources/views/admin/content/formParts/basicInformation.blade.php +++ b/resources/views/admin/content/formParts/basicInformation.blade.php @@ -24,7 +24,7 @@
-
+
+ + @if (isset($errors) && $errors->has('status')) +
+ {{ $errors->first('status') }} +
+ @endif +