locale settings

This commit is contained in:
Kerim 2023-03-01 12:11:50 +05:00
parent 01673668f2
commit 038de879eb
8 changed files with 302 additions and 173 deletions

View File

@ -35,6 +35,7 @@ class PostsController extends Controller
if($validator->fails()) {
return $this->helpers->apiArrayResponseBuilder(400, 'fail', $validator->errors() );
}
$locale = $request->get('locale');
$filter = [
'page' => input('page'),
@ -44,15 +45,18 @@ class PostsController extends Controller
'category' => input('category'),
'date' => input('date'),
'type' => input('type'),
'select' => ['id','title','slug', 'excerpt', 'published_at', 'en_id', 'ru_id', 'tm_id', 'powerseo_title', 'powerseo_description', 'powerseo_keywords']
];
if(request()->has('featured')){
$filter['featured'] = true;
}
$data = response()->json(
PostResource::collection($this->Post::with(['categories:id,name'])->listFrontEnd($filter)));
return $data;
$data = $this->Post::where("lang", $locale)->with(['categories:id,name'])->listFrontEnd($filter);
// $data = response()->json(
// PostResource::collection($this->Post::with(['categories:id,name'])->listFrontEnd($filter)));
// return $data;
return $this->helpers->apiArrayResponseBuilder(200, 'success', $data);
}
public function show($id, Request $request)

View File

@ -1,37 +1,37 @@
<?php
namespace RainLab\Blog\Classes;
use Illuminate\Http\Resources\Json\JsonResource;
use Config;
class PostResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
$locale = $request->get('locale');
$path = Config::get('app.url').Config::get('cms.storage.media.path');
return [
'id' => $this->id,
'title' => $this->getAttributeTranslated('title', $locale),
'slug' => $this->getAttributeTranslated('slug', $locale),
'excerpt' => $this->getAttributeTranslated('excerpt', $locale),
'published_at' => $this->published_at->format('d.m.Y'),
'type' => $this->type,
'featured_images' => ImageResource::collection($this->featured_images),
'video' => $path.$this->video,
'content_html' => $this->getAttributeTranslated('content_html', $locale),
'categories' => CategoryResource::collection($this->categories),
'powerseo_title' => $this->getAttributeTranslated('powerseo_title', $locale),
'powerseo_description' => $this->getAttributeTranslated('powerseo_description', $locale),
'powerseo_keywords' => $this->getAttributeTranslated('powerseo_keywords', $locale),
];
}
}
<?php
namespace RainLab\Blog\Classes;
use Illuminate\Http\Resources\Json\JsonResource;
use Config;
class PostResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
$locale = $request->get('locale');
$path = Config::get('app.url').Config::get('cms.storage.media.path');
return [
'id' => $this->id,
'title' => $this->getAttributeTranslated('title', $locale),
'slug' => $this->getAttributeTranslated('slug', $locale),
'excerpt' => $this->getAttributeTranslated('excerpt', $locale),
'published_at' => $this->published_at->format('d.m.Y'),
'type' => $this->type,
'featured_images' => ImageResource::collection($this->featured_images),
'video' => $path.$this->video,
'content_html' => $this->getAttributeTranslated('content_html', $locale),
'categories' => CategoryResource::collection($this->categories),
'powerseo_title' => $this->getAttributeTranslated('powerseo_title', $locale),
'powerseo_description' => $this->getAttributeTranslated('powerseo_description', $locale),
'powerseo_keywords' => $this->getAttributeTranslated('powerseo_keywords', $locale),
];
}
}

View File

