annotate app/controllers/timelog_controller.rb @ 859:9a16b2c1fd56 bug_189

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