Optimize homepage cats queries

This commit is contained in:
Fatih Alp 2021-01-10 22:33:12 +03:00
parent bfa1c12aa4
commit a03f373374
29 changed files with 126 additions and 431 deletions

View File

@ -43,18 +43,15 @@ class AdvsModulePlugin extends Plugin
), new \Twig_SimpleFunction(
'latestAds',
function () {
if (!$latestAds = $this->dispatch(new LatestAds())) {
return 0;
}
return $latestAds;
}
),
new \Twig_SimpleFunction(
'appendRequestURL',
function ($request, $url, $new_parameters, $removeParams = []) {
return $this->dispatch(new appendRequestURL($request, $url, $new_parameters, $removeParams));
}
),

View File

@ -1065,13 +1065,6 @@ class AdvsController extends PublicController
return $this->view->make('theme::passwords/reset', compact('code'));
}
public function homePage(CategoryRepositoryInterface $repository)
{
$cats = $repository->mainCats();
return $this->view->make('theme::addons/anomaly/pages-module/page', compact('cats'));
}
public function mapJson(Request $request, AdvRepositoryInterface $repository)
{
$param = $request->toArray();

View File

@ -1,15 +1,13 @@
{% set sub_categories_limit = setting_value('visiosoft.theme.base::home_page_sub_categories_limit') %}
<ul class="categories-list px-0 mr-2 d-block d-sm-none">
<li class="categories-list-li category-icon text-truncate category-{{ main_category.slug }}">
<a href="{{ url_route('adv_list_seo', [main_category.slug]) }}" class="main-category">
{% set catIcon = main_category.icon.path != "" ? url('files/' ~ main_category.icon.path) : img('visiosoft.theme.base::images/default-categories-icon.png').url %}
<img src="{{ catIcon }}" alt="{{ main_category.name }}" class="img-responsive">
<img src="{{ catIcon(main_category.icon.path) }}" alt="{{ main_category.name }}" class="img-responsive">
<span>{{ main_category.name }}</span>
<p class="p-0 sub-categories">
{% set sub_categories = entries('cats','category').where('parent_category_id', main_category.id).where('deleted_at', null).orderBy('sort_order').get() %}
{% for sub_category in sub_categories|slice(0,sub_categories_limit) %}
{{ sub_category.name }},
{% for subcats in cats.subcats %}
{% if main_category.id == subcats.id %}
{{ subcats.c2_name }},
{% endif %}
{% endfor %}
</p>
</a>

View File

@ -1,46 +1,38 @@
{% set sub_categories_limit = setting_value('visiosoft.theme.base::home_page_sub_categories_limit') %}
{% set showAdsCount = setting_value('visiosoft.module.advs::show_ads_count') %}
<ul class="categories-list p-0 d-none d-sm-block">
<li class="categories-list-li category-icon category-{{ main_category.slug }}">
{% set catIcon = main_category.icon.path != "" ? url('files/' ~ main_category.icon.path) : img('visiosoft.theme.base::images/default-categories-icon.png').url %}
<img src="{{ catIcon }}" alt="{{ main_category.name }}" class="img-responsive">
<img src="{{ catIcon(main_category.icon.path) }}" alt="{{ main_category.name }}" class="img-responsive">
<a href="{{ url_route('adv_list_seo', [main_category.slug]) }}" class="main-category">
<b>{{ main_category.name }}</b>
{% if showAdsCount %}
<small class="text-muted">
({{ catAdsCount }})
({{ main_category.adcount }})
</small>
{% endif %}
</a>
<ul class="p-0">
{% set sub_categories = entries('cats','category').where('parent_category_id', main_category.id).where('deleted_at', null).orderBy('sort_order').get() %}
{% set subCatLoop = [
{
'start': 0,
'end': sub_categories_limit,
'class': '',
},
{
'start': sub_categories_limit,
'end': count(sub_categories),
'class': 'hidden hidden-category',
}
] %}
{% for subLoop in subCatLoop %}
{% for sub_category in sub_categories|slice(subLoop.start, subLoop.end) %}
<li class="{{ subLoop.class }}">
<a href="{{ url_route('adv_list_seo', [sub_category.slug]) }}">{{ sub_category.name }}</a>
{% set subCatCount = 0 %}
{% for subcats in cats.subcats %}
{% if main_category.id == subcats.id %}
{% set subCatCount = subCatCount + 1 %}
{% set hideclass = "" %}
{% if subCatCount > sub_categories_limit %}
{% set hideclass = "hidden hidden-category" %}
{% endif %}
<li class="{{ hideclass }}">
<a href="{{ url_route('adv_list_seo', [subcats.c2_slug]) }}"
class="">{{ subcats.c2_name }}</a>
{% if showAdsCount %}
{% set catLevel = getParentsCount(sub_category.id) + 1 %}
<small class="text-muted">
({{ catAdsCount }})
({{ subcats.adcount }})
</small>
{% endif %}
</li>
{% endfor %}
{% endif %}
{% endfor %}
{% if count(sub_categories) > sub_categories_limit %}
{% if subCatCount > sub_categories_limit %}
<li class="show-all" data-limit="{{ sub_categories_limit }}">
<a>
<span class="">
@ -51,7 +43,6 @@
{{ trans('visiosoft.theme.base::button.hide_all') }}
<i class="fas fa-arrow-circle-up"></i>
</span>
</a>
</li>
{% endif %}

View File

@ -1,5 +1,7 @@
<div class="col left-categories-section mx-0">
{% set sub_categories_limit = setting_value('visiosoft.theme.base::home_page_sub_categories_limit') %}
{% set showAdsCount = setting_value('visiosoft.module.advs::show_ads_count') %}
<div class="col left-categories-section mx-0">
<ul class="categories-list p-0 d-none d-sm-block mb-0">
<li class="categories-list-li category-icon border-bottom-0 mt-0">
<img src="{{ img('visiosoft.theme.base::images/store-search.png').url }}" class="img-responsive">
@ -35,12 +37,10 @@
</li>
{{ addBlock('base/leftSidebar')|raw }}
</ul>
{{ addBlock('base/categoriesRow')|raw }}
<div class="col-12 p-0 m-0">
{% set main_categories = entries('cats','category').where('parent_category_id', null).where('deleted_at', null).orderBy('sort_order').get() %}
{% for main_category in main_categories %}
{% set cats = getLevel2Cats() %}
{% for main_category in cats.maincats %}
{% include "visiosoft.theme.base::partials/categories-web" %}
{% include "visiosoft.theme.base::partials/categories-mobile" %}
{% endfor %}

View File

@ -1,35 +0,0 @@
<?php
use Anomaly\Streams\Platform\Database\Migration\Migration;
class VisiosoftModuleCatsCreatePlaceholderforsearchStream extends Migration
{
/**
* The stream definition.
*
* @var array
*/
protected $stream = [
'slug' => 'placeholderforsearch',
'title_column' => 'name',
'translatable' => true,
'versionable' => false,
'trashable' => false,
'searchable' => false,
'sortable' => false,
];
/**
* The stream assignments.
*
* @var array
*/
protected $assignments = [
'name' => [
'translatable' => true,
'required' => true,
],
];
}

View File

@ -11,9 +11,5 @@ class VisiosoftModuleCatsAlterIndexToAllTable extends Migration
Schema::table('cats_category_translations', function (Blueprint $table) {
$table->index('entry_id');
});
Schema::table('cats_placeholderforsearch_translations', function (Blueprint $table) {
$table->index('entry_id');
});
}
}

