photo/app/Models/Abstracted/Catalog.php

614 lines
14 KiB
PHP
Raw Normal View History

2018-01-11 12:59:53 +00:00
<?php
2019-11-08 12:51:47 +00:00
namespace App\Models\Abstracted;
2018-01-11 12:59:53 +00:00
2019-12-15 12:34:42 +00:00
use Carbon\Carbon;
2018-01-11 12:59:53 +00:00
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
2018-01-11 12:59:53 +00:00
use Illuminate\Support\Facades\Schema;
2019-12-26 04:56:31 +00:00
use Illuminate\Support\Facades\DB;
2018-01-11 12:59:53 +00:00
2019-12-26 04:56:31 +00:00
use App\Models\{Person,Software,Tag};
2019-11-08 12:51:47 +00:00
2018-01-11 12:59:53 +00:00
abstract class Catalog extends Model
{
2019-12-14 11:29:54 +00:00
protected static $includeSubSecTime = FALSE;
2020-01-05 10:55:26 +00:00
protected $dates = ['created','created_manual'];
2019-11-23 01:51:30 +00:00
2020-01-04 22:37:50 +00:00
protected $casts = [
'subsectime' => 'int',
];
/**
* People in Multimedia Object
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
2019-11-23 01:51:30 +00:00
public function people()
2018-01-11 12:59:53 +00:00
{
2019-11-08 12:51:47 +00:00
return $this->belongsToMany(Person::class);
2018-01-11 12:59:53 +00:00
}
/**
* Software used to create Multimedia Object
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2019-12-26 04:56:31 +00:00
public function software()
{
return $this->belongsTo(Software::class);
}
/**
* Tags added to Multimedia Object
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
2019-11-23 01:51:30 +00:00
public function tags()
2018-01-11 12:59:53 +00:00
{
2019-11-08 12:51:47 +00:00
return $this->belongsToMany(Tag::class);
2018-01-11 12:59:53 +00:00
}
/**
* Find records marked as duplicate
*
* @param $query
* @return mixed
*/
2019-12-14 11:29:54 +00:00
public function scopeDuplicates($query) {
$query->notRemove()
->where('duplicate',TRUE)
->where(function($q) {
$q->Where('ignore_duplicate','<>',TRUE)
->orWhereNull('ignore_duplicate');
});
}
/**
* Search Database for duplicates of this object
*
* @param $query
* @return mixed
*/
public function scopeMyDuplicates($query) {
2019-12-14 11:29:54 +00:00
if (! $this->exists)
return $query;
// Exclude this record
$query->where('id','<>',$this->attributes['id']);
// Skip ignore dups
$query->where(function($q) {
$q->whereNull('ignore_duplicate')
->orWhere('ignore_duplicate','=',0);
});
2019-12-14 11:29:54 +00:00
// Exclude those marked as remove
$query->where(function ($q) {
$q->where('remove','<>',TRUE)
->orWhere('remove','=',NULL);
});
$query->where(function ($q) {
$q->where('signature','=',$this->attributes['signature'])
->orWhere('file_signature','=',$this->attributes['file_signature'])
// Where the signature is the same
->orWhere(function($q) {
// Or they have the same time taken with the same camera
if ($this->attributes['created'] AND $this->software_id) {
$q->where(function ($q) {
$q->where('created','=',$this->attributes['created'])
->orWhere('created_manual','=',$this->attributes['created']);
});
if (static::$includeSubSecTime)
$q->where('subsectime','=',Arr::get($this->attributes,'subsectime'));
$q->where('software_id','=',$this->attributes['software_id']);
} elseif ($this->attributes['created_manual'] AND $this->software_id) {
$q->where(function ($q) {
$q->where('created','=',$this->attributes['created_manual'])
->orWhere('created_manual','=',$this->attributes['created_manual']);
});
2019-12-14 11:29:54 +00:00
if (static::$includeSubSecTime)
$q->where('subsectime','=',Arr::get($this->attributes,'subsectime'));
2019-12-14 11:29:54 +00:00
$q->where('software_id','=',$this->attributes['software_id']);
}
});
});
}
2018-01-11 12:59:53 +00:00
/**
* Multimedia NOT duplicate.
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeNotDuplicate($query)
{
return $query->where(function($query)
{
2019-11-23 01:51:30 +00:00
$query->where('duplicate','<>',TRUE)
2020-01-06 09:01:04 +00:00
->orWhere('duplicate','=',NULL)
->orWhere(function($q) {
$q->where('duplicate','=',TRUE)
->where('ignore_duplicate','=',TRUE);
});
2018-01-11 12:59:53 +00:00
});
}
/**
* Multimedia NOT pending removal.
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeNotRemove($query)
{
return $query->where(function($query)
{
2019-11-23 01:51:30 +00:00
$query->where('remove','<>',TRUE)
2018-01-11 12:59:53 +00:00
->orWhere('remove','=',NULL);
});
}
/**
* Multimedia NOT scanned.
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeNotScanned($query)
{
return $query->where(function($query)
{
2019-11-23 01:51:30 +00:00
$query->where('scanned','<>',TRUE)
2018-01-11 12:59:53 +00:00
->orWhere('scanned','=',NULL);
});
}
// Children objects must inherit this methods
abstract public function setLocation();
abstract public function setSubSecTime();
abstract public function setThumbnail();
abstract public function getHtmlImageURL();
2018-01-11 12:59:53 +00:00
/**
* Date the multimedia was created
*/
2019-11-23 01:51:30 +00:00
public function date_taken(): string
2018-01-11 12:59:53 +00:00
{
return $this->created
? $this->created->format('Y-m-d H:i:s').(static::$includeSubSecTime ? sprintf('.%03d',$this->subsectime) : '')
: 'UNKNOWN';
2018-01-11 12:59:53 +00:00
}
/**
* What device was the multimedia created on
*
* @return string
*/
2019-12-14 11:29:54 +00:00
public function device(): string
{
$result = '';
2019-12-15 10:08:33 +00:00
if ($this->software_id) {
if ($this->software->model_id) {
if ($this->software->model->make_id) {
$result .= $this->software->model->make->name;
}
2019-12-14 11:29:54 +00:00
2019-12-15 10:08:33 +00:00
$result .= ($result ? ' ' : '').$this->software->model->name;
}
2019-12-14 11:29:54 +00:00
$result .= ($result ? ' ' : '').$this->software->name;
2019-12-15 10:08:33 +00:00
}
2019-12-14 11:29:54 +00:00
return $result;
}
2018-01-11 12:59:53 +00:00
/**
* Return the date of the file
2019-11-23 01:51:30 +00:00
* @todo return Carbon date or NULL
2018-01-11 12:59:53 +00:00
*/
public function file_date($type,$format=FALSE)
{
if (! is_readable($this->file_path()))
return NULL;
switch ($type)
{
2020-01-04 23:53:54 +00:00
case 'a': $t = fileatime($this->file_path());
2018-01-11 12:59:53 +00:00
break;
2020-01-04 23:53:54 +00:00
case 'c': $t = filectime($this->file_path());
2018-01-11 12:59:53 +00:00
break;
2020-01-04 23:53:54 +00:00
case 'm': $t = filemtime($this->file_path());
2018-01-11 12:59:53 +00:00
break;
}
return $format ? date('d-m-Y H:i:s',$t) : $t;
}
2020-01-02 21:04:15 +00:00
/**
* Return what the filename should be.
*
* @return string
*/
public function file_name(bool $short=TRUE): string
{
// If the date created is not set, the file name will be based on the ID of the file.
$file = sprintf('%s.%s',(is_null($this->created)
2020-01-02 21:04:15 +00:00
? sprintf('UNKNOWN/%07s',$this->file_path_id())
2020-01-04 22:37:50 +00:00
: $this->created->format('Y/m/d-His').
((! is_null($this->subsectime)) ? sprintf('_%03d',$this->subsectime) : '' ).
sprintf('-%05s',$this->id))
,$this->type()
);
2020-01-02 21:04:15 +00:00
return (($short OR preg_match('/^\//',$file)) ? '' : config($this->type.'.dir').DIRECTORY_SEPARATOR).$file;
2020-01-02 21:04:15 +00:00
}
2018-01-11 12:59:53 +00:00
/**
* Determine the new name for the image
2020-07-16 01:41:17 +00:00
* @deprecated use $this->file_name()
2018-01-11 12:59:53 +00:00
*/
public function file_path($short=FALSE,$new=FALSE)
{
$file = $this->filename;
if ($new)
2020-01-05 10:55:26 +00:00
$file = $this->file_name(TRUE);
2018-01-11 12:59:53 +00:00
return (($short OR preg_match('/^\//',$file)) ? '' : config($this->type.'.dir').DIRECTORY_SEPARATOR).$file;
2018-01-11 12:59:53 +00:00
}
/**
* Calculate a file path ID based on the id of the file
*/
2019-11-23 01:51:30 +00:00
public function file_path_id($sep=3,$depth=9): string
2018-01-11 12:59:53 +00:00
{
return trim(chunk_split(sprintf("%0{$depth}s",$this->id),$sep,'/'),'/');
}
/**
* Display the file signature
*/
2019-12-14 11:29:54 +00:00
public function file_signature($short=FALSE): string
2018-01-11 12:59:53 +00:00
{
2019-12-14 11:29:54 +00:00
return $short ? static::stringtrim($this->file_signature) : $this->file_signature;
2018-01-11 12:59:53 +00:00
}
/**
2019-11-23 01:51:30 +00:00
* Return the file size
* @deprecated
2018-01-11 12:59:53 +00:00
*/
public function file_size()
{
return (! is_readable($this->file_path())) ? NULL : filesize($this->file_path());
}
public function getCreatedAttribute()
{
return $this->created_manual ?: ($this->attributes['created'] ? $this->asDateTime($this->attributes['created']) : NULL);
}
2019-11-23 01:51:30 +00:00
/**
* Return item dimensions
*/
public function getDimensionsAttribute(): string
2018-01-11 12:59:53 +00:00
{
2019-11-23 01:51:30 +00:00
return $this->width.'x'.$this->height;
2018-01-11 12:59:53 +00:00
}
2019-11-23 01:51:30 +00:00
/**
* Return HTML Checkbox for duplicate
*/
public function getDuplicateCheckboxAttribute()
2018-01-11 12:59:53 +00:00
{
2019-11-23 01:51:30 +00:00
return $this->HTMLCheckbox('duplicate',$this->id,$this->duplicate);
2018-01-11 12:59:53 +00:00
}
2019-11-23 01:51:30 +00:00
/**
* Return the file size
*
* @return false|int|null
*/
public function getFileSizeAttribute()
2018-01-11 12:59:53 +00:00
{
2019-11-23 01:51:30 +00:00
return (! is_readable($this->file_path())) ? NULL : filesize($this->file_path());
2018-01-11 12:59:53 +00:00
}
2019-11-08 10:43:36 +00:00
/**
2019-11-23 01:51:30 +00:00
* Return HTML Checkbox for flagged
2019-11-08 10:43:36 +00:00
*/
2019-11-23 01:51:30 +00:00
public function getFlagCheckboxAttribute()
2019-11-08 10:43:36 +00:00
{
2019-11-23 01:51:30 +00:00
return $this->HTMLCheckbox('flag',$this->id,$this->flag);
2019-11-08 10:43:36 +00:00
}
/**
* Return HTML Checkbox for ignore
*/
public function getIgnoreCheckboxAttribute()
{
return $this->HTMLCheckbox('ignore_duplicate',$this->id,$this->ignore_duplicate);
}
2019-11-08 10:43:36 +00:00
/**
2019-11-23 01:51:30 +00:00
* @deprecated
2019-11-08 10:43:36 +00:00
*/
2019-11-23 01:51:30 +00:00
public function getDuplicateTextAttribute()
2019-11-08 10:43:36 +00:00
{
2019-11-23 01:51:30 +00:00
return $this->TextTrueFalse($this->duplicate);
2019-11-08 10:43:36 +00:00
}
2018-01-11 12:59:53 +00:00
/**
2019-11-23 01:51:30 +00:00
* @deprecated
2018-01-11 12:59:53 +00:00
*/
2019-11-23 01:51:30 +00:00
public function getFlagTextAttribute()
2018-01-11 12:59:53 +00:00
{
2019-11-23 01:51:30 +00:00
return $this->TextTrueFalse($this->flag);
2018-01-11 12:59:53 +00:00
}
/**
* Return HTML Checkbox for remove
*/
public function getRemoveCheckboxAttribute()
{
return $this->HTMLCheckbox('remove',$this->id,$this->remove);
}
2019-12-26 05:46:35 +00:00
/**
* Return the object type
* @return string
2019-12-27 02:32:42 +00:00
* @deprecated Use objecttype()
2019-12-26 05:46:35 +00:00
*/
public function getTypeAttribute(): string
{
switch(get_class($this)) {
case 'App\Models\Photo': return 'photo';
case 'App\Models\Video': return 'video';
default: return 'unknown';
}
}
2018-01-11 12:59:53 +00:00
/**
* Display the GPS coordinates
*/
2019-11-23 01:51:30 +00:00
public function gps(): string
2018-01-11 12:59:53 +00:00
{
return ($this->gps_lat AND $this->gps_lon) ? sprintf('%s/%s',$this->gps_lat,$this->gps_lon) : 'UNKNOWN';
}
2020-01-02 21:04:15 +00:00
/**
* Return if this source file is readable.
*
* @return bool
*/
public function isReadable(): bool
{
2020-01-04 23:50:12 +00:00
return is_readable($this->file_path());
2020-01-02 21:04:15 +00:00
}
2018-01-11 12:59:53 +00:00
/**
2019-11-23 01:51:30 +00:00
* Return an HTML checkbox
2018-01-11 12:59:53 +00:00
*/
2019-11-23 01:51:30 +00:00
protected function HTMLCheckbox($name,$id,$value)
2018-01-11 12:59:53 +00:00
{
2019-11-23 01:51:30 +00:00
return sprintf('<input type="checkbox" name="%s[%s]" value="1"%s>',$name,$id,$value ? ' checked="checked"' : '');
2018-01-11 12:59:53 +00:00
}
/**
2019-11-23 01:51:30 +00:00
* Get ID Info link
2018-01-11 12:59:53 +00:00
*/
2019-11-23 01:51:30 +00:00
protected function HTMLLinkAttribute($id,$url)
2018-01-11 12:59:53 +00:00
{
return sprintf('<a href="%s" target="%s">%s</a>',url($url,$id),$id,$id);
2018-01-11 12:59:53 +00:00
}
2019-11-23 01:51:30 +00:00
/**
* Determine if the parent dir is writable
*
* @param $dir
* @return bool
*/
public function isParentWritable($dir): bool
2018-01-11 12:59:53 +00:00
{
if (file_exists($dir) AND is_writable($dir) AND is_dir($dir))
return TRUE;
2019-11-23 01:51:30 +00:00
2018-01-11 12:59:53 +00:00
elseif ($dir == dirname($dir) OR file_exists($dir))
return FALSE;
2019-11-23 01:51:30 +00:00
2018-01-11 12:59:53 +00:00
else
return ($this->isParentWritable(dirname($dir)));
}
/**
* Determine if a file is moveable
*
* useID boolean Determine if the path is based on the the ID or date
2019-11-23 01:51:30 +00:00
* @todo Change to boolean and rename isMoveable() Log any FALSE reason.
2018-01-11 12:59:53 +00:00
*/
public function moveable()
{
// If the source and target are the same, we dont need to move it
if ($this->file_path() == $this->file_path(FALSE,TRUE))
return FALSE;
// If there is already a file in the target.
// @todo If the target file is to be deleted, we could move this file
if (file_exists($this->file_path(FALSE,TRUE)))
return 1;
// Test if the source is readable
if (! is_readable($this->file_path()))
return 2;
// Test if the dir is writable (so we can remove the file)
if (! $this->isParentWritable(dirname($this->file_path())))
return 3;
// Test if the target dir is writable
// @todo The target dir may not exist yet, so we should check that a parent exists and is writable.
if (! $this->isParentWritable($this->file_path(FALSE,TRUE)))
return 4;
return TRUE;
}
/**
* Get the id of the previous record
*/
public function next()
{
return DB::table($this->getTable())
->where('id','>',$this->id)
->orderby('id','ASC')
->first();
}
2019-12-15 12:34:42 +00:00
abstract public function property(string $property);
2018-01-11 12:59:53 +00:00
/**
* Return my class shortname
*/
2019-11-23 01:51:30 +00:00
public function objectType(): string
2018-01-11 12:59:53 +00:00
{
return (new \ReflectionClass($this))->getShortName();
}
/**
* Get the id of the previous record
*/
public function previous()
{
return DB::table($this->getTable())
->where('id','<',$this->id)
2019-11-09 02:52:04 +00:00
->orderby('id','DESC')
2018-01-11 12:59:53 +00:00
->first();
}
public function setDateCreated()
{
2020-01-06 09:01:04 +00:00
$this->created = $this->property('creationdate') ?: NULL;
}
2018-01-11 12:59:53 +00:00
public function setHeightWidth()
{
$this->height = $this->property('height');
$this->width = $this->property('width');
$this->orientation = $this->property('orientation');
}
public function setSignature()
{
$this->signature = $this->property('signature');
2020-01-04 23:53:54 +00:00
$this->file_signature = md5_file($this->file_path());
}
2018-01-11 12:59:53 +00:00
/**
* Display the media signature
*/
public function signature($short=FALSE)
{
2019-12-15 12:34:42 +00:00
return ($short AND $this->signature) ? static::stringtrim($this->signature) : $this->signature;
2018-01-11 12:59:53 +00:00
}
2019-11-23 01:51:30 +00:00
/**
* Trim a string
*
* @param string $string
* @param int $chrs
* @return string
*/
public static function stringtrim(string $string,int $chrs=6)
{
return sprintf('%s...%s',substr($string,0,$chrs),substr($string,-1*$chrs));
}
/**
* @deprecated
* @param string $string
* @param int $chrs
* @return string
*/
public static function signaturetrim(string $string,int $chrs=6)
2018-01-11 12:59:53 +00:00
{
2019-11-23 01:51:30 +00:00
return static::stringtrim($string,$chrs);
2018-01-11 12:59:53 +00:00
}
/**
* Determine if the image should be moved
*/
2019-12-14 11:29:54 +00:00
public function shouldMove(): bool
2018-01-11 12:59:53 +00:00
{
return $this->filename !== $this->file_name();
2018-01-11 12:59:53 +00:00
}
2019-11-23 01:51:30 +00:00
protected function TextTrueFalse($value): string
2018-01-11 12:59:53 +00:00
{
return $value ? 'TRUE' : 'FALSE';
}
2019-11-23 01:51:30 +00:00
/**
* @todo Check if this is redundant
*
* @param bool $includeme
* @return mixed
*/
private function list_duplicate($includeme=FALSE)
2018-01-11 12:59:53 +00:00
{
return $this->list_duplicates($includeme)->pluck('id');
}
/**
* Find duplicate images based on some attributes of the current image
2019-12-14 11:29:54 +00:00
* @deprecate Use static::duplicates()
2018-01-11 12:59:53 +00:00
*/
private function list_duplicates($includeme=FALSE)
2018-01-11 12:59:53 +00:00
{
$o = static::select();
if ($this->id AND ! $includeme)
$o->where('id','!=',$this->id);
// Ignore photo's pending removal.
if (! $includeme)
$o->where(function($query)
{
2019-11-23 01:51:30 +00:00
$query->where('remove','<>',TRUE)
2018-01-11 12:59:53 +00:00
->orWhere('remove','=',NULL);
});
// Where the signature is the same
$o->where(function($query)
{
$query->where('signature','=',$this->signature);
// Or they have the same time taken with the same camera
if ($this->date_created AND ($this->model OR $this->make))
{
$query->orWhere(function($query)
{
$query->where('date_created','=',$this->date_created ? $this->date_created : NULL);
if (Schema::hasColumn($this->getTable(),'subsectime'))
$query->where('subsectime','=',$this->subsectime ? $this->subsectime : NULL);
if (! is_null($this->model))
$query->where('model','=',$this->model);
if (! is_null($this->make))
$query->where('make','=',$this->make);
});
}
});
return $o->get();
}
2019-11-08 12:51:47 +00:00
}