To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / app / controllers / timelog_controller.rb @ 1298:4f746d8966dd

History | View | Annotate | Download (10.5 KB)

1 119:8661b858af72 Chris
# Redmine - project management software
2 1295:622f24f53b42 Chris
# Copyright (C) 2006-2013  Jean-Philippe Lang
3 0:513646585e45 Chris
#
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 909:cbb26bc654de Chris
#
9 0:513646585e45 Chris
# 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 909:cbb26bc654de Chris
#
14 0:513646585e45 Chris
# 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 TimelogController < ApplicationController
19
  menu_item :issues
20 1115:433d4f72a19b Chris
21
  before_filter :find_project_for_new_time_entry, :only => [:create]
22 441:cbce1fd3b1b7 Chris
  before_filter :find_time_entry, :only => [:show, :edit, :update]
23
  before_filter :find_time_entries, :only => [:bulk_edit, :bulk_update, :destroy]
24 1115:433d4f72a19b Chris
  before_filter :authorize, :except => [:new, :index, :report]
25
26
  before_filter :find_optional_project, :only => [:index, :report]
27
  before_filter :find_optional_project_for_new_time_entry, :only => [:new]
28
  before_filter :authorize_global, :only => [:new, :index, :report]
29
30 507:0c939c159af4 Chris
  accept_rss_auth :index
31
  accept_api_auth :index, :show, :create, :update, :destroy
32 909:cbb26bc654de Chris
33 1295:622f24f53b42 Chris
  rescue_from Query::StatementInvalid, :with => :query_statement_invalid
34
35 0:513646585e45 Chris
  helper :sort
36
  include SortHelper
37
  helper :issues
38
  include TimelogHelper
39
  helper :custom_fields
40
  include CustomFieldsHelper
41 1295:622f24f53b42 Chris
  helper :queries
42
  include QueriesHelper
43 909:cbb26bc654de Chris
44 37:94944d00e43c chris
  def index
45 1295:622f24f53b42 Chris
    @query = TimeEntryQuery.build_from_params(params, :project => @project, :name => '_')
46
    scope = time_entry_scope
47 909:cbb26bc654de Chris
48 1295:622f24f53b42 Chris
    sort_init(@query.sort_criteria.empty? ? [['spent_on', 'desc']] : @query.sort_criteria)
49
    sort_update(@query.sortable_columns)
50 909:cbb26bc654de Chris
51 441:cbce1fd3b1b7 Chris
    respond_to do |format|
52
      format.html {
53
        # Paginate results
54 1115:433d4f72a19b Chris
        @entry_count = scope.count
55 1295:622f24f53b42 Chris
        @entry_pages = Paginator.new @entry_count, per_page_option, params['page']
56 1115:433d4f72a19b Chris
        @entries = scope.all(
57
          :include => [:project, :activity, :user, {:issue => :tracker}],
58
          :order => sort_clause,
59 1295:622f24f53b42 Chris
          :limit  =>  @entry_pages.per_page,
60
          :offset =>  @entry_pages.offset
61 1115:433d4f72a19b Chris
        )
62
        @total_hours = scope.sum(:hours).to_f
63 0:513646585e45 Chris
64 441:cbce1fd3b1b7 Chris
        render :layout => !request.xhr?
65
      }
66
      format.api  {
67 1115:433d4f72a19b Chris
        @entry_count = scope.count
68 441:cbce1fd3b1b7 Chris
        @offset, @limit = api_offset_and_limit
69 1115:433d4f72a19b Chris
        @entries = scope.all(
70
          :include => [:project, :activity, :user, {:issue => :tracker}],
71
          :order => sort_clause,
72
          :limit  => @limit,
73
          :offset => @offset
74
        )
75 441:cbce1fd3b1b7 Chris
      }
76
      format.atom {
77 1115:433d4f72a19b Chris
        entries = scope.all(
78
          :include => [:project, :activity, :user, {:issue => :tracker}],
79
          :order => "#{TimeEntry.table_name}.created_on DESC",
80
          :limit => Setting.feeds_limit.to_i
81
        )
82 441:cbce1fd3b1b7 Chris
        render_feed(entries, :title => l(:label_spent_time))
83
      }
84
      format.csv {
85
        # Export all entries
86 1115:433d4f72a19b Chris
        @entries = scope.all(
87
          :include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}],
88
          :order => sort_clause
89
        )
90 1295:622f24f53b42 Chris
        send_data(query_to_csv(@entries, @query, params), :type => 'text/csv; header=present', :filename => 'timelog.csv')
91 441:cbce1fd3b1b7 Chris
      }
92 0:513646585e45 Chris
    end
93
  end
94 909:cbb26bc654de Chris
95 1115:433d4f72a19b Chris
  def report
96 1295:622f24f53b42 Chris
    @query = TimeEntryQuery.build_from_params(params, :project => @project, :name => '_')
97
    scope = time_entry_scope
98
99
    @report = Redmine::Helpers::TimeReport.new(@project, @issue, params[:criteria], params[:columns], scope)
100 1115:433d4f72a19b Chris
101
    respond_to do |format|