View File

@ -0,0 +1,21 @@
<?php
use Anomaly\Streams\Platform\Database\Migration\Migration;
class VisiosoftModuleCatsAdcount extends Migration
{
protected $stream = [
'slug' => 'category',
];
protected $fields = [
'adcount' => 'anomaly.field_type.integer',
'adcount_updateat' => 'anomaly.field_type.datetime'
];
protected $assignments = [
'adcount' => []
];
}

View File

@ -4,11 +4,6 @@ return [
'category' => [
'read',
'write',
'delete',
],
'placeholderforsearch' => [
'read',
'write',
'delete',
],
'delete'
]
];

View File

@ -4,6 +4,8 @@ use Anomaly\Streams\Platform\Model\Cats\CatsCategoryEntryModel;
use Visiosoft\AdvsModule\Adv\Contract\AdvRepositoryInterface;
use Visiosoft\CatsModule\Category\Contract\CategoryRepositoryInterface;
use Anomaly\Streams\Platform\Entry\EntryRepository;
use Illuminate\Support\Facades\DB;
use Eloquent\Support\Collection;
class CategoryRepository extends EntryRepository implements CategoryRepositoryInterface
{
@ -37,6 +39,39 @@ class CategoryRepository extends EntryRepository implements CategoryRepositoryIn
{
return $this->model->where('parent_category_id', null)->orderBy('sort_order')->get();
}
public function getLevel2Cats()
{
$dBName = 'default_cats_category';
$dBNamet = $dBName.'_translations';
$catsDB = DB::table((DB::raw($dBName.' c1')))
->select(
DB::raw('c1.id'),
DB::raw('c1.slug'),
DB::raw('c1.parent_category_id'),
DB::raw('t1.name'),
DB::raw('c2.id as c2_id'),
DB::raw('c2.slug as c2_slug'),
DB::raw('c2.parent_category_id as c2_parent_category_id'),
DB::raw('t2.name as c2_name')
)
->leftJoin((DB::raw($dBName.' c2')), DB::raw('c2.parent_category_id'), '=', DB::raw('c1.id'))
->leftJoin((DB::raw($dBNamet.' t1')), DB::raw('c1.id'), '=', DB::raw('t1.entry_id'))
->leftJoin((DB::raw($dBNamet.' t2')), DB::raw('c2.id'), '=', DB::raw('t2.entry_id'))
->where(DB::raw('t1.locale'), Request()->session()->get('_locale', setting_value('streams::default_locale')))
->where(DB::raw('t2.locale'), Request()->session()->get('_locale', setting_value('streams::default_locale')))
->where(DB::raw("c1.deleted_at"),NULL)
->where(DB::raw("c2.deleted_at"),NULL)
->whereNull(DB::raw("c1.parent_category_id"))
->orderBy(DB::raw("c1.sort_order"))
->orderBy(DB::raw("c2.sort_order"))
->get();
$cats = collect([]);
$cats->subcats = $catsDB;
$cats->maincats = $catsDB->unique('id');
return $cats;
}
public function getItem($cat)
{

View File

@ -0,0 +1,12 @@
<?php
namespace Visiosoft\CatsModule\Category\Command;
use Visiosoft\CatsModule\Category\Contract\CategoryRepositoryInterface;
class getLevel2Cats
{
public function handle(CategoryRepositoryInterface $repo)
{
return $repo->getLevel2Cats();
}
}

View File

@ -8,6 +8,8 @@ interface CategoryRepositoryInterface extends EntryRepositoryInterface
public function mainCats();
public function getLevel2Cats();
public function getItem($cat);
public function getCatById($id);

View File

@ -29,12 +29,7 @@ class CatsModule extends Module
'buttons' => [
'new_category',
],
],
'placeholderforsearch' => [
'buttons' => [
'new_placeholderforsearch',
],
],
]
];
}

