mivita/app/Models/DcFile.php
2025-04-01 10:36:47 +02:00

206 lines
5.6 KiB
PHP

<?php
/**
* Created by Reliese Model.
*/
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
/**
* Class DcFile
*
* @property int $id
* @property string $filename
* @property string $original_name
* @property string $ext
* @property string $mine
* @property int|null $size
* @property int|null $active
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
* @property Collection|DcFileTag[] $dc_file_tags
* @package App\Models
* @property-read int|null $dc_file_tags_count
* @property-read Collection<int, \App\Models\DcFileTag> $fileTag
* @property-read int|null $file_tag_count
* @property-read Collection<int, \App\Models\DcTag> $tags
* @property-read int|null $tags_count
* @method static \Illuminate\Database\Eloquent\Builder|DcFile newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|DcFile newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|DcFile query()
* @method static \Illuminate\Database\Eloquent\Builder|DcFile whereActive($value)
* @method static \Illuminate\Database\Eloquent\Builder|DcFile whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|DcFile whereExt($value)
* @method static \Illuminate\Database\Eloquent\Builder|DcFile whereFilename($value)
* @method static \Illuminate\Database\Eloquent\Builder|DcFile whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|DcFile whereMine($value)
* @method static \Illuminate\Database\Eloquent\Builder|DcFile whereOriginalName($value)
* @method static \Illuminate\Database\Eloquent\Builder|DcFile whereSize($value)
* @method static \Illuminate\Database\Eloquent\Builder|DcFile whereUpdatedAt($value)
* @mixin \Eloquent
*/
class DcFile extends Model
{
protected $table = 'dc_files';
protected $casts = [
'size' => 'int',
'active' => 'int'
];
protected $fillable = [
'filename',
'original_name',
'ext',
'mine',
'size',
'active'
];
public function dc_file_tags()
{
return $this->hasMany(DcFileTag::class, 'file_id');
}
public function tags(){
return $this->belongsToMany(DcTag::class, 'dc_file_tags', 'file_id', 'tag_id');
}
public function fileTag(){
return $this->hasMany(DcFileTag::class, 'file_id');
}
public function hasTags()
{
if($this->fileTag()->count()){
return true;
}
return false;
}
//path /storage/app/dc/ ...
/*public function isImage(){
if(empty($this->attributes['filename']) || @$this->attributes['filename'] == null || @$this->attributes['filename'] == ""){
return false;
}
if(!\Storage::disk('public')->has('images/shop/'.$this->filename)){
return false;
}
return true;
}
public function getImage(){
if($this->isImage()){
$link = 'images/shop/'.$this->filename;
return '/storage/'.$link.'?=lm='.\Storage::disk('public')->lastModified($link);
}
return false;
}*/
public function hasThumb(){
if(\Storage::disk('public')->exists('dc/thumb/'.$this->filename) || \Storage::disk('public')->exists('dc/thumb/'.$this->filename.".jpg")){
return true;
}
return false;
}
public function hasBig(){
if(\Storage::disk('public')->exists('dc/big/'.$this->filename) || \Storage::disk('public')->exists('dc/big/'.$this->filename.".jpg")){
return true;
}
return false;
}
public function hasFile(){
if(\Storage::disk('public')->exists('dc/files/'.$this->filename) || \Storage::disk('public')->exists('dc/files/'.$this->filename.".jpg")){
return true;
}
return false;
}
public function getThumb($full = false){
$link = 'dc/thumb/'.$this->filename;
if(\Storage::disk('public')->exists($link)){
return $full ? \Storage::disk('public')->path($link) : $link;
}
$link.=".jpg";
if(\Storage::disk('public')->exists($link)){
return $full ? \Storage::disk('public')->path($link) : $link;
}
return false;
}
public function getBig($full = false){
$link = 'dc/big/'.$this->filename;
if(\Storage::disk('public')->exists($link)){
return $full ? \Storage::disk('public')->path($link) : $link;
}
$link.=".jpg";
if(\Storage::disk('public')->exists($link)){
return $full ? \Storage::disk('public')->path($link) : $link;
}
return false;
}
public function getFile($full = false){
$link = 'dc/files/'.$this->filename;
if(\Storage::disk('public')->exists($link)){
return $full ? \Storage::disk('public')->path($link) : $link;
}
$link.=".jpg";
if(\Storage::disk('public')->exists($link)){
return $full ? \Storage::disk('public')->path($link) : $link;
}
return false;
}
public function getLastModified($type = 'thumb'){
if($type == 'thumb'){
$link = $this->getThumb();
}
if($type == 'big'){
$link = $this->getBig();
}
if($type == 'file'){
$link = $this->getFile();
}
return \Storage::disk('public')->lastModified($link);
}
public function getTags(){
$tags = $this->tags;
$tagArray = [];
foreach($tags as $tag){
$tagArray[] = $tag->name;
}
return implode(', ', $tagArray);
}
public function thumb(){
$ext = $this->ext;
$ext = strtolower($ext);
if($ext == 'pdf'){
return 'pdf.png';
}
if($ext == 'doc' || $ext == 'docx'){
return 'doc.png';
}
if($ext == 'xls' || $ext == 'xlsx'){
return 'xls.png';
}
if($ext == 'ppt' || $ext == 'pptx'){
return 'ppt.png';
}
if($ext == 'zip' || $ext == 'rar'){
return 'zip.png';
}
if($ext == 'jpg' || $ext == 'jpeg' || $ext == 'png' || $ext == 'gif'){
return $this->filename;
}
return 'file.png';
}
}