You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

timelog_controller.rb 11KB

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