Passed
Pull Request — master (#81)
by Michael
02:55
created

VisibilityHandler   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 24
dl 0
loc 92
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getAllByFieldId() 0 13 2
A visibilitySort() 0 17 3
A __construct() 0 3 1
A getVisibleFields() 0 17 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace XoopsModules\Yogurt;
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
34
    public function __construct(\XoopsDatabase $db)
35
    {
36
        parent::__construct($db, 'yogurt_profile_visibility', Visibility::class, 'field_id');
37
    }
38
39
    /**
40
     * Get fields visible to the $user_groups on a $profile_groups profile
41
     *
42
     * @param array $profile_groups groups of the user to be accessed
43
     * @param array $user_groups    groups of the visitor, default as $GLOBALS['xoopsUser']
44
     *
45
     * @return array
46
     */
47
48
    public function getVisibleFields($profile_groups, $user_groups = null)
49
    {
50
        $profile_groups[] = $user_groups[] = 0;
51
52
        $sql = "SELECT field_id FROM {$this->table} WHERE profile_group IN (" . \implode(',', $profile_groups) . ')';
53
54
        $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

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