blue-twilight/app/Label.php

60 lines
1.4 KiB
PHP

<?php
namespace App;
use App\Helpers\DbHelper;
use App\Helpers\MiscHelper;
use Illuminate\Database\Eloquent\Model;
class Label extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'url_alias'
];
public function generateAlias()
{
$this->url_alias = preg_replace('/[^a-z0-9\-]/', '-', strtolower($this->name));
}
public function photoCount()
{
return $this->photos()->whereIn('album_id', DbHelper::getAlbumIDsForCurrentUser())->count();
}
public function photos()
{
return $this->belongsToMany(Photo::class, 'photo_labels');
}
public function thumbnailUrl($thumbnailName)
{
$photo = $this->photos()
->whereIn('album_id', DbHelper::getAlbumIDsForCurrentUser())
->inRandomOrder()
->first();
if (!is_null($photo))
{
return $photo->album->getAlbumSource()->getUrlToPhoto($photo, $thumbnailName);
}
// Rotate standard images
$images = [
asset('themes/base/images/empty-album-1.jpg'),
asset('themes/base/images/empty-album-2.jpg'),
asset('themes/base/images/empty-album-3.jpg')
];
return $images[rand(0, count($images) - 1)];
}
public function url()
{
return route('viewLabel', $this->url_alias);
}
}