SettingController   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 13
eloc 67
dl 0
loc 151
rs 10
c 0
b 0
f 0

7 Methods

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