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 @ 441:cbce1fd3b1b7

History | View | Annotate | Download (12.1 KB)

1 119:8661b858af72 Chris
# Redmine - project management software
2
# Copyright (C) 2006-2010  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
#
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 TimelogController < ApplicationController
19
  menu_item :issues
20 37:94944d00e43c chris
  before_filter :find_project, :only => [:new, :create]
21 441:cbce1fd3b1b7 Chris
  before_filter :find_time_entry, :only => [:show, :edit, :update]
22
  before_filter :find_time_entries, :only => [:bulk_edit, :bulk_update, :destroy]
23 37:94944d00e43c chris
  before_filter :authorize, :except => [:index]
24
  before_filter :find_optional_project, :only => [:index]
25 119:8661b858af72 Chris
  accept_key_auth :index, :show, :create, :update, :destroy
26
27 0:513646585e45 Chris
  helper :sort
28
  include SortHelper
29
  helper :issues
30
  include TimelogHelper
31
  helper :custom_fields
32
  include CustomFieldsHelper
33
34 37:94944d00e43c chris
  def index
35 0:513646585e45 Chris
    sort_init 'spent_on', 'desc'
36
    sort_update 'spent_on' => 'spent_on',
37
                'user' => 'user_id',
38
                'activity' => 'activity_id',
39
                'project' => "#{Project.table_name}.name",
40
                'issue' => 'issue_id',
41
                'hours' => 'hours'
42
43
    cond = ARCondition.new
44 441:cbce1fd3b1b7 Chris
    if @issue
45
      cond << "#{Issue.table_name}.root_id = #{@issue.root_id} AND #{Issue.table_name}.lft >= #{@issue.lft} AND #{Issue.table_name}.rgt <= #{@issue.rgt}"
46
    elsif @project
47 0:513646585e45 Chris
      cond << @project.project_condition(Setting.display_subprojects_issues?)
48
    end
49
50
    retrieve_date_range
51
    cond << ['spent_on BETWEEN ? AND ?', @from, @to]
52
53 441:cbce1fd3b1b7 Chris
    respond_to do |format|
54
      format.html {
55
        # Paginate results
56
        @entry_count = TimeEntry.visible.count(:include => [:project, :issue], :conditions => cond.conditions)
57
        @entry_pages = Paginator.new self, @entry_count, per_page_option, params['page']
58
        @entries = TimeEntry.visible.find(:all,
59
                                  :include => [:project, :activity, :user, {:issue => :tracker}],
60
                                  :conditions => cond.conditions,
61
                                  :order => sort_clause,
62
                                  :limit  =>  @entry_pages.items_per_page,
63
                                  :offset =>  @entry_pages.current.offset)
64
        @total_hours = TimeEntry.visible.sum(:hours, :include => [:project, :issue], :conditions => cond.conditions).to_f
65 0:513646585e45 Chris
66 441:cbce1fd3b1b7 Chris
        render :layout => !request.xhr?
67
      }
68
      format.api  {
69
        @entry_count = TimeEntry.visible.count(:include => [:project, :issue], :conditions => cond.conditions)
70
        @offset, @limit = api_offset_and_limit
71
        @entries = TimeEntry.visible.find(:all,
72
                                  :include => [:project, :activity, :user, {:issue => :tracker}],
73
                                  :conditions => cond.conditions,
74
                                  :order => sort_clause,
75
                                  :limit  => @limit,
76
                                  :offset => @offset)
77
      }
78
      format.atom {
79
        entries = TimeEntry.visible.find(:all,
80
                                 :include => [:project, :activity, :user, {:issue => :tracker}],
81
                                 :conditions => cond.conditions,
82
                                 :order => "#{TimeEntry.table_name}.created_on DESC",
83
                                 :limit => Setting.feeds_limit.to_i)
84
        render_feed(entries, :title => l(:label_spent_time))
85
      }
86
      format.csv {
87
        # Export all entries
88
        @entries = TimeEntry.visible.find(:all,
89
                                  :include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}],
90
                                  :conditions => cond.conditions,
91
                                  :order => sort_clause)
92
        send_data(entries_to_csv(@entries), :type => 'text/csv; header=present', :filename => 'timelog.csv')
93
      }
94 0:513646585e45 Chris
    end
95
  end
96 119:8661b858af72 Chris
97
  def show
98
    respond_to do |format|
99
      # TODO: Implement html response
100
      format.html { render :nothing => true, :status => 406 }
101
      format.api
102
    end
