Project

General

Profile

Feature #2295 » projects_controller.rb

These are the main modifications, mainly setting up the new query and the toggle logic. Search for keyword "Tyler" for begin and end of modifications. - Tyler Z, 2008-12-07 22:52

 
1
# redMine - project management software
2
# Copyright (C) 2006-2007  Jean-Philippe Lang
3
#
4
# This program is free software; you can redistribute it and/or
5
# modify it under the terms of the GNU General Public License
6
# as published by the Free Software Foundation; either version 2
7
# of the License, or (at your option) any later version.
8
# 
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
# 
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
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17

    
18
class ProjectsController < ApplicationController
19
  layout 'base'
20
  menu_item :overview
21
  menu_item :activity, :only => :activity
22
  menu_item :roadmap, :only => :roadmap
23
  menu_item :files, :only => [:list_files, :add_file]
24
  menu_item :settings, :only => :settings
25
  menu_item :issues, :only => [:changelog]
26
  
27
  before_filter :find_project, :except => [ :index, :list, :add, :activity ]
28
  before_filter :find_optional_project, :only => :activity
29
  before_filter :authorize, :except => [ :index, :list, :add, :archive, :unarchive, :destroy, :activity ]
30
  before_filter :require_admin, :only => [ :add, :archive, :unarchive, :destroy ]
31
  accept_key_auth :activity, :calendar
32
  
33
  helper :sort
34
  include SortHelper
35
  helper :custom_fields
36
  include CustomFieldsHelper   
37
  helper :ifpdf
38
  include IfpdfHelper
39
  helper :issues
40
  helper IssuesHelper
41
  helper :queries
42
  include QueriesHelper
43
  helper :repositories
44
  include RepositoriesHelper
45
  include ProjectsHelper
46
  
47
  # Lists visible projects
48
  def index
49
    projects = Project.find :all,
50
                            :conditions => Project.visible_by(User.current),
51
                            :include => :parent
52
    respond_to do |format|
53
      format.html { 
54
        @project_tree = projects.group_by {|p| p.parent || p}
55
        @project_tree.keys.each {|p| @project_tree[p] -= [p]} 
56
      }
57
      format.atom {
58
        render_feed(projects.sort_by(&:created_on).reverse.slice(0, Setting.feeds_limit.to_i), 
59
                                  :title => "#{Setting.app_title}: #{l(:label_project_latest)}")
60
      }
61
    end
62
  end
63
  
64
  # Add a new project
65
  def add
66
    @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
67
    @trackers = Tracker.all
68
    @root_projects = Project.find(:all,
69
                                  :conditions => "parent_id IS NULL AND status = #{Project::STATUS_ACTIVE}",
70
                                  :order => 'name')
71
    @project = Project.new(params[:project])
72
    if request.get?
73
      @project.trackers = Tracker.all
74
      @project.is_public = Setting.default_projects_public?
75
      @project.enabled_module_names = Redmine::AccessControl.available_project_modules
76
    else
77
      @project.enabled_module_names = params[:enabled_modules]
78
      if @project.save
79
        flash[:notice] = l(:notice_successful_create)
80
        redirect_to :controller => 'admin', :action => 'projects'
81
	  end		
82
    end	
83
  end
84
	
85
  # Show @project
86
  def show
87
    @members_by_role = @project.members.find(:all, :include => [:user, :role], :order => 'position').group_by {|m| m.role}
88
    @subprojects = @project.children.find(:all, :conditions => Project.visible_by(User.current))
89
    @news = @project.news.find(:all, :limit => 5, :include => [ :author, :project ], :order => "#{News.table_name}.created_on DESC")
90
    @trackers = @project.rolled_up_trackers
91
    
92
    cond = @project.project_condition(Setting.display_subprojects_issues?)
93
    Issue.visible_by(User.current) do
