1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Teampass - a collaborative passwords manager. |
4
|
|
|
* --- |
5
|
|
|
* This library is distributed in the hope that it will be useful, |
6
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
7
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
8
|
|
|
* --- |
9
|
|
|
* |
10
|
|
|
* @project Teampass |
11
|
|
|
* @version API |
12
|
|
|
* |
13
|
|
|
* @file folderModel.php |
14
|
|
|
* --- |
15
|
|
|
* |
16
|
|
|
* @author Nils Laumaillé ([email protected]) |
17
|
|
|
* |
18
|
|
|
* @copyright 2009-2023 Teampass.net |
19
|
|
|
* |
20
|
|
|
* @license https://spdx.org/licenses/GPL-3.0-only.html#licenseText GPL-3.0 |
21
|
|
|
* --- |
22
|
|
|
* |
23
|
|
|
* @see https://www.teampass.net |
24
|
|
|
*/ |
25
|
|
|
require_once API_ROOT_PATH . "/Model/Database.php"; |
26
|
|
|
|
27
|
|
|
class FolderModel extends Database |
28
|
|
|
{ |
29
|
|
|
public function getFoldersInfo(array $foldersId): array |
30
|
|
|
{ |
31
|
|
|
$rows = $this->select( "SELECT id, title FROM " . prefixTable('nested_tree') . " WHERE nlevel=1" ); |
32
|
|
|
|
33
|
|
|
$ret = []; |
34
|
|
|
|
35
|
|
|
foreach ($rows as $row) { |
36
|
|
|
$isVisible = in_array((int) $row['id'], $foldersId); |
37
|
|
|
$childrens = $this->getFoldersChildren($row['id'], $foldersId); |
38
|
|
|
|
39
|
|
|
if ($isVisible || count($childrens) > 0) { |
40
|
|
|
array_push( |
41
|
|
|
$ret, |
42
|
|
|
[ |
43
|
|
|
'id' => (int) $row['id'], |
44
|
|
|
'title' => $row['title'], |
45
|
|
|
'isVisible' => $isVisible, |
46
|
|
|
'childrens' => $childrens |
47
|
|
|
] |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $ret; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
private function getFoldersChildren(int $parentId, array $foldersId): array |
56
|
|
|
{ |
57
|
|
|
$ret = []; |
58
|
|
|
$childrens = $this->select('SELECT id, title FROM ' . prefixTable('nested_tree') . ' WHERE parent_id=' . $parentId); |
59
|
|
|
|
60
|
|
|
if ( count($childrens) > 0) { |
61
|
|
|
foreach ($childrens as $children) { |
62
|
|
|
$isVisible = in_array((int) $children['id'], $foldersId); |
63
|
|
|
$childs = $this->getFoldersChildren($children['id'], $foldersId); |
64
|
|
|
|
65
|
|
|
if (in_array((int) $children['id'], $foldersId) || count($childs) > 0) { |
66
|
|
|
array_push( |
67
|
|
|
$ret, |
68
|
|
|
[ |
69
|
|
|
'id' => (int) $children['id'], |
70
|
|
|
'title' => $children['title'], |
71
|
|
|
'isVisible' => $isVisible, |
72
|
|
|
'childrens' => $childs |
73
|
|
|
] |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $ret; |
80
|
|
|
} |
81
|
|
|
} |