Notes::getGroupsRead()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Suico;
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
*/
14
15
/**
16
 * @category        Module
17
 * @copyright       {@link https://xoops.org/ XOOPS Project}
18
 * @license         GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
19
 * @author          Bruno Barthez, Marcello Brandão aka  Suico, Mamba, LioMJ  <https://xoops.org>
20
 */
21
22
use Xmf\Module\Helper\Permission;
23
use XoopsDatabaseFactory;
24
use XoopsObject;
25
26
require_once XOOPS_ROOT_PATH . '/kernel/object.php';
27
require_once XOOPS_ROOT_PATH . '/class/module.textsanitizer.php';
28
29
/**
30
 * Notes class.
31
 * $this class is responsible for providing data access mechanisms to the data source
32
 * of XOOPS user class objects.
33
 */
34
class Notes extends XoopsObject
35
{
36
    public \XoopsDatabase $db;
37
    public Helper         $helper;
38
    public Permission     $permHelper;
39
    public                $note_id;
40
    public $note_text;
41
    public $note_from;
42
    public $note_to;
43
    public $private;
44
    public $date_created;
45
    
46
    
47
    // constructor
48
49
    /**
50
     * Notes constructor.
51
     * @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...
52
     */
53
    public function __construct($id = null)
54
    {
55
        /** @var Helper $helper */
56
        $this->helper     = Helper::getInstance();
57
        $this->permHelper = new Permission();
58
        $this->db         = XoopsDatabaseFactory::getDatabaseConnection();
59
        $this->initVar('note_id', \XOBJ_DTYPE_INT, null, false, 10);
60
        $this->initVar('note_text', \XOBJ_DTYPE_OTHER, null, false);
61
        $this->initVar('note_from', \XOBJ_DTYPE_INT, null, false, 10);
62
        $this->initVar('note_to', \XOBJ_DTYPE_INT, null, false, 10);
63
        $this->initVar('private', \XOBJ_DTYPE_INT, null, false, 10);
64
        $this->initVar('date_created', \XOBJ_DTYPE_INT, 0, false);
65
        if (!empty($id)) {
66
            if (\is_array($id)) {
67
                $this->assignVars($id);
68
            } else {
69
                $this->load((int)$id);
70
            }
71
        } else {
72
            $this->setNew();
73
        }
74
    }
75
76
    /**
77
     * @param $id
78
     */
79
    public function load($id): void
80
    {
81
        $sql   = 'SELECT * FROM ' . $this->db->prefix('suico_notes') . ' WHERE note_id=' . $id;
82
        $myrow = $this->db->fetchArray($this->db->query($sql));
83
        $this->assignVars($myrow);
84
        if (!$myrow) {
85
            $this->setNew();
86
        }
87
    }
88
89
    /**
90
     * @param array  $criteria
91
     * @param bool   $asobject
92
     * @param string $sort
93
     * @param string $order
94
     * @param int    $limit
95
     * @param int    $start
96
     * @return array
97
     */
98
    public function getAllNotes(
99
        $criteria = [],
100
        $asobject = false,
101
        $sort = 'note_id',
102
        $order = 'ASC',
103
        $limit = 0,
104
        $start = 0
105
    ) {
106
        $db         = XoopsDatabaseFactory::getDatabaseConnection();
107
        $ret        = [];
108
        $whereQuery = '';
109
        if (\is_array($criteria) && \count($criteria) > 0) {
110
            $whereQuery = ' WHERE';
111
            foreach ($criteria as $c) {
112
                $whereQuery .= " {$c} AND";
113
            }
114
            $whereQuery = mb_substr($whereQuery, 0, -4);
115
        } elseif (!\is_array($criteria) && $criteria) {
116
            $whereQuery = ' WHERE ' . $criteria;
117
        }
118
        if ($asobject) {
119
            $sql    = 'SELECT * FROM ' . $db->prefix('suico_notes') . "{$whereQuery} ORDER BY {$sort} {$order}";
120
            $result = $db->query($sql, $limit, $start);
121
            while (false !== ($myrow = $db->fetchArray($result))) {
122
                $ret[] = new self($myrow);
123
            }
124
        } else {
125
            $sql    = 'SELECT note_id FROM ' . $db->prefix('suico_notes') . "{$whereQuery} ORDER BY {$sort} {$order}";
126
            $result = $db->query($sql, $limit, $start);
127
            while (false !== ($myrow = $db->fetchArray($result))) {
128
                $ret[] = $myrow['suico_notes_id'];
129
            }
130
        }
131
132
        return $ret;
133
    }
134
135
    /**
136
     * Get form
137
     *
138
     * @return \XoopsModules\Suico\Form\NotesForm
139
     */
140
    public function getForm()
141
    {
142
        return new Form\NotesForm($this);
143
    }
144
145
    /**
146
     * @return array|null
147
     */
148
    public function getGroupsRead()
149
    {
150
        //$permHelper = new \Xmf\Module\Helper\Permission();
151
        return $this->permHelper->getGroupsForItem(
152
            'sbcolumns_read',
153
            $this->getVar('note_id')
154
        );
155
    }
156
157
    /**
158
     * @return array|null
159
     */
160
    public function getGroupsSubmit()
161
    {
162
        //$permHelper = new \Xmf\Module\Helper\Permission();
163
        return $this->permHelper->getGroupsForItem(
164
            'sbcolumns_submit',
165
            $this->getVar('note_id')
166
        );
167
    }
168
169
    /**
170
     * @return array|null
171
     */
172
    public function getGroupsModeration()
173
    {
174
        //$permHelper = new \Xmf\Module\Helper\Permission();
175
        return $this->permHelper->getGroupsForItem(
176
            'sbcolumns_moderation',
177
            $this->getVar('note_id')
178
        );
179
    }
180
}
181