94
      @open_issues_by_tracker = Issue.count(:group => :tracker,
95
                                            :include => [:project, :status, :tracker],
96
                                            :conditions => ["(#{cond}) AND #{IssueStatus.table_name}.is_closed=?", false])
97
      @total_issues_by_tracker = Issue.count(:group => :tracker,
98
                                            :include => [:project, :status, :tracker],
99
                                            :conditions => cond)
100
    end
101
    TimeEntry.visible_by(User.current) do
102
      @total_hours = TimeEntry.sum(:hours, 
103
                                   :include => :project,
104
                                   :conditions => cond).to_f
105
    end
106
    @key = User.current.rss_key
107
  end
108

    
109
  def settings
110
    @root_projects = Project.find(:all,
111
                                  :conditions => ["parent_id IS NULL AND status = #{Project::STATUS_ACTIVE} AND id <> ?", @project.id],
112
                                  :order => 'name')
113
    @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
114
    @issue_category ||= IssueCategory.new
115
    @member ||= @project.members.new
116
    @trackers = Tracker.all
117
    @repository ||= @project.repository
118
    @wiki ||= @project.wiki
119
  end
120
  
121
  # Edit @project
122
  def edit
123
    if request.post?
124
      @project.attributes = params[:project]
125
      if @project.save
126
        flash[:notice] = l(:notice_successful_update)
127
        redirect_to :action => 'settings', :id => @project
128
      else
129
        settings
130
        render :action => 'settings'
131
      end
132
    end
133
  end
134
  
135
  def modules
136
    @project.enabled_module_names = params[:enabled_modules]
137
    redirect_to :action => 'settings', :id => @project, :tab => 'modules'
138
  end
139

    
140
  def archive
141
    @project.archive if request.post? && @project.active?
142
    redirect_to :controller => 'admin', :action => 'projects'
143
  end
144
  
145
  def unarchive
146
    @project.unarchive if request.post? && !@project.active?
147
    redirect_to :controller => 'admin', :action => 'projects'
148
  end
149
  
150
  # Delete @project
151
  def destroy
152
    @project_to_destroy = @project
153
    if request.post? and params[:confirm]
154
      @project_to_destroy.destroy
155
      redirect_to :controller => 'admin', :action => 'projects'
156
    end
157
    # hide project in layout
158
    @project = nil
159
  end
160
	
161
  # Add a new issue category to @project
162
  def add_issue_category
163
    @category = @project.issue_categories.build(params[:category])
164
    if request.post? and @category.save
165
  	  respond_to do |format|
166
        format.html do
167
          flash[:notice] = l(:notice_successful_create)
168
          redirect_to :action => 'settings', :tab => 'categories', :id => @project
169
        end
170
        format.js do
171
          # IE doesn't support the replace_html rjs method for select box options
172
          render(:update) {|page| page.replace "issue_category_id",
173
            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]')
174
          }
175
        end
176
      end
177
    end
178
  end
179
	
180
  # Add a new version to @project
181
  def add_version
182
  	@version = @project.versions.build(params[:version])
183
  	if request.post? and @version.save
184
  	  flash[:notice] = l(:notice_successful_create)
185
      redirect_to :action => 'settings', :tab => 'versions', :id => @project
186
  	end
187
  end
188

    
189
  def add_file
190
    if request.post?
191
      @version = @project.versions.find_by_id(params[:version_id])
192
      attachments = attach_files(@version, params[:attachments])
193
      Mailer.deliver_attachments_added(attachments) if !attachments.empty? && Setting.notified_events.include?('file_added')
194
      redirect_to :controller => 'projects', :action => 'list_files', :id => @project
195
    end
196
    @versions = @project.versions.sort
197
  end
198
  
199
  def list_files
200
    sort_init "#{Attachment.table_name}.filename", "asc"
201
    sort_update
202
    @versions = @project.versions.find(:all, :include => :attachments, :order => sort_clause).sort.reverse
203
    render :layout => !request.xhr?
204
  end
205
  
