##// END OF EJS Templates
If 'Display subprojects issues on main projects' is set to false:...
Jean-Philippe Lang -
r1283:89602004098d
parent child
Show More
@@ -1,426 +1,428
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 class ProjectsController < ApplicationController
18 class ProjectsController < ApplicationController
19 layout 'base'
19 layout 'base'
20 menu_item :overview
20 menu_item :overview
21 menu_item :activity, :only => :activity
21 menu_item :activity, :only => :activity
22 menu_item :roadmap, :only => :roadmap
22 menu_item :roadmap, :only => :roadmap
23 menu_item :files, :only => [:list_files, :add_file]
23 menu_item :files, :only => [:list_files, :add_file]
24 menu_item :settings, :only => :settings
24 menu_item :settings, :only => :settings
25 menu_item :issues, :only => [:changelog]
25 menu_item :issues, :only => [:changelog]
26
26
27 before_filter :find_project, :except => [ :index, :list, :add, :activity ]
27 before_filter :find_project, :except => [ :index, :list, :add, :activity ]
28 before_filter :find_optional_project, :only => :activity
28 before_filter :find_optional_project, :only => :activity
29 before_filter :authorize, :except => [ :index, :list, :add, :archive, :unarchive, :destroy, :activity ]
29 before_filter :authorize, :except => [ :index, :list, :add, :archive, :unarchive, :destroy, :activity ]
30 before_filter :require_admin, :only => [ :add, :archive, :unarchive, :destroy ]
30 before_filter :require_admin, :only => [ :add, :archive, :unarchive, :destroy ]
31 accept_key_auth :activity, :calendar
31 accept_key_auth :activity, :calendar
32
32
33 helper :sort
33 helper :sort
34 include SortHelper
34 include SortHelper
35 helper :custom_fields
35 helper :custom_fields
36 include CustomFieldsHelper
36 include CustomFieldsHelper
37 helper :ifpdf
37 helper :ifpdf
38 include IfpdfHelper
38 include IfpdfHelper
39 helper :issues
39 helper :issues
40 helper IssuesHelper
40 helper IssuesHelper
41 helper :queries
41 helper :queries
42 include QueriesHelper
42 include QueriesHelper
43 helper :repositories
43 helper :repositories
44 include RepositoriesHelper
44 include RepositoriesHelper
45 include ProjectsHelper
45 include ProjectsHelper
46
46
47 def index
47 def index
48 list
48 list
49 render :action => 'list' unless request.xhr?
49 render :action => 'list' unless request.xhr?
50 end
50 end
51
51
52 # Lists visible projects
52 # Lists visible projects
53 def list
53 def list
54 projects = Project.find :all,
54 projects = Project.find :all,
55 :conditions => Project.visible_by(User.current),
55 :conditions => Project.visible_by(User.current),
56 :include => :parent
56 :include => :parent
57 @project_tree = projects.group_by {|p| p.parent || p}
57 @project_tree = projects.group_by {|p| p.parent || p}
58 @project_tree.each_key {|p| @project_tree[p] -= [p]}
58 @project_tree.each_key {|p| @project_tree[p] -= [p]}
59 end
59 end
60
60
61 # Add a new project
61 # Add a new project
62 def add
62 def add
63 @custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
63 @custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
64 @trackers = Tracker.all
64 @trackers = Tracker.all
65 @root_projects = Project.find(:all,
65 @root_projects = Project.find(:all,
66 :conditions => "parent_id IS NULL AND status = #{Project::STATUS_ACTIVE}",
66 :conditions => "parent_id IS NULL AND status = #{Project::STATUS_ACTIVE}",
67 :order => 'name')
67 :order => 'name')
68 @project = Project.new(params[:project])
68 @project = Project.new(params[:project])
69 @project.enabled_module_names = Redmine::AccessControl.available_project_modules
69 @project.enabled_module_names = Redmine::AccessControl.available_project_modules
70 if request.get?
70 if request.get?
71 @custom_values = ProjectCustomField.find(:all, :order => "#{CustomField.table_name}.position").collect { |x| CustomValue.new(:custom_field => x, :customized => @project) }
71 @custom_values = ProjectCustomField.find(:all, :order => "#{CustomField.table_name}.position").collect { |x| CustomValue.new(:custom_field => x, :customized => @project) }
72 @project.trackers = Tracker.all
72 @project.trackers = Tracker.all
73 @project.is_public = Setting.default_projects_public?
73 @project.is_public = Setting.default_projects_public?
74 else
74 else
75 @project.custom_fields = CustomField.find(params[:custom_field_ids]) if params[:custom_field_ids]
75 @project.custom_fields = CustomField.find(params[:custom_field_ids]) if params[:custom_field_ids]
76 @custom_values = ProjectCustomField.find(:all, :order => "#{CustomField.table_name}.position").collect { |x| CustomValue.new(:custom_field => x, :customized => @project, :value => (params[:custom_fields] ? params["custom_fields"][x.id.to_s] : nil)) }
76 @custom_values = ProjectCustomField.find(:all, :order => "#{CustomField.table_name}.position").collect { |x| CustomValue.new(:custom_field => x, :customized => @project, :value => (params[:custom_fields] ? params["custom_fields"][x.id.to_s] : nil)) }
77 @project.custom_values = @custom_values
77 @project.custom_values = @custom_values
78 if @project.save
78 if @project.save
79 @project.enabled_module_names = params[:enabled_modules]
79 @project.enabled_module_names = params[:enabled_modules]
80 flash[:notice] = l(:notice_successful_create)
80 flash[:notice] = l(:notice_successful_create)
81 redirect_to :controller => 'admin', :action => 'projects'
81 redirect_to :controller => 'admin', :action => 'projects'
82 end
82 end
83 end
83 end
84 end
84 end
85
85
86 # Show @project
86 # Show @project
87 def show
87 def show
88 @custom_values = @project.custom_values.find(:all, :include => :custom_field, :order => "#{CustomField.table_name}.position")
88 @custom_values = @project.custom_values.find(:all, :include => :custom_field, :order => "#{CustomField.table_name}.position")
89 @members_by_role = @project.members.find(:all, :include => [:user, :role], :order => 'position').group_by {|m| m.role}
89 @members_by_role = @project.members.find(:all, :include => [:user, :role], :order => 'position').group_by {|m| m.role}
90 @subprojects = @project.active_children
90 @subprojects = @project.active_children
91 @news = @project.news.find(:all, :limit => 5, :include => [ :author, :project ], :order => "#{News.table_name}.created_on DESC")
91 @news = @project.news.find(:all, :limit => 5, :include => [ :author, :project ], :order => "#{News.table_name}.created_on DESC")
92 @trackers = @project.rolled_up_trackers
92 @trackers = @project.rolled_up_trackers
93
94 cond = @project.project_condition(Setting.display_subprojects_issues?)
93 Issue.visible_by(User.current) do
95 Issue.visible_by(User.current) do
94 @open_issues_by_tracker = Issue.count(:group => :tracker,
96 @open_issues_by_tracker = Issue.count(:group => :tracker,
95 :include => [:project, :status, :tracker],
97 :include => [:project, :status, :tracker],
96 :conditions => ["(#{Project.table_name}.id=? OR #{Project.table_name}.parent_id=?) and #{IssueStatus.table_name}.is_closed=?", @project.id, @project.id, false])
98 :conditions => ["(#{cond}) AND #{IssueStatus.table_name}.is_closed=?", false])
97 @total_issues_by_tracker = Issue.count(:group => :tracker,
99 @total_issues_by_tracker = Issue.count(:group => :tracker,
98 :include => [:project, :status, :tracker],
100 :include => [:project, :status, :tracker],
99 :conditions => ["#{Project.table_name}.id=? OR #{Project.table_name}.parent_id=?", @project.id, @project.id])
101 :conditions => cond)
100 end
102 end
101 TimeEntry.visible_by(User.current) do
103 TimeEntry.visible_by(User.current) do
102 @total_hours = TimeEntry.sum(:hours,
104 @total_hours = TimeEntry.sum(:hours,
103 :include => :project,
105 :include => :project,
104 :conditions => ["(#{Project.table_name}.id = ? OR #{Project.table_name}.parent_id = ?)", @project.id, @project.id]).to_f
106 :conditions => cond).to_f
105 end
107 end
106 @key = User.current.rss_key
108 @key = User.current.rss_key
107 end
109 end
108
110
109 def settings
111 def settings
110 @root_projects = Project.find(:all,
112 @root_projects = Project.find(:all,
111 :conditions => ["parent_id IS NULL AND status = #{Project::STATUS_ACTIVE} AND id <> ?", @project.id],
113 :conditions => ["parent_id IS NULL AND status = #{Project::STATUS_ACTIVE} AND id <> ?", @project.id],
112 :order => 'name')
114 :order => 'name')
113 @custom_fields = IssueCustomField.find(:all)
115 @custom_fields = IssueCustomField.find(:all)
114 @issue_category ||= IssueCategory.new
116 @issue_category ||= IssueCategory.new
115 @member ||= @project.members.new
117 @member ||= @project.members.new
116 @trackers = Tracker.all
118 @trackers = Tracker.all
117 @custom_values ||= ProjectCustomField.find(:all, :order => "#{CustomField.table_name}.position").collect { |x| @project.custom_values.find_by_custom_field_id(x.id) || CustomValue.new(:custom_field => x) }
119 @custom_values ||= ProjectCustomField.find(:all, :order => "#{CustomField.table_name}.position").collect { |x| @project.custom_values.find_by_custom_field_id(x.id) || CustomValue.new(:custom_field => x) }
118 @repository ||= @project.repository
120 @repository ||= @project.repository
119 @wiki ||= @project.wiki
121 @wiki ||= @project.wiki
120 end
122 end
121
123
122 # Edit @project
124 # Edit @project
123 def edit
125 def edit
124 if request.post?
126 if request.post?
125 if params[:custom_fields]
127 if params[:custom_fields]
126 @custom_values = ProjectCustomField.find(:all, :order => "#{CustomField.table_name}.position").collect { |x| CustomValue.new(:custom_field => x, :customized => @project, :value => params["custom_fields"][x.id.to_s]) }
128 @custom_values = ProjectCustomField.find(:all, :order => "#{CustomField.table_name}.position").collect { |x| CustomValue.new(:custom_field => x, :customized => @project, :value => params["custom_fields"][x.id.to_s]) }
127 @project.custom_values = @custom_values
129 @project.custom_values = @custom_values
128 end
130 end
129 @project.attributes = params[:project]
131 @project.attributes = params[:project]
130 if @project.save
132 if @project.save
131 flash[:notice] = l(:notice_successful_update)
133 flash[:notice] = l(:notice_successful_update)
132 redirect_to :action => 'settings', :id => @project
134 redirect_to :action => 'settings', :id => @project
133 else
135 else
134 settings
136 settings
135 render :action => 'settings'
137 render :action => 'settings'
136 end
138 end
137 end
139 end
138 end
140 end
139
141
140 def modules
142 def modules
141 @project.enabled_module_names = params[:enabled_modules]
143 @project.enabled_module_names = params[:enabled_modules]
142 redirect_to :action => 'settings', :id => @project, :tab => 'modules'
144 redirect_to :action => 'settings', :id => @project, :tab => 'modules'
143 end
145 end
144
146
145 def archive
147 def archive
146 @project.archive if request.post? && @project.active?
148 @project.archive if request.post? && @project.active?
147 redirect_to :controller => 'admin', :action => 'projects'
149 redirect_to :controller => 'admin', :action => 'projects'
148 end
150 end
149
151
150 def unarchive
152 def unarchive
151 @project.unarchive if request.post? && !@project.active?
153 @project.unarchive if request.post? && !@project.active?
152 redirect_to :controller => 'admin', :action => 'projects'
154 redirect_to :controller => 'admin', :action => 'projects'
153 end
155 end
154
156
155 # Delete @project
157 # Delete @project
156 def destroy
158 def destroy
157 @project_to_destroy = @project
159 @project_to_destroy = @project
158 if request.post? and params[:confirm]
160 if request.post? and params[:confirm]
159 @project_to_destroy.destroy
161 @project_to_destroy.destroy
160 redirect_to :controller => 'admin', :action => 'projects'
162 redirect_to :controller => 'admin', :action => 'projects'
161 end
163 end
162 # hide project in layout
164 # hide project in layout
163 @project = nil
165 @project = nil
164 end
166 end
165
167
166 # Add a new issue category to @project
168 # Add a new issue category to @project
167 def add_issue_category
169 def add_issue_category
168 @category = @project.issue_categories.build(params[:category])
170 @category = @project.issue_categories.build(params[:category])
169 if request.post? and @category.save
171 if request.post? and @category.save
170 respond_to do |format|
172 respond_to do |format|
171 format.html do
173 format.html do
172 flash[:notice] = l(:notice_successful_create)
174 flash[:notice] = l(:notice_successful_create)
173 redirect_to :action => 'settings', :tab => 'categories', :id => @project
175 redirect_to :action => 'settings', :tab => 'categories', :id => @project
174 end
176 end
175 format.js do
177 format.js do
176 # IE doesn't support the replace_html rjs method for select box options
178 # IE doesn't support the replace_html rjs method for select box options
177 render(:update) {|page| page.replace "issue_category_id",
179 render(:update) {|page| page.replace "issue_category_id",
178 content_tag('select', '<option></option>' + options_from_collection_for_select(@project.issue_categories, 'id', 'name', @category.id), :id => 'issue_category_id', :name => 'issue[category_id]')
180 content_tag('select', '<option></option>' + options_from_collection_for_select(@project.issue_categories, 'id', 'name', @category.id), :id => 'issue_category_id', :name => 'issue[category_id]')
179 }
181 }
180 end
182 end
181 end
183 end
182 end
184 end
183 end
185 end
184
186
185 # Add a new version to @project
187 # Add a new version to @project
186 def add_version
188 def add_version
187 @version = @project.versions.build(params[:version])
189 @version = @project.versions.build(params[:version])
188 if request.post? and @version.save
190 if request.post? and @version.save
189 flash[:notice] = l(:notice_successful_create)
191 flash[:notice] = l(:notice_successful_create)
190 redirect_to :action => 'settings', :tab => 'versions', :id => @project
192 redirect_to :action => 'settings', :tab => 'versions', :id => @project
191 end
193 end
192 end
194 end
193
195
194 def add_file
196 def add_file
195 if request.post?
197 if request.post?
196 @version = @project.versions.find_by_id(params[:version_id])
198 @version = @project.versions.find_by_id(params[:version_id])
197 attachments = attach_files(@version, params[:attachments])
199 attachments = attach_files(@version, params[:attachments])
198 Mailer.deliver_attachments_added(attachments) if !attachments.empty? && Setting.notified_events.include?('file_added')
200 Mailer.deliver_attachments_added(attachments) if !attachments.empty? && Setting.notified_events.include?('file_added')
199 redirect_to :controller => 'projects', :action => 'list_files', :id => @project
201 redirect_to :controller => 'projects', :action => 'list_files', :id => @project
200 end
202 end
201 @versions = @project.versions.sort
203 @versions = @project.versions.sort
202 end
204 end
203
205
204 def list_files
206 def list_files
205 @versions = @project.versions.sort.reverse
207 @versions = @project.versions.sort.reverse
206 end
208 end
207
209
208 # Show changelog for @project
210 # Show changelog for @project
209 def changelog
211 def changelog
210 @trackers = @project.trackers.find(:all, :conditions => ["is_in_chlog=?", true], :order => 'position')
212 @trackers = @project.trackers.find(:all, :conditions => ["is_in_chlog=?", true], :order => 'position')
211 retrieve_selected_tracker_ids(@trackers)
213 retrieve_selected_tracker_ids(@trackers)
212 @versions = @project.versions.sort
214 @versions = @project.versions.sort
213 end
215 end
214
216
215 def roadmap
217 def roadmap
216 @trackers = @project.trackers.find(:all, :conditions => ["is_in_roadmap=?", true])
218 @trackers = @project.trackers.find(:all, :conditions => ["is_in_roadmap=?", true])
217 retrieve_selected_tracker_ids(@trackers)
219 retrieve_selected_tracker_ids(@trackers)
218 @versions = @project.versions.sort
220 @versions = @project.versions.sort
219 @versions = @versions.select {|v| !v.completed? } unless params[:completed]
221 @versions = @versions.select {|v| !v.completed? } unless params[:completed]
220 end
222 end
221
223
222 def activity
224 def activity
223 @days = Setting.activity_days_default.to_i
225 @days = Setting.activity_days_default.to_i
224
226
225 if params[:from]
227 if params[:from]
226 begin; @date_to = params[:from].to_date; rescue; end
228 begin; @date_to = params[:from].to_date; rescue; end
227 end
229 end
228
230
229 @date_to ||= Date.today + 1
231 @date_to ||= Date.today + 1
230 @date_from = @date_to - @days
232 @date_from = @date_to - @days
231
233
232 @event_types = %w(issues news files documents changesets wiki_pages messages)
234 @event_types = %w(issues news files documents changesets wiki_pages messages)
233 if @project
235 if @project
234 @event_types.delete('wiki_pages') unless @project.wiki
236 @event_types.delete('wiki_pages') unless @project.wiki
235 @event_types.delete('changesets') unless @project.repository
237 @event_types.delete('changesets') unless @project.repository
236 @event_types.delete('messages') unless @project.boards.any?
238 @event_types.delete('messages') unless @project.boards.any?
237 # only show what the user is allowed to view
239 # only show what the user is allowed to view
238 @event_types = @event_types.select {|o| User.current.allowed_to?("view_#{o}".to_sym, @project)}
240 @event_types = @event_types.select {|o| User.current.allowed_to?("view_#{o}".to_sym, @project)}
239 @with_subprojects = params[:with_subprojects].nil? ? Setting.display_subprojects_issues? : (params[:with_subprojects] == '1')
241 @with_subprojects = params[:with_subprojects].nil? ? Setting.display_subprojects_issues? : (params[:with_subprojects] == '1')
240 end
242 end
241 @scope = @event_types.select {|t| params["show_#{t}"]}
243 @scope = @event_types.select {|t| params["show_#{t}"]}
242 # default events if none is specified in parameters
244 # default events if none is specified in parameters
243 @scope = (@event_types - %w(wiki_pages messages))if @scope.empty?
245 @scope = (@event_types - %w(wiki_pages messages))if @scope.empty?
244
246
245 @events = []
247 @events = []
246
248
247 if @scope.include?('issues')
249 if @scope.include?('issues')
248 cond = ARCondition.new(Project.allowed_to_condition(User.current, :view_issues, :project => @project, :with_subprojects => @with_subprojects))
250 cond = ARCondition.new(Project.allowed_to_condition(User.current, :view_issues, :project => @project, :with_subprojects => @with_subprojects))
249 cond.add(["#{Issue.table_name}.created_on BETWEEN ? AND ?", @date_from, @date_to])
251 cond.add(["#{Issue.table_name}.created_on BETWEEN ? AND ?", @date_from, @date_to])
250 @events += Issue.find(:all, :include => [:project, :author, :tracker], :conditions => cond.conditions)
252 @events += Issue.find(:all, :include => [:project, :author, :tracker], :conditions => cond.conditions)
251
253
252 cond = ARCondition.new(Project.allowed_to_condition(User.current, :view_issues, :project => @project, :with_subprojects => @with_subprojects))
254 cond = ARCondition.new(Project.allowed_to_condition(User.current, :view_issues, :project => @project, :with_subprojects => @with_subprojects))
253 cond.add(["#{Journal.table_name}.journalized_type = 'Issue' AND #{JournalDetail.table_name}.prop_key = 'status_id' AND #{Journal.table_name}.created_on BETWEEN ? AND ?", @date_from, @date_to])
255 cond.add(["#{Journal.table_name}.journalized_type = 'Issue' AND #{JournalDetail.table_name}.prop_key = 'status_id' AND #{Journal.table_name}.created_on BETWEEN ? AND ?", @date_from, @date_to])
254 @events += Journal.find(:all, :include => [{:issue => :project}, :details, :user], :conditions => cond.conditions)
256 @events += Journal.find(:all, :include => [{:issue => :project}, :details, :user], :conditions => cond.conditions)
255 end
257 end
256
258
257 if @scope.include?('news')
259 if @scope.include?('news')
258 cond = ARCondition.new(Project.allowed_to_condition(User.current, :view_news, :project => @project, :with_subprojects => @with_subprojects))
260 cond = ARCondition.new(Project.allowed_to_condition(User.current, :view_news, :project => @project, :with_subprojects => @with_subprojects))
259 cond.add(["#{News.table_name}.created_on BETWEEN ? AND ?", @date_from, @date_to])
261 cond.add(["#{News.table_name}.created_on BETWEEN ? AND ?", @date_from, @date_to])
260 @events += News.find(:all, :include => [:project, :author], :conditions => cond.conditions)
262 @events += News.find(:all, :include => [:project, :author], :conditions => cond.conditions)
261 end
263 end
262
264
263 if @scope.include?('files')
265 if @scope.include?('files')
264 cond = ARCondition.new(Project.allowed_to_condition(User.current, :view_files, :project => @project, :with_subprojects => @with_subprojects))
266 cond = ARCondition.new(Project.allowed_to_condition(User.current, :view_files, :project => @project, :with_subprojects => @with_subprojects))
265 cond.add(["#{Attachment.table_name}.created_on BETWEEN ? AND ?", @date_from, @date_to])
267 cond.add(["#{Attachment.table_name}.created_on BETWEEN ? AND ?", @date_from, @date_to])
266 @events += Attachment.find(:all, :select => "#{Attachment.table_name}.*",
268 @events += Attachment.find(:all, :select => "#{Attachment.table_name}.*",
267 :joins => "LEFT JOIN #{Version.table_name} ON #{Attachment.table_name}.container_type='Version' AND #{Version.table_name}.id = #{Attachment.table_name}.container_id " +
269 :joins => "LEFT JOIN #{Version.table_name} ON #{Attachment.table_name}.container_type='Version' AND #{Version.table_name}.id = #{Attachment.table_name}.container_id " +
268 "LEFT JOIN #{Project.table_name} ON #{Version.table_name}.project_id = #{Project.table_name}.id",
270 "LEFT JOIN #{Project.table_name} ON #{Version.table_name}.project_id = #{Project.table_name}.id",
269 :conditions => cond.conditions)
271 :conditions => cond.conditions)
270 end
272 end
271
273
272 if @scope.include?('documents')
274 if @scope.include?('documents')
273 cond = ARCondition.new(Project.allowed_to_condition(User.current, :view_documents, :project => @project, :with_subprojects => @with_subprojects))
275 cond = ARCondition.new(Project.allowed_to_condition(User.current, :view_documents, :project => @project, :with_subprojects => @with_subprojects))
274 cond.add(["#{Document.table_name}.created_on BETWEEN ? AND ?", @date_from, @date_to])
276 cond.add(["#{Document.table_name}.created_on BETWEEN ? AND ?", @date_from, @date_to])
275 @events += Document.find(:all, :include => :project, :conditions => cond.conditions)
277 @events += Document.find(:all, :include => :project, :conditions => cond.conditions)
276
278
277 cond = ARCondition.new(Project.allowed_to_condition(User.current, :view_documents, :project => @project, :with_subprojects => @with_subprojects))
279 cond = ARCondition.new(Project.allowed_to_condition(User.current, :view_documents, :project => @project, :with_subprojects => @with_subprojects))
278 cond.add(["#{Attachment.table_name}.created_on BETWEEN ? AND ?", @date_from, @date_to])
280 cond.add(["#{Attachment.table_name}.created_on BETWEEN ? AND ?", @date_from, @date_to])
279 @events += Attachment.find(:all, :select => "#{Attachment.table_name}.*",
281 @events += Attachment.find(:all, :select => "#{Attachment.table_name}.*",
280 :joins => "LEFT JOIN #{Document.table_name} ON #{Attachment.table_name}.container_type='Document' AND #{Document.table_name}.id = #{Attachment.table_name}.container_id " +
282 :joins => "LEFT JOIN #{Document.table_name} ON #{Attachment.table_name}.container_type='Document' AND #{Document.table_name}.id = #{Attachment.table_name}.container_id " +
281 "LEFT JOIN #{Project.table_name} ON #{Document.table_name}.project_id = #{Project.table_name}.id",
283 "LEFT JOIN #{Project.table_name} ON #{Document.table_name}.project_id = #{Project.table_name}.id",
282 :conditions => cond.conditions)
284 :conditions => cond.conditions)
283 end
285 end
284
286
285 if @scope.include?('wiki_pages')
287 if @scope.include?('wiki_pages')
286 select = "#{WikiContent.versioned_table_name}.updated_on, #{WikiContent.versioned_table_name}.comments, " +
288 select = "#{WikiContent.versioned_table_name}.updated_on, #{WikiContent.versioned_table_name}.comments, " +
287 "#{WikiContent.versioned_table_name}.#{WikiContent.version_column}, #{WikiPage.table_name}.title, " +
289 "#{WikiContent.versioned_table_name}.#{WikiContent.version_column}, #{WikiPage.table_name}.title, " +
288 "#{WikiContent.versioned_table_name}.page_id, #{WikiContent.versioned_table_name}.author_id, " +
290 "#{WikiContent.versioned_table_name}.page_id, #{WikiContent.versioned_table_name}.author_id, " +
289 "#{WikiContent.versioned_table_name}.id"
291 "#{WikiContent.versioned_table_name}.id"
290 joins = "LEFT JOIN #{WikiPage.table_name} ON #{WikiPage.table_name}.id = #{WikiContent.versioned_table_name}.page_id " +
292 joins = "LEFT JOIN #{WikiPage.table_name} ON #{WikiPage.table_name}.id = #{WikiContent.versioned_table_name}.page_id " +
291 "LEFT JOIN #{Wiki.table_name} ON #{Wiki.table_name}.id = #{WikiPage.table_name}.wiki_id " +
293 "LEFT JOIN #{Wiki.table_name} ON #{Wiki.table_name}.id = #{WikiPage.table_name}.wiki_id " +
292 "LEFT JOIN #{Project.table_name} ON #{Project.table_name}.id = #{Wiki.table_name}.project_id"
294 "LEFT JOIN #{Project.table_name} ON #{Project.table_name}.id = #{Wiki.table_name}.project_id"
293
295
294 cond = ARCondition.new(Project.allowed_to_condition(User.current, :view_wiki_pages, :project => @project, :with_subprojects => @with_subprojects))
296 cond = ARCondition.new(Project.allowed_to_condition(User.current, :view_wiki_pages, :project => @project, :with_subprojects => @with_subprojects))
295 cond.add(["#{WikiContent.versioned_table_name}.updated_on BETWEEN ? AND ?", @date_from, @date_to])
297 cond.add(["#{WikiContent.versioned_table_name}.updated_on BETWEEN ? AND ?", @date_from, @date_to])
296 @events += WikiContent.versioned_class.find(:all, :select => select, :joins => joins, :conditions => cond.conditions)
298 @events += WikiContent.versioned_class.find(:all, :select => select, :joins => joins, :conditions => cond.conditions)
297 end
299 end
298
300
299 if @scope.include?('changesets')
301 if @scope.include?('changesets')
300 cond = ARCondition.new(Project.allowed_to_condition(User.current, :view_changesets, :project => @project, :with_subprojects => @with_subprojects))
302 cond = ARCondition.new(Project.allowed_to_condition(User.current, :view_changesets, :project => @project, :with_subprojects => @with_subprojects))
301 cond.add(["#{Changeset.table_name}.committed_on BETWEEN ? AND ?", @date_from, @date_to])
303 cond.add(["#{Changeset.table_name}.committed_on BETWEEN ? AND ?", @date_from, @date_to])
302 @events += Changeset.find(:all, :include => {:repository => :project}, :conditions => cond.conditions)
304 @events += Changeset.find(:all, :include => {:repository => :project}, :conditions => cond.conditions)
303 end
305 end
304
306
305 if @scope.include?('messages')
307 if @scope.include?('messages')
306 cond = ARCondition.new(Project.allowed_to_condition(User.current, :view_messages, :project => @project, :with_subprojects => @with_subprojects))
308 cond = ARCondition.new(Project.allowed_to_condition(User.current, :view_messages, :project => @project, :with_subprojects => @with_subprojects))
307 cond.add(["#{Message.table_name}.created_on BETWEEN ? AND ?", @date_from, @date_to])
309 cond.add(["#{Message.table_name}.created_on BETWEEN ? AND ?", @date_from, @date_to])
308 @events += Message.find(:all, :include => [{:board => :project}, :author], :conditions => cond.conditions)
310 @events += Message.find(:all, :include => [{:board => :project}, :author], :conditions => cond.conditions)
309 end
311 end
310
312
311 @events_by_day = @events.group_by(&:event_date)
313 @events_by_day = @events.group_by(&:event_date)
312
314
313 respond_to do |format|
315 respond_to do |format|
314 format.html { render :layout => false if request.xhr? }
316 format.html { render :layout => false if request.xhr? }
315 format.atom { render_feed(@events, :title => "#{@project || Setting.app_title}: #{l(:label_activity)}") }
317 format.atom { render_feed(@events, :title => "#{@project || Setting.app_title}: #{l(:label_activity)}") }
316 end
318 end
317 end
319 end
318
320
319 def calendar
321 def calendar
320 @trackers = @project.rolled_up_trackers
322 @trackers = @project.rolled_up_trackers
321 retrieve_selected_tracker_ids(@trackers)
323 retrieve_selected_tracker_ids(@trackers)
322
324
323 if params[:year] and params[:year].to_i > 1900
325 if params[:year] and params[:year].to_i > 1900
324 @year = params[:year].to_i
326 @year = params[:year].to_i
325 if params[:month] and params[:month].to_i > 0 and params[:month].to_i < 13
327 if params[:month] and params[:month].to_i > 0 and params[:month].to_i < 13
326 @month = params[:month].to_i
328 @month = params[:month].to_i
327 end
329 end
328 end
330 end
329 @year ||= Date.today.year
331 @year ||= Date.today.year
330 @month ||= Date.today.month
332 @month ||= Date.today.month
331 @calendar = Redmine::Helpers::Calendar.new(Date.civil(@year, @month, 1), current_language, :month)
333 @calendar = Redmine::Helpers::Calendar.new(Date.civil(@year, @month, 1), current_language, :month)
332 @with_subprojects = params[:with_subprojects].nil? ? Setting.display_subprojects_issues? : (params[:with_subprojects] == '1')
334 @with_subprojects = params[:with_subprojects].nil? ? Setting.display_subprojects_issues? : (params[:with_subprojects] == '1')
333 events = []
335 events = []
334 @project.issues_with_subprojects(@with_subprojects) do
336 @project.issues_with_subprojects(@with_subprojects) do
335 events += Issue.find(:all,
337 events += Issue.find(:all,
336 :include => [:tracker, :status, :assigned_to, :priority, :project],
338 :include => [:tracker, :status, :assigned_to, :priority, :project],
337 :conditions => ["((start_date BETWEEN ? AND ?) OR (due_date BETWEEN ? AND ?)) AND #{Issue.table_name}.tracker_id IN (#{@selected_tracker_ids.join(',')})", @calendar.startdt, @calendar.enddt, @calendar.startdt, @calendar.enddt]
339 :conditions => ["((start_date BETWEEN ? AND ?) OR (due_date BETWEEN ? AND ?)) AND #{Issue.table_name}.tracker_id IN (#{@selected_tracker_ids.join(',')})", @calendar.startdt, @calendar.enddt, @calendar.startdt, @calendar.enddt]
338 ) unless @selected_tracker_ids.empty?
340 ) unless @selected_tracker_ids.empty?
339 end
341 end
340 events += @project.versions.find(:all, :conditions => ["effective_date BETWEEN ? AND ?", @calendar.startdt, @calendar.enddt])
342 events += @project.versions.find(:all, :conditions => ["effective_date BETWEEN ? AND ?", @calendar.startdt, @calendar.enddt])
341 @calendar.events = events
343 @calendar.events = events
342
344
343 render :layout => false if request.xhr?
345 render :layout => false if request.xhr?
344 end
346 end
345
347
346 def gantt
348 def gantt
347 @trackers = @project.rolled_up_trackers
349 @trackers = @project.rolled_up_trackers
348 retrieve_selected_tracker_ids(@trackers)
350 retrieve_selected_tracker_ids(@trackers)
349
351
350 if params[:year] and params[:year].to_i >0
352 if params[:year] and params[:year].to_i >0
351 @year_from = params[:year].to_i
353 @year_from = params[:year].to_i
352 if params[:month] and params[:month].to_i >=1 and params[:month].to_i <= 12
354 if params[:month] and params[:month].to_i >=1 and params[:month].to_i <= 12
353 @month_from = params[:month].to_i
355 @month_from = params[:month].to_i
354 else
356 else
355 @month_from = 1
357 @month_from = 1
356 end
358 end
357 else
359 else
358 @month_from ||= Date.today.month
360 @month_from ||= Date.today.month
359 @year_from ||= Date.today.year
361 @year_from ||= Date.today.year
360 end
362 end
361
363
362 zoom = (params[:zoom] || User.current.pref[:gantt_zoom]).to_i
364 zoom = (params[:zoom] || User.current.pref[:gantt_zoom]).to_i
363 @zoom = (zoom > 0 && zoom < 5) ? zoom : 2
365 @zoom = (zoom > 0 && zoom < 5) ? zoom : 2
364 months = (params[:months] || User.current.pref[:gantt_months]).to_i
366 months = (params[:months] || User.current.pref[:gantt_months]).to_i
365 @months = (months > 0 && months < 25) ? months : 6
367 @months = (months > 0 && months < 25) ? months : 6
366
368
367 # Save gantt paramters as user preference (zoom and months count)
369 # Save gantt paramters as user preference (zoom and months count)
368 if (User.current.logged? && (@zoom != User.current.pref[:gantt_zoom] || @months != User.current.pref[:gantt_months]))
370 if (User.current.logged? && (@zoom != User.current.pref[:gantt_zoom] || @months != User.current.pref[:gantt_months]))
369 User.current.pref[:gantt_zoom], User.current.pref[:gantt_months] = @zoom, @months
371 User.current.pref[:gantt_zoom], User.current.pref[:gantt_months] = @zoom, @months
370 User.current.preference.save
372 User.current.preference.save
371 end
373 end
372
374
373 @date_from = Date.civil(@year_from, @month_from, 1)
375 @date_from = Date.civil(@year_from, @month_from, 1)
374 @date_to = (@date_from >> @months) - 1
376 @date_to = (@date_from >> @months) - 1
375 @with_subprojects = params[:with_subprojects].nil? ? Setting.display_subprojects_issues? : (params[:with_subprojects] == '1')
377 @with_subprojects = params[:with_subprojects].nil? ? Setting.display_subprojects_issues? : (params[:with_subprojects] == '1')
376
378
377 @events = []
379 @events = []
378 @project.issues_with_subprojects(@with_subprojects) do
380 @project.issues_with_subprojects(@with_subprojects) do
379 @events += Issue.find(:all,
381 @events += Issue.find(:all,
380 :order => "start_date, due_date",
382 :order => "start_date, due_date",
381 :include => [:tracker, :status, :assigned_to, :priority, :project],
383 :include => [:tracker, :status, :assigned_to, :priority, :project],
382 :conditions => ["(((start_date>=? and start_date<=?) or (due_date>=? and due_date<=?) or (start_date<? and due_date>?)) and start_date is not null and due_date is not null and #{Issue.table_name}.tracker_id in (#{@selected_tracker_ids.join(',')}))", @date_from, @date_to, @date_from, @date_to, @date_from, @date_to]
384 :conditions => ["(((start_date>=? and start_date<=?) or (due_date>=? and due_date<=?) or (start_date<? and due_date>?)) and start_date is not null and due_date is not null and #{Issue.table_name}.tracker_id in (#{@selected_tracker_ids.join(',')}))", @date_from, @date_to, @date_from, @date_to, @date_from, @date_to]
383 ) unless @selected_tracker_ids.empty?
385 ) unless @selected_tracker_ids.empty?
384 end
386 end
385 @events += @project.versions.find(:all, :conditions => ["effective_date BETWEEN ? AND ?", @date_from, @date_to])
387 @events += @project.versions.find(:all, :conditions => ["effective_date BETWEEN ? AND ?", @date_from, @date_to])
386 @events.sort! {|x,y| x.start_date <=> y.start_date }
388 @events.sort! {|x,y| x.start_date <=> y.start_date }
387
389
388 if params[:format]=='pdf'
390 if params[:format]=='pdf'
389 @options_for_rfpdf ||= {}
391 @options_for_rfpdf ||= {}
390 @options_for_rfpdf[:file_name] = "#{@project.identifier}-gantt.pdf"
392 @options_for_rfpdf[:file_name] = "#{@project.identifier}-gantt.pdf"
391 render :template => "projects/gantt.rfpdf", :layout => false
393 render :template => "projects/gantt.rfpdf", :layout => false
392 elsif params[:format]=='png' && respond_to?('gantt_image')
394 elsif params[:format]=='png' && respond_to?('gantt_image')
393 image = gantt_image(@events, @date_from, @months, @zoom)
395 image = gantt_image(@events, @date_from, @months, @zoom)
394 image.format = 'PNG'
396 image.format = 'PNG'
395 send_data(image.to_blob, :disposition => 'inline', :type => 'image/png', :filename => "#{@project.identifier}-gantt.png")
397 send_data(image.to_blob, :disposition => 'inline', :type => 'image/png', :filename => "#{@project.identifier}-gantt.png")
396 else
398 else
397 render :template => "projects/gantt.rhtml"
399 render :template => "projects/gantt.rhtml"
398 end
400 end
399 end
401 end
400
402
401 private
403 private
402 # Find project of id params[:id]
404 # Find project of id params[:id]
403 # if not found, redirect to project list
405 # if not found, redirect to project list
404 # Used as a before_filter
406 # Used as a before_filter
405 def find_project
407 def find_project
406 @project = Project.find(params[:id])
408 @project = Project.find(params[:id])
407 rescue ActiveRecord::RecordNotFound
409 rescue ActiveRecord::RecordNotFound
408 render_404
410 render_404
409 end
411 end
410
412
411 def find_optional_project
413 def find_optional_project
412 return true unless params[:id]
414 return true unless params[:id]
413 @project = Project.find(params[:id])
415 @project = Project.find(params[:id])
414 authorize
416 authorize
415 rescue ActiveRecord::RecordNotFound
417 rescue ActiveRecord::RecordNotFound
416 render_404
418 render_404
417 end
419 end
418
420
419 def retrieve_selected_tracker_ids(selectable_trackers)
421 def retrieve_selected_tracker_ids(selectable_trackers)
420 if ids = params[:tracker_ids]
422 if ids = params[:tracker_ids]
421 @selected_tracker_ids = (ids.is_a? Array) ? ids.collect { |id| id.to_i.to_s } : ids.split('/').collect { |id| id.to_i.to_s }
423 @selected_tracker_ids = (ids.is_a? Array) ? ids.collect { |id| id.to_i.to_s } : ids.split('/').collect { |id| id.to_i.to_s }
422 else
424 else
423 @selected_tracker_ids = selectable_trackers.collect {|t| t.id.to_s }
425 @selected_tracker_ids = selectable_trackers.collect {|t| t.id.to_s }
424 end
426 end
425 end
427 end
426 end
428 end
@@ -1,241 +1,241
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 class TimelogController < ApplicationController
18 class TimelogController < ApplicationController
19 layout 'base'
19 layout 'base'
20 menu_item :issues
20 menu_item :issues
21 before_filter :find_project, :authorize
21 before_filter :find_project, :authorize
22
22
23 verify :method => :post, :only => :destroy, :redirect_to => { :action => :details }
23 verify :method => :post, :only => :destroy, :redirect_to => { :action => :details }
24
24
25 helper :sort
25 helper :sort
26 include SortHelper
26 include SortHelper
27 helper :issues
27 helper :issues
28 include TimelogHelper
28 include TimelogHelper
29
29
30 def report
30 def report
31 @available_criterias = { 'project' => {:sql => "#{TimeEntry.table_name}.project_id",
31 @available_criterias = { 'project' => {:sql => "#{TimeEntry.table_name}.project_id",
32 :klass => Project,
32 :klass => Project,
33 :label => :label_project},
33 :label => :label_project},
34 'version' => {:sql => "#{Issue.table_name}.fixed_version_id",
34 'version' => {:sql => "#{Issue.table_name}.fixed_version_id",
35 :klass => Version,
35 :klass => Version,
36 :label => :label_version},
36 :label => :label_version},
37 'category' => {:sql => "#{Issue.table_name}.category_id",
37 'category' => {:sql => "#{Issue.table_name}.category_id",
38 :klass => IssueCategory,
38 :klass => IssueCategory,
39 :label => :field_category},
39 :label => :field_category},
40 'member' => {:sql => "#{TimeEntry.table_name}.user_id",
40 'member' => {:sql => "#{TimeEntry.table_name}.user_id",
41 :klass => User,
41 :klass => User,
42 :label => :label_member},
42 :label => :label_member},
43 'tracker' => {:sql => "#{Issue.table_name}.tracker_id",
43 'tracker' => {:sql => "#{Issue.table_name}.tracker_id",
44 :klass => Tracker,
44 :klass => Tracker,
45 :label => :label_tracker},
45 :label => :label_tracker},
46 'activity' => {:sql => "#{TimeEntry.table_name}.activity_id",
46 'activity' => {:sql => "#{TimeEntry.table_name}.activity_id",
47 :klass => Enumeration,
47 :klass => Enumeration,
48 :label => :label_activity}
48 :label => :label_activity}
49 }
49 }
50
50
51 @criterias = params[:criterias] || []
51 @criterias = params[:criterias] || []
52 @criterias = @criterias.select{|criteria| @available_criterias.has_key? criteria}
52 @criterias = @criterias.select{|criteria| @available_criterias.has_key? criteria}
53 @criterias.uniq!
53 @criterias.uniq!
54 @criterias = @criterias[0,3]
54 @criterias = @criterias[0,3]
55
55
56 @columns = (params[:period] && %w(year month week).include?(params[:period])) ? params[:period] : 'month'
56 @columns = (params[:period] && %w(year month week).include?(params[:period])) ? params[:period] : 'month'
57
57
58 if params[:date_from]
58 if params[:date_from]
59 begin; @date_from = params[:date_from].to_date; rescue; end
59 begin; @date_from = params[:date_from].to_date; rescue; end
60 end
60 end
61 if params[:date_to]
61 if params[:date_to]
62 begin; @date_to = params[:date_to].to_date; rescue; end
62 begin; @date_to = params[:date_to].to_date; rescue; end
63 end
63 end
64 @date_from ||= Date.civil(Date.today.year, 1, 1)
64 @date_from ||= Date.civil(Date.today.year, 1, 1)
65 @date_to ||= (Date.civil(Date.today.year, Date.today.month, 1) >> 1) - 1
65 @date_to ||= (Date.civil(Date.today.year, Date.today.month, 1) >> 1) - 1
66
66
67 unless @criterias.empty?
67 unless @criterias.empty?
68 sql_select = @criterias.collect{|criteria| @available_criterias[criteria][:sql] + " AS " + criteria}.join(', ')
68 sql_select = @criterias.collect{|criteria| @available_criterias[criteria][:sql] + " AS " + criteria}.join(', ')
69 sql_group_by = @criterias.collect{|criteria| @available_criterias[criteria][:sql]}.join(', ')
69 sql_group_by = @criterias.collect{|criteria| @available_criterias[criteria][:sql]}.join(', ')
70
70
71 sql = "SELECT #{sql_select}, tyear, tmonth, tweek, SUM(hours) AS hours"
71 sql = "SELECT #{sql_select}, tyear, tmonth, tweek, SUM(hours) AS hours"
72 sql << " FROM #{TimeEntry.table_name}"
72 sql << " FROM #{TimeEntry.table_name}"
73 sql << " LEFT JOIN #{Issue.table_name} ON #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id"
73 sql << " LEFT JOIN #{Issue.table_name} ON #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id"
74 sql << " LEFT JOIN #{Project.table_name} ON #{TimeEntry.table_name}.project_id = #{Project.table_name}.id"
74 sql << " LEFT JOIN #{Project.table_name} ON #{TimeEntry.table_name}.project_id = #{Project.table_name}.id"
75 sql << " WHERE (#{Project.table_name}.id = %s OR #{Project.table_name}.parent_id = %s)" % [@project.id, @project.id]
75 sql << " WHERE (%s)" % @project.project_condition(Setting.display_subprojects_issues?)
76 sql << " AND (%s)" % Project.allowed_to_condition(User.current, :view_time_entries)
76 sql << " AND (%s)" % Project.allowed_to_condition(User.current, :view_time_entries)
77 sql << " AND spent_on BETWEEN '%s' AND '%s'" % [ActiveRecord::Base.connection.quoted_date(@date_from.to_time), ActiveRecord::Base.connection.quoted_date(@date_to.to_time)]
77 sql << " AND spent_on BETWEEN '%s' AND '%s'" % [ActiveRecord::Base.connection.quoted_date(@date_from.to_time), ActiveRecord::Base.connection.quoted_date(@date_to.to_time)]
78 sql << " GROUP BY #{sql_group_by}, tyear, tmonth, tweek"
78 sql << " GROUP BY #{sql_group_by}, tyear, tmonth, tweek"
79
79
80 @hours = ActiveRecord::Base.connection.select_all(sql)
80 @hours = ActiveRecord::Base.connection.select_all(sql)
81
81
82 @hours.each do |row|
82 @hours.each do |row|
83 case @columns
83 case @columns
84 when 'year'
84 when 'year'
85 row['year'] = row['tyear']
85 row['year'] = row['tyear']
86 when 'month'
86 when 'month'
87 row['month'] = "#{row['tyear']}-#{row['tmonth']}"
87 row['month'] = "#{row['tyear']}-#{row['tmonth']}"
88 when 'week'
88 when 'week'
89 row['week'] = "#{row['tyear']}-#{row['tweek']}"
89 row['week'] = "#{row['tyear']}-#{row['tweek']}"
90 end
90 end
91 end
91 end
92
92
93 @total_hours = @hours.inject(0) {|s,k| s = s + k['hours'].to_f}
93 @total_hours = @hours.inject(0) {|s,k| s = s + k['hours'].to_f}
94 end
94 end
95
95
96 @periods = []
96 @periods = []
97 date_from = @date_from
97 date_from = @date_from
98 # 100 columns max
98 # 100 columns max
99 while date_from < @date_to && @periods.length < 100
99 while date_from < @date_to && @periods.length < 100
100 case @columns
100 case @columns
101 when 'year'
101 when 'year'
102 @periods << "#{date_from.year}"
102 @periods << "#{date_from.year}"
103 date_from = date_from >> 12
103 date_from = date_from >> 12
104 when 'month'
104 when 'month'
105 @periods << "#{date_from.year}-#{date_from.month}"
105 @periods << "#{date_from.year}-#{date_from.month}"
106 date_from = date_from >> 1
106 date_from = date_from >> 1
107 when 'week'
107 when 'week'
108 @periods << "#{date_from.year}-#{date_from.cweek}"
108 @periods << "#{date_from.year}-#{date_from.cweek}"
109 date_from = date_from + 7
109 date_from = date_from + 7
110 end
110 end
111 end
111 end
112
112
113 render :layout => false if request.xhr?
113 render :layout => false if request.xhr?
114 end
114 end
115
115
116 def details
116 def details
117 sort_init 'spent_on', 'desc'
117 sort_init 'spent_on', 'desc'
118 sort_update
118 sort_update
119
119
120 @free_period = false
120 @free_period = false
121 @from, @to = nil, nil
121 @from, @to = nil, nil
122
122
123 if params[:period_type] == '1' || (params[:period_type].nil? && !params[:period].nil?)
123 if params[:period_type] == '1' || (params[:period_type].nil? && !params[:period].nil?)
124 case params[:period].to_s
124 case params[:period].to_s
125 when 'today'
125 when 'today'
126 @from = @to = Date.today
126 @from = @to = Date.today
127 when 'yesterday'
127 when 'yesterday'
128 @from = @to = Date.today - 1
128 @from = @to = Date.today - 1
129 when 'current_week'
129 when 'current_week'
130 @from = Date.today - (Date.today.cwday - 1)%7
130 @from = Date.today - (Date.today.cwday - 1)%7
131 @to = @from + 6
131 @to = @from + 6
132 when 'last_week'
132 when 'last_week'
133 @from = Date.today - 7 - (Date.today.cwday - 1)%7
133 @from = Date.today - 7 - (Date.today.cwday - 1)%7
134 @to = @from + 6
134 @to = @from + 6
135 when '7_days'
135 when '7_days'
136 @from = Date.today - 7
136 @from = Date.today - 7
137 @to = Date.today
137 @to = Date.today
138 when 'current_month'
138 when 'current_month'
139 @from = Date.civil(Date.today.year, Date.today.month, 1)
139 @from = Date.civil(Date.today.year, Date.today.month, 1)
140 @to = (@from >> 1) - 1
140 @to = (@from >> 1) - 1
141 when 'last_month'
141 when 'last_month'
142 @from = Date.civil(Date.today.year, Date.today.month, 1) << 1
142 @from = Date.civil(Date.today.year, Date.today.month, 1) << 1
143 @to = (@from >> 1) - 1
143 @to = (@from >> 1) - 1
144 when '30_days'
144 when '30_days'
145 @from = Date.today - 30
145 @from = Date.today - 30
146 @to = Date.today
146 @to = Date.today
147 when 'current_year'
147 when 'current_year'
148 @from = Date.civil(Date.today.year, 1, 1)
148 @from = Date.civil(Date.today.year, 1, 1)
149 @to = Date.civil(Date.today.year, 12, 31)
149 @to = Date.civil(Date.today.year, 12, 31)
150 end
150 end
151 elsif params[:period_type] == '2' || (params[:period_type].nil? && (!params[:from].nil? || !params[:to].nil?))
151 elsif params[:period_type] == '2' || (params[:period_type].nil? && (!params[:from].nil? || !params[:to].nil?))
152 begin; @from = params[:from].to_s.to_date unless params[:from].blank?; rescue; end
152 begin; @from = params[:from].to_s.to_date unless params[:from].blank?; rescue; end
153 begin; @to = params[:to].to_s.to_date unless params[:to].blank?; rescue; end
153 begin; @to = params[:to].to_s.to_date unless params[:to].blank?; rescue; end
154 @free_period = true
154 @free_period = true
155 else
155 else
156 # default
156 # default
157 end
157 end
158
158
159 @from, @to = @to, @from if @from && @to && @from > @to
159 @from, @to = @to, @from if @from && @to && @from > @to
160
160
161 cond = ARCondition.new
161 cond = ARCondition.new
162 cond << (@issue.nil? ? ["(#{Project.table_name}.id = ? OR #{Project.table_name}.parent_id = ?)", @project.id, @project.id] :
162 cond << (@issue.nil? ? @project.project_condition(Setting.display_subprojects_issues?) :
163 ["#{TimeEntry.table_name}.issue_id = ?", @issue.id])
163 ["#{TimeEntry.table_name}.issue_id = ?", @issue.id])
164
164
165 if @from
165 if @from
166 if @to
166 if @to
167 cond << ['spent_on BETWEEN ? AND ?', @from, @to]
167 cond << ['spent_on BETWEEN ? AND ?', @from, @to]
168 else
168 else
169 cond << ['spent_on >= ?', @from]
169 cond << ['spent_on >= ?', @from]
170 end
170 end
171 elsif @to
171 elsif @to
172 cond << ['spent_on <= ?', @to]
172 cond << ['spent_on <= ?', @to]
173 end
173 end
174
174
175 TimeEntry.visible_by(User.current) do
175 TimeEntry.visible_by(User.current) do
176 respond_to do |format|
176 respond_to do |format|
177 format.html {
177 format.html {
178 # Paginate results
178 # Paginate results
179 @entry_count = TimeEntry.count(:include => :project, :conditions => cond.conditions)
179 @entry_count = TimeEntry.count(:include => :project, :conditions => cond.conditions)
180 @entry_pages = Paginator.new self, @entry_count, per_page_option, params['page']
180 @entry_pages = Paginator.new self, @entry_count, per_page_option, params['page']
181 @entries = TimeEntry.find(:all,
181 @entries = TimeEntry.find(:all,
182 :include => [:project, :activity, :user, {:issue => :tracker}],
182 :include => [:project, :activity, :user, {:issue => :tracker}],
183 :conditions => cond.conditions,
183 :conditions => cond.conditions,
184 :order => sort_clause,
184 :order => sort_clause,
185 :limit => @entry_pages.items_per_page,
185 :limit => @entry_pages.items_per_page,
186 :offset => @entry_pages.current.offset)
186 :offset => @entry_pages.current.offset)
187 @total_hours = TimeEntry.sum(:hours, :include => :project, :conditions => cond.conditions).to_f
187 @total_hours = TimeEntry.sum(:hours, :include => :project, :conditions => cond.conditions).to_f
188 render :layout => !request.xhr?
188 render :layout => !request.xhr?
189 }
189 }
190 format.csv {
190 format.csv {
191 # Export all entries
191 # Export all entries
192 @entries = TimeEntry.find(:all,
192 @entries = TimeEntry.find(:all,
193 :include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}],
193 :include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}],
194 :conditions => cond.conditions,
194 :conditions => cond.conditions,
195 :order => sort_clause)
195 :order => sort_clause)
196 send_data(entries_to_csv(@entries).read, :type => 'text/csv; header=present', :filename => 'timelog.csv')
196 send_data(entries_to_csv(@entries).read, :type => 'text/csv; header=present', :filename => 'timelog.csv')
197 }
197 }
198 end
198 end
199 end
199 end
200 end
200 end
201
201
202 def edit
202 def edit
203 render_403 and return if @time_entry && !@time_entry.editable_by?(User.current)
203 render_403 and return if @time_entry && !@time_entry.editable_by?(User.current)
204 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => Date.today)
204 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => Date.today)
205 @time_entry.attributes = params[:time_entry]
205 @time_entry.attributes = params[:time_entry]
206 if request.post? and @time_entry.save
206 if request.post? and @time_entry.save
207 flash[:notice] = l(:notice_successful_update)
207 flash[:notice] = l(:notice_successful_update)
208 redirect_to :action => 'details', :project_id => @time_entry.project
208 redirect_to :action => 'details', :project_id => @time_entry.project
209 return
209 return
210 end
210 end
211 @activities = Enumeration::get_values('ACTI')
211 @activities = Enumeration::get_values('ACTI')
212 end
212 end
213
213
214 def destroy
214 def destroy
215 render_404 and return unless @time_entry
215 render_404 and return unless @time_entry
216 render_403 and return unless @time_entry.editable_by?(User.current)
216 render_403 and return unless @time_entry.editable_by?(User.current)
217 @time_entry.destroy
217 @time_entry.destroy
218 flash[:notice] = l(:notice_successful_delete)
218 flash[:notice] = l(:notice_successful_delete)
219 redirect_to :back
219 redirect_to :back
220 rescue RedirectBackError
220 rescue RedirectBackError
221 redirect_to :action => 'details', :project_id => @time_entry.project
221 redirect_to :action => 'details', :project_id => @time_entry.project
222 end
222 end
223
223
224 private
224 private
225 def find_project
225 def find_project
226 if params[:id]
226 if params[:id]
227 @time_entry = TimeEntry.find(params[:id])
227 @time_entry = TimeEntry.find(params[:id])
228 @project = @time_entry.project
228 @project = @time_entry.project
229 elsif params[:issue_id]
229 elsif params[:issue_id]
230 @issue = Issue.find(params[:issue_id])
230 @issue = Issue.find(params[:issue_id])
231 @project = @issue.project
231 @project = @issue.project
232 elsif params[:project_id]
232 elsif params[:project_id]
233 @project = Project.find(params[:project_id])
233 @project = Project.find(params[:project_id])
234 else
234 else
235 render_404
235 render_404
236 return false
236 return false
237 end
237 end
238 rescue ActiveRecord::RecordNotFound
238 rescue ActiveRecord::RecordNotFound
239 render_404
239 render_404
240 end
240 end
241 end
241 end
@@ -1,248 +1,254
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006 Jean-Philippe Lang
2 # Copyright (C) 2006 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 class Project < ActiveRecord::Base
18 class Project < ActiveRecord::Base
19 # Project statuses
19 # Project statuses
20 STATUS_ACTIVE = 1
20 STATUS_ACTIVE = 1
21 STATUS_ARCHIVED = 9
21 STATUS_ARCHIVED = 9
22
22
23 has_many :members, :include => :user, :conditions => "#{User.table_name}.status=#{User::STATUS_ACTIVE}"
23 has_many :members, :include => :user, :conditions => "#{User.table_name}.status=#{User::STATUS_ACTIVE}"
24 has_many :users, :through => :members
24 has_many :users, :through => :members
25 has_many :custom_values, :dependent => :delete_all, :as => :customized
25 has_many :custom_values, :dependent => :delete_all, :as => :customized
26 has_many :enabled_modules, :dependent => :delete_all
26 has_many :enabled_modules, :dependent => :delete_all
27 has_and_belongs_to_many :trackers, :order => "#{Tracker.table_name}.position"
27 has_and_belongs_to_many :trackers, :order => "#{Tracker.table_name}.position"
28 has_many :issues, :dependent => :destroy, :order => "#{Issue.table_name}.created_on DESC", :include => [:status, :tracker]
28 has_many :issues, :dependent => :destroy, :order => "#{Issue.table_name}.created_on DESC", :include => [:status, :tracker]
29 has_many :issue_changes, :through => :issues, :source => :journals
29 has_many :issue_changes, :through => :issues, :source => :journals
30 has_many :versions, :dependent => :destroy, :order => "#{Version.table_name}.effective_date DESC, #{Version.table_name}.name DESC"
30 has_many :versions, :dependent => :destroy, :order => "#{Version.table_name}.effective_date DESC, #{Version.table_name}.name DESC"
31 has_many :time_entries, :dependent => :delete_all
31 has_many :time_entries, :dependent => :delete_all
32 has_many :queries, :dependent => :delete_all
32 has_many :queries, :dependent => :delete_all
33 has_many :documents, :dependent => :destroy
33 has_many :documents, :dependent => :destroy
34 has_many :news, :dependent => :delete_all, :include => :author
34 has_many :news, :dependent => :delete_all, :include => :author
35 has_many :issue_categories, :dependent => :delete_all, :order => "#{IssueCategory.table_name}.name"
35 has_many :issue_categories, :dependent => :delete_all, :order => "#{IssueCategory.table_name}.name"
36 has_many :boards, :order => "position ASC"
36 has_many :boards, :order => "position ASC"
37 has_one :repository, :dependent => :destroy
37 has_one :repository, :dependent => :destroy
38 has_many :changesets, :through => :repository
38 has_many :changesets, :through => :repository
39 has_one :wiki, :dependent => :destroy
39 has_one :wiki, :dependent => :destroy
40 # Custom field for the project issues
40 # Custom field for the project issues
41 has_and_belongs_to_many :custom_fields,
41 has_and_belongs_to_many :custom_fields,
42 :class_name => 'IssueCustomField',
42 :class_name => 'IssueCustomField',
43 :order => "#{CustomField.table_name}.position",
43 :order => "#{CustomField.table_name}.position",
44 :join_table => "#{table_name_prefix}custom_fields_projects#{table_name_suffix}",
44 :join_table => "#{table_name_prefix}custom_fields_projects#{table_name_suffix}",
45 :association_foreign_key => 'custom_field_id'
45 :association_foreign_key => 'custom_field_id'
46
46
47 acts_as_tree :order => "name", :counter_cache => true
47 acts_as_tree :order => "name", :counter_cache => true
48
48
49 acts_as_searchable :columns => ['name', 'description'], :project_key => 'id'
49 acts_as_searchable :columns => ['name', 'description'], :project_key => 'id'
50 acts_as_event :title => Proc.new {|o| "#{l(:label_project)}: #{o.name}"},
50 acts_as_event :title => Proc.new {|o| "#{l(:label_project)}: #{o.name}"},
51 :url => Proc.new {|o| {:controller => 'projects', :action => 'show', :id => o.id}}
51 :url => Proc.new {|o| {:controller => 'projects', :action => 'show', :id => o.id}}
52
52
53 attr_protected :status, :enabled_module_names
53 attr_protected :status, :enabled_module_names
54
54
55 validates_presence_of :name, :identifier
55 validates_presence_of :name, :identifier
56 validates_uniqueness_of :name, :identifier
56 validates_uniqueness_of :name, :identifier
57 validates_associated :custom_values, :on => :update
57 validates_associated :custom_values, :on => :update
58 validates_associated :repository, :wiki
58 validates_associated :repository, :wiki
59 validates_length_of :name, :maximum => 30
59 validates_length_of :name, :maximum => 30
60 validates_length_of :homepage, :maximum => 60
60 validates_length_of :homepage, :maximum => 60
61 validates_length_of :identifier, :in => 3..20
61 validates_length_of :identifier, :in => 3..20
62 validates_format_of :identifier, :with => /^[a-z0-9\-]*$/
62 validates_format_of :identifier, :with => /^[a-z0-9\-]*$/
63
63
64 before_destroy :delete_all_members
64 before_destroy :delete_all_members
65
65
66 def identifier=(identifier)
66 def identifier=(identifier)
67 super unless identifier_frozen?
67 super unless identifier_frozen?
68 end
68 end
69
69
70 def identifier_frozen?
70 def identifier_frozen?
71 errors[:identifier].nil? && !(new_record? || identifier.blank?)
71 errors[:identifier].nil? && !(new_record? || identifier.blank?)
72 end
72 end
73
73
74 def issues_with_subprojects(include_subprojects=false)
74 def issues_with_subprojects(include_subprojects=false)
75 conditions = nil
75 conditions = nil
76 if include_subprojects && !active_children.empty?
76 if include_subprojects && !active_children.empty?
77 ids = [id] + active_children.collect {|c| c.id}
77 ids = [id] + active_children.collect {|c| c.id}
78 conditions = ["#{Issue.table_name}.project_id IN (#{ids.join(',')})"]
78 conditions = ["#{Issue.table_name}.project_id IN (#{ids.join(',')})"]
79 end
79 end
80 conditions ||= ["#{Issue.table_name}.project_id = ?", id]
80 conditions ||= ["#{Issue.table_name}.project_id = ?", id]
81 # Quick and dirty fix for Rails 2 compatibility
81 # Quick and dirty fix for Rails 2 compatibility
82 Issue.send(:with_scope, :find => { :conditions => conditions }) do
82 Issue.send(:with_scope, :find => { :conditions => conditions }) do
83 yield
83 yield
84 end
84 end
85 end
85 end
86
86
87 # returns latest created projects
87 # returns latest created projects
88 # non public projects will be returned only if user is a member of those
88 # non public projects will be returned only if user is a member of those
89 def self.latest(user=nil, count=5)
89 def self.latest(user=nil, count=5)
90 find(:all, :limit => count, :conditions => visible_by(user), :order => "created_on DESC")
90 find(:all, :limit => count, :conditions => visible_by(user), :order => "created_on DESC")
91 end
91 end
92
92
93 def self.visible_by(user=nil)
93 def self.visible_by(user=nil)
94 if user && user.admin?
94 if user && user.admin?
95 return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"
95 return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"
96 elsif user && user.memberships.any?
96 elsif user && user.memberships.any?
97 return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE} AND (#{Project.table_name}.is_public = #{connection.quoted_true} or #{Project.table_name}.id IN (#{user.memberships.collect{|m| m.project_id}.join(',')}))"
97 return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE} AND (#{Project.table_name}.is_public = #{connection.quoted_true} or #{Project.table_name}.id IN (#{user.memberships.collect{|m| m.project_id}.join(',')}))"
98 else
98 else
99 return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE} AND #{Project.table_name}.is_public = #{connection.quoted_true}"
99 return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE} AND #{Project.table_name}.is_public = #{connection.quoted_true}"
100 end
100 end
101 end
101 end
102
102
103 def self.allowed_to_condition(user, permission, options={})
103 def self.allowed_to_condition(user, permission, options={})
104 statements = []
104 statements = []
105 base_statement = "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"
105 base_statement = "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"
106 if options[:project]
106 if options[:project]
107 project_statement = "#{Project.table_name}.id = #{options[:project].id}"
107 project_statement = "#{Project.table_name}.id = #{options[:project].id}"
108 project_statement << " OR #{Project.table_name}.parent_id = #{options[:project].id}" if options[:with_subprojects]
108 project_statement << " OR #{Project.table_name}.parent_id = #{options[:project].id}" if options[:with_subprojects]
109 base_statement = "(#{project_statement}) AND (#{base_statement})"
109 base_statement = "(#{project_statement}) AND (#{base_statement})"
110 end
110 end
111 if user.admin?
111 if user.admin?
112 # no restriction
112 # no restriction
113 elsif user.logged?
113 elsif user.logged?
114 statements << "#{Project.table_name}.is_public = #{connection.quoted_true}" if Role.non_member.allowed_to?(permission)
114 statements << "#{Project.table_name}.is_public = #{connection.quoted_true}" if Role.non_member.allowed_to?(permission)
115 allowed_project_ids = user.memberships.select {|m| m.role.allowed_to?(permission)}.collect {|m| m.project_id}
115 allowed_project_ids = user.memberships.select {|m| m.role.allowed_to?(permission)}.collect {|m| m.project_id}
116 statements << "#{Project.table_name}.id IN (#{allowed_project_ids.join(',')})" if allowed_project_ids.any?
116 statements << "#{Project.table_name}.id IN (#{allowed_project_ids.join(',')})" if allowed_project_ids.any?
117 elsif Role.anonymous.allowed_to?(permission)
117 elsif Role.anonymous.allowed_to?(permission)
118 # anonymous user allowed on public project
118 # anonymous user allowed on public project
119 statements << "#{Project.table_name}.is_public = #{connection.quoted_true}"
119 statements << "#{Project.table_name}.is_public = #{connection.quoted_true}"
120 else
120 else
121 # anonymous user is not authorized
121 # anonymous user is not authorized
122 statements << "1=0"
122 statements << "1=0"
123 end
123 end
124 statements.empty? ? base_statement : "((#{base_statement}) AND (#{statements.join(' OR ')}))"
124 statements.empty? ? base_statement : "((#{base_statement}) AND (#{statements.join(' OR ')}))"
125 end
125 end
126
126
127 def project_condition(with_subprojects)
128 cond = "#{Project.table_name}.id = #{id}"
129 cond = "(#{cond} OR #{Project.table_name}.parent_id = #{id})" if with_subprojects
130 cond
131 end
132
127 def self.find(*args)
133 def self.find(*args)
128 if args.first && args.first.is_a?(String) && !args.first.match(/^\d*$/)
134 if args.first && args.first.is_a?(String) && !args.first.match(/^\d*$/)
129 project = find_by_identifier(*args)
135 project = find_by_identifier(*args)
130 raise ActiveRecord::RecordNotFound, "Couldn't find Project with identifier=#{args.first}" if project.nil?
136 raise ActiveRecord::RecordNotFound, "Couldn't find Project with identifier=#{args.first}" if project.nil?
131 project
137 project
132 else
138 else
133 super
139 super
134 end
140 end
135 end
141 end
136
142
137 def to_param
143 def to_param
138 identifier
144 identifier
139 end
145 end
140
146
141 def active?
147 def active?
142 self.status == STATUS_ACTIVE
148 self.status == STATUS_ACTIVE
143 end
149 end
144
150
145 def archive
151 def archive
146 # Archive subprojects if any
152 # Archive subprojects if any
147 children.each do |subproject|
153 children.each do |subproject|
148 subproject.archive
154 subproject.archive
149 end
155 end
150 update_attribute :status, STATUS_ARCHIVED
156 update_attribute :status, STATUS_ARCHIVED
151 end
157 end
152
158
153 def unarchive
159 def unarchive
154 return false if parent && !parent.active?
160 return false if parent && !parent.active?
155 update_attribute :status, STATUS_ACTIVE
161 update_attribute :status, STATUS_ACTIVE
156 end
162 end
157
163
158 def active_children
164 def active_children
159 children.select {|child| child.active?}
165 children.select {|child| child.active?}
160 end
166 end
161
167
162 # Returns an array of the trackers used by the project and its sub projects
168 # Returns an array of the trackers used by the project and its sub projects
163 def rolled_up_trackers
169 def rolled_up_trackers
164 @rolled_up_trackers ||=
170 @rolled_up_trackers ||=
165 Tracker.find(:all, :include => :projects,
171 Tracker.find(:all, :include => :projects,
166 :select => "DISTINCT #{Tracker.table_name}.*",
172 :select => "DISTINCT #{Tracker.table_name}.*",
167 :conditions => ["#{Project.table_name}.id = ? OR #{Project.table_name}.parent_id = ?", id, id],
173 :conditions => ["#{Project.table_name}.id = ? OR #{Project.table_name}.parent_id = ?", id, id],
168 :order => "#{Tracker.table_name}.position")
174 :order => "#{Tracker.table_name}.position")
169 end
175 end
170
176
171 # Deletes all project's members
177 # Deletes all project's members
172 def delete_all_members
178 def delete_all_members
173 Member.delete_all(['project_id = ?', id])
179 Member.delete_all(['project_id = ?', id])
174 end
180 end
175
181
176 # Users issues can be assigned to
182 # Users issues can be assigned to
177 def assignable_users
183 def assignable_users
178 members.select {|m| m.role.assignable?}.collect {|m| m.user}.sort
184 members.select {|m| m.role.assignable?}.collect {|m| m.user}.sort
179 end
185 end
180
186
181 # Returns the mail adresses of users that should be always notified on project events
187 # Returns the mail adresses of users that should be always notified on project events
182 def recipients
188 def recipients
183 members.select {|m| m.mail_notification? || m.user.mail_notification?}.collect {|m| m.user.mail}
189 members.select {|m| m.mail_notification? || m.user.mail_notification?}.collect {|m| m.user.mail}
184 end
190 end
185
191
186 # Returns an array of all custom fields enabled for project issues
192 # Returns an array of all custom fields enabled for project issues
187 # (explictly associated custom fields and custom fields enabled for all projects)
193 # (explictly associated custom fields and custom fields enabled for all projects)
188 def custom_fields_for_issues(tracker)
194 def custom_fields_for_issues(tracker)
189 all_custom_fields.select {|c| tracker.custom_fields.include? c }
195 all_custom_fields.select {|c| tracker.custom_fields.include? c }
190 end
196 end
191
197
192 def all_custom_fields
198 def all_custom_fields
193 @all_custom_fields ||= (IssueCustomField.for_all + custom_fields).uniq
199 @all_custom_fields ||= (IssueCustomField.for_all + custom_fields).uniq
194 end
200 end
195
201
196 def <=>(project)
202 def <=>(project)
197 name.downcase <=> project.name.downcase
203 name.downcase <=> project.name.downcase
198 end
204 end
199
205
200 def to_s
206 def to_s
201 name
207 name
202 end
208 end
203
209
204 # Returns a short description of the projects (first lines)
210 # Returns a short description of the projects (first lines)
205 def short_description(length = 255)
211 def short_description(length = 255)
206 description.gsub(/^(.{#{length}}[^\n]*).*$/m, '\1').strip if description
212 description.gsub(/^(.{#{length}}[^\n]*).*$/m, '\1').strip if description
207 end
213 end
208
214
209 def allows_to?(action)
215 def allows_to?(action)
210 if action.is_a? Hash
216 if action.is_a? Hash
211 allowed_actions.include? "#{action[:controller]}/#{action[:action]}"
217 allowed_actions.include? "#{action[:controller]}/#{action[:action]}"
212 else
218 else
213 allowed_permissions.include? action
219 allowed_permissions.include? action
214 end
220 end
215 end
221 end
216
222
217 def module_enabled?(module_name)
223 def module_enabled?(module_name)
218 module_name = module_name.to_s
224 module_name = module_name.to_s
219 enabled_modules.detect {|m| m.name == module_name}
225 enabled_modules.detect {|m| m.name == module_name}
220 end
226 end
221
227
222 def enabled_module_names=(module_names)
228 def enabled_module_names=(module_names)
223 enabled_modules.clear
229 enabled_modules.clear
224 module_names = [] unless module_names && module_names.is_a?(Array)
230 module_names = [] unless module_names && module_names.is_a?(Array)
225 module_names.each do |name|
231 module_names.each do |name|
226 enabled_modules << EnabledModule.new(:name => name.to_s)
232 enabled_modules << EnabledModule.new(:name => name.to_s)
227 end
233 end
228 end
234 end
229
235
230 protected
236 protected
231 def validate
237 def validate
232 errors.add(parent_id, " must be a root project") if parent and parent.parent
238 errors.add(parent_id, " must be a root project") if parent and parent.parent
233 errors.add_to_base("A project with subprojects can't be a subproject") if parent and children.size > 0
239 errors.add_to_base("A project with subprojects can't be a subproject") if parent and children.size > 0
234 errors.add(:identifier, :activerecord_error_invalid) if !identifier.blank? && identifier.match(/^\d*$/)
240 errors.add(:identifier, :activerecord_error_invalid) if !identifier.blank? && identifier.match(/^\d*$/)
235 end
241 end
236
242
237 private
243 private
238 def allowed_permissions
244 def allowed_permissions
239 @allowed_permissions ||= begin
245 @allowed_permissions ||= begin
240 module_names = enabled_modules.collect {|m| m.name}
246 module_names = enabled_modules.collect {|m| m.name}
241 Redmine::AccessControl.modules_permissions(module_names).collect {|p| p.name}
247 Redmine::AccessControl.modules_permissions(module_names).collect {|p| p.name}
242 end
248 end
243 end
249 end
244
250
245 def allowed_actions
251 def allowed_actions
246 @actions_allowed ||= allowed_permissions.inject([]) { |actions, permission| actions += Redmine::AccessControl.allowed_actions(permission) }.flatten
252 @actions_allowed ||= allowed_permissions.inject([]) { |actions, permission| actions += Redmine::AccessControl.allowed_actions(permission) }.flatten
247 end
253 end
248 end
254 end
General Comments 0
You need to be logged in to leave comments. Login now