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

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
11 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
11 years ago
​
11 years ago
​
​
11 years ago
​
​
11 years ago
​
​
​
11 years ago
​
​
​
​
​
​
11 years ago
​
​
​
​
​
​
11 years ago
​
​
​
​
11 years ago
​
11 years ago
​
11 years ago
​
11 years ago
​
11 years ago
​
​
​
​
11 years ago
​
11 years ago
​
11 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
11 years ago
​
​
​
11 years ago
​
​
​
​
11 years ago
​
​
​
​
​
​
​
​
​
​
​
​
11 years ago
​
​
​
11 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
11 years ago
​
​
​
​
11 years ago
11 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. # added by fq
  2. class ForumsController < ApplicationController
  3. layout "users_base"
  4. include ApplicationHelper
  5. # GET /forums
  6. # GET /forums.json
  7. before_filter :find_forum_if_available
  8. before_filter :authenticate_user_edit, :only => [:edit, :update]
  9. before_filter :authenticate_user_destroy, :only => [:destroy]
  10. before_filter :require_login, :only => [:new, :create,:destroy,:update,:edit]
  11. helper :sort
  12. include SortHelper
  13. PageLimit = 20
  14. def create_feedback
  15. if User.current.logged?
  16. #@memo = Memo.new(params[:memo])
  17. #@memo.forum_id = "1"
  18. #@memo.author_id = User.current.id
  19. #@forum = @memo.forum
  20. cs = CommentService.new
  21. @memo,message = cs.create_feedback params,User.current
  22. respond_to do |format|
  23. if !@memo.new_record?
  24. format.html { redirect_to forum_path(@memo.forum) }
  25. else
  26. sort_init 'updated_at', 'desc'
  27. sort_update 'created_at' => "#{Memo.table_name}.created_at",
  28. 'replies' => "#{Memo.table_name}.replies_count",
  29. 'updated_at' => "COALESCE (last_replies_memos.created_at, #{Memo.table_name}.created_at)"
  30. @topic_count = @forum.topics.count
  31. @topic_pages = Paginator.new @topic_count, per_page_option, params['page']
  32. @memos = @forum.topics.
  33. reorder("#{Memo.table_name}.sticky DESC").
  34. includes(:last_reply).
  35. limit(@topic_pages.per_page).
  36. offset(@topic_pages.offset).
  37. order(sort_clause).
  38. preload(:author, {:last_reply => :author}).
  39. all
  40. flash.now[:error] = "#{l :label_memo_create_fail}: #{@memo.errors.full_messages[0]}"
  41. # back_error_page = @memo.parent_id.nil? ? forum_path(@forum) : forum_memo_path(@forum, @memo.parent_id)
  42. format.html { render action: :show, layout: 'base_forums' }#, error: "#{l :label_memo_create_fail}: #{@memo.errors.full_messages[0]}" }
  43. format.json { render json: @memo.errors, status: :unprocessable_entity }
  44. end
  45. end
  46. else
  47. respond_to do |format|
  48. format.html { redirect_to signin_path }
  49. end
  50. end
  51. end
  52. def create_memo
  53. @memo = Memo.new(params[:memo])
  54. @memo.forum_id = @forum.id
  55. @memo.author_id = User.current.id
  56. @memo.save_attachments(params[:attachments] || (params[:memo] && params[:memo][:uploads]))
  57. respond_to do |format|
  58. if @memo.save
  59. if params[:asset_id]
  60. ids = params[:asset_id].split(',')
  61. update_kindeditor_assets_owner ids ,@memo.id,OwnerTypeHelper::MEMO
  62. end
  63. #end
  64. format.html { redirect_to (forum_memo_url(@forum, (@memo.parent_id.nil? ? @memo : @memo.parent_id))), notice: "#{l :label_memo_create_succ}" }
  65. format.json { render json: @memo, status: :created, location: @memo }
  66. else
  67. sort_init 'updated_at', 'desc'
  68. sort_update 'created_at' => "#{Memo.table_name}.created_at",
  69. 'replies' => "#{Memo.table_name}.replies_count",
  70. 'updated_at' => "COALESCE (last_replies_memos.created_at, #{Memo.table_name}.created_at)"
  71. @topic_count = @forum.topics.count
  72. @topic_pages = Paginator.new @topic_count, per_page_option, params['page']
  73. @memos = @forum.topics.
  74. reorder("#{Memo.table_name}.sticky DESC").
  75. includes(:last_reply).
  76. limit(@topic_pages.per_page).
  77. offset(@topic_pages.offset).
  78. order(sort_clause).
  79. preload(:author, {:last_reply => :author}).
  80. all
  81. @memos
  82. flash.now[:error] = "#{l :label_memo_create_fail}: #{@memo.errors.full_messages[0]}"
  83. # back_error_page = @memo.parent_id.nil? ? forum_path(@forum) : forum_memo_path(@forum, @memo.parent_id)
  84. format.html { render action: :show, layout: 'base_forums' }#, error: "#{l :label_memo_create_fail}: #{@memo.errors.full_messages[0]}" }
  85. format.json { render json: @memo.errors, status: :unprocessable_entity }
  86. end
  87. end
  88. end
  89. def index
  90. @offset, @limit = api_offset_and_limit({:limit => 10})
  91. if(params[:reorder_complex])
  92. @type="reorder_complex"
  93. @str=params[:reorder_complex]
  94. @forums_all = Forum.reorder("topic_count #{params[:reorder_complex]},updated_at #{params[:reorder_complex]}")
  95. elsif(params[:reorder_popu])
  96. @type="reorder_popu"
  97. @str=params[:reorder_popu]
  98. @forums_all = Forum.reorder("topic_count #{params[:reorder_popu]}")
  99. elsif(params[:reorder_time])
  100. @type="reorder_time"
  101. @str=params[:reorder_time]
  102. @forums_all = Forum.reorder("updated_at #{params[:reorder_time]}")
  103. else
  104. params[:reorder_complex] = "desc"
  105. @type="reorder_complex"
  106. @str=params[:reorder_complex]
  107. @forums_all = Forum.reorder("topic_count desc,updated_at desc")
  108. end
  109. @forums_count = @forums_all.count
  110. @forums_pages = Paginator.new @forums_count, @limit, params['page']
  111. @offset ||= @forums_pages.offset
  112. @forums = @forums_all.offset(@offset).limit(@limit).all
  113. #@forums = Forum.all
  114. respond_to do |format|
  115. format.html # index.html.erb
  116. format.js
  117. format.json { render json: @forums }
  118. end
  119. end
  120. # GET /forums/1
  121. # GET /forums/1.json
  122. def show
  123. # sort_init 'updated_at', 'desc'
  124. # sort_update 'created_at' => "#{Memo.table_name}.created_at",
  125. # 'replies' => "#{Memo.table_name}.replies_count",
  126. # 'updated_at' => "COALESCE (last_replies_memos.created_at, #{Memo.table_name}.created_at)"
  127. order = ""
  128. @order_str = ""
  129. if(params[:reorder_complex])
  130. order = " last_replies_memos.created_at #{params[:reorder_complex]}, #{Memo.table_name}.created_at #{params[:reorder_complex]}"
  131. @order_str = "reorder_complex="+params[:reorder_complex]
  132. elsif(params[:reorder_popu])
  133. order = "replies_count #{params[:reorder_popu]}"
  134. @order_str = "reorder_popu="+params[:reorder_popu]
  135. elsif(params[:reorder_time])
  136. order = "#{Memo.table_name}.updated_at #{params[:reorder_time]}"
  137. @order_str = "reorder_time="+params[:reorder_time]
  138. else
  139. order = "#{Memo.table_name}.updated_at desc"
  140. @order_str = "reorder_time=desc"
  141. end
  142. @memo = Memo.new(:forum => @forum)
  143. @topic_count = @forum.topics.count
  144. @topic_pages = Paginator.new @topic_count, per_page_option, params['page']
  145. @memos = @forum.topics.
  146. # reorder("#{Memo.table_name}.sticky DESC").
  147. includes(:last_reply).
  148. limit(@topic_pages.per_page).
  149. offset(@topic_pages.offset).
  150. reorder(order).
  151. preload(:author, {:last_reply => :author}).
  152. all
  153. @memos
  154. @my_topic_count = Memo.where("forum_id = #{@forum.id} and author_id = #{User.current.id} and parent_id is null").count
  155. @my_replies_count = Memo.where("forum_id = #{@forum.id} and author_id = #{User.current.id} and parent_id is not null").count
  156. @errors = params[:errors]
  157. respond_to do |format|
  158. format.js
  159. format.html {
  160. render :layout => 'base_forums'
  161. }# show.html.erb
  162. format.json { render json: @forum }
  163. end
  164. end
  165. # GET /forums/new
  166. # GET /forums/new.json
  167. def new
  168. @forum = Forum.new
  169. respond_to do |format|
  170. format.html # new.html.erb
  171. format.json { render json: @forum }
  172. end
  173. end
  174. # GET /forums/1/edit
  175. def edit
  176. @forum = Forum.find(params[:id])
  177. end
  178. # POST /forums
  179. # POST /forums.json
  180. def create
  181. @forum = Forum.new(params[:forum])
  182. @forum.creator_id = User.current.id
  183. if @forum.save
  184. # Time 2015-03-24 17:07:05
  185. # Author lizanle
  186. # Description after save后需要进行资源记录的更新
  187. # owner_type = 2 对应的是 forum
  188. @save_flag=true
  189. if params[:asset_id]
  190. ids = params[:asset_id].split(',')
  191. update_kindeditor_assets_owner ids ,@forum.id,OwnerTypeHelper::FORUM
  192. end
  193. #end
  194. respond_to do |format|
  195. format.js
  196. format.html { redirect_to @forum, notice: l(:label_forum_create_succ) }
  197. format.json { render json: @forum, status: :created, location: @forum }
  198. end
  199. else
  200. @save_flag=false
  201. respond_to do |format|
  202. flash.now[:error] = "#{l :label_forum_create_fail}: #{@forum.errors.full_messages[0]}"
  203. format.js
  204. format.html { render action: "new" }
  205. format.json { render json: @forum.errors, status: :unprocessable_entity }
  206. end
  207. end
  208. end
  209. # PUT /forums/1
  210. # PUT /forums/1.json
  211. def update
  212. @forum = Forum.find(params[:id])
  213. respond_to do |format|
  214. if @forum.update_attributes(params[:forum])
  215. format.js {render :text=> true}
  216. format.html { redirect_to @forum, notice: l(:label_forum_update_succ) }
  217. format.json { head :no_content }
  218. else
  219. flash.now[:error] = "#{l :label_forum_update_fail}: #{@forum.errors.full_messages[0]}"
  220. format.js { render :text=> false}
  221. format.html { render action: "edit" }
  222. format.json { render json: @forum.errors, status: :unprocessable_entity }
  223. end
  224. end
  225. end
  226. # DELETE /forums/1
  227. # DELETE /forums/1.json
  228. def destroy
  229. @forum = Forum.find(params[:id])
  230. @forum.destroy
  231. respond_to do |format|
  232. format.html { redirect_to forums_url }
  233. format.json { head :no_content }
  234. end
  235. end
  236. def search_forum
  237. # @forums = paginateHelper Forum.where("name LIKE '%#{params[:name]}%'")
  238. q = "%#{params[:name].strip}%"
  239. (redirect_to forums_url, :notice => l(:label_sumbit_empty);return) if params[:name].blank?
  240. @offset, @limit = api_offset_and_limit({:limit => 10})
  241. @forums_all = Forum.where("name LIKE ?", q)
  242. @forums_count = @forums_all.count
  243. @forums_pages = Paginator.new @forums_count, @limit, params['page']
  244. @offset ||= @forums_pages.offset
  245. @forums = @forums_all.offset(@offset).limit(@limit).all
  246. respond_to do |format|
  247. format.html {
  248. render 'index'
  249. }
  250. format.json { render json: @forums }
  251. end
  252. end
  253. def search_memo
  254. q = "%#{params[:name].strip}%"
  255. limit = PageLimit
  256. @memo = Memo.new
  257. @offset, @limit = api_offset_and_limit({:limit => limit})
  258. @forum = Forum.find(params[:id])
  259. @memos_all = @forum.topics.where("subject LIKE ?", q)
  260. @topic_count = @memos_all.count
  261. @topic_pages = Paginator.new @topic_count, @limit, params['page']
  262. @offset ||= @topic_pages.offset
  263. @memos = @memos_all.offset(@offset).limit(@limit).all
  264. respond_to do |format|
  265. format.html {
  266. render 'show', :layout => 'base_forums'
  267. }
  268. format.json { render json: @forum }
  269. end
  270. end
  271. #检查forum的名字
  272. def check_forum_name
  273. forum_name_exist = true
  274. if params[:forum_id]
  275. forum_name_exist = Forum.where("name = '#{params[:forum_name]}' and id != #{params[:forum_id]}").count >= 1 ? true : false
  276. else
  277. forum_name_exist = Forum.where("name = '#{params[:forum_name]}' ").count >= 1 ? true : false
  278. end
  279. render :text => forum_name_exist
  280. end
  281. #添加论坛tag
  282. def add_forum_tag
  283. @forum = Forum.find(params[:id])
  284. unless @forum.nil?
  285. @forum.tag_list.add(params[:tag_str].split(','))
  286. @forum.save
  287. end
  288. respond_to do |format|
  289. format.js {render :delete_forum_tag}
  290. end
  291. end
  292. #删除forum的tag
  293. def delete_forum_tag
  294. @tag_id = (ActsAsTaggableOn::Tag.find_by_name(params[:tag_name])).id
  295. #forum的taggable_type = 5
  296. @taggings = ActsAsTaggableOn::Tagging.find_by_tag_id_and_taggable_id_and_taggable_type(@tag_id,params[:id],'Forum')
  297. unless @taggings.nil?
  298. @taggings.delete
  299. end
  300. # 是否还有其他记录 引用了 tag_id
  301. @tagging = ActsAsTaggableOn::Tagging.find_by_tag_id(@tag_id)
  302. # 如果taggings表中记录已经不存在 ,那么检查tags表 作删除动作
  303. if @tagging.nil?
  304. @tag = ActsAsTaggableOn::Tag.find_by_id(@tag_id)
  305. @tag.delete unless @tag.nil?
  306. end
  307. @forum = Forum.find(params[:id])
  308. respond_to do |format|
  309. format.js
  310. end
  311. end
  312. private
  313. def find_forum_if_available
  314. @forum = Forum.find(params[:id]) if params[:id]
  315. rescue ActiveRecord::RecordNotFound
  316. render_404
  317. nil
  318. end
  319. def authenticate_user_edit
  320. find_forum_if_available
  321. render_403 unless @forum.editable_by? User.current
  322. end
  323. def authenticate_user_destroy
  324. find_forum_if_available
  325. render_403 unless @forum.destroyable_by? User.current
  326. end
  327. end

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