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

ForumCoreModel::getForum()   C

Complexity

Conditions 7
Paths 64

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 18
nc 64
nop 4
dl 0
loc 32
rs 6.7272
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 / Core / 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 ForumCoreModel extends Framework\Mvc\Model\Engine\Model
14
{
15
    const
16
    CACHE_GROUP = 'db/sys/mod/forum',
17
    CREATED = 'createdDate DESC',
18
    UPDATED = 'updatedDate DESC',
19
    NAME = 'name ASC';
20
21
    /**
22
     * @param integer|null $iForumId
23
     * @param integer|null $iOffset
24
     * @param integer|null $iLimit
25
     * @param string $sOrder
26
     *
27
     * @return \stdClass|false
28
     */
29
    public function getForum($iForumId = null, $iOffset = null, $iLimit = null, $sOrder = self::NAME)
30
    {
31
        $bIsLimit = isset($iOffset, $iLimit);
32
33
        $iOffset = (int) $iOffset;
34
        $iLimit = (int) $iLimit;
35
36
        $sSqlLimit = ($bIsLimit) ?  ' LIMIT :offset, :limit' : '';
37
        $sSqlForumId = (!empty($iForumId)) ? 'WHERE forumId = :forumId ' : '';
38
39
        $rStmt = Db::getInstance()->prepare('SELECT * FROM' . Db::prefix('Forums') . $sSqlForumId . 'ORDER BY ' . $sOrder . $sSqlLimit);
40
41
        if (!empty($iForumId)) {
42
            $rStmt->bindParam(':forumId', $iForumId, \PDO::PARAM_INT);
43
        }
44
45
        if ($bIsLimit) {
46
            $rStmt->bindParam(':offset', $iOffset, \PDO::PARAM_INT);
47
        }
48
49
        if ($bIsLimit) {
50
            $rStmt->bindParam(':limit', $iLimit, \PDO::PARAM_INT);
51
        }
52
53
        $rStmt->execute();
54
55
        if (!empty($iForumId)) {
56
            return $rStmt->fetch(\PDO::FETCH_OBJ);
57
        } else {
58
            return $rStmt->fetchAll(\PDO::FETCH_OBJ);
59
        }
60
    }
61
62
    /**
63
     * @param integer $iTopicId
64
     * @param integer|null $iMessageId
65
     * @param integer|null $iProfileId
66
     * @param integer $iApproved
67
     * @param integer $iOffset
68
     * @param integer $iLimit
69
     * @param string $sSort
70
     *
71
     * @return \stdClass|false
72
     */
73
    public function getMessage($iTopicId, $iMessageId = null, $iProfileId = null, $iApproved, $iOffset, $iLimit, $sSort = Db::ASC)
74
    {
75
        $iOffset = (int) $iOffset;
76
        $iLimit = (int) $iLimit;
77
78
        $sSqlMessageId = (!empty($iMessageId)) ? ' AND msg.messageId = :messageId ' : '';
79
        $sSqlProfileId = (!empty($iProfileId)) ? ' AND msg.profileId = :profileId ' : '';
80
81
        $rStmt = Db::getInstance()->prepare('SELECT f.name, t.title, t.forumId, msg.*, m.username, m.firstName, m.sex FROM' . Db::prefix('Forums') .
82
            'AS f INNER JOIN' . Db::prefix('ForumsTopics') . 'AS t ON f.forumId = t.forumId INNER JOIN ' . Db::prefix('ForumsMessages') .
83
            'AS msg ON t.topicId = msg.topicId LEFT JOIN' . Db::prefix('Members') . 'AS m ON msg.profileId = m.profileId WHERE msg.topicId = :topicId ' .
84
            $sSqlMessageId . $sSqlProfileId . ' AND msg.approved = :approved ORDER BY msg.createdDate ' . $sSort . ' LIMIT :offset, :limit');
85
86
        $rStmt->bindValue(':topicId', $iTopicId, \PDO::PARAM_INT);
87
88
        if (!empty($iMessageId)) {
89
            $rStmt->bindValue(':messageId', $iMessageId, \PDO::PARAM_INT);
90
        }
91
92
        if (!empty($iProfileId)) {
93
            $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
94
        }
95
96
        $rStmt->bindValue(':approved', $iApproved, \PDO::PARAM_INT);
97
        $rStmt->bindParam(':offset', $iOffset, \PDO::PARAM_INT);
98
        $rStmt->bindParam(':limit', $iLimit, \PDO::PARAM_INT);
99
        $rStmt->execute();
100
101
        if (!empty($iProfileId)) {
102
            return $rStmt->fetch(\PDO::FETCH_OBJ);
103
        } else {
104
            return $rStmt->fetchAll(\PDO::FETCH_OBJ);
105
        }
106
    }
107
}
108