Completed
Pull Request — master (#46)
by Richard
10:46
created

ProfileVisibilityHandler::getAllByFieldId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
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
 * @version             $Id: visibility.php 13082 2015-06-06 21:59:41Z beckmi $
19
 */
20
21
// defined('XOOPS_ROOT_PATH') || exit("XOOPS root path not defined");
22
23
/**
24
 * Class ProfileVisibility
25
 */
26
class ProfileVisibility extends XoopsObject
27
{
28
    /**
29
     *
30
     */
31
    public function __construct()
32
    {
33
        $this->initVar('field_id', XOBJ_DTYPE_INT);
34
        $this->initVar('user_group', XOBJ_DTYPE_INT);
35
        $this->initVar('profile_group', XOBJ_DTYPE_INT);
36
    }
37
}
38
39
/**
40
 * Class ProfileVisibilityHandler
41
 */
42
class ProfileVisibilityHandler extends XoopsPersistableObjectHandler
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
43
{
44
    /**
45
     * @param null|XoopsDatabase $db
46
     */
47
    public function __construct(XoopsDatabase $db)
48
    {
49
        parent::__construct($db, 'profile_visibility', 'profilevisibility', 'field_id');
50
    }
51
52
    /**
53
     * Get fields visible to the $user_groups on a $profile_groups profile
54
     *
55
     * @param array $profile_groups groups of the user to be accessed
56
     * @param array $user_groups    groups of the visitor, default as $GLOBALS['xoopsUser']
57
     *
58
     * @return array
59
     */
60
    public function getVisibleFields($profile_groups, $user_groups = null)
61
    {
62
        $profile_groups[] = $user_groups[] = 0;
63
        $sql  = "SELECT field_id FROM {$this->table} WHERE profile_group IN (" . implode(',', $profile_groups) . ')';
64
        $sql .= ' AND user_group IN (' . implode(',', $user_groups) . ')';
65
        $field_ids = array();
66
        if ($result = $this->db->query($sql)) {
67
            while (list($field_id) = $this->db->fetchRow($result)) {
68
                $field_ids[] = $field_id;
69
            }
70
        }
71
72
        return $field_ids;
73
    }
74
75
    /**
76
     * get all rows matching a condition
77
     *
78
     * @param  CriteriaElement $criteria  {@link CriteriaElement} to match
79
     *
80
     * @return array of row arrays, indexed by field_id
81
     */
82
    public function getAllByFieldId(CriteriaElement $criteria = null)
83
    {
84
        $rawRows = parent::getAll($criteria, null, false, false);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getAll() instead of getAllByFieldId()). Are you sure this is correct? If so, you might want to change this to $this->getAll().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
85
86
        usort($rawRows, array($this, 'visibilitySort'));
87
88
        $rows = array();
89
        foreach ($rawRows as $rawRow) {
90
            $rows[$rawRow['field_id']][] = $rawRow;
91
        }
92
93
        return $rows;
94
    }
95
96
    /**
97
     * compare two arrays, each a row from profile_visibility
98
     * The comparison is on three columns, 'field_id', 'user_group', 'profile_group' considered in that
99
     * order for comparison
100
     *
101
     * @param array $a associative array with 3 numeric entries 'field_id', 'user_group', 'profile_group'
102
     * @param array $b associative array with 3 numeric entries 'field_id', 'user_group', 'profile_group'
103
     *
104
     * @return int integer less that zero if $a is less than $b
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|double?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
105
     *              integer zero if $a and $b are equal
106
     *              integer greater than zero if $a is greater than $b
107
     */
108
    protected function visibilitySort($a, $b)
109
    {
110
        $fieldDiff = $a['field_id'] - $b['field_id'];
111
        $userDiff  = $a['user_group'] - $b['user_group'];
112
        $profDiff  = $a['profile_group'] - $b['profile_group'];
113
        if (0 != $fieldDiff) {
114
            return $fieldDiff;
115
        } elseif (0 !== $userDiff) {
116
            return $userDiff;
117
        } else {
118
            return $profDiff;
119
        }
120
    }
121
}
122