|
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; |
|
|
|
|
|
|
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
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/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: