first upload all files

This commit is contained in:
NW
2023-06-11 13:14:03 +01:00
parent f14dbc52b5
commit c08b36d1b6
1705 changed files with 106852 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
<?php
namespace Modules\Product\Http\Controllers\Admin;
use Modules\Product\Entities\Product;
use Modules\Admin\Traits\HasCrudActions;
use Modules\Product\Http\Requests\SaveProductRequest;
class ProductController
{
use HasCrudActions;
/**
* Model for the resource.
*
* @var string
*/
protected $model = Product::class;
/**
* Label of the resource.
*
* @var string
*/
protected $label = 'product::products.product';
/**
* View path of the resource.
*
* @var string
*/
protected $viewPath = 'product::admin.products';
/**
* Form requests for the resource.
*
* @var array|string
*/
protected $validation = SaveProductRequest::class;
}

View File

@@ -0,0 +1,68 @@
<?php
namespace Modules\Product\Http\Controllers;
use Illuminate\Routing\Controller;
use Modules\Review\Entities\Review;
use Modules\Product\Entities\Product;
use Modules\Product\Events\ProductViewed;
use Modules\Product\Filters\ProductFilter;
use Modules\Product\Http\Middleware\SetProductSortOption;
class ProductController extends Controller
{
use ProductSearch;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware(SetProductSortOption::class)->only('index');
}
/**
* Display a listing of the resource.
*
* @param \Modules\Product\Entities\Product $model
* @param \Modules\Product\Filters\ProductFilter $productFilter
* @return \Illuminate\Http\Response
*/
public function index(Product $model, ProductFilter $productFilter)
{
if (request()->expectsJson()) {
return $this->searchProducts($model, $productFilter);
}
return view('public.products.index');
}
/**
* Show the specified resource.
*
* @param string $slug
* @return \Illuminate\Http\Response
*/
public function show($slug)
{
$product = Product::findBySlug($slug);
$relatedProducts = $product->relatedProducts()->forCard()->get();
$upSellProducts = $product->upSellProducts()->forCard()->get();
$review = $this->getReviewData($product);
event(new ProductViewed($product));
return view('public.products.show', compact('product', 'relatedProducts', 'upSellProducts', 'review'));
}
private function getReviewData(Product $product)
{
if (! setting('reviews_enabled')) {
return;
}
return Review::countAndAvgRating($product);
}
}

View File

@@ -0,0 +1,52 @@
<?php
namespace Modules\Product\Http\Controllers;
use Modules\Cart\CartItem;
use Darryldecode\Cart\ItemCollection;
use Modules\Product\Entities\Product;
use Modules\Product\Services\ChosenProductOptions;
class ProductPriceController
{
/**
* Show the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$product = Product::queryWithoutEagerRelations()
->select('id')
->withPrice()
->findOrFail($id);
$variantPrice = $this->cartItem($product, request('options', []))
->total()
->convertToCurrentCurrency()
->format();
return product_price_formatted($product, function ($price) use ($product, $variantPrice) {
if (! $product->hasSpecialPrice()) {
return $variantPrice;
}
return "{$variantPrice} <span class='previous-price'>{$price}</span>";
});
}
private function cartItem(Product $product, array $options)
{
$chosenOptions = new ChosenProductOptions($product, $options);
return new CartItem(new ItemCollection([
'id' => $product->id,
'quantity' => 1,
'attributes' => [
'product' => $product,
'options' => $chosenOptions->getEntities(),
],
]));
}
}

View File

@@ -0,0 +1,74 @@
<?php
namespace Modules\Product\Http\Controllers;
use Illuminate\Support\Facades\DB;
use Modules\Product\Entities\Product;
use Modules\Category\Entities\Category;
use Modules\Attribute\Entities\Attribute;
use Modules\Product\Filters\ProductFilter;
use Modules\Product\Events\ShowingProductList;
trait ProductSearch
{
/**
* Search products for the request.
*
* @param \Modules\Product\Entities\Product $model
* @param \Modules\Product\Filters\ProductFilter $productFilter
* @return \Illuminate\Http\Response
*/
public function searchProducts(Product $model, ProductFilter $productFilter)
{
$productIds = [];
if (request()->filled('query')) {
$model = $model->search(request('query'));
$productIds = $model->keys();
}
$query = $model->filter($productFilter);
if (request()->filled('category')) {
$productIds = (clone $query)->select('products.id')->resetOrders()->pluck('id');
}
$products = $query->paginate(request('perPage', 30));
event(new ShowingProductList($products));
return response()->json([
'products' => $products,
'attributes' => $this->getAttributes($productIds),
]);
}
private function getAttributes($productIds)
{
if (! request()->filled('category') || $this->filteringViaRootCategory()) {
return collect();
}
return Attribute::with('values')
->where('is_filterable', true)
->whereHas('categories', function ($query) use ($productIds) {
$query->whereIn('id', $this->getProductsCategoryIds($productIds));
})
->get();
}
private function filteringViaRootCategory()
{
return Category::where('slug', request('category'))
->firstOrNew([])
->isRoot();
}
private function getProductsCategoryIds($productIds)
{
return DB::table('product_categories')
->whereIn('product_id', $productIds)
->distinct()
->pluck('category_id');
}
}

View File

