diff --git a/app/controllers/public/content_viewer_controller.rb b/app/controllers/public/content_viewer_controller.rb index 3b7d9e4..5a4a86a 100644 --- a/app/controllers/public/content_viewer_controller.rb +++ b/app/controllers/public/content_viewer_controller.rb @@ -8,49 +8,31 @@ class ContentViewerController < ApplicationController helper TagsHelper def view_page - path = params[:page] - path = path.join('/') if path.kind_of?(Array) - path = "#{path}.#{params[:format]}" if params[:format] + path = get_path(params[:page], params[:format]) + @version = params[:version].to_i if path.blank? - @page = profile.home_page - if @page.nil? - redirect_to :controller => 'profile', :action => 'index', :profile => profile.identifier - return - end + @page = profile.home_page + return if redirected_to_profile_index else @page = profile.articles.find_by_path(path) - unless @page - page_from_old_path = profile.articles.find_by_old_path(path) - if page_from_old_path - redirect_to profile.url.merge(:page => page_from_old_path.explode_path) - return - end - end + return if redirecetd_page_from_old_path(path) end return unless allow_access_to_page(path) if @version > 0 return render_access_denied unless @page.display_versions? - @versioned_article = @page.versions.find_by_version(@version) - if @versioned_article && @page.versions.latest.version != @versioned_article.version - render :template => 'content_viewer/versioned_article.html.erb' - return - end + return if rendered_versioned_article end redirect_to_translation and return if @page.profile.redirect_l10n - if request.post? - if @page.forum? && @page.has_terms_of_use && params[:terms_accepted] == "true" - @page.add_agreed_user(user) - end - elsif !@page.parent.nil? && @page.parent.forum? - unless @page.parent.agrees_with_terms?(user) - redirect_to @page.parent.url - end + if request.post? && @page.forum? + process_forum_terms_of_use(user, params[:terms_accepted]) + elsif is_a_forum_topic?(@page) + redirect_to @page.parent.url unless @page.parent.agrees_with_terms?(user) end # At this point the page will be showed @@ -58,64 +40,22 @@ class ContentViewerController < ApplicationController @page = FilePresenter.for @page - if @page.download? params[:view] - headers['Content-Type'] = @page.mime_type - headers.merge! @page.download_headers - data = @page.data - - # TODO test the condition - if data.nil? - raise "No data for file" - end - - render :text => data, :layout => false - return - end + return if rendered_file_download(params[:view]) @form_div = params[:form] #FIXME see a better way to do this. It's not need to pass this variable anymore @comment = Comment.new - if @page.has_posts? - posts = if params[:year] and params[:month] - filter_date = DateTime.parse("#{params[:year]}-#{params[:month]}-01") - @page.posts.by_range(filter_date..filter_date.at_end_of_month) - else - @page.posts - end - - #FIXME Need to run this before the pagination because this version of - # will_paginate returns a will_paginate collection instead of a - # relation. - blog_with_translation = @page.blog? && @page.display_posts_in_current_language? - posts = posts.native_translations if blog_with_translation - - @posts = posts.paginate({ :page => params[:npage], :per_page => @page.posts_per_page }.merge(Article.display_filter(user, profile))).to_a - - if blog_with_translation - @posts.replace @posts.map{ |p| p.get_translation_to(FastGettext.locale) }.compact - end - end + process_page_posts(params) if @page.folder? && @page.gallery? - @images = @page.images.select{ |a| a.display_to? user } - @images = @images.paginate(:per_page => per_page, :page => params[:npage]) unless params[:slideshow] + @images = get_images(@page, params[:npage], params[:slideshow]) end - @unfollow_form = params[:unfollow] && params[:unfollow] == 'true' - if params[:unfollow] && params[:unfollow] == 'commit' && request.post? - @page.followers -= [params[:email]] - if @page.save - session[:notice] = _("Notification of new comments to '%s' was successfully canceled") % params[:email] - end - end + process_page_followers(params) - @comments = @page.comments.without_spam - @comments = @plugins.filter(:unavailable_comments, @comments) - @comments_count = @comments.count - @comments = @comments.without_reply.paginate(:per_page => per_page, :page => params[:comment_page] ) - @comment_order = params[:comment_order].nil? ? 'oldest' : params[:comment_order] + process_comments(params) if request.xhr? and params[:comment_order] if @comment_order == 'newest' @@ -203,4 +143,127 @@ class ContentViewerController < ApplicationController user_agent.match(/crawler/) || user_agent.match(/\(.*https?:\/\/.*\)/) end + + def get_path(page, format = nil) + path = page + path = path.join('/') if path.kind_of?(Array) + path = "#{path}.#{format}" if format + + return path + end + + def redirected_to_profile_index + if @page.nil? + redirect_to :controller => 'profile', :action => 'index', :profile => profile.identifier + return true + end + + return false + end + + def redirecetd_page_from_old_path(path) + unless @page + page_from_old_path = profile.articles.find_by_old_path(path) + if page_from_old_path + redirect_to profile.url.merge(:page => page_from_old_path.explode_path) + return true + end + end + + return false + end + + def process_forum_terms_of_use(user, terms_accepted = nil) + if @page.forum? && @page.has_terms_of_use && terms_accepted == "true" + @page.add_agreed_user(user) + end + end + + def is_a_forum_topic? (page) + return (!@page.parent.nil? && @page.parent.forum?) + end + + def rendered_versioned_article + @versioned_article = @page.versions.find_by_version(@version) + if @versioned_article && @page.versions.latest.version != @versioned_article.version + render :template => 'content_viewer/versioned_article.html.erb' + return true + end + + return false + end + + def rendered_file_download(view = nil) + if @page.download? view + headers['Content-Type'] = @page.mime_type + headers.merge! @page.download_headers + data = @page.data + + # TODO test the condition + if data.nil? + raise "No data for file" + end + + render :text => data, :layout => false + return true + end + + return false + end + + def process_page_posts(params) + if @page.has_posts? + posts = get_posts(params[:year], params[:month]) + + #FIXME Need to run this before the pagination because this version of + # will_paginate returns a will_paginate collection instead of a + # relation. + posts = posts.native_translations if blog_with_translation?(@page) + + @posts = posts.paginate({ :page => params[:npage], :per_page => @page.posts_per_page }.merge(Article.display_filter(user, profile))).to_a + + if blog_with_translation?(@page) + @posts.replace @posts.map{ |p| p.get_translation_to(FastGettext.locale) }.compact + end + end + end + + def get_posts(year = nil, month = nil) + if year && month + filter_date = DateTime.parse("#{year}-#{month}-01") + return @page.posts.by_range(filter_date..filter_date.at_end_of_month) + else + return @page.posts + end + end + + def blog_with_translation?(page) + return (page.blog? && page.display_posts_in_current_language?) + end + + def get_images(page, npage, slideshow) + images = page.images.select{ |a| a.display_to? user } + images = images.paginate(:per_page => per_page, :page => npage) unless slideshow + + return images + end + + def process_page_followers(params) + @unfollow_form = params[:unfollow] == 'true' + if params[:unfollow] == 'commit' && request.post? + @page.followers -= [params[:email]] + if @page.save + session[:notice] = _("Notification of new comments to '%s' was successfully canceled") % params[:email] + end + end + end + + def process_comments(params) + @comments = @page.comments.without_spam + @comments = @plugins.filter(:unavailable_comments, @comments) + @comments_count = @comments.count + @comments = @comments.without_reply.paginate(:per_page => per_page, :page => params[:comment_page] ) + @comment_order = params[:comment_order].nil? ? 'oldest' : params[:comment_order] + end + end -- libgit2 0.21.2