@ -1,4 +1,6 @@
<?php namespace RainLab\Blog\Models;
<?php
namespace RainLab\Blog\Models;
use Url;
use Html;
@ -24,7 +26,7 @@ class Post extends Model
use \October\Rain\Database\Traits\Validation;
public $table = 'rainlab_blog_posts';
public $implement = ['@RainLab.Translate.Behaviors.TranslatableModel'];
// public $implement = ['@RainLab.Translate.Behaviors.TranslatableModel'];
/*
* Validation
@ -42,17 +44,17 @@ class Post extends Model
/**
* @var array Attributes that support translation, if available.
*/
public $translatable = [
'title',
'content',
'content_html',
'excerpt',
'metadata',
'powerseo_title',
'powerseo_description',
'powerseo_keywords',
['slug', 'index' => true]
];
// public $translatable = [
// 'title',
// 'content',
// 'content_html',
// 'excerpt',
// 'metadata',
// 'powerseo_title',
// 'powerseo_description',
// 'powerseo_keywords',
// ['slug', 'index' => true]
// ];
/**
* @var array Attributes to be stored as JSON
@ -117,9 +119,42 @@ class Post extends Model
*/
public function filterFields($fields, $context = null)
{
if(isset($fields->type)){
// dd($fields->type->value);
if($fields->type->value == 'photo'){
if (isset($fields->lang)) {
if ($this->lang == 'en') {
$fields->en_id->disabled = true;
$fields->ru_id->disabled = false;
$fields->tm_id->disabled = false;
} else if ($this->lang == 'ru') {
$fields->en_id->disabled = false;
$fields->ru_id->disabled = true;
$fields->tm_id->disabled = false;
} else if ($this->lang == 'tm') {
$fields->en_id->disabled = false;
$fields->ru_id->disabled = false;
$fields->tm_id->disabled = true;
}
}
// if (isset($lang)) {
// // dd($fields->lang->value);
// if ($fields->lang == 'en') {
// $fields->en_id->disabled = true;
// $fields->tm_id->disabled = false;
// $fields->ru_id->disabled = false;
// }else if($fields->lang == 'tm') {
// $fields->en_id->disabled = false;
// $fields->tm_id->disabled = true;
// $fields->ru_id->disabled = false;
// }else if($fields->lang == 'ru') {
// $fields->en_id->disabled = false;
// $fields->tm_id->disabled = false;
// $fields->ru_id->disabled = true;
// }
// }
if (isset($fields->type)) {
// dd($fields->type->value);
if ($fields->type->value == 'photo') {
$fields->video->hidden = true;
}
}
@ -133,8 +168,7 @@ class Post extends Model
if (!$user->hasAnyAccess(['rainlab.blog.access_publish'])) {
$fields->published->hidden = true;
$fields->published_at->hidden = true;
}
else {
} else {
$fields->published->hidden = false;
$fields->published_at->hidden = false;
}
@ -162,7 +196,7 @@ class Post extends Model
{
if ($this->published && !$this->published_at) {
throw new ValidationException([
'published_at' => Lang::get('rainlab.blog::lang.post.published_validation')
'published_at' => Lang::get('rainlab.blog::lang.post.published_validation')
]);
}
}
@ -175,7 +209,7 @@ class Post extends Model
$options = [];
foreach (BackendUser::all() as $user) {
$options[$user->id] = $user->fullname . ' ('.$user->login.')';
$options[$user->id] = $user->fullname . ' (' . $user->login . ')';
}
return $options;
@ -250,8 +284,7 @@ class Post extends Model
->whereNotNull('published')
->where('published', true)
->whereNotNull('published_at')
->where('published_at', '<', Carbon::now())
;
->where('published_at', '<', Carbon::now());
}
/**
@ -278,11 +311,16 @@ class Post extends Model
'featured' => false,
'type' => 'photo',
'date' => '',
'exceptPost' => null
'exceptPost' => null,
'select' => null,
], $options));
$searchableFields = ['title', 'slug', 'excerpt', 'content'];
if ($select) {
$query->select($select);
}
if ($published) {
$query->isPublished();
}
@ -292,7 +330,7 @@ class Post extends Model
$query->where('type', $type);
}
if (isset($featured)) {
if (isset($featured)) {
$query->where('featured', $featured);
}
@ -358,7 +396,7 @@ class Post extends Model
*/
if ($categories !== null) {
$categories = is_array($categories) ? $categories : [$categories];
$query->whereHas('categories', function($q) use ($categories) {
$query->whereHas('categories', function ($q) use ($categories) {
$q->withoutGlobalScope(NestedTreeScope::class)->whereIn('id', $categories);
});
}
@ -382,7 +420,7 @@ class Post extends Model
$category = Category::find($category);
$categories = $category->getAllChildrenAndSelf()->lists('id');
$query->whereHas('categories', function($q) use ($categories) {
$query->whereHas('categories', function ($q) use ($categories) {
$q->withoutGlobalScope(NestedTreeScope::class)->whereIn('id', $categories);
});
}
@ -398,7 +436,7 @@ class Post extends Model
*/
public function scopeFilterCategories($query, $categories)
{
return $query->whereHas('categories', function($q) use ($categories) {
return $query->whereHas('categories', function ($q) use ($categories) {
$q->withoutGlobalScope(NestedTreeScope::class)->whereIn('id', $categories);
});
}
@ -416,8 +454,7 @@ class Post extends Model
$more = Config::get('rainlab.blog::summary_separator', '<!-- more -->');
$length = Config::get('rainlab.blog::summary_default_length', 600);
return (
!!strlen(trim($this->excerpt)) ||
return (!!strlen(trim($this->excerpt)) ||
strpos($this->content_html, $more) !== false ||
strlen(Html::strip($this->content_html)) > $length
);
@ -645,16 +682,14 @@ class Post extends Model
$result['url'] = $pageUrl;
$result['isActive'] = $pageUrl == $url;
$result['mtime'] = $category->updated_at;
}
elseif ($item->type == 'all-blog-posts') {
} elseif ($item->type == 'all-blog-posts') {
$result = [
'items' => []
];
$posts = self::isPublished()
->orderBy('title')
->get()
;
->get();
foreach ($posts as $post) {
$postItem = [
@ -667,8 +702,7 @@ class Post extends Model
$result['items'][] = $postItem;
}
}
elseif ($item->type == 'category-blog-posts') {
} elseif ($item->type == 'category-blog-posts') {
if (!$item->reference || !$item->cmsPage) {
return;
}
@ -683,10 +717,10 @@ class Post extends Model
];
$query = self::isPublished()
->orderBy('title');
->orderBy('title');
$categories = $category->getAllChildrenAndSelf()->lists('id');
$query->whereHas('categories', function($q) use ($categories) {
$query->whereHas('categories', function ($q) use ($categories) {
$q->withoutGlobalScope(NestedTreeScope::class)->whereIn('id', $categories);
});

View File

@ -4,6 +4,9 @@
columns:
id:
label: ID
searchable: true
title:
label: rainlab.blog::lang.post.title
searchable: true
@ -18,7 +21,6 @@ columns:
label: rainlab.blog::lang.post.categories
relation: categories
select: name
searchable: true
sortable: false
created_at:

View File

@ -1,95 +1,122 @@
fields:
title:
label: 'rainlab.blog::lang.post.title'
span: left
placeholder: 'rainlab.blog::lang.post.title_placeholder'
type: text
slug:
label: 'rainlab.blog::lang.post.slug'
span: right
placeholder: 'rainlab.blog::lang.post.slug_placeholder'
preset:
field: title
type: slug
type: text
toolbar:
type: partial
path: post_toolbar
cssClass: collapse-visible
secondaryTabs:
fields:
content:
tab: 'rainlab.blog::lang.post.tab_edit'
type: RainLab\Blog\FormWidgets\BlogMarkdown
cssClass: 'field-slim blog-post-preview'
stretch: true
mode: split
categories:
nameFrom: name
descriptionFrom: description
span: left
type: relation
commentAbove: 'rainlab.blog::lang.post.categories_comment'
tab: 'rainlab.blog::lang.post.tab_categories'
published:
label: 'rainlab.blog::lang.post.published'
span: left
type: checkbox
tab: 'rainlab.blog::lang.post.tab_manage'
featured:
label: 'показать в слайдере на главной странице'
span: auto
type: checkbox
comment: 'не отмечать если не нужно выставлять на слайдере'
tab: 'rainlab.blog::lang.post.tab_manage'
user:
tab: 'rainlab.blog::lang.post.tab_manage'
label: 'rainlab.blog::lang.post.published_by'
span: right
type: dropdown
emptyOption: 'rainlab.blog::lang.post.current_user'
published_at:
tab: 'rainlab.blog::lang.post.tab_manage'
label: 'rainlab.blog::lang.post.published_on'
span: left
cssClass: checkbox-align
type: datepicker
mode: datetime
trigger:
action: enable
field: published
condition: checked
excerpt:
tab: 'rainlab.blog::lang.post.tab_manage'
label: 'rainlab.blog::lang.post.excerpt'
type: textarea
size: small
featured_images:
label: 'rainlab.blog::lang.post.featured_images'
mode: image
imageWidth: 200
imageHeight: 200
useCaption: true
thumbOptions:
mode: crop
extension: auto
span: left
type: fileupload
dependsOn: type
tab: 'rainlab.blog::lang.post.tab_manage'
type:
label: Type
options:
photo: Photo
video: Video
span: auto
default: photo
type: balloon-selector
tab: 'rainlab.blog::lang.post.tab_manage'
video:
label: Video
mode: file
span: left
type: mediafinder
dependsOn: type
tab: 'rainlab.blog::lang.post.tab_manage'
fields:
title:
label: 'rainlab.blog::lang.post.title'
span: left
placeholder: 'rainlab.blog::lang.post.title_placeholder'
type: text
slug:
label: 'rainlab.blog::lang.post.slug'
span: right
placeholder: 'rainlab.blog::lang.post.slug_placeholder'
preset:
field: title
type: slug
type: text
toolbar:
type: partial
path: post_toolbar
cssClass: collapse-visible
secondaryTabs:
fields:
content:
tab: 'rainlab.blog::lang.post.tab_edit'
type: RainLab\Blog\FormWidgets\BlogMarkdown
cssClass: 'field-slim blog-post-preview'
stretch: true
mode: split
categories:
nameFrom: name
descriptionFrom: description
span: left
type: relation
commentAbove: 'rainlab.blog::lang.post.categories_comment'
tab: 'Categories Locale'
lang:
label: Language
options:
en: English
ru: Russian
tm: Turkmen
span: auto
type: balloon-selector
tab: 'Categories Locale'
en_id:
label: 'English Post Id'
span: right
type: number
dependsOn: lang
tab: 'Categories Locale'
ru_id:
label: 'Russian Post Id'
span: right
type: number
dependsOn: lang
tab: 'Categories Locale'
tm_id:
label: 'Turkmen Post Id'
span: right
type: number
dependsOn: lang
tab: 'Categories Locale'
published:
label: 'rainlab.blog::lang.post.published'
span: left
type: checkbox
tab: 'rainlab.blog::lang.post.tab_manage'
featured:
label: 'показать в слайдере на главной странице'
span: auto
type: checkbox
comment: 'не отмечать если не нужно выставлять на слайдере'
tab: 'rainlab.blog::lang.post.tab_manage'
user:
tab: 'rainlab.blog::lang.post.tab_manage'
label: 'rainlab.blog::lang.post.published_by'
span: right
type: dropdown
emptyOption: 'rainlab.blog::lang.post.current_user'
published_at:
tab: 'rainlab.blog::lang.post.tab_manage'
label: 'rainlab.blog::lang.post.published_on'
span: left
cssClass: checkbox-align
type: datepicker
mode: datetime
trigger:
action: enable
field: published
condition: checked
excerpt:
tab: 'rainlab.blog::lang.post.tab_manage'
label: 'rainlab.blog::lang.post.excerpt'
type: textarea
size: small
featured_images:
label: 'rainlab.blog::lang.post.featured_images'
mode: image
imageWidth: 200
imageHeight: 200
useCaption: true
thumbOptions:
mode: crop
extension: auto
span: left
type: fileupload
dependsOn: type
tab: 'rainlab.blog::lang.post.tab_manage'
type:
label: Type
options:
photo: Photo
video: Video
span: auto
default: photo
type: balloon-selector
tab: 'rainlab.blog::lang.post.tab_manage'
video:
label: Video
mode: file
span: left
type: mediafinder
dependsOn: type
tab: 'rainlab.blog::lang.post.tab_manage'

View File

@ -0,0 +1,29 @@
<?php namespace RainLab\Blog\Updates;
use Schema;
use October\Rain\Database\Updates\Migration;
class BuilderTableUpdateRainlabBlogPosts4 extends Migration
{
public function up()
{
Schema::table('rainlab_blog_posts', function($table)
{
$table->string('lang');
$table->integer('en_id');
$table->integer('ru_id');
$table->integer('tm_id');
});
}
public function down()
{
Schema::table('rainlab_blog_posts', function($table)
{
$table->dropColumn('lang');
$table->dropColumn('en_id');
$table->dropColumn('ru_id');
$table->dropColumn('tm_id');
});
}
}

View File

@ -0,0 +1,27 @@
<?php namespace RainLab\Blog\Updates;
use Schema;
use October\Rain\Database\Updates\Migration;
class BuilderTableUpdateRainlabBlogPosts5 extends Migration
{
public function up()
{
Schema::table('rainlab_blog_posts', function($table)
{
$table->integer('en_id')->nullable()->change();
$table->integer('ru_id')->nullable()->change();
$table->integer('tm_id')->nullable()->change();
});
}
public function down()
{
Schema::table('rainlab_blog_posts', function($table)
{
$table->integer('en_id')->nullable(false)->change();
$table->integer('ru_id')->nullable(false)->change();
$table->integer('tm_id')->nullable(false)->change();
});
}
}

View File

@ -76,3 +76,9 @@
1.6.5:
- 'Updated table rainlab_blog_posts'
- builder_table_update_rainlab_blog_posts_3.php
1.6.6:
- 'Updated table rainlab_blog_posts'
- builder_table_update_rainlab_blog_posts_4.php
1.6.7:
- 'Updated table rainlab_blog_posts'
- builder_table_update_rainlab_blog_posts_5.php