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

Configs::getAllyogurt_configs()   B

Complexity

Conditions 9
Paths 12

Size

Total Lines 37
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 23
c 0
b 0
f 0
dl 0
loc 37
rs 8.0555
cc 9
nc 12
nop 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A Configs::load() 0 10 2
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          Bruno Barthez, 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
require_once XOOPS_ROOT_PATH . '/kernel/object.php';
30
31
/**
32
 * Configs class.
33
 * $this class is responsible for providing data access mechanisms to the data source
34
 * of XOOPS user class objects.
35
 */
36
class Configs extends XoopsObject
37
{
38
    public $db;
39
40
    public $helper;
41
42
    public $permHelper;
43
44
    // constructor
45
46
    /**
47
     * Configs constructor.
48
     * @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...
49
     */
50
51
    public function __construct($id = null)
52
    {
53
        /** @var Helper $helper */
54
55
        $this->helper = Helper::getInstance();
56
57
        $this->permHelper = new Permission();
58
59
        $this->db = XoopsDatabaseFactory::getDatabaseConnection();
60
61
        $this->initVar('config_id', \XOBJ_DTYPE_INT);
62
63
        $this->initVar('config_uid', \XOBJ_DTYPE_INT, null, false, 10);
64
65
        $this->initVar('pictures', \XOBJ_DTYPE_INT, null, false, 10);
66
67
        $this->initVar('audio', \XOBJ_DTYPE_INT, null, false, 10);
68
69
        $this->initVar('videos', \XOBJ_DTYPE_INT, null, false, 10);
70
71
        $this->initVar('groups', \XOBJ_DTYPE_INT, null, false, 10);
72
73
        $this->initVar('notes', \XOBJ_DTYPE_INT, null, false, 10);
74
75
        $this->initVar('friends', \XOBJ_DTYPE_INT, null, false, 10);
76
77
        $this->initVar('profile_contact', \XOBJ_DTYPE_INT, null, false, 10);
78
79
        $this->initVar('profile_general', \XOBJ_DTYPE_INT, null, false, 10);
80
81
        $this->initVar('profile_stats', \XOBJ_DTYPE_INT, null, false, 10);
82
83
        $this->initVar('suspension', \XOBJ_DTYPE_INT, null, false, 10);
84
85
        $this->initVar('backup_password', \XOBJ_DTYPE_TXTBOX, null, false);
86
87
        $this->initVar('backup_email', \XOBJ_DTYPE_TXTBOX, null, false);
88
89
        $this->initVar('end_suspension', \XOBJ_DTYPE_INT, 0, false);
90
91
        if (!empty($id)) {
92
            if (\is_array($id)) {
93
                $this->assignVars($id);
94
            } else {
95
                $this->load((int)$id);
96
            }
97
        } else {
98
            $this->setNew();
99
        }
100
    }
101
102
    /**
103
     * @param $id
104
     */
105
106
    public function load($id)
107
    {
108
        $sql = 'SELECT * FROM ' . $this->db->prefix('yogurt_configs') . ' WHERE config_id=' . $id;
109
110
        $myrow = $this->db->fetchArray($this->db->query($sql));
111
112
        $this->assignVars($myrow);
113
114
        if (!$myrow) {
115
            $this->setNew();
116
        }
117
    }
118
119
    /**
120
     * @param array  $criteria
121
     * @param bool   $asobject
122
     * @param string $sort
123
     * @param string $order
124
     * @param int    $limit
125
     * @param int    $start
126
     * @return array
127
     */
128
129
    public function getAllConfigs(
130
        $criteria = [],
131
        $asobject = false,
132
        $sort = 'config_id',
133
        $order = 'ASC',
134
        $limit = 0,
135
        $start = 0
136
    ) {
137
        $db = XoopsDatabaseFactory::getDatabaseConnection();
138
139
        $ret = [];
140
141
        $whereQuery = '';
142
143
        if (\is_array($criteria) && \count($criteria) > 0) {
144
            $whereQuery = ' WHERE';
145
146
            foreach ($criteria as $c) {
147
                $whereQuery .= " ${c} AND";
148
            }
149
150
            $whereQuery = mb_substr($whereQuery, 0, -4);
151
        } elseif (!\is_array($criteria) && $criteria) {
152
            $whereQuery = ' WHERE ' . $criteria;
153
        }
154
155
        if (!$asobject) {
156
            $sql = 'SELECT config_id FROM ' . $db->prefix(
157
                    'yogurt_configs'
158
                ) . "${whereQuery} ORDER BY ${sort} ${order}";
159
160
            $result = $db->query($sql, $limit, $start);
161
162
            while (false !== ($myrow = $db->fetchArray($result))) {
163
                $ret[] = $myrow['yogurt_configs_id'];
164
            }
165
        } else {
166
            $sql = 'SELECT * FROM ' . $db->prefix('yogurt_configs') . "${whereQuery} ORDER BY ${sort} ${order}";
167
168
            $result = $db->query($sql, $limit, $start);
169
170
            while (false !== ($myrow = $db->fetchArray($result))) {
171
                $ret[] = new self($myrow);
172
            }
173
        }
174
175
        return $ret;
176
    }
177
178
    /**
179
     * Get form
180
     *
181
     * @return \XoopsModules\Yogurt\Form\ConfigsForm
182
     */
183
184
    public function getForm()
185
    {
186
        return new Form\ConfigsForm($this);
187
    }
188
189
    /**
190
     * @return array|null
191
     */
192
193
    public function getGroupsRead()
194
    {
195
        //$permHelper = new \Xmf\Module\Helper\Permission();
196
197
        return $this->permHelper->getGroupsForItem(
198
            'sbcolumns_read',
199
            $this->getVar('config_id')
200
        );
201
    }
202
203
    /**
204
     * @return array|null
205
     */
206
207
    public function getGroupsSubmit()
208
    {
209
        //$permHelper = new \Xmf\Module\Helper\Permission();
210
211
        return $this->permHelper->getGroupsForItem(
212
            'sbcolumns_submit',
213
            $this->getVar('config_id')
214
        );
215
    }
216
217
    /**
218
     * @return array|null
219
     */
220
221
    public function getGroupsModeration()
222
    {
223
        //$permHelper = new \Xmf\Module\Helper\Permission();
224
225
        return $this->permHelper->getGroupsForItem(
226
            'sbcolumns_moderation',
227
            $this->getVar('config_id')
228
        );
229
    }
230
}
231