File: D:/HostingSpaces/SBogers10/shop.komma.nl/app/Catalog/CatalogService.php
<?php
namespace App\Catalog;
use App\Categories\Models\Categorizable;
use App\Categories\Models\Category;
use App\Products\AbstractProductable;
use App\Products\ProductGroup\ProductGroup;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Komma\KMS\Core\ModelService;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
/**
* Has methods for displaying all productables on the frontend.
* And had methods which enabled users to search through models using laravel scout.
*
* Class CatalogService
* @package App\Catalog
*/
class CatalogService extends ModelService
{
/**
* Returns Categorizables that are linked to a category. These all have a relation to
* productable that resolves to one of the productable types.
* With the paginate option you can specify how much items should be visible per page
*
* @param Category $category
* @param int|null $paginate
*
* @param \Closure|null $filterFunction a function that can be used to filter out some of the results.
*
* @return \Illuminate\Database\Eloquent\Collection|LengthAwarePaginator
*/
public function getCategorizablesByCategory(Category $category, int $paginate = null, \Closure $filterFunction = null)
{
$allItemsQuery = Categorizable::whereHas('category', function(Builder $query) use($category) {
$query->where('id', '=', $category->id);
})->with('productable.images','productable.translation');
$allItems = $allItemsQuery->get();
if($filterFunction) $allItems = $allItems->filter($filterFunction);
if (!empty($paginate)) {
//$allItems->paginate($paginate);
$page = 1;
$perPage = $paginate;
$allItems = new \Illuminate\Pagination\LengthAwarePaginator(
$allItems->forPage($page, $perPage),
$allItems->count(),
$perPage,
$page
);
}
return $allItems;
}
/**
* Strip the last part (segment) of the previous url (HTTP_REFERER), and use that to find the
* category of the productable. Then return the parent of that category. If the category of the productable
* or the parent category could not be found, null will be returned
*
* @param AbstractProductable $productable
*
* @return Category|null
*/
public function getProductableParentCategoryUsingReferrer(AbstractProductable $productable) {
//Get the "previous url"
$requestsSource = request()->server('HTTP_REFERER'); //Example: http://localhost:8000/nl/producten
//Use the previous url to find the productables category
$currentProductCategory = null;
if($productable->categories->count() > 0 && $requestsSource) {
$explodedRequestSource = explode('/', $requestsSource);
$categorySlug = end($explodedRequestSource);
$currentProductCategory = $productable->categories()->where('code_name', $categorySlug)->first();
}
//If the products category could not be determined, the parent of that category could also not be found. And we return null.
if(!$currentProductCategory) return null;
//If the product has a parent, we return that.
if($currentProductCategory->getParentId() != 0) {
$parentCategory = $currentProductCategory->getParent();
if($parentCategory) return $parentCategory;
}
//The product did not seem to have a parent. Just return null.
return null;
}
public function destroyForModel(Model $model): Model
{
if(!$model->exists) return $model;
Categorizable::where('categorizable_type', get_class($model))->where('categorizable_id', '=', $model->id)->delete();
return $model;
}
}