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

Notes::getAllNotes()   B

Complexity

Conditions 9
Paths 12

Size

Total Lines 45
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 45
rs 8.0555
c 0
b 0
f 0
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
 * @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
25
use Xmf\Module\Helper\Permission;
26
use XoopsDatabaseFactory;
27
use XoopsObject;
28
29
require_once XOOPS_ROOT_PATH . '/kernel/object.php';
30
require_once XOOPS_ROOT_PATH . '/class/module.textsanitizer.php';
31
32
/**
33
 * Notes class.
34
 * $this class is responsible for providing data access mechanisms to the data source
35
 * of XOOPS user class objects.
36
 */
37
class Notes extends XoopsObject
38
{
39
    public $db;
40
41
    public $helper;
42
43
    public $permHelper;
44
45
    // constructor
46
47
    /**
48
     * Notes constructor.
49
     * @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...
50
     */
51
52
    public function __construct($id = null)
53
    {
54
        /** @var Helper $helper */
55
56
        $this->helper = Helper::getInstance();
57
58
        $this->permHelper = new Permission();
59
60
        $this->db = XoopsDatabaseFactory::getDatabaseConnection();
61
62
        $this->initVar('note_id', \XOBJ_DTYPE_INT, null, false, 10);
63
64
        $this->initVar('note_text', \XOBJ_DTYPE_OTHER, null, false);
65
66
        $this->initVar('note_from', \XOBJ_DTYPE_INT, null, false, 10);
67
68
        $this->initVar('note_to', \XOBJ_DTYPE_INT, null, false, 10);
69
70
        $this->initVar('private', \XOBJ_DTYPE_INT, null, false, 10);
71
72
        $this->initVar('date_created', \XOBJ_DTYPE_INT, 0, false);
73
74
        if (!empty($id)) {
75
            if (\is_array($id)) {
76
                $this->assignVars($id);
77
            } else {
78
                $this->load((int)$id);
79
            }
80
        } else {
81
            $this->setNew();
82
        }
83
    }
84
85
    /**
86
     * @param $id
87
     */
88
89
    public function load($id)
90
    {
91
        $sql = 'SELECT * FROM ' . $this->db->prefix('yogurt_notes') . ' WHERE note_id=' . $id;
92
93
        $myrow = $this->db->fetchArray($this->db->query($sql));
94
95
        $this->assignVars($myrow);
96
97
        if (!$myrow) {
98
            $this->setNew();
99
        }
100
    }
101
102
    /**
103
     * @param array  $criteria
104
     * @param bool   $asobject
105
     * @param string $sort
106
     * @param string $order
107
     * @param int    $limit
108
     * @param int    $start
109
     * @return array
110
     */
111
112
    public function getAllNotes(
113
        $criteria = [],
114
        $asobject = false,
115
        $sort = 'note_id',
116
        $order = 'ASC',
117
        $limit = 0,
118
        $start = 0
119
    ) {
120
        $db = XoopsDatabaseFactory::getDatabaseConnection();
121
122
        $ret = [];
123
124
        $whereQuery = '';
125
126
        if (\is_array($criteria) && \count($criteria) > 0) {
127
            $whereQuery = ' WHERE';
128
129
            foreach ($criteria as $c) {
130
                $whereQuery .= " ${c} AND";
131
            }
132
133
            $whereQuery = mb_substr($whereQuery, 0, -4);
134
        } elseif (!\is_array($criteria) && $criteria) {
135
            $whereQuery = ' WHERE ' . $criteria;
136
        }
137
138
        if (!$asobject) {
139
            $sql = 'SELECT note_id FROM ' . $db->prefix('yogurt_notes') . "${whereQuery} ORDER BY ${sort} ${order}";
140
141
            $result = $db->query($sql, $limit, $start);
142
143
            while (false !== ($myrow = $db->fetchArray($result))) {
144
                $ret[] = $myrow['yogurt_notes_id'];
145
            }
146
        } else {
147
            $sql = 'SELECT * FROM ' . $db->prefix('yogurt_notes') . "${whereQuery} ORDER BY ${sort} ${order}";
148
149
            $result = $db->query($sql, $limit, $start);
150
151
            while (false !== ($myrow = $db->fetchArray($result))) {
152
                $ret[] = new self($myrow);
153
            }
154
        }
155
156
        return $ret;
157
    }
158
159
    /**
160
     * Get form
161
     *
162
     * @return \XoopsModules\Yogurt\Form\NotesForm
163
     */
164
165
    public function getForm()
166
    {
167
        return new Form\NotesForm($this);
168
    }
169
170
    /**
171
     * @return array|null
172
     */
173
174
    public function getGroupsRead()
175
    {
176
        //$permHelper = new \Xmf\Module\Helper\Permission();
177
178
        return $this->permHelper->getGroupsForItem(
179
            'sbcolumns_read',
180
            $this->getVar('note_id')
181
        );
182
    }
183
184
    /**
185
     * @return array|null
186
     */
187
188
    public function getGroupsSubmit()
189
    {
190
        //$permHelper = new \Xmf\Module\Helper\Permission();
191
192
        return $this->permHelper->getGroupsForItem(
193
            'sbcolumns_submit',
194
            $this->getVar('note_id')
195
        );
196
    }
197
198
    /**
199
     * @return array|null
200
     */
201
202
    public function getGroupsModeration()
203
    {
204
        //$permHelper = new \Xmf\Module\Helper\Permission();
205
206
        return $this->permHelper->getGroupsForItem(
207
            'sbcolumns_moderation',
208
            $this->getVar('note_id')
209
        );
210
    }
211
}
212