annotate app/controllers/timelog_controller.rb @ 904:0a8317a50fa0 redmine-1.1

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