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-2021 Marcel Scherello |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace OCA\audioplayer\Controller; |
13
|
|
|
|
14
|
|
|
use OCP\AppFramework\Controller; |
15
|
|
|
use OCP\AppFramework\Http\JSONResponse; |
16
|
|
|
use OCP\IRequest; |
17
|
|
|
use OCP\IConfig; |
18
|
|
|
use OCP\Files\IRootFolder; |
19
|
|
|
use OCP\ITagManager; |
20
|
|
|
use OCP\IDBConnection; |
21
|
|
|
use OCP\ISession; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Controller class for main page. |
25
|
|
|
*/ |
26
|
|
|
class SettingController extends Controller { |
27
|
|
|
|
28
|
|
|
private $userId; |
29
|
|
|
private $config; |
30
|
|
|
private $rootFolder; |
31
|
|
|
private $tagger; |
32
|
|
|
private $tagManager; |
33
|
|
|
private $db; |
34
|
|
|
private $session; |
35
|
|
|
private $DBController; |
36
|
|
|
|
37
|
|
|
public function __construct( |
38
|
|
|
$appName, |
39
|
|
|
IRequest $request, |
40
|
|
|
$userId, |
41
|
|
|
IConfig $config, |
42
|
|
|
IDBConnection $db, |
43
|
|
|
ITagManager $tagManager, |
44
|
|
|
IRootFolder $rootFolder, |
45
|
|
|
ISession $session, |
46
|
|
|
DbController $DBController |
47
|
|
|
) |
48
|
|
|
{ |
49
|
|
|
parent::__construct($appName, $request); |
50
|
|
|
$this->appName = $appName; |
51
|
|
|
$this->userId = $userId; |
52
|
|
|
$this->config = $config; |
53
|
|
|
$this->db = $db; |
54
|
|
|
$this->tagManager = $tagManager; |
55
|
|
|
$this->tagger = null; |
56
|
|
|
$this->rootFolder = $rootFolder; |
57
|
|
|
$this->session = $session; |
58
|
|
|
$this->DBController = $DBController; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param $type |
63
|
|
|
* @param $value |
64
|
|
|
* @return JSONResponse |
65
|
|
|
*/ |
66
|
|
|
public function admin($type, $value) |
67
|
|
|
{ |
68
|
|
|
//\OCP\Util::writeLog('audioplayer', 'settings save: '.$type.$value, \OCP\Util::DEBUG); |
69
|
|
|
$this->config->setAppValue($this->appName, $type, $value); |
70
|
|
|
return new JSONResponse(array('success' => 'true')); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @NoAdminRequired |
75
|
|
|
* @param $type |
76
|
|
|
* @param $value |
77
|
|
|
* @return JSONResponse |
78
|
|
|
* @throws \OCP\PreConditionNotMetException |
79
|
|
|
*/ |
80
|
|
|
public function setValue($type, $value) { |
81
|
|
|
//\OCP\Util::writeLog('audioplayer', 'settings save: '.$type.$value, \OCP\Util::DEBUG); |
82
|
|
|
$this->config->setUserValue($this->userId, $this->appName, $type, $value); |
83
|
|
|
return new JSONResponse(array('success' => 'true')); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @NoAdminRequired |
88
|
|
|
* @param $type |
89
|
|
|
* @return JSONResponse |
90
|
|
|
*/ |
91
|
|
|
public function getValue($type) { |
92
|
|
|
$value = $this->config->getUserValue($this->userId, $this->appName, $type); |
93
|
|
|
|
94
|
|
|
//\OCP\Util::writeLog('audioplayer', 'settings load: '.$type.$value, \OCP\Util::DEBUG); |
95
|
|
|
|
96
|
|
|
if ($value !== '') { |
97
|
|
|
$result = [ |
98
|
|
|
'status' => 'success', |
99
|
|
|
'value' => $value |
100
|
|
|
]; |
101
|
|
|
} else { |
102
|
|
|
$result = [ |
103
|
|
|
'status' => 'false', |
104
|
|
|
'value' =>'nodata' |
105
|
|
|
]; |
106
|
|
|
} |
107
|
|
|
return new JSONResponse($result); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @NoAdminRequired |
112
|
|
|
* @param $value |
113
|
|
|
* @return JSONResponse |
114
|
|
|
* @throws \OCP\PreConditionNotMetException |
115
|
|
|
*/ |
116
|
|
|
public function userPath($value) { |
117
|
|
|
$path = $value; |
118
|
|
|
try { |
119
|
|
|
$this->rootFolder->getUserFolder($this -> userId)->get($path); |
120
|
|
|
} catch (\OCP\Files\NotFoundException $e) { |
121
|
|
|
return new JSONResponse(array('success' => false)); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
if ($path[0] !== '/') { |
125
|
|
|
$path = '/'.$path; |
126
|
|
|
} |
127
|
|
|
if ($path[strlen($path) - 1] !== '/') { |
128
|
|
|
$path .= '/'; |
129
|
|
|
} |
130
|
|
|
$this->config->setUserValue($this->userId, $this->appName, 'path', $path); |
131
|
|
|
return new JSONResponse(array('success' => true)); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @NoAdminRequired |
136
|
|
|
* @param $trackid |
137
|
|
|
* @param $isFavorite |
138
|
|
|
* @return bool |
139
|
|
|
*/ |
140
|
|
|
public function setFavorite($trackid, $isFavorite) |
141
|
|
|
{ |
142
|
|
|
$this->tagger = $this->tagManager->load('files'); |
143
|
|
|
$fileId = $this->DBController->getFileId($trackid); |
144
|
|
|
|
145
|
|
|
if ($isFavorite === "true") { |
146
|
|
|
$return = $this->tagger->removeFromFavorites($fileId); |
147
|
|
|
} else { |
148
|
|
|
$return = $this->tagger->addToFavorites($fileId); |
149
|
|
|
} |
150
|
|
|
return $return; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @NoAdminRequired |
155
|
|
|
* @param $track_id |
156
|
|
|
* @return int|string |
157
|
|
|
* @throws \Exception |
158
|
|
|
*/ |
159
|
|
|
public function setStatistics($track_id) { |
160
|
|
|
$date = new \DateTime(); |
161
|
|
|
$playtime = $date->getTimestamp(); |
162
|
|
|
|
163
|
|
|
$SQL='SELECT `id`, `playcount` FROM `*PREFIX*audioplayer_stats` WHERE `user_id`= ? AND `track_id`= ?'; |
164
|
|
|
$stmt = $this->db->prepare($SQL); |
165
|
|
|
$stmt->execute(array($this->userId, $track_id)); |
166
|
|
|
$row = $stmt->fetch(); |
|
|
|
|
167
|
|
|
if (isset($row['id'])) { |
168
|
|
|
$playcount = $row['playcount'] + 1; |
169
|
|
|
$stmt = $this->db->prepare( 'UPDATE `*PREFIX*audioplayer_stats` SET `playcount`= ?, `playtime`= ? WHERE `id` = ?'); |
170
|
|
|
$stmt->execute(array($playcount, $playtime, $row['id'])); |
171
|
|
|
return 'update'; |
172
|
|
|
} else { |
173
|
|
|
$stmt = $this->db->prepare( 'INSERT INTO `*PREFIX*audioplayer_stats` (`user_id`,`track_id`,`playtime`,`playcount`) VALUES(?,?,?,?)' ); |
174
|
|
|
$stmt->execute(array($this->userId, $track_id, $playtime, 1)); |
175
|
|
|
return $this->db->lastInsertId('*PREFIX*audioplayer_stats'); |
|
|
|
|
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
} |
180
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.