103
  end
104 37:94944d00e43c chris
105
  def new
106
    @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today)
107
    @time_entry.attributes = params[:time_entry]
108
109
    call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
110
    render :action => 'edit'
111
  end
112
113
  verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed }
114
  def create
115 0:513646585e45 Chris
    @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today)
116
    @time_entry.attributes = params[:time_entry]
117
118
    call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
119
120 37:94944d00e43c chris
    if @time_entry.save
121 119:8661b858af72 Chris
      respond_to do |format|
122
        format.html {
123
          flash[:notice] = l(:notice_successful_update)
124
          redirect_back_or_default :action => 'index', :project_id => @time_entry.project
125
        }
126
        format.api  { render :action => 'show', :status => :created, :location => time_entry_url(@time_entry) }
127
      end
128 37:94944d00e43c chris
    else
129 119:8661b858af72 Chris
      respond_to do |format|
130
        format.html { render :action => 'edit' }
131
        format.api  { render_validation_errors(@time_entry) }
132
      end
133 0:513646585e45 Chris
    end
134
  end
135
136 37:94944d00e43c chris
  def edit
137
    @time_entry.attributes = params[:time_entry]
138
139
    call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
140
  end
141
142
  verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
143
  def update
144
    @time_entry.attributes = params[:time_entry]
145
146
    call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
147
148
    if @time_entry.save
149 119:8661b858af72 Chris
      respond_to do |format|
150
        format.html {
151
          flash[:notice] = l(:notice_successful_update)
152
          redirect_back_or_default :action => 'index', :project_id => @time_entry.project
153
        }
154
        format.api  { head :ok }
155
      end
156 37:94944d00e43c chris
    else
157 119:8661b858af72 Chris
      respond_to do |format|
158
        format.html { render :action => 'edit' }
159
        format.api  { render_validation_errors(@time_entry) }
160
      end
161 37:94944d00e43c chris
    end
162
  end
163
164 441:cbce1fd3b1b7 Chris
  def bulk_edit
165
    @available_activities = TimeEntryActivity.shared.active
166
    @custom_fields = TimeEntry.first.available_custom_fields
167
  end
168
169
  def bulk_update
170
    attributes = parse_params_for_bulk_time_entry_attributes(params)
171
172
    unsaved_time_entry_ids = []
173
    @time_entries.each do |time_entry|
174
      time_entry.reload
175
      time_entry.attributes = attributes
176
      call_hook(:controller_time_entries_bulk_edit_before_save, { :params => params, :time_entry => time_entry })
177
      unless time_entry.save
178
        # Keep unsaved time_entry ids to display them in flash error
179
        unsaved_time_entry_ids << time_entry.id
180
      end
181
    end
182
    set_flash_from_bulk_time_entry_save(@time_entries, unsaved_time_entry_ids)
183
    redirect_back_or_default({:controller => 'timelog', :action => 'index', :project_id => @projects.first})
184
  end
185
186 37:94944d00e43c chris
  verify :method => :delete, :only => :destroy, :render => {:nothing => true, :status => :method_not_allowed }
187 0:513646585e45 Chris
  def destroy
188 441:cbce1fd3b1b7 Chris
    @time_entries.each do |t|
189
      begin
190
        unless t.destroy && t.destroyed?
191
          respond_to do |format|
192
            format.html {
193
              flash[:error] = l(:notice_unable_delete_time_entry)
194
              redirect_to :back
195
            }
196
            format.api  { render_validation_errors(t) }
197
          end
198
          return
199
        end
200
      rescue ::ActionController::RedirectBackError
201
        redirect_to :action => 'index', :project_id => @projects.first
202
        return
203 119:8661b858af72 Chris
      end
204 0:513646585e45 Chris
    end
205 441:cbce1fd3b1b7 Chris
206
    respond_to do |format|
207
      format.html {
208
        flash[:notice] = l(:notice_successful_delete)
209
        redirect_back_or_default(:action => 'index', :project_id => @projects.first)
210
      }
211
      format.api  { head :ok }
212
    end
213 0:513646585e45 Chris
  end
214
215
private
216 37:94944d00e43c chris
  def find_time_entry
217
    @time_entry = TimeEntry.find(params[:id])
218
    unless @time_entry.editable_by?(User.current)
219
      render_403
220
      return false
221
    end
222
    @project = @time_entry.project
223
  rescue ActiveRecord::RecordNotFound
224
    render_404
225
  end
226
227 441:cbce1fd3b1b7 Chris
  def find_time_entries
