ldc-legaulois/app/Http/Controllers/Controller.php
Jérémy Munsch 4d76c8157c
Fix acto controller general config+
Fix actu controller bo image storage path
Refactorize image upload handle
2021-09-23 13:38:11 +02:00

132 lines
4.7 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Str;
class Controller extends BaseController
{
use AuthorizesRequests;
use DispatchesJobs;
use ValidatesRequests;
/**
* Undocumented function
*
* @param Builder $query The eloquent query builder
* @param string $search The query string
* @param string|array ...$field 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,
...$field
): Builder {
if (count($field)) {
$table = $query->getModel()->getTable();
$query = $query->where(function ($q) use ($field, $search, $table) {
foreach ($field as $f) {
$f = str_replace('?', '\?', $f);
if (is_array($f)) {
$q->orWhereRaw(
sprintf("UPPER(CONCAT(%s)) LIKE UPPER(?)", collect($f)->map(function ($q) use ($table) {
return "`$table`.`$q`";
})->implode(', \' \', ')),
["%$search%"]
);
} else {
$q->orWhereRaw("UPPER(`$table`.`$f`) LIKE UPPER(?)", ["%$search%"]);
}
}
});
}
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);
}
if (request()->sort_way) {
Session::put("$rName.sort_way", request()->sort_way);
}
if (request()->rst) {
Session::remove("$rName.sort_col");
Session::remove("$rName.sort_way");
}
if (request()->sort_col and request()->sort_way) {
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 (!request()->rst and Schema::hasColumn($table, 'order')) {
$query = $query->orderBy('order', 'ASC');
Session::put("$rName.sort_col", 'order');
Session::put("$rName.sort_way", 'asc');
}
return $query;
}
/**
* Store image to storage path
* and put into model the image path
*
* @param Request $request
* @param Model $model
* @param string $attribute
* @return void
*/
protected function storeUploadedImage(Request $request, Model &$model, string $attribute)
{
// try delete old file
if (File::exists(ltrim($model->{$attribute}, '/')) and \strpos($model->{$attribute}, '/storage') === 0) {
File::delete(ltrim($model->{$attribute}, '/'));
}
if ($request->{$attribute}) {
/** @var \Illuminate\Http\UploadedFile $image */
$image = $request->{$attribute};
$newPath = $this->prepareMoveFile($image->getClientOriginalName(), $model->getTable());
$newPath = str_replace(storage_path('app'), '', $newPath);
$image->storePubliclyAs(pathinfo($newPath, \PATHINFO_DIRNAME), pathinfo($newPath, \PATHINFO_BASENAME));
$model->{$attribute} = '/storage' . str_replace('/public', '', $newPath);
}
}
protected function prepareMoveFile(string $filename, string $folder): string
{
$pathFolder = \storage_path("app/public/images/$folder");
if (!File::exists($pathFolder)) {
File::makeDirectory($pathFolder, 0755, true);
}
$filename = sprintf('%s-%s', Str::uuid(), $filename);
$fullPath = sprintf('%s/%s', $pathFolder, $filename);
if (File::exists($fullPath)) {
File::delete($fullPath);
}
return $fullPath;
}
}