1
+ <?php namespace Syntax \Core ;
2
+
3
+ class User_Forum extends Syntax \Core \User {
4
+
5
+ public function __construct ()
6
+ {
7
+ parent ::__construct ();
8
+
9
+ self ::$ relationsData = array_merge (parent ::$ relationsData , array (
10
+ 'posts ' => array ('hasMany ' , 'Forum_Post ' , 'foreignKey ' => 'user_id ' ),
11
+ 'replies ' => array ('hasMany ' , 'Forum_Reply ' , 'foreignKey ' => 'user_id ' ),
12
+ ));
13
+ }
14
+
15
+ /**
16
+ * See if there are unread posts in a certain forum board
17
+ *
18
+ * @param int $boardId A forum board Id
19
+ *
20
+ * @return boolean
21
+ */
22
+ public function checkUnreadBoard ($ boardId )
23
+ {
24
+ // Future version
25
+ // return Forum_Board::where('id', '=', $boardId)->or_where('parent_id', '=', $boardId)->get()->unreadFlagForUser($this->id);
26
+
27
+ // Get all parent and child boards matching the id
28
+ $ boardIds = Forum_Board::where ('uniqueId ' , $ boardId )->orWhere ('parent_id ' , '= ' , $ boardId )->get ()->id ->toArray ();
29
+
30
+ // Get any posts within those boards
31
+ $ posts = Forum_Post::whereIn ('forum_board_id ' , $ boardIds )->get ();
32
+ $ postIds = $ posts ->id ->toArray ();
33
+
34
+ // Make sure there are posts
35
+ if (count ($ postIds ) > 0 ) {
36
+
37
+ // See which of these posts the user has already viewed
38
+ $ viewedPosts = Forum_Post_View::where ('user_id ' , '= ' , $ this ->id )->whereIn ('forum_post_id ' , $ postIds )->get ();
39
+
40
+ // If the posts are greater than the viewed, there are new posts
41
+ if (count ($ posts ) > count ($ viewedPosts )) {
42
+ return true ;
43
+ }
44
+ }
45
+ return false ;
46
+ }
47
+
48
+ /**
49
+ * Get the number of unread posts
50
+ *
51
+ * @return int
52
+ */
53
+ public function unreadPostCount ()
54
+ {
55
+ // Get the id of all posts
56
+ $ posts = Forum_Post::all ();
57
+ $ postsCount = $ posts ->count ();
58
+
59
+ if ($ postsCount > 0 ) {
60
+ foreach ($ posts as $ key => $ post ) {
61
+ if ($ post ->board ->forum_board_type_id == Forum_Board::TYPE_GM && !$ this ->checkPermission ('GAME_MASTER ' )) {
62
+ unset($ posts [$ key ]);
63
+ }
64
+ }
65
+ $ postIds = $ posts ->id ->toArray ();
66
+
67
+ // See which of these the user has viewed
68
+ $ viewedPostCount = Forum_Post_View::where ('user_id ' , $ this ->id )->whereIn ('forum_post_id ' , $ postIds )->count ();
69
+
70
+ // If there are more posts than viewed posts, return the remainder
71
+ if ($ postsCount > $ viewedPostCount ) {
72
+ return $ postsCount - $ viewedPostCount ;
73
+ }
74
+ }
75
+ return 0 ;
76
+ }
77
+ }
0 commit comments