@@ -0,0 +1,83 @@
<?php
namespace Modules\Product\Http\Controllers;
use Modules\Product\Entities\Product;
use Illuminate\Database\Eloquent\Builder;
use Modules\Product\Http\Response\SuggestionsResponse;
class SuggestionController
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Product $model)
{
$products = $this->getProducts($model);
return new SuggestionsResponse(
request('query'),
$products,
$products->pluck('categories')->flatten(),
$this->getTotalResults($model)
);
}
/**
* Get total results count.
*
* @param \Modules\Product\Entities\Product $model
* @return int
*/
private function getTotalResults(Product $model)
{
return $model->search(request('query'))
->query()
->when(request()->filled('category'), $this->categoryQuery())
->count();
}
/**
* Get products suggestions.
*
* @param \Modules\Product\Entities\Product $model
* @return \Illuminate\Database\Eloquent\Collection
*/
private function getProducts(Product $model)
{
return $model->search(request('query'))
->query()
->limit(10)
->withName()
->withBaseImage()
->withPrice()
->addSelect([
'products.id',
'products.slug',
'products.in_stock',
'products.manage_stock',
'products.qty',
])
->with(['files', 'categories' => function ($query) {
$query->limit(5);
}])
->when(request()->filled('category'), $this->categoryQuery())
->get();
}
/**
* Returns categories condition closure.
*
* @return \Closure
*/
private function categoryQuery()
{
return function (Builder $query) {
$query->whereHas('categories', function ($categoryQuery) {
$categoryQuery->where('slug', request('category'));
});
};
}
}

View File

@@ -0,0 +1,51 @@
<?php
namespace Modules\Product\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class SetProductSortOption
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
if ($this->shouldSetRelevanceSortOption($request)) {
$request->query->set('sort', 'relevance');
}
if ($this->shouldSetLatestSortOption($request)) {
$request->query->set('sort', 'latest');
}
return $next($request);
}
/**
* Determine if the request should set "relevance" sort option.
*
* @param \Illuminate\Http\Request $request
* @return void
*/
private function shouldSetRelevanceSortOption($request)
{
return $request->has('query') && ! $request->has('sort');
}
/**
* Determine if the request should set "latest" sort option.
*
* @param \Illuminate\Http\Request $request
* @return bool
*/
private function shouldSetLatestSortOption($request)
{
return ! $request->has('query') && ! $request->has('sort');
}
}

View File

@@ -0,0 +1,58 @@
<?php
namespace Modules\Product\Http\Requests;
use Illuminate\Validation\Rule;
use Modules\Product\Entities\Product;
use Modules\Core\Http\Requests\Request;
class SaveProductRequest extends Request
{
/**
* Available attributes.
*
* @var string
*/
protected $availableAttributes = 'product::attributes';
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'slug' => $this->getSlugRules(),
'name' => 'required',
'description' => 'required',
'brand_id' => ['nullable', Rule::exists('brands', 'id')],
'tax_class_id' => ['nullable', Rule::exists('tax_classes', 'id')],
'is_virtual' => 'required|boolean',
'is_active' => 'required|boolean',
'price' => 'required|numeric|min:0|max:99999999999999',
'special_price' => 'nullable|numeric|min:0|max:99999999999999',
'special_price_type' => ['nullable', Rule::in(['fixed', 'percent'])],
'special_price_start' => 'nullable|date',
'special_price_end' => 'nullable|date',
'manage_stock' => 'required|boolean',
'qty' => 'required_if:manage_stock,1|nullable|numeric',
'in_stock' => 'required|boolean',
'new_from' => 'nullable|date',
'new_to' => 'nullable|date',
];
}
private function getSlugRules()
{
$rules = $this->route()->getName() === 'admin.products.update' ? ['required'] : ['sometimes'];
$slug = Product::withoutGlobalScope('active')
->where('id', $this->id)
->value('slug');
$rules[] = Rule::unique('products', 'slug')->ignore($slug, 'slug');
return $rules;
}
}

View File

@@ -0,0 +1,105 @@
<?php
namespace Modules\Product\Http\Response;
use Illuminate\Support\Collection;
use Modules\Product\Entities\Product;
use Modules\Category\Entities\Category;
use Illuminate\Contracts\Support\Responsable;
class SuggestionsResponse implements Responsable
{
private $query;
private $products;
private $categories;
private $totalResults;
/**
* Create a new instance.
*
* @param string $query
* @param int $totalResults
* @param \Illuminate\Support\Collection $products
* @param \Illuminate\Support\Collection $categories
*/
public function __construct($query, Collection $products, Collection $categories, $totalResults)
{
$this->query = $query;
$this->products = $products;
$this->categories = $categories;
$this->totalResults = $totalResults;
}
/**
* Create an HTTP response that represents the object.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function toResponse($request)
{
return response()->json([
'categories' => $this->transformCategories(),
'products' => $this->transformProducts(),
'remaining' => $this->getRemainingCount(),
]);
}
/**
* Transform the categories.
*
* @return \Illuminate\Support\Collection
*/
private function transformCategories()
{
return $this->categories->map(function (Category $category) {
return [
'slug' => $category->slug,
'name' => $category->name,
'url' => $category->url(),
];
})->unique('slug')->values();
}
/**
* Transform the products.
*
* @return \Illuminate\Support\Collection
*/
private function transformProducts()
{
return $this->products->map(function (Product $product) {
return [
'slug' => $product->slug,
'name' => $this->highlight($product->name),
'formatted_price' => $product->getFormattedPriceAttribute(),
'base_image' => $product->getBaseImageAttribute(),
'is_out_of_stock' => $product->isOutOfStock(),
'url' => $product->url(),
];
});
}
/**
* Highlight the given text.
*
* @param string $text
* @return string
*/
private function highlight($text)
{
$query = str_replace(' ', '|', preg_quote($this->query));
return preg_replace("/($query)/i", '<em>$1</em>', $text);
}
/**
* Get remaining results count.
*
* @return int
*/
private function getRemainingCount()
{
return $this->totalResults - $this->products->count();
}
}