VisibilityHandler::getVisibleFields()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 11
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 16
rs 9.9
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Songlist;
4
5
/*
6
 * You may not change or alter any portion of this comment or credits
7
 * of supporting developers from this source code or any supporting source code
8
 * which is considered copyrighted (c) material of the original comment or credit authors.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 */
14
15
use CriteriaElement;
16
use XoopsDatabase;
17
use XoopsPersistableObjectHandler;
18
19
/**
20
 * @copyright      {@link https://xoops.org/ XOOPS Project}
21
 * @license        {@link https://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later}
22
 * @author         XOOPS Development Team, Wishcraft
23
 */
24
25
/**
26
 * Class VisibilityHandler
27
 */
28
class VisibilityHandler extends XoopsPersistableObjectHandler
29
{
30
    /**
31
     * VisibilityHandler constructor.
32
     * @param \XoopsDatabase $db
33
     */
34
    public function __construct(XoopsDatabase $db)
35
    {
36
        parent::__construct($db, 'songlist_visibility', Visibility::class, 'field_id');
37
    }
38
39
    /**
40
     * get all objects matching a condition
41
     *
42
     * @param \CriteriaElement|null $criteria {@link \CriteriaElement} to match
43
     * @param null                  $fields
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $fields is correct as it would always require null to be passed?
Loading history...
44
     * @param bool                  $asObject
45
     * @param bool                  $id_as_key
46
     * @return array of objects/array <a href='psi_element://XoopsObject'>XoopsObject</a>
47
     * @internal param array $fields variables to fetch
48
     * @internal param bool $asObject flag indicating as object, otherwise as array
49
     * @internal param bool $id_as_key use the ID as key for the array
50
     */
51
    public function &getAll(CriteriaElement $criteria = null, $fields = null, $asObject = true, $id_as_key = true): array //getAll($criteria = null)
52
    {
53
        $limit            = null;
54
        $GLOBALS['start'] = null;
55
        $sql              = "SELECT * FROM `{$this->table}`";
56
        if (($criteria instanceof \CriteriaCompo) || ($criteria instanceof \Criteria)) {
57
            $sql     .= ' ' . $criteria->renderWhere();
58
            $groupby = $criteria->getGroupby();
59
            if ($groupby) {
60
                $sql .= ' ' . $groupby;
61
            }
62
            $sort = $criteria->getSort();
63
            if ($sort) {
64
                $sql      .= " ORDER BY {$sort} " . $criteria->getOrder();
65
                $orderSet = true;
66
            }
67
            $limit            = $criteria->getLimit();
68
            $GLOBALS['start'] = $criteria->getStart();
69
        }
70
        if (empty($orderSet)) {
71
            $sql .= " ORDER BY `{$this->keyName}` DESC";
72
        }
73
        $result = $this->db->query($sql, $limit, $GLOBALS['start']);
74
        $ret    = [];
75
        while (false !== ($row = $this->db->fetchArray($result))) {
76
            $ret[$row['field_id']][] = $row;
77
        }
78
79
        return $ret;
80
    }
81
82
    /**
83
     * Get fields visible to the $user_groups on a $profile_groups profile
84
     *
85
     * @param array $profile_groups groups of the user to be accessed
86
     * @param array $user_groups    groups of the visitor, default as $GLOBALS['xoopsUser']
87
     *
88
     * @return array
89
     */
90
    public function getVisibleFields($profile_groups, $user_groups = []): array
91
    {
92
        $profile_groups[] = '0';
93
        $user_groups[]    = '0';
94
        $profile_groups[] = $user_groups[] = 0;
95
        $sql              = "SELECT field_id FROM {$this->table} WHERE profile_group IN (" . \implode(',', $profile_groups) . ')';
96
        $sql              .= ' AND user_group IN (' . \implode(',', $user_groups) . ')';
97
        $field_ids        = [];
98
        $result           = $this->db->query($sql);
99
        if ($result) {
100
            while ([$field_id] = $this->db->fetchRow($result)) {
101
                $field_ids[] = $field_id;
102
            }
103
        }
104
105
        return $field_ids;
106
    }
107
}
108