Passed
Push — master ( f0fd80...9c2eb6 )
by Michael
33s queued 12s
created

VisibilityHandler   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 75
rs 10
c 0
b 0
f 0
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A visibilitySort() 0 12 3
A getAllByFieldId() 0 9 2
A getVisibleFields() 0 13 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace XoopsModules\Suico;
6
7
/**
8
 * Extended User Profile
9
 *
10
 * You may not change or alter any portion of this comment or credits
11
 * of supporting developers from this source code or any supporting source code
12
 * which is considered copyrighted (c) material of the original comment or credit authors.
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
 *
17
 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
18
 * @license             GNU GPL 2 (http://www.gnu.org/licenses/gpl-2.0.html)
19
 * @package             profile
20
 * @since               2.3.0
21
 * @author              Jan Pedersen
22
 * @author              Taiwen Jiang <[email protected]>
23
 */
24
25
/**
26
 * Class ProfileVisibilityHandler
27
 */
28
class VisibilityHandler extends \XoopsPersistableObjectHandler
29
{
30
    /**
31
     * @param \XoopsDatabase $db
32
     */
33
    public function __construct(\XoopsDatabase $db)
34
    {
35
        parent::__construct($db, 'suico_profile_visibility', Visibility::class, 'field_id');
36
    }
37
38
    /**
39
     * Get fields visible to the $user_groups on a $profile_groups profile
40
     *
41
     * @param array $profile_groups groups of the user to be accessed
42
     * @param array|null  $user_groups    groups of the visitor, default as $GLOBALS['xoopsUser']
43
     *
44
     * @return array
45
     */
46
    public function getVisibleFields($profile_groups, $user_groups = null)
47
    {
48
        $user_groups[]    = 0;
49
        $profile_groups[] = 0;
50
        $sql              = "SELECT field_id FROM {$this->table} WHERE profile_group IN (" . \implode(',', $profile_groups) . ')';
51
        $sql              .= ' AND user_group IN (' . \implode(',', $user_groups) . ')';
0 ignored issues
show
Bug introduced by
It seems like $user_groups can also be of type null; however, parameter $pieces of implode() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

51
        $sql              .= ' AND user_group IN (' . \implode(',', /** @scrutinizer ignore-type */ $user_groups) . ')';
Loading history...
52
        $field_ids        = [];
53
        if (false !== ($result = $this->db->query($sql))) {
54
            while (false !== (list($field_id) = $this->db->fetchRow($result))) {
55
                $field_ids[] = $field_id;
56
            }
57
        }
58
        return $field_ids;
59
    }
60
61
    /**
62
     * get all rows matching a condition
63
     *
64
     * @param \CriteriaElement|null $criteria {@link \CriteriaElement} to match
65
     *
66
     * @return array of row arrays, indexed by field_id
67
     */
68
    public function getAllByFieldId(\CriteriaElement $criteria = null)
69
    {
70
        $rawRows = parent::getAll($criteria, null, false, false);
71
        \usort($rawRows, [$this, 'visibilitySort']);
72
        $rows = [];
73
        foreach ($rawRows as $rawRow) {
74
            $rows[$rawRow['field_id']][] = $rawRow;
75
        }
76
        return $rows;
77
    }
78
79
    /**
80
     * compare two arrays, each a row from profile_visibility
81
     * The comparison is on three columns, 'field_id', 'user_group', 'profile_group' considered in that
82
     * order for comparison
83
     *
84
     * @param array $a associative array with 3 numeric entries 'field_id', 'user_group', 'profile_group'
85
     * @param array $b associative array with 3 numeric entries 'field_id', 'user_group', 'profile_group'
86
     *
87
     * @return int integer less that zero if $a is less than $b
88
     *              integer zero if $a and $b are equal
89
     *              integer greater than zero if $a is greater than $b
90
     */
91
    protected function visibilitySort($a, $b)
92
    {
93
        $fieldDiff = $a['field_id'] - $b['field_id'];
94
        $userDiff  = $a['user_group'] - $b['user_group'];
95
        $profDiff  = $a['profile_group'] - $b['profile_group'];
96
        if (0 != $fieldDiff) {
97
            return $fieldDiff;
98
        }
99
        if (0 !== $userDiff) {
100
            return $userDiff;
101
        }
102
        return $profDiff;
103
    }
104
}
105