View File

@ -1,7 +1,11 @@
<?php namespace Visiosoft\CatsModule;
use Anomaly\Streams\Platform\Addon\Plugin\Plugin;
use Anomaly\Streams\Platform\Image\Command\MakeImageInstance;
use Visiosoft\CatsModule\Category\Command\getLevel2Cats;
use Visiosoft\CatsModule\Category\CategoryModel;
use Visiosoft\CatsModule\Category\CategoryRepository;
use Visiosoft\CatsModule\Category\Command\GetCategoryName;
use Visiosoft\CatsModule\Category\Command\GetCategoryDetail;
@ -38,7 +42,7 @@ class CatsModulePlugin extends Plugin
'category_parents_name',
function ($id) {
$category_model = new CategoryModel();
return $category_model->getParentCats($id,'add_main');
return $category_model->getParentCats($id, 'add_main');
}
), new \Twig_SimpleFunction(
'getParentsCount',
@ -46,6 +50,23 @@ class CatsModulePlugin extends Plugin
$category_model = new CategoryModel();
return $category_model->getParentsCount($id);
}
), new \Twig_SimpleFunction(
'catIcon',
function ($path) {
if ($path == "") {
return $this->dispatch(new MakeImageInstance('visiosoft.theme.base::images/default-categories-icon.png', 'img'))->url();
} else {
return url('files/'.$path);
}
}
), new \Twig_SimpleFunction(
'getLevel2Cats',
function () {
if (!$getLevel2Cats = $this->dispatch(new getLevel2Cats())) {
return 0;
}
return $getLevel2Cats;
}
)
];
}

