diff --git a/app/Http/Controllers/CustomerController.php b/app/Http/Controllers/CustomerController.php index 3bf3384..89cd738 100644 --- a/app/Http/Controllers/CustomerController.php +++ b/app/Http/Controllers/CustomerController.php @@ -9,23 +9,64 @@ use App\Models\Customer; use App\Models\CustomerInfo; use App\Models\CustomerLogin; use App\Models\SingleOptin; +use Illuminate\Http\Request; use Illuminate\Support\Carbon; +use Illuminate\Support\Facades\Schema; +use Illuminate\Support\Facades\Session; use Illuminate\Support\Str; use League\Csv\Writer; use Symfony\Component\HttpFoundation\StreamedResponse; class CustomerController extends Controller { - public function index() + public function index(Request $request) { - $brands = Brand::select('id', 'name')->get(); - $query = CustomerInfo::with(['customer']); - if (request()->brand) { - $query = $query->where('brand_id', request()->brand); - } - $customers = $query->paginate(); $modelTable = (new Customer())->getTable(); $modelClass = Customer::class; + $brands = Brand::select('id', 'name')->get(); + $columns = ['lastname', 'firstname', 'opt_in']; + + /** @var \Illuminate\Database\Eloquent\Builder */ + $query = CustomerInfo::with(['customer']); + if (request()->brand) { + $query = $query->where('brand_id', $request->brand); + } + + // Search on crud Label column + if ($request->search) { + foreach ($columns as $column) { + $query = $query->orWhere($column, 'LIKE', "%$request->search%"); + } + $query = $query->orWhereHas('customer', function ($q) use ($request) { + $q->where('email', 'LIKE', "%$request->search%"); + }); + } + + // Sort columns + if ($request->sort_col) { + Session::put("crud.$modelTable.sort_col", $request->sort_col); + } + if ($request->sort_way) { + Session::put("crud.$modelTable.sort_way", $request->sort_way); + } + + if ($request->rst) { + Session::remove("crud.$modelTable.sort_way"); + Session::remove("crud.$modelTable.sort_col"); + } + if ( + Session::has("crud.$modelTable.sort_col") and + Session::has("crud.$modelTable.sort_way") and + Schema::hasColumn($modelTable, Session::get("crud.$modelTable.sort_col")) + ) { + $query = $query->orderBy( + Session::get("crud.$modelTable.sort_col"), + Session::get("crud.$modelTable.sort_way") + ); + } + + /** @var \Illuminate\Pagination\AbstractPaginator */ + $customers = $query->paginate(config('crud.paginate', 50)); // use a crud controller (any) to gather crudModels $modelsClasses = (new BrandController())->getCrudModels(); diff --git a/composer.json b/composer.json index 05936d8..4317184 100644 --- a/composer.json +++ b/composer.json @@ -32,7 +32,7 @@ "fideloper/proxy": "^4.4", "fruitcake/laravel-cors": "^2.0", "guzzlehttp/guzzle": "^7.0.1", - "kwaadpepper/crud-policies": "^0.1.15", + "kwaadpepper/crud-policies": "^0.1.17", "kwaadpepper/enum": "^2.0", "laravel-lang/lang": "~7.0", "laravel/framework": "^8.39", diff --git a/composer.lock b/composer.lock index 07ce438..c9b2e40 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "ad679e344512ce53865817160816c3fa", + "content-hash": "52ddf0e771e6661178474f2a87aeaf7e", "packages": [ { "name": "arondeparon/laravel-request-sanitizer", @@ -1730,16 +1730,16 @@ }, { "name": "kwaadpepper/crud-policies", - "version": "0.1.15", + "version": "0.1.17", "source": { "type": "git", "url": "https://github.com/Kwaadpepper/crud-policies.git", - "reference": "cf471dc2dad7851474ee005fbc8dfdfe05096b84" + "reference": "007552cbc9f34790acadf1fde1da2eed347be1b9" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/Kwaadpepper/crud-policies/zipball/cf471dc2dad7851474ee005fbc8dfdfe05096b84", - "reference": "cf471dc2dad7851474ee005fbc8dfdfe05096b84", + "url": "https://api.github.com/repos/Kwaadpepper/crud-policies/zipball/007552cbc9f34790acadf1fde1da2eed347be1b9", + "reference": "007552cbc9f34790acadf1fde1da2eed347be1b9", "shasum": "" }, "require": { @@ -1751,6 +1751,7 @@ "require-dev": { "orchestra/testbench": "^6.13", "phpunit/phpunit": "~9.0", + "slevomat/coding-standard": "^7.0", "squizlabs/php_codesniffer": "^3.5" }, "type": "library", @@ -1787,9 +1788,9 @@ ], "support": { "issues": "https://github.com/Kwaadpepper/crud-policies/issues", - "source": "https://github.com/Kwaadpepper/crud-policies/tree/0.1.15" + "source": "https://github.com/Kwaadpepper/crud-policies/tree/0.1.17" }, - "time": "2021-08-27T10:42:55+00:00" + "time": "2021-09-09T12:42:59+00:00" }, { "name": "kwaadpepper/enum", diff --git a/resources/views/customer/index-tableHeaders.blade.php b/resources/views/customer/index-tableHeaders.blade.php index 56cf8c8..2286d8b 100644 --- a/resources/views/customer/index-tableHeaders.blade.php +++ b/resources/views/customer/index-tableHeaders.blade.php @@ -1,62 +1,41 @@ - @php $colname = 'email' @endphp {{ __('Email')}} - @if(!session("crud.$modelTable.sort_way") or session("crud.$modelTable.sort_col") !== $colname or (session("crud.$modelTable.sort_way") === 'desc' and session("crud.$modelTable.sort_col") === $colname)) -  ↓  - @endif - @if(!session("crud.$modelTable.sort_way") or session("crud.$modelTable.sort_col") !== $colname or (session("crud.$modelTable.sort_way") === 'asc' and session("crud.$modelTable.sort_col") === $colname)) -  ↑  - @endif @php $colname = 'lastname' @endphp {{ __('Nom')}} @if(!session("crud.$modelTable.sort_way") or session("crud.$modelTable.sort_col") !== $colname or (session("crud.$modelTable.sort_way") === 'desc' and session("crud.$modelTable.sort_col") === $colname)) -  ↓  +  ↓  @endif @if(!session("crud.$modelTable.sort_way") or session("crud.$modelTable.sort_col") !== $colname or (session("crud.$modelTable.sort_way") === 'asc' and session("crud.$modelTable.sort_col") === $colname)) -  ↑  +  ↑  @endif @php $colname = 'firstname' @endphp {{ __('Prénom')}} @if(!session("crud.$modelTable.sort_way") or session("crud.$modelTable.sort_col") !== $colname or (session("crud.$modelTable.sort_way") === 'desc' and session("crud.$modelTable.sort_col") === $colname)) -  ↓  +  ↓  @endif @if(!session("crud.$modelTable.sort_way") or session("crud.$modelTable.sort_col") !== $colname or (session("crud.$modelTable.sort_way") === 'asc' and session("crud.$modelTable.sort_col") === $colname)) -  ↑  +  ↑  @endif - @php $colname = 'optin' @endphp + @php $colname = 'opt_in' @endphp {{ __('Opt-In')}} @if(!session("crud.$modelTable.sort_way") or session("crud.$modelTable.sort_col") !== $colname or (session("crud.$modelTable.sort_way") === 'desc' and session("crud.$modelTable.sort_col") === $colname)) -  ↓  +  ↓  @endif @if(!session("crud.$modelTable.sort_way") or session("crud.$modelTable.sort_col") !== $colname or (session("crud.$modelTable.sort_way") === 'asc' and session("crud.$modelTable.sort_col") === $colname)) -  ↑  +  ↑  @endif - @php $colname = 'brand_id' @endphp {{ __('Marque')}} - @if(!session("crud.$modelTable.sort_way") or session("crud.$modelTable.sort_col") !== $colname or (session("crud.$modelTable.sort_way") === 'desc' and session("crud.$modelTable.sort_col") === $colname)) -  ↓  - @endif - @if(!session("crud.$modelTable.sort_way") or session("crud.$modelTable.sort_col") !== $colname or (session("crud.$modelTable.sort_way") === 'asc' and session("crud.$modelTable.sort_col") === $colname)) -  ↑  - @endif - @php $colname = 'status' @endphp {{ __('Activé ?')}} - @if(!session("crud.$modelTable.sort_way") or session("crud.$modelTable.sort_col") !== $colname or (session("crud.$modelTable.sort_way") === 'desc' and session("crud.$modelTable.sort_col") === $colname)) -  ↓  - @endif - @if(!session("crud.$modelTable.sort_way") or session("crud.$modelTable.sort_col") !== $colname or (session("crud.$modelTable.sort_way") === 'asc' and session("crud.$modelTable.sort_col") === $colname)) -  ↑  - @endif {{ __('Voir')}} diff --git a/resources/views/customer/index.blade.php b/resources/views/customer/index.blade.php index 80c2959..14926dc 100644 --- a/resources/views/customer/index.blade.php +++ b/resources/views/customer/index.blade.php @@ -38,6 +38,7 @@ + {{ $customers->appends(Request::except('rst'))->links() }} @@ -66,7 +67,7 @@ {{ $customerInfo->lastname }}
- {{ $customerInfo->optin ? 'Oui' :'Non' }} + {{ $customerInfo->opt_in ? 'Oui' :'Non' }} {{ $customerInfo->brand->name }} @@ -97,7 +98,7 @@ @endif
- {{ $customers->links() }} + {{ $customers->appends(Request::except('rst'))->links() }} @include('crud-policies::crud.modules.scriptConstants')