brantje /
nextnote
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Nextcloud - NextNote |
||
| 4 | * |
||
| 5 | * |
||
| 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 | |||
| 32 | class NotebookMapper extends Mapper { |
||
| 33 | private $utils; |
||
| 34 | |||
| 35 | public function __construct(IDBConnection $db, Utils $utils) { |
||
| 36 | parent::__construct($db, 'nextnote_groups'); |
||
| 37 | $this->utils = $utils; |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Get Notebook(s) |
||
| 42 | * @param null|int $notebook_id |
||
| 43 | * @param null|int $user_id |
||
| 44 | * @param bool|int $deleted |
||
| 45 | * @return Notebook[]|Notebook |
||
| 46 | */ |
||
| 47 | View Code Duplication | public function find($notebook_id = null, $user_id = null, $deleted = false) { |
|
| 48 | $qb = $this->db->getQueryBuilder(); |
||
| 49 | $qb->select('g.*', 'g.guid as guid', $qb->createFunction('COUNT(n.id) as note_count')) //'COUNT(n.id) as note_count' |
||
| 50 | ->from('nextnote_groups', 'g') |
||
| 51 | ->leftJoin('g','nextnote_notes','n', $qb->expr()->eq('g.id', 'n.notebook'))->groupBy(['g.id']); |
||
| 52 | |||
| 53 | $where = []; |
||
| 54 | if($notebook_id){ |
||
|
0 ignored issues
–
show
|
|||
| 55 | $where['g.id'] = $notebook_id; |
||
| 56 | } |
||
| 57 | |||
| 58 | if ($user_id !== null) { |
||
| 59 | $where['g.uid'] = $user_id; |
||
| 60 | } |
||
| 61 | |||
| 62 | if ($deleted !== false) { |
||
| 63 | $where['g.deleted'] = $deleted; |
||
| 64 | } |
||
| 65 | $i = 0; |
||
| 66 | foreach ($where as $field => $value){ |
||
| 67 | if($i === 0){ |
||
| 68 | $qb->where($qb->expr()->eq($field, $qb->createNamedParameter($value))); |
||
| 69 | } else { |
||
| 70 | $qb->andWhere($qb->expr()->eq($field, $qb->createNamedParameter($value))); |
||
| 71 | } |
||
| 72 | $i++; |
||
| 73 | } |
||
| 74 | |||
| 75 | $result = $qb->execute(); |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var $results Notebook[] |
||
| 79 | */ |
||
| 80 | $results = []; |
||
| 81 | while ($item = $result->fetch()) { |
||
| 82 | $results[] = $this->makeEntityFromDBResult($item); |
||
| 83 | } |
||
| 84 | $result->closeCursor(); |
||
| 85 | return $results; |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @param $group_name |
||
| 90 | * @param null $user_id |
||
| 91 | * @param bool $deleted |
||
| 92 | * @return Notebook[]|Notebook |
||
| 93 | */ |
||
| 94 | View Code Duplication | public function findByName($group_name, $user_id = null, $deleted = false) { |
|
| 95 | $qb = $this->db->getQueryBuilder(); |
||
| 96 | $qb->select('g.*', 'g.guid as guid', $qb->createFunction('COUNT(n.id) as note_count')) //'COUNT(n.id) as note_count' |
||
| 97 | ->from('nextnote_groups', 'g') |
||
| 98 | ->leftJoin('g','nextnote_notes','n', $qb->expr()->eq('g.id', 'n.notebook'))->groupBy(['g.id']); |
||
| 99 | |||
| 100 | $where = []; |
||
| 101 | if($group_name){ |
||
| 102 | $where['g.name'] = $group_name; |
||
| 103 | } |
||
| 104 | |||
| 105 | if ($user_id !== null) { |
||
| 106 | $where['g.uid'] = $user_id; |
||
| 107 | } |
||
| 108 | |||
| 109 | if ($deleted !== false) { |
||
| 110 | $where['g.deleted'] = $deleted; |
||
| 111 | } |
||
| 112 | $i = 0; |
||
| 113 | foreach ($where as $field => $value){ |
||
| 114 | if($i === 0){ |
||
| 115 | $qb->where($qb->expr()->eq($field, $qb->createNamedParameter($value))); |
||
| 116 | } else { |
||
| 117 | $qb->andWhere($qb->expr()->eq($field, $qb->createNamedParameter($value))); |
||
| 118 | } |
||
| 119 | $i++; |
||
| 120 | } |
||
| 121 | |||
| 122 | $result = $qb->execute(); |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @var $results Notebook[] |
||
| 126 | */ |
||
| 127 | $results = []; |
||
| 128 | while ($item = $result->fetch()) { |
||
| 129 | $results[] = $this->makeEntityFromDBResult($item); |
||
| 130 | } |
||
| 131 | $result->closeCursor(); |
||
| 132 | return $results; |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Creates a group |
||
| 137 | * |
||
| 138 | * @param Notebook|Entity $group |
||
| 139 | * @return Note|Entity |
||
| 140 | * @internal param $userId |
||
| 141 | */ |
||
| 142 | public function insert(Entity $group) { |
||
| 143 | $group->setNoteCount(null); |
||
| 144 | return parent::insert($group); |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Update group |
||
| 149 | * |
||
| 150 | * @param Notebook|Entity $group |
||
| 151 | * @return Notebook|Entity |
||
| 152 | */ |
||
| 153 | public function update(Entity $group) { |
||
| 154 | $group->setNoteCount(null); |
||
| 155 | return parent::update($group); |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Delete group |
||
| 160 | * |
||
| 161 | * @param Notebook|Entity $group |
||
| 162 | * @return Notebook|Entity |
||
| 163 | */ |
||
| 164 | public function delete(Entity $group) { |
||
| 165 | return parent::delete($group); |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @param $arr |
||
| 170 | * @return Notebook |
||
| 171 | */ |
||
| 172 | public function makeEntityFromDBResult($arr) { |
||
| 173 | $group = new Notebook(); |
||
| 174 | $group->setId($arr['id']); |
||
| 175 | $group->setName($arr['name']); |
||
| 176 | $group->setGuid($arr['guid']); |
||
| 177 | $group->setParentId($arr['parent_id']); |
||
| 178 | $group->setColor($arr['color']); |
||
| 179 | $group->setNoteCount((int) $arr['note_count']); |
||
| 180 | $group->setDeleted($arr['deleted']); |
||
| 181 | |||
| 182 | return $group; |
||
| 183 | } |
||
| 184 | |||
| 185 | |||
| 186 | } |
||
| 187 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: