# # Used by Gitlab only # class ArticlesController < ApplicationController after_filter :after_article, :only => [ :find_by_permalink ] before_filter :find_project, :except => [ :find_by_date, :find_by_permalink, :find_by_type, :find_by_category, :find_by_user, :find_by_sitename, :find_by_sitename_and_type, :find_by_project_and_type ] before_filter :authorised, :only => [ :update, :edit, :destroy, :new, :create ] # Show all articles, render grouped by year, month def index # Project articles index if @project @articles = @is_member ? Article.find_by_project!(@project.id) : Article.find_by_project(@project.id, current_user) @title, @url_suffix = Project.tab_settings(PRJ_TAB_ARTICLES) render :action => "ac_project_index" else # Archives articles index @title = "Archives" @articles = Article.find_all_articles(current_user) unless @articles.empty? @show_archive_breadcrumb = false render :action => "ac_index" else flash[:error] = "No articles found." redirect_to root_path end end end # not used as only code to display a single article uses find_by_permalink def show end def edit if @project # Project article @article = Article.find_by_id!(params[:id]) if @article @title = "Editing: " + @project.title + ": " + @article.title render :action => "ac_project_edit" else flash[:error] = "Article not found." redirect_to project_path(@project) end else # Normal article index page end end def update if @project # Project article @article = Article.find_by_id(params[:id], current_user) if @article #FIXME is this correct?, does deep_clone make one? (if not it should) original_article = Article.new original_article = @article.deep_clone if @article.update_attributes(params[:article]) @article.save Journal.update(original_article, @article, request.env["REMOTE_ADDR"], @project.id, current_user.id, "Edited article: \'" + @article.title + "\'.") flash[:notice] = "Article was successfully updated." redirect_to project_articles_path(@project) else @title = "Editing: " + @project.title + ": " + @article.title render :action => "ac_project_edit" end else flash[:error] = "Article not found." redirect_to project_path(@project) end else # Normal Article end end def destroy if @is_member @article = Article.find(params[:id]) if @article original_article = Article.new original_article = @article.deep_clone @article.status = :deleted if @article.save Journal.update(original_article, @article, request.env["REMOTE_ADDR"], @project.id, current_user.id, "Deleted article: \'" + @article.title + "\'.") flash[:notice] = 'Article deleted.' else #TODO add error report flash[:error] = 'An error occurred.' end redirect_to project_articles_path(@project) else flash[:error] = 'Article ' + params[:id] + ' does not exist.' redirect_to project_articles_path(@project) end end end # Show all articles /2008 or /2008/11 or /2008/11/1 and render grouped by date def find_by_date @articles = Article.find_by_date(current_user, params[:year], params[:month], params[:day]) # # FIXME improve this to show "November 2008" rather than "2008/11" # @title = "Articles for " << params[:year] @title = @title << "/" << params[:month] if params[:month] != ni # unless @articles.empty? @show_archive_breadcrumb = true render :action => "ac_index" else flash[:error] = "Article does not exist." redirect_to articles_path end end # Show /2008/11/1/permalink-title def find_by_permalink # @article = Article.find_by_permalink(params[:permalink], current_user) if @article @title = @article.title render :action => "ac_show" else flash[:error] = "Article does not exist." redirect_to articles_path end end # Show all 'News' articles def find_by_type @articles = Article.find_by_type(params[:article_type], current_user) unless @articles.empty? #@title = Article.format_article_type(params[:article_type]) @show_archive_breadcrumb = true @title = Article::format_article_type(@articles[0].article_type) #FIXME ?? render :action => "ac_index" else flash[:error] = "No articles for type '" << params[:article_type] << "'." redirect_to articles_path end end # Show all articles for a category def find_by_category # # get category_id from :category category = Category.find_by_slug(params[:category]) if category # @articles = Article.find_by_category(category.id, current_user) unless @articles.empty? @title = "'" << category.name << "' Articles" @show_archive_breadcrumb = true render :action => "ac_index" else flash[:error] = "No articles for category '" << category.name << "'." redirect_to articles_path end else flash[:error] = "'" << params[:category] << "' category not found." redirect_to articles_path end end # Show all articles for a project def find_by_project if @project @articles = Article.find_by_project(@project.id, current_user) unless @articles.empty? @title = @project.title << " Articles" @show_archive_breadcrumb = true render :action => "ac_index" else flash[:error] = "No articles found for project '" << @project.title << "'." redirect_to project_path(@project) end else flash[:error] = "Project not found." redirect_to root_path end end # Show all articles for a user def find_by_user @user = User.find_by_slug(params[:user_slug]) if @user @articles = Article.find_by_user(@user.id, current_user) unless @articles.empty? @title = @user.fullname << " Articles" @show_archive_breadcrumb = true render :action => "ac_index" else flash[:error] = "No articles found for '" << @user.fullname << "'." redirect_to articles_path end else flash[:error] = "User not found." redirect_to articles_path end end # Show all articles for sitename def find_by_sitename @articles = Article.find_by_sitename unless @articles.empty? @title = SITENAME << " Articles" @show_archive_breadcrumb = true render :action => "ac_index" else flash[:error] = "No articles found for Sitename." redirect_to articles_path end end # Show all articles for sitename # FIXME can reuse find_by_sitename for this, by testing if params[:article_type] exists def find_by_sitename_and_type @articles = Article.find_by_sitename_and_type(params[:article_type]) unless @articles.empty? @title = SITENAME << " '" << Article.format_article_type(params[:article_type]) << "' Articles" @show_archive_breadcrumb = true render :action => "ac_index" else flash[:error] = "No " << Article.format_article_type(params[:article_type]) << " articles found for " << SITENAME << "." redirect_to articles_path end end # Show all articles for a project def find_by_project_and_type @articles = Article.find_by_project_and_type(params[:article_type], @project.id, current_user) unless @articles.empty? @title = @project.title << " '" << Article.format_article_type(params[:article_type]) << "' Articles" @show_archive_breadcrumb = true render :action => "ac_index" else flash[:error] = "No '" << Article.format_article_type(params[:article_type]) << "' articles found for project '" << @project.name << "'." redirect_to articles_path end end protected # Increase view count for a permalink page def after_article if @article @article.views = @article.views + 1 @article.save! end end end