Skip to content

Commit

Permalink
[ADD] Product search.
Browse files Browse the repository at this point in the history
  • Loading branch information
Seiger committed Jan 17, 2024
1 parent 3a52619 commit 19151ae
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 29 deletions.
8 changes: 4 additions & 4 deletions module/sCommerceModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down Expand Up @@ -128,7 +128,7 @@

if ($product) {
$sCommerceController->removeDirRecursive(MODX_BASE_PATH . 'assets/sgallery/product/' . $product->id);

$product->categories()->sync([]);
$product->texts()->delete();
$product->delete();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
30 changes: 30 additions & 0 deletions src/Models/sProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.
*
Expand Down
33 changes: 30 additions & 3 deletions views/index.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@extends('manager::template.page')
@section('content')
<h1><i id="main-logo" class="@lang('sCommerce::global.icon')" data-tooltip="@lang('sCommerce::global.description')"></i> @lang('sCommerce::global.title')</h1>
<h1><i class="@lang('sCommerce::global.icon')" data-tooltip="@lang('sCommerce::global.description')"></i> @lang('sCommerce::global.title')</h1>

<div class="sectionBody">
<div class="tab-pane" id="resourcesPane">
Expand Down Expand Up @@ -118,13 +118,40 @@ function() {
// Flash messages
@if (session()->has('success'))
alertify.success("{{session('success')}}");
alertify.success("{{session('success')}}");
@endif
@if (session()->has('error'))
alertify.success("{{session('error')}}");
alertify.success("{{session('error')}}");
@endif
});
// Search form
const searchForm = document.querySelector('input[name="search"]');
const submitForm = document.querySelector('.scom-submit-search');
const clearFrom = document.querySelector('.scom-clear-search');
searchForm?.addEventListener('keypress', (e) => {
if (e.which == 13) {
searchFormSend(searchForm.value);
}
});
submitForm?.addEventListener('click', () => {
searchFormSend(searchForm.value);
});
clearFrom?.addEventListener('click', () => {
searchForm.value = "";
searchFormSend(searchForm.value);
});
function searchFormSend(search) {
const _url = window.location.pathname;
const _get = new URLSearchParams(window.location.search);
let string = '';
_get.delete('search');
if (search.length > 0) {
string = '&search='+search;
}
window.location.href = _url+'?'+_get.toString()+string;
}
// Save tab content on the fly
const submitting = document.querySelectorAll('[data-target] a');
for (let i = 0; i < submitting.length; i++) {
Expand Down
43 changes: 21 additions & 22 deletions views/productsTab.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,25 @@
<div class="scom-conters-item scom-status-title scom-disactive">@lang('sCommerce::global.unpublisheds'): <span>{{$disactive??0}}</span></div>
</div>
</div>
<form id="search" name="search" method="get" action="{!!$moduleUrl!!}&get=products">
<div class="input-group">
<div class="input-group mb-3">
<input name="search" value="{{request()->search ?? ''}}" type="search" class="form-control rounded-left scom-input seiger__search" placeholder="@lang('sCommerce::global.search_among_products')" aria-label="@lang('sCommerce::global.search_among_products')" aria-describedby="basic-addon2">
<input name="search"
value="{{request()->search ?? ''}}"
type="search"
class="form-control rounded-left scom-input seiger__search"
placeholder="@lang('sCommerce::global.search_among_products') (@lang('sCommerce::global.sku'), @lang('sCommerce::global.product_name'), @lang('global.long_title'), @lang('global.resource_summary'), @lang('sCommerce::global.content'))"
aria-label="@lang('sCommerce::global.search_among_products') (@lang('sCommerce::global.sku'), @lang('sCommerce::global.product_name'), @lang('global.long_title'), @lang('global.resource_summary'), @lang('sCommerce::global.content'))"
aria-describedby="basic-addon2" />
<span class="scom-clear-search">
<svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 32 32" fill="none">
<path d="M22 11.2086L20.7914 10L16 14.7914L11.2086 10L10 11.2086L14.7914 16L10 20.7914L11.2086 22L16 17.2086L20.7914 22L22 20.7914L17.2086 16L22 11.2086Z" fill="#63666B"/>
</svg>
</span>
<div class="input-group-append">
<button class="btn btn-outline-secondary rounded-right" type="submit"><i class="fas fa-search"></i></button>
<button class="btn btn-outline-secondary rounded-right scom-submit-search" type="button"><i class="fas fa-search"></i></button>
</div>
</div>
</form>
</div>
@php($resources = \EvolutionCMS\Models\SiteContent::select('id', 'pagetitle')->whereIn('id', $items->pluck('category')->unique()->toArray())->get()->pluck('pagetitle', 'id')->toArray())
<div class="table-responsive seiger__module-table">
<table class="table table-condensed table-hover sectionTrans scom-table">
Expand Down Expand Up @@ -147,16 +153,16 @@
@if (sCommerce::config('products.show_field_views', 1) == 1)
<td>{{$item->views}}</td>
@endif
<td style="text-align:center;">
<div class="btn-group">
<a href="{!!$moduleUrl!!}&get=product&i={{$item->id}}" class="btn btn-outline-success">
<i class="fa fa-pencil"></i> <span>@lang('global.edit')</span>
</a>
<a href="#" data-href="{!!$moduleUrl!!}&get=productDelete&i={{$item->id}}" data-delete="{{$item->id}}" data-name="{{$item->pagetitle ?? __('sCommerce::global.no_text')}}" class="btn btn-outline-danger">
<i class="fa fa-trash"></i> <span>@lang('global.remove')</span>
</a>
</div>
</td>
<td style="text-align:center;">
<div class="btn-group">
<a href="{!!$moduleUrl!!}&get=product&i={{$item->id}}" class="btn btn-outline-success">
<i class="fa fa-pencil"></i> <span>@lang('global.edit')</span>
</a>
<a href="#" data-href="{!!$moduleUrl!!}&get=productDelete&i={{$item->id}}" data-delete="{{$item->id}}" data-name="{{$item->pagetitle ?? __('sCommerce::global.no_text')}}" class="btn btn-outline-danger">
<i class="fa fa-trash"></i> <span>@lang('global.remove')</span>
</a>
</div>
</td>
</tr>
@endforeach
</tbody>
Expand Down Expand Up @@ -235,7 +241,7 @@ function(){
});
//dropdown
document.addEventListener("click", function (event) {
const dropdowns = document.querySelectorAll('.dropdown');
const dropdowns = document.querySelectorAll('.dropdown');
dropdowns.forEach(function(dropdown) {
if (!dropdown.contains(event.target)) {
dropdown.classList.remove('active')
Expand All @@ -245,13 +251,6 @@ function(){
});
});
//dropdown
//form clear
const clearFrom = document.querySelector('.seiger-clear-search');
const searchForm = document.querySelector('.seiger__search');
clearFrom?.addEventListener('click', () => {
searchForm.value = "";
document.querySelector('#search').submit();
})
// cookies
const cookieName = "scom_products_page_items";
const cookieItems = document.querySelectorAll('[data-items]');
Expand Down

0 comments on commit 19151ae

Please sign in to comment.