206
  # Show changelog for @project
207
  def changelog
208
    @trackers = @project.trackers.find(:all, :conditions => ["is_in_chlog=?", true], :order => 'position')
209
    retrieve_selected_tracker_ids(@trackers)    
210
    @versions = @project.versions.sort
211
  end
212

    
213
  def roadmap
214
    @trackers = @project.trackers.find(:all, :conditions => ["is_in_roadmap=?", true])
215
    retrieve_selected_tracker_ids(@trackers)
216
    @versions = @project.versions.sort
217
    @versions = @versions.select {|v| !v.completed? } unless params[:completed]
218
  end
219
  
220
  def activity
221
    @days = Setting.activity_days_default.to_i
222
    
223
    if params[:from]
224
      begin; @date_to = params[:from].to_date; rescue; end
225
    end
226

    
227
    @date_to ||= Date.today + 1
228
    @date_from = @date_to - @days
229
    
230
    @event_types = %w(issues news files documents changesets wiki_pages messages)
231
    if @project
232
      @event_types.delete('wiki_pages') unless @project.wiki
233
      @event_types.delete('changesets') unless @project.repository
234
      @event_types.delete('messages') unless @project.boards.any?
235
      # only show what the user is allowed to view
236
      @event_types = @event_types.select {|o| User.current.allowed_to?("view_#{o}".to_sym, @project)}
237
      @with_subprojects = params[:with_subprojects].nil? ? Setting.display_subprojects_issues? : (params[:with_subprojects] == '1')
238
    end
239
# Tyler modified
240
    if params["cont_activity_req"]
241
        @noGroupActivity=!params["group_obj_activity"]
242
    else
243
        @noGroupActivity=false
244
    end
245
    @contActivityReq = true
246

    
247
    @scope = @event_types.select {|t| params["show_#{t}"]}
248
    # default events if none is specified in parameters
249
# Allow wiki and messages as default overall activity to show for projects. Would prefer this as a user setting, 
250
# and possibly saved after each change as the latest setting, this a setting in itself to save it after each change
251
# or fix it to a specified setting.
252
#   @scope = (@event_types - %w(wiki_pages messages))if @scope.empty?
253
    @scope = (@event_types)if @scope.empty?
254
    # default events if none is specified in parameters
255
#    @scope = (@event_types - %w(wiki_pages messages))if @scope.empty?
256
    
257
# Tyler modified end
258

    
259
    @events = []    
260
    
261
    if @scope.include?('issues')
262
      cond = ARCondition.new(Project.allowed_to_condition(User.current, :view_issues, :project => @project, :with_subprojects => @with_subprojects))
263
      cond.add(["#{Issue.table_name}.created_on BETWEEN ? AND ?", @date_from, @date_to])
264
      @events += Issue.find(:all, :include => [:project, :author, :tracker], :conditions => cond.conditions)
265
      
266
      cond = ARCondition.new(Project.allowed_to_condition(User.current, :view_issues, :project => @project, :with_subprojects => @with_subprojects))
267
      cond.add(["#{Journal.table_name}.journalized_type = 'Issue' AND #{Journal.table_name}.created_on BETWEEN ? AND ?", @date_from, @date_to])
268
      cond.add("#{JournalDetail.table_name}.prop_key = 'status_id' OR #{Journal.table_name}.notes <> ''")
269
      @events += Journal.find(:all, :include => [{:issue => :project}, :details, :user], :conditions => cond.conditions)
270
    end
271
    
272
    if @scope.include?('news')
273
      cond = ARCondition.new(Project.allowed_to_condition(User.current, :view_news, :project => @project, :with_subprojects => @with_subprojects))
274
      cond.add(["#{News.table_name}.created_on BETWEEN ? AND ?", @date_from, @date_to])
275
      @events += News.find(:all, :include => [:project, :author], :conditions => cond.conditions)
276
    end
277
    
278
    if @scope.include?('files')
