Groups::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 21
rs 9.7
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Suico;
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
/**
16
 * @category        Module
17
 * @copyright       {@link https://xoops.org/ XOOPS Project}
18
 * @license         GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
19
 * @author          Marcello Brandão aka  Suico, Mamba, LioMJ  <https://xoops.org>
20
 */
21
22
use Xmf\Module\Helper\Permission;
23
use XoopsDatabaseFactory;
24
use XoopsObject;
25
26
const GROUPID     = 'group_id';
27
const SUICOGROUPS = 'suico_groups'; //table
28
/**
29
 * Includes of form objects and uploader
30
 */
31
require_once XOOPS_ROOT_PATH . '/class/uploader.php';
32
require_once XOOPS_ROOT_PATH . '/kernel/object.php';
33
require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
34
require_once XOOPS_ROOT_PATH . '/kernel/object.php';
35
36
/**
37
 * Groups class.
38
 * $this class is responsible for providing data access mechanisms to the data source
39
 * of XOOPS user class objects.
40
 */
41
class Groups extends XoopsObject
42
{
43
    public \XoopsDatabase $xoopsDB;
44
    public Helper         $helper;
45
    public Permission     $permHelper;
46
    public                $group_id;
47
    public $owner_uid;
48
    public $group_title;
49
    public $group_desc;
50
    public $group_img;
51
    public $date_created;
52
    public $date_updated;
53
54
    /**
55
     * Groups constructor.
56
     * @param null $id
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $id is correct as it would always require null to be passed?
Loading history...
57
     */
58
    public function __construct($id = null)
59
    {
60
        /** @var Helper $helper */
61
        $this->helper     = Helper::getInstance();
62
        $this->permHelper = new Permission();
63
        $this->xoopsDB    = XoopsDatabaseFactory::getDatabaseConnection();
64
        $this->initVar(GROUPID, \XOBJ_DTYPE_INT, null, false, 10);
65
        $this->initVar('owner_uid', \XOBJ_DTYPE_INT, null, false, 10);
66
        $this->initVar('group_title', \XOBJ_DTYPE_TXTBOX, null, false);
67
        $this->initVar('group_desc', \XOBJ_DTYPE_OTHER, null, false);
68
        $this->initVar('group_img', \XOBJ_DTYPE_TXTBOX, null, false);
69
        $this->initVar('date_created', \XOBJ_DTYPE_INT);
70
        $this->initVar('date_updated', \XOBJ_DTYPE_INT);
71
        if (!empty($id)) {
72
            if (\is_array($id)) {
73
                $this->assignVars($id);
74
            } else {
75
                $this->load((int)$id);
76
            }
77
        } else {
78
            $this->setNew();
79
        }
80
    }
81
82
    /**
83
     * @param $id
84
     */
85
    public function load($id): void
86
    {
87
        $sql   = 'SELECT * FROM ' . $this->xoopsDB->prefix(SUICOGROUPS) . ' WHERE group_id=' . $id;
88
        $myrow = $this->xoopsDB->fetchArray($this->xoopsDB->query($sql));
89
        $this->assignVars($myrow);
90
        if (!$myrow) {
91
            $this->setNew();
92
        }
93
    }
94
95
    /**
96
     * @param array  $criteria
97
     * @param bool   $asobject
98
     * @param string $sort
99
     * @param string $order
100
     * @param int    $limit
101
     * @param int    $start
102
     * @return array
103
     */
104
    public function getAllGroups(
105
        $criteria = [],
106
        $asobject = false,
107
        $sort = GROUPID,
108
        $order = 'ASC',
109
        $limit = 0,
110
        $start = 0
111
    ) {
112
        $ret        = [];
113
        $whereQuery = '';
114
        if (\is_array($criteria) && \count($criteria) > 0) {
115
            $whereQuery = ' WHERE';
116
            foreach ($criteria as $c) {
117
                $whereQuery .= "{$c} AND";
118
            }
119
            $whereQuery = mb_substr($whereQuery, 0, -4);
120
        } elseif (!\is_array($criteria) && $criteria) {
121
            $whereQuery = ' WHERE ' . $criteria;
122
        }
123
        if ($asobject) {
124
            $sql    = 'SELECT * FROM ' . $this->xoopsDB->prefix(SUICOGROUPS) . "{$whereQuery} ORDER BY {$sort} {$order}";
125
            $result = $this->xoopsDB->query($sql, $limit, $start);
126
            while (false !== ($myrow = $this->xoopsDB->fetchArray($result))) {
127
                $ret[] = new self($myrow);
128
            }
129
        } else {
130
            $sql    = 'SELECT group_id FROM ' . $this->xoopsDB->prefix(
131
                    SUICOGROUPS
132
                ) . "{$whereQuery} ORDER BY {$sort} {$order}";
133
            $result = $this->xoopsDB->query($sql, $limit, $start);
134
            while (false !== ($myrow = $this->xoopsDB->fetchArray($result))) {
135
                $ret[] = $myrow['suico_groups_id'];
136
            }
137
        }
138
139
        return $ret;
140
    }
141
142
    /**
143
     * Get form
144
     *
145
     * @return \XoopsModules\Suico\Form\GroupsForm
146
     */
147
    public function getForm()
148
    {
149
        return new Form\GroupsForm($this);
150
    }
151
152
    /**
153
     * @return array|null
154
     */
155
    public function getGroupsRead()
156
    {
157
        return $this->permHelper->getGroupsForItem(
158
            'sbcolumns_read',
159
            $this->getVar(GROUPID)
160
        );
161
    }
162
163
    /**
164
     * @return array|null
165
     */
166
    public function getGroupsSubmit()
167
    {
168
        return $this->permHelper->getGroupsForItem(
169
            'sbcolumns_submit',
170
            $this->getVar(GROUPID)
171
        );
172
    }
173
174
    /**
175
     * @return array|null
176
     */
177
    public function getGroupsModeration()
178
    {
179
        return $this->permHelper->getGroupsForItem(
180
            'sbcolumns_moderation',
181
            $this->getVar(GROUPID)
182
        );
183
    }
184
}
185