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 7.0 kB

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

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