|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MarkSitko\LaravelUnsplash\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\DB; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
7
|
|
|
use Illuminate\Support\Facades\Storage; |
|
8
|
|
|
use MarkSitko\LaravelUnsplash\Unsplash; |
|
9
|
|
|
|
|
10
|
|
|
class UnsplashAsset extends Model |
|
11
|
|
|
{ |
|
12
|
|
|
protected $fillable = [ |
|
13
|
|
|
'unsplash_id', |
|
14
|
|
|
'name', |
|
15
|
|
|
'author', |
|
16
|
|
|
'author_link', |
|
17
|
|
|
]; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* The booting method of the model. |
|
21
|
|
|
* |
|
22
|
|
|
* @return void |
|
23
|
|
|
*/ |
|
24
|
|
|
protected static function boot() |
|
25
|
|
|
{ |
|
26
|
|
|
parent::boot(); |
|
27
|
|
|
|
|
28
|
|
|
// cleanup while deleting an asset |
|
29
|
|
|
static::deleting(function (UnsplashAsset $unsplashAsset) { |
|
30
|
|
|
|
|
31
|
|
|
if ( Storage::disk( config('unsplash.disk', 'local') )->exists("{$unsplashAsset->name}") ) { |
|
32
|
|
|
Storage::disk( config('unsplash.disk', 'local') )->delete("{$unsplashAsset->name}"); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
DB::delete('delete from unsplashables where unsplash_asset_id = ?', [$unsplashAsset->id]); |
|
36
|
|
|
|
|
37
|
|
|
}); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Get all unsplashables by given model |
|
42
|
|
|
* @param $model Illuminate\Database\Eloquent\Model |
|
43
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany |
|
44
|
|
|
*/ |
|
45
|
|
|
public function assets(Illuminate\Database\Eloquent\Model $model) |
|
46
|
|
|
{ |
|
47
|
|
|
return $this->morphedByMany($model, 'unsplashables'); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Creates a new instance of the unsplash api client |
|
52
|
|
|
* @return MarkSitko\LaravelUnsplash\Unsplash |
|
53
|
|
|
*/ |
|
54
|
|
|
public static function api() |
|
55
|
|
|
{ |
|
56
|
|
|
return new Unsplash(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Returns a complete copyright link for a given data set |
|
61
|
|
|
* @return string |
|
62
|
|
|
*/ |
|
63
|
|
|
public function getFullCopyrightLink() |
|
64
|
|
|
{ |
|
65
|
|
|
return "Photo by <a target='_blank' href='{$this->author_link}'>{$this->author}</a> on <a target='_blank' href='https://unsplash.com/'>Unsplash</a>"; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|