Completed
Push — master ( 172550...380074 )
by Sander
10s
created

NotebookMapper::find()   C

Complexity

Conditions 7
Paths 64

Size

Total Lines 36
Code Lines 22

Duplication

Lines 36
Ratio 100 %

Importance

Changes 0
Metric Value
dl 36
loc 36
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 22
nc 64
nop 3
1
<?php
2
/**
3
 * Nextcloud - NextNote
4
 *
5
 * @copyright Copyright (c) 2015, Ben Curtis <[email protected]>
6
 * @copyright Copyright (c) 2017, Sander Brand ([email protected])
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\NextNote\Db;
25
26
use \OCA\NextNote\Utility\Utils;
27
use OCP\AppFramework\Db\Entity;
28
use OCP\IDBConnection;
29
use OCP\AppFramework\Db\Mapper;
30
31
class NotebookMapper extends Mapper {
32
	private $utils;
33
34
	public function __construct(IDBConnection $db, Utils $utils) {
35
		parent::__construct($db, 'nextnote_groups');
36
		$this->utils = $utils;
37
	}
38
39
	/**
40
	 * Get Notebook(s)
41
	 * @param int $notebook_id
42
	 * @param null|int $user_id
43
	 * @param bool|int $deleted
44
	 * @return Notebook[]|Notebook
45
	 */
46 View Code Duplication
	public function find($notebook_id, $user_id = null, $deleted = false) {
47
		$params = [];
48
		$where = [];
49
		if($notebook_id){
50
			$where[] = 'g.id= ?';
51
			$params[] = $notebook_id;
52
		}
53
54
		if ($user_id !== null) {
55
			$params[] = $user_id;
56
			$where[] = 'g.uid = ?';
57
		}
58
59
		if ($deleted !== false) {
60
			$params[] = $deleted;
61
			$where[] = 'g.deleted = ?';
62
		}
63
		$where = implode(' AND ', $where);
64
		if($where){
65
			$where = 'WHERE '. $where;
66
		}
67
		$sql = "SELECT g.*, g.guid as guid, COUNT(n.id) as note_count FROM *PREFIX*nextnote_groups g LEFT JOIN *PREFIX*nextnote_notes n ON g.name=n.grouping $where  GROUP BY g.id";
68
		/**
69
		 * @var $results Notebook[]
70
		 */
71
		$results = [];
72
		foreach ($this->execute($sql, $params)->fetchAll() as $item) {
73
			$results[] = $this->makeEntityFromDBResult($item);
74
		}
75
//		var_dump($results);
76
		if(count($results) === 1){
77
			return reset($results);
78
		}
79
80
		return $results;
81
	}
82
83
	/**
84
	 * @param $group_name
85
	 * @param null $user_id
86
	 * @param bool $deleted
87
	 * @return Notebook[]|Notebook
88
	 */
89 View Code Duplication
	public function findByName($group_name, $user_id = null, $deleted = false) {
90
		$params = [];
91
		$where = [];
92
		if($group_name){
93
			$where[] = 'g.name = ?';
94
			$params[] = $group_name;
95
		}
96
97
		if ($user_id) {
98
			$params[] = $user_id;
99
			$where[] = 'g.uid = ?';
100
		}
101
102
		if ($deleted !== false) {
103
			$params[] = $deleted;
104
			$where[] = 'g.deleted = ?';
105
		}
106
		$where = implode(' AND ', $where);
107
		if($where){
108
			$where = 'WHERE '. $where;
109
		}
110
		$sql = "SELECT g.*, COUNT(n.id) as note_count FROM *PREFIX*nextnote_groups g LEFT JOIN *PREFIX*nextnote_notes n ON g.name=n.grouping $where  GROUP BY g.id";
111
		/**
112
		 * @var $results Notebook[]
113
		 */
114
		$results = [];
115
		foreach ($this->execute($sql, $params)->fetchAll() as $item) {
116
			$results[] = $this->makeEntityFromDBResult($item);
117
		}
118
119
		if(count($results) === 1){
120
			return reset($results);
121
		}
122
123
		return $results;
124
	}
125
126
   	/**
127
	 * Creates a group
128
	 *
129
	 * @param Notebook|Entity $group
130
	 * @return Note|Entity
131
	 * @internal param $userId
132
	 */
133
	public function insert(Entity $group) {
134
		$group->setNoteCount(null);
135
		return parent::insert($group);
136
	}
137
138
	/**
139
	 * Update group
140
	 *
141
	 * @param Notebook|Entity $group
142
	 * @return Notebook|Entity
143
	 */
144
	public function update(Entity $group) {
145
		$group->setNoteCount(null);
146
		return parent::update($group);
147
	}
148
149
	/**
150
	 * Delete group
151
	 *
152
	 * @param Notebook|Entity $group
153
	 * @return Notebook|Entity
154
	 */
155
	public function delete(Entity $group) {
156
		return parent::delete($group);
157
	}
158
159
	/**
160
	 * @param $arr
161
	 * @return Notebook
162
	 */
163
	public function makeEntityFromDBResult($arr) {
164
		$group = new Notebook();
165
		$group->setId($arr['id']);
166
		$group->setName($arr['name']);
167
		$group->setGuid($arr['guid']);
168
		$group->setParentId($arr['parent_id']);
169
		$group->setColor($arr['color']);
170
		$group->setNoteCount($arr['note_count']);
171
		$group->setDeleted($arr['deleted']);
172
173
		return $group;
174
	}
175
176
177
}
178