ldc-boutique-cnc/app/Http/Controllers/Controller.php

168 lines
5.8 KiB
PHP
Raw Permalink Normal View History

2020-10-30 17:26:00 +01:00
<?php
namespace App\Http\Controllers;
2022-11-28 16:56:39 +01:00
use Gloudemans\Shoppingcart\Cart;
use Gloudemans\Shoppingcart\CartItem;
2022-10-03 17:13:44 +02:00
use Illuminate\Database\Eloquent\Builder;
2020-10-30 17:26:00 +01:00
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
2022-11-28 16:56:39 +01:00
use Illuminate\Support\Collection;
2022-10-03 17:13:44 +02:00
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Session;
2020-10-30 17:26:00 +01:00
class Controller extends BaseController
{
2022-08-25 12:26:43 +02:00
use AuthorizesRequests;
use DispatchesJobs;
use ValidatesRequests;
2022-10-03 17:13:44 +02:00
/**
* Build Search query on specified fields
* splitting search into words or using whole sentence
*
* @param Builder $query The eloquent query builder.
* @param string $search The query string.
* @param callable $searchQuery The query string.
* @param string ...$fields Either a simple array or a string
* in case of a string the two fields would be concatenate to make the search.
* @return Builder
*/
protected function searchQuery(
Builder $query,
string $search,
callable $searchQuery = null,
string ...$fields
): Builder {
if (count($fields)) {
// Search using words.
$words = explode(' ', $search);
$query->where(function ($query) use ($searchQuery, $words, $search, $fields) {
foreach ($words as $word) {
if (!strlen($word)) {
continue;
}
$this->searchOnFields($query, $word, $fields);
} //end foreach
// Search whole using whole sentence.
$this->searchOnFields($query, $search, $fields);
if ($searchQuery) {
$searchQuery($query);
}
});
} //end if
return $query;
}
/**
* Build query search on all fields
*
* @param Builder $query
* @param string $search
* @param array $fields
* @return Builder
*/
private function searchOnFields(Builder $query, string $search, array $fields): Builder
{
$table = $query->getModel()->getTable();
foreach ($fields as $f) {
$f = str_replace('?', '\?', $f);
if (is_array($f)) {
$query->orWhereRaw(
sprintf("UPPER(CONCAT(%s)) LIKE UPPER(?)", collect($f)->map(function ($q) use ($table) {
return "`$table`.`$q`";
})->implode(', \' \', ')),
['%' . filter_var($search, FILTER_SANITIZE_STRING) . '%']
);
} else {
$query->orWhereRaw(
"UPPER(`$table`.`$f`) LIKE UPPER(?)",
['%' . filter_var($search, FILTER_SANITIZE_STRING) . '%']
);
}
}
return $query;
}
/**
* Sort columns with a query
*
* @param Builder $query
* @param array $ignore
* @return Builder
*/
protected function sortQuery(Builder $query, array $ignore = []): Builder
{
$rName = request()->route()->getName();
$table = $query->getModel()->getTable();
if (request()->sort_col) {
Session::put("$rName.sort_col", request()->sort_col);
2023-02-06 14:00:33 +01:00
Session::put("$rName.sorted", true);
2022-10-03 17:13:44 +02:00
}
if (request()->sort_way) {
Session::put("$rName.sort_way", request()->sort_way);
2023-02-06 14:00:33 +01:00
Session::put("$rName.sorted", true);
2022-10-03 17:13:44 +02:00
}
if (request()->rst) {
2023-02-06 14:00:33 +01:00
Session::remove("$rName.sorted");
2022-10-03 17:13:44 +02:00
Session::remove("$rName.sort_col");
Session::remove("$rName.sort_way");
}
if (request()->sort_col and request()->sort_way and Schema::hasColumn($table, request()->sort_col)) {
if (
!isset($ignore) or
(isset($ignore) and !in_array(request()->sort_col, $ignore))
) {
$query = $query->orderBy(\sprintf("$table.%s", request()->sort_col), request()->sort_way);
}
} elseif (Schema::hasColumn($table, 'order')) {
$query = $query->orderBy('order', 'ASC');
Session::put("$rName.sort_col", 'order');
Session::put("$rName.sort_way", 'asc');
}
return $query;
}
2022-11-28 16:56:39 +01:00
2022-11-30 16:36:27 +01:00
/**
* Filter basket items to keep only the orderable ones
*
* ! Assert Model found, status true and orderable true.
*
* @param \Gloudemans\Shoppingcart\Cart $basket
* @param array<string> $messages Will be filled with messages on warnings.
* @return \Gloudemans\Shoppingcart\Cart
*/
protected function filterBasketKeepOnlyOrderableItems(Cart $basket, array &$messages = []): Cart
{
\collect($basket->content())->each(function (CartItem $item) use (&$basket, &$messages) {
/** @var \App\Models\Product|null */
$product = $item->model;
if (!$product or !$product->status or !$product->orderable) {
$basket->remove($item->rowId);
$messages[] = \trans(
'Le produit :name a été retiré du panier car il n\'est plus disponible',
[
'name' => $item->name
]
);
}
});
return $basket;
}
2022-11-28 16:56:39 +01:00
/**
* Get basket as product list
*
* @param \Gloudemans\Shoppingcart\Cart $basket
* @return \Illuminate\Support\Collection
*/
protected function basketToList(Cart $basket): Collection
{
return $basket->content()->map(function (CartItem $cartItem) {
2022-12-19 16:20:00 +01:00
return \json_decode(\json_encode($cartItem->toArray()));
2022-11-28 16:56:39 +01:00
});
}
2020-10-30 17:26:00 +01:00
}