View File

@ -1,7 +1,6 @@
<?php namespace Visiosoft\CatsModule;
use Anomaly\Streams\Platform\Database\Seeder\Seeder;
use Visiosoft\CatsModule\Placeholderforsearch\PlaceholderforsearchSeeder;
class CatsModuleSeeder extends Seeder
{
@ -10,6 +9,6 @@ class CatsModuleSeeder extends Seeder
*/
public function run()
{
$this->call(PlaceholderforsearchSeeder::class);
}
}

View File

@ -1,10 +1,6 @@
<?php namespace Visiosoft\CatsModule;
use Anomaly\Streams\Platform\Addon\AddonServiceProvider;
use Visiosoft\CatsModule\Placeholderforsearch\Contract\PlaceholderforsearchRepositoryInterface;
use Visiosoft\CatsModule\Placeholderforsearch\PlaceholderforsearchRepository;
use Anomaly\Streams\Platform\Model\Cats\CatsPlaceholderforsearchEntryModel;
use Visiosoft\CatsModule\Placeholderforsearch\PlaceholderforsearchModel;
use Visiosoft\CatsModule\Category\Contract\CategoryRepositoryInterface;
use Visiosoft\CatsModule\Category\CategoryRepository;
use Anomaly\Streams\Platform\Model\Cats\CatsCategoryEntryModel;
@ -51,9 +47,7 @@ class CatsModuleServiceProvider extends AddonServiceProvider
*/
protected $routes = [
'admin/cats/clean_subcats' => 'Visiosoft\CatsModule\Http\Controller\Admin\CategoryController@cleanSubcats',
'admin/cats/placeholderforsearch' => 'Visiosoft\CatsModule\Http\Controller\Admin\PlaceholderforsearchController@index',
'admin/cats/placeholderforsearch/create' => 'Visiosoft\CatsModule\Http\Controller\Admin\PlaceholderforsearchController@create',
'admin/cats/placeholderforsearch/edit/{id}' => 'Visiosoft\CatsModule\Http\Controller\Admin\PlaceholderforsearchController@edit',
'admin/cats' => 'Visiosoft\CatsModule\Http\Controller\Admin\CategoryController@index',
'admin/cats/create' => 'Visiosoft\CatsModule\Http\Controller\Admin\CategoryController@create',
'admin/cats/edit/{id}' => 'Visiosoft\CatsModule\Http\Controller\Admin\CategoryController@edit',
@ -117,7 +111,6 @@ class CatsModuleServiceProvider extends AddonServiceProvider
* @type array|null
*/
protected $bindings = [
CatsPlaceholderforsearchEntryModel::class => PlaceholderforsearchModel::class,
CatsCategoryEntryModel::class => CategoryModel::class,
];
@ -127,7 +120,6 @@ class CatsModuleServiceProvider extends AddonServiceProvider
* @type array|null
*/
protected $singletons = [
PlaceholderforsearchRepositoryInterface::class => PlaceholderforsearchRepository::class,
CategoryRepositoryInterface::class => CategoryRepository::class,
];
@ -202,18 +194,10 @@ class CatsModuleServiceProvider extends AddonServiceProvider
'href' => '/admin/cats/create?parent='.$request->cat
],
],
],
'placeholderforsearch' => [
'buttons' => [
'new_placeholderforsearch',
],
],
]
];
$this->addon->setSections($sections);
}
return parent::getOverrides();
}
}

