From a29faabbf2b8315686c31f8e7f0fb8e0807fce04 Mon Sep 17 00:00:00 2001 From: Ludovic CANDELLIER Date: Tue, 12 Sep 2023 23:00:36 +0200 Subject: [PATCH] add methods to get icon on article natures --- .../Admin/Shop/ArticleNaturesDataTable.php | 11 +- .../Admin/Shop/ArticleNatureController.php | 17 ++- .../Controllers/Shop/CategoryController.php | 37 +---- app/Models/Shop/Article.php | 24 +-- app/Models/Shop/ArticleNature.php | 20 ++- app/Models/Shop/Basket.php | 3 +- app/Models/Shop/Categorizable.php | 8 +- app/Models/Shop/Category.php | 6 +- app/Models/Shop/Customer.php | 12 +- app/Repositories/Core/DateRange.php | 91 ++++++++---- app/Repositories/Core/DateTime.php | 89 ++++++++--- app/Repositories/Core/File.php | 84 ++++++++++- app/Repositories/Core/HelperDate.php | 7 +- app/Repositories/Core/MediaPathGenerator.php | 31 ++++ app/Repositories/Core/Storage.php | 10 ++ app/Repositories/Shop/ArticleNatures.php | 59 ++++++++ .../Admin/Shop/ArticleNatures/edit.blade.php | 9 +- .../Admin/Shop/ArticleNatures/form.blade.php | 54 ++++--- .../views/Shop/Customers/profile.blade.php | 4 +- resources/views/Shop/Shelves/shelve.blade.php | 2 +- .../components/widgets/imgUpload.blade.php | 21 +++ .../load/form/upload/fileinput.blade.php | 139 ++++++++++++++++-- 22 files changed, 583 insertions(+), 155 deletions(-) create mode 100644 app/Repositories/Core/MediaPathGenerator.php create mode 100644 resources/views/components/widgets/imgUpload.blade.php diff --git a/app/Datatables/Admin/Shop/ArticleNaturesDataTable.php b/app/Datatables/Admin/Shop/ArticleNaturesDataTable.php index 2ce6fb1c..9a4c6daa 100644 --- a/app/Datatables/Admin/Shop/ArticleNaturesDataTable.php +++ b/app/Datatables/Admin/Shop/ArticleNaturesDataTable.php @@ -4,6 +4,7 @@ namespace App\Datatables\Admin\Shop; use App\Datatables\ParentDataTable as DataTable; use App\Models\Shop\ArticleNature; +use App\Repositories\Core\Medias; use App\Repositories\Shop\ArticleNatures; use Yajra\DataTables\Html\Column; @@ -11,6 +12,8 @@ class ArticleNaturesDataTable extends DataTable { public $model_name = 'article_natures'; + public $sortedColumn = 1; + public function query(ArticleNature $model) { $model = $model::withCount('Articles'); @@ -21,10 +24,15 @@ class ArticleNaturesDataTable extends DataTable public function modifier($datatables) { $datatables + ->editColumn('icon', function (ArticleNature $nature) { + $logo = Medias::getImage($nature, 'thumb'); + + return $logo ? "" : ''; + }) ->editColumn('product_type', function (ArticleNature $nature) { return ArticleNatures::getProductTypeName($nature->product_type); }) - ->rawColumns(['action']); + ->rawColumns(['icon', 'action']); return parent::modifier($datatables); } @@ -32,6 +40,7 @@ class ArticleNaturesDataTable extends DataTable protected function getColumns() { return [ + Column::make('icon')->title('Icone')->width(40)->orderable(false), Column::make('product_type')->title('Famille de produit')->width(140), Column::make('name')->title('Nom'), Column::make('articles_count')->title('Nb Art.')->addClass('text-right')->searchable(false)->width(60), diff --git a/app/Http/Controllers/Admin/Shop/ArticleNatureController.php b/app/Http/Controllers/Admin/Shop/ArticleNatureController.php index 59653d3c..f00844dd 100644 --- a/app/Http/Controllers/Admin/Shop/ArticleNatureController.php +++ b/app/Http/Controllers/Admin/Shop/ArticleNatureController.php @@ -20,24 +20,29 @@ class ArticleNatureController extends Controller public function store(Request $request) { - $ret = ArticleNatures::store($request->all()); + $articleNature = ArticleNatures::store($request->all()); + ArticleNatures::storeIcon($articleNature, $request->file('icon')); return redirect()->route('Admin.Shop.ArticleNatures.index'); } public function show($id) { - $data['article_nature'] = ArticleNatures::get($id); + $data = [ + 'article_nature' => ArticleNatures::get($id), + ]; return view('Admin.Shop.ArticleNatures.view', $data); } public function edit($id) { - $data['article_nature'] = ArticleNatures::get($id); - $data['product_types'] = ArticleNatures::getProductTypes(); - // dump($data); - // exit; + $data = [ + 'article_nature' => ArticleNatures::get($id), + 'product_types' => ArticleNatures::getProductTypes(), + ]; + $data['article_nature']['icon'] = ArticleNatures::getIcon($id); + return view('Admin.Shop.ArticleNatures.edit', $data); } diff --git a/app/Http/Controllers/Shop/CategoryController.php b/app/Http/Controllers/Shop/CategoryController.php index e3e76d69..47e72a53 100644 --- a/app/Http/Controllers/Shop/CategoryController.php +++ b/app/Http/Controllers/Shop/CategoryController.php @@ -33,42 +33,21 @@ class CategoryController extends Controller ]); if (count($articleNatures) === 1) { $articleNature = $articleNatures[0]; + } else { + $articleNature = 'semences'; } $productTypes = Articles::getProductTypesWithOffers([ 'category_id' => $categoryId, ]); if (count($productTypes) === 1) { $productType = $productTypes[0]; - } + } + } else { + $productType = ArticleNatures::getProductTypeBySlug($articleNature); } - - switch ($articleNature) { - case 'semences': - $productType = 'botanic'; - $articleNatureId = 1; - break; - case 'plants': - $productType = 'botanic'; - $articleNatureId = 2; - break; - case 'legumes': - $productType = 'botanic'; - $articleNatureId = 3; - break; - default: - if (!($productType ?? false)) { - $productType = 'botanic'; - $articleNatureId = 1; - $articleNature = 'semences'; - } - break; - } - // $productType = ArticleNatures::getProductType($articleNatureId); - // dump($productType); - // dump($articleNatureId); - // exit; + + $articleNatureId = ArticleNatures::getIdBySlug($articleNature); } - // exit; $data = [ 'category' => Categories::getFull($categoryId), 'breadcrumb' => Categories::getAncestorsByCategory($categoryId), @@ -86,8 +65,6 @@ class CategoryController extends Controller ]), 'tags' => TagGroups::getWithTagsAndCountOffers($categoryId), ]; - // dump($data); - // exit; return view('Shop.Shelves.shelve', $data); } diff --git a/app/Models/Shop/Article.php b/app/Models/Shop/Article.php index e5c698ce..4b467330 100644 --- a/app/Models/Shop/Article.php +++ b/app/Models/Shop/Article.php @@ -8,11 +8,17 @@ use App\Traits\Model\HasComments; use App\Traits\Model\Imageable; use Fico7489\Laravel\EloquentJoin\Traits\EloquentJoin; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Database\Eloquent\Relations\HasManyThrough; +use Illuminate\Database\Eloquent\Relations\MorphTo; +use Illuminate\Database\Eloquent\Relations\MorphToMany; use Illuminate\Database\Eloquent\SoftDeletes; use Kirschbaum\PowerJoins\PowerJoins; use Rinvex\Categories\Traits\Categorizable; use Rinvex\Tags\Traits\Taggable; use Spatie\MediaLibrary\HasMedia; +use Staudenmeir\EloquentHasManyDeep\HasManyDeep; use Staudenmeir\EloquentHasManyDeep\HasRelationships; use \Venturecraft\Revisionable\RevisionableTrait; use Wildside\Userstamps\Userstamps; @@ -47,22 +53,22 @@ class Article extends Model implements HasMedia 'homepage', ]; - public function article_nature() + public function article_nature(): BelongsTo { return $this->belongsTo(ArticleNature::class); } - public function invoiceItems() + public function invoiceItems(): HasMany { return $this->hasMany(InvoiceItem::class); } - public function offers() + public function offers(): HasMany { return $this->hasMany(Offer::class); } - public function price_lists() + public function price_lists(): HasManyDeep { return $this->hasManyDeep( PriceList::class, @@ -72,7 +78,7 @@ class Article extends Model implements HasMedia ); } - public function prices() + public function prices(): HasManyDeep { return $this->hasManyDeep( PriceListValue::class, @@ -82,22 +88,22 @@ class Article extends Model implements HasMedia ); } - public function product() + public function product(): MorphTo { return $this->morphTo(); } - public function siblings() + public function siblings(): HasMany { return $this->hasMany(Article::class, 'name', 'name'); } - public function tags() + public function tags(): MorphToMany { return $this->morphToMany(Tag::class, 'taggable'); } - public function tariffs() + public function tariffs(): HasManyThrough { return $this->hasManyThrough(Tariff::class, Offer::class, 'article_id', 'id', 'id', 'tariff_id'); // return $this->belongsToMany(Tariff::class, Offer::$table, 'id', 'id', 'tariff_id', 'tariff_id'); diff --git a/app/Models/Shop/ArticleNature.php b/app/Models/Shop/ArticleNature.php index 51f0e68e..a7a3baae 100644 --- a/app/Models/Shop/ArticleNature.php +++ b/app/Models/Shop/ArticleNature.php @@ -3,14 +3,19 @@ namespace App\Models\Shop; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\SoftDeletes; +use Spatie\Image\Manipulations; +use Spatie\MediaLibrary\HasMedia; +use Spatie\MediaLibrary\InteractsWithMedia; +use Spatie\MediaLibrary\MediaCollections\Models\Media; use Staudenmeir\EloquentHasManyDeep\HasRelationships; -use \Venturecraft\Revisionable\RevisionableTrait; +use Venturecraft\Revisionable\RevisionableTrait; use Wildside\Userstamps\Userstamps; -class ArticleNature extends Model +class ArticleNature extends Model implements HasMedia { - use HasRelationships, SoftDeletes, RevisionableTrait, UserStamps; + use HasRelationships, InteractsWithMedia, SoftDeletes, RevisionableTrait, UserStamps; protected $guarded = ['id']; @@ -20,7 +25,7 @@ class ArticleNature extends Model protected $keepRevisionOf = ['product_type', 'name', 'description']; - public function articles() + public function articles(): HasMany { return $this->hasMany(Article::class); } @@ -49,4 +54,11 @@ class ArticleNature extends Model { return $query->whereIn($this->table.'.id', $ids); } + + public function registerMediaConversions(Media $media = null): void + { + $this->addMediaConversion('thumb')->fit(Manipulations::FIT_MAX, 60, 32)->keepOriginalImageFormat()->nonQueued(); + $this->addMediaConversion('normal')->fit(Manipulations::FIT_MAX, 360, 192)->keepOriginalImageFormat()->nonQueued(); + } + } diff --git a/app/Models/Shop/Basket.php b/app/Models/Shop/Basket.php index 03bd035c..004bfa10 100644 --- a/app/Models/Shop/Basket.php +++ b/app/Models/Shop/Basket.php @@ -3,6 +3,7 @@ namespace App\Models\Shop; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\BelongsTo; class Basket extends Model { @@ -10,7 +11,7 @@ class Basket extends Model protected $table = 'shop_baskets'; - public function Offer() + public function Offer(): BelongsTo { return $this->belongsTo(Offer::class); } diff --git a/app/Models/Shop/Categorizable.php b/app/Models/Shop/Categorizable.php index 4f4cc2fd..00bc2373 100644 --- a/app/Models/Shop/Categorizable.php +++ b/app/Models/Shop/Categorizable.php @@ -3,22 +3,24 @@ namespace App\Models\Shop; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Relations\MorphTo; class Categorizable extends Model { protected $table = 'categorizables'; - public function categorizable() + public function categorizable(): MorphTo { return $this->morphTo(); } - public function category() + public function category(): BelongsTo { return $this->belongsTo(Category::class); } - public function article() + public function article(): BelongsTo { return $this->belongsTo(Article::class, 'categorizable_id'); } diff --git a/app/Models/Shop/Category.php b/app/Models/Shop/Category.php index 7b6608ac..e60d8a04 100644 --- a/app/Models/Shop/Category.php +++ b/app/Models/Shop/Category.php @@ -4,6 +4,8 @@ namespace App\Models\Shop; use App\Repositories\Shop\SaleChannels; use Cesargb\Database\Support\CascadeDelete; +use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Database\Eloquent\Relations\HasOne; use Illuminate\Database\Eloquent\SoftDeletes; use Kalnoy\Nestedset\NestedSet; use Rinvex\Categories\Models\Category as parentCategory; @@ -43,12 +45,12 @@ class Category extends parentCategory return $this->tags->articles; } - public function Parent() + public function Parent(): HasOne { return $this->hasOne(Category::class, 'id', 'parent_id'); } - public function categorizables() + public function categorizables(): HasMany { return $this->hasMany(Categorizable::class); } diff --git a/app/Models/Shop/Customer.php b/app/Models/Shop/Customer.php index 17390f53..82addf10 100644 --- a/app/Models/Shop/Customer.php +++ b/app/Models/Shop/Customer.php @@ -4,6 +4,8 @@ namespace App\Models\Shop; use App\Notifications\ResetPassword; use App\Notifications\VerifyEmail; +use Illuminate\Database\Eloquent\Relations\BelongsToMany; +use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; @@ -21,27 +23,27 @@ class Customer extends Authenticatable protected $casts = ['email_verified_at' => 'datetime']; - public function addresses() + public function addresses(): HasMany { return $this->hasMany(CustomerAddress::class); } - public function customer_deliveries() + public function customer_deliveries(): HasMany { return $this->hasMany(CustomerDelivery::class); } - public function deliveries() + public function deliveries(): BelongsToMany { return $this->belongsToMany(Delivery::class, CustomerDelivery::class); } - public function invoices() + public function invoices(): HasMany { return $this->hasMany(Invoice::class); } - public function orders() + public function orders(): HasMany { return $this->hasMany(Order::class); } diff --git a/app/Repositories/Core/DateRange.php b/app/Repositories/Core/DateRange.php index 6a25c826..c5e3e954 100644 --- a/app/Repositories/Core/DateRange.php +++ b/app/Repositories/Core/DateRange.php @@ -3,9 +3,10 @@ namespace App\Repositories\Core; use Carbon\Carbon; -use function League\Period\interval_after; use League\Period\Period; +use function League\Period\interval_after; + class DateRange { public static function today() @@ -60,9 +61,10 @@ class DateRange ]; } - public static function getPeriodsLastMonthWithLabels($nb, $with_actual = true) + public static function getPeriodsLastMonthWithLabels($nb, $withActual = true) { - $periods = DateRange::PeriodsToCarbon(DateRange::getPeriodsLastMonth($nb, $with_actual)); + $data = []; + $periods = DateRange::PeriodsToCarbon(DateRange::getPeriodsLastMonth($nb, $withActual)); $labels = DateRange::getMonthNames($periods); foreach ($labels as $label) { $data[$label] = $periods; @@ -76,13 +78,12 @@ class DateRange return self::getMonthNames(self::PeriodsToCarbon(self::getPeriodsLastMonth(12))); } - public static function getPeriodsLastMonth($nb = 1, $with_actual = true) + public static function getPeriodsLastMonth($nb = 1, $withActual = true) { - $end = $with_actual ? Carbon::now()->endOfMonth() : self::lastMonth(); - $begin = ($nb == 1) ? $end->copy()->startOfMonth() : $end->copy()->startOfMonth()->subMonth($nb - 1); - $t = self::getPeriodsbyMonth($begin, $end); + $end = $withActual ? Carbon::now()->endOfMonth() : self::lastMonth(); + $begin = $nb === 1 ? $end->copy()->startOfMonth() : $end->copy()->startOfMonth()->subMonth($nb - 1); - return self::getPeriodsbyMonth($begin, $end); + return self::getPeriodsByMonth($begin, $end); } public static function getMonthNamesByPeriods($periods) @@ -106,20 +107,20 @@ class DateRange return $months; } - public static function getPeriodsLastWeek($nb = 1, $with_actual = true) + public static function getPeriodsLastWeek($nb = 1, $withActual = true) { - $end = $with_actual ? Carbon::now()->endOfWeek() : self::lastWeek(); + $end = $withActual ? Carbon::now()->endOfWeek() : self::lastWeek(); $begin = $end->copy()->subWeek($nb); - return self::getPeriodsbyWeek($begin, $end); + return static::getPeriodsByWeek($begin, $end); } - public static function getPeriodsLastDay($nb = 1, $with_actual = true) + public static function getPeriodsLastDay($nb = 1, $withActual = true) { - $end = $with_actual ? Carbon::now()->endOfDay() : static::lastDay(); + $end = $withActual ? Carbon::now()->endOfDay() : static::lastDay(); $begin = $end->copy()->subDay($nb); - return self::getPeriodsbyDay($begin, $end); + return static::getPeriodsByDay($begin, $end); } public static function byDay() @@ -156,6 +157,8 @@ class DateRange case 4: $date = Carbon::now()->subMonth(3)->startOfQuarter(); break; + default: + return false; } return [$date, $date->addMonth(6)]; @@ -181,32 +184,38 @@ class DateRange return Carbon::now()->subDay()->startOfDay(); } - public static function getPeriodsbyMonth($begin, $end, $interval = 1) + public static function getPeriodsByMonth($begin, $end, $interval = 1) { - return self::getPeriods($begin, $end, "$interval MONTH"); + return self::getPeriods($begin, $end, "{$interval} MONTH"); } - public static function getPeriodsbyWeek($begin, $end, $interval = 1) + public static function getPeriodsByWeek($begin, $end, $interval = 1) { - return self::getPeriods($begin, $end, "$interval WEEK"); + return self::getPeriods($begin, $end, "{$interval} WEEK"); } - public static function getPeriodsbyDay($begin, $end, $interval = 1) + public static function getPeriodsByDay($begin, $end, $interval = 1) { - return self::getPeriods($begin, $end, "$interval DAY"); + return self::getPeriods($begin, $end, "{$interval} DAY"); } - public static function getPeriods($begin, $end, $duration, $interval = 1) + public static function getPeriods($begin, $end, $duration) { - $period = new Period($begin, $end, $interval); + $range = []; + $period = new Period($begin, $end); foreach ($period->getDatePeriod($duration) as $day) { - $daterange[] = interval_after($day, $duration); + $range[] = interval_after($day, $duration); } - return $daterange; + /* + foreach ($period->dateRangeForward($duration) as $day) { + $daterange[] = interval_after($day, $duration); + } + */ + return $range; } - public static function PeriodsToCarbon($periods) + public static function periodsToCarbon($periods) { $data = []; foreach ($periods as $period) { @@ -216,13 +225,39 @@ class DateRange return $data; } - public static function PeriodToCarbon($period) + public static function periodToCarbon($period) { - return ['start' => self::DatePointToCarbon($period->getStartDate()), 'end' => self::DatePointToCarbon($period->getEndDate())]; + return [ + 'start' => self::DatePointToCarbon($period->getStartDate()), + 'end' => self::DatePointToCarbon($period->getEndDate()), + ]; } - public static function DatePointToCarbon($date) + public static function datePointToCarbon($date) { return Carbon::createFromFormat('Y-m-d H:i:s', $date->format('Y-m-d H:i:s')); } + + public static function convertPeriod($txt) + { + return array_values(self::convertDaterange($txt)); + } + + public static function convertDaterange($txt) + { + $dates = explode('-', $txt); + + return [ + 'start' => DateTime::convert(trim($dates[0])), + 'end' => DateTime::convert(trim($dates[1])), + ]; + } + + public static function getPeriodByLocale($start, $end) + { + return [ + 'start' => DateTime::convert($start), + 'end' => DateTime::convert($end), + ]; + } } diff --git a/app/Repositories/Core/DateTime.php b/app/Repositories/Core/DateTime.php index a6367f63..a6121f09 100644 --- a/app/Repositories/Core/DateTime.php +++ b/app/Repositories/Core/DateTime.php @@ -2,9 +2,9 @@ namespace App\Repositories\Core; -use Carbon\Carbon; +use App; +use Illuminate\Support\Carbon; use Illuminate\Support\Str; -use Jenssegers\Date\Date; class DateTime { @@ -28,60 +28,90 @@ class DateTime return $short ? $date->shortDayName : $date->dayName; } - public static function DatetoLocale($date = null) + public static function getDateTime() + { + return self::datetimeToLocale(date('Y-m-d H:i:s')); + } + + public static function getDate() + { + return self::dateToLocale(date('Y-m-d')); + } + + public static function getLang() + { + return App::currentLocale(); + // return session('locale') ? session('locale') : 'fr'; + } + + public static function datetoLocale($date = null) { $format = self::getLocaleFormatDate(); if (! is_null($date) && ! empty($date)) { $date = Carbon::parse($date)->format($format); - } elseif ($date == 'now') { + } elseif ($date === 'now') { $date = today()->format($format); } return $date; } - public static function DatetimeToLocale($date = null) + public static function datetimeToLocale($date = null) { $format = self::getLocaleFormatDatetime(); if (! is_null($date) && ! empty($date)) { $date = Carbon::parse($date)->format($format); - } elseif ($date == 'now') { + } elseif ($date === 'now') { $date = now()->format($format); } return $date; } - public static function getDateTime() + public static function countDaysFrom($date) { - return self::DatetimeToLocale(date('Y-m-d H:i:s')); + $begin = DateTime::getCarbonTime($date); + + return $begin->diffInDays(now()) + 1; } - public static function getDate() + public static function getCarbonTime($date) { - return self::DateToLocale(date('Y-m-d')); + $format = self::getLocaleFormatDatetime(); + if (strlen($date) === 16) { + $date .= ':00'; + } + + return ! empty($date) ? Carbon::createFromFormat($format, $date) : null; } - public static function getLang() + public static function getCarbonDate($date) { - return session('locale') ? session('locale') : 'fr'; + $format = self::getLocaleFormatDate(); + + return ! empty($date) ? Carbon::createFromFormat($format, $date) : null; } public static function convert($date) { - $format = self::getLocaleFormatDate(); - - return ! empty($date) ? Carbon::createFromFormat($format, $date)->isoFormat('Y-MM-DD') : null; + return ! empty($date) ? self::getCarbonDate($date)->isoFormat('Y-MM-DD') : null; } public static function convertTime($date) { - $format = self::getLocaleFormatDatetime(); - if (strlen($date) == 16) { - $date .= ':00'; - } + return ! empty($date) ? self::getCarbonTime($date)->isoFormat('Y-MM-DD HH:mm:ss') : null; + } - return ! empty($date) ? Carbon::createFromFormat($format, $date)->isoFormat('Y-MM-DD HH:mm:ss') : null; + public static function toLocale($date) + { + $format = self::getLocaleFormatDate(); + + return ! empty($date) ? Carbon::parse($date)->Format($format) : null; + } + + public static function toISO($date) + { + return ! empty($date) ? Carbon::parse($date)->isoFormat('Y-MM-DD') : null; } public static function toFr($date) @@ -97,7 +127,7 @@ class DateTime public static function getYearFromDate($date) { // return date_format(DateTime::convert($signature_date), 'Y'); - $date = DateTime::convert($date); + $date = self::convert($date); $date = date_create($date); return date_format($date, 'Y'); @@ -118,6 +148,20 @@ class DateTime return $format; } + public static function checkDateFormat($date) + { + if (preg_match("/^\d{2}\/\d{2}\/\d{4}$/", $date)) { + $day = substr($date, 0, 2); + $month = substr($date, 3, 2); + $year = substr($date, 6, 4); + if (checkdate($month, $day, $year)) { + return true; + } + } + + return false; + } + public static function getLocaleFormatDatetime() { $locale = self::getLang(); @@ -165,7 +209,8 @@ class DateTime return Carbon::parse($date)->isoFormat($format); } - public static function relativeTime() + public static function convertTimeToDate($date) { + return substr($date, 0, 10); } } diff --git a/app/Repositories/Core/File.php b/app/Repositories/Core/File.php index 19dbab60..b3c8df04 100644 --- a/app/Repositories/Core/File.php +++ b/app/Repositories/Core/File.php @@ -8,13 +8,66 @@ use SoftCreatR\MimeDetector\MimeDetectorException; class File { + public static function copy($source, $target) + { + $source = self::sanitize($source); + $target = self::sanitize($target); + if (! self::checkFile($source)) { + return false; + } + $dirname = self::relative(dirname($target)); + self::checkDirOrCreate($dirname); + + return copy($source, $target); + } + public static function checkDirOrCreate($dir) { return self::checkDir($dir) ? true : self::createDir($dir); } + public static function sanitize($filename) + { + return str_replace('\\', '/', $filename); + } + + public static function relative($filename) + { + return str_replace(self::getStorageAppPath(), '', $filename); + } + + public static function getStorageAppPath() + { + return self::sanitize(storage_path()).'/app'; + } + + public static function getTree($path) + { + $tree = []; + + $branch = [ + 'label' => basename($path), + ]; + + foreach (\File::files($path) as $file) { + $branch['children'][] = basename($file); + } + + foreach (\File::directories($path) as $directory) { + $branch['children'][] = self::getTree($directory); + } + + return array_merge($tree, $branch); + } + + public static function list($dir, $mask = '/*') + { + return glob($dir); + } + public static function checkDir($dir) { + // return File::isDirectory($dir) return is_dir($dir); } @@ -25,19 +78,17 @@ class File public static function createDir($dir) { - return mkdir($dir, '0777', true); + return Storage::makeDirectory($dir); } public static function deleteDir($dir) { - Storage::deleteDirectory($dir); - - return true; + return Storage::deleteDirectory($dir); } public static function createFile($file, $content) { - Storage::put($file, $content); + return Storage::put($file, $content); } public static function getFile($file) @@ -68,4 +119,27 @@ class File exit('An error occured while trying to load the given file.'); } } + + public static function replaceInFile($path, $string, $replace) + { + set_time_limit(0); + $temp = false; + + if (is_file($path) === true) { + $file = fopen($path, 'r'); + $temp = tempnam('./', 'tmp'); + + if (is_resource($file) === true) { + while (feof($file) === false) { + file_put_contents($temp, str_replace($string, $replace, fgets($file)), FILE_APPEND); + } + + fclose($file); + } + + unlink($path); + } + + return $temp ? rename($temp, $path) : false; + } } diff --git a/app/Repositories/Core/HelperDate.php b/app/Repositories/Core/HelperDate.php index 7b216679..4a57764f 100644 --- a/app/Repositories/Core/HelperDate.php +++ b/app/Repositories/Core/HelperDate.php @@ -6,8 +6,6 @@ use Carbon\Carbon; class HelperDate { - public static $is_debug = true; - public static function toLocaleFormat($date) { if (! (! is_null($date) && ! empty($date))) { @@ -24,9 +22,8 @@ class HelperDate default: $format = 'Y-m-d'; } - $date = Carbon::parse($date)->format($format); - return $date; + return Carbon::parse($date)->format($format); } public static function fromLocale($d) @@ -61,7 +58,7 @@ class HelperDate public static function toFrenchDate($d) { - if ($d && $d != '0000-00-00') { + if ($d && $d !== '0000-00-00') { return Carbon::createFromFormat('Y-m-d', $d)->format('d/m/Y'); } diff --git a/app/Repositories/Core/MediaPathGenerator.php b/app/Repositories/Core/MediaPathGenerator.php new file mode 100644 index 00000000..8ce003ea --- /dev/null +++ b/app/Repositories/Core/MediaPathGenerator.php @@ -0,0 +1,31 @@ +getBasePath($media).'/'; + } + + public function getPathForConversions(Media $media): string + { + return $this->getBasePath($media).'/conversions/'; + } + + public function getPathForResponsiveImages(Media $media): string + { + return $this->getBasePath($media).'/responsive-images/'; + } + + protected function getBasePath(Media $media): string + { + $model = basename(str_replace('\\', '/', $media->model_type)); + + return basename($model).'/images/'.$media->getKey(); + } +} diff --git a/app/Repositories/Core/Storage.php b/app/Repositories/Core/Storage.php index 5fff1325..cd4150f8 100644 --- a/app/Repositories/Core/Storage.php +++ b/app/Repositories/Core/Storage.php @@ -18,6 +18,16 @@ class Storage return Storage2::disk('local')->has($dir); } + public static function move($source, $target) + { + return Storage2::move($source, $target); + } + + public static function copy($source, $target) + { + return Storage2::copy($source, $target); + } + public static function checkFile($file) { return Storage2::exists($file); diff --git a/app/Repositories/Shop/ArticleNatures.php b/app/Repositories/Shop/ArticleNatures.php index 43667906..b3d60065 100644 --- a/app/Repositories/Shop/ArticleNatures.php +++ b/app/Repositories/Shop/ArticleNatures.php @@ -7,12 +7,71 @@ use App\Models\Botanic\Variety; use App\Models\Shop\Article; use App\Models\Shop\ArticleNature; use App\Models\Shop\Merchandise; +use App\Repositories\Core\Medias; use App\Traits\Model\Basic; class ArticleNatures { use Basic; + public static function getIconBySlug($slug, $conversion = 'normal', $collection = 'images') + { + return self::getIcon(self::getIdBySlug($slug), $conversion, $collection); + } + + public static function getIcon($id, $conversion = 'normal', $collection = 'images') + { + return Medias::getImage(self::get($id), $conversion, $collection); + } + + public static function getIdBySlug($slug) + { + switch ($slug) { + case 'semences': + $id = 1; + break; + case 'plants': + $id = 2; + break; + case 'legumes': + $id = 3; + break; + case 'marchandises': + $id = 4; + break; + default: + $id = 1; + break; + } + + return $id; + } + + public static function getProductTypeBySlug($slug) + { + switch ($slug) { + case 'semences': + case 'plants': + case 'legumes': + $productType = 'botanic'; + break; + case 'marchandises': + $productType = 'merchandise'; + break; + default: + $productType = 'botanic'; + break; + } + return $productType; + } + + public static function storeIcon($nature, $file, $collection = 'images') + { + Medias::deleteImages($nature, $collection); + + return Medias::storeImage($nature, $file, $collection); + } + public static function getProductType($id) { $type = self::get($id)->product_type ?? false; diff --git a/resources/views/Admin/Shop/ArticleNatures/edit.blade.php b/resources/views/Admin/Shop/ArticleNatures/edit.blade.php index 03ea681f..9ed0066d 100644 --- a/resources/views/Admin/Shop/ArticleNatures/edit.blade.php +++ b/resources/views/Admin/Shop/ArticleNatures/edit.blade.php @@ -4,11 +4,14 @@ 'breadcrumb' => [__('shop.article_natures.title'), __('shop.article_natures.edit')] ]) -@include('boilerplate::load.fileinput') - @section('content') - {{ Form::open(['route' => 'Admin.Shop.ArticleNatures.store', 'id' => 'article_nature-form', 'autocomplete' => 'off', 'files' => true]) }} + {{ Form::open([ + 'route' => 'Admin.Shop.ArticleNatures.store', + 'id' => 'article_nature-form', + 'autocomplete' => 'off', + 'files' => true, + ]) }} @include('Admin.Shop.ArticleNatures.form') diff --git a/resources/views/Admin/Shop/ArticleNatures/form.blade.php b/resources/views/Admin/Shop/ArticleNatures/form.blade.php index 28219bfc..76d79c3a 100644 --- a/resources/views/Admin/Shop/ArticleNatures/form.blade.php +++ b/resources/views/Admin/Shop/ArticleNatures/form.blade.php @@ -1,41 +1,59 @@ -
-
- {{ Form::label('product_type', 'Famille de produit') }} - @include('components.form.select', [ - 'name' => 'product_type', - 'value' => $article_nature['product_type'] ?? null, - 'list' => $product_types ?? null, - 'required' => true, - ]) -
-
-
-
+
+
-
+
+ {{ Form::label('product_type', 'Famille de produit') }} + @include('components.form.select', [ + 'name' => 'product_type', + 'value' => $article_nature['product_type'] ?? null, + 'list' => $product_types ?? null, + 'required' => true, + ]) +
+
{{ Form::label('name', 'Nom') }} - @include('components.form.input', ['name' => 'name', 'value' => $article_nature['name'] ?? null, 'required' => true]) + @include('components.form.input', [ + 'name' => 'name', + 'value' => $article_nature['name'] ?? null, + 'required' => true, + ])
-
{{ Form::label('description', 'Description') }} - @include('components.form.textarea', ['name' => 'description', 'value' => $article_nature['description'] ?? null, 'class' => 'editor', 'required' => false]) + @include('components.form.textarea', [ + 'name' => 'description', + 'value' => $article_nature['description'] ?? null, + 'class' => 'editor', + 'required' => false, + ])
+ +
+ + @include('components.widgets.imgUpload', [ + 'name' => 'icon', + 'id_name' => 'icon', + 'file' => $article_nature['icon'] ?? false, + ]) + +
-@include('components.save') + @include('load.form.editor') +@include('load.form.upload.fileinput') @include('load.form.save') @push('js') diff --git a/resources/views/Shop/Customers/profile.blade.php b/resources/views/Shop/Customers/profile.blade.php index d319caa2..2bee448a 100644 --- a/resources/views/Shop/Customers/profile.blade.php +++ b/resources/views/Shop/Customers/profile.blade.php @@ -4,10 +4,10 @@ @section('content')
-
+
@include('Shop.Customers.partials.user')
-
+
@include('Shop.Customers.partials.sale')
diff --git a/resources/views/Shop/Shelves/shelve.blade.php b/resources/views/Shop/Shelves/shelve.blade.php index 64ec872d..fccfc966 100644 --- a/resources/views/Shop/Shelves/shelve.blade.php +++ b/resources/views/Shop/Shelves/shelve.blade.php @@ -26,7 +26,7 @@
- @include('Shop.Shelves.partials.category_add') + @include('Shop.Shelves.partials.category_add')
diff --git a/resources/views/components/widgets/imgUpload.blade.php b/resources/views/components/widgets/imgUpload.blade.php new file mode 100644 index 00000000..f9c8cb91 --- /dev/null +++ b/resources/views/components/widgets/imgUpload.blade.php @@ -0,0 +1,21 @@ +
+
+
+ + @if (!($disabled ?? false)) +
+ +
+ @endif +
+ + @if (!($disabled ?? false)) +
+ @include('components.form.file', [ + 'name' => $name, + 'value' => $file ?? null, + ]) +
+ @endif +
+
diff --git a/resources/views/load/form/upload/fileinput.blade.php b/resources/views/load/form/upload/fileinput.blade.php index 159325f5..585cc8b3 100644 --- a/resources/views/load/form/upload/fileinput.blade.php +++ b/resources/views/load/form/upload/fileinput.blade.php @@ -1,25 +1,144 @@ @if(!defined('LOAD_FILEINPUT')) @push('css') - + @endpush - @push('js') - + @push('scripts') + @if(App::getLocale() !== 'en') - + + + @endif + @component('boilerplate::minify') - @endif - @endpush + @endcomponent + @endpush @php(define('LOAD_FILEINPUT', true)) -@endif \ No newline at end of file +@endif