diff --git a/module/sCommerceModule.php b/module/sCommerceModule.php index 4955490..b8aed14 100644 --- a/module/sCommerceModule.php +++ b/module/sCommerceModule.php @@ -46,7 +46,7 @@ $order = request()->input('order', 'id'); $direc = request()->input('direc', 'desc'); - $data['items'] = sProduct::lang($sCommerceController->langDefault())->orderBy($order, $direc)->paginate($perpage); + $data['items'] = sProduct::lang($sCommerceController->langDefault())->search()->orderBy($order, $direc)->paginate($perpage); $data['total'] = sProduct::count(); $data['active'] = sProduct::wherePublished(1)->count(); $data['disactive'] = $data['total'] - $data['active']; @@ -128,7 +128,7 @@ if ($product) { $sCommerceController->removeDirRecursive(MODX_BASE_PATH . 'assets/sgallery/product/' . $product->id); - + $product->categories()->sync([]); $product->texts()->delete(); $product->delete(); @@ -220,7 +220,7 @@ $renders = []; $fields = glob(MODX_BASE_PATH . 'assets/modules/scommerce/builder/*/config.php'); View::getFinder()->setPaths([MODX_BASE_PATH . 'assets/modules/scommerce/builder']); - + if (count($fields)) { foreach ($fields as $field) { $render = str_replace('config.php', 'render.blade.php', $field); @@ -293,7 +293,7 @@ } $data['sCommerceController'] = $sCommerceController; -$data['editor'] = $sCommerceController->textEditor(implode(',', $editor)); +$data['editor'] = count($editor) ? $sCommerceController->textEditor(implode(',', $editor)) : ''; $data['tabs'] = $tabs; $data['get'] = $get; $data['iUrl'] = $iUrl; diff --git a/src/Models/sProduct.php b/src/Models/sProduct.php index 64d3514..6802fa6 100644 --- a/src/Models/sProduct.php +++ b/src/Models/sProduct.php @@ -4,6 +4,7 @@ use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\DB; +use Illuminate\Support\Str; use ReflectionClass; use Seiger\sCommerce\Facades\sCommerce; @@ -100,6 +101,35 @@ public function scopeLang($query, $locale) }); } + /** + * Apply search filters to the query + * + * @param \Illuminate\Database\Eloquent\Builder $query The query builder object + * + * @return \Illuminate\Database\Eloquent\Builder The modified query builder object + */ + public function scopeSearch($query) + { + if (request()->has('search')) { + $fields = collect(['sku', 'pagetitle', 'longtitle', 'introtext', 'content']); + + $search = Str::of(request('search')) + ->stripTags() + ->replaceMatches('/[^\p{L}\p{N}\@\.!#$%&\'*+-\/=?^_`{|}~]/iu', ' ') // allowed symbol in email + ->replaceMatches('/(\s){2,}/', '$1') // removing extra spaces + ->trim()->explode(' ') + ->filter(fn($word) => mb_strlen($word) > 2); + + $select = collect([0]); + + $search->map(fn($word) => $fields->map(fn($field) => $select->push("(CASE WHEN \"{$field}\" LIKE '%{$word}%' THEN 1 ELSE 0 END)"))); // Generate points source + + return $query->addSelect('*', DB::Raw('(' . $select->implode(' + ') . ') as points')) + ->when($search->count(), fn($query) => $query->where(fn($query) => $search->map(fn($word) => $fields->map(fn($field) => $query->orWhere($field, 'like', "%{$word}%"))))) + ->orderByDesc('points'); + } + } + /** * Apply the active scope to the given query builder. * diff --git a/views/index.blade.php b/views/index.blade.php index b198b0b..6690b0b 100644 --- a/views/index.blade.php +++ b/views/index.blade.php @@ -1,6 +1,6 @@ @extends('manager::template.page') @section('content') -