View File

@ -1,43 +0,0 @@
<?php namespace Visiosoft\CatsModule\Http\Controller\Admin;
use Visiosoft\CatsModule\Placeholderforsearch\Form\PlaceholderforsearchFormBuilder;
use Visiosoft\CatsModule\Placeholderforsearch\Table\PlaceholderforsearchTableBuilder;
use Anomaly\Streams\Platform\Http\Controller\AdminController;
class PlaceholderforsearchController extends AdminController
{
/**
* Display an index of existing entries.
*
* @param PlaceholderforsearchTableBuilder $table
* @return \Symfony\Component\HttpFoundation\Response
*/
public function index(PlaceholderforsearchTableBuilder $table)
{
return $table->render();
}
/**
* Create a new entry.
*
* @param PlaceholderforsearchFormBuilder $form
* @return \Symfony\Component\HttpFoundation\Response
*/
public function create(PlaceholderforsearchFormBuilder $form)
{
return $form->render();
}
/**
* Edit an existing entry.
*
* @param PlaceholderforsearchFormBuilder $form
* @param $id
* @return \Symfony\Component\HttpFoundation\Response
*/
public function edit(PlaceholderforsearchFormBuilder $form, $id)
{
return $form->render($id);
}
}

View File

@ -1,8 +0,0 @@
<?php namespace Visiosoft\CatsModule\Placeholderforsearch\Contract;
use Anomaly\Streams\Platform\Entry\Contract\EntryInterface;
interface PlaceholderforsearchInterface extends EntryInterface
{
}

View File

@ -1,8 +0,0 @@
<?php namespace Visiosoft\CatsModule\Placeholderforsearch\Contract;
use Anomaly\Streams\Platform\Entry\Contract\EntryRepositoryInterface;
interface PlaceholderforsearchRepositoryInterface extends EntryRepositoryInterface
{
}

View File

@ -1,66 +0,0 @@
<?php namespace Visiosoft\CatsModule\Placeholderforsearch\Form;
use Anomaly\Streams\Platform\Ui\Form\FormBuilder;
class PlaceholderforsearchFormBuilder extends FormBuilder
{
/**
* The form fields.
*
* @var array|string
*/
protected $fields = [];
/**
* Additional validation rules.
*
* @var array|string
*/
protected $rules = [];
/**
* Fields to skip.
*
* @var array|string
*/
protected $skips = [];
/**
* The form actions.
*
* @var array|string
*/
protected $actions = [];
/**
* The form buttons.
*
* @var array|string
*/
protected $buttons = [
'cancel',
];
/**
* The form options.
*
* @var array
*/
protected $options = [];
/**
* The form sections.
*
* @var array
*/
protected $sections = [];
/**
* The form assets.
*
* @var array
*/
protected $assets = [];
}

View File

@ -1,8 +0,0 @@
<?php namespace Visiosoft\CatsModule\Placeholderforsearch;
use Anomaly\Streams\Platform\Entry\EntryCollection;
class PlaceholderforsearchCollection extends EntryCollection
{
}

View File

@ -1,8 +0,0 @@
<?php namespace Visiosoft\CatsModule\Placeholderforsearch;
use Anomaly\Streams\Platform\Entry\EntryCriteria;
class PlaceholderforsearchCriteria extends EntryCriteria
{
}

View File

