Passed
Branch feature/PHP8.2 (fa54d6)
by Michael
09:03
created

ProfileProfileHandler::loadFields()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
/**
3
 * Extended User Profile
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
13
 * @license             GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html)
14
 * @package             profile
15
 * @since               2.3.0
16
 * @author              Jan Pedersen
17
 * @author              Taiwen Jiang <[email protected]>
18
 */
19
20
// defined('XOOPS_ROOT_PATH') || exit("XOOPS root path not defined");
21
22
/**
23
 * @package             kernel
24
 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
25
 */
26
class ProfileProfile extends XoopsObject
27
{
28
    //PHP 8.2 Dynamic properties deprecated
29
    public $profile_id;
30
    public $handler;
31
32
    /**
33
     * @param $fields
34
     */
35
    public function __construct($fields)
36
    {
37
        $this->initVar('profile_id', XOBJ_DTYPE_INT, null, true);
38
        $this->init($fields);
39
    }
40
41
    /**
42
     * Initiate variables
43
     * @param array $fields field information array of {@link XoopsProfileField} objects
44
     */
45
    public function init($fields)
46
    {
47
        if (is_array($fields) && count($fields) > 0) {
48
            foreach (array_keys($fields) as $key) {
49
                $this->initVar($key, $fields[$key]->getVar('field_valuetype'), $fields[$key]->getVar('field_default', 'n'), $fields[$key]->getVar('field_required'), $fields[$key]->getVar('field_maxlength'));
50
            }
51
        }
52
    }
53
}
54
55
/**
56
 * @package             kernel
57
 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
58
 */
