Passed
Push — master ( 6209eb...36ba5e )
by Michael
51s queued 14s
created

YogurtVisibilityHandler   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 25
c 1
b 0
f 0
dl 0
loc 77
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getAllByFieldId() 0 12 2
A __construct() 0 3 1
A getVisibleFields() 0 13 3
A visibilitySort() 0 11 3
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 (http://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
 * Class YogurtVisibility
24
 */
25
class YogurtVisibility extends XoopsObject
26
{
27
    /**
28
     *
29
     */
30
    public function __construct()
31
    {
32
        $this->initVar('field_id', XOBJ_DTYPE_INT);
33
        $this->initVar('user_group', XOBJ_DTYPE_INT);
34
        $this->initVar('profile_group', XOBJ_DTYPE_INT);
35
    }
36
}
37
38
/**
39
 * Class ProfileVisibilityHandler
40
 */
41
class YogurtVisibilityHandler extends XoopsPersistableObjectHandler
42
{
43
    /**
44
     * @param null|XoopsDatabase $db
45
     */
46
    public function __construct(XoopsDatabase $db)
47
    {
48
        parent::__construct($db, 'yogurt_profile_visibility', 'yogurtvisibility', 'field_id');
49
    }
50
51
    /**
52
     * Get fields visible to the $user_groups on a $profile_groups profile
53
     *
54
     * @param array $profile_groups groups of the user to be accessed
55
     * @param array $user_groups    groups of the visitor, default as $GLOBALS['xoopsUser']
56
     *
57
     * @return array
58
     */
59
    public function getVisibleFields($profile_groups, $user_groups = null)
60
    {
61
        $profile_groups[] = $user_groups[] = 0;
62
        $sql  = "SELECT field_id FROM {$this->table} WHERE profile_group IN (" . implode(',', $profile_groups) . ')';
63
        $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

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