annotate app/controllers/timelog_controller.rb @ 872:9d7526c3a78a feature_124

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