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

Notes::getGroupsRead()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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