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