You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

forums_controller.rb 9.3 kB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. # added by fq
  2. class ForumsController < ApplicationController
  3. layout "users_base"
  4. # GET /forums
  5. # GET /forums.json
  6. before_filter :find_forum_if_available
  7. before_filter :authenticate_user_edit, :only => [:edit, :update]
  8. before_filter :authenticate_user_destroy, :only => [:destroy]
  9. before_filter :require_login, :only => [:new, :create]
  10. helper :sort
  11. include SortHelper
  12. PageLimit = 20
  13. def create_feedback
  14. if User.current.logged?
  15. @memo = Memo.new(params[:memo])
  16. @memo.forum_id = "1"
  17. @memo.author_id = User.current.id
  18. #@forum = @memo.forum
  19. respond_to do |format|
  20. if @memo.save
  21. format.html { redirect_to forum_path(@memo.forum) }
  22. else
  23. sort_init 'updated_at', 'desc'
  24. sort_update 'created_at' => "#{Memo.table_name}.created_at",
  25. 'replies' => "#{Memo.table_name}.replies_count",
  26. 'updated_at' => "COALESCE (last_replies_memos.created_at, #{Memo.table_name}.created_at)"
  27. @topic_count = @forum.topics.count
  28. @topic_pages = Paginator.new @topic_count, per_page_option, params['page']
  29. @memos = @forum.topics.
  30. reorder("#{Memo.table_name}.sticky DESC").
  31. includes(:last_reply).
  32. limit(@topic_pages.per_page).
  33. offset(@topic_pages.offset).
  34. order(sort_clause).
  35. preload(:author, {:last_reply => :author}).
  36. all
  37. flash.now[:error] = "#{l :label_memo_create_fail}: #{@memo.errors.full_messages[0]}"
  38. # back_error_page = @memo.parent_id.nil? ? forum_path(@forum) : forum_memo_path(@forum, @memo.parent_id)
  39. format.html { render action: :show, layout: 'base_forums' }#, error: "#{l :label_memo_create_fail}: #{@memo.errors.full_messages[0]}" }
  40. format.json { render json: @memo.errors, status: :unprocessable_entity }
  41. end
  42. end
  43. else
  44. respond_to do |format|
  45. format.html { redirect_to signin_path }
  46. end
  47. end
  48. end
  49. def create_memo
  50. @memo = Memo.new(params[:memo])
  51. @memo.forum_id = @forum.id
  52. @memo.author_id = User.current.id
  53. @memo.save_attachments(params[:attachments] || (params[:memo] && params[:memo][:uploads]))
  54. respond_to do |format|
  55. if @memo.save
  56. format.html { redirect_to (forum_memo_url(@forum, (@memo.parent_id.nil? ? @memo : @memo.parent_id))), notice: "#{l :label_memo_create_succ}" }
  57. format.json { render json: @memo, status: :created, location: @memo }
  58. else
  59. sort_init 'updated_at', 'desc'
  60. sort_update 'created_at' => "#{Memo.table_name}.created_at",
  61. 'replies' => "#{Memo.table_name}.replies_count",
  62. 'updated_at' => "COALESCE (last_replies_memos.created_at, #{Memo.table_name}.created_at)"
  63. @topic_count = @forum.topics.count
  64. @topic_pages = Paginator.new @topic_count, per_page_option, params['page']
  65. @memos = @forum.topics.
  66. reorder("#{Memo.table_name}.sticky DESC").
  67. includes(:last_reply).
  68. limit(@topic_pages.per_page).
  69. offset(@topic_pages.offset).
  70. order(sort_clause).
  71. preload(:author, {:last_reply => :author}).
  72. all
  73. flash.now[:error] = "#{l :label_memo_create_fail}: #{@memo.errors.full_messages[0]}"
  74. # back_error_page = @memo.parent_id.nil? ? forum_path(@forum) : forum_memo_path(@forum, @memo.parent_id)
  75. format.html { render action: :show, layout: 'base_forums' }#, error: "#{l :label_memo_create_fail}: #{@memo.errors.full_messages[0]}" }
  76. format.json { render json: @memo.errors, status: :unprocessable_entity }
  77. end
  78. end
  79. end
  80. def index
  81. @offset, @limit = api_offset_and_limit({:limit => 10})
  82. @forums_all = Forum.reorder("sticky DESC")
  83. @forums_count = @forums_all.count
  84. @forums_pages = Paginator.new @forums_count, @limit, params['page']
  85. @offset ||= @forums_pages.offset
  86. @forums = @forums_all.offset(@offset).limit(@limit).all
  87. #@forums = Forum.all
  88. respond_to do |format|
  89. format.html # index.html.erb
  90. format.json { render json: @forums }
  91. end
  92. end
  93. # GET /forums/1
  94. # GET /forums/1.json
  95. def show
  96. sort_init 'updated_at', 'desc'
  97. sort_update 'created_at' => "#{Memo.table_name}.created_at",
  98. 'replies' => "#{Memo.table_name}.replies_count",
  99. 'updated_at' => "COALESCE (last_replies_memos.created_at, #{Memo.table_name}.created_at)"
  100. @memo = Memo.new(:forum => @forum)
  101. @topic_count = @forum.topics.count
  102. @topic_pages = Paginator.new @topic_count, per_page_option, params['page']
  103. @memos = @forum.topics.
  104. reorder("#{Memo.table_name}.sticky DESC").
  105. includes(:last_reply).
  106. limit(@topic_pages.per_page).
  107. offset(@topic_pages.offset).
  108. order(sort_clause).
  109. preload(:author, {:last_reply => :author}).
  110. all
  111. # @offset, @limit = api_offset_and_limit({:limit => 10})
  112. # @forum = Forum.find(params[:id])
  113. # @memos_all = @forum.topics
  114. # @topic_count = @memos_all.count
  115. # @topic_pages = Paginator.new @topic_count, @limit, params['page']
  116. # @offset ||= @topic_pages.offset
  117. # @memos = @memos_all.offset(@offset).limit(@limit).all
  118. respond_to do |format|
  119. format.html {
  120. render :layout => 'base_forums'
  121. }# show.html.erb
  122. format.json { render json: @forum }
  123. end
  124. end
  125. # GET /forums/new
  126. # GET /forums/new.json
  127. def new
  128. @forum = Forum.new
  129. respond_to do |format|
  130. format.html # new.html.erb
  131. format.json { render json: @forum }
  132. end
  133. end
  134. # GET /forums/1/edit
  135. def edit
  136. @forum = Forum.find(params[:id])
  137. end
  138. # POST /forums
  139. # POST /forums.json
  140. def create
  141. @forum = Forum.new(params[:forum])
  142. @forum.creator_id = User.current.id
  143. respond_to do |format|
  144. if @forum.save
  145. format.html { redirect_to @forum, notice: l(:label_forum_create_succ) }
  146. format.json { render json: @forum, status: :created, location: @forum }
  147. else
  148. flash.now[:error] = "#{l :label_forum_create_fail}: #{@forum.errors.full_messages[0]}"
  149. format.html { render action: "new" }
  150. format.json { render json: @forum.errors, status: :unprocessable_entity }
  151. end
  152. end
  153. end
  154. # PUT /forums/1
  155. # PUT /forums/1.json
  156. def update
  157. @forum = Forum.find(params[:id])
  158. respond_to do |format|
  159. if @forum.update_attributes(params[:forum])
  160. format.html { redirect_to @forum, notice: l(:label_forum_update_succ) }
  161. format.json { head :no_content }
  162. else
  163. flash.now[:error] = "#{l :label_forum_update_fail}: #{@forum.errors.full_messages[0]}"
  164. format.html { render action: "edit" }
  165. format.json { render json: @forum.errors, status: :unprocessable_entity }
  166. end
  167. end
  168. end
  169. # DELETE /forums/1
  170. # DELETE /forums/1.json
  171. def destroy
  172. @forum = Forum.find(params[:id])
  173. @forum.destroy
  174. respond_to do |format|
  175. format.html { redirect_to forums_url }
  176. format.json { head :no_content }
  177. end
  178. end
  179. def search_forum
  180. # @forums = paginateHelper Forum.where("name LIKE '%#{params[:name]}%'")
  181. q = "%#{params[:name].strip}%"
  182. (redirect_to forums_url, :notice => l(:label_sumbit_empty);return) if params[:name].blank?
  183. @offset, @limit = api_offset_and_limit({:limit => 10})
  184. @forums_all = Forum.where("name LIKE ?", q)
  185. @forums_count = @forums_all.count
  186. @forums_pages = Paginator.new @forums_count, @limit, params['page']
  187. @offset ||= @forums_pages.offset
  188. @forums = @forums_all.offset(@offset).limit(@limit).all
  189. respond_to do |format|
  190. format.html {
  191. render 'index'
  192. }
  193. format.json { render json: @forums }
  194. end
  195. end
  196. def search_memo
  197. q = "%#{params[:name].strip}%"
  198. limit = PageLimit
  199. @memo = Memo.new
  200. @offset, @limit = api_offset_and_limit({:limit => limit})
  201. @forum = Forum.find(params[:id])
  202. @memos_all = @forum.topics.where("subject LIKE ?", q)
  203. @topic_count = @memos_all.count
  204. @topic_pages = Paginator.new @topic_count, @limit, params['page']
  205. @offset ||= @topic_pages.offset
  206. @memos = @memos_all.offset(@offset).limit(@limit).all
  207. respond_to do |format|
  208. format.html {
  209. render 'show', :layout => 'base_forums'
  210. }
  211. format.json { render json: @forum }
  212. end
  213. end
  214. private
  215. def find_forum_if_available
  216. @forum = Forum.find(params[:id]) if params[:id]
  217. rescue ActiveRecord::RecordNotFound
  218. render_404
  219. nil
  220. end
  221. def authenticate_user_edit
  222. find_forum_if_available
  223. render_403 unless @forum.editable_by? User.current
  224. end
  225. def authenticate_user_destroy
  226. find_forum_if_available
  227. render_403 unless @forum.destroyable_by? User.current
  228. end
  229. end

软件技术创新是当今国家整体创新能力的重要基础和发展引擎。近年来,大众参与的软件创新与创业活动已经成为网络时代软件开发和应用的新形态,正在快速改变着全球软件创新模式和软件产业格局。 ...