Passed
Push — master ( 4ffbc4...bfa0fa )
by Andreas
25:06
created

org_openpsa_contacts_handler   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 58.62%

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 51
ccs 17
cts 29
cp 0.5862
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A add_head_elements() 0 5 1
A get_person_schema() 0 23 5
A get_group_tree() 0 14 1
1
<?php
2
/**
3
 * @package org.openpsa.contacts
4
 * @author CONTENT CONTROL http://www.contentcontrol-berlin.de/
5
 * @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/
6
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
7
 */
8
9
/**
10
 * Handler addons
11
 *
12
 * @package org.openpsa.contacts
13
 */
14
trait org_openpsa_contacts_handler
15
{
16
    public function add_head_elements()
17
    {
18
        midcom::get()->uimessages->add_head_elements();
19
        midcom::get()->head->add_jsfile(MIDCOM_STATIC_URL . "/org.openpsa.helpers/editable.js");
20
        org_openpsa_widgets_contact::add_head_elements();
21
    }
22
23 2
    public function get_group_tree() : org_openpsa_widgets_tree
24
    {
25 2
        $root_group = org_openpsa_contacts_interface::find_root_group();
26 2
        $nap = new midcom_helper_nav;
27 2
        $prefix = $nap->get_node($this->_topic->id)[MIDCOM_NAV_ABSOLUTEURL];
28
29 2
        $tree = new org_openpsa_widgets_tree(org_openpsa_contacts_group_dba::class, 'owner');
30 2
        $tree->link_callback = function ($guid) use ($prefix) {
31
            return $prefix . 'group/' . $guid . '/';
32
        };
33 2
        $tree->constraints[] = ['orgOpenpsaObtype', '<', org_openpsa_contacts_group_dba::ORGANIZATION];
34 2
        $tree->root_node = $root_group->id;
35 2
        $tree->title_fields = ['official', 'name'];
36 2
        return $tree;
37
    }
38
39
    /**
40
     * Get schema name for person
41
     */
42 2
    public function get_person_schema(org_openpsa_contacts_person_dba $contact) : string
43
    {
44 2
        $my_company_guid = $this->_config->get('owner_organization');
45
46 2
        if (   empty($my_company_guid)
47 2
            || !mgd_is_guid($my_company_guid)) {
48 2
            if (midcom::get()->auth->admin) {
49
                midcom::get()->uimessages->add(
50
                    $this->_l10n->get($this->_component),
51
                    $this->_l10n->get('owner organization couldnt be found'),
52 2
                    'error'
53
                );
54
            }
55
        } else {
56
            // Figure out if user is from own organization or other org
57
            $person_user = new midcom_core_user($contact->id);
58
59
            if ($person_user->is_in_group("group:{$my_company_guid}")) {
60
                return 'employee';
61
            }
62
        }
63
64 2
        return 'default';
65
    }
66
}
67