279
      cond = ARCondition.new(Project.allowed_to_condition(User.current, :view_files, :project => @project, :with_subprojects => @with_subprojects))
280
      cond.add(["#{Attachment.table_name}.created_on BETWEEN ? AND ?", @date_from, @date_to])
281
      @events += Attachment.find(:all, :select => "#{Attachment.table_name}.*", 
282
                                       :joins => "LEFT JOIN #{Version.table_name} ON #{Attachment.table_name}.container_type='Version' AND #{Version.table_name}.id = #{Attachment.table_name}.container_id " +
283
                                                 "LEFT JOIN #{Project.table_name} ON #{Version.table_name}.project_id = #{Project.table_name}.id",
284
                                       :conditions => cond.conditions)
285
    end
286
    
287
    if @scope.include?('documents')
288
      cond = ARCondition.new(Project.allowed_to_condition(User.current, :view_documents, :project => @project, :with_subprojects => @with_subprojects))
289
      cond.add(["#{Document.table_name}.created_on BETWEEN ? AND ?", @date_from, @date_to])
290
      @events += Document.find(:all, :include => :project, :conditions => cond.conditions)
291
      
292
      cond = ARCondition.new(Project.allowed_to_condition(User.current, :view_documents, :project => @project, :with_subprojects => @with_subprojects))
293
      cond.add(["#{Attachment.table_name}.created_on BETWEEN ? AND ?", @date_from, @date_to])
294
      @events += Attachment.find(:all, :select => "#{Attachment.table_name}.*", 
295
                                       :joins => "LEFT JOIN #{Document.table_name} ON #{Attachment.table_name}.container_type='Document' AND #{Document.table_name}.id = #{Attachment.table_name}.container_id " +
296
                                                 "LEFT JOIN #{Project.table_name} ON #{Document.table_name}.project_id = #{Project.table_name}.id",
297
                                       :conditions => cond.conditions)
298
    end
299
    
300
    if @scope.include?('wiki_pages')
301

    
302
# Tyler changed begin
303
        if @noGroupActivity
304

    
305
      select = "#{WikiContent.versioned_table_name}.updated_on, #{WikiContent.versioned_table_name}.comments, " +
306
               "#{WikiContent.versioned_table_name}.#{WikiContent.version_column}, #{WikiPage.table_name}.title, " +
307
               "#{WikiContent.versioned_table_name}.page_id, #{WikiContent.versioned_table_name}.author_id, " +
308
               "#{WikiContent.versioned_table_name}.id"
309
# Tyler changed end
310

    
311
      joins = "LEFT JOIN #{WikiPage.table_name} ON #{WikiPage.table_name}.id = #{WikiContent.versioned_table_name}.page_id " +
312
              "LEFT JOIN #{Wiki.table_name} ON #{Wiki.table_name}.id = #{WikiPage.table_name}.wiki_id " +
313
              "LEFT JOIN #{Project.table_name} ON #{Project.table_name}.id = #{Wiki.table_name}.project_id"
314

    
315
      cond = ARCondition.new(Project.allowed_to_condition(User.current, :view_wiki_pages, :project => @project, :with_subprojects => @with_subprojects))
316
      
317
	cond.add(["#{WikiContent.versioned_table_name}.updated_on BETWEEN ? AND ?", @date_from, @date_to])
318

    
319
      @events += WikiContent.versioned_class.find(:all, :select => select, :joins => joins, :conditions => cond.conditions)
320

    
321
# Tyler added begin
322
        else
323

    
324
      select = "ver1.updated_on, ver1.comments, " +
325
               "ver1.#{WikiContent.version_column}, #{WikiPage.table_name}.title, " +
326
               "ver1.page_id, ver1.author_id, " +
327
               "ver1.id"
328
      joins = "LEFT JOIN #{WikiContent.versioned_table_name} ver2 ON ver2.page_id=ver1.page_id AND ver1.version<ver2.version " +
