Completed
Push — master ( da2115...dd0feb )
by Andreas
18:06
created

midcom_db_person::add_to_group()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 14
nc 3
nop 1
dl 0
loc 18
ccs 0
cts 15
cp 0
crap 12
rs 9.7998
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 195
    public function __construct($id = null)
87
    {
88 195
        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 161
            $this->__mgdschema_class_name__ = midcom::get()->config->get('person_class');
90
        }
91 195
        parent::__construct($id);
92 195
    }
93
94 86
    public function __set($property, $value)
95
    {
96 86
        parent::__set($property, $value);
97
98 86
        if (in_array($property, ['firstname', 'lastname', 'homepage', 'email'])) {
99 15
            $this->_update_computed_members();
100
        }
101 86
    }
102
103
    /**
104
     * Updates all computed members.
105
     */
106 177
    public function _on_loaded()
107
    {
108 177
        $this->_update_computed_members();
109 177
    }
110
111
    /**
112
     * Updates all computed members and adds a midgard:owner privilege for the person itself
113
     * on the record.
114
     */
115 102
    public function _on_created()
116
    {
117 102
        $this->set_privilege('midgard:owner', "user:{$this->guid}");
118
119 102
        $this->_update_computed_members();
120 102
    }
121
122
    /**
123
     * Synchronizes the $name, $rname, $emaillink and $homepagelink members
124
     * with the members they are based on.
125
     */
126 180
    private function _update_computed_members()
127
    {
128 180
        $firstname = trim($this->firstname);
129 180
        $lastname = trim($this->lastname);
130 180
        $this->name = trim("{$firstname} {$lastname}");
131 180
        $this->homepagelink = '';
132 180
        $this->emaillink = '';
133
134 180
        $this->rname = $lastname;
135 180
        if ($this->rname == '') {
136 173
            $this->rname = $firstname;
137 27
        } elseif ($firstname != '') {
138 18
            $this->rname .= ", {$firstname}";
139
        }
140
141 180
        if ($this->name == '') {
142 170
            $this->name = 'person #' . $this->id;
143 170
            $this->rname = 'person #' . $this->id;
144
        }
145
146 180
        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 180
        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 180
    }
158
159 25
    public function get_label()
160
    {
161 25
        return $this->rname;
162
    }
163
164
    /**
165
     * Adds a user to a given Midgard Group. Caller must ensure access permissions
166
     * are right.
167
     *
168
     * @param string $name The name of the group we should be added to.
169
     * @return boolean Indicating success.
170
     *
171
     * @todo Check if user is already assigned to the group.
172
     */
173
    function add_to_group($name)
174
    {
175
        $group = midcom::get()->auth->get_midgard_group_by_name($name);
176
        if (!$group) {
177
            debug_add("Failed to add the person {$this->id} to group {$name}, the group does not exist.", MIDCOM_LOG_WARN);
178
            return false;
179
        }
180
        $storage = $group->get_storage();
181
        $member = new midcom_db_member();
182
        $member->uid = $this->id;
183
        $member->gid = $storage->id;
184
        if (!$member->create()) {
185
            debug_add("Failed to add the person {$this->id} to group {$name}, object could not be created.", MIDCOM_LOG_WARN);
186
            debug_add('Last Midgard error was: ' . midcom_connection::get_error_string(), MIDCOM_LOG_WARN);
187
            debug_print_r('Tried to create this object:', $member);
188
            return false;
189
        }
190
        return true;
191
    }
192
}
193