Passed
Push — master ( 2dc7fc...e8f302 )
by Andreas
34:28
created

org_openpsa_helpers_list::projects()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 2
nop 0
dl 0
loc 17
ccs 9
cts 9
cp 1
crap 3
rs 9.9666
c 0
b 0
f 0
1
<?php
2
/**
3
 * Collection of list functions for OpenPSA
4
 *
5
 * @package org.openpsa.helpers
6
 * @author Eero af Heurlin, http://www.iki.fi/rambo
7
 * @copyright Nemein Oy, http://www.nemein.com
8
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
9
 */
10
11
/**
12
 * @package org.openpsa.helpers
13
 */
14
class org_openpsa_helpers_list
15
{
16
    /**
17
     * List virtual groups of user
18
     */
19 23
    public static function workgroups(string $add_me = 'last', bool $show_members = false) : array
20
    {
21 23
        if (!midcom::get()->auth->user) {
22
            return [];
23
        }
24 23
        static $cache = [];
25
        // List user's ACL groups for usage in DM arrays
26 23
        $array_name = $add_me . '_' . $show_members;
27 23
        if (!array_key_exists($array_name, $cache)) {
28 2
            $cache[$array_name] = [];
29 2
            if ($add_me == 'first') {
30 1
                $cache[$array_name][midcom::get()->auth->user->id] = midcom::get()->i18n->get_string('me', 'org.openpsa.contacts');
31
            }
32
33 2
            $users_groups = midcom::get()->auth->user->list_memberships();
34 2
            foreach ($users_groups as $key => $group) {
35
                $cache[$array_name][$key] = $group->name;
36
                if ($show_members) {
37
                    foreach ($group->list_members() as $key2 => $person) {
38
                        $cache[$array_name][$key2] = '&nbsp;&nbsp;&nbsp;' . $person->name;
39
                    }
40
                }
41
            }
42
43 2
            asort($cache[$array_name]);
44
45 2
            if ($add_me == 'last') {
46 1
                $cache[$array_name][midcom::get()->auth->user->id] = midcom::get()->i18n->get_string('me', 'org.openpsa.contacts');
47
            }
48
        }
49 23
        return $cache[$array_name];
50
    }
51
}
52