backend category select

This commit is contained in:
Kerim 2023-01-18 12:21:51 +05:00
parent 757fdb1a5f
commit 2022c3b735
3 changed files with 199 additions and 91 deletions

View File

@ -1,4 +1,6 @@
<?php namespace RainLab\Blog\Models;
<?php
namespace RainLab\Blog\Models;
use Db;
use Illuminate\Support\Facades\Log;
@ -30,7 +32,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
@ -45,20 +47,21 @@ class Post extends Model
'excerpt' => 'required',
'awtor' => 'required',
'img_source' => ['required', 'not_in:orient,orientnews,Orient,OrientNews,Orient News,Orient news,orient news,ORIENT NEWS,ORIENT,NEWS,orient news,news'],
'author_name' => 'required'
'author_name' => 'required',
'category_groups' => ['array', 'max:1']
];
/**
* @var array Attributes that support translation, if available.
*/
// public $translatable = [
// 'title',
// 'content',
// 'content_html',
// 'excerpt',
// 'metadata',
// ['slug', 'index' => true]
// ];
// public $translatable = [
// 'title',
// 'content',
// 'content_html',
// 'excerpt',
// 'metadata',
// ['slug', 'index' => true]
// ];
/**
* @var array Attributes to be stored as JSON
@ -92,10 +95,15 @@ class Post extends Model
*/
public $belongsTo = [
'user' => ['Backend\Models\User'],
'awtor' => ['Tps\Tps\Models\Authors',
'key' => 'author_id',
'table' => 'tps_tps_authors'
],
'awtor' => [
'Tps\Tps\Models\Authors',
'key' => 'author_id',
'table' => 'tps_tps_authors'
],
// 'category_groups_all' => [
// 'RainLab\Blog\Models\CategoryGroup',
// 'table' => 'rainlab_blog_category_group',
// ],
];
public $belongsToMany = [
@ -118,18 +126,20 @@ class Post extends Model
'table' => 'rainlab_blog_post_cat_group',
'pivot' => ['id', 'category_group_id']
],
// 'tags_ru' =>[
// 'Bedard\BlogTags\Models\Tag',
// 'table' => 'bedard_blogtags_post_tag',
// 'order' => 'name',
// 'conditions' => 'bedard_blogtags_tags.locale = "ru"'
// ],
// 'tags_en' =>[
// 'Bedard\BlogTags\Models\Tag',
// 'table' => 'bedard_blogtags_post_tag',
// 'order' => 'name',
// 'conditions' => 'bedard_blogtags_tags.locale = "en"'
// ]
// 'tags_ru' =>[
// 'Bedard\BlogTags\Models\Tag',
// 'table' => 'bedard_blogtags_post_tag',
// 'order' => 'name',
// 'conditions' => 'bedard_blogtags_tags.locale = "ru"'
// ],
// 'tags_en' =>[
// 'Bedard\BlogTags\Models\Tag',
// 'table' => 'bedard_blogtags_post_tag',
// 'order' => 'name',
// 'conditions' => 'bedard_blogtags_tags.locale = "en"'
// ]
];
// public static function myFilterMethod($query, $related, $parent)
@ -139,7 +149,7 @@ class Post extends Model
// }
public $attachMany = [
// 'featured_images' => ['System\Models\File', 'order' => 'sort_order'],
// 'featured_images' => ['System\Models\File', 'order' => 'sort_order'],
'content_images' => ['System\Models\File']
];
@ -172,22 +182,104 @@ class Post extends Model
->lists('name', 'id');
// \Log::info($query);
return $query;
}
public function getCategoryGroupsOptions()
{
$query = CategoryGroup::get()
->lists('name', 'id');
// \Log::info($query);
return $query;
}
// public function getActiveCategoriesOptions()
// {
// // $groupIds = $this->category_groups_all;
// if (isset($this->category_groups_all)) {
// dd($this->category_groups_all);
// return $query;
// }
// // $query = Category::
// // // where('status', 1)
// // select('rainlab_blog_categories.id', 'rainlab_blog_categories.nest_left', 'rainlab_blog_categories.name')
// // ->distinct()
// // ->join('rainlab_blog_category_pivot', 'rainlab_blog_categories.id', '=', 'rainlab_blog_category_pivot.category_id')
// // ->whereIn('rainlab_blog_category_pivot.category_group_id', $groupIds)
// // ->get()
// // ->lists('name', 'id');
// // \Log::info($query);
// }
// public function getCategoryGroupOptions()
// {
// $groupIds = $this->category_groups_all;
// $query = Category::
// select('rainlab_blog_categories.id', 'rainlab_blog_categories.nest_left', 'rainlab_blog_categories.name')
// ->get()
// ->lists('name', 'id');
// // \Log::info($query);
// return $query;
// }
public function filterFields($fields, $context = null)
{
if(isset($this->awtor)){
if($this->awtor->type == 'other'){
// $fields->afisha_phone->value = $fields->category_groupsq->value;
// $fields->category_groups->value = $fields->category_groupsq->value;
// \Log::info($fields->category_groups->value);
// \Log::info($fields->category_groupsq->value);
if (isset($this->awtor)) {
if ($this->awtor->type == 'other') {
$fields->author_name->value = '';
$fields->author_name->readOnly = false;
}elseif(isset($fields->author_name)){
} elseif (isset($fields->author_name)) {
$fields->author_name->value = $this->awtor->name;
$fields->author_name->readOnly = true;
}
}
// dd($fields->category_groups);
// if (isset($this->category_groups_all)) {
// // if($this->category_groups_all->type == 'other'){
// // $fields->author_name->value = '';
// // $fields->author_name->readOnly = false;
// // }elseif(isset($fields->author_name)){
// // $fields->author_name->value = $this->awtor->name;
// // $fields->author_name->readOnly = true;
// // }
// if ($this->category_groups_all->type == 'news') {
// $fields->type_post->hidden = false;
// } else {
// $fields->type_post->hidden = true;
// }
// if ($this->category_groups_all->type == 'afisha') {
// $fields->afisha_phone->hidden = false;
// $fields->afisha_address->hidden = false;
// } else {
// $fields->afisha_phone->hidden = true;
// $fields->afisha_address->hidden = true;
// }
// }
if(isset($fields->category_groups)){
if($this->category_groups->count() == 0 || $this->category_groups->first()->id != $fields->category_groups->value[0]){
$fields->categories->value = null;
}
\Log::info($fields->category_groups->value);
\Log::info($this->category_groups);
if($this->category_groups->where('type', 'news')->count()){
$fields->type_post->hidden = false;
}else{
@ -203,9 +295,8 @@ class Post extends Model
}
}
if(isset($fields->locale)){
if($this->locale == 'en')
{
if (isset($fields->locale)) {
if ($this->locale == 'en') {
$fields->tags_en->hidden = false;
$fields->tags_ru->hidden = true;
$fields->tags_tm->hidden = true;
@ -213,7 +304,7 @@ class Post extends Model
$fields->id_en->disabled = true;
$fields->id_ru->disabled = false;
$fields->id_tm->disabled = false;
}else if ($this->locale == 'ru'){
} else if ($this->locale == 'ru') {
$fields->tags_en->hidden = true;
$fields->tags_ru->hidden = false;
$fields->tags_tm->hidden = true;
@ -221,7 +312,7 @@ class Post extends Model
$fields->id_en->disabled = false;
$fields->id_ru->disabled = true;
$fields->id_tm->disabled = false;
}else if ($this->locale == 'tm'){
} else if ($this->locale == 'tm') {
$fields->tags_en->hidden = true;
$fields->tags_ru->hidden = true;
$fields->tags_tm->hidden = false;
@ -243,8 +334,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;
}
@ -254,7 +344,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')
]);
}
}
@ -269,14 +359,14 @@ class Post extends Model
}
$this->content_html = self::formatHtml($this->content);
// Log::info(input());
Log::info(input());
}
protected function afterSave()
{
$this->tags_ru()->attach($this->tags_ru);
$this->tags_en()->attach($this->tags_en);
$this->tags_tm()->attach($this->tags_tm);
$this->tags_tm()->attach($this->tags_tm);
}
/**
@ -349,15 +439,15 @@ class Post extends Model
->where('published', true)
->whereNotNull('published_at')
->where('published_at', '<', Carbon::now())
->where('locale',App::getLocale())
;
->where('locale', App::getLocale());
}
public function scopePostsWithCats($query){
public function scopePostsWithCats($query)
{
return $query
->isPublished()
->with('categories')
->orderBy('published_at','desc')
->orderBy('published_at', 'desc')
->take(7);
}
/**
@ -396,10 +486,10 @@ class Post extends Model
$query->isPublished();
}
if ($typePost !==null) {
if ($typePost !== null) {
$typePost = is_array($typePost) ? $typePost : [$typePost];
$query->whereIn('type_post',$typePost);
$query->whereIn('type_post', $typePost);
}
@ -409,18 +499,18 @@ class Post extends Model
// }
if(isset($featured)){
$query-> where('featured',$featured);
if (isset($featured)) {
$query->where('featured', $featured);
}
if(isset($morque)){
$query-> where('on_morque',$morque);
if (isset($morque)) {
$query->where('on_morque', $morque);
}
$date = trim($date);
if(strtotime($date)){
$query->whereDate('published_at','=',$date);
if (strtotime($date)) {
$query->whereDate('published_at', '=', $date);
}
/*
@ -479,7 +569,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->whereIn('id', $categories);
});
}
@ -504,8 +594,8 @@ class Post extends Model
*/
if ($postGroup !== null) {
// dd($postGroup);
$query->whereHas('category_groups', function($q) use ($postGroup) {
// dd($postGroup);
$query->whereHas('category_groups', function ($q) use ($postGroup) {
$q->where('slug', $postGroup);
});
}
@ -514,17 +604,16 @@ class Post extends Model
$category = Category::find($category);
$categories = $category->getAllChildrenAndSelf()->lists('id');
if(!$categories){
if (!$categories) {
$categories[] = $category->id;
}
// dd($categories);
$query->whereHas('categories', function($q) use ($categories) {
// dd($categories);
$query->whereHas('categories', function ($q) use ($categories) {
$q->whereIn('id', $categories);
});
}
if($select){
if ($select) {
$query->select($select);
}
@ -540,21 +629,21 @@ 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->whereIn('id', $categories);
});
}
public function scopeFilterAuthors($query, $authors)
{
return $query->whereHas('awtor', function($q) use ($authors) {
return $query->whereHas('awtor', function ($q) use ($authors) {
$q->where('id', $authors);
});
}
public function scopeFilterId($query, $id)
{
if(!isset($id)){
if (!isset($id)) {
return;
}
return $query->where('id', $id);
@ -562,7 +651,7 @@ class Post extends Model
public function scopeFilterName($query, $name)
{
return $query->where('title','LIKE','%'.$name.'%')->orWhere('slug','LIKE','%'.$name.'%');
return $query->where('title', 'LIKE', '%' . $name . '%')->orWhere('slug', 'LIKE', '%' . $name . '%');
}
// public function scopeFilterName($query, $name)
@ -570,7 +659,8 @@ class Post extends Model
// return $query = where('title','LIKE','%'.$name.'%');
// }
public function scopeFilterLocale($query, array $types) {
public function scopeFilterLocale($query, array $types)
{
foreach ($types as $type) {
switch ($type) {
case 'en':
@ -579,13 +669,11 @@ class Post extends Model
case 'ru':
return $query->where('locale', 'ru');
break;
case 'tm':
case 'tm':
return $query->where('locale', 'tm');
break;
}
}
}
//
@ -600,8 +688,7 @@ class Post extends Model
{
$more = '<!-- more -->';
return (
!!strlen(trim($this->excerpt)) ||
return (!!strlen(trim($this->excerpt)) ||
strpos($this->content_html, $more) !== false ||
strlen(Html::strip($this->content_html)) > 600
);
@ -632,27 +719,29 @@ class Post extends Model
return Html::limit($content, 280);
}
public function getFullTextAttribute(){
public function getFullTextAttribute()
{
return preg_replace("/<img[^>]+\>/i", " ", $this->content_html);
}
//public function getTranslatedPostAttribute(){
// if($this->locale === 'en')
// return $this->id_ru;
//return $this->id_en;
// if($this->locale === 'en')
// return $this->id_ru;
//return $this->id_en;
//}
//public function setTranslatedPostAttribute($value){
// if($this->attributes['locale'] === 'en')
// $this->attributes['id_ru'] = $value;
//else
// $this->attributes['id_en'] = $value;
// if($this->attributes['locale'] === 'en')
// $this->attributes['id_ru'] = $value;
//else
// $this->attributes['id_en'] = $value;
//}
public function getRssDateAttribute(){
public function getRssDateAttribute()
{
setlocale(LC_TIME, 'en');
Carbon::setLocale('en');
return Carbon::parse($this->published_at)->format('D, d M Y H:i:s O');//Sun, 29 Sep 2002 19:59:01 +0300
return Carbon::parse($this->published_at)->format('D, d M Y H:i:s O'); //Sun, 29 Sep 2002 19:59:01 +0300
}
//
// Next / Previous
@ -849,15 +938,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();
->orderBy('title')
->get();
foreach ($posts as $post) {
$postItem = [
@ -870,8 +958,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;
}
@ -886,10 +973,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->whereIn('id', $categories);
});

View File

@ -145,11 +145,18 @@ secondaryTabs:
required: 0
type: balloon-selector
tab: 'Category Group'
# category_groups:
# span: left
# path: field_category_group_item
# type: partial
# dependsOn: category_groupsq
# tab: 'Category Group'
category_groups:
span: left
path: field_category_group_item
type: partial
type: relation
tab: 'Category Group'
# options: getCategoryGroupsOptions
categories:
tab: 'Category Group'
type: checkboxlist

View File

@ -26,3 +26,17 @@ fields:
size: large
span: auto
type: richeditor
radio1:
label: 'Radio list'
options:
1: sdfsdf
2: sdfsdf
span: auto
type: radio
checkboxlist1:
label: 'Checkbox list'
span: auto
options:
1: grwererg
2: ergerg
type: checkboxlist