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 @ 1535:e2c122809c5c

History | View | Annotate | Download (10.1 KB)

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