59
class ProfileProfileHandler extends XoopsPersistableObjectHandler
60
{
61
    /**
62
     * holds reference to {@link profileFieldHandler} object
63
     */
64
    public $_fHandler;
65
66
    /**
67
     * Array of {@link XoopsProfileField} objects
68
     * @var array
69
     */
70
    public $_fields = array();
71
72
    /**
73
     * @param null|XoopsDatabase $db
74
     */
75
    public function __construct(XoopsDatabase $db)
76
    {
77
        parent::__construct($db, 'profile_profile', 'profileprofile', 'profile_id');
78
        $this->_fHandler = xoops_getModuleHandler('field', 'profile');
79
    }
80
81
    /**
82
     * create a new {@link ProfileProfile}
83
     *
84
     * @param bool $isNew Flag the new objects as "new"?
85
     *
86
     * @return object {@link ProfileProfile}
87
     */
88
    public function create($isNew = true)
89
    {
90
        $obj          = new $this->className($this->loadFields());
91
        $obj->handler = $this;
92
        if ($isNew === true) {
93
            $obj->setNew();
94
        }
95
96
        return $obj;
97
    }
98
99
    /**
100
     * Get a ProfileProfile object for a user id.
101
     *
102
     * We will create an empty profile if none exists. This behavior allows user objects
103
     * created outside of profile to be edited correctly in the profile module.
104
     *
105
     * @param int|null      $uid
106
     * @param string[]|null $fields array of field names to fetch, null for all
107
     *
108
     * @return object {@link ProfileProfile}
109
     *
110
     * @internal This was get($uid, $createOnFailure = true). No callers found using the extra parameter.
111
     * @internal Modified to match parent signature.
112
     */
113
    public function get($uid = null, $fields = null)
114
    {
115
        $obj = parent::get($uid, $fields);
116
        if (!is_object($obj)) {
117
            $obj = $this->create();
118
        }
119
120
        return $obj;
121
    }
122
123
    /**
124
     * Create new {@link ProfileField} object
125
     *
126
     * @param bool $isNew
127
     *
128
     * @return ProfileField
129
     */
130
    public function createField($isNew = true)
131
    {
132
        $return = $this->_fHandler->create($isNew);
0 ignored issues
show
Unused Code introduced by
The call to XoopsObjectHandler::create() has too many arguments starting with $isNew. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

132
        /** @scrutinizer ignore-call */ 
133
        $return = $this->_fHandler->create($isNew);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
Bug introduced by
Are you sure the assignment to $return is correct as $this->_fHandler->create($isNew) targeting XoopsObjectHandler::create() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
133
134
        return $return;
135
    }
136
137
    /**
138
     * Load field information
139
     *
140
     * @return array
141
     */
142
    public function loadFields()
143
    {
144
        if (count($this->_fields) == 0) {
145
            $this->_fields = $this->_fHandler->loadFields();
0 ignored issues
show
Bug introduced by
The method loadFields() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsPersistableObjectHandler. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

145
            /** @scrutinizer ignore-call */ 
146
            $this->_fields = $this->_fHandler->loadFields();
Loading history...
146
        }
147
148
        return $this->_fields;
149
    }
150
151
    /**
152
     * Fetch fields
153
     *
154
     * @param CriteriaElement $criteria  {@link CriteriaElement} object
155
     * @param bool            $id_as_key return array with field IDs as key?
156
     * @param bool            $as_object return array of objects?
157
     *
158
     * @return array
159
     **/
160
    public function getFields(CriteriaElement $criteria, $id_as_key = true, $as_object = true)
161
    {
162
        return $this->_fHandler->getObjects($criteria, $id_as_key, $as_object);
0 ignored issues
show
Bug introduced by
The method getObjects() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of said class. However, the method does not exist in XoopsRankHandler or XoUserHandler. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

162
        return $this->_fHandler->/** @scrutinizer ignore-call */ getObjects($criteria, $id_as_key, $as_object);
Loading history...
163
    }
164
165
    /**
166
     * Insert a field in the database
167
     *
168
     * @param ProfileField $field
169
     * @param bool         $force
170
     *
171
     * @return bool
172
     */
173
    public function insertField(ProfileField $field, $force = false)
174
    {
175
        return $this->_fHandler->insert($field, $force);
0 ignored issues
show
Unused Code introduced by
The call to XoopsObjectHandler::insert() has too many arguments starting with $force. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

175
        return $this->_fHandler->/** @scrutinizer ignore-call */ insert($field, $force);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
Bug introduced by
Are you sure the usage of $this->_fHandler->insert($field, $force) targeting XoopsObjectHandler::insert() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
176
    }
177
178
    /**
179
     * Delete a field from the database
180
     *
181
     * @param ProfileField $field
182
     * @param bool         $force
183
     *
184
     * @return bool
185
     */
186
    public function deleteField(ProfileField $field, $force = false)
187
    {
188
        return $this->_fHandler->delete($field, $force);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->_fHandler->delete($field, $force) targeting XoopsObjectHandler::delete() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Unused Code introduced by
The call to XoopsObjectHandler::delete() has too many arguments starting with $force. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

188
        return $this->_fHandler->/** @scrutinizer ignore-call */ delete($field, $force);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
189
    }
190
191
    /**
192
     * Save a new field in the database
193
     *
194
     * @param array $vars array of variables, taken from $module->loadInfo('profile')['field']
195
     * @param int   $weight
196
     *
197
     * @internal param int $categoryid ID of the category to add it to
198
     * @internal param int $type valuetype of the field
199
     * @internal param int $moduleid ID of the module, this field belongs to
200
     * @return string
201
     */
202
    public function saveField($vars, $weight = 0)
203
    {
204
        $field = $this->createField();
205
        $field->setVar('field_name', $vars['name']);
206
        $field->setVar('field_valuetype', $vars['valuetype']);
207
        $field->setVar('field_type', $vars['type']);
208
        $field->setVar('field_weight', $weight);
209
        if (isset($vars['title'])) {
210
            $field->setVar('field_title', $vars['title']);
211
        }
212
        if (isset($vars['description'])) {
213
            $field->setVar('field_description', $vars['description']);
214
        }
215
        if (isset($vars['required'])) {
216
            $field->setVar('field_required', $vars['required']); //0 = no, 1 = yes
217
        }
218
        if (isset($vars['maxlength'])) {
219
            $field->setVar('field_maxlength', $vars['maxlength']);
220
        }
221
        if (isset($vars['default'])) {
222
            $field->setVar('field_default', $vars['default']);
223
        }
224
        if (isset($vars['notnull'])) {
225
            $field->setVar('field_notnull', $vars['notnull']);
226
        }
227
        if (isset($vars['show'])) {
228
            $field->setVar('field_show', $vars['show']);
229
        }
230
        if (isset($vars['edit'])) {
231
            $field->setVar('field_edit', $vars['edit']);
232
        }
233
        if (isset($vars['config'])) {
234
            $field->setVar('field_config', $vars['config']);
235
        }
236
        if (isset($vars['options'])) {
237
            $field->setVar('field_options', $vars['options']);
238
        } else {
239
            $field->setVar('field_options', array());
240
        }
241
        if ($this->insertField($field)) {
242
            $msg = '&nbsp;&nbsp;Field <strong>' . $vars['name'] . '</strong> added to the database';
243
        } else {
244
            $msg = '&nbsp;&nbsp;<span class="red">ERROR: Could not insert field <strong>' . $vars['name'] . '</strong> into the database. ' . implode(' ', $field->getErrors()) . $this->db->error() . '</span>';
0 ignored issues
show
Bug introduced by
The method error() does not exist on XoopsDatabase. Since it exists in all sub-types, consider adding an abstract or default implementation to XoopsDatabase. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

244
            $msg = '&nbsp;&nbsp;<span class="red">ERROR: Could not insert field <strong>' . $vars['name'] . '</strong> into the database. ' . implode(' ', $field->getErrors()) . $this->db->/** @scrutinizer ignore-call */ error() . '</span>';
Loading history...
245
        }
246
        unset($field);
247
248
        return $msg;
249
    }
250
251
    /**
252
     * insert a new object in the database
253
     *
254
     * @param XoopsObject|ProfileProfile $obj   reference to the object
255
     * @param bool                       $force whether to force the query execution despite security settings
256
     *
257
     * @return bool FALSE if failed, TRUE if already present and unchanged or successful
258
     */
259
    public function insert(XoopsObject $obj, $force = false)
260
    {
261
        if (!($obj instanceof $this->className)) {
262
            return false;
263
        }
264
        $uservars = $this->getUserVars();
265
        foreach ($uservars as $var) {
266
            unset($obj->vars[$var]);
267
        }
268
        if (count($obj->vars) == 0) {
269
            return true;
270
        }
271
272
        return parent::insert($obj, $force);
273
    }
274
275
    /**
276
     * Get array of standard variable names (user table)
277
     *
278
     * @return array
279
     */
280
    public function getUserVars()
281
    {
282
        return $this->_fHandler->getUserVars();
0 ignored issues
show
Bug introduced by
The method getUserVars() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsPersistableObjectHandler. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

282
        return $this->_fHandler->/** @scrutinizer ignore-call */ getUserVars();
Loading history...
283
    }
284
285
    /**
286
     * Search profiles and users
287
     *
288
     * @param CriteriaElement $criteria   CriteriaElement
289
     * @param array           $searchvars Fields to be fetched
290
     * @param array           $groups     for Usergroups is selected (only admin!)
291
     *
292
     * @return array
293
     */
294
    public function search(CriteriaElement $criteria, $searchvars = array(), $groups = null)
295
    {
296
        $uservars = $this->getUserVars();
297
298
        $searchvars_user    = array_intersect($searchvars, $uservars);
299
        $searchvars_profile = array_diff($searchvars, $uservars);
300
        $sv                 = array('u.uid, u.uname, u.email, u.user_viewemail');
301
        if (!empty($searchvars_user)) {
302
            $sv[0] .= ',u.' . implode(', u.', $searchvars_user);
303
        }
304
        if (!empty($searchvars_profile)) {
305
            $sv[] = 'p.' . implode(', p.', $searchvars_profile);
306
        }
307
308
        $sql_select = 'SELECT ' . (empty($searchvars) ? 'u.*, p.*' : implode(', ', $sv));
309
        $sql_from   = ' FROM ' . $this->db->prefix('users') . ' AS u LEFT JOIN ' . $this->table . ' AS p ON u.uid=p.profile_id' . (empty($groups) ? '' : ' LEFT JOIN ' . $this->db->prefix('groups_users_link') . ' AS g ON u.uid=g.uid');
310
        $sql_clause = ' WHERE 1=1';
311
        $sql_order  = '';
312
313
        $limit = $start = 0;
314
        if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) {
315
            $sql_clause .= ' AND ' . $criteria->render();
316
            if ($criteria->getSort() !== '') {
317
                $sql_order = ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder();
318
            }
319
            $limit = $criteria->getLimit();
320
            $start = $criteria->getStart();
321
        }
322
323
        if (!empty($groups)) {
324
            $sql_clause .= ' AND g.groupid IN (' . implode(', ', $groups) . ')';
325
        }
326
327
        $sql_users = $sql_select . $sql_from . $sql_clause . $sql_order;
328
        $result    = $this->db->query($sql_users, $limit, $start);
0 ignored issues
show
Bug introduced by
The method query() does not exist on XoopsDatabase. Since it exists in all sub-types, consider adding an abstract or default implementation to XoopsDatabase. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

328
        /** @scrutinizer ignore-call */ 
329
        $result    = $this->db->query($sql_users, $limit, $start);
Loading history...
329
330
//        if (!$result) {
331
        if (!$this->db->isResultSet($result)) {
332
            return array(array(), array(), 0);
333
        }
334
        $user_handler = xoops_getHandler('user');
335
        $uservars     = $this->getUserVars();
336
        $users        = array();
337
        $profiles     = array();
338
        /** @var array $myrow */
339
        while (false !== ($myrow = $this->db->fetchArray($result))) {
0 ignored issues
show
Bug introduced by
The method fetchArray() does not exist on XoopsDatabase. Since it exists in all sub-types, consider adding an abstract or default implementation to XoopsDatabase. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

339
        while (false !== ($myrow = $this->db->/** @scrutinizer ignore-call */ fetchArray($result))) {
Loading history...
340
            $profile = $this->create(false);
341
            $user    = $user_handler->create(false);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $user is correct as $user_handler->create(false) targeting XoopsObjectHandler::create() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Unused Code introduced by
The call to XoopsObjectHandler::create() has too many arguments starting with false. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

341
            /** @scrutinizer ignore-call */ 
342
            $user    = $user_handler->create(false);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
342
343
            foreach ($myrow as $name => $value) {
344
                if (in_array($name, $uservars)) {
345
                    $user->assignVar($name, $value);
346
                } else {
347
                    $profile->assignVar($name, $value);
348
                }
349
            }
350
            $profiles[$myrow['uid']] = $profile;
351
            $users[$myrow['uid']]    = $user;
352
        }
353
354
        $count = count($users);
355
        if ((!empty($limit) && $count >= $limit) || !empty($start)) {
356
            $sql_count = 'SELECT COUNT(*)' . $sql_from . $sql_clause;
357
            $result    = $this->db->query($sql_count);
358
            if (!$this->db->isResultSet($result)) {
359
                \trigger_error("Query Failed! SQL: $sql_count- Error: " . $this->db->error(), E_USER_ERROR);
360
            }
361
            list($count) = $this->db->fetchRow($result);
0 ignored issues
show
Bug introduced by
The method fetchRow() does not exist on XoopsDatabase. Since it exists in all sub-types, consider adding an abstract or default implementation to XoopsDatabase. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

361
            /** @scrutinizer ignore-call */ 
362
            list($count) = $this->db->fetchRow($result);
Loading history...
362
        }
363
364
        return array($users, $profiles, (int)$count);
365
    }
366
}
367