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