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

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. if @forum.save
  144. respond_to do |format|
  145. format.html { redirect_to @forum, notice: l(:label_forum_create_succ) }
  146. format.json { render json: @forum, status: :created, location: @forum }
  147. end
  148. else
  149. respond_to do |format|
  150. flash.now[:error] = "#{l :label_forum_create_fail}: #{@forum.errors.full_messages[0]}"
  151. format.html { render action: "new" }
  152. format.json { render json: @forum.errors, status: :unprocessable_entity }
  153. end
  154. end
  155. end
  156. # PUT /forums/1
  157. # PUT /forums/1.json
  158. def update
  159. @forum = Forum.find(params[:id])
  160. respond_to do |format|
  161. if @forum.update_attributes(params[:forum])
  162. format.html { redirect_to @forum, notice: l(:label_forum_update_succ) }
  163. format.json { head :no_content }
  164. else
  165. flash.now[:error] = "#{l :label_forum_update_fail}: #{@forum.errors.full_messages[0]}"
  166. format.html { render action: "edit" }
  167. format.json { render json: @forum.errors, status: :unprocessable_entity }
  168. end
  169. end
  170. end
  171. # DELETE /forums/1
  172. # DELETE /forums/1.json
  173. def destroy
  174. @forum = Forum.find(params[:id])
  175. @forum.destroy
  176. respond_to do |format|
  177. format.html { redirect_to forums_url }
  178. format.json { head :no_content }
  179. end
  180. end
  181. def search_forum
  182. # @forums = paginateHelper Forum.where("name LIKE '%#{params[:name]}%'")
  183. q = "%#{params[:name].strip}%"
  184. (redirect_to forums_url, :notice => l(:label_sumbit_empty);return) if params[:name].blank?
  185. @offset, @limit = api_offset_and_limit({:limit => 10})
  186. @forums_all = Forum.where("name LIKE ?", q)
  187. @forums_count = @forums_all.count
  188. @forums_pages = Paginator.new @forums_count, @limit, params['page']
  189. @offset ||= @forums_pages.offset
  190. @forums = @forums_all.offset(@offset).limit(@limit).all
  191. respond_to do |format|
  192. format.html {
  193. render 'index'
  194. }
  195. format.json { render json: @forums }
  196. end
  197. end
  198. def search_memo
  199. q = "%#{params[:name].strip}%"
  200. limit = PageLimit
  201. @memo = Memo.new
  202. @offset, @limit = api_offset_and_limit({:limit => limit})
  203. @forum = Forum.find(params[:id])
  204. @memos_all = @forum.topics.where("subject LIKE ?", q)
  205. @topic_count = @memos_all.count
  206. @topic_pages = Paginator.new @topic_count, @limit, params['page']
  207. @offset ||= @topic_pages.offset
  208. @memos = @memos_all.offset(@offset).limit(@limit).all
  209. respond_to do |format|
  210. format.html {
  211. render 'show', :layout => 'base_forums'
  212. }
  213. format.json { render json: @forum }
  214. end
  215. end
  216. private
  217. def find_forum_if_available
  218. @forum = Forum.find(params[:id]) if params[:id]
  219. rescue ActiveRecord::RecordNotFound
  220. render_404
  221. nil
  222. end
  223. def authenticate_user_edit
  224. find_forum_if_available
  225. render_403 unless @forum.editable_by? User.current
  226. end
  227. def authenticate_user_destroy
  228. find_forum_if_available
  229. render_403 unless @forum.destroyable_by? User.current
  230. end
  231. end

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