photo/app/Models/Abstracted/Catalog.php

490 lines
11 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\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;
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
}
2019-12-26 04:56:31 +00:00
public function software()
{
return $this->belongsTo(Software::class);
}
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
}
2019-12-14 11:29:54 +00:00
public function scopeDuplicates($query) {
if (! $this->exists)
return $query;
// Exclude this record
$query->where('id','<>',$this->attributes['id']);
// 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['date_created'] AND $this->software_id) {
$q->where('date_created','=',$this->attributes['date_created'] ?: NULL);
if (static::$includeSubSecTime)
$q->where('subsectime','=',$this->attributes['subsectime'] ?: NULL);
$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)
2018-01-11 12:59:53 +00:00
->orWhere('duplicate','=',NULL);
});
}
/**
* 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);
});
}
2019-11-09 02:52:04 +00:00
abstract public function setDateCreated();
abstract public function setLocation();
abstract public function setSignature();
abstract public function setSubSecTime();
abstract public function setThumbnail();
2019-12-14 11:29:54 +00:00
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
{
2019-12-15 12:34:42 +00:00
return $this->date_created ? $this->date_created->format('Y-m-d H:i:s') : 'UNKNOWN';
2018-01-11 12:59:53 +00:00
}
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)
{
case 'a': $t = fileatime($this->file_path());
break;
case 'c': $t = filectime($this->file_path());
break;
case 'm': $t = filemtime($this->file_path());
break;
}
return $format ? date('d-m-Y H:i:s',$t) : $t;
}
/**
* Determine the new name for the image
*/
public function file_path($short=FALSE,$new=FALSE)
{
$file = $this->filename;
if ($new)
$file = sprintf('%s.%s',((is_null($this->date_created) OR ! $this->date_created)
? sprintf('UNKNOWN/%07s',$this->file_path_id())
2019-12-15 12:34:42 +00:00
: $this->date_created->format('Y/m/d-His')),$this->type());
2018-01-11 12:59:53 +00:00
return (($short OR preg_match('/^\//',$file)) ? '' : config('video.dir').DIRECTORY_SEPARATOR).$file;
}
/**
* 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());
}
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
}
2019-12-15 12:34:42 +00:00
public function getDateCreatedAttribute() {
2019-12-21 11:57:34 +00:00
return $this->attributes['date_created'] ? Carbon::createFromTimestamp($this->attributes['date_created']) : NULL;
2019-12-15 12:34:42 +00:00
}
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
*/
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';
}
/**
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
{
2019-11-23 01:51:30 +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 setHeightWidth()
{
$this->height = $this->property('height');
$this->width = $this->property('width');
$this->orientation = $this->property('orientation');
}
/**
* 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_path(TRUE,TRUE));
}
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
*/
2018-01-11 12:59:53 +00:00
public function list_duplicate($includeme=FALSE)
{
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
*/
public function list_duplicates($includeme=FALSE)
{
$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
}