diff --git a/app/Http/Controllers/Admin/CreateNewContent.php b/app/Http/Controllers/Admin/CreateNewContent.php index e01b0d08..44778f73 100644 --- a/app/Http/Controllers/Admin/CreateNewContent.php +++ b/app/Http/Controllers/Admin/CreateNewContent.php @@ -10,6 +10,7 @@ use App\Models\Module; use App\Models\Template; use Illuminate\Support\Facades\Event; use Illuminate\Support\Str; +use Mcamara\LaravelLocalization\Facades\LaravelLocalization; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; class CreateNewContent extends Controller @@ -56,7 +57,8 @@ class CreateNewContent extends Controller 'templates' => $templates, 'moduleId' => $moduleId, 'page' => $this->content, - 'rootPages' => $this->content->getOverview()->get(), + 'rootPages' => $this->content->getOverview() + ->get(), 'moduleName' => trans($module->name), 'extraData' => call_user_func([$module->handler, 'loadExtraData'], $this->content), 'formSectionNames' => call_user_func([$module->handler, 'getExtendedFormSections']), @@ -86,6 +88,7 @@ class CreateNewContent extends Controller // setup basic info $page->user_id = \Auth::user()->id; + $page->language = LaravelLocalization::getLocale(); $page->title = $request->title; $page->url = Str::slug($request->url); $page->published_from = $request->publishedFrom; @@ -112,7 +115,7 @@ class CreateNewContent extends Controller call_user_func_array([$module->handler, 'createNew'], [&$page, $request]); $page->save(); - + // fire event about creating new content page Event::fire(new CreateNewContentEvent($page)); diff --git a/app/Http/Controllers/Admin/EditContent.php b/app/Http/Controllers/Admin/EditContent.php index 79c8723e..2e3ad79f 100644 --- a/app/Http/Controllers/Admin/EditContent.php +++ b/app/Http/Controllers/Admin/EditContent.php @@ -17,6 +17,7 @@ use App\Models\File; use App\Models\Template; use Illuminate\Support\Facades\Event; use Illuminate\Support\Str; +use Mcamara\LaravelLocalization\Facades\LaravelLocalization; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; class EditContent extends Controller @@ -65,8 +66,8 @@ class EditContent extends Controller 'contentStatuses' => $statuses, 'page' => $page, 'rootPages' => $page->getOverview() - ->where('contents.id', '!=', $page->id) - ->get(), + ->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']), @@ -223,14 +224,14 @@ class EditContent extends Controller // change url to something unique $oldURL = $page->url; - $page->url = uniqid($page->url.'_deleted'); + $page->url = uniqid($page->url . '_deleted'); $page->save(); list ($revision, $changedColumns) = $page->updateWithHistory(['status' => 'status'], ['status' => Content::STATUS_DELETED]); // let module to delete some extra content created during page's life call_user_func([$page->module_handler, 'delete'], $page); - + // was something changed? if ($changedColumns == 0) { return redirect() @@ -274,6 +275,7 @@ class EditContent extends Controller // disable any other existing homepage Content::where('is_homepage', 1) + ->where('language', LaravelLocalization::getCurrentLocale()) ->update(['is_homepage' => 0]); // set new homepage diff --git a/app/Models/Content.php b/app/Models/Content.php index 9172605b..44eb2123 100644 --- a/app/Models/Content.php +++ b/app/Models/Content.php @@ -5,6 +5,7 @@ namespace App\Models; use App\Enumerations\ContentStatuses; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\DB; +use Mcamara\LaravelLocalization\Facades\LaravelLocalization; /** * Class Content @@ -15,6 +16,7 @@ class Content extends Model protected $fillable = [ 'user_id', 'parent_id', + 'language', 'module_handler', 'published_from', 'published_to', @@ -53,6 +55,7 @@ class Content extends Model public function getHomepage() { return $this->getPageQuery() + ->where('language', LaravelLocalization::getCurrentLocale()) ->where('is_homepage', 1) ->first(); } @@ -68,6 +71,7 @@ class Content extends Model { return $this->getPageQuery() ->where('url', $url) + ->where('language', LaravelLocalization::getCurrentLocale()) ->first(); } @@ -81,6 +85,7 @@ class Content extends Model if (!$this->subpagesInit) { $this->subpages = $this->getPageQuery() ->where('parent_id', $this->id) + ->where('language', LaravelLocalization::getCurrentLocale()) ->get(); // convert JSON settings into stdClass @@ -142,6 +147,7 @@ class Content extends Model public function rootPages() { $rootPages = $this->getPageQuery() + ->where('language', LaravelLocalization::getCurrentLocale()) ->where('parent_id', null); if ($this->id) { @@ -166,6 +172,7 @@ class Content extends Model $query = DB::table('contents') ->select('contents.*', 'modules.name AS module_name') ->where('contents.status', '<>', self::STATUS_DELETED) + ->where('contents.language', LaravelLocalization::getCurrentLocale()) ->join('modules', 'modules.handler', '=', 'contents.module_handler') ->orderBy('created_at', 'DESC'); diff --git a/app/Providers/LaravelLocalizationServiceProvider.php b/app/Providers/LaravelLocalizationServiceProvider.php index c4f0ed99..7cbdd327 100644 --- a/app/Providers/LaravelLocalizationServiceProvider.php +++ b/app/Providers/LaravelLocalizationServiceProvider.php @@ -1,7 +1,5 @@ get(); + $languages = DB::table('languages') + ->get(); - $supportedLocales = ['en' => ['native' => 'English', 'script' => 'Latn', 'name' => 'common.lang.english']]; + $supportedLocales = [ + config('app.locale') => [ + 'native' => env('LOCALE_NATIVE_NAME'), + 'script' => env('LOCALE_SCRIPT'), + 'name' => env('LOCALE_TRANSLATED_NAME'), + ] + ]; foreach ($languages as $language) { $supportedLocales[$language->code] = [ 'native' => $language->native, - 'name' => $language->name, + 'name' => $language->name, 'script' => $language->script ]; } @@ -30,7 +35,8 @@ class LaravelLocalizationServiceProvider extends ServiceProvider ]); } - public function boot() { - \App::setLocale(LaravelLocalization::setLocale()); + public function boot() + { + \App::setLocale(LaravelLocalization::getCurrentLocale()); } } \ No newline at end of file diff --git a/config/database.php b/config/database.php index 66e88a90..dace41ee 100644 --- a/config/database.php +++ b/config/database.php @@ -59,7 +59,7 @@ return [ 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), 'charset' => 'utf8', - 'collation' => 'utf8_unicode_ci', + 'collation' => 'utf8_general_ci', 'prefix' => '', 'strict' => false, ], diff --git a/database/migrations/.gitkeep b/database/migrations/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/database/migrations/2016_07_22_095923_AddLanguageToContentPages.php b/database/migrations/2016_07_22_095923_AddLanguageToContentPages.php new file mode 100644 index 00000000..f154677b --- /dev/null +++ b/database/migrations/2016_07_22_095923_AddLanguageToContentPages.php @@ -0,0 +1,75 @@ +getDoctrineSchemaManager() + ->getDatabasePlatform(); + $platform->registerDoctrineTypeMapping('enum', 'string'); + + DB::raw('ALTER TABLE `languages` + CHANGE `code` `code` varchar(8) COLLATE \'utf8_general_ci\' NOT NULL FIRST, + CHANGE `name` `name` varchar(255) COLLATE \'utf8_general_ci\' NOT NULL COMMENT \'Translated name for given language\' AFTER `code`, + CHANGE `script` `script` varchar(16) COLLATE \'utf8_general_ci\' NOT NULL DEFAULT \'Latn\' COMMENT \'ISO code for locale script (Latn, Arab, ...)\' AFTER `name`, + CHANGE `native` `native` varchar(255) COLLATE \'utf8_general_ci\' NOT NULL COMMENT \'Native name for language\' AFTER `script`, + COLLATE \'utf8_general_ci\';'); + + Schema::table('contents', function (Blueprint $table) { + $table->string('language', 8) + ->after('template') + ->default(\App::getLocale()); + }); + + Schema::table('contents', function (Blueprint $table) { + $table->index('language') + ->change(); + + $table->unique(['language', 'url']); + + $doctrine = Schema::getConnection() + ->getDoctrineSchemaManager() + ->listTableDetails('contents'); + + // remove unique index from URL column + if ($doctrine->hasIndex('url_UNIQUE')) { + $table->dropUnique('url_UNIQUE'); + } else { + $table->dropUnique('contents_url_unique'); + } + + $table->foreign('language') + ->references('code') + ->on('languages') + ->onDelete('cascade') + ->onUpdate('cascade'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('contents', function (Blueprint $table) { + $table->dropForeign('contents_language_foreign'); + $table->dropIndex('contents_language_index'); + $table->dropUnique('contents_language_url_unique'); + $table->dropColumn('language'); + $table->unique('url'); + }); + } +}