gurl_o/plugins/tps/birzha/models/Category.php

136 lines
3.6 KiB
PHP
Raw Normal View History

2023-07-23 05:57:06 +00:00
<?php namespace TPS\Birzha\Models;
use Cms\Classes\Page as CmsPage;
use Cms\Classes\Theme;
use Model;
2023-11-17 19:15:34 +00:00
use DB;
2023-07-23 05:57:06 +00:00
/**
* Model
*/
class Category extends Model
{
use \October\Rain\Database\Traits\Validation;
use \October\Rain\Database\Traits\SoftDelete;
use \October\Rain\Database\Traits\Sortable;
protected $dates = ['deleted_at'];
public $implement = ['@RainLab.Translate.Behaviors.TranslatableModel'];
/**
* @var string The database table used by the model.
*/
public $table = 'tps_birzha_categories';
/**
* @var array Validation rules
*/
public $rules = [
'name' => 'required',
2023-07-24 11:45:53 +00:00
'sort_order' => 'required',
2023-07-23 05:57:06 +00:00
'slug' => ['required', 'regex:/^[a-z0-9\/\:_\-\*\[\]\+\?\|]*$/i', 'unique:tps_birzha_categories'],
'status' => 'required',
];
public $translatable = ['name',['slug', 'index' => true]];
2023-10-03 12:10:50 +00:00
2023-10-01 06:27:37 +00:00
public $hasMany = [
'filter_group' => ['TPS\Birzha\Models\FilterGroups', 'key' => 'category_id'],
2023-10-03 12:10:50 +00:00
'subs' => ['TPS\Birzha\Models\Category', 'key' => 'primary_key']
2023-10-01 06:27:37 +00:00
];
2023-10-22 13:08:31 +00:00
public $belongsTo = [
'parent' => ['TPS\Birzha\Models\Category','key' => 'primary_key'],
];
2023-10-01 06:27:37 +00:00
2023-07-23 05:57:06 +00:00
public $belongsToMany = [
2023-07-24 11:45:53 +00:00
'products' => [
'TPS\Birzha\Models\Product',
'table'=>'tps_birzha_product_categories'
],
'users' => [
2023-10-12 20:59:00 +00:00
'RainLab\User\Models\User',
2023-07-24 11:45:53 +00:00
'table'=>'tps_birzha_users_categories',
],
2023-07-23 05:57:06 +00:00
];
public function scopeActive($query)
{
return $query->where('status', 1);
}
2023-11-17 19:15:34 +00:00
public function scopeUserCategory($query)
{
return $query->where('status', 1)->orderBy('id', 'DESC')->select(DB::raw("CONCAT(id, '. ', name, ' --- ', slug, ' / ', primary_key) AS name"),'id', 'primary_key');
}
2023-07-23 05:57:06 +00:00
2023-10-03 12:10:50 +00:00
public function scopeActiveAndParent($query)
{
return $query->where('status', 1)->where('primary_key',0);
}
2023-11-17 19:15:34 +00:00
2023-10-03 12:10:50 +00:00
public function scopeActiveAndParentFirstFour($query)
{
return $query->where('status', 1)->where('primary_key', 0)->offset(0)->limit(4);
}
public function scopeActiveAndParentLastFour($query)
{
return $query->where('status', 1)->where('primary_key', 0)->offset(4)->limit(8);
}
2023-07-23 05:57:06 +00:00
public static function getMenuTypeInfo($type){
$result = [];
if ($type == 'category') {
$result = [
'references' => Category::active()->pluck('name','id'),
'nesting' => true,
'dynamicItems' => true
];
}
if ($type == 'all-categories') {
$result = [
'dynamicItems' => true
];
}
if ($result) {
$theme = Theme::getActiveTheme();
$pages = CmsPage::listInTheme($theme, true);
$cmsPages = [];
foreach ($pages as $page) {
if (!$page->hasComponent('categories')) {
continue;
}
/*
* Component must use a category filter with a routing parameter
* eg: categoryFilter = "{{ :somevalue }}"
*/
$properties = $page->getComponentProperties('blogPosts');
if (!isset($properties['categoryFilter']) || !preg_match('/{{\s*:/', $properties['categoryFilter'])) {
continue;
}
$cmsPages[] = $page;
}
$result['cmsPages'] = $cmsPages;
}
return $result;
}
public static function resolveMenuItem($item, $url, $theme){
//todo
}
}