102
      format.html { render :layout => !request.xhr? }
103
      format.csv  { send_data(report_to_csv(@report), :type => 'text/csv; header=present', :filename => 'timelog.csv') }
104
    end
105
  end
106
107 119:8661b858af72 Chris
  def show
108
    respond_to do |format|
109
      # TODO: Implement html response
110
      format.html { render :nothing => true, :status => 406 }
111
      format.api
112
    end
113
  end
114 37:94944d00e43c chris
115
  def new
116
    @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today)
117 929:5f33065ddc4b Chris
    @time_entry.safe_attributes = params[:time_entry]
118 37:94944d00e43c chris
  end
119
120
  def create
121 0:513646585e45 Chris
    @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today)
122 929:5f33065ddc4b Chris
    @time_entry.safe_attributes = params[:time_entry]
123 909:cbb26bc654de Chris
124 0:513646585e45 Chris
    call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
125 909:cbb26bc654de Chris
126 37:94944d00e43c chris
    if @time_entry.save
127 119:8661b858af72 Chris
      respond_to do |format|
128
        format.html {
129 1115:433d4f72a19b Chris
          flash[:notice] = l(:notice_successful_create)
130
          if params[:continue]
131
            if params[:project_id]
132 1295:622f24f53b42 Chris
              options = {
133 1115:433d4f72a19b Chris
                :time_entry => {:issue_id => @time_entry.issue_id, :activity_id => @time_entry.activity_id},
134
                :back_url => params[:back_url]
135 1295:622f24f53b42 Chris
              }
136
              if @time_entry.issue
137
                redirect_to new_project_issue_time_entry_path(@time_entry.project, @time_entry.issue, options)
138
              else
139
                redirect_to new_project_time_entry_path(@time_entry.project, options)
140
              end
141 1115:433d4f72a19b Chris
            else
142 1295:622f24f53b42 Chris
              options = {
143 1115:433d4f72a19b Chris
                :time_entry => {:project_id => @time_entry.project_id, :issue_id => @time_entry.issue_id, :activity_id => @time_entry.activity_id},
144
                :back_url => params[:back_url]
145 1295:622f24f53b42 Chris
              }
146
              redirect_to new_time_entry_path(options)
147 1115:433d4f72a19b Chris
            end
148
          else
149 1295:622f24f53b42 Chris
            redirect_back_or_default project_time_entries_path(@time_entry.project)
150 1115:433d4f72a19b Chris
          end
151 119:8661b858af72 Chris
        }
152
        format.api  { render :action => 'show', :status => :created, :location => time_entry_url(@time_entry) }
153
      end
154 37:94944d00e43c chris
    else
155 119:8661b858af72 Chris
      respond_to do |format|
156 1115:433d4f72a19b Chris
        format.html { render :action => 'new' }
157 119:8661b858af72 Chris
        format.api  { render_validation_errors(@time_entry) }
158
      end
159 909:cbb26bc654de Chris
    end
160 0:513646585e45 Chris
  end
161 909:cbb26bc654de Chris
162 37:94944d00e43c chris
  def edit
163 929:5f33065ddc4b Chris
    @time_entry.safe_attributes = params[:time_entry]
164 37:94944d00e43c chris
  end
165
166
  def update
167 929:5f33065ddc4b Chris
    @time_entry.safe_attributes = params[:time_entry]
168 909:cbb26bc654de Chris
169 37:94944d00e43c chris
    call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
170 909:cbb26bc654de Chris
171 37:94944d00e43c chris
    if @time_entry.save
172 119:8661b858af72 Chris
      respond_to do |format|
173
        format.html {
174
          flash[:notice] = l(:notice_successful_update)
175 1295:622f24f53b42 Chris
          redirect_back_or_default project_time_entries_path(@time_entry.project)
176 119:8661b858af72 Chris
        }
177 1115:433d4f72a19b Chris
        format.api  { render_api_ok }
178 119:8661b858af72 Chris
      end
179 37:94944d00e43c chris
    else
180 119:8661b858af72 Chris
      respond_to do |format|
181
        format.html { render :action => 'edit' }
182
        format.api  { render_validation_errors(@time_entry) }
183
      end
184 909:cbb26bc654de Chris
    end
185 37:94944d00e43c chris
  end
186
187 441:cbce1fd3b1b7 Chris
  def bulk_edit
188
    @available_activities = TimeEntryActivity.shared.active
189
    @custom_fields = TimeEntry.first.available_custom_fields
190
  end
191
192
  def bulk_update
193
    attributes = parse_params_for_bulk_time_entry_attributes(params)
194
195
    unsaved_time_entry_ids = []
196
    @time_entries.each do |time_entry|
197
      time_entry.reload
198 929:5f33065ddc4b Chris
      time_entry.safe_attributes = attributes
199 441:cbce1fd3b1b7 Chris
      call_hook(:controller_time_entries_bulk_edit_before_save, { :params => params, :time_entry => time_entry })
200
      unless time_entry.save
201
        # Keep unsaved time_entry ids to display them in flash error
202
        unsaved_time_entry_ids << time_entry.id
