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 @ 438:34214e593c67
History | View | Annotate | Download (8.42 KB)
| 1 |
# redMine - project management software
|
|---|---|
| 2 |
# Copyright (C) 2006-2007 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 => [:edit, :update, :destroy] |
| 22 |
before_filter :authorize, :except => [:index] |
| 23 |
before_filter :find_optional_project, :only => [:index] |
| 24 |
|
| 25 |
helper :sort
|
| 26 |
include SortHelper
|
| 27 |
helper :issues
|
| 28 |
include TimelogHelper
|
| 29 |
helper :custom_fields
|
| 30 |
include CustomFieldsHelper
|
| 31 |
|
| 32 |
def index |
| 33 |
sort_init 'spent_on', 'desc' |
| 34 |
sort_update 'spent_on' => 'spent_on', |
| 35 |
'user' => 'user_id', |
| 36 |
'activity' => 'activity_id', |
| 37 |
'project' => "#{Project.table_name}.name", |
| 38 |
'issue' => 'issue_id', |
| 39 |
'hours' => 'hours' |
| 40 |
|
| 41 |
cond = ARCondition.new
|
| 42 |
if @project.nil? |
| 43 |
cond << Project.allowed_to_condition(User.current, :view_time_entries) |
| 44 |
elsif @issue.nil? |
| 45 |
cond << @project.project_condition(Setting.display_subprojects_issues?) |
| 46 |
else
|
| 47 |
cond << "#{Issue.table_name}.root_id = #{@issue.root_id} AND #{Issue.table_name}.lft >= #{@issue.lft} AND #{Issue.table_name}.rgt <= #{@issue.rgt}"
|
| 48 |
end
|
| 49 |
|
| 50 |
retrieve_date_range |
| 51 |
cond << ['spent_on BETWEEN ? AND ?', @from, @to] |
| 52 |
|
| 53 |
TimeEntry.visible_by(User.current) do |
| 54 |
respond_to do |format|
|
| 55 |
format.html {
|
| 56 |
# Paginate results
|
| 57 |
@entry_count = TimeEntry.count(:include => [:project, :issue], :conditions => cond.conditions) |
| 58 |
@entry_pages = Paginator.new self, @entry_count, per_page_option, params['page'] |
| 59 |
@entries = TimeEntry.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.sum(:hours, :include => [:project, :issue], :conditions => cond.conditions).to_f |
| 66 |
|
| 67 |
render :layout => !request.xhr?
|
| 68 |
} |
| 69 |
format.atom {
|
| 70 |
entries = TimeEntry.find(:all, |
| 71 |
:include => [:project, :activity, :user, {:issue => :tracker}], |
| 72 |
:conditions => cond.conditions,
|
| 73 |
:order => "#{TimeEntry.table_name}.created_on DESC", |
| 74 |
:limit => Setting.feeds_limit.to_i) |
| 75 |
render_feed(entries, :title => l(:label_spent_time)) |
| 76 |
} |
| 77 |
format.csv {
|
| 78 |
# Export all entries
|
| 79 |
@entries = TimeEntry.find(:all, |
| 80 |
:include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}], |
| 81 |
:conditions => cond.conditions,
|
| 82 |
:order => sort_clause)
|
| 83 |
send_data(entries_to_csv(@entries), :type => 'text/csv; header=present', :filename => 'timelog.csv') |
| 84 |
} |
| 85 |
end
|
| 86 |
end
|
| 87 |
end
|
| 88 |
|
| 89 |
def new |
| 90 |
@time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today) |
| 91 |
@time_entry.attributes = params[:time_entry] |
| 92 |
|
| 93 |
call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry }) |
| 94 |
render :action => 'edit' |
| 95 |
end
|
| 96 |
|
| 97 |
verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed } |
| 98 |
def create |
| 99 |
@time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today) |
| 100 |
@time_entry.attributes = params[:time_entry] |
| 101 |
|
| 102 |
call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry }) |
| 103 |
|
| 104 |
if @time_entry.save |
| 105 |
flash[:notice] = l(:notice_successful_update) |
| 106 |
redirect_back_or_default :action => 'index', :project_id => @time_entry.project |
| 107 |
else
|
| 108 |
render :action => 'edit' |
| 109 |
end
|
| 110 |
end
|
| 111 |
|
| 112 |
def edit |
| 113 |
@time_entry.attributes = params[:time_entry] |
| 114 |
|
| 115 |
call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry }) |
| 116 |
end
|
| 117 |
|
| 118 |
verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed } |
| 119 |
def update |
| 120 |
@time_entry.attributes = params[:time_entry] |
| 121 |
|
| 122 |
call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry }) |
| 123 |
|
| 124 |
if @time_entry.save |
| 125 |
flash[:notice] = l(:notice_successful_update) |
| 126 |
redirect_back_or_default :action => 'index', :project_id => @time_entry.project |
| 127 |
else
|
| 128 |
render :action => 'edit' |
| 129 |
end
|
| 130 |
end
|
| 131 |
|
| 132 |
verify :method => :delete, :only => :destroy, :render => {:nothing => true, :status => :method_not_allowed } |
| 133 |
def destroy |
| 134 |
if @time_entry.destroy && @time_entry.destroyed? |
| 135 |
flash[:notice] = l(:notice_successful_delete) |
| 136 |
else
|
| 137 |
flash[:error] = l(:notice_unable_delete_time_entry) |
| 138 |
end
|
| 139 |
redirect_to :back
|
| 140 |
rescue ::ActionController::RedirectBackError |
| 141 |
redirect_to :action => 'index', :project_id => @time_entry.project |
| 142 |
end
|
| 143 |
|
| 144 |
private |
| 145 |
def find_time_entry |
| 146 |
@time_entry = TimeEntry.find(params[:id]) |
| 147 |
unless @time_entry.editable_by?(User.current) |
| 148 |
render_403 |
| 149 |
return false |
| 150 |
end
|
| 151 |
@project = @time_entry.project |
| 152 |
rescue ActiveRecord::RecordNotFound |
| 153 |
render_404 |
| 154 |
end
|
| 155 |
|
| 156 |
def find_project |
| 157 |
if params[:issue_id] |
| 158 |
@issue = Issue.find(params[:issue_id]) |
| 159 |
@project = @issue.project |
| 160 |
elsif params[:project_id] |
| 161 |
@project = Project.find(params[:project_id]) |
| 162 |
else
|
| 163 |
render_404 |
| 164 |
return false |
| 165 |
end
|
| 166 |
rescue ActiveRecord::RecordNotFound |
| 167 |
render_404 |
| 168 |
end
|
| 169 |
|
| 170 |
def find_optional_project |
| 171 |
if !params[:issue_id].blank? |
| 172 |
@issue = Issue.find(params[:issue_id]) |
| 173 |
@project = @issue.project |
| 174 |
elsif !params[:project_id].blank? |
| 175 |
@project = Project.find(params[:project_id]) |
| 176 |
end
|
| 177 |
deny_access unless User.current.allowed_to?(:view_time_entries, @project, :global => true) |
| 178 |
end
|
| 179 |
|
| 180 |
# Retrieves the date range based on predefined ranges or specific from/to param dates
|
| 181 |
def retrieve_date_range |
| 182 |
@free_period = false |
| 183 |
@from, @to = nil, nil |
| 184 |
|
| 185 |
if params[:period_type] == '1' || (params[:period_type].nil? && !params[:period].nil?) |
| 186 |
case params[:period].to_s |
| 187 |
when 'today' |
| 188 |
@from = @to = Date.today |
| 189 |
when 'yesterday' |
| 190 |
@from = @to = Date.today - 1 |
| 191 |
when 'current_week' |
| 192 |
@from = Date.today - (Date.today.cwday - 1)%7 |
| 193 |
@to = @from + 6 |
| 194 |
when 'last_week' |
| 195 |
@from = Date.today - 7 - (Date.today.cwday - 1)%7 |
| 196 |
@to = @from + 6 |
| 197 |
when '7_days' |
| 198 |
@from = Date.today - 7 |
| 199 |
@to = Date.today |
| 200 |
when 'current_month' |
| 201 |
@from = Date.civil(Date.today.year, Date.today.month, 1) |
| 202 |
@to = (@from >> 1) - 1 |
| 203 |
when 'last_month' |
| 204 |
@from = Date.civil(Date.today.year, Date.today.month, 1) << 1 |
| 205 |
@to = (@from >> 1) - 1 |
| 206 |
when '30_days' |
| 207 |
@from = Date.today - 30 |
| 208 |
@to = Date.today |
| 209 |
when 'current_year' |
| 210 |
@from = Date.civil(Date.today.year, 1, 1) |
| 211 |
@to = Date.civil(Date.today.year, 12, 31) |
| 212 |
end
|
| 213 |
elsif params[:period_type] == '2' || (params[:period_type].nil? && (!params[:from].nil? || !params[:to].nil?)) |
| 214 |
begin; @from = params[:from].to_s.to_date unless params[:from].blank?; rescue; end |
| 215 |
begin; @to = params[:to].to_s.to_date unless params[:to].blank?; rescue; end |
| 216 |
@free_period = true |
| 217 |
else
|
| 218 |
# default
|
| 219 |
end
|
| 220 |
|
| 221 |
@from, @to = @to, @from if @from && @to && @from > @to |
| 222 |
@from ||= (TimeEntry.earilest_date_for_project(@project) || Date.today) |
| 223 |
@to ||= (TimeEntry.latest_date_for_project(@project) || Date.today) |
| 224 |
end
|
| 225 |
|
| 226 |
end
|