annotate .svn/pristine/ef/efdcfd2d94fd0bcb04b7cf4a72bebfe621b1194f.svn-base @ 1478:5ca1f4a47171 bibplugin_db_migrations

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