228
    @time_entries = TimeEntry.find_all_by_id(params[:id] || params[:ids])
229
    raise ActiveRecord::RecordNotFound if @time_entries.empty?
230
    @projects = @time_entries.collect(&:project).compact.uniq
231
    @project = @projects.first if @projects.size == 1
232
  rescue ActiveRecord::RecordNotFound
233
    render_404
234
  end
235
236
  def set_flash_from_bulk_time_entry_save(time_entries, unsaved_time_entry_ids)
237
    if unsaved_time_entry_ids.empty?
238
      flash[:notice] = l(:notice_successful_update) unless time_entries.empty?
239
    else
240
      flash[:error] = l(:notice_failed_to_save_time_entries,
241
                        :count => unsaved_time_entry_ids.size,
242
                        :total => time_entries.size,
243
                        :ids => '#' + unsaved_time_entry_ids.join(', #'))
244
    end
245
  end
246
247 0:513646585e45 Chris
  def find_project
248 119:8661b858af72 Chris
    if (issue_id = (params[:issue_id] || params[:time_entry] && params[:time_entry][:issue_id])).present?
249
      @issue = Issue.find(issue_id)
250 0:513646585e45 Chris
      @project = @issue.project
251 119:8661b858af72 Chris
    elsif (project_id = (params[:project_id] || params[:time_entry] && params[:time_entry][:project_id])).present?
252
      @project = Project.find(project_id)
253 0:513646585e45 Chris
    else
254
      render_404
255
      return false
256
    end
257
  rescue ActiveRecord::RecordNotFound
258
    render_404
259
  end
260
261
  def find_optional_project
262
    if !params[:issue_id].blank?
263
      @issue = Issue.find(params[:issue_id])
264
      @project = @issue.project
265
    elsif !params[:project_id].blank?
266
      @project = Project.find(params[:project_id])
267
    end
268
    deny_access unless User.current.allowed_to?(:view_time_entries, @project, :global => true)
269
  end
270
271
  # Retrieves the date range based on predefined ranges or specific from/to param dates
272
  def retrieve_date_range
273
    @free_period = false
274
    @from, @to = nil, nil
275
276
    if params[:period_type] == '1' || (params[:period_type].nil? && !params[:period].nil?)
277
      case params[:period].to_s
278
      when 'today'
279
        @from = @to = Date.today
280
      when 'yesterday'
281
        @from = @to = Date.today - 1
282
      when 'current_week'
283
        @from = Date.today - (Date.today.cwday - 1)%7
284
        @to = @from + 6
285
      when 'last_week'
286
        @from = Date.today - 7 - (Date.today.cwday - 1)%7
287
        @to = @from + 6
288
      when '7_days'
289
        @from = Date.today - 7
290
        @to = Date.today
291
      when 'current_month'
292
        @from = Date.civil(Date.today.year, Date.today.month, 1)
293
        @to = (@from >> 1) - 1
294
      when 'last_month'
295
        @from = Date.civil(Date.today.year, Date.today.month, 1) << 1
296
        @to = (@from >> 1) - 1
297
      when '30_days'
298
        @from = Date.today - 30
299
        @to = Date.today
300
      when 'current_year'
301
        @from = Date.civil(Date.today.year, 1, 1)
302
        @to = Date.civil(Date.today.year, 12, 31)
303
      end
304
    elsif params[:period_type] == '2' || (params[:period_type].nil? && (!params[:from].nil? || !params[:to].nil?))
305
      begin; @from = params[:from].to_s.to_date unless params[:from].blank?; rescue; end
306
      begin; @to = params[:to].to_s.to_date unless params[:to].blank?; rescue; end
307
      @free_period = true
308
    else
309
      # default
310
    end
311
312
    @from, @to = @to, @from if @from && @to && @from > @to
313 22:40f7cfd4df19 chris
    @from ||= (TimeEntry.earilest_date_for_project(@project) || Date.today)
314
    @to   ||= (TimeEntry.latest_date_for_project(@project) || Date.today)
315 0:513646585e45 Chris
  end
316
317 441:cbce1fd3b1b7 Chris
  def parse_params_for_bulk_time_entry_attributes(params)
318
    attributes = (params[:time_entry] || {}).reject {|k,v| v.blank?}
319
    attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'}
320
    attributes[:custom_field_values].reject! {|k,v| v.blank?} if attributes[:custom_field_values]
321
    attributes
322
  end
323 0:513646585e45 Chris
end