329
	      "LEFT JOIN #{WikiPage.table_name} ON #{WikiPage.table_name}.id = ver1.page_id " +
330
              "LEFT JOIN #{Wiki.table_name} ON #{Wiki.table_name}.id = #{WikiPage.table_name}.wiki_id " +
331
              "LEFT JOIN #{Project.table_name} ON #{Project.table_name}.id = #{Wiki.table_name}.project_id"
332

    
333
      cond = ARCondition.new(Project.allowed_to_condition(User.current, :view_wiki_pages, :project => @project, :with_subprojects => @with_subprojects))
334

    
335
      cond.add(["ver1.updated_on BETWEEN ? AND ?", @date_from, @date_to])
336
      cond.add(["ver2.id is NULL"])
337

    
338
      @events += WikiContent.versioned_class.find(:all, :from => "#{WikiContent.versioned_table_name} ver1", :select => select, :joins => joins, :conditions => cond.conditions)
339
        end
340
# Tyler added end
341
    end
342

    
343
    if @scope.include?('changesets')
344
      cond = ARCondition.new(Project.allowed_to_condition(User.current, :view_changesets, :project => @project, :with_subprojects => @with_subprojects))
345
      cond.add(["#{Changeset.table_name}.committed_on BETWEEN ? AND ?", @date_from, @date_to])
346
      @events += Changeset.find(:all, :include => {:repository => :project}, :conditions => cond.conditions)
347
    end
348
    
349
    if @scope.include?('messages')
350
      cond = ARCondition.new(Project.allowed_to_condition(User.current, :view_messages, :project => @project, :with_subprojects => @with_subprojects))
351
      cond.add(["#{Message.table_name}.created_on BETWEEN ? AND ?", @date_from, @date_to])
352
      @events += Message.find(:all, :include => [{:board => :project}, :author], :conditions => cond.conditions)
353
    end
354
    
355
    @events_by_day = @events.group_by(&:event_date)
356
    
357
    respond_to do |format|
358
      format.html { render :layout => false if request.xhr? }
359
      format.atom { render_feed(@events, :title => "#{@project || Setting.app_title}: #{l(:label_activity)}") }
360
    end
361
  end
362
  
363
  def calendar
364
    @trackers = @project.rolled_up_trackers
365
    retrieve_selected_tracker_ids(@trackers)
366
    
367
    if params[:year] and params[:year].to_i > 1900
368
      @year = params[:year].to_i
369
      if params[:month] and params[:month].to_i > 0 and params[:month].to_i < 13
370
        @month = params[:month].to_i
371
      end    
372
    end
373
    @year ||= Date.today.year
374
    @month ||= Date.today.month    
375
    @calendar = Redmine::Helpers::Calendar.new(Date.civil(@year, @month, 1), current_language, :month)
376
    @with_subprojects = params[:with_subprojects].nil? ? Setting.display_subprojects_issues? : (params[:with_subprojects] == '1')
377
    events = []
378
    @project.issues_with_subprojects(@with_subprojects) do
379
      events += Issue.find(:all, 
380
                           :include => [:tracker, :status, :assigned_to, :priority, :project], 
381
                           :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]
382
                           ) unless @selected_tracker_ids.empty?
383
      events += Version.find(:all, :include => :project,
384
                                   :conditions => ["effective_date BETWEEN ? AND ?", @calendar.startdt, @calendar.enddt])
385
    end
386
    @calendar.events = events
387
    
388
    render :layout => false if request.xhr?
389
  end  
390

    
391
  def gantt
392
    @trackers = @project.rolled_up_trackers
393
    retrieve_selected_tracker_ids(@trackers)
394
    
395
    if params[:year] and params[:year].to_i >0
396
      @year_from = params[:year].to_i
397
      if params[:month] and params[:month].to_i >=1 and params[:month].to_i <= 12
398
        @month_from = params[:month].to_i
399
      else
