Passed
Pull Request — master (#3505)
by
unknown
05:44
created

FolderModel::getFoldersInfo()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 13
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 24
rs 9.8333
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
26
require_once API_ROOT_PATH . "/Model/Database.php";
27
28
class FolderModel extends Database
29
{
30
    public function getFoldersInfo(array $foldersId): array
31
    {
32
        $rows = $this->select( "SELECT id, title FROM " . prefixTable('nested_tree') . " WHERE nlevel=1" );
33
34
        $ret = [];
35
36
        foreach ($rows as $row) {
37
			$isVisible = in_array((int) $row['id'], $foldersId);
38
            $childrens = $this->getFoldersChildren($row['id'], $foldersId);
39
40
            if ($isVisible || count($childrens) > 0) {
41
                array_push(
42
                    $ret,
43
                    [
44
                        'id' => (int) $row['id'],
45
                        'title' => $row['title'],
46
						'isVisible' => $isVisible,
47
                        'childrens' => $childrens
48
                    ]
49
                );
50
            }
51
        }
52
53
        return $ret;
54
    }
55
56
    private function getFoldersChildren(int $parentId, array $foldersId): array
57
    {
58
        $ret = [];
59
60
        $nbVisible = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $nbVisible is dead and can be removed.
Loading history...
61
62
        $childrens = $this->select('SELECT id, title FROM ' . prefixTable('nested_tree') . ' WHERE parent_id=' . $parentId);
63
64
        if ( count($childrens) > 0) {
65
            foreach ($childrens as $children) {
66
				$isVisible = in_array((int) $children['id'], $foldersId);
67
                $childs = $this->getFoldersChildren($children['id'], $foldersId);
68
69
                if (in_array((int) $children['id'], $foldersId) || count($childs) > 0) {
70
                    array_push(
71
                        $ret,
72
                        [
73
                            'id' => (int) $children['id'],
74
                            'title' => $children['title'],
75
							'isVisible' => $isVisible,
76
                            'childrens' => $childs
77
                        ]
78
                    );
79
                }
80
            }
81
        }
82
83
        return $ret;
84
    }
85
}
86