Blob of app/controllers/articles_controller.rb (raw blob data)

1 #
2 # Used by Gitlab only
3 #
4 class ArticlesController < ApplicationController
5
6 after_filter :after_article, :only => [ :find_by_permalink ]
7 before_filter :find_project, :except => [ :find_by_date, :find_by_permalink, :find_by_type, :find_by_category,
8 :find_by_user, :find_by_sitename, :find_by_sitename_and_type, :find_by_project_and_type ]
9 before_filter :authorised, :only => [ :update, :edit, :destroy, :new, :create ]
10
11 # Show all articles, render grouped by year, month
12 def index
13 # Project articles index
14 if @project
15 @articles = @is_member ? Article.find_by_project!(@project.id) : Article.find_by_project(@project.id, current_user)
16 @title, @url_suffix = Project.tab_settings(PRJ_TAB_ARTICLES)
17 render :action => "ac_project_index"
18 else
19 # Archives articles index
20 @title = "Archives"
21 @articles = Article.find_all_articles(current_user)
22 unless @articles.empty?
23 @show_archive_breadcrumb = false
24 render :action => "ac_index"
25 else
26 flash[:error] = "No articles found."
27 redirect_to root_path
28 end
29 end
30 end
31
32 # not used as only code to display a single article uses find_by_permalink
33 def show
34 end
35
36 def edit
37 if @project
38 # Project article
39 @article = Article.find_by_id!(params[:id])
40 if @article
41 @title = "Editing: " + @project.title + ": " + @article.title
42 render :action => "ac_project_edit"
43 else
44 flash[:error] = "Article not found."
45 redirect_to project_path(@project)
46 end
47 else
48 # Normal article index page
49 end
50 end
51
52 def update
53 if @project
54 # Project article
55 @article = Article.find_by_id(params[:id], current_user)
56 if @article
57 #FIXME is this correct?, does deep_clone make one? (if not it should)
58 original_article = Article.new
59 original_article = @article.deep_clone
60 if @article.update_attributes(params[:article])
61 @article.save
62 Journal.update(original_article, @article, request.env["REMOTE_ADDR"], @project.id, current_user.id, "Edited article: \'" + @article.title + "\'.")
63 flash[:notice] = "Article was successfully updated."
64 redirect_to project_articles_path(@project)
65 else
66 @title = "Editing: " + @project.title + ": " + @article.title
67 render :action => "ac_project_edit"
68 end
69 else
70 flash[:error] = "Article not found."
71 redirect_to project_path(@project)
72 end
73 else
74 # Normal Article
75 end
76 end
77
78 def destroy
79 if @is_member
80 @article = Article.find(params[:id])
81 if @article
82 original_article = Article.new
83 original_article = @article.deep_clone
84 @article.status = :deleted
85 if @article.save
86 Journal.update(original_article, @article, request.env["REMOTE_ADDR"], @project.id, current_user.id, "Deleted article: \'" + @article.title + "\'.")
87 flash[:notice] = 'Article deleted.'
88 else
89 #TODO add error report
90 flash[:error] = 'An error occurred.'
91 end
92 redirect_to project_articles_path(@project)
93 else
94 flash[:error] = 'Article ' + params[:id] + ' does not exist.'
95 redirect_to project_articles_path(@project)
96 end
97 end
98 end
99
100 # Show all articles /2008 or /2008/11 or /2008/11/1 and render grouped by date
101 def find_by_date
102 @articles = Article.find_by_date(current_user, params[:year], params[:month], params[:day])
103 #
104 # FIXME improve this to show "November 2008" rather than "2008/11"
105 #
106 @title = "Articles for " << params[:year]
107 @title = @title << "/" << params[:month] if params[:month] != ni
108 #
109 unless @articles.empty?
110 @show_archive_breadcrumb = true
111 render :action => "ac_index"
112 else
113 flash[:error] = "Article does not exist."
114 redirect_to articles_path
115 end
116 end
117
118 # Show /2008/11/1/permalink-title
119 def find_by_permalink
120 #
121 @article = Article.find_by_permalink(params[:permalink], current_user)
122 if @article
123 @title = @article.title
124 render :action => "ac_show"
125 else
126 flash[:error] = "Article does not exist."
127 redirect_to articles_path
128 end
129 end
130
131 # Show all 'News' articles
132 def find_by_type
133 @articles = Article.find_by_type(params[:article_type], current_user)
134 unless @articles.empty?
135 #@title = Article.format_article_type(params[:article_type])
136 @show_archive_breadcrumb = true
137 @title = Article::format_article_type(@articles[0].article_type) #FIXME ??
138 render :action => "ac_index"
139 else
140 flash[:error] = "No articles for type '" << params[:article_type] << "'."
141 redirect_to articles_path
142 end
143 end
144
145 # Show all articles for a category
146 def find_by_category
147 #
148 # get category_id from :category
149 category = Category.find_by_slug(params[:category])
150 if category
151 #
152 @articles = Article.find_by_category(category.id, current_user)
153 unless @articles.empty?
154 @title = "'" << category.name << "' Articles"
155 @show_archive_breadcrumb = true
156 render :action => "ac_index"
157 else
158 flash[:error] = "No articles for category '" << category.name << "'."
159 redirect_to articles_path
160 end
161 else
162 flash[:error] = "'" << params[:category] << "' category not found."
163 redirect_to articles_path
164 end
165 end
166
167
168 # Show all articles for a project
169 def find_by_project
170 if @project
171 @articles = Article.find_by_project(@project.id, current_user)
172 unless @articles.empty?
173 @title = @project.title << " Articles"
174 @show_archive_breadcrumb = true
175 render :action => "ac_index"
176 else
177 flash[:error] = "No articles found for project '" << @project.title << "'."
178 redirect_to project_path(@project)
179 end
180 else
181 flash[:error] = "Project not found."
182 redirect_to root_path
183 end
184 end
185
186
187 # Show all articles for a user
188 def find_by_user
189 @user = User.find_by_slug(params[:user_slug])
190 if @user
191 @articles = Article.find_by_user(@user.id, current_user)
192 unless @articles.empty?
193 @title = @user.fullname << " Articles"
194 @show_archive_breadcrumb = true
195 render :action => "ac_index"
196 else
197 flash[:error] = "No articles found for '" << @user.fullname << "'."
198 redirect_to articles_path
199 end
200 else
201 flash[:error] = "User not found."
202 redirect_to articles_path
203 end
204 end
205
206 # Show all articles for sitename
207 def find_by_sitename
208 @articles = Article.find_by_sitename
209 unless @articles.empty?
210 @title = SITENAME << " Articles"
211 @show_archive_breadcrumb = true
212 render :action => "ac_index"
213 else
214 flash[:error] = "No articles found for Sitename."
215 redirect_to articles_path
216 end
217 end
218
219 # Show all articles for sitename
220 # FIXME can reuse find_by_sitename for this, by testing if params[:article_type] exists
221 def find_by_sitename_and_type
222 @articles = Article.find_by_sitename_and_type(params[:article_type])
223 unless @articles.empty?
224 @title = SITENAME << " '" << Article.format_article_type(params[:article_type]) << "' Articles"
225 @show_archive_breadcrumb = true
226 render :action => "ac_index"
227 else
228 flash[:error] = "No " << Article.format_article_type(params[:article_type]) << " articles found for " << SITENAME << "."
229 redirect_to articles_path
230 end
231 end
232
233 # Show all articles for a project
234 def find_by_project_and_type
235 @articles = Article.find_by_project_and_type(params[:article_type], @project.id, current_user)
236 unless @articles.empty?
237 @title = @project.title << " '" << Article.format_article_type(params[:article_type]) << "' Articles"
238 @show_archive_breadcrumb = true
239 render :action => "ac_index"
240 else
241 flash[:error] = "No '" << Article.format_article_type(params[:article_type]) << "' articles found for project '" << @project.name << "'."
242 redirect_to articles_path
243 end
244 end
245
246
247 protected
248
249 # Increase view count for a permalink page
250 def after_article
251 if @article
252 @article.views = @article.views + 1
253 @article.save!
254 end
255 end
256
257 end