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

class/NotesHandler.php (2 issues)

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