2024-04-19 10:08:36 +02:00
< ? php
namespace App\Http\Controllers\Fo ;
2024-07-10 17:10:33 +02:00
use App\Enums\StaticPageType ;
2024-04-19 10:08:36 +02:00
use App\Http\Controllers\Controller ;
2024-06-17 16:50:56 +02:00
use App\Models\Meat ;
2024-04-19 10:08:36 +02:00
use App\Models\Product ;
2024-06-17 16:50:56 +02:00
use App\Models\Range ;
use App\Models\Recipe ;
2024-07-10 17:10:33 +02:00
use App\Models\StaticPage ;
2024-06-17 16:50:56 +02:00
use App\Models\Submeat ;
use App\Models\Subrange ;
use Illuminate\Contracts\Pagination\LengthAwarePaginator ;
use Illuminate\Database\Eloquent\Builder ;
2024-04-19 10:08:36 +02:00
use Illuminate\Http\Request ;
2024-06-17 16:50:56 +02:00
use Illuminate\Support\Collection ;
2024-06-27 12:34:33 +02:00
use Illuminate\Support\Facades\Cache ;
2024-04-19 10:08:36 +02:00
class ProductController extends Controller
{
2024-06-17 16:50:56 +02:00
/**
* Use for pagination ,
* number of product models per page .
*
* @ var integer
*/
private $productModelsPerPage = 8 ;
/**
* Get products by a specific range into a json response .
*
* @ param \Illuminate\Contracts\Pagination\LengthAwarePaginator | null $productModelsFiltered
* @ param \Illuminate\Support\Collection < \App\Models\Subrange >| null $subrangeModelsFiltered
* @ param \Illuminate\Support\Collection < \App\Models\Submeat >| null $submeatModelsFiltered
* @ return \Illuminate\Contracts\View\View
*/
public function index (
LengthAwarePaginator $productModelsFiltered = null ,
Collection $subrangeModelsFiltered = null ,
Collection $submeatModelsFiltered = null ,
) : \Illuminate\Contracts\View\View {
2024-10-29 13:23:10 +00:00
2024-06-17 16:50:56 +02:00
/** @var \Illuminate\Contracts\Pagination\LengthAwarePaginator $productModels */
2024-11-06 13:47:54 +00:00
$productModels = $productModelsFiltered ? ? Product :: query ()
2024-06-17 16:50:56 +02:00
-> where ( 'published' , true )
-> orderBy ( 'slug' )
-> paginate ( $this -> productModelsPerPage );
2024-11-06 13:47:54 +00:00
2024-09-26 12:18:18 +02:00
$productDiscoverModels = Product :: getPublishedDiscovered ();
$staticPageModel = StaticPage :: getCachedStaticPage ( StaticPageType :: products );
$rangeModels = Range :: getPublishedOrderedWithSubranges ();
$subrangeModels = $subrangeModelsFiltered ? ? Subrange :: getPublishedOrdered ();
$meatModels = Meat :: getPublishedOrderedWithSubmeats ();
$submeatModels = $submeatModelsFiltered ? ? Submeat :: getPublishedOrdered ();
2024-06-17 16:50:56 +02:00
return view ( 'fo.pages.products.index' , [
'viewsPaginate' => $this -> getViewsFromPaginateModels ( $productModels ),
'productDiscoverModels' => $productDiscoverModels ,
2024-07-10 17:10:33 +02:00
'staticPageModel' => $staticPageModel ,
2024-06-17 16:50:56 +02:00
'rangeModels' => $rangeModels ,
'subrangeModels' => $subrangeModels ,
'meatModels' => $meatModels ,
'submeatModels' => $submeatModels ,
'searchPropositions' => $this -> getProductModelsTitles (),
]);
}
/**
* Get the view of a specific product .
*
* @ param string $slug
* @ return \Illuminate\Contracts\View\View
*/
public function show ( string $slug ) : \Illuminate\Contracts\View\View
{
/** @var \App\Models\Product|null $productModel Current product model */
$productModel = Product :: query () -> where ( 'slug' , $slug ) -> first ();
if ( is_null ( $productModel )) {
abort ( 404 );
}
2024-06-27 12:34:33 +02:00
$productModelSibling = $this -> getSiblingModels ( $productModel );
2024-06-17 16:50:56 +02:00
2024-09-26 12:18:18 +02:00
$staticPageModel = StaticPage :: getCachedStaticPage ( StaticPageType :: product );
2024-06-17 16:50:56 +02:00
2024-06-27 15:54:29 +02:00
/** @var \App\Models\Recipe|null $recipeModel */
2024-09-24 09:43:36 +02:00
$recipeModel = $productModel -> recipes -> isNotEmpty ()
? $productModel -> recipes () -> with ([ 'products' , 'consumptionMoment' ]) -> first ()
2024-06-27 15:54:29 +02:00
: Recipe :: query () -> whereHas ( 'products' , function ( Builder $q ) use ( $productModel ) {
2024-09-24 09:43:36 +02:00
$q -> when ( $productModel -> subrange , function ( Builder $q ) use ( $productModel ) {
$q -> whereHas ( 'subrange' , function ( Builder $q ) use ( $productModel ) {
$q -> where ( 'id' , $productModel -> subrange -> getKey ());
});
}) -> when ( $productModel -> range , function ( Builder $q ) use ( $productModel ) {
$q -> whereHas ( 'range' , function ( Builder $q ) use ( $productModel ) {
$q -> where ( 'id' , $productModel -> range -> getKey ());
});
});
}) -> with ([ 'products' , 'consumptionMoment' ]) -> first ();
2024-06-27 15:54:29 +02:00
/** @var \App\Models\Video $videoModel */
2024-09-24 09:43:36 +02:00
$videoModel = $productModel -> videos -> sortByDesc ( 'created_at' ) -> first ();
2024-06-27 15:54:29 +02:00
2024-10-02 09:57:11 +02:00
$nutritionnalValueModel = $productModel -> getNutritionnalValue ();
2024-06-17 16:50:56 +02:00
return view ( 'fo.pages.products.show' , [
2024-10-02 09:57:11 +02:00
'productModel' => $productModel ,
'nutritionnalValueModel' => $nutritionnalValueModel ,
'prevProductModel' => $productModelSibling [ 0 ],
'nextProductModel' => $productModelSibling [ 1 ],
'productModels' => $this -> getProductsFiltered ( $productModel ),
'recipeModels' => $this -> getRecipesFiltered ( $productModel ),
'recipeModel' => $recipeModel ,
'videoModel' => $videoModel ,
'staticPageModel' => $staticPageModel ,
2024-06-17 16:50:56 +02:00
]);
}
2024-06-27 12:34:33 +02:00
/**
* Get product models titles for search input propositions .
*
* @ param \App\Models\Product $productModel
* @ return array
*/
public function getSiblingModels ( Product $productModel ) : array
{
2024-11-06 13:47:54 +00:00
$filter = Cache :: get ( 'productsFilter' );
2024-10-30 11:25:23 +00:00
$cleanedFilter = $this -> cleanSubSubFilter ( $filter );
2024-11-06 13:47:54 +00:00
2024-06-27 15:54:29 +02:00
/** @var string $slug */
2024-10-30 11:25:23 +00:00
$slug = $filter
? (( is_null ( $productModel -> { $filter })
? $productModel -> { " sub " . $this -> cleanSubSubFilter ( $filter )} -> { $this -> cleanSubSubFilter ( $filter )} ? ? null
: $productModel -> { $filter }) -> slug ? ? " " )
2024-06-27 12:34:33 +02:00
: " " ;
2024-10-30 11:25:23 +00:00
2024-11-06 13:47:54 +00:00
// FIXME: the previous element are added on 30/10/24 to avoid error when productFilters was set on "submeat" for example and user access to product which not have submeat relation -> "Attempt to read property 'sub***' on null"
2024-10-30 11:25:23 +00:00
// this fix will just avoid the error, but in this case, product.show have no previous or next product.
2024-06-27 15:54:29 +02:00
/** @var \Illuminate\Support\Collection $productModelsList */
2024-06-27 12:34:33 +02:00
$productModelsList = Product :: query ()
-> with ( 'range' , 'subrange' , 'meat' , 'submeat' )
2024-10-30 11:25:23 +00:00
-> when ( in_array ( $filter , [ 'range' , 'meat' ]), function ( Builder $q ) use ( $slug , $filter ) {
2024-06-27 12:34:33 +02:00
$q -> where ( 'published' , true )
2024-10-30 11:25:23 +00:00
-> whereHas ( $filter , function ( Builder $q ) use ( $slug , $filter ) {
2024-08-27 17:36:47 +02:00
$q -> where ( 'published' , true ) -> where ( 'slug' , $slug );
})
2024-10-30 11:25:23 +00:00
-> orWhereHas ( 'sub' . $filter , function ( Builder $q ) use ( $slug , $filter ) {
2024-08-27 17:36:47 +02:00
$q -> where ( 'published' , true )
2024-10-30 11:25:23 +00:00
-> whereHas ( $filter , function ( Builder $q ) use ( $slug ) {
2024-08-27 17:36:47 +02:00
$q -> where ( 'published' , true ) -> where ( 'slug' , $slug );
});
});
2024-06-27 12:34:33 +02:00
})
2024-10-30 11:25:23 +00:00
-> when ( in_array ( $filter , [ 'subrange' , 'submeat' ]), function ( Builder $q ) use ( $slug , $filter ) {
2024-06-27 12:34:33 +02:00
$q -> where ( 'published' , true )
2024-10-30 11:25:23 +00:00
-> whereHas ( $filter , function ( Builder $q ) use ( $slug ) {
2024-06-27 12:34:33 +02:00
$q -> where ( 'published' , true ) -> where ( 'slug' , $slug );
});
})
-> where ( 'published' , true )
2024-09-18 09:54:18 +02:00
-> orderBy ( 'slug' )
2024-06-27 12:34:33 +02:00
-> get ();
2024-11-06 13:47:54 +00:00
$collectionIndex = $productModelsList -> search ( function ( $productModelItem , $key ) use ( $productModel ) {
2024-06-27 12:34:33 +02:00
if ( $productModelItem -> getKey () === $productModel -> getKey ()) {
return $key ;
};
});
$productModelSibling [ 0 ] = ( is_null ( $productModelsList -> get ( $collectionIndex - 1 )))
? $productModelsList -> last ()
: $productModelsList -> get ( $collectionIndex - 1 );
$productModelSibling [ 1 ] = ( is_null ( $productModelsList -> get ( $collectionIndex + 1 )))
? $productModelsList -> first ()
: $productModelsList -> get ( $collectionIndex + 1 );
return $productModelSibling ;
}
2024-06-17 16:50:56 +02:00
/**
* Get product models titles for search input propositions .
*
* @ param string | null $search
* @ return \Illuminate\Pagination\LengthAwarePaginator
*/
public function getProductModelsTitles ( string | null $search = null ) : \Illuminate\Pagination\LengthAwarePaginator
{
$query = Product :: query ();
return $this -> getModelsTitles ( $query , 'label' , $search );
}
/**
* Get a list of words for the search input propositions .
*
* @ param \Illuminate\Http\Request $request
* @ return \Illuminate\Http\JsonResponse
*/
public function getWordsList ( Request $request ) : \Illuminate\Http\JsonResponse
{
return response () -> json ( $this -> getProductModelsTitles ( $request -> input ( 'search' )));
}
/**
* Get products by specific words ,
* the response can be a json or a view .
*
* @ param \Illuminate\Http\Request $request
* @ return \Illuminate\Http\JsonResponse | \Illuminate\Contracts\View\View
*/
public function getProductsByWords ( Request $request ) : \Illuminate\Http\JsonResponse | \Illuminate\Contracts\View\View
{
/** @var \Illuminate\Database\Eloquent\Builder $productModels */
$productModels = Product :: query ()
-> with ([ 'range' , 'subrange' , 'meat' , 'submeat' ])
-> where ( 'published' , true )
-> orderBy ( 'slug' );
/** @var string $search Search field */
$search = $request -> input ( 'words' );
if ( $search ) {
$this -> searchQuery (
$productModels ,
str_replace ( " , " , " " , $request -> input ( 'words' )),
null ,
'label' ,
);
}
$productModels = $productModels -> paginate ( $this -> productModelsPerPage );
if ( $request -> ajax ()) {
return response () -> json ([
'viewsPaginate' => $this -> getViewsFromPaginateModels ( $productModels ),
'children' => [],
]);
}
return $this -> index ( $productModels , collect ());
}
/**
* Get products by a specific meat ,
* the response can be a json or a view .
*
* @ param \Illuminate\Http\Request $request
* @ return \Illuminate\Http\JsonResponse | \Illuminate\Contracts\View\View
*/
public function getProductsByRange (
Request $request
) : \Illuminate\Http\JsonResponse | \Illuminate\Contracts\View\View {
2024-06-27 12:34:33 +02:00
Cache :: put ( 'productsFilter' , 'range' );
2024-06-17 16:50:56 +02:00
$rangeModel = Range :: query ()
-> where ( 'slug' , $request -> model_slug )
-> where ( 'published' , true )
-> first ();
$subrangeModels = Subrange :: query ()
-> when ( ! is_null ( $rangeModel ), function ( Builder $q ) use ( $rangeModel ) {
$q -> where ( 'range_id' , $rangeModel -> getKey ());
})
-> where ( 'published' , true )
2024-09-18 09:54:18 +02:00
-> orderBy ( 'order' )
2024-06-17 16:50:56 +02:00
-> get ();
2024-10-31 13:48:07 +00:00
$productModels = Product :: query ()
2024-06-17 16:50:56 +02:00
-> with ([ 'range' , 'subrange' , 'meat' , 'submeat' ])
-> where ( 'published' , true )
2024-10-29 13:23:10 +00:00
-> when ( ! is_null ( $rangeModel ), function ( Builder $q ) use ( $rangeModel , $request ) {
2024-11-06 13:47:54 +00:00
$q -> whereHas ( 'range' , function ( Builder $q ) use ( $rangeModel ) {
$q -> where ( 'published' , true ) -> where ( 'id' , $rangeModel -> getKey ());
}) -> orWhereHas ( 'subrange' , function ( Builder $q ) use ( $rangeModel , $request ) {
if ( $request -> child_slug ) {
2024-10-29 13:23:10 +00:00
$q -> where ( 'published' , true ) -> where ( " slug " , $request -> child_slug );
2024-11-06 13:47:54 +00:00
} else {
$q -> where ( 'published' , true ) -> whereIn ( 'id' , $rangeModel -> subranges -> pluck ( 'id' ) -> toArray ());
}
});
2024-06-17 16:50:56 +02:00
})
-> where ( 'published' , true )
-> orderBy ( 'slug' )
-> paginate ( $this -> productModelsPerPage );
2024-10-31 13:48:07 +00:00
2024-06-17 16:50:56 +02:00
if ( $request -> ajax ()) {
return response () -> json ([
'viewsPaginate' => $this -> getViewsFromPaginateModels ( $productModels ),
'children' => $subrangeModels ,
2024-11-06 13:47:54 +00:00
'productModels' => $productModels ,
2024-06-17 16:50:56 +02:00
]);
}
2024-11-06 13:47:54 +00:00
2024-06-17 16:50:56 +02:00
return $this -> index ( $productModels , $subrangeModels );
}
/**
* Get products by a specific submeat ,
* the response can be a json or a view .
*
* @ param \Illuminate\Http\Request $request
* @ return \Illuminate\Http\JsonResponse | \Illuminate\Contracts\View\View
*/
public function getProductsBySubrange (
Request $request
) : \Illuminate\Http\JsonResponse | \Illuminate\Contracts\View\View {
2024-06-27 12:34:33 +02:00
Cache :: put ( 'productsFilter' , 'subrange' );
2024-11-06 13:47:54 +00:00
$rangeModel = Range :: query ()
2024-06-17 16:50:56 +02:00
-> where ( 'slug' , $request -> model_slug )
-> where ( 'published' , true )
-> first ();
2024-11-06 13:47:54 +00:00
$subrangeModel = Subrange :: query ()
2024-06-17 16:50:56 +02:00
-> when ( ! is_null ( $rangeModel ), function ( Builder $q ) use ( $rangeModel ) {
$q -> where ( 'range_id' , $rangeModel -> getKey ());
})
-> where ( 'slug' , $request -> child_slug )
-> where ( 'published' , true )
-> first ();
2024-11-06 13:47:54 +00:00
2024-06-17 16:50:56 +02:00
$subrangeModels = Subrange :: query ()
-> when ( ! is_null ( $rangeModel ), function ( Builder $q ) use ( $rangeModel ) {
$q -> where ( 'range_id' , $rangeModel -> getKey ());
})
-> where ( 'published' , true )
2024-09-18 09:54:18 +02:00
-> orderBy ( 'order' )
2024-06-17 16:50:56 +02:00
-> get ();
$productModels = Product :: query ()
-> with ([ 'range' , 'subrange' , 'meat' , 'submeat' ])
-> where ( 'published' , true )
-> when ( ! is_null ( $subrangeModel ), function ( Builder $q ) use ( $subrangeModel ) {
$q -> whereHas ( 'subrange' , function ( Builder $q ) use ( $subrangeModel ) {
$q -> where ( 'published' , true ) -> where ( 'id' , $subrangeModel -> getKey ());
});
})
-> orderBy ( 'slug' )
-> paginate ( $this -> productModelsPerPage );
2024-11-06 13:47:54 +00:00
2024-06-17 16:50:56 +02:00
if ( $request -> ajax ()) {
return response () -> json ([
2024-11-06 13:47:54 +00:00
'productModels' => $productModels ,
2024-06-17 16:50:56 +02:00
'viewsPaginate' => $this -> getViewsFromPaginateModels ( $productModels ),
2024-11-06 13:47:54 +00:00
'children' => $subrangeModels ,
2024-06-17 16:50:56 +02:00
]);
}
return $this -> index ( $productModels , $subrangeModels );
}
/**
* Get products by a specific meat ,
* the response can be a json or a view .
*
* @ param \Illuminate\Http\Request $request
* @ return \Illuminate\Http\JsonResponse | \Illuminate\Contracts\View\View
*/
public function getProductsByMeat (
Request $request
) : \Illuminate\Http\JsonResponse | \Illuminate\Contracts\View\View {
2024-06-27 12:34:33 +02:00
Cache :: put ( 'productsFilter' , 'meat' );
2024-06-17 16:50:56 +02:00
$meatModel = Meat :: query ()
-> where ( 'slug' , $request -> model_slug )
-> where ( 'published' , true )
-> first ();
$submeatModels = Submeat :: query ()
-> when ( ! is_null ( $meatModel ), function ( Builder $q ) use ( $meatModel ) {
$q -> where ( 'meat_id' , $meatModel -> getKey ());
})
-> where ( 'published' , true )
2024-09-18 09:54:18 +02:00
-> orderBy ( 'order' )
2024-06-17 16:50:56 +02:00
-> get ();
$productModels = Product :: query ()
-> with ([ 'range' , 'subrange' , 'meat' , 'submeat' ])
-> when ( ! is_null ( $meatModel ), function ( Builder $q ) use ( $meatModel ) {
$q -> whereHas ( 'meat' , function ( Builder $q ) use ( $meatModel ) {
$q -> where ( 'published' , true ) -> where ( 'id' , $meatModel -> getKey ());
}) -> orWhereHas ( 'submeat' , function ( Builder $q ) use ( $meatModel ) {
$q -> where ( 'published' , true ) -> whereIn ( 'id' , $meatModel -> submeats -> pluck ( 'id' ) -> toArray ());
});
})
-> where ( 'published' , true )
-> orderBy ( 'slug' )
-> paginate ( $this -> productModelsPerPage );
if ( $request -> ajax ()) {
return response () -> json ([
'viewsPaginate' => $this -> getViewsFromPaginateModels ( $productModels ),
'children' => $submeatModels ,
]);
}
return $this -> index ( $productModels , null , $submeatModels );
}
/**
* Get products by a specific submeat ,
* the response can be a json or a view .
*
* @ param \Illuminate\Http\Request $request
* @ return \Illuminate\Http\JsonResponse | \Illuminate\Contracts\View\View
*/
public function getProductsBySubmeat (
Request $request
) : \Illuminate\Http\JsonResponse | \Illuminate\Contracts\View\View {
2024-06-27 12:34:33 +02:00
Cache :: put ( 'productsFilter' , 'submeat' );
2024-06-17 16:50:56 +02:00
$meatModel = Meat :: query ()
-> where ( 'slug' , $request -> model_slug )
-> where ( 'published' , true )
-> first ();
$submeatModel = Submeat :: query ()
-> when ( ! is_null ( $meatModel ), function ( Builder $q ) use ( $meatModel ) {
$q -> where ( 'meat_id' , $meatModel -> getKey ());
})
-> where ( 'slug' , $request -> child_slug )
-> where ( 'published' , true )
-> first ();
$submeatModels = Submeat :: query ()
-> when ( ! is_null ( $meatModel ), function ( Builder $q ) use ( $meatModel ) {
$q -> where ( 'meat_id' , $meatModel -> getKey ());
})
-> where ( 'published' , true )
2024-09-18 09:54:18 +02:00
-> orderBy ( 'order' )
2024-06-17 16:50:56 +02:00
-> get ();
$productModels = Product :: query ()
-> with ([ 'range' , 'subrange' , 'meat' , 'submeat' ])
-> where ( 'published' , true )
-> when ( ! is_null ( $submeatModel ), function ( Builder $q ) use ( $submeatModel ) {
$q -> whereHas ( 'submeat' , function ( Builder $q ) use ( $submeatModel ) {
$q -> where ( 'published' , true ) -> where ( 'id' , $submeatModel -> getKey ());
});
})
-> orderBy ( 'slug' )
-> paginate ( $this -> productModelsPerPage );
if ( $request -> ajax ()) {
return response () -> json ([
'viewsPaginate' => $this -> getViewsFromPaginateModels ( $productModels ),
2024-11-06 13:47:54 +00:00
'children' => $submeatModels ,
2024-06-17 16:50:56 +02:00
]);
}
return $this -> index ( $productModels , null , $submeatModels );
}
/**
* Get products by a selected product model ,
* filtered first by product subrange / range ,
* and by meats / submeats .
*
* @ param \App\Models\Product $productModel
* @ return \Illuminate\Support\Collection
*/
public function getProductsFiltered ( Product $productModel ) : \Illuminate\Support\Collection
{
/** @var int $nbrMaxProducts Maximum number of products required */
$nbrMaxProducts = 9 ;
/** @var \Illuminate\Support\Collection<\App\Models\Product> $productModels products by same product */
$productModels = Product :: query ()
-> whereNot ( 'id' , $productModel -> getKey ())
-> where ( 'published' , true )
-> when ( ! is_null ( $productModel -> range ), function ( Builder $q ) use ( $productModel ) {
$q -> whereHas ( 'range' , function ( Builder $q ) use ( $productModel ) {
$q -> where ( 'published' , true ) -> where ( 'id' , $productModel -> range -> getKey ());
}) -> orWhereHas ( 'subrange' , function ( Builder $q ) use ( $productModel ) {
$q -> whereHas ( 'range' , function ( Builder $q ) use ( $productModel ) {
$q -> where ( 'published' , true ) -> where ( 'id' , $productModel -> range -> getKey ());
});
});
}) -> when ( ! is_null ( $productModel -> subrange ), function ( Builder $q ) use ( $productModel ) {
$q -> whereHas ( 'subrange' , function ( Builder $q ) use ( $productModel ) {
$q -> where ( 'published' , true ) -> where ( 'id' , $productModel -> subrange -> getKey ());
});
})
-> get ();
if ( count ( $productModels ) < $nbrMaxProducts ) {
$test = Product :: query ()
-> whereNot ( 'id' , $productModel -> getKey ())
-> where ( 'published' , true )
-> when ( ! is_null ( $productModel -> meat ), function ( Builder $q ) use ( $productModel ) {
$q -> whereHas ( 'meat' , function ( Builder $q ) use ( $productModel ) {
$q -> where ( 'published' , true ) -> where ( 'id' , $productModel -> meat -> getKey ());
}) -> orWhereHas ( 'submeat' , function ( Builder $q ) use ( $productModel ) {
$q -> whereHas ( 'meat' , function ( Builder $q ) use ( $productModel ) {
$q -> where ( 'published' , true ) -> where ( 'id' , $productModel -> meat -> getKey ());
});
});
}) -> when ( ! is_null ( $productModel -> submeat ), function ( Builder $q ) use ( $productModel ) {
$q -> whereHas ( 'submeat' , function ( Builder $q ) use ( $productModel ) {
$q -> where ( 'published' , true ) -> where ( 'id' , $productModel -> submeat -> getKey ());
});
})
-> get ();
}
return $productModels -> slice ( 0 , $nbrMaxProducts );
}
/**
* Get recipes by a selected product model ,
* filtered first by products associated ,
* by meats / submeats and finally by consumption moment .
*
* @ param \App\Models\Product $productModel
* @ return \Illuminate\Support\Collection
*/
public function getRecipesFiltered ( Product $productModel ) : \Illuminate\Support\Collection
{
/** @var int $nbrMaxRecipes Maximum number of recipes required */
$nbrMaxRecipes = 9 ;
/** @var \Illuminate\Support\Collection<\App\Models\Recipe> $recipeModels Recipes by same product */
$recipeModels = Recipe :: query ()
-> with ([ 'products' , 'products.meat' , 'products.submeat' , 'consumptionMoment' ])
-> where ( 'published' , true )
-> whereHas ( 'products' , function ( Builder $q ) use ( $productModel ) {
$q -> where ( 'id' , $productModel -> getKey ());
})
-> get ();
if ( count ( $recipeModels ) < $nbrMaxRecipes ) {
/** @var \Illuminate\Support\Collection<\App\Models\Recipe> $recipeModels Recipes by same product range */
$recipeModels = $recipeModels -> merge ( Recipe :: query ()
-> with ([ 'products' , 'products.meat' , 'products.submeat' , 'consumptionMoment' ])
-> where ( 'published' , true )
-> whereHas ( 'products' , function ( Builder $q ) use ( $productModel ) {
$q -> when ( ! is_null ( $productModel -> range ), function ( Builder $q ) use ( $productModel ) {
$q -> whereHas ( 'range' , function ( Builder $q ) use ( $productModel ) {
$q -> where ( 'published' , true ) -> where ( 'id' , $productModel -> range -> getKey ());
}) -> orWhereHas ( 'subrange' , function ( Builder $q ) use ( $productModel ) {
$q -> whereHas ( 'range' , function ( Builder $q ) use ( $productModel ) {
$q -> where ( 'published' , true ) -> where ( 'id' , $productModel -> range -> getKey ());
});
});
}) -> when ( ! is_null ( $productModel -> subrange ), function ( Builder $q ) use ( $productModel ) {
$q -> whereHas ( 'subrange' , function ( Builder $q ) use ( $productModel ) {
$q -> where ( 'published' , true ) -> where ( 'id' , $productModel -> subrange -> getKey ());
});
});
})
-> get ());
} //end if
return $recipeModels -> slice ( 0 , $nbrMaxRecipes );
}
/**
* Get an array of views for each product models given .
*
* @ param \Illuminate\Contracts\Pagination\LengthAwarePaginator $productModels
* @ return array
*/
public function getViewsFromPaginateModels ( LengthAwarePaginator $productModels ) : array
{
// @phpstan-ignore-next-line
return $productModels -> through ( function ( $productModel ) {
return view ( 'fo.partials.product-card' , [ 'model' => $productModel ]) -> render ();
}) -> toArray ();
}
2024-04-19 10:08:36 +02:00
/**
* Get products by a specific range into a json response .
*
* @ param \Illuminate\Http\Request $request
* @ return \Illuminate\Http\JsonResponse
*/
public function jsonGetProductsViewsByRange ( Request $request ) : \Illuminate\Http\JsonResponse
{
2024-08-30 16:09:01 +02:00
return response () -> json ( Product :: getProductsViewsByRange ( $request -> LEGENDSLUG ));
2024-04-19 10:08:36 +02:00
}
2024-10-30 11:25:23 +00:00
2024-11-06 13:47:54 +00:00
/**
* Delete the " sub " start filter to avoid " subsubrange " when user has some filters
* and hard visit a total different product filter
*
* @ var string $filter
* @ return mixed
*/
private function cleanSubSubFilter ( $filter ) : mixed
{
2024-10-30 11:25:23 +00:00
return str_starts_with ( $filter , " sub " ) ? preg_replace ( '/^sub/' , '' , $filter ) : $filter ;
}
2024-04-19 10:08:36 +02:00
}