Passed
Branch wip_sessions (2e0cc8)
by Nils
04:59
created

FolderModel::getFoldersInfo()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 3
nop 1
dl 0
loc 24
rs 9.8333
c 0
b 0
f 0
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
}