2013-07-28 19:45:21 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Entities;
|
2013-08-19 06:19:04 +02:00
|
|
|
use Helpers;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
2013-08-21 04:40:11 +02:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2013-08-19 06:19:04 +02:00
|
|
|
use Illuminate\Support\Facades\URL;
|
2013-07-28 19:45:21 +02:00
|
|
|
use Traits\SlugTrait;
|
|
|
|
|
|
|
|
class Playlist extends \Eloquent {
|
|
|
|
protected $table = 'playlists';
|
|
|
|
protected $softDelete = true;
|
|
|
|
|
|
|
|
use SlugTrait;
|
|
|
|
|
|
|
|
public static function summary() {
|
2013-08-19 06:19:04 +02:00
|
|
|
return self::select('id', 'title', 'user_id', 'slug', 'created_at', 'is_public', 'description', 'comment_count', 'download_count', 'view_count', 'favourite_count');
|
|
|
|
}
|
|
|
|
|
2013-08-29 05:19:24 +02:00
|
|
|
public function scopeUserDetails($query) {
|
2013-08-19 06:19:04 +02:00
|
|
|
if (Auth::check()) {
|
|
|
|
$query->with(['users' => function($query) {
|
|
|
|
$query->whereUserId(Auth::user()->id);
|
|
|
|
}]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return !$query;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function mapPublicPlaylistShow($playlist) {
|
|
|
|
$tracks = [];
|
|
|
|
foreach ($playlist->tracks as $track) {
|
|
|
|
$tracks[] = Track::mapPublicTrackSummary($track);
|
|
|
|
}
|
|
|
|
|
|
|
|
$formats = [];
|
|
|
|
foreach (Track::$Formats as $name => $format) {
|
|
|
|
$formats[] = [
|
|
|
|
'name' => $name,
|
|
|
|
'extension' => $format['extension'],
|
|
|
|
'url' => $playlist->getDownloadUrl($name),
|
|
|
|
'size' => Helpers::formatBytes($playlist->getFilesize($name))
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
$comments = [];
|
|
|
|
foreach ($playlist->comments as $comment) {
|
|
|
|
$comments[] = Comment::mapPublic($comment);
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = self::mapPublicPlaylistSummary($playlist);
|
|
|
|
$data['tracks'] = $tracks;
|
|
|
|
$data['comments'] = $comments;
|
|
|
|
$data['formats'] = $formats;
|
2013-08-29 05:19:24 +02:00
|
|
|
$data['share'] = [
|
|
|
|
'url' => URL::to('/p' . $playlist->id),
|
|
|
|
'tumblrUrl' => 'http://www.tumblr.com/share/link?url=' . urlencode($playlist->url) . '&name=' . urlencode($playlist->title) . '&description=' . urlencode($playlist->description),
|
|
|
|
'twitterUrl' => 'https://platform.twitter.com/widgets/tweet_button.html?text=' . $playlist->title . ' by ' . $playlist->user->display_name . ' on Pony.fm'
|
|
|
|
];
|
2013-08-19 06:19:04 +02:00
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function mapPublicPlaylistSummary($playlist) {
|
|
|
|
$userData = [
|
|
|
|
'stats' => [
|
|
|
|
'views' => 0,
|
|
|
|
'downloads' => 0
|
|
|
|
],
|
|
|
|
'is_favourited' => false
|
|
|
|
];
|
|
|
|
|
2013-09-01 04:28:45 +02:00
|
|
|
if (Auth::check() && $playlist->users->count()) {
|
2013-08-19 06:19:04 +02:00
|
|
|
$userRow = $playlist->users[0];
|
|
|
|
$userData = [
|
|
|
|
'stats' => [
|
2015-09-07 22:28:21 +02:00
|
|
|
'views' => (int) $userRow->view_count,
|
|
|
|
'downloads' => (int) $userRow->download_count,
|
2013-08-19 06:19:04 +02:00
|
|
|
],
|
2015-09-07 22:28:21 +02:00
|
|
|
'is_favourited' => (bool) $userRow->is_favourited
|
2013-08-19 06:19:04 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
2015-09-07 22:28:21 +02:00
|
|
|
'id' => (int) $playlist->id,
|
2013-08-21 22:45:33 +02:00
|
|
|
'track_count' => $playlist->track_count,
|
2013-08-19 06:19:04 +02:00
|
|
|
'title' => $playlist->title,
|
|
|
|
'slug' => $playlist->slug,
|
|
|
|
'created_at' => $playlist->created_at,
|
2015-09-07 22:28:21 +02:00
|
|
|
'is_public' => (bool) $playlist->is_public,
|
2013-08-19 06:19:04 +02:00
|
|
|
'stats' => [
|
2015-09-07 22:28:21 +02:00
|
|
|
'views' => (int) $playlist->view_count,
|
|
|
|
'downloads' => (int) $playlist->download_count,
|
|
|
|
'comments' => (int) $playlist->comment_count,
|
|
|
|
'favourites' => (int) $playlist->favourite_count
|
2013-08-19 06:19:04 +02:00
|
|
|
],
|
|
|
|
'covers' => [
|
|
|
|
'small' => $playlist->getCoverUrl(Image::SMALL),
|
|
|
|
'normal' => $playlist->getCoverUrl(Image::NORMAL)
|
|
|
|
],
|
|
|
|
'url' => $playlist->url,
|
|
|
|
'user' => [
|
2015-09-07 22:28:21 +02:00
|
|
|
'id' => (int) $playlist->user->id,
|
2013-08-19 06:19:04 +02:00
|
|
|
'name' => $playlist->user->display_name,
|
|
|
|
'url' => $playlist->user->url,
|
|
|
|
],
|
2013-08-31 03:46:35 +02:00
|
|
|
'user_data' => $userData,
|
|
|
|
'permissions' => [
|
|
|
|
'delete' => Auth::check() && Auth::user()->id == $playlist->user_id,
|
|
|
|
'edit' => Auth::check() && Auth::user()->id == $playlist->user_id
|
|
|
|
]
|
2013-08-19 06:19:04 +02:00
|
|
|
];
|
2013-07-28 19:45:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function tracks() {
|
2013-08-01 10:57:08 +02:00
|
|
|
return $this
|
|
|
|
->belongsToMany('Entities\Track')
|
|
|
|
->withPivot('position')
|
|
|
|
->withTimestamps()
|
|
|
|
->orderBy('position', 'asc');
|
|
|
|
}
|
|
|
|
|
2013-08-19 06:19:04 +02:00
|
|
|
public function users() {
|
|
|
|
return $this->hasMany('Entities\ResourceUser');
|
|
|
|
}
|
|
|
|
|
2013-08-01 10:57:08 +02:00
|
|
|
public function comments(){
|
2013-08-29 05:19:24 +02:00
|
|
|
return $this->hasMany('Entities\Comment')->orderBy('created_at', 'desc');
|
2013-07-28 19:45:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function pins() {
|
|
|
|
return $this->hasMany('Entities\PinnedPlaylist');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function user() {
|
|
|
|
return $this->belongsTo('Entities\User');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function hasPinFor($userId) {
|
|
|
|
foreach ($this->pins as $pin) {
|
|
|
|
if ($pin->user_id == $userId)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function canView($user) {
|
|
|
|
return $this->is_public || ($user != null && $user->id == $this->user_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getUrlAttribute() {
|
2013-08-19 06:19:04 +02:00
|
|
|
return URL::to('/playlist/' . $this->id . '-' . $this->slug);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getDownloadUrl($format) {
|
|
|
|
return URL::to('p' . $this->id . '/dl.' . Track::$Formats[$format]['extension']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFilesize($format) {
|
2013-08-21 04:40:11 +02:00
|
|
|
$tracks = $this->tracks;
|
2013-08-19 06:19:04 +02:00
|
|
|
if (!count($tracks))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return Cache::remember($this->getCacheKey('filesize-' . $format), 1440, function() use ($tracks, $format) {
|
|
|
|
$size = 0;
|
|
|
|
foreach ($tracks as $track) {
|
|
|
|
$size += $track->getFilesize($format);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $size;
|
|
|
|
});
|
2013-07-28 19:45:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getCoverUrl($type = Image::NORMAL) {
|
|
|
|
if ($this->tracks->count() == 0)
|
|
|
|
return $this->user->getAvatarUrl($type);
|
|
|
|
|
|
|
|
return $this->tracks[0]->getCoverUrl($type);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function pin($userId) {
|
|
|
|
$pin = new PinnedPlaylist();
|
|
|
|
$pin->playlist_id = $this->id;
|
|
|
|
$pin->user_id = $userId;
|
|
|
|
$pin->save();
|
|
|
|
}
|
2013-08-19 06:19:04 +02:00
|
|
|
|
|
|
|
private function getCacheKey($key) {
|
2013-08-21 04:40:11 +02:00
|
|
|
return 'playlist-' . $this->id . '-' . $key;
|
2013-08-19 06:19:04 +02:00
|
|
|
}
|
2015-09-07 22:28:21 +02:00
|
|
|
}
|