Failed Conditions
Pull Request — master (#72)
by Sander
01:36
created

NotebookMapper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 3
nc 1
nop 2
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 View Code Duplication
	public function find($notebook_id, $user_id = null, $deleted = false) {
41
		$params = [];
42
		$where = [];
43
		if($notebook_id){
44
			$where[] = 'g.id= ?';
45
			$params[] = $notebook_id;
46
		}
47
48
		if ($user_id) {
49
			$params[] = $user_id;
50
			$where[] = 'g.uid = ?';
51
		}
52
53
		if ($deleted !== false) {
54
			$params[] = $deleted;
55
			$where[] = 'g.deleted = ?';
56
		}
57
		$where = implode(' AND ', $where);
58
		if($where){
59
			$where = 'WHERE '. $where;
60
		}
61
		$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";
62
		$results = [];
63
		foreach ($this->execute($sql, $params)->fetchAll() as $item) {
64
			$results[] = $this->makeEntityFromDBResult($item);
65
		}
66
//		var_dump($results);
67
		if(count($results) === 1){
68
			return reset($results);
69
		}
70
71
		return $results;
72
	}
73
74
75 View Code Duplication
	public function findByName($group_name, $user_id = null, $deleted = false) {
76
		$params = [];
77
		$where = [];
78
		if($group_name){
79
			$where[] = 'g.name = ?';
80
			$params[] = $group_name;
81
		}
82
83
		if ($user_id) {
84
			$params[] = $user_id;
85
			$where[] = 'g.uid = ?';
86
		}
87
88
		if ($deleted !== false) {
89
			$params[] = $deleted;
90
			$where[] = 'g.deleted = ?';
91
		}
92
		$where = implode(' AND ', $where);
93
		if($where){
94
			$where = 'WHERE '. $where;
95
		}
96
		$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";
97
		$results = [];
98
		foreach ($this->execute($sql, $params)->fetchAll() as $item) {
99
			$results[] = $this->makeEntityFromDBResult($item);
100
		}
101
102
		if(count($results) === 1){
103
			return reset($results);
104
		}
105
106
		return $results;
107
	}
108
109
   	/**
110
	 * Creates a group
111
	 *
112
	 * @param Notebook|Entity $group
113
	 * @return Note|Entity
114
	 * @internal param $userId
115
	 */
116
	public function insert(Entity $group) {
117
		$group->setNoteCount(null);
118
		return parent::insert($group);
119
	}
120
121
	/**
122
	 * Update group
123
	 *
124
	 * @param Notebook|Entity $group
125
	 * @return Notebook|Entity
126
	 */
127
	public function update(Entity $group) {
128
		$group->setNoteCount(null);
129
		return parent::update($group);
130
	}
131
132
	/**
133
	 * Delete group
134
	 *
135
	 * @param Notebook|Entity $group
136
	 * @return Notebook|Entity
137
	 */
138
	public function delete(Entity $group) {
139
		return parent::delete($group);
140
	}
141
142
	/**
143
	 * @param $arr
144
	 * @return Notebook
145
	 */
146
	public function makeEntityFromDBResult($arr) {
147
		$group = new Notebook();
148
		$group->setId($arr['id']);
149
		$group->setName($arr['name']);
150
		$group->setGuid($arr['guid']);
151
		$group->setParentId($arr['parent_id']);
152
		$group->setColor($arr['color']);
153
		$group->setNoteCount($arr['note_count']);
154
		$group->setDeleted($arr['deleted']);
155
156
		return $group;
157
	}
158
159
160
}
161