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

Configs   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 192
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 18
eloc 67
dl 0
loc 192
rs 10
c 0
b 0
f 0

7 Methods

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