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;
@@ -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));
+6 -4
View File
@@ -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
+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,13 +10,20 @@ 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,
'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());
}
}