203
      end
204
    end
205
    set_flash_from_bulk_time_entry_save(@time_entries, unsaved_time_entry_ids)
206 1295:622f24f53b42 Chris
    redirect_back_or_default project_time_entries_path(@projects.first)
207 441:cbce1fd3b1b7 Chris
  end
208
209 0:513646585e45 Chris
  def destroy
210 1115:433d4f72a19b Chris
    destroyed = TimeEntry.transaction do
211
      @time_entries.each do |t|
212 441:cbce1fd3b1b7 Chris
        unless t.destroy && t.destroyed?
213 1115:433d4f72a19b Chris
          raise ActiveRecord::Rollback
214 441:cbce1fd3b1b7 Chris
        end
215 119:8661b858af72 Chris
      end
216 0:513646585e45 Chris
    end
217 441:cbce1fd3b1b7 Chris
218
    respond_to do |format|
219
      format.html {
220 1115:433d4f72a19b Chris
        if destroyed
221
          flash[:notice] = l(:notice_successful_delete)
222
        else
223
          flash[:error] = l(:notice_unable_delete_time_entry)
224
        end
225 1295:622f24f53b42 Chris
        redirect_back_or_default project_time_entries_path(@projects.first)
226 441:cbce1fd3b1b7 Chris
      }
227 1115:433d4f72a19b Chris
      format.api  {
228
        if destroyed
229
          render_api_ok
230
        else
231
          render_validation_errors(@time_entries)
232
        end
233
      }
234 441:cbce1fd3b1b7 Chris
    end
235 0:513646585e45 Chris
  end
236
237
private
238 37:94944d00e43c chris
  def find_time_entry
239
    @time_entry = TimeEntry.find(params[:id])
240
    unless @time_entry.editable_by?(User.current)
241
      render_403
242
      return false
243
    end
244
    @project = @time_entry.project
245
  rescue ActiveRecord::RecordNotFound
246
    render_404
247
  end
248
249 441:cbce1fd3b1b7 Chris
  def find_time_entries
250
    @time_entries = TimeEntry.find_all_by_id(params[:id] || params[:ids])
251
    raise ActiveRecord::RecordNotFound if @time_entries.empty?
252
    @projects = @time_entries.collect(&:project).compact.uniq
253
    @project = @projects.first if @projects.size == 1
254
  rescue ActiveRecord::RecordNotFound
255
    render_404
256
  end
257
258
  def set_flash_from_bulk_time_entry_save(time_entries, unsaved_time_entry_ids)
259
    if unsaved_time_entry_ids.empty?
260
      flash[:notice] = l(:notice_successful_update) unless time_entries.empty?
261
    else
262
      flash[:error] = l(:notice_failed_to_save_time_entries,
263
                        :count => unsaved_time_entry_ids.size,
264
                        :total => time_entries.size,
265
                        :ids => '#' + unsaved_time_entry_ids.join(', #'))
266
    end
267
  end
268
269 1115:433d4f72a19b Chris
  def find_optional_project_for_new_time_entry
270
    if (project_id = (params[:project_id] || params[:time_entry] && params[:time_entry][:project_id])).present?
271
      @project = Project.find(project_id)
272
    end
273 119:8661b858af72 Chris
    if (issue_id = (params[:issue_id] || params[:time_entry] && params[:time_entry][:issue_id])).present?
274
      @issue = Issue.find(issue_id)
275 1115:433d4f72a19b Chris
      @project ||= @issue.project
276 0:513646585e45 Chris
    end
277
  rescue ActiveRecord::RecordNotFound
278
    render_404
279
  end
280 909:cbb26bc654de Chris
281 1115:433d4f72a19b Chris
  def find_project_for_new_time_entry
282
    find_optional_project_for_new_time_entry
283
    if @project.nil?
284
      render_404
285
    end
286
  end
287
288 0:513646585e45 Chris
  def find_optional_project
289
    if !params[:issue_id].blank?
290
      @issue = Issue.find(params[:issue_id])
291
      @project = @issue.project
292
    elsif !params[:project_id].blank?
293
      @project = Project.find(params[:project_id])
294
    end
295
  end
296 909:cbb26bc654de Chris
297 1295:622f24f53b42 Chris
  # Returns the TimeEntry scope for index and report actions
298
  def time_entry_scope
299
    scope = TimeEntry.visible.where(@query.statement)
300
    if @issue
301
      scope = scope.on_issue(@issue)
302
    elsif @project
303
      scope = scope.on_project(@project, Setting.display_subprojects_issues?)
304 0:513646585e45 Chris
    end
305 1295:622f24f53b42 Chris
    scope
306 0:513646585e45 Chris
  end
307
308 441:cbce1fd3b1b7 Chris
  def parse_params_for_bulk_time_entry_attributes(params)
309
    attributes = (params[:time_entry] || {}).reject {|k,v| v.blank?}
310
    attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'}
311
    attributes[:custom_field_values].reject! {|k,v| v.blank?} if attributes[:custom_field_values]
312
    attributes
313
  end
314 0:513646585e45 Chris
end