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

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