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

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

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