Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 75 additions & 48 deletions components/Categories.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Carbon\Carbon;
use Cms\Classes\Page;
use Cms\Classes\ComponentBase;
use System\Classes\PluginManager;
use JanVince\SmallRecords\Models\Category;
use JanVince\SmallRecords\Models\Record;
use JanVince\SmallRecords\Models\Settings;
Expand Down Expand Up @@ -186,96 +187,122 @@ public function onRender()
*/
public function items() {

$pluginManager = PluginManager::instance()->findByIdentifier('Rainlab.Translate');

$categories = Category::query();

/**
* Get only categories with one or more "main category" records
*/
$categories->with(['records' => function($query) {

$categories->with(['records' => function($query) use($pluginManager) {

if( $this->property('areaSlug') ) {

$query->whereHas('area', function ($query2) {

$query2->where('slug', '=', $this->property('areaSlug'));

$query->whereHas('area', function ($query2) use($pluginManager) {

if ($pluginManager && !$pluginManager->disabled) {
$query2->transWhere('slug', '=', $this->property('areaSlug'));
} else {
$query2->where('slug', '=', $this->property('areaSlug'));
}

});
}
}

if( $this->property('activeRecordsOnly') ) {
$query->where('active', '=', true);
}

$query->where('active', '=', true);
}
}]);

if( $this->property('useMainCategory') ) {

$categories->whereHas('records', function ($query) {
$categories->whereHas('records', function ($query) use($pluginManager) {

if( $this->property('areaSlug') ) {

$query->whereHas('area', function ($query2) {

$query2->where('slug', '=', $this->property('areaSlug'));
$query->whereHas('area', function ($query2) use($pluginManager) {

if ($pluginManager && !$pluginManager->disabled) {
$query2->transWhere('slug', '=', $this->property('areaSlug'));
} else {
$query2->where('slug', '=', $this->property('areaSlug'));
}

});
}
}

if( $this->property('activeRecordsOnly') ) {

$query->where('active', '=', true);
}
$query->where('active', '=', true);
}
});
}

/**
* Get only categories with one or more "secondary categories" records
*/
$categories->with(['records_multicategories' => function($query) {

$categories->with(['records_multicategories' => function($query) use($pluginManager) {

if( $this->property('areaSlug') ) {

$query->whereHas('area', function ($query2) {

$query2->where('slug', '=', $this->property('areaSlug'));

$query->whereHas('area', function ($query2) use($pluginManager) {

if ($pluginManager && !$pluginManager->disabled) {
$query2->transWhere('slug', '=', $this->property('areaSlug'));
} else {
$query2->where('slug', '=', $this->property('areaSlug'));
}

});
}
}

if( $this->property('activeRecordsOnly') ) {
$query->where('active', '=', true);
}

$query->where('active', '=', true);
}
}]);

if( $this->property('useMultiCategories') ) {
$categories->whereHas('records_multicategories', function ($query) {

$categories->whereHas('records_multicategories', function ($query) use($pluginManager) {

if( $this->property('areaSlug') ) {

$query->whereHas('area', function ($query2) {

$query2->where('slug', '=', $this->property('areaSlug'));
$query->whereHas('area', function ($query2) use($pluginManager) {

if ($pluginManager && !$pluginManager->disabled) {
$query2->transWhere('slug', '=', $this->property('areaSlug'));
} else {
$query2->where('slug', '=', $this->property('areaSlug'));
}

});
}
}

if( $this->property('activeRecordsOnly') ) {

$query->where('active', '=', true);
}
$query->where('active', '=', true);
}
});
}

/**
* Filter only children of parent category
*/
if( $this->property('parentCategorySlug') ) {

$parentCategory = Category::where('slug', $this->property('parentCategorySlug'))->first();

if($parentCategory) {
$categories->where('parent_id', $parentCategory->id);
}
if ($pluginManager && !$pluginManager->disabled) {
$parentCategory = Category::transWhere('slug', $this->property('parentCategorySlug'))->first();
} else {
$parentCategory = Category::where('slug', $this->property('parentCategorySlug'))->first();
}

if ($parentCategory) {
$categories->where('parent_id', $parentCategory->id);
}
}

/**
Expand Down