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

Groups::getAllGroups()   B

Complexity

Conditions 9
Paths 12

Size

Total Lines 37
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

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