400
        @month_from = 1
401
      end
402
    else
403
      @month_from ||= Date.today.month
404
      @year_from ||= Date.today.year
405
    end
406
    
407
    zoom = (params[:zoom] || User.current.pref[:gantt_zoom]).to_i
408
    @zoom = (zoom > 0 && zoom < 5) ? zoom : 2    
409
    months = (params[:months] || User.current.pref[:gantt_months]).to_i
410
    @months = (months > 0 && months < 25) ? months : 6
411
    
412
    # Save gantt paramters as user preference (zoom and months count)
413
    if (User.current.logged? && (@zoom != User.current.pref[:gantt_zoom] || @months != User.current.pref[:gantt_months]))
414
      User.current.pref[:gantt_zoom], User.current.pref[:gantt_months] = @zoom, @months
415
      User.current.preference.save
416
    end
417
    
418
    @date_from = Date.civil(@year_from, @month_from, 1)
419
    @date_to = (@date_from >> @months) - 1
420
    @with_subprojects = params[:with_subprojects].nil? ? Setting.display_subprojects_issues? : (params[:with_subprojects] == '1')
421
    
422
    @events = []
423
    @project.issues_with_subprojects(@with_subprojects) do
424
      # Issues that have start and due dates
425
      @events += Issue.find(:all, 
426
                           :order => "start_date, due_date",
427
                           :include => [:tracker, :status, :assigned_to, :priority, :project], 
428
                           :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]
429
                           ) unless @selected_tracker_ids.empty?
430
      # Issues that don't have a due date but that are assigned to a version with a date
431
      @events += Issue.find(:all, 
432
                           :order => "start_date, effective_date",
433
                           :include => [:tracker, :status, :assigned_to, :priority, :project, :fixed_version], 
434
                           :conditions => ["(((start_date>=? and start_date<=?) or (effective_date>=? and effective_date<=?) or (start_date<? and effective_date>?)) and start_date is not null and due_date is null and effective_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]
435
                           ) unless @selected_tracker_ids.empty?
436
      @events += Version.find(:all, :include => :project,
437
                                    :conditions => ["effective_date BETWEEN ? AND ?", @date_from, @date_to])
438
    end
439
    @events.sort! {|x,y| x.start_date <=> y.start_date }
440
    
441
    if params[:format]=='pdf'
442
      @options_for_rfpdf ||= {}
443
      @options_for_rfpdf[:file_name] = "#{@project.identifier}-gantt.pdf"
444
      render :template => "projects/gantt.rfpdf", :layout => false
445
    elsif params[:format]=='png' && respond_to?('gantt_image')
446
      image = gantt_image(@events, @date_from, @months, @zoom)
447
      image.format = 'PNG'
448
      send_data(image.to_blob, :disposition => 'inline', :type => 'image/png', :filename => "#{@project.identifier}-gantt.png")
449
    else
450
      render :template => "projects/gantt.rhtml"
451
    end
452
  end
453
  
454
private
455
  # Find project of id params[:id]
456
  # if not found, redirect to project list
457
  # Used as a before_filter
458
  def find_project
459
    @project = Project.find(params[:id])
460
  rescue ActiveRecord::RecordNotFound
461
    render_404
462
  end
463
  
464
  def find_optional_project
465
    return true unless params[:id]
466
    @project = Project.find(params[:id])
467
    authorize
468
  rescue ActiveRecord::RecordNotFound
469
    render_404
470
  end
471

    
472
  def retrieve_selected_tracker_ids(selectable_trackers)
473
    if ids = params[:tracker_ids]
474
      @selected_tracker_ids = (ids.is_a? Array) ? ids.collect { |id| id.to_i.to_s } : ids.split('/').collect { |id| id.to_i.to_s }
475
    else
476
      @selected_tracker_ids = selectable_trackers.collect {|t| t.id.to_s }
477
    end
478
  end
479
end
(2-2/3)