Passed
Push — master ( 11084f...ddfcc7 )
by Andreas
23:39
created

org_openpsa_contacts_group_dba::__set()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4.074

Importance

Changes 0
Metric Value
cc 4
eloc 5
nc 2
nop 2
dl 0
loc 8
ccs 5
cts 6
cp 0.8333
crap 4.074
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package org.openpsa.contacts
4
 * @author Nemein Oy http://www.nemein.com/
5
 * @copyright Nemein Oy http://www.nemein.com/
6
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
7
 */
8
9
/**
10
 * @property string $name Path name of the group
11
 * @property string $official Official name of the group
12
 * @property string $street Street address of the group
13
 * @property string $postcode Zip code of the group
14
 * @property string $city City of the group
15
 * @property string $country Country of the group
16
 * @property string $homepage Homepage URL of the group
17
 * @property string $email Email of the group
18
 * @property string $phone Phone number of the group
19
 * @property string $fax Fax number of the group
20
 * @property string $extra Additional information about the group
21
 * @property integer $owner Group the group is under
22
 * @property integer $orgOpenpsaObtype
23
 * @property string $postalStreet
24
 * @property string $postalPostcode
25
 * @property string $postalCity
26
 * @property string $postalCountry
27
 * @property string $invoiceStreet
28
 * @property string $invoicePostcode
29
 * @property string $invoiceCity
30
 * @property string $invoiceCountry
31
 * @property string $keywords
32
 * @property integer $invoiceDue
33
 * @property integer $invoiceVat
34
 * @property string $invoiceDistribution
35
 * @property string $vatNo
36
 * @package org.openpsa.contacts
37
 */
38
class org_openpsa_contacts_group_dba extends midcom_core_dbaobject
39
{
40
    public string $__midcom_class_name__ = __CLASS__;
41
    public string $__mgdschema_class_name__ = 'org_openpsa_organization';
42
43
    public array $autodelete_dependents = [
44
        org_openpsa_contacts_member_dba::class => 'gid'
45
    ];
46
47
    const OTHERGROUP = 0;
48
    const ORGANIZATION = 1000;
49
    const DAUGHTER = 1001;
50
    const DEPARTMENT = 1002;
51
52
    private array $members = [];
53
    private bool $_members_loaded = false;
54
    private array $_address_extras = [];
55
56 9
    public function get_label() : string
57
    {
58 9
        return $this->official ?: $this->name;
59
    }
60
61
    public function render_link() : string
62
    {
63
        $siteconfig = new org_openpsa_core_siteconfig();
64
65
        if ($contacts_url = $siteconfig->get_node_full_url('org.openpsa.contacts')) {
66
            return '<a href="' . $contacts_url . 'group/' . $this->guid . '/">' . $this->get_label() . "</a>";
67
        }
68
        return $this->get_label();
69
    }
70
71 1
    private function _get_address_extra(string $property)
72
    {
73 1
        $return = $this->get_parameter('midcom.helper.datamanager2', $property) ?: $this->get_label();
74 1
        $this->_address_extras[$property] = $return;
75
    }
76
77 20
    public function __get($property)
78
    {
79 20
        if (in_array($property, ['invoice_label', 'postal_label'])) {
80 1
            if (!isset($this->_address_extras[$property])) {
81 1
                $this->_get_address_extra($property);
82
            }
83 1
            return $this->_address_extras[$property];
84
        }
85 20
        return parent::__get($property);
86
    }
87
88 11
    public function _on_loaded()
89
    {
90 11
        if (empty($this->official)) {
91 11
            $this->official = $this->name ?: "Group #{$this->id}";
92
        }
93
    }
94
95
    public function get_members() : array
96
    {
97
        if (!$this->_members_loaded) {
98
            $mc = midcom_db_member::new_collector('gid', $this->id);
99
            $this->members = array_fill_keys($mc->get_values('uid'), true);
100
            $this->_members_loaded = true;
101
        }
102
        return $this->members;
103
    }
104
}
105