PageRenderTime 44ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/wojilu.Controller/Forum/Moderators/PostController.cs

https://bitbucket.org/kingshine/wojilu
C# | 261 lines | 185 code | 71 blank | 5 comment | 36 complexity | 63fb4d2b8a84d4f0f8abfb5b3ae7a120 MD5 | raw file
Possible License(s): MIT
  1. /*
  2. * Copyright (c) 2010, www.wojilu.com. All rights reserved.
  3. */
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using wojilu.Web.Mvc;
  8. using wojilu.Web.Mvc.Attr;
  9. using wojilu.Apps.Forum.Domain;
  10. using wojilu.Apps.Forum.Interface;
  11. using wojilu.Apps.Forum.Service;
  12. using wojilu.Common.Money.Domain;
  13. using wojilu.Common.Money.Interface;
  14. using wojilu.Common.Money.Service;
  15. using wojilu.Common.Resource;
  16. using wojilu.Members.Users.Domain;
  17. using wojilu.Web.Controller.Forum.Utils;
  18. namespace wojilu.Web.Controller.Forum.Moderators {
  19. [App( typeof( ForumApp ) )]
  20. public class PostController : ControllerBase {
  21. public IForumBoardService boardService { get; set; }
  22. public ICurrencyService currencyService { get; set; }
  23. public IForumTopicService topicService { get; set; }
  24. public IForumPostService postService { get; set; }
  25. public IForumRateService rateService { get; set; }
  26. public PostController() {
  27. topicService = new ForumTopicService();
  28. boardService = new ForumBoardService();
  29. postService = new ForumPostService();
  30. currencyService = new CurrencyService();
  31. rateService = new ForumRateService();
  32. }
  33. private Boolean checkCreatorPermission( ForumTopic topic ) {
  34. if (topic.Creator.Id != ctx.viewer.Id) {
  35. echoText( alang( "exRewardSelfOnly" ) );
  36. return false;
  37. }
  38. return true;
  39. }
  40. private Boolean boardError( ForumTopic topic ) {
  41. if (ctx.GetInt( "boardId" ) != topic.ForumBoard.Id) {
  42. echoRedirect( lang( "exNoPermission" ) );
  43. return true;
  44. }
  45. return false;
  46. }
  47. private Boolean boardError( ForumPost post ) {
  48. if (ctx.GetInt( "boardId" ) != post.ForumBoardId) {
  49. echoRedirect( lang( "exNoPermission" ) );
  50. return true;
  51. }
  52. return false;
  53. }
  54. //------------------------------------ 悬赏 -----------------------------------------
  55. public void SetReward( int id ) {
  56. ForumTopic topic = topicService.GetById( id, ctx.owner.obj );
  57. if (topic == null) {
  58. echoRedirect( alang( "exTopicNotFound" ) );
  59. return;
  60. }
  61. if (boardError( topic )) return;
  62. if (!checkCreatorPermission( topic )) return;
  63. Page.Title = alang( "setReward" ) + ":" + topic.Title;
  64. set( "ActionLink", to( new PostSaveController().SaveReward, id ) + "?boardId=" + topic.ForumBoard.Id );
  65. DataPage<ForumPost> list = postService.GetPageList( id, getPageSize(), 0 );
  66. bindRewardInfo( topic );
  67. bindPostList( list );
  68. }
  69. public void RewardList( int id ) {
  70. ForumTopic topic = topicService.GetById( id, ctx.owner.obj );
  71. if (topic == null) {
  72. echoRedirect( alang( "exTopicNotFound" ) );
  73. return;
  74. }
  75. if (boardError( topic )) return;
  76. set( "ActionLink", to( new PostSaveController().SaveReward, id ) + "?boardId=" + topic.ForumBoard.Id );
  77. DataPage<ForumPost> list = postService.GetPageList( id, getPageSize(), 0 );
  78. bindRewardInfo( topic );
  79. bindRewardList( list );
  80. }
  81. public void AddReward( int id ) {
  82. ForumPost post = postService.GetById( id, ctx.owner.obj );
  83. ForumTopic topic = topicService.GetById( post.TopicId, ctx.owner.obj );
  84. if (boardError( topic )) return;
  85. if (!checkCreatorPermission( topic )) return;
  86. set( "post.RewardAvailable", topic.RewardAvailable );
  87. set( "post.Id", id );
  88. set( "ActionLink", to( new PostSaveController().SaveReward, id ) + "?boardId=" + topic.ForumBoard.Id );
  89. }
  90. //--------------------------------------------------------------------------
  91. public void AddCredit( int id ) {
  92. String msg = "<div style=\"font-size:22px;color:red;font-weight:bold;margin-top:30px; text-align:center;\">{0}</div>";
  93. if (rateService.IsUserRate( (User)ctx.viewer.obj, id )) {
  94. actionContent( string.Format( msg, alang( "exRewarded" ) ) );
  95. return;
  96. }
  97. ForumPost post = postService.GetById( id, ctx.owner.obj );
  98. if (post == null) {
  99. actionContent( string.Format( msg, alang( "exPostNotFound" ) ) );
  100. return;
  101. }
  102. if (post.Creator.Id == ctx.viewer.Id) {
  103. actionContent( string.Format( msg, alang( "exNotAllowSelfCredit" ) ) );
  104. return;
  105. }
  106. if (boardError( post )) return;
  107. ForumBoard board = boardService.GetById( post.ForumBoardId, ctx.owner.obj );
  108. set( "ActionLink", to( new PostSaveController().SaveCredit, id ) + "?boardId=" + board.Id );
  109. IList currencyList = currencyService.GetForumRateCurrency();
  110. dropList( "CurrencyId", currencyList, "Name=Id", 0 );
  111. List<PropertyItem> values = getCurrencyValues();
  112. dropList( "CurrencyValue", values, "Value=Value", 2 );
  113. }
  114. public void Detail( int id ) {
  115. ForumPost post = postService.GetById( id, ctx.owner.obj );
  116. if (boardError( post )) return;
  117. String postContent = "<div style='width:600px;'><div>" +
  118. lang( "author" ) + ": {0} " +
  119. lang( "title" ) + ": {1} <span class='note'>({2})</span></div>" +
  120. "<hr/><div>{3}</div></div>";
  121. actionContent( string.Format( postContent,
  122. post.Creator.Name, post.Title, post.Created, post.Content ) );
  123. }
  124. private List<PropertyItem> getCurrencyValues() {
  125. List<PropertyItem> values = new List<PropertyItem>();
  126. int rateMaxValue = ((ForumApp)ctx.app.obj).MaxRateValue;
  127. for (int i = rateMaxValue / 2; i > 0; i--) {
  128. values.Add( new PropertyItem( "CurencyValue", -i * 2 ) );
  129. }
  130. for (int i = 1; i <= (rateMaxValue / 2); i++) {
  131. values.Add( new PropertyItem( "CurencyValue", i * 2 ) );
  132. }
  133. return values;
  134. }
  135. private int getPageSize() { return 100; }
  136. private void bindRewardInfo( ForumTopic topic ) {
  137. List<ForumBoard> pathboards = getTree().GetPath( topic.ForumBoard.Id );
  138. set( "location", ForumLocationUtil.GetSetReward( pathboards, topic, ctx ) );
  139. int rewardAvailable = topic.RewardAvailable;
  140. set( "currency.Name", KeyCurrency.Instance.Name );
  141. set( "post.Reward", topic.Reward );
  142. set( "post.RewardSetted", topic.Reward - rewardAvailable );
  143. set( "post.RewardAvailable", rewardAvailable );
  144. String rewardInfo = string.Format( alang( "rewardInfo" ), (topic.Reward - rewardAvailable), rewardAvailable );
  145. set( "rewardInfo", rewardInfo );
  146. }
  147. private void bindPostList( DataPage<ForumPost> list ) {
  148. IBlock block = getBlock( "list" );
  149. foreach (ForumPost post in list.Results) {
  150. if (post.ParentId == 0)
  151. block.Set( "p.Reward", "--" );
  152. else if (post.Reward > 0)
  153. block.Set( "p.Reward", cvt.ToInt( post.Reward ) );
  154. else
  155. block.Set( "p.Reward", string.Format( "<a href='{0}' class='frmBox'>+ " + alang( "setReward" ) + "</a>", to( AddReward, post.Id ) + "?boardId=" + post.ForumBoardId ) );
  156. block.Set( "p.User", post.Creator.Name );
  157. String content = strUtil.ParseHtml( post.Content, 70 );
  158. if (content.EndsWith( "..." )) {
  159. String lnkDetail = string.Format( "<a href='{0}' class='frmBox left10'>" + lang( "more" ) + ForumLocationUtil.separator + "</a>", to( Detail, post.Id ) + "?boardId=" + post.ForumBoardId, sys.Path.Skin );
  160. block.Set( "p.Content", content + lnkDetail );
  161. }
  162. else {
  163. block.Set( "p.Content", content );
  164. }
  165. block.Set( "p.Created", post.Created );
  166. block.Next();
  167. }
  168. set( "page", list.PageBar );
  169. }
  170. private void bindRewardList( DataPage<ForumPost> list ) {
  171. IBlock block = getBlock( "list" );
  172. foreach (ForumPost post in list.Results) {
  173. if ((post.ParentId == 0) || (post.Reward == 0))
  174. block.Set( "p.Reward", "--" );
  175. else
  176. block.Set( "p.Reward", cvt.ToInt( post.Reward ) );
  177. block.Set( "p.User", post.Creator.Name );
  178. block.Set( "p.Content", strUtil.ParseHtml( post.Content, 70 ) );
  179. block.Set( "p.Created", post.Created );
  180. block.Next();
  181. }
  182. set( "page", list.PageBar );
  183. }
  184. private Tree<ForumBoard> _tree;
  185. private Tree<ForumBoard> getTree() {
  186. if (_tree == null) _tree = new Tree<ForumBoard>( boardService.GetBoardAll( ctx.app.Id, ctx.viewer.IsLogin ) );
  187. return _tree;
  188. }
  189. }
  190. }