Passed
Pull Request — master (#81)
by Michael
02:55
created

Groups::load()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
dl 0
loc 10
rs 10
c 1
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace XoopsModules\Yogurt;
6
7
/*
8
 You may not change or alter any portion of this comment or credits
9
 of supporting developers from this source code or any supporting source code
10
 which is considered copyrighted (c) material of the original comment or credit authors.
11
 
12
 This program is distributed in the hope that it will be useful,
13
 but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
*/
16
17
/**
18
 * @category        Module
19
 * @package         yogurt
20
 * @copyright       {@link https://xoops.org/ XOOPS Project}
21
 * @license         GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
22
 * @author          Marcello Brandão aka  Suico, Mamba, LioMJ  <https://xoops.org>
23
 */
24
25
use Xmf\Module\Helper\Permission;
26
use XoopsDatabaseFactory;
27
use XoopsObject;
28
29
/**
30
 * Includes of form objects and uploader
31
 */
32
require_once XOOPS_ROOT_PATH . '/class/uploader.php';
33
require_once XOOPS_ROOT_PATH . '/kernel/object.php';
34
require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
35
require_once XOOPS_ROOT_PATH . '/kernel/object.php';
36
37
/**
38
 * Groups class.
39
 * $this class is responsible for providing data access mechanisms to the data source
40
 * of XOOPS user class objects.
41
 */
42
class Groups extends XoopsObject
43
{
44
    public $xoopsDb;
45
46
    public $helper;
47
48
    public $permHelper;
49
50
    public const GROUPID = 'group_id';
51
52
    public const YOGURTGROUPS = 'yogurt_groups'; //table
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
59
    public function __construct($id = null)
60
    {
61
        /** @var Helper $helper */
62
63
        $this->helper = Helper::getInstance();
64
65
        $this->permHelper = new Permission();
66
67
        $this->xoopsDb = XoopsDatabaseFactory::getDatabaseConnection();
68
69
        $this->initVar(GROUPID, \XOBJ_DTYPE_INT, null, false, 10);
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Yogurt\GROUPID was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
70
71
        $this->initVar('owner_uid', \XOBJ_DTYPE_INT, null, false, 10);
72
73
        $this->initVar('group_title', \XOBJ_DTYPE_TXTBOX, null, false);
74
75
        $this->initVar('group_desc', \XOBJ_DTYPE_TXTBOX, null, false);
76
77
        $this->initVar('group_img', \XOBJ_DTYPE_TXTBOX, null, false);
78
79
        $this->initVar('date_created', \XOBJ_DTYPE_INT);
80
81
        $this->initVar('date_updated', \XOBJ_DTYPE_INT);
82
83
        if (!empty($id)) {
84
            if (\is_array($id)) {
85
                $this->assignVars($id);
86
            } else {
87
                $this->load((int)$id);
88
            }
89
        } else {
90
            $this->setNew();
91
        }
92
    }
93
94
    /**
95
     * @param $id
96
     */
97
98
    public function load($id)
99
    {
100
        $sql = 'SELECT * FROM ' . $this->xoopsDb->prefix(YOGURTGROUPS) . ' WHERE group_id=' . $id;
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Yogurt\YOGURTGROUPS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
101
102
        $myrow = $this->xoopsDb->fetchArray($this->xoopsDb->query($sql));
103
104
        $this->assignVars($myrow);
105
106
        if (!$myrow) {
107
            $this->setNew();
108
        }
109
    }
110
111
    /**
112
     * @param array  $criteria
113
     * @param bool   $asobject
114
     * @param string $sort
115
     * @param string $order
116
     * @param int    $limit
117
     * @param int    $start
118
     * @return array
119
     */
120
121
    public function getAllGroups(
122
        $criteria = [],
123
        $asobject = false,
124
        $sort = GROUPID,
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Yogurt\GROUPID was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
125
        $order = 'ASC',
126
        $limit = 0,
127
        $start = 0
128
    ) {
129
        $ret = [];
130
131
        $whereQuery = '';
132
133
        if (\is_array($criteria) && \count($criteria) > 0) {
134
            $whereQuery = ' WHERE';
135
136
            foreach ($criteria as $c) {
137
                $whereQuery .= " ${c} AND";
138
            }
139
140
            $whereQuery = mb_substr($whereQuery, 0, -4);
141
        } elseif (!\is_array($criteria) && $criteria) {
142
            $whereQuery = ' WHERE ' . $criteria;
143
        }
144
145
        if (!$asobject) {
146
            $sql = 'SELECT group_id FROM ' . $this->xoopsDb->prefix(
147
                    YOGURTGROUPS
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Yogurt\YOGURTGROUPS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
148
                ) . "${whereQuery} ORDER BY ${sort} ${order}";
149
150
            $result = $this->xoopsDb->query($sql, $limit, $start);
151
152
            while (false !== ($myrow = $this->xoopsDb->fetchArray($result))) {
153
                $ret[] = $myrow['yogurt_groups_id'];
154
            }
155
        } else {
156
            $sql = 'SELECT * FROM ' . $this->xoopsDb->prefix(YOGURTGROUPS) . "${whereQuery} ORDER BY ${sort} ${order}";
157
158
            $result = $this->xoopsDb->query($sql, $limit, $start);
159
160
            while (false !== ($myrow = $this->xoopsDb->fetchArray($result))) {
161
                $ret[] = new self($myrow);
162
            }
163
        }
164
165
        return $ret;
166
    }
167
168
    /**
169
     * Get form
170
     *
171
     * @return \XoopsModules\Yogurt\Form\GroupsForm
172
     */
173
174
    public function getForm()
175
    {
176
        return new Form\GroupsForm($this);
177
    }
178
179
    /**
180
     * @return array|null
181
     */
182
183
    public function getGroupsRead()
184
    {
185
        return $this->permHelper->getGroupsForItem(
186
            'sbcolumns_read',
187
            $this->getVar(GROUPID)
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Yogurt\GROUPID was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
188
        );
189
    }
190
191
    /**
192
     * @return array|null
193
     */
194
195
    public function getGroupsSubmit()
196
    {
197
        return $this->permHelper->getGroupsForItem(
198
            'sbcolumns_submit',
199
            $this->getVar(GROUPID)
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Yogurt\GROUPID was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
200
        );
201
    }
202
203
    /**
204
     * @return array|null
205
     */
206
207
    public function getGroupsModeration()
208
    {
209
        return $this->permHelper->getGroupsForItem(
210
            'sbcolumns_moderation',
211
            $this->getVar(GROUPID)
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Yogurt\GROUPID was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
212
        );
213
    }
214
}
215