-
Notifications
You must be signed in to change notification settings - Fork 587
/
SongViewAdapter.php
489 lines (415 loc) · 16.4 KB
/
SongViewAdapter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
<?php
/*
* vim:set softtabstop=4 shiftwidth=4 expandtab:
*
* LICENSE: GNU Affero General Public License, version 3 (AGPL-3.0-or-later)
* Copyright 2001 - 2020 Ampache.org
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
declare(strict_types=0);
namespace Ampache\Gui\Song;
use Ampache\Config\ConfigContainerInterface;
use Ampache\Config\ConfigurationKeyEnum;
use Ampache\Repository\Model\Catalog;
use Ampache\Repository\Model\ModelFactoryInterface;
use Ampache\Repository\Model\Rating;
use Ampache\Repository\Model\Share;
use Ampache\Repository\Model\Song;
use Ampache\Repository\Model\Userflag;
use Ampache\Module\Api\Ajax;
use Ampache\Module\Application\Song\DeleteAction;
use Ampache\Module\Authorization\AccessLevelEnum;
use Ampache\Module\Authorization\GuiGatekeeperInterface;
use Ampache\Module\Playback\Stream_Playlist;
use Ampache\Module\System\Core;
use Ampache\Module\Util\Ui;
final class SongViewAdapter implements SongViewAdapterInterface
{
private ConfigContainerInterface $configContainer;
private ModelFactoryInterface $modelFactory;
private GuiGatekeeperInterface $gatekeeper;
private Song $song;
public function __construct(
ConfigContainerInterface $configContainer,
ModelFactoryInterface $modelFactory,
GuiGatekeeperInterface $gatekeeper,
Song $song
) {
$this->configContainer = $configContainer;
$this->modelFactory = $modelFactory;
$this->gatekeeper = $gatekeeper;
$this->song = $song;
}
public function getId(): int
{
return $this->song->getId();
}
public function getRating(): string
{
return Rating::show($this->song->getId(), 'song');
}
public function getAverageRating(): string
{
$rating = $this->modelFactory->createRating(
$this->song->getId(),
'song'
);
return (string) $rating->get_average_rating();
}
public function canAutoplayNext(): bool
{
return Stream_Playlist::check_autoplay_next();
}
public function canAppendNext(): bool
{
return Stream_Playlist::check_autoplay_append();
}
public function getUserFlags(): string
{
return Userflag::show($this->song->getId(), 'song');
}
public function getWaveformUrl(): string
{
return sprintf(
'%s/waveform.php?song_id=%d',
$this->configContainer->getWebPath(),
$this->song->getId()
);
}
public function getDirectplayButton(): string
{
$songId = $this->song->getId();
return Ajax::button(
'?page=stream&action=directplay&object_type=song&object_id=' . $songId,
'play',
T_('Play'),
'play_song_' . $songId
);
}
public function getAutoplayNextButton(): string
{
$songId = $this->song->getId();
return Ajax::button(
'?page=stream&action=directplay&object_type=song&object_id=' . $songId . '&playnext=true',
'play_next',
T_('Play next'),
'nextplay_song_' . $songId
);
}
public function getAppendNextButton(): string
{
$songId = $this->song->getId();
return Ajax::button(
'?page=stream&action=directplay&object_type=song&object_id=' . $songId . '&append=true',
'play_add',
T_('Play last'),
'addplay_song_' . $songId
);
}
public function getCustomPlayActions(): string
{
$actions = Song::get_custom_play_actions();
$buttons = '';
foreach ($actions as $action) {
$buttons .= Ajax::button(
'?page=stream&action=directplay&object_type=song&object_id=' . $this->song->getId() . '&custom_play_action=' . $action['index'],
$action['icon'],
T_($action['title']),
$action['icon'] . '_song_' . $this->song->getId()
);
}
return $buttons;
}
public function getTemporaryPlaylistButton(): string
{
$songId = $this->song->getId();
return Ajax::button(
'?action=basket&type=song&id=' . $songId,
'add',
T_('Add to Temporary Playlist'),
'add_song_' . $songId
);
}
public function canPostShout(): bool
{
return (
$this->configContainer->isAuthenticationEnabled() === false ||
$this->gatekeeper->mayAccess(AccessLevelEnum::TYPE_INTERFACE, AccessLevelEnum::LEVEL_USER) === true
) &&
$this->configContainer->isFeatureEnabled(ConfigurationKeyEnum::SOCIABLE);
}
public function getPostShoutUrl(): string
{
return sprintf(
'%s/shout.php?action=show_add_shout&type=song&id=%d',
$this->configContainer->getWebPath(),
$this->song->getId()
);
}
public function getPostShoutIcon(): string
{
return Ui::get_icon('comment', T_('Post Shout'));
}
public function canShare(): bool
{
return $this->gatekeeper->mayAccess(AccessLevelEnum::TYPE_INTERFACE, AccessLevelEnum::LEVEL_USER) &&
$this->configContainer->isFeatureEnabled(ConfigurationKeyEnum::SHARE);
}
public function getShareUi(): string
{
return Share::display_ui('song', $this->song->getId(), false);
}
public function canDownload(): bool
{
return $this->configContainer->isFeatureEnabled(ConfigurationKeyEnum::DOWNLOAD);
}
public function getExternalPlayUrl(): string
{
return $this->song->play_url(
'&action=download',
'',
false,
Core::get_global('user')->id
);
}
public function getExternalPlayIcon(): string
{
return Ui::get_icon('link', T_('Link'));
}
public function getDownloadUrl(): string
{
return sprintf(
'%s/stream.php?action=download&song_id=%d',
$this->configContainer->getWebPath(),
$this->song->getId()
);
}
public function getDownloadIcon(): string
{
return Ui::get_icon('download', T_('Download'));
}
public function canDisplayStats(): bool
{
$owner = $this->song->get_user_owner();
return (
($owner !== null && $owner == $GLOBALS['user']->id) ||
$this->gatekeeper->mayAccess(AccessLevelEnum::TYPE_INTERFACE, AccessLevelEnum::LEVEL_CONTENT_MANAGER)
) &&
$this->configContainer->isFeatureEnabled(ConfigurationKeyEnum::STATISTICAL_GRAPHS) &&
is_dir(__DIR__ . '/../../../vendor/szymach/c-pchart/src/Chart/');
}
public function getDisplayStatsUrl(): string
{
return sprintf(
'%s/stats.php?action=graph&object_type=song&object_id=%d',
$this->configContainer->getWebPath(),
$this->song->getId()
);
}
public function getDisplayStatsIcon(): string
{
return Ui::get_icon('statistics', T_('Graphs'));
}
public function isEditable(): bool
{
return $this->gatekeeper->mayAccess(AccessLevelEnum::TYPE_INTERFACE, AccessLevelEnum::LEVEL_CONTENT_MANAGER) || (
$this->song->get_user_owner() == Core::get_global('user')->id &&
$this->configContainer->isFeatureEnabled(ConfigurationKeyEnum::UPLOAD_ALLOW_EDIT) === true
);
}
public function getEditButtonTitle(): string
{
return T_('Song Edit');
}
public function getEditIcon(): string
{
return Ui::get_icon('edit', T_('Edit'));
}
public function canToggleState(): bool
{
return $this->gatekeeper->mayAccess(AccessLevelEnum::TYPE_INTERFACE, AccessLevelEnum::LEVEL_MANAGER) || (
$this->song->get_user_owner() == Core::get_global('user')->id &&
$this->configContainer->isFeatureEnabled(ConfigurationKeyEnum::UPLOAD_ALLOW_EDIT) === true
);
}
public function getToggleStateButton(): string
{
$songId = $this->song->getId();
if ($this->song->enabled) {
$icon = 'disable';
$icontext = T_('Disable');
} else {
$icon = 'enable';
$icontext = T_('Enable');
}
return Ajax::button(
'?page=song&action=flip_state&song_id=' . $songId,
$icon,
$icontext,
'flip_song_' . $songId
);
}
public function getDeletionUrl(): string
{
return sprintf(
'%s/song.php?action=%s&song_id=%d',
$this->configContainer->getWebPath(),
DeleteAction::REQUEST_KEY,
$this->song->getId()
);
}
public function getDeletionIcon(): string
{
return Ui::get_icon('delete', T_('Delete'));
}
public function canBeDeleted(): bool
{
return Catalog::can_remove($this->song);
}
public function getProperties(): array
{
$songprops = [];
$songprops[T_('Title')] = scrub_out($this->song->title);
$songprops[T_('Song Artist')] = $this->song->f_artist_link;
if (!empty($this->song->f_albumartist_link)) {
$songprops[T_('Album Artist')] = $this->song->f_albumartist_link;
}
$songprops[T_('Album')] = $this->song->f_album_link . ($this->song->year ? " (" . scrub_out($this->song->year) . ")" : "");
$songprops[T_('Composer')] = scrub_out($this->song->composer);
$songprops[T_('Genres')] = $this->song->f_tags;
$songprops[T_('Year')] = $this->song->year;
$songprops[T_('Original Year')] = scrub_out($this->song->get_album_original_year($this->song->album));
$songprops[T_('Length')] = scrub_out($this->song->f_time);
$songprops[T_('Links')] = "<a href=\"http://www.google.com/search?q=%22" . rawurlencode($this->song->f_artist) . "%22+%22" . rawurlencode($this->song->f_name) . "%22\" target=\"_blank\">" . UI::get_icon('google', T_('Search on Google ...')) . "</a>";
$songprops[T_('Links')] .= " <a href=\"https://www.duckduckgo.com/?q=%22" . rawurlencode($this->song->f_artist) . "%22+%22" . rawurlencode($this->song->f_name) . "%22\" target=\"_blank\">" . UI::get_icon('duckduckgo', T_('Search on DuckDuckGo ...')) . "</a>";
$songprops[T_('Links')] .= " <a href=\"http://www.last.fm/search?q=%22" . rawurlencode($this->song->f_artist) . "%22+%22" . rawurlencode($this->song->f_name) . "%22&type=track\" target=\"_blank\">" . UI::get_icon('lastfm', T_('Search on Last.fm ...')) . "</a>";
if ($this->song->mbid) {
$songprops[T_('Links')] .= " <a href=\"https://musicbrainz.org/recording/" . $this->song->mbid . "\" target=\"_blank\">" . UI::get_icon('musicbrainz', T_('Search on Musicbrainz ...')) . "</a>";
} else {
$songprops[T_('Links')] .= " <a href=\"https://musicbrainz.org/taglookup?tag-lookup.artist=%22" . rawurlencode($this->song->f_artist) . "%22&tag-lookup.track=%22" . rawurlencode($this->song->f_name) . "%22\" target=\"_blank\">" . UI::get_icon('musicbrainz', T_('Search on Musicbrainz ...')) . "</a>";
}
$songprops[T_('Comment')] = scrub_out($this->song->comment);
$label_string = '';
foreach (array_map('trim', explode(';', $this->song->label)) as $label_name) {
$label_string .= "<a href=\"" . $this->configContainer->getWebPath() . "/labels.php?action=show&name=" . scrub_out($label_name) . "\">" . scrub_out($label_name) . "</a> ";
}
$songprops[T_('Label')] = $this->configContainer->isFeatureEnabled(ConfigurationKeyEnum::LABEL) ? $label_string : scrub_out($this->song->label);
$songprops[T_('Song Language')] = scrub_out($this->song->language);
$songprops[T_('Catalog Number')] = scrub_out($this->song->get_album_catalog_number($this->song->album));
$songprops[T_('Barcode')] = scrub_out($this->song->get_album_barcode($this->song->album));
$songprops[T_('Bitrate')] = scrub_out($this->song->f_bitrate);
$songprops[T_('Channels')] = scrub_out($this->song->channels);
$songprops[T_('Song MBID')] = scrub_out($this->song->mbid);
$songprops[T_('Album MBID')] = scrub_out($this->song->album_mbid);
$songprops[T_('Artist MBID')] = scrub_out($this->song->artist_mbid);
if ($this->song->replaygain_track_gain !== null) {
$songprops[T_('ReplayGain Track Gain')] = scrub_out($this->song->replaygain_track_gain);
}
if ($this->song->replaygain_album_gain !== null) {
$songprops[T_('ReplayGain Album Gain')] = scrub_out($this->song->replaygain_album_gain);
}
if ($this->gatekeeper->mayAccess(AccessLevelEnum::TYPE_INTERFACE, AccessLevelEnum::LEVEL_MANAGER)) {
$songprops[T_('Filename')] = scrub_out($this->song->file) . " " . $this->song->f_size;
}
if ($this->song->update_time) {
$songprops[T_('Last Updated')] = get_datetime((int) $this->song->update_time);
}
$songprops[T_('Added')] = get_datetime((int) $this->song->addition_time);
if ($this->configContainer->isFeatureEnabled(ConfigurationKeyEnum::SHOW_PLAYED_TIMES)) {
$songprops[T_('# Played')] = scrub_out($this->song->total_count);
}
if ($this->configContainer->isFeatureEnabled(ConfigurationKeyEnum::SHOW_SKIPPED_TIMES)) {
$songprops[T_('# Skipped')] = scrub_out($this->song->total_skip);
}
if ($this->configContainer->isFeatureEnabled(ConfigurationKeyEnum::SHOW_LYRICS)) {
$songprops[T_('Lyrics')] = $this->song->f_lyrics;
}
if ($this->configContainer->isFeatureEnabled(ConfigurationKeyEnum::LICENSING) && $this->song->license) {
$songprops[T_('Licensing')] = $this->song->f_license;
}
$owner_id = $this->song->get_user_owner();
if ($this->configContainer->isFeatureEnabled(ConfigurationKeyEnum::SOCIABLE) && $owner_id > 0) {
$owner = $this->modelFactory->createUser($owner_id);
$owner->format();
$songprops[T_('Uploaded by')] = $owner->f_link;
}
return $songprops;
}
public function canEditPlaylist(): bool
{
return $this->gatekeeper->mayAccess(AccessLevelEnum::TYPE_INTERFACE, AccessLevelEnum::LEVEL_USER);
}
public function getAddToPlaylistIcon(): string
{
return Ui::get_icon('playlist_add', T_('Add to playlist'));
}
public function canBeReordered(): bool
{
return $this->gatekeeper->mayAccess(AccessLevelEnum::TYPE_INTERFACE, AccessLevelEnum::LEVEL_CONTENT_MANAGER);
}
public function getReorderIcon(): string
{
return Ui::get_icon('drag', T_('Reorder'));
}
public function getPreferencesIcon(): string
{
return Ui::get_icon('preferences', T_('Song Information'));
}
public function getTrackNumber(): string
{
return $this->song->f_track;
}
public function getSongUrl(): string
{
return $this->song->link;
}
public function getSongLink(): string
{
return $this->song->f_link;
}
public function getArtistLink(): string
{
return $this->song->f_artist_link;
}
public function getAlbumLink(): string
{
return $this->song->f_album_link;
}
public function getYear(): int
{
return $this->song->year;
}
public function getGenre(): string
{
return $this->song->f_tags;
}
public function getPlayDuration(): string
{
return $this->song->f_time;
}
public function getLicenseLink(): string
{
return (string) $this->song->f_license;
}
public function getNumberPlayed(): int
{
return $this->song->total_count;
}
public function getNumberSkipped(): int
{
return $this->song->total_skip;
}
}