Passed
Push — master ( 07fdea...6c7f77 )
by Marcel
02:24
created

lib/Controller/SidebarController.php (1 issue)

1
<?php
2
/**
3
 * Audio Player
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the LICENSE.md file.
7
 *
8
 * @author Marcel Scherello <[email protected]>
9
 * @copyright 2016-2019 Marcel Scherello
10
 */
11
12
namespace OCA\audioplayer\Controller;
13
14
use OCP\AppFramework\Controller;
15
use OCP\AppFramework\Http\JSONResponse;
16
use OCP\Files\IRootFolder;
17
use OCP\IRequest;
18
use OCP\IL10N;
19
use OCP\IDbConnection;
20
use OCP\ITagManager;
21
22
/**
23
 * Controller class for Sidebar.
24
 */
25
class SidebarController extends Controller
26
{
27
28
    private $userId;
29
    private $db;
30
    private $l10n;
31
    private $tagger;
32
    private $tagManager;
33
    private $DBController;
34
35
    public function __construct(
36
        $appName,
37
        IRequest $request,
38
        $userId,
39
        IL10N $l10n,
40
        ITagManager $tagManager,
41
        IDBConnection $db,
42
        DbController $DBController,
43
        IRootFolder $rootFolder
44
    )
45
    {
46
        parent::__construct($appName, $request);
47
        $this->appName = $appName;
48
        $this->l10n = $l10n;
49
        $this->userId = $userId;
50
        $this->tagManager = $tagManager;
51
        $this->tagger = null;
52
        $this->db = $db;
53
        $this->DBController = $DBController;
54
        $this->rootFolder = $rootFolder;
0 ignored issues
show
Bug Best Practice introduced by
The property rootFolder does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
55
    }
56
57
    /**
58
     * @NoAdminRequired
59
     * @param $trackid
60
     * @return JSONResponse
61
     */
62
    public function getAudioInfo($trackid)
63
    {
64
65
        $row = $this->DBController->getTrackInfo($trackid);
66
        $artist = $this->DBController->loadArtistsToAlbum($row['album_id'], $row['Album Artist']);
67
        $row['Album Artist'] = $artist;
68
69
        if ($row['Year'] === '0') $row['Year'] = $this->l10n->t('Unknown');
70
        if ($row['Bitrate'] !== '') $row['Bitrate'] = $row['Bitrate'] . ' kbps';
71
72
        array_splice($row, 15, 3);
73
74
        $fileId = $this->DBController->getFileId($trackid);
75
        $nodes = $this->rootFolder->getUserFolder($this->userId)->getById($fileId);
76
        $file = array_shift($nodes);
77
        $row['Path'] = $file->getPath();
78
79
        if ($row['Title']) {
80
            $result = [
81
                'status' => 'success',
82
                'data' => $row];
83
        } else {
84
            $result = [
85
                'status' => 'error',
86
                'data' => 'nodata'];
87
        }
88
        return new JSONResponse($result);
89
    }
90
91
    /**
92
     * @NoAdminRequired
93
     * @param $trackid
94
     * @return JSONResponse
95
     */
96
    public function getPlaylists($trackid)
97
    {
98
        $playlists = $this->DBController->getPlaylistsForTrack($this->userId, $trackid);
99
        if (!empty($playlists)) {
100
            $result = [
101
                'status' => 'success',
102
                'data' => $playlists];
103
        } else {
104
            $result = [
105
                'status' => 'error',
106
                'data' => 'nodata'];
107
        }
108
        return new JSONResponse($result);
109
    }
110
111
}
112