1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Itstructure\MFU\Models\Owners; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\{Collection as EloquentCollection, Builder as EloquentBuilder}; |
6
|
|
|
use Itstructure\MFU\Traits\HasCompositePrimaryKey; |
7
|
|
|
use Itstructure\MFU\Models\Albums\{AlbumBase, AlbumTyped}; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class OwnerAlbum |
11
|
|
|
* @package Itstructure\MFU\Models\Owners |
12
|
|
|
*/ |
13
|
|
|
class OwnerAlbum extends Owner |
14
|
|
|
{ |
15
|
|
|
use HasCompositePrimaryKey; |
16
|
|
|
|
17
|
|
|
public $incrementing = false; |
18
|
|
|
|
19
|
|
|
protected $primaryKey = ['album_id', 'owner_id', 'owner_name', 'owner_attribute']; |
20
|
|
|
|
21
|
|
|
protected $table = 'owners_albums'; |
22
|
|
|
|
23
|
|
|
protected $fillable = ['album_id', 'owner_id', 'owner_name', 'owner_attribute']; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne |
27
|
|
|
*/ |
28
|
|
|
public function getAlbum() |
29
|
|
|
{ |
30
|
|
|
return $this->hasOne(AlbumBase::class, 'album_id', 'id'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Get all albums by owner. |
35
|
|
|
* @param string $ownerName |
36
|
|
|
* @param int $ownerId |
37
|
|
|
* @param string|null $ownerAttribute |
38
|
|
|
* @return EloquentCollection |
39
|
|
|
*/ |
40
|
|
|
public static function getAlbums(string $ownerName, int $ownerId, string $ownerAttribute = null): EloquentCollection |
41
|
|
|
{ |
42
|
|
|
return static::getAlbumsQuery(static::buildFilterOptions($ownerId, $ownerName, $ownerAttribute))->get(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get all albums query by owner. |
47
|
|
|
* @param array $args. It can be an array of the next params: owner_name{string}, owner_id{int}, owner_attribute{string}. |
48
|
|
|
* @return EloquentBuilder |
49
|
|
|
*/ |
50
|
|
|
public static function getAlbumsQuery(array $args = []): EloquentBuilder |
51
|
|
|
{ |
52
|
|
|
return AlbumBase::query()->whereIn('id', static::getEntityIdsQuery('album_id', $args)->get()->pluck('album_id')); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get image albums by owner. |
57
|
|
|
* @param string $ownerName |
58
|
|
|
* @param int $ownerId |
59
|
|
|
* @return EloquentCollection |
60
|
|
|
*/ |
61
|
|
|
public static function getImageAlbums(string $ownerName, int $ownerId): EloquentCollection |
62
|
|
|
{ |
63
|
|
|
return static::getAlbums($ownerName, $ownerId, AlbumTyped::ALBUM_TYPE_IMAGE); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get audio albums by owner. |
68
|
|
|
* @param string $ownerName |
69
|
|
|
* @param int $ownerId |
70
|
|
|
* @return EloquentCollection |
71
|
|
|
*/ |
72
|
|
|
public static function getAudioAlbums(string $ownerName, int $ownerId): EloquentCollection |
73
|
|
|
{ |
74
|
|
|
return static::getAlbums($ownerName, $ownerId, AlbumTyped::ALBUM_TYPE_AUDIO); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get video albums by owner. |
79
|
|
|
* @param string $ownerName |
80
|
|
|
* @param int $ownerId |
81
|
|
|
* @return EloquentCollection |
82
|
|
|
*/ |
83
|
|
|
public static function getVideoAlbums(string $ownerName, int $ownerId): EloquentCollection |
84
|
|
|
{ |
85
|
|
|
return static::getAlbums($ownerName, $ownerId, AlbumTyped::ALBUM_TYPE_VIDEO); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get application albums by owner. |
90
|
|
|
* @param string $ownerName |
91
|
|
|
* @param int $ownerId |
92
|
|
|
* @return EloquentCollection |
93
|
|
|
*/ |
94
|
|
|
public static function getAppAlbums(string $ownerName, int $ownerId): EloquentCollection |
95
|
|
|
{ |
96
|
|
|
return static::getAlbums($ownerName, $ownerId, AlbumTyped::ALBUM_TYPE_APP); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get text albums by owner. |
101
|
|
|
* @param string $ownerName |
102
|
|
|
* @param int $ownerId |
103
|
|
|
* @return EloquentCollection |
104
|
|
|
*/ |
105
|
|
|
public static function getTextAlbums(string $ownerName, int $ownerId): EloquentCollection |
106
|
|
|
{ |
107
|
|
|
return static::getAlbums($ownerName, $ownerId, AlbumTyped::ALBUM_TYPE_TEXT); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get other albums by owner. |
112
|
|
|
* @param string $ownerName |
113
|
|
|
* @param int $ownerId |
114
|
|
|
* @return EloquentCollection |
115
|
|
|
*/ |
116
|
|
|
public static function getOtherAlbums(string $ownerName, int $ownerId): EloquentCollection |
117
|
|
|
{ |
118
|
|
|
return static::getAlbums($ownerName, $ownerId, AlbumTyped::ALBUM_TYPE_OTHER); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get model album primary key name. |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
protected static function getDependencyKeyName(): string |
126
|
|
|
{ |
127
|
|
|
return 'album_id'; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param int $albumId |
132
|
|
|
* @return bool |
133
|
|
|
*/ |
134
|
|
|
protected static function removeDependency(int $albumId): bool |
135
|
|
|
{ |
136
|
|
|
return AlbumBase::find($albumId) |
|
|
|
|
137
|
|
|
->setRemoveDependencies(true) |
138
|
|
|
->delete(); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|