Passed
Push — master ( 4761c2...696f77 )
by
unknown
05:50 queued 19s
created

class/NotesHandler.php (1 issue)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace XoopsModules\Suico;
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         suico
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 CriteriaElement;
26
use MyTextSanitizer;
27
use XoopsDatabase;
28
use XoopsObject;
29
use XoopsPersistableObjectHandler;
30
31
require_once XOOPS_ROOT_PATH . '/kernel/object.php';
32
require_once XOOPS_ROOT_PATH . '/class/module.textsanitizer.php';
33
34
/**
35
 * NotesHandler class.
36
 * This class provides simple mechanism forNotes object
37
 */
38
class NotesHandler extends XoopsPersistableObjectHandler
39
{
40
    public $helper;
41
    public $isAdmin;
42
43
    /**
44
     * Constructor
45
     * @param \XoopsDatabase|null             $xoopsDatabase
46
     * @param \XoopsModules\Suico\Helper|null $helper
47
     */
48
    public function __construct(
49
        ?XoopsDatabase $xoopsDatabase = null,
50
        $helper = null
51
    ) {
52
        /** @var \XoopsModules\Suico\Helper $this ->helper */
53
        if (null === $helper) {
54
            $this->helper = Helper::getInstance();
55
        } else {
56
            $this->helper = $helper;
57
        }
58
        $isAdmin = $this->helper->isUserAdmin();
59
        parent::__construct($xoopsDatabase, 'suico_notes', Notes::class, 'note_id', 'note_id');
60
    }
61
62
    /**
63
     * create a new Groups
64
     *
65
     * @param bool $isNew flag the new objects as "new"?
66
     * @return \XoopsObject Groups
67
     */
68
    public function create(
69
        $isNew = true
70
    ) {
71
        $obj = parent::create($isNew);
72
        if ($isNew) {
73
            $obj->setNew();
74
        } else {
75
            $obj->unsetNew();
76
        }
77
        $obj->helper = $this->helper;
78
        return $obj;
79
    }
80
81
    /**
82
     * retrieve aNotes
83
     *
84
     * @param int  $id of theNotes
85
     * @param null $fields
86
     * @return mixed reference to the {@linkNotes} object, FALSE if failed
87
     */
88
    public function get2(
89
        $id = null,
90
        $fields = null
91
    ) {
92
        $sql = 'SELECT * FROM ' . $this->db->prefix('suico_notes') . ' WHERE note_id=' . $id;
93
        if (!$result = $this->db->query($sql)) {
94
            return false;
95
        }
96
        $numrows = $this->db->getRowsNum($result);
97
        if (1 === $numrows) {
98
            $suico_notes = new Notes();
99
            $suico_notes->assignVars($this->db->fetchArray($result));
100
            return $suico_notes;
101
        }
102
        return false;
103
    }
104
105
    /**
106
     * insert a new Notes in the database
107
     *
108
     * @param \XoopsObject $xoopsObject   reference to the {@linkNotes}
109
     *                                    object
110
     * @param bool         $force
111
     * @return bool FALSE if failed, TRUE if already present and unchanged or successful
112
     */
113
    public function insert2(
114
        XoopsObject $xoopsObject,
115
        $force = false
116
    ) {
117
        global $xoopsConfig;
118
        if (!$xoopsObject instanceof Notes) {
119
            return false;
120
        }
121
        if (!$xoopsObject->isDirty()) {
122
            return true;
123
        }
124
        if (!$xoopsObject->cleanVars()) {
125
            return false;
126
        }
127
        $noteId = $note_from = $note_to = $date_created = $private = '';
128
        foreach ($xoopsObject->cleanVars as $k => $v) {
129
            ${$k} = $v;
130
        }
131
        //        $now = 'date_add(now(), interval ' . $xoopsConfig['server_TZ'] . ' hour)';
132
        if ($xoopsObject->isNew()) {
133
            // add / modify a Notes
134
            $xoopsObject = new Notes();
135
            $format      = 'INSERT INTO %s (note_id, note_text, note_from, note_to, date_created, private)';
136
            $format      .= 'VALUES (%u, %s, %u, %u, %u,%u)';
137
            $sql         = \sprintf(
138
                $format,
139
                $this->db->prefix('suico_notes'),
140
                $noteId,
141
                $this->db->quoteString($note_text),
142
                $note_from,
143
                $note_to,
144
                $date_created,
145
                $private
146
            );
147
            $force       = true;
148
        } else {
149
            $format = 'UPDATE %s SET ';
150
            $format .= 'note_id=%u, note_text=%s, note_from=%u, note_to=%u, date_created=%u, private=%u';
151
            $format .= ' WHERE note_id = %u';
152
            $sql    = \sprintf(
153
                $format,
154
                $this->db->prefix('suico_notes'),
155
                $noteId,
156
                $this->db->quoteString($note_text),
157
                $note_from,
158
                $note_to,
159
                $date_created,
160
                $private,
161
                $noteId
162
            );
163
        }
164
        if ($force) {
165
            $result = $this->db->queryF($sql);
166
        } else {
167
            $result = $this->db->query($sql);
168
        }
169
        if (!$result) {
170
            return false;
171
        }
172
        if (empty($noteId)) {
0 ignored issues
show
The condition empty($noteId) is always true.
Loading history...
173
            $noteId = $this->db->getInsertId();
174
        }
175
        $xoopsObject->assignVar('note_id', $noteId);
176
        return true;
177
    }
178
179
    /**
180
     * delete aNotes from the database
181
     *
182
     * @param \XoopsObject $xoopsObject reference to theNotes to delete
183
     * @param bool         $force
184
     * @return bool FALSE if failed.
185
     */
186
    public function delete(
187
        XoopsObject $xoopsObject,
188
        $force = false
189
    ) {
190
        if (!$xoopsObject instanceof Notes) {
191
            return false;
192
        }
193
        $sql = \sprintf(
194
            'DELETE FROM %s WHERE note_id = %u',
195
            $this->db->prefix('suico_notes'),
196
            $xoopsObject->getVar('note_id')
197
        );
198
        if ($force) {
199
            $result = $this->db->queryF($sql);
200
        } else {
201
            $result = $this->db->query($sql);
202
        }
203
        if (!$result) {
204
            return false;
205
        }
206
        return true;
207
    }
208
209
    /**
210
     * retrieve suico_notes from the database
211
     *
212
     * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link CriteriaElement} conditions to be met
213
     * @param bool                                 $id_as_key       use the UID as key for the array?
214
     * @param bool                                 $as_object
215
     * @return array array of {@linkNotes} objects
216
     */
217
    public function &getObjects(
218
        ?CriteriaElement $criteriaElement = null,
219
        $id_as_key = false,
220
        $as_object = true
221
    ) {
222
        $ret   = [];
223
        $limit = $start = 0;
224
        $sql   = 'SELECT * FROM ' . $this->db->prefix('suico_notes');
225
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
226
            $sql .= ' ' . $criteriaElement->renderWhere();
227
            if ('' !== $criteriaElement->getSort()) {
228
                $sql .= ' ORDER BY ' . $criteriaElement->getSort() . ' ' . $criteriaElement->getOrder();
229
            }
230
            $limit = $criteriaElement->getLimit();
231
            $start = $criteriaElement->getStart();
232
        }
233
        $result = $this->db->query($sql, $limit, $start);
234
        if (!$result) {
235
            return $ret;
236
        }
237
        while (false !== ($myrow = $this->db->fetchArray($result))) {
238
            $suico_notes = new Notes();
239
            $suico_notes->assignVars($myrow);
240
            if (!$id_as_key) {
241
                $ret[] = &$suico_notes;
242
            } else {
243
                $ret[$myrow['note_id']] = &$suico_notes;
244
            }
245
            unset($suico_notes);
246
        }
247
        return $ret;
248
    }
