forked from wojilu/wojilu
/
TopicController.cs
executable file
·235 lines (171 loc) · 8.09 KB
/
TopicController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
* Copyright (c) 2010, www.wojilu.com. All rights reserved.
*/
using System;
using System.Collections.Generic;
using wojilu.Web.Mvc;
using wojilu.Web.Mvc.Attr;
using wojilu.Members.Users.Domain;
using wojilu.Apps.Forum.Domain;
using wojilu.Apps.Forum.Service;
using wojilu.Apps.Forum.Interface;
using wojilu.Common.Security;
using wojilu.Common.Money.Domain;
using wojilu.Common.Money.Service;
using wojilu.Common.Money.Interface;
using wojilu.Web.Controller.Forum.Utils;
using wojilu.Web.Controller.Common;
using wojilu.Members.Users.Interface;
using wojilu.Members.Users.Service;
namespace wojilu.Web.Controller.Forum {
[App( typeof( ForumApp ) )]
public partial class TopicController : ControllerBase {
public virtual IForumBoardService boardService { get; set; }
public virtual IForumTopicService topicService { get; set; }
public virtual IForumPostService postService { get; set; }
public virtual IUserIncomeService incomeService { get; set; }
public virtual IAttachmentService attachService { get; set; }
public virtual IUserService userService { get; set; }
public virtual IForumRateService rateService { get; set; }
public virtual ICurrencyService currencyService { get; set; }
public virtual IForumBuyLogService buylogService { get; set; }
public virtual IModeratorService moderatorService { get; set; }
public TopicController() {
boardService = new ForumBoardService();
topicService = new ForumTopicService();
postService = new ForumPostService();
incomeService = new UserIncomeService();
attachService = new AttachmentService();
userService = new UserService();
rateService = new ForumRateService();
currencyService = new CurrencyService();
buylogService = new ForumBuyLogService();
moderatorService = new ModeratorService();
}
public override void CheckPermission() {
ForumTopic topic = topicService.GetById( ctx.route.id, ctx.owner.obj );
if (topic == null) {
echo( alang( "exTopicNotFound" ) );
return;
}
// 判断阅读权限
if (topic.ReadPermission > 0 && haveReadPermission( topic ) == false) {
echo( alang( "exReadPermission" ) );
return;
}
ForumBoard board = topic.ForumBoard;
SecurityHelper.Check( this, board );
}
private Tree<ForumBoard> _tree;
private Tree<ForumBoard> getTree() {
if (_tree == null) _tree = new Tree<ForumBoard>( boardService.GetBoardAll( ctx.app.Id, ctx.viewer.IsLogin ) );
return _tree;
}
public virtual void Show( long id ) {
ForumTopic topic = topicService.GetById( id, ctx.owner.obj );
ForumBoard board = topic.ForumBoard;
set( "isTopicLocked", topic.IsLocked == 1 ? "true" : "false" );
topicService.AddHits( topic );
long userId = ctx.GetLong( "userId" );
bindPosts( id, topic, board, userId );
set( "moderatorJson", moderatorService.GetModeratorJson( board ) );
set( "creatorId", topic.Creator.Id );
set( "tagAction", to( new Edits.TagController().SaveTag, topic.Id ) );
}
private void bindPosts( long id, ForumTopic topic, ForumBoard board, long userId ) {
DataPage<ForumPost> list = postService.GetPageList( id, getPageSize( ctx.app.obj ), userId );
ForumPost lastPost = getLastPost( list, topic.Id );
List<Attachment> attachments = attachService.GetByTopic( list.Results );
UserIncomeUtil.AddIncomeListToUser( list.Results );
forwardPreNext( topic );
ctx.Page.SetTitle( topic.Title, board.Name );
Page.Keywords = topic.Tag.TextString;
List<ForumBoard> pathboards = getTree().GetPath( board.Id );
set( "location", ForumLocationUtil.GetTopic( pathboards, topic, ctx ) );
set( "newReplyUrl", to( new Users.PostController().ReplyTopic, id ) + "?boardId=" + topic.ForumBoard.Id );
set( "newTopicUrl", to( new Users.TopicController().NewTopic ) + "?boardId=" + topic.ForumBoard.Id );
ctx.SetItem( "forumTopic", topic );
ctx.SetItem( "forumBoard", board );
ctx.SetItem( "posts", list.Results );
ctx.SetItem( "attachs", attachments );
ctx.SetItem( "pageSize", getPageSize( ctx.app.obj ) );
load( "postBlock", PostLoop );
bindForm( topic, board, lastPost );
list.RecordCount++;
String pager = list.PageCount > 1 ? list.PageBar : "";
set( "page", pager );
}
private ForumPost getLastPost(DataPage<ForumPost> list, long topicId) {
ForumPost lastPost;
if (list.Current == list.PageCount) {
lastPost = list.Results[list.Results.Count - 1];
}
else {
lastPost = postService.GetLastPostByTopic( topicId );
}
return lastPost;
}
private Boolean haveReadPermission( ForumTopic topic ) {
if (ctx.viewer.IsLogin == false) return false;
if (ctx.viewer.Id == topic.Creator.Id) return true;
int permission = incomeService.GetUserIncome( ctx.viewer.Id, Currency.ReadPermission().Id ).Income;
return permission >= topic.ReadPermission;
}
//-----------------------------------------------------------------------------------------------
private void bindForm( ForumTopic topic, ISecurity board, ForumPost lastPost ) {
IBlock formBlock = getBlock( "form" );
//if (topic.IsLocked == 1) return;
//ISecurityAction replyAction = ForumAction.Get( new PostController().ReplyPost, ctx.route.getRootNamespace() );
//if (PermissionUtil.HasAction( (User)ctx.viewer.obj, board, replyAction, ctx ))
bindFormNew( topic, lastPost, formBlock );
}
private void bindFormNew( ForumTopic topic, ForumPost lastPost, IBlock formBlock ) {
User user = ctx.viewer.obj as User;
if (strUtil.HasText( user.Pic )) {
formBlock.Set( "currentUser", "<img src=\"" + user.PicM + "\"/>" );
}
else {
formBlock.Set( "currentUser", user.Name );
}
formBlock.Set( "post.ReplyActionUrl", to( new Users.PostController().Create ) + "?boardId=" + topic.ForumBoard.Id );
formBlock.Set( "post.ReplyTitle", "re:" + topic.Title );
formBlock.Set( "post.TopicId", topic.Id );
formBlock.Set( "post.ParentId", lastPost.Id );
formBlock.Set( "forumBoard.Id", topic.ForumBoard.Id );
IEditor ed = EditorFactory.NewOne( "Content", "", "150px", Editor.ToolbarType.Basic );
ed.AddUploadUrl( ctx );
formBlock.Set( "Editor", ed );
formBlock.Set( "currentPageId", ctx.route.page );
formBlock.Next();
}
private void forwardPreNext( ForumTopic topic ) {
if (ctx.url.Query.Equals( "?next" )) {
forwardToPre( topic );
}
else if (ctx.url.Query.Equals( "?pre" )) {
forwardToNext( topic );
}
}
private void forwardToNext( ForumTopic topic ) {
ForumTopic pre = topicService.GetPre( topic );
if (pre == null) {
echoRedirect( alang( "exFirstTopic" ) );
}
else {
redirect( Show, pre.Id );
}
}
private void forwardToPre( ForumTopic topic ) {
ForumTopic next = topicService.GetNext( topic );
if (next == null) {
echoRedirect( alang( "exLastTopic" ) );
}
else {
redirect( Show, next.Id );
}
}
private int getPageSize( object app ) {
return ((ForumApp)app).GetSettingsObj().ReplySize;
}
}
}