Passed
Push — master ( 3657f2...b17fdb )
by Andreas
24:28 queued 05:07
created

midcom_db_person::__set()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package midcom.db
4
 * @author The Midgard Project, http://www.midgard-project.org
5
 * @copyright The Midgard Project, http://www.midgard-project.org
6
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
7
 */
8
9
/**
10
 * MidCOM level replacement for the Midgard Person record with framework support.
11
 *
12
 * @property string $firstname First name of the person
13
 * @property string $lastname Last name of the person
14
 * @property string $homephone Home phone number of the person
15
 * @property string $handphone Cell phone number of the person
16
 * @property string $workphone Work phone name of the person
17
 * @property string $homepage Homepage URL of the person
18
 * @property string $email Email address of the person
19
 * @property string $street Street address of the person
20
 * @property string $postcode Zip code of the person
21
 * @property string $city City of the person
22
 * @property string $extra Additional information about the person
23
 * @property integer $salutation
24
 * @property string $title
25
 * @property midgard_datetime $birthdate
26
 * @property string $pgpkey
27
 * @property string $country
28
 * @property string $fax
29
 * @property integer $orgOpenpsaAccesstype
30
 * @package midcom.db
31
 */
32
class midcom_db_person extends midcom_core_dbaobject
33
{
34
    public $__midcom_class_name__ = __CLASS__;
35
    public $__mgdschema_class_name__ = 'midgard_person';
36
37
    public $autodelete_dependents = [
38
        midcom_db_member::class => 'uid'
39
    ];
40
41
    /**
42
     * Read-Only variable, consisting of "$firstname $lastname".
43
     *
44
     * Updated during all DB operations.
45
     *
46
     * @var string
47
     */
48
    public $name = '';
49
50
    /**
51
     * Read-Only variable, consisting of "$lastname, $firstname".
52
     *
53
     * Updated during all DB operations.
54
     *
55
     * @var string
56
     */
57
    public $rname = '';
58
59
    /**
60
     * Read-Only variable, consisting of a complete A HREF tag to homepage
61
     * if set.
62
     *
63
     * Updated during all DB operations.
64
     *
65
     * @var string
66
     */
67
    public $homepagelink = '';
68
69
    /**
70
     * Read-Only variable, consisting of a complete mailto A HREF tag to
71
     * the set email address.
72
     *
73
     * Updated during all DB operations.
74
     *
75
     * @var string
76
     */
77
    public $emaillink = '';
78
79
    /**
80
     * The default constructor will create an empty object. Optionally, you can pass
81
     * an object ID or GUID to the object which will then initialize the object with
82
     * the corresponding DB instance.
83
     *
84
     * @param mixed $id A valid object ID or GUID, omit for an empty object.
85
     */
86 193
    public function __construct($id = null)
87
    {
88 193
        if ($this->__mgdschema_class_name__ == 'midgard_person') {
0 ignored issues
show
introduced by
The condition $this->__mgdschema_class...e__ == 'midgard_person' is always false.
Loading history...
89 167
            $this->__mgdschema_class_name__ = midcom::get()->config->get('person_class');
90
        }
91 193
        parent::__construct($id);
92 193
    }
93
94 92
    public function __set($property, $value)
95
    {
96 92
        parent::__set($property, $value);
97
98 92
        if (in_array($property, ['firstname', 'lastname', 'homepage', 'email'])) {
99 17
            $this->_update_computed_members();
100
        }
101 92
    }
102
103
    /**
104
     * Updates all computed members.
105
     */
106 174
    public function _on_loaded()
107
    {
108 174
        $this->_update_computed_members();
109 174
    }
110
111
    /**
112
     * Updates all computed members and adds a midgard:owner privilege for the person itself
113
     * on the record.
114
     */
115 100
    public function _on_created()
116
    {
117 100
        $this->set_privilege('midgard:owner', "user:{$this->guid}");
118
119 100
        $this->_update_computed_members();
120 100
    }
121
122
    /**
123
     * Synchronizes the $name, $rname, $emaillink and $homepagelink members
124
     * with the members they are based on.
125
     */
126 177
    private function _update_computed_members()
127
    {
128 177
        $firstname = trim($this->firstname);
129 177
        $lastname = trim($this->lastname);
130 177
        $this->name = trim("{$firstname} {$lastname}");
131 177
        $this->homepagelink = '';
132 177
        $this->emaillink = '';
133
134 177
        $this->rname = $lastname;
135 177
        if ($this->rname == '') {
136 172
            $this->rname = $firstname;
137 26
        } elseif ($firstname != '') {
138 17
            $this->rname .= ", {$firstname}";
139
        }
140
141 177
        if ($this->name == '') {
142 169
            $this->name = 'person #' . $this->id;
143 169
            $this->rname = 'person #' . $this->id;
144
        }
145
146 177
        if ($this->homepage != '') {
147 1
            $title = htmlspecialchars($this->name);
148 1
            $url = htmlspecialchars($this->homepage);
149 1
            $this->homepagelink = "<a href=\"{$url}\" title=\"{$title}\">{$url}</a>";
150
        }
151
152 177
        if ($this->email != '') {
153 10
            $title = htmlspecialchars($this->name);
154 10
            $url = htmlspecialchars($this->email);
155 10
            $this->emaillink = "<a href=\"mailto:{$url}\" title=\"{$title}\">{$url}</a>";
156
        }
157 177
    }
158
159 4
    public function get_label_property() : string
160
    {
161 4
        return 'rname';
162
    }
163
164 26
    public function get_label() : string
165
    {
166 26
        return $this->rname;
167
    }
168
169
    /**
170
     * Adds a user to a given Midgard Group. Caller must ensure access permissions
171
     * are right.
172
     *
173
     * @param string $name The name of the group we should be added to.
174
     * @return boolean Indicating success.
175
     *
176
     * @todo Check if user is already assigned to the group.
177
     */
178
    function add_to_group($name)
179
    {
180
        $group = midcom::get()->auth->get_midgard_group_by_name($name);
181
        if (!$group) {
182
            debug_add("Failed to add the person {$this->id} to group {$name}, the group does not exist.", MIDCOM_LOG_WARN);
183
            return false;
184
        }
185
        $storage = $group->get_storage();
186
        $member = new midcom_db_member();
187
        $member->uid = $this->id;
188
        $member->gid = $storage->id;
189
        if (!$member->create()) {
190
            debug_add("Failed to add the person {$this->id} to group {$name}, object could not be created.", MIDCOM_LOG_WARN);
191
            debug_add('Last Midgard error was: ' . midcom_connection::get_error_string(), MIDCOM_LOG_WARN);
192
            debug_print_r('Tried to create this object:', $member);
193
            return false;
194
        }
195
        return true;
196
    }
197
}
198