Files
opensem/app/Repositories/Botanic/Species.php
Ludovic CANDELLIER 0879b0abf0 add shipping rules
2023-07-16 14:45:42 +02:00

103 lines
2.2 KiB
PHP

<?php
namespace App\Repositories\Botanic;
use App\Exports\Botanic\SpeciesExport;
use App\Models\Botanic\Specie;
use App\Repositories\Core\Tag;
use App\Traits\Repository\Imageable;
use Maatwebsite\Excel\Facades\Excel;
class Species
{
use Imageable;
public static function getOptions()
{
return Specie::get()->SortBy('name')->pluck('name', 'id')->toArray();
}
public static function getAll()
{
return Specie::orderBy('name', 'asc')->get();
}
public static function getDescription($id)
{
return self::get($id)->description ?? '';
}
public static function getTags($id)
{
$model = self::get($id) ?? false;
return $model ? $model->tags->toArray() : false;
}
public static function getFull($id)
{
$specie = self::get($id);
$data = $specie->toArray();
$data['tags'] = self::getTagsBySpecie($specie);
return $data;
}
public static function getTagsBySpecie($specie)
{
return Tag::getTagsByModel($specie);
}
public static function get($id)
{
return Specie::with('tags.tag_group')->find($id);
}
public static function storeFull($data)
{
$images = $data['images'] ?? false;
$tags = $data['tags'] ?? false;
unset($data['images']);
unset($data['tags']);
$specie = self::store($data);
self::storeImages($specie, $images);
self::storeTags($specie, $tags);
return $specie;
}
public static function store($data)
{
return ($data['id'] ?? false) ? self::update($data) : self::create($data);
}
public static function create($data)
{
return Specie::create($data);
}
public static function update($data, $id = false)
{
$id = $id ? $id : $data['id'];
$model = self::get($id);
$ret = $model->update($data);
return $model;
}
public static function destroy($id)
{
return Specie::destroy($id);
}
public static function storeTags($specie, $tags)
{
return Tag::storeTags($specie, $tags);
}
public static function exportExcel()
{
return Excel::download(new SpeciesExport, 'species.xlsx');
}
}