class TopicsController < ApplicationController before_filter :find_forum_and_topic, :except => :index @@hubssolib_permissions = HubSsoLib::Permissions.new({ :new => [ :admin, :webmaster, :privileged, :normal ], :create => [ :admin, :webmaster, :privileged, :normal ], :edit => [ :admin, :webmaster ], :update => [ :admin, :webmaster ], :destroy => [ :admin, :webmaster ], }) def TopicsController.hubssolib_permissions @@hubssolib_permissions end def index respond_to do |format| format.html { redirect_to forum_path(params[:forum_id]) } format.xml do @topics = Topic.find_all_by_forum_id(params[:forum_id], :order => 'sticky desc, replied_at desc', :limit => 25) render :xml => @topics.to_xml end end end def new @topic = Topic.new end def show respond_to do |format| format.html do # see notes in application.rb on how this works update_last_seen_at # keep track of when we last viewed this topic for activity indicators (session[:topics] ||= {})[@topic.id] = Time.now.utc if logged_in? # authors of topics don't get counted towards total hits @topic.hit! unless logged_in? and @topic.user == current_user per_page = [params[:posts_per_page].to_i, 25].max @post_pages, @posts = paginate(:posts, :per_page => per_page, :order => 'posts.created_at', :include => :user, :conditions => ['posts.topic_id = ?', params[:id]]) @voices = @posts.map(&:user) ; @voices.uniq! @post = Post.new end format.xml do render :xml => @topic.to_xml end format.rss do @posts = @topic.posts.find(:all, :order => 'created_at desc', :limit => 50) render :action => 'show.rxml', :layout => false end end end def create # this is icky - move the topic/first post workings into the topic model? Topic.transaction do @topic = @forum.topics.build(params[:topic]) assign_protected @topic.save! @post = @topic.posts.build(params[:topic]) @post.user = current_user @post.save! end respond_to do |format| format.html { redirect_to topic_path(@forum, @topic) } format.xml { head :created, :location => formatted_topic_url(:forum_id => @forum, :id => @topic, :format => :xml) } end rescue ActiveRecord::RecordInvalid flash[:error] = "Your topic's first post was empty, or contained prohibited words" respond_to do |format| format.html { redirect_to forum_path(@forum) } format.xml { render :xml => @post.errors.to_xml, :status => 400 } end end def update @topic.attributes = params[:topic] assign_protected @topic.save! respond_to do |format| format.html { redirect_to topic_path(@forum, @topic) } format.xml { head 200 } end end def destroy @topic.destroy flash[:notice] = "Topic '#{CGI::escapeHTML @topic.title}' was deleted." respond_to do |format| format.html { redirect_to forum_path(@forum) } format.xml { head 200 } end end protected def assign_protected @topic.user = current_user if @topic.new_record? # admins and moderators can sticky and lock topics return unless admin? or current_user.moderator_of?(@topic.forum) @topic.sticky, @topic.locked = params[:topic][:sticky], params[:topic][:locked] # only admins can move return unless admin? @topic.forum_id = params[:topic][:forum_id] if params[:topic][:forum_id] end def find_forum_and_topic @forum = Forum.find(params[:forum_id]) @topic = @forum.topics.find(params[:id]) if params[:id] end def authorized? %w(new create).include?(action_name) || @topic.editable_by?(current_user) end end