Completed
Push — xmfsync ( d2ecf4...a4059d )
by Richard
05:48
created

ProfileVisibilityHandler::visibilitySort()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 10

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 3
eloc 10
nc 3
nop 2
1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
use Xoops\Core\Database\Connection;
13
use Xoops\Core\Kernel\XoopsObject;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, XoopsObject.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
14
use Xoops\Core\Kernel\XoopsPersistableObjectHandler;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, XoopsPersistableObjectHandler.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
15
use Xoops\Core\Kernel\CriteriaElement;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, CriteriaElement.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
16
17
/**
18
 * Extended User Profile
19
 *
20
 * @copyright       XOOPS Project (http://xoops.org)
21
 * @license         GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
22
 * @package         profile
23
 * @since           2.3.0
24
 * @author          Jan Pedersen
25
 * @author          Taiwen Jiang <[email protected]>
26
 * @version         $Id$
27
 */
28
29
class ProfileVisibility extends XoopsObject
30
{
31
    /**
32
     * Constructor
33
     */
34
    public function __construct()
35
    {
36
        $this->initVar('field_id', XOBJ_DTYPE_INT);
37
        $this->initVar('user_group', XOBJ_DTYPE_INT);
38
        $this->initVar('profile_group', XOBJ_DTYPE_INT);
39
    }
40
}
41
42
class ProfileVisibilityHandler extends XoopsPersistableObjectHandler
43
{
44
    /**
45
     * @param null|Connection $db database
46
     */
47
    public function __construct(Connection $db = null)
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 $xoops->user
57
     *
58
     * @return array
59
     */
60
    public function getVisibleFields($profile_groups, $user_groups = null)
61
    {
62
        $profile_groups[] = 0;
63
        array_walk($profile_groups, 'intval');
64
        $user_groups[] = 0;
65
        array_walk($user_groups, 'intval');
66
67
        $qb = $this->db2->createXoopsQueryBuilder();
68
        $eb = $qb->expr();
69
        $sql = $qb->select('t1.field_id')
70
            ->from($this->table, 't1')
71
            ->where($eb->in('t1.profile_group', $profile_groups))
72
            ->andWhere($eb->in('t1.user_group', $user_groups));
73
74
        $result = $sql->execute();
75
        $field_ids = array();
76
        while (list($field_id) = $result->fetch(PDO::FETCH_NUM)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $field_id is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
77
            $field_ids[] = $field_ids;
78
        }
79
80
        return $field_ids;
81
    }
82
83
    /**
84
     * get all rows matching a condition
85
     *
86
     * @param  CriteriaElement $criteria  {@link CriteriaElement} to match
87
     *
88
     * @return array of row arrays, indexed by field_id
89
     */
90
    public function getAllByFieldId(CriteriaElement $criteria = null)
91
    {
92
        $rawRows = $this->getAll($criteria, null, false, false);
93
94
        usort($rawRows, array($this, 'visibilitySort'));
95
96
        $rows = array();
97
        foreach ($rawRows as $rawRow) {
98
            $rows[$rawRow['field_id']][] = $rawRow;
99
        }
100
101
        return $rows;
102
    }
103
104
    /**
105
     * compare two arrays, each a row from profile_visibility
106
     * The comparison is on three columns, 'field_id', 'user_group', 'profile_group' considered in that
107
     * order for comparison
108
     *
109
     * @param array $a associative array with 3 numeric entries 'field_id', 'user_group', 'profile_group'
110
     * @param array $b associative array with 3 numeric entries 'field_id', 'user_group', 'profile_group'
111
     *
112
     * @return int integer less that zero if $a is less than $b
113
     *              integer zero if $a and $b are equal
114
     *              integer greater than zero if $a is greater than $b
115
     */
116
    protected function visibilitySort($a, $b)
117
    {
118
        $fieldDiff = $a['field_id'] - $b['field_id'];
119
        $userDiff  = $a['user_group'] - $b['user_group'];
120
        $profDiff  = $a['profile_group'] - $b['profile_group'];
121
        if (0 != $fieldDiff) {
122
            return $fieldDiff;
123
        } elseif (0 !== $userDiff) {
124
            return $userDiff;
125
        } else {
126
            return $profDiff;
127
        }
128
    }
129
}
130