@ -1,9 +0,0 @@
<?php namespace Visiosoft\CatsModule\Placeholderforsearch;
use Visiosoft\CatsModule\Placeholderforsearch\Contract\PlaceholderforsearchInterface;
use Anomaly\Streams\Platform\Model\Cats\CatsPlaceholderforsearchEntryModel;
class PlaceholderforsearchModel extends CatsPlaceholderforsearchEntryModel implements PlaceholderforsearchInterface
{
}

View File

@ -1,8 +0,0 @@
<?php namespace Visiosoft\CatsModule\Placeholderforsearch;
use Anomaly\Streams\Platform\Entry\EntryObserver;
class PlaceholderforsearchObserver extends EntryObserver
{
}

View File

@ -1,8 +0,0 @@
<?php namespace Visiosoft\CatsModule\Placeholderforsearch;
use Anomaly\Streams\Platform\Entry\EntryPresenter;
class PlaceholderforsearchPresenter extends EntryPresenter
{
}

View File

@ -1,25 +0,0 @@
<?php namespace Visiosoft\CatsModule\Placeholderforsearch;
use Visiosoft\CatsModule\Placeholderforsearch\Contract\PlaceholderforsearchRepositoryInterface;
use Anomaly\Streams\Platform\Entry\EntryRepository;
class PlaceholderforsearchRepository extends EntryRepository implements PlaceholderforsearchRepositoryInterface
{
/**
* The entry model.
*
* @var PlaceholderforsearchModel
*/
protected $model;
/**
* Create a new PlaceholderforsearchRepository instance.
*
* @param PlaceholderforsearchModel $model
*/
public function __construct(PlaceholderforsearchModel $model)
{
$this->model = $model;
}
}

View File

@ -1,8 +0,0 @@
<?php namespace Visiosoft\CatsModule\Placeholderforsearch;
use Anomaly\Streams\Platform\Entry\EntryRouter;
class PlaceholderforsearchRouter extends EntryRouter
{
}

View File

@ -1,49 +0,0 @@
<?php namespace Visiosoft\CatsModule\Placeholderforsearch;
use Anomaly\Streams\Platform\Database\Seeder\Seeder;
use Illuminate\Support\Facades\DB;
class PlaceholderforsearchSeeder extends Seeder
{
/**
* Run the seeder.
*/
public function run()
{
DB::table('cats_placeholderforsearch')->truncate();
DB::table('cats_placeholderforsearch_translations')->truncate();
PlaceholderforsearchModel::create([
'en' => [
'name' => 'Chevrolet Camaro'
],
'tr' => [
'name' => 'Chevrolet Camaro'
]
]);
PlaceholderforsearchModel::create([
'en' => [
'name' => 'Xiaomi Black Shark 128 GB'
],
'tr' => [
'name' => 'Xiaomi Black Shark 128 GB'
]
]);
PlaceholderforsearchModel::create([
'en' => [
'name' => 'Apple MacBook Pro'
],
'tr' => [
'name' => 'Apple MacBook Pro'
]
]);
PlaceholderforsearchModel::create([
'en' => [
'name' => 'Make your search now'
],
'tr' => [
'name' => 'Make your search now'
]
]);
}
}

View File

@ -1,61 +0,0 @@
<?php namespace Visiosoft\CatsModule\Placeholderforsearch\Table;
use Anomaly\Streams\Platform\Ui\Table\TableBuilder;
class PlaceholderforsearchTableBuilder extends TableBuilder
{
/**
* The table views.
*
* @var array|string
*/
protected $views = [];
/**
* The table filters.
*
* @var array|string
*/
protected $filters = [];
/**
* The table columns.
*
* @var array|string
*/
protected $columns = [];
/**
* The table buttons.
*
* @var array|string
*/
protected $buttons = [
'edit'
];
/**
* The table actions.
*
* @var array|string
*/
protected $actions = [
'delete'
];
/**
* The table options.
*
* @var array
*/
protected $options = [];
/**
* The table assets.
*
* @var array
*/
protected $assets = [];
}