119 lines
2.6 KiB
PHP
119 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Repositories\Botanic;
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
|
|
use App\Repositories\Core\Tag;
|
|
use App\Models\Botanic\Specie;
|
|
use App\Exports\Botanic\SpeciesExport;
|
|
|
|
class Species
|
|
{
|
|
|
|
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)
|
|
{
|
|
return self::get($id)->tags->toArray();
|
|
}
|
|
|
|
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.group')->findOrFail($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)
|
|
{
|
|
$id = $data['id'] ?? false;
|
|
$specie = $id ? self::update($data, $id) : self::create($data);
|
|
return $specie;
|
|
}
|
|
|
|
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 storeImages($specie, $files)
|
|
{
|
|
return Media::storeImages($specie, $files);
|
|
}
|
|
|
|
public static function getImages($id)
|
|
{
|
|
return Media::getImages(self::get($id));
|
|
}
|
|
|
|
public static function deleteImage($id, $index)
|
|
{
|
|
return Media::deleteImage(self::get($id), $index);
|
|
}
|
|
|
|
public static function exportExcel()
|
|
{
|
|
return Excel::download(new SpeciesExport, 'species.xlsx');
|
|
}
|
|
}
|