comparison app/controllers/timelog_controller.rb @ 119:8661b858af72

* Update to Redmine trunk rev 4705
author Chris Cannam
date Thu, 13 Jan 2011 14:12:06 +0000
parents 94944d00e43c
children cbce1fd3b1b7
comparison
equal deleted inserted replaced
39:150ceac17a8d 119:8661b858af72
1 # redMine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang 2 # Copyright (C) 2006-2010 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.
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 menu_item :issues 19 menu_item :issues
20 before_filter :find_project, :only => [:new, :create] 20 before_filter :find_project, :only => [:new, :create]
21 before_filter :find_time_entry, :only => [:edit, :update, :destroy] 21 before_filter :find_time_entry, :only => [:show, :edit, :update, :destroy]
22 before_filter :authorize, :except => [:index] 22 before_filter :authorize, :except => [:index]
23 before_filter :find_optional_project, :only => [:index] 23 before_filter :find_optional_project, :only => [:index]
24 24 accept_key_auth :index, :show, :create, :update, :destroy
25
25 helper :sort 26 helper :sort
26 include SortHelper 27 include SortHelper
27 helper :issues 28 helper :issues
28 include TimelogHelper 29 include TimelogHelper
29 helper :custom_fields 30 helper :custom_fields
64 :offset => @entry_pages.current.offset) 65 :offset => @entry_pages.current.offset)
65 @total_hours = TimeEntry.sum(:hours, :include => [:project, :issue], :conditions => cond.conditions).to_f 66 @total_hours = TimeEntry.sum(:hours, :include => [:project, :issue], :conditions => cond.conditions).to_f
66 67
67 render :layout => !request.xhr? 68 render :layout => !request.xhr?
68 } 69 }
70 format.api {
71 @entry_count = TimeEntry.count(:include => [:project, :issue], :conditions => cond.conditions)
72 @entry_pages = Paginator.new self, @entry_count, per_page_option, params['page']
73 @entries = TimeEntry.find(:all,
74 :include => [:project, :activity, :user, {:issue => :tracker}],
75 :conditions => cond.conditions,
76 :order => sort_clause,
77 :limit => @entry_pages.items_per_page,
78 :offset => @entry_pages.current.offset)
79 }
69 format.atom { 80 format.atom {
70 entries = TimeEntry.find(:all, 81 entries = TimeEntry.find(:all,
71 :include => [:project, :activity, :user, {:issue => :tracker}], 82 :include => [:project, :activity, :user, {:issue => :tracker}],
72 :conditions => cond.conditions, 83 :conditions => cond.conditions,
73 :order => "#{TimeEntry.table_name}.created_on DESC", 84 :order => "#{TimeEntry.table_name}.created_on DESC",
83 send_data(entries_to_csv(@entries), :type => 'text/csv; header=present', :filename => 'timelog.csv') 94 send_data(entries_to_csv(@entries), :type => 'text/csv; header=present', :filename => 'timelog.csv')
84 } 95 }
85 end 96 end
86 end 97 end
87 end 98 end
99
100 def show
101 respond_to do |format|
102 # TODO: Implement html response
103 format.html { render :nothing => true, :status => 406 }
104 format.api
105 end
106 end
88 107
89 def new 108 def new
90 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today) 109 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today)
91 @time_entry.attributes = params[:time_entry] 110 @time_entry.attributes = params[:time_entry]
92 111
100 @time_entry.attributes = params[:time_entry] 119 @time_entry.attributes = params[:time_entry]
101 120
102 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry }) 121 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
103 122
104 if @time_entry.save 123 if @time_entry.save
105 flash[:notice] = l(:notice_successful_update) 124 respond_to do |format|
106 redirect_back_or_default :action => 'index', :project_id => @time_entry.project 125 format.html {
107 else 126 flash[:notice] = l(:notice_successful_update)
108 render :action => 'edit' 127 redirect_back_or_default :action => 'index', :project_id => @time_entry.project
128 }
129 format.api { render :action => 'show', :status => :created, :location => time_entry_url(@time_entry) }
130 end
131 else
132 respond_to do |format|
133 format.html { render :action => 'edit' }
134 format.api { render_validation_errors(@time_entry) }
135 end
109 end 136 end
110 end 137 end
111 138
112 def edit 139 def edit
113 @time_entry.attributes = params[:time_entry] 140 @time_entry.attributes = params[:time_entry]
120 @time_entry.attributes = params[:time_entry] 147 @time_entry.attributes = params[:time_entry]
121 148
122 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry }) 149 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
123 150
124 if @time_entry.save 151 if @time_entry.save
125 flash[:notice] = l(:notice_successful_update) 152 respond_to do |format|
126 redirect_back_or_default :action => 'index', :project_id => @time_entry.project 153 format.html {
127 else 154 flash[:notice] = l(:notice_successful_update)
128 render :action => 'edit' 155 redirect_back_or_default :action => 'index', :project_id => @time_entry.project
156 }
157 format.api { head :ok }
158 end
159 else
160 respond_to do |format|
161 format.html { render :action => 'edit' }
162 format.api { render_validation_errors(@time_entry) }
163 end
129 end 164 end
130 end 165 end
131 166
132 verify :method => :delete, :only => :destroy, :render => {:nothing => true, :status => :method_not_allowed } 167 verify :method => :delete, :only => :destroy, :render => {:nothing => true, :status => :method_not_allowed }
133 def destroy 168 def destroy
134 if @time_entry.destroy && @time_entry.destroyed? 169 if @time_entry.destroy && @time_entry.destroyed?
135 flash[:notice] = l(:notice_successful_delete) 170 respond_to do |format|
136 else 171 format.html {
137 flash[:error] = l(:notice_unable_delete_time_entry) 172 flash[:notice] = l(:notice_successful_delete)
138 end 173 redirect_to :back
139 redirect_to :back 174 }
175 format.api { head :ok }
176 end
177 else
178 respond_to do |format|
179 format.html {
180 flash[:error] = l(:notice_unable_delete_time_entry)
181 redirect_to :back
182 }
183 format.api { render_validation_errors(@time_entry) }
184 end
185 end
140 rescue ::ActionController::RedirectBackError 186 rescue ::ActionController::RedirectBackError
141 redirect_to :action => 'index', :project_id => @time_entry.project 187 redirect_to :action => 'index', :project_id => @time_entry.project
142 end 188 end
143 189
144 private 190 private
152 rescue ActiveRecord::RecordNotFound 198 rescue ActiveRecord::RecordNotFound
153 render_404 199 render_404
154 end 200 end
155 201
156 def find_project 202 def find_project
157 if params[:issue_id] 203 if (issue_id = (params[:issue_id] || params[:time_entry] && params[:time_entry][:issue_id])).present?
158 @issue = Issue.find(params[:issue_id]) 204 @issue = Issue.find(issue_id)
159 @project = @issue.project 205 @project = @issue.project
160 elsif params[:project_id] 206 elsif (project_id = (params[:project_id] || params[:time_entry] && params[:time_entry][:project_id])).present?
161 @project = Project.find(params[:project_id]) 207 @project = Project.find(project_id)
162 else 208 else
163 render_404 209 render_404
164 return false 210 return false
165 end 211 end
166 rescue ActiveRecord::RecordNotFound 212 rescue ActiveRecord::RecordNotFound