PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/app/controllers/my_space_controller.rb

http://mushplatform.googlecode.com/
Ruby | 300 lines | 244 code | 41 blank | 15 comment | 20 complexity | 608d9fc78e5c5eb3e76e893c89c90c2a MD5 | raw file
Possible License(s): LGPL-2.1
  1. class MySpaceController < ApplicationController
  2. # caches_page :index, :show
  3. skip_before_filter :verify_authenticity_token,:only => [:create_comment]
  4. include ControllerExtend
  5. layout :theme_layout
  6. Blog_Count_PerPage = 16
  7. Rss_Blog_Perppage = 20
  8. Comment_Count_PerPage = 50
  9. Photo_PerPage = 16
  10. Latest_Votes_Count = 5
  11. Latest_Blogs_Count = 5
  12. Latest_Photos_Count = 6
  13. Latest_Groups_Count = 5
  14. Latest_Friends_Count = 6
  15. Hot_Blog_Count = 10
  16. Latest_Messages_Count = 20
  17. List_Friends_Count = 24
  18. List_Votes_Count = 20
  19. Groups_Count_For_List = 10
  20. helper_method :current_blog_user
  21. before_filter :subdomain_exist?
  22. before_filter :setup_theme
  23. before_filter :set_visite
  24. def index
  25. BlogConfig.add_view_count(current_blog_user.id)
  26. unless current_theme.is_sns_theme?
  27. general_blog_index
  28. else
  29. sns_index
  30. end
  31. return true
  32. end
  33. def general_blog_index
  34. entries = Blog.publised_blogs.paginate(:page => params[:page]||1,:per_page => Blog_Count_PerPage, :conditions =>generate_conditions,:order => 'if_top desc,id desc')
  35. blog_owner = is_blog_admin? ? current_user : nil
  36. render_liquid({:template => 'entries',:layout => true},{'entries' => entries,'blog_owner' => blog_owner,'will_paginate_options' => {'prev_label' => '???','next_label' => '???'}.merge(keep_params)})
  37. end
  38. def sns_index
  39. @votes = current_blog_user.votes.find(:all,:limit => Latest_Votes_Count,:order => 'id desc')
  40. @blogs = current_blog_user.blogs.find(:all,:limit => Latest_Blogs_Count,:conditions => "published = #{Blog::Published_Blogs}",:order => 'if_top desc,id desc')
  41. @photos = current_blog_user.photos.find(:all,:limit => Latest_Photos_Count,:order => 'id desc')
  42. @message = current_blog_user.messages.find(:first,:order => 'id desc')
  43. @friends = current_blog_user.friends.find(:all,:limit => Latest_Friends_Count,:order => 'id desc')
  44. @user_groups = current_blog_user.user_groups.find(:all,:limit => Latest_Groups_Count,:order => 'id desc')
  45. stat_info()
  46. render :template => 'index'
  47. end
  48. def blogs
  49. @categories = current_blog_user.categories
  50. @hot_blogs = Blog.publised_blogs.find(:all,:limit => Hot_Blog_Count,:order => "comment_count desc, view_count desc",:conditions => "user_id=#{current_blog_user.id}")
  51. @blogs = Blog.publised_blogs.paginate(:page => params[:page]||1,:per_page => Blog_Count_PerPage, :conditions => generate_conditions,:order => 'if_top desc,id desc')
  52. stat_info()
  53. render :template => 'blogs'
  54. return true
  55. end
  56. protected :sns_index,:general_blog_index
  57. def show
  58. @blog = Blog.find(params[:id])
  59. if @blog.user_id != current_blog_user.id
  60. render :text => '??????'
  61. return true
  62. end
  63. @blog.add_view_count
  64. BlogConfig.add_view_count(current_blog_user.id)
  65. @comments = @blog.comments.paginate(:page => params[:page]||1,:per_page => Comment_Count_PerPage,:order => "created_at")
  66. unless current_theme.is_sns_theme?
  67. render_liquid({:template => 'entry',:layout => true},{'entry' => @blog,'if_login' => current_user,'comments' => @comments ,'will_paginate_options' => {'prev_label' => '???','next_label' => '???',:page => params[:page]||1,:path => "#{request.path.gsub(/\/comments\/page\/\d+/,'')}/comments"}})
  68. else
  69. stat_info
  70. @categories = current_blog_user.categories
  71. @hot_blogs = Blog.publised_blogs.find(:all,:limit => Hot_Blog_Count,:order => "comment_count desc, view_count desc",:conditions => "user_id=#{current_blog_user.id}")
  72. render :template => 'blog'
  73. end
  74. return true
  75. end
  76. def create_comment
  77. if user = (current_user || (params[:user] && User.authenticate(params[:user][:user_name],params[:user][:password])))
  78. @comment = Comment.new(params[:comment])
  79. @comment.author = user.real_name
  80. @comment.email = user.email
  81. @comment.user_id = user.id
  82. @comment.blog_user_id = current_blog_user.id
  83. if @comment.save
  84. Blog.connection.execute("update blogs set comment_count = comment_count + 1 where id=#{@comment.blog_id}")
  85. unless current_theme.is_sns_theme?
  86. content = parse_liquid('_comment',{'comment' => @comment })
  87. render :update do |page|
  88. page.insert_html :bottom, 'comments',content
  89. end
  90. else
  91. render :update do |page|
  92. page.insert_html :bottom, 'comments',:partial => "/share/comment",:locals => {:comment => @comment }
  93. end
  94. end
  95. else
  96. render :update do |page|
  97. page.replace_html 'erro_info',@comment.errors.full_messages.join(';')
  98. page['erro_info'].show
  99. end
  100. end
  101. else
  102. render :update do |page|
  103. page.replace_html 'erro_info', "???????????????"
  104. page['erro_info'].show
  105. end
  106. end
  107. end
  108. def rss
  109. @blogs = Blog.publised_blogs.find(:all,:conditions => "user_id=#{current_blog_user.id}",:limit => Rss_Blog_Perppage)
  110. @user = current_blog_user
  111. render :content_type => "application/xml",:template => "/my_space/rss",:layout => 'rss'
  112. end
  113. def photos
  114. @photos = Photo.user_photos(current_blog_user.id).paginate(:page => params[:page],:per_page => Photo_PerPage,:order => "id desc")
  115. unless current_theme.is_sns_theme?
  116. render_liquid({:template => 'photos',:layout => true},{'photos' => @photos,'if_login' => current_user ? true : false,'comments' => @comments ,'will_paginate_options' => {'prev_label' => '???','next_label' => '???',:page => params[:page]||1}})
  117. else
  118. render :template => "photos"
  119. end
  120. return
  121. end
  122. def photo
  123. @photo = Photo.find(params[:id])
  124. if @photo.user_id != current_blog_user.id
  125. render :text => '??????'
  126. return
  127. end
  128. @next_photo = Photo.find(:first,:conditions => "id>#{@photo.id} and user_id=#{current_blog_user.id}",:order => "id")
  129. @per_photo = Photo.find(:first,:conditions => "id<#{@photo.id} and user_id=#{current_blog_user.id}",:order => "id")
  130. unless current_theme.is_sns_theme?
  131. render_liquid({:template => 'photo',:layout => true},{'photo' => @photo,'if_login' => current_user ? true : false})
  132. else
  133. @photos = current_blog_user.photos.find(:all,:limit => Latest_Photos_Count,:order => 'id desc')
  134. render :template => 'photo'
  135. end
  136. return
  137. end
  138. def messages
  139. @messages = current_blog_user.messages.paginate(:page => params[:page],:per_page => Latest_Messages_Count,:order => "id desc")
  140. render :template => 'messages'
  141. end
  142. def friends
  143. @friends = current_blog_user.friends.paginate(:page => params[:page],:per_page => List_Friends_Count,:order => "friends.created_at desc")
  144. render :template => 'friends'
  145. end
  146. def votes
  147. @votes = current_blog_user.votes.paginate(:page => params[:page],:per_page => List_Votes_Count,:order => "id desc")
  148. render :template => 'votes'
  149. end
  150. def vote
  151. @vote = Vote.find(params[:id])
  152. @user_voters = @vote.user_votes.find(:all,:limit => 20,:order => 'id desc')
  153. @vote_options = VoteOption.find(:all,:conditions => "voter_id=#{@vote.id}")
  154. render :template => 'vote'
  155. end
  156. def groups
  157. @user_groups = current_blog_user.user_groups.paginate(:page => params[:page],:per_page => Groups_Count_For_List,:order => "id desc")
  158. render :template => 'user_groups'
  159. end
  160. def search
  161. unless params[:keyword].blank?
  162. @categories = current_blog_user.categories
  163. @hot_blogs = Blog.publised_blogs.find(:all,:limit => Hot_Blog_Count,:order => "comment_count desc, view_count desc",:conditions => "user_id=#{current_blog_user.id}")
  164. conditions = ["user_id=#{current_blog_user.id} and (title like ? or keywords like ? or body like ?)","%#{params[:keyword]}%","%#{params[:keyword]}%","%#{params[:keyword]}%"]
  165. @blogs = Blog.publised_blogs.paginate(:page => params[:page]||1,:per_page => 50, :conditions => conditions)
  166. render :template => 'blogs'
  167. else
  168. redirect_to :action => 'index'
  169. end
  170. return
  171. end
  172. def profile
  173. render :template => 'profile'
  174. end
  175. protected
  176. def set_visite
  177. return true if current_user.nil? || current_user.id == current_blog_user.id#if ??????????????
  178. if current_user && session["visite_info_#{current_blog_user.id}"].blank?
  179. current_user.visite(current_blog_user.id)
  180. session["visite_info_#{current_blog_user.id}"] = 'yes'
  181. end
  182. end
  183. def subdomain_exist?
  184. unless current_blog_user
  185. render :template => "/my_space/none_user",:layout => false
  186. return false
  187. end
  188. return true
  189. end
  190. def stat_info
  191. @blogs_count = Blog.count('id', :conditions => "user_id = #{current_blog_user.id} and published = #{Blog::Published_Blogs}")
  192. @comments_count = Comment.count('id', :conditions => "blog_user_id = #{current_blog_user.id}")
  193. blog_config = BlogConfig.find_by_user_id(current_blog_user.id)
  194. if blog_config
  195. @hits_count = blog_config.view_count
  196. else
  197. @hits_count = Blog.sum('view_count', :conditions => "user_id = #{current_blog_user.id}")
  198. end
  199. @created_at = current_blog_user.created_at
  200. end
  201. def keep_params
  202. orignal_params = {}
  203. # [:month,:date,:year,:keyword,:category_id,:tag].each do |attr|
  204. # orignal_params[attr.to_s] = params[attr] unless params[attr].blank?
  205. # end
  206. orignal_params['page'] = params[:page] || 1
  207. orignal_params[:path] = request.path.gsub(/\/page\/\d+/,'')
  208. orignal_params
  209. end
  210. def generate_conditions
  211. conditions = ["user_id=#{current_blog_user.id}"]
  212. #????????
  213. if (!params[:month].blank? and !params[:year].blank?) and params[:date].blank?
  214. m_dates = [31,28,31,30,31,30,31,31,30,31,30,31]
  215. limit_date = Date.parse("#{params[:year]}-#{params[:month]}-1")
  216. max_date = limit_date + m_dates[limit_date.month-1] - 1
  217. conditions << "(created_at >= '#{limit_date}' and created_at <= '#{max_date}')"
  218. end
  219. #????????
  220. unless params[:year].blank? or params[:month].blank? or params[:date].blank?
  221. date = Date.parse("#{params[:year]}-#{params[:month]}-#{params[:date]}")
  222. conditions << "(created_at >= '#{date}' and created_at <= '#{date.next}')"
  223. end
  224. #???????????
  225. unless params[:keyword].blank?
  226. conditions << Blog.generate_sql_from_arry(["(title like ? or body like ? or keywords like ?)","%#{params[:keyword]}%","%#{params[:keyword]}%","%#{params[:keyword]}%"])
  227. end
  228. #?????????
  229. unless params[:category_id].blank?
  230. conditions << "category_id=#{params[:category_id]}"
  231. end
  232. #?????tag??
  233. unless params[:tag].blank?
  234. tag_id = params[:tag].strip
  235. tag = tag_id =~ /^\d+&/ ? Tag.find(tag_id) : Tag.find_by_name(tag_id)
  236. conditions << "id in (select taggable_id from taggings where tag_id=#{tag.id})"
  237. end
  238. conditions.join(' and ')
  239. end
  240. end
  241. #??subdomain page cache??.
  242. module ActionController::Caching::Pages
  243. def cache_page(content = nil, options = {})
  244. return unless perform_caching && caching_allowed
  245. subdomain = ''
  246. #TODO: ???? .com.cn ???(3?)??www??????? request.subdomains.first.blank?
  247. #?????????,??? rewrite rule?????
  248. unless request.subdomains.first.blank? or request.subdomains.first=='www'
  249. subdomain += request.subdomains.first || ''
  250. subdomain = '/' + subdomain unless subdomain.blank?
  251. end
  252. path = subdomain+url_for(options.merge(:only_path => true, :skip_relative_url_root => true, :format => params[:format]))
  253. #self.class.cache_page????page_cache_file?private????????,
  254. #????????????name = ((path.empty? || path == "/") ? "/index" : URI.unescape(path))
  255. #?cache?".ext"???,????.
  256. path += "index"+ page_cache_extension if path[-1..-1]=='/'
  257. self.class.cache_page(content || response.body, path)
  258. end
  259. end