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

timelog_controller.rb 11 kB

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

软件技术创新是当今国家整体创新能力的重要基础和发展引擎。近年来,大众参与的软件创新与创业活动已经成为网络时代软件开发和应用的新形态,正在快速改变着全球软件创新模式和软件产业格局。 ...