249
250
    /**
251
     * count suico_notes matching a condition
252
     *
253
     * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link CriteriaElement} to match
254
     * @return int count of suico_notes
255
     */
256
    public function getCount(
257
        ?CriteriaElement $criteriaElement = null
258
    ) {
259
        $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('suico_notes');
260
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
261
            $sql .= ' ' . $criteriaElement->renderWhere();
262
        }
263
        $result = $this->db->query($sql);
264
        if (!$result) {
265
            return 0;
266
        }
267
        [$count] = $this->db->fetchRow($result);
268
        return (int)$count;
269
    }
270
271
    /**
272
     * delete suico_notes matching a set of conditions
273
     *
274
     * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link CriteriaElement}
275
     * @param bool                                 $force
276
     * @param bool                                 $asObject
277
     * @return bool FALSE if deletion failed
278
     */
279
    public function deleteAll(
280
        ?CriteriaElement $criteriaElement = null,
281
        $force = true,
282
        $asObject = false
283
    ) {
284
        $sql = 'DELETE FROM ' . $this->db->prefix('suico_notes');
285
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
286
            $sql .= ' ' . $criteriaElement->renderWhere();
287
        }
288
        if (!$result = $this->db->query($sql)) {
289
            return false;
290
        }
291
        return true;
292
    }
293
294
    /**
295
     * @param                                      $countNotes
296
     * @param \CriteriaElement|\CriteriaCompo|null $criteria
297
     * @return array
298
     */
299
    public function getNotes(
300
        $countNotes,
301
        $criteria
302
    ) {
303
        $myts = new MyTextSanitizer();
304
        $ret  = [];
305
        $sql  = 'SELECT note_id, uid, uname, user_avatar, note_from, note_text, date_created FROM ' . $this->db->prefix(
306
                'suico_notes'
307
            ) . ', ' . $this->db->prefix(
308
                'users'
309
            );
310
        if (isset($criteria) && $criteria instanceof CriteriaElement) {
311
            $sql .= ' ' . $criteria->renderWhere();
312
            //attention here this is kind of a hack
313
            $sql .= ' AND uid = note_from';
314
            if ('' !== $criteria->getSort()) {
315
                $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder();
316
            }
317
            $limit  = $criteria->getLimit();
318
            $start  = $criteria->getStart();
319
            $result = $this->db->query($sql, $limit, $start);
320
            $vetor  = [];
321
            $i      = 0;
322
            while (false !== ($myrow = $this->db->fetchArray($result))) {
323
                $vetor[$i]['uid']          = $myrow['uid'];
324
                $vetor[$i]['uname']        = $myrow['uname'];
325
                $vetor[$i]['user_avatar']  = $myrow['user_avatar'];
326
                $temptext                  = $myts->xoopsCodeDecode($myrow['note_text'], 1);
327
                $vetor[$i]['text']         = $myts->nl2Br($temptext);
328
                $vetor[$i]['id']           = $myrow['note_id'];
329
                $vetor[$i]['date_created'] = \formatTimestamp($myrow['date_created'], 's');
330
                $i++;
331
            }
332
            return $vetor;
333
        }
334
    }
335
}
336