FEATURE: add language to content pages

This commit is contained in:
Michal Kváček
2016-07-22 14:55:51 +02:00
parent 2189bbed5e
commit f90aee4108
7 changed files with 107 additions and 14 deletions
@@ -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;
@@ -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
@@ -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
+7
View File
@@ -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');
@@ -1,7 +1,5 @@
<?php namespace App\Providers;
use App\Models\File;
use App\Models\Language;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\ServiceProvider;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
@@ -12,9 +10,16 @@ class LaravelLocalizationServiceProvider extends ServiceProvider
public function register()
{
$languages = DB::table('languages')->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,
@@ -30,7 +35,8 @@ class LaravelLocalizationServiceProvider extends ServiceProvider
]);
}
public function boot() {
\App::setLocale(LaravelLocalization::setLocale());
public function boot()
{
\App::setLocale(LaravelLocalization::getCurrentLocale());
}
}
+1 -1
View File
@@ -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,
],
View File
@@ -0,0 +1,75 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
class AddLanguageToContentPages extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$platform = Schema::getConnection()
->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');
});
}
}