Completed
Branch master (3041d9)
by Pierre-Henry
35:57
created

NoteModel::addPost()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 20
nc 1
nop 1
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author         Pierre-Henry Soria <[email protected]>
4
 * @copyright      (c) 2012-2017, Pierre-Henry Soria. All Rights Reserved.
5
 * @license        GNU General Public License; See PH7.LICENSE.txt and PH7.COPYRIGHT.txt in the root directory.
6
 * @package        PH7 / App / System / Module / Note / Model
7
 */
8
9
namespace PH7;
10
11
use PH7\Framework\Mvc\Model\Engine\Db;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, PH7\Db.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
12
13
class NoteModel extends NoteCoreModel
14
{
15
    /**
16
     * @param integer|null $iNoteId
17
     * @param integer $iOffset
18
     * @param integer $iLimit
19
     * @param boolean $bCount
20
     *
21
     * @return \stdClass
22
     */
23
    public function getCategory($iNoteId = null, $iOffset, $iLimit, $bCount = false)
24
    {
25
        $this->cache->start(self::CACHE_GROUP, 'category' . $iNoteId . $iOffset . $iLimit . $bCount, static::CACHE_TIME);
26
        if (!$oData = $this->cache->get()) {
27
            $iOffset = (int) $iOffset;
28
            $iLimit = (int) $iLimit;
29
30
            if ($bCount) {
31
                $sSql = 'SELECT *, COUNT(c.noteId) AS totalCatNotes FROM' . Db::prefix('NotesDataCategories') . 'AS d INNER JOIN' . Db::prefix('NotesCategories') . 'AS c ON d.categoryId = c.categoryId GROUP BY d.name ASC LIMIT :offset, :limit';
32
            } else {
33
                $sSqlNoteId = (isset($iNoteId)) ? ' INNER JOIN ' . Db::prefix('NotesCategories') . 'AS c ON d.categoryId = c.categoryId WHERE c.noteId = :noteId ' : ' ';
34
                $sSql = 'SELECT * FROM' . Db::prefix('NotesDataCategories') . 'AS d' . $sSqlNoteId . 'ORDER BY d.name ASC LIMIT :offset, :limit';
35
            }
36
37
            $rStmt = Db::getInstance()->prepare($sSql);
38
39
            if (isset($iNoteId)) {
40
                $rStmt->bindParam(':noteId', $iNoteId, \PDO::PARAM_INT);
41
            }
42
43
            $rStmt->bindParam(':offset', $iOffset, \PDO::PARAM_INT);
44
            $rStmt->bindParam(':limit', $iLimit, \PDO::PARAM_INT);
45
            $rStmt->execute();
46
            $oData = $rStmt->fetchAll(\PDO::FETCH_OBJ);
47
            Db::free($rStmt);
48
            $this->cache->put($oData);
49
        }
50
51
        return $oData;
52
    }
53
54
    /**
55
     * @param integer $iOffset
56
     * @param integer $iLimit
57
     * @param boolean $bCount
58
     *
59
     * @return \stdClass
60
     */
61
    public function getAuthor($iOffset, $iLimit, $bCount = false)
62
    {
63
        $this->cache->start(self::CACHE_GROUP, 'author' . $iOffset . $iLimit . $bCount, static::CACHE_TIME);
64
65
        if (!$oData = $this->cache->get()) {
66
            $iOffset = (int) $iOffset;
67
            $iLimit = (int) $iLimit;
68
69
            $sSelect = ($bCount)  ? '*, COUNT(n.noteId) AS totalAuthors' : '*';
70
71
            $rStmt = Db::getInstance()->prepare('SELECT ' . $sSelect . ' FROM' . Db::prefix('Notes') . 'AS n INNER JOIN' . Db::prefix('Members') . 'AS m ON n.profileId = m.profileId GROUP BY m.username ASC LIMIT :offset, :limit');
72
73
            $rStmt->bindParam(':offset', $iOffset, \PDO::PARAM_INT);
74
            $rStmt->bindParam(':limit', $iLimit, \PDO::PARAM_INT);
75
            $rStmt->execute();
76
            $oData = $rStmt->fetchAll(\PDO::FETCH_OBJ);
77
            Db::free($rStmt);
78
            $this->cache->put($oData);
79
        }
80
81
        return $oData;
82
    }
83
84
    /**
85
     * @param integer $iCategoryId
86
     * @param integer $iNoteId
87
     * @param integer $iProfileId
88
     */
89
    public function addCategory($iCategoryId, $iNoteId, $iProfileId)
90
    {
91
        $rStmt = Db::getInstance()->prepare('INSERT INTO' . Db::prefix('NotesCategories') . '(categoryId, noteId, profileId) VALUES(:categoryId, :noteId, :profileId)');
92
        $rStmt->bindParam(':categoryId', $iCategoryId, \PDO::PARAM_INT);
93
        $rStmt->bindParam(':noteId', $iNoteId, \PDO::PARAM_INT);
94
        $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
95
        $rStmt->execute();
96
        Db::free($rStmt);
97
    }
98
99
    /**
100
     * @param string $sPostId
101
     * @param integer $iProfileId
102
     * @param integer $iApproved
103
     *
104
     * @return \stdClass
105
     */
106
    public function readPost($sPostId, $iProfileId, $iApproved = 1)
107
    {
108
        $this->cache->start(self::CACHE_GROUP, 'readPost' . $sPostId . $iProfileId . $iApproved, static::CACHE_TIME);
109
110
        if (!$oData = $this->cache->get()) {
111
            $sSqlApproved = (isset($iApproved)) ? ' AND approved = :approved' : '';
112
113
            $rStmt = Db::getInstance()->prepare('SELECT n.*, c.*, m.username, m.firstName, m.sex FROM' . Db::prefix('Notes') . 'AS n LEFT JOIN' . Db::prefix('NotesCategories') . 'AS c ON n.noteId = c.noteId INNER JOIN' . Db::prefix('Members') . ' AS m ON n.profileId = m.profileId WHERE n.profileId = :profileId AND n.postId = :postId' . $sSqlApproved . ' LIMIT 1');
114
            $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
115
            $rStmt->bindValue(':postId', $sPostId, \PDO::PARAM_STR);
116
            if (isset($iApproved)) {
117
                $rStmt->bindValue(':approved', $iApproved, \PDO::PARAM_INT);
118
            }
119
            $rStmt->execute();
120
            $oData = $rStmt->fetch(\PDO::FETCH_OBJ);
121
            Db::free($rStmt);
122
            $this->cache->put($oData);
123
        }
124
125
        return $oData;
126
    }
127
128
    /**
129
     * @param array $aData
130
     *
131
     * @return boolean
132
     */
133
    public function addPost(array $aData)
134
    {
135
        $rStmt = Db::getInstance()->prepare('INSERT INTO' . Db::prefix('Notes') .
136
            '(profileId, postId, langId, title, content, slogan, tags, pageTitle, metaDescription, metaKeywords, metaRobots, metaAuthor, metaCopyright, enableComment, createdDate, approved)
137
            VALUES (:profileId, :postId, :langId, :title, :content, :slogan, :tags, :pageTitle, :metaDescription, :metaKeywords, :metaRobots, :metaAuthor, :metaCopyright, :enableComment, :createdDate, :approved)');
138
139
        $rStmt->bindValue(':profileId', $aData['profile_id'], \PDO::PARAM_INT);
140
        $rStmt->bindValue(':postId', $aData['post_id'], \PDO::PARAM_STR);
141
        $rStmt->bindValue(':langId', $aData['lang_id'], \PDO::PARAM_STR);
142
        $rStmt->bindValue(':title', $aData['title'], \PDO::PARAM_STR);
143
        $rStmt->bindValue(':content', $aData['content'], \PDO::PARAM_STR);
144
        $rStmt->bindValue(':slogan', $aData['slogan'], \PDO::PARAM_STR);
145
        $rStmt->bindValue(':tags', $aData['tags'], \PDO::PARAM_STR);
146
        $rStmt->bindValue(':pageTitle', $aData['page_title'], \PDO::PARAM_STR);
147
        $rStmt->bindValue(':metaDescription', $aData['meta_description'], \PDO::PARAM_STR);
148
        $rStmt->bindValue(':metaKeywords', $aData['meta_keywords'], \PDO::PARAM_STR);
149
        $rStmt->bindValue(':metaRobots', $aData['meta_robots'], \PDO::PARAM_STR);
150
        $rStmt->bindValue(':metaAuthor', $aData['meta_author'], \PDO::PARAM_STR);
151
        $rStmt->bindValue(':metaCopyright', $aData['meta_copyright'], \PDO::PARAM_STR);
152
        $rStmt->bindValue(':enableComment', $aData['enable_comment'], \PDO::PARAM_INT);
153
        $rStmt->bindValue(':createdDate', $aData['created_date'], \PDO::PARAM_STR);
154
        $rStmt->bindValue(':approved', $aData['approved'], \PDO::PARAM_INT);
155
156
        return $rStmt->execute();
157
    }
158
159
    /**
160
     * @param string $sCategoryName
161
     * @param boolean $bCount
162
     * @param string $sOrderBy
163
     * @param integer $iSort
164
     * @param integer $iOffset
165
     * @param integer $iLimit
166
     *
167
     * @return integer|\stdClass
168
     */
169
    public function category($sCategoryName, $bCount, $sOrderBy, $iSort, $iOffset, $iLimit)
170
    {
171
        $bCount = (bool) $bCount;
172
        $iOffset = (int) $iOffset;
173
        $iLimit = (int) $iLimit;
174
        $sCategoryName = trim($sCategoryName);
175
176
        $sSqlOrder = SearchCoreModel::order($sOrderBy, $iSort, 'n');
177
178
        $sSqlLimit = (!$bCount) ?  'LIMIT :offset, :limit' : '';
179
        $sSqlSelect = (!$bCount) ?  'n.*, c.*, d.*, m.username, m.firstName, m.sex' : 'COUNT(n.noteId) AS totalNotes';
180
181
        $rStmt = Db::getInstance()->prepare('SELECT ' . $sSqlSelect . ' FROM' . Db::prefix('Notes') . 'AS n LEFT JOIN ' . Db::prefix('NotesCategories') . 'AS c ON n.noteId = c.noteId LEFT JOIN' .
182
                Db::prefix('NotesDataCategories') . 'AS d ON c.categoryId = d.categoryId INNER JOIN' . Db::prefix('Members') . 'AS m ON n.profileId = m.profileId WHERE d.name LIKE :name' . $sSqlOrder . $sSqlLimit);
183
184
        $rStmt->bindValue(':name', '%' . $sCategoryName . '%', \PDO::PARAM_STR);
185
186
        if (!$bCount) {
187
            $rStmt->bindParam(':offset', $iOffset, \PDO::PARAM_INT);
188
            $rStmt->bindParam(':limit', $iLimit, \PDO::PARAM_INT);
189
        }
190
191
        $rStmt->execute();
192
193
        if (!$bCount) {
194
            $mData = $rStmt->fetchAll(\PDO::FETCH_OBJ);
195
            Db::free($rStmt);
196
        } else {
197
            $oRow = $rStmt->fetch(\PDO::FETCH_OBJ);
198
            Db::free($rStmt);
199
            $mData = (int) $oRow->totalNotes;
200
            unset($oRow);
201
        }
202
203
        return $mData;
204
    }
205
206
    /**
207
     * @param string $sAuthor
208
     * @param boolean $bCount
209
     * @param string $sOrderBy
210
     * @param integer $iSort
211
     * @param integer $iOffset
212
     * @param integer $iLimit
213
     *
214
     * @return integer|\stdClass
215
     */
216
    public function author($sAuthor, $bCount, $sOrderBy, $iSort, $iOffset, $iLimit)
217
    {
218
        $bCount = (bool) $bCount;
219
        $iOffset = (int) $iOffset;
220
        $iLimit = (int) $iLimit;
221
        $sAuthor = trim($sAuthor);
222
223
        $sSqlOrder = SearchCoreModel::order($sOrderBy, $iSort, 'n');
224
225
        $sSqlLimit = (!$bCount) ?  'LIMIT :offset, :limit' : '';
226
        $sSqlSelect = (!$bCount) ?  'n.*, m.username, m.firstName, m.sex' : 'COUNT(m.profileId) AS totalAuthors';
227
228
        $rStmt = Db::getInstance()->prepare('SELECT ' . $sSqlSelect . ' FROM' . Db::prefix('Notes') . 'AS n
229
                INNER JOIN' . Db::prefix('Members') . 'AS m ON n.profileId = m.profileId WHERE m.username LIKE :name' . $sSqlOrder . $sSqlLimit);
230
231
        $rStmt->bindValue(':name', '%' . $sAuthor . '%', \PDO::PARAM_STR);
232
233
        if (!$bCount) {
234
            $rStmt->bindParam(':offset', $iOffset, \PDO::PARAM_INT);
235
            $rStmt->bindParam(':limit', $iLimit, \PDO::PARAM_INT);
236
        }
237
238
        $rStmt->execute();
239
240
        if (!$bCount) {
241
            $mData = $rStmt->fetchAll(\PDO::FETCH_OBJ);
242
            Db::free($rStmt);
243
        } else {
244
            $oRow = $rStmt->fetch(\PDO::FETCH_OBJ);
245
            Db::free($rStmt);
246
            $mData = (int) $oRow->totalAuthors;
247
            unset($oRow);
248
        }
249
250
        return $mData;
251
    }
252
253
    /**
254
     * @param integer|string $mLooking Integer for post ID or string for a keyword
255
     * @param boolean $bCount Put 'true' for count the notes or 'false' for the result of notes.
256
     * @param string $sOrderBy
257
     * @param integer $iSort
258
     * @param integer $iOffset
259
     * @param integer $iLimit
260
     * @param integer $iApproved (0 = Unmoderated | 1 = Approved | NULL = unmoderated and approved) Default 1
261
     *
262
     * @return integer|\stdClass (integer for the number notes returned or an object containing the notes list)
263
     */
264
    public function search($mLooking, $bCount, $sOrderBy, $iSort, $iOffset, $iLimit, $iApproved = 1)
265
    {
266
        $bCount = (bool) $bCount;
267
        $iOffset = (int) $iOffset;
268
        $iLimit = (int) $iLimit;
269
        $mLooking = trim($mLooking);
270
271
        $sSqlApproved = (isset($iApproved)) ? ' AND (approved = :approved)' : '';
272
        $sSqlOrder = SearchCoreModel::order($sOrderBy, $iSort, 'n');
273
274
        $sSqlLimit = (!$bCount) ?  'LIMIT :offset, :limit' : '';
275
        $sSqlSelect = (!$bCount) ?  'n.*, m.username, m.firstName, m.sex' : 'COUNT(noteId) AS totalNotes';
276
277
        if (ctype_digit($mLooking)) {
278
            $sSqlWhere = ' WHERE (noteId = :looking)';
279
        } else {
280
            $sSqlWhere = ' WHERE (postId LIKE :looking OR title LIKE :looking OR
281
                pageTitle LIKE :looking OR content LIKE :looking OR tags LIKE :looking OR username LIKE :looking OR firstName LIKE :looking OR lastName LIKE :looking)';
282
        }
283
284
        $rStmt = Db::getInstance()->prepare('SELECT ' . $sSqlSelect . ' FROM' . Db::prefix('Notes') . 'AS n INNER JOIN' . Db::prefix('Members') . 'AS m ON n.profileId = m.profileId' . $sSqlWhere . $sSqlApproved . $sSqlOrder . $sSqlLimit);
285
286
        (ctype_digit($mLooking)) ? $rStmt->bindValue(':looking', $mLooking, \PDO::PARAM_INT) : $rStmt->bindValue(':looking', '%' . $mLooking . '%', \PDO::PARAM_STR);
287
288
        if (isset($iApproved)) {
289
            $rStmt->bindParam(':approved', $iApproved, \PDO::PARAM_INT);
290
        }
291
292
        if (!$bCount) {
293
            $rStmt->bindParam(':offset', $iOffset, \PDO::PARAM_INT);
294
            $rStmt->bindParam(':limit', $iLimit, \PDO::PARAM_INT);
295
        }
296
297
        $rStmt->execute();
298
299
        if (!$bCount) {
300
            $mData = $rStmt->fetchAll(\PDO::FETCH_OBJ);
301
            Db::free($rStmt);
302
        } else {
303
            $oRow = $rStmt->fetch(\PDO::FETCH_OBJ);
304
            Db::free($rStmt);
305
            $mData = (int) $oRow->totalNotes;
306
            unset($oRow);
307
        }
308
309
        return $mData;
310
    }
311
312
    /**
313
     * @param integer $iNoteId
314
     *
315
     * @return string
316
     */
317
    public function getPostId($iNoteId)
318
    {
319
        $this->cache->start(self::CACHE_GROUP, 'postId' . $iNoteId, static::CACHE_TIME);
320
321
        if (!$sData = $this->cache->get()) {
322
            $rStmt = Db::getInstance()->prepare('SELECT postId FROM' . Db::prefix('Notes') . ' WHERE noteId = :noteId LIMIT 1');
323
            $rStmt->bindValue(':noteId', $iNoteId, \PDO::PARAM_INT);
324
            $rStmt->execute();
325
            $oRow = $rStmt->fetch(\PDO::FETCH_OBJ);
326
            Db::free($rStmt);
327
            $sData = @$oRow->postId;
328
            unset($oRow);
329
            $this->cache->put($sData);
330
        }
331
332
        return $sData;
333
    }
334
335
    /**
336
     * @param integer $sPostId
337
     * @param integer $iProfileId
338
     *
339
     * @return boolean
340
     */
341
    public function postIdExists($sPostId, $iProfileId)
342
    {
343
        $this->cache->start(self::CACHE_GROUP, 'postIdExists' . $sPostId . $iProfileId, static::CACHE_TIME);
344
345
        if (!$bData = $this->cache->get()) {
346
            $rStmt = Db::getInstance()->prepare('SELECT COUNT(postId) FROM'.Db::prefix('Notes').'WHERE postId = :postId AND profileId = :profileId LIMIT 1');
347
            $rStmt->bindValue(':postId', $sPostId, \PDO::PARAM_STR);
348
            $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
349
            $rStmt->execute();
350
            $bData = ($rStmt->fetchColumn() == 1);
351
            Db::free($rStmt);
352
            $this->cache->put($bData);
0 ignored issues
show
Documentation introduced by
$bData is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
353
        }
354
355
        return $bData;
356
    }
357
358
    /**
359
     * @param integer $iNoteId
360
     * @param integer $iProfileId
361
     *
362
     * @return boolean
363
     */
364
    public function deletePost($iNoteId, $iProfileId)
365
    {
366
        $iNoteId = (int) $iNoteId;
367
        $iProfileId = (int) $iProfileId;
368
369
        $rStmt = Db::getInstance()->prepare('DELETE FROM' . Db::prefix('Notes') . 'WHERE noteId = :noteId AND profileId = :profileId');
370
        $rStmt->bindValue(':noteId', $iNoteId, \PDO::PARAM_INT);
371
        $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
372
373
        return $rStmt->execute();
374
    }
375
376
    /**
377
     * @param integer $iNoteId
378
     */
379
    public function deleteCategory($iNoteId)
380
    {
381
        $iNoteId = (int) $iNoteId;
382
383
        $rStmt = Db::getInstance()->prepare('DELETE FROM' . Db::prefix('NotesCategories') . 'WHERE noteId = :noteId');
384
        $rStmt->bindValue(':noteId', $iNoteId, \PDO::PARAM_INT);
385
        $rStmt->execute();
386
    }
387
388
    /**
389
     * @param integer $iNoteId
390
     * @param integer $iProfileId
391
     */
392
    public function deleteThumb($iNoteId, $iProfileId)
393
    {
394
        $iNoteId = (int) $iNoteId;
395
        $iProfileId = (int) $iProfileId;
396
397
        $this->updatePost('thumb', null, $iNoteId, $iProfileId);
398
    }
399
400
    /**
401
     * @param string $sSection
402
     * @param string $sValue
403
     * @param integer $iNoteId
404
     * @param integer $iProfileId
405
     *
406
     * @return boolean
407
     */
408
    public function updatePost($sSection, $sValue, $iNoteId, $iProfileId)
409
    {
410
        $rStmt = Db::getInstance()->prepare('UPDATE'.Db::prefix('Notes').'SET ' . $sSection . ' = :value WHERE noteId = :noteId AND profileId = :profileId');
411
        $rStmt->bindValue(':value', $sValue, \PDO::PARAM_STR);
412
        $rStmt->bindValue(':noteId', $iNoteId, \PDO::PARAM_INT);
413
        $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
414
415
        return $rStmt->execute();
416
    }
417
418
    /**
419
     * @param integer $iNoteId
420
     * @param integer $iStatus
421
     *
422
     * @return boolean
423
     */
424
    public function approved($iNoteId, $iStatus = 1)
425
    {
426
        $rStmt = Db::getInstance()->prepare('UPDATE'.Db::prefix('Notes').'SET approved = :status WHERE noteId = :noteId');
427
        $rStmt->bindParam(':noteId', $iNoteId, \PDO::PARAM_INT);
428
        $rStmt->bindParam(':status', $iStatus, \PDO::PARAM_INT);
429
        return $rStmt->execute();
430
    }
431
432
    /**
433
     * To prevent spam!
434
     *
435
     * @param integer $iProfileId
436
     * @param integer $iWaitTime In minutes
437
     * @param string $sCurrentTime In date format: 0000-00-00 00:00:00
438
     *
439
     * @return boolean Return TRUE if the weather was fine, otherwise FALSE
440
     */
441
    public function checkWaitSend($iProfileId, $iWaitTime, $sCurrentTime)
442
    {
443
        $rStmt = Db::getInstance()->prepare('SELECT noteId FROM' . Db::prefix('Notes') .
444
            'WHERE profileId = :profileId AND DATE_ADD(createdDate, INTERVAL :waitTime MINUTE) > :currentTime LIMIT 1');
445
        $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
446
        $rStmt->bindValue(':waitTime', $iWaitTime, \PDO::PARAM_INT);
447
        $rStmt->bindValue(':currentTime', $sCurrentTime, \PDO::PARAM_STR);
448
        $rStmt->execute();
449
        return ($rStmt->rowCount() === 0);
450
    }
451
}
452