Passed
Push — master ( 1f91ec...0204e8 )
by Nils
04:11
created

FolderModel::getFoldersChildren()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 15
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 28
rs 9.4555
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
59
        $nbVisible = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $nbVisible is dead and can be removed.
Loading history...
60
61
        $childrens = $this->select('SELECT id, title FROM ' . prefixTable('nested_tree') . ' WHERE parent_id=' . $parentId);
62
63
        if ( count($childrens) > 0) {
64
            foreach ($childrens as $children) {
65
				$isVisible = in_array((int) $children['id'], $foldersId);
66
                $childs = $this->getFoldersChildren($children['id'], $foldersId);
67
68
                if (in_array((int) $children['id'], $foldersId) || count($childs) > 0) {
69
                    array_push(
70
                        $ret,
71
                        [
72
                            'id' => (int) $children['id'],
73
                            'title' => $children['title'],
74
							'isVisible' => $isVisible,
75
                            'childrens' => $childs
76
                        ]
77
                    );
78
                }
79
            }
80
        }
81
82
        return $ret;
83
    }
84
}