Skip to content

Commit

Permalink
Merge pull request #4 from sl0wik/feature/language_helpers
Browse files Browse the repository at this point in the history
Language helpers
  • Loading branch information
sl0wik authored Feb 24, 2018
2 parents 5644c9a + 6368fa2 commit 1474812
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 27 deletions.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
"autoload": {
"psr-4": {
"Sl0wik\\Webset\\": "src/"
}
},
"files": [
"src/helpers.php"
]
},
"extra": {
"laravel": {
Expand Down
11 changes: 5 additions & 6 deletions src/Http/Controller/ContentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

namespace Sl0wik\Webset\Http\Controllers;

use Sl0wik\Webset\Models\Content;
use Sl0wik\Webset\Models\Menu;
use Illuminate\Http\Request;
use Mcamara\LaravelLocalization\LaravelLocalization;
use Sl0wik\Webset\Models\Content;

class ContentController extends Controller
{
Expand All @@ -23,10 +22,10 @@ public function showBySlug(Request $request)
{
$uri = str_replace(LaravelLocalization::getCurrentLocale().'/', '', $request->route()->uri);

$content = Content::where([
['url_path', $uri],
['language_code', LaravelLocalization::getCurrentLocale()]
]);
$content = Content::where([
['url_path', $uri],
['language_code', LaravelLocalization::getCurrentLocale()],
]);

return view('pages.content', ['content' => $content->firstOrFail()]);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Controller/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Sl0wik\Webset\Http\Controllers;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Routing\Controller as BaseController;

class Controller extends BaseController
{
Expand Down
15 changes: 7 additions & 8 deletions src/Models/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@

namespace Sl0wik\Webset\Models;

use Sl0wik\Webset\Models\ContentSlide;
use Sl0wik\Webset\Models\Website;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Content extends Model
{
use SoftDeletes;

/**
* Relation with childrens.
*
* @return Illuminate\Database\Eloquent\Relations\HasMany
*/
/**
* Relation with childrens.
*
* @return Illuminate\Database\Eloquent\Relations\HasMany
*/
public function childrens()
{
return $this->hasMany(static::class, 'parent_id');
Expand Down Expand Up @@ -46,7 +44,8 @@ public function website()
*
* @return Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function slides () {
public function slides()
{
return $this->hasMany(ContentSlide::class);
}
}
2 changes: 0 additions & 2 deletions src/Models/ContentSlide.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Sl0wik\Webset\Models;

use Sl0wik\Webset\Models\Content;
use Sl0wik\Webset\Models\Image;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

Expand Down
5 changes: 3 additions & 2 deletions src/Models/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Sl0wik\Webset\Models;

use Sl0wik\LaravelImageEditor\Traits\ImageCache;
use Sl0wik\LaravelImageEditor\Traits\ImageEditor;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Storage;
use Sl0wik\LaravelImageEditor\Traits\ImageCache;
use Sl0wik\LaravelImageEditor\Traits\ImageEditor;

class Image extends Model
{
Expand Down Expand Up @@ -60,6 +60,7 @@ public function getHrefAttribute()
public function href()
{
$href = '/images/'.$this->hash.'/'.$this->size();

return url($href);
}

Expand Down
10 changes: 6 additions & 4 deletions src/Models/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Sl0wik\Webset\Models;

use Sl0wik\Webset\Models\Content;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Route;
Expand All @@ -15,13 +14,14 @@ class Menu extends Model
* Generate menu.
*
* @param string $name
*
* @return array
*/
public static function generate($name)
{
$fields = ['url_path', 'id', 'parent_id', 'head_title', 'menu_title', 'website_id'];

$content = Menu::where('name', $name)
$content = self::where('name', $name)
->with('contents')
->first()
->contents()
Expand All @@ -35,7 +35,7 @@ public static function generate($name)
return $content;
});

return $content;
return $content;
}

/**
Expand Down Expand Up @@ -63,6 +63,7 @@ public static function languages()
->get(['language_code'])
->pluck('language_code')
->toArray();

return Language::whereIn('code', $languageCodes);
}

Expand All @@ -81,7 +82,8 @@ public function getLanguages()
*
* @return Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function contents () {
public function contents()
{
return $this->belongsToMany(Content::class);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Models/Website.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Website extends Model
use SoftDeletes;

/**
* Json objects
* Json objects.
*
* @var array
*/
Expand Down
2 changes: 1 addition & 1 deletion src/WebsetServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ protected function setupConfig()
{
// @TODO
}
}
}
42 changes: 42 additions & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/**
* Returning translated (lozalized) url.
*
* @param string $url url for example /guides/{name}
* @param string|null $attributes For example ['name' => 'Brazil']
*
* @return string Localized url for example /guias/Brazil
*/
function localize_url($url, $attributes = null)
{
$prefix = request()->route()->getPrefix();
if (is_null($prefix)) {
$url = \LaravelLocalization::getLocalizedURL(
false,
$url,
$attributes
);
// LaravelLocalization seems to add locale even when its set to false
$url = str_replace(LaravelLocalization::getCurrentLocale().'/', '', $url);

return $url;
} else {
return \LaravelLocalization::getLocalizedURL(
LaravelLocalization::getCurrentLocale(),
$url,
$attributes
);
}
}

/**
* Return current language code (locale).
* Based on mcamara/laravel-localization function LaravelLocalization::getCurrentLocale().
*
* @return string
*/
function getCurrentLanguageCode()
{
return LaravelLocalization::getCurrentLocale();
}

0 comments on commit 1474812

Please sign in to comment.