Chris@1295: # Redmine - project management software Chris@1295: # Copyright (C) 2006-2013 Jean-Philippe Lang Chris@1295: # Chris@1295: # This program is free software; you can redistribute it and/or Chris@1295: # modify it under the terms of the GNU General Public License Chris@1295: # as published by the Free Software Foundation; either version 2 Chris@1295: # of the License, or (at your option) any later version. Chris@1295: # Chris@1295: # This program is distributed in the hope that it will be useful, Chris@1295: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1295: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1295: # GNU General Public License for more details. Chris@1295: # Chris@1295: # You should have received a copy of the GNU General Public License Chris@1295: # along with this program; if not, write to the Free Software Chris@1295: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1295: Chris@1295: class TimelogController < ApplicationController Chris@1295: menu_item :issues Chris@1295: Chris@1295: before_filter :find_project_for_new_time_entry, :only => [:create] Chris@1295: before_filter :find_time_entry, :only => [:show, :edit, :update] Chris@1295: before_filter :find_time_entries, :only => [:bulk_edit, :bulk_update, :destroy] Chris@1295: before_filter :authorize, :except => [:new, :index, :report] Chris@1295: Chris@1295: before_filter :find_optional_project, :only => [:index, :report] Chris@1295: before_filter :find_optional_project_for_new_time_entry, :only => [:new] Chris@1295: before_filter :authorize_global, :only => [:new, :index, :report] Chris@1295: Chris@1295: accept_rss_auth :index Chris@1295: accept_api_auth :index, :show, :create, :update, :destroy Chris@1295: Chris@1295: rescue_from Query::StatementInvalid, :with => :query_statement_invalid Chris@1295: Chris@1295: helper :sort Chris@1295: include SortHelper Chris@1295: helper :issues Chris@1295: include TimelogHelper Chris@1295: helper :custom_fields Chris@1295: include CustomFieldsHelper Chris@1295: helper :queries Chris@1295: include QueriesHelper Chris@1295: Chris@1295: def index Chris@1295: @query = TimeEntryQuery.build_from_params(params, :project => @project, :name => '_') Chris@1295: scope = time_entry_scope Chris@1295: Chris@1295: sort_init(@query.sort_criteria.empty? ? [['spent_on', 'desc']] : @query.sort_criteria) Chris@1295: sort_update(@query.sortable_columns) Chris@1295: Chris@1295: respond_to do |format| Chris@1295: format.html { Chris@1295: # Paginate results Chris@1295: @entry_count = scope.count Chris@1295: @entry_pages = Paginator.new @entry_count, per_page_option, params['page'] Chris@1295: @entries = scope.all( Chris@1295: :include => [:project, :activity, :user, {:issue => :tracker}], Chris@1295: :order => sort_clause, Chris@1295: :limit => @entry_pages.per_page, Chris@1295: :offset => @entry_pages.offset Chris@1295: ) Chris@1295: @total_hours = scope.sum(:hours).to_f Chris@1295: Chris@1295: render :layout => !request.xhr? Chris@1295: } Chris@1295: format.api { Chris@1295: @entry_count = scope.count Chris@1295: @offset, @limit = api_offset_and_limit Chris@1295: @entries = scope.all( Chris@1295: :include => [:project, :activity, :user, {:issue => :tracker}], Chris@1295: :order => sort_clause, Chris@1295: :limit => @limit, Chris@1295: :offset => @offset Chris@1295: ) Chris@1295: } Chris@1295: format.atom { Chris@1295: entries = scope.all( Chris@1295: :include => [:project, :activity, :user, {:issue => :tracker}], Chris@1295: :order => "#{TimeEntry.table_name}.created_on DESC", Chris@1295: :limit => Setting.feeds_limit.to_i Chris@1295: ) Chris@1295: render_feed(entries, :title => l(:label_spent_time)) Chris@1295: } Chris@1295: format.csv { Chris@1295: # Export all entries Chris@1295: @entries = scope.all( Chris@1295: :include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}], Chris@1295: :order => sort_clause Chris@1295: ) Chris@1295: send_data(query_to_csv(@entries, @query, params), :type => 'text/csv; header=present', :filename => 'timelog.csv') Chris@1295: } Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: def report Chris@1295: @query = TimeEntryQuery.build_from_params(params, :project => @project, :name => '_') Chris@1295: scope = time_entry_scope Chris@1295: Chris@1295: @report = Redmine::Helpers::TimeReport.new(@project, @issue, params[:criteria], params[:columns], scope) Chris@1295: Chris@1295: respond_to do |format| Chris@1295: format.html { render :layout => !request.xhr? } Chris@1295: format.csv { send_data(report_to_csv(@report), :type => 'text/csv; header=present', :filename => 'timelog.csv') } Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: def show Chris@1295: respond_to do |format| Chris@1295: # TODO: Implement html response Chris@1295: format.html { render :nothing => true, :status => 406 } Chris@1295: format.api Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: def new Chris@1295: @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today) Chris@1295: @time_entry.safe_attributes = params[:time_entry] Chris@1295: end Chris@1295: Chris@1295: def create Chris@1295: @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today) Chris@1295: @time_entry.safe_attributes = params[:time_entry] Chris@1295: Chris@1295: call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry }) Chris@1295: Chris@1295: if @time_entry.save Chris@1295: respond_to do |format| Chris@1295: format.html { Chris@1295: flash[:notice] = l(:notice_successful_create) Chris@1295: if params[:continue] Chris@1295: if params[:project_id] Chris@1295: options = { Chris@1295: :time_entry => {:issue_id => @time_entry.issue_id, :activity_id => @time_entry.activity_id}, Chris@1295: :back_url => params[:back_url] Chris@1295: } Chris@1295: if @time_entry.issue Chris@1295: redirect_to new_project_issue_time_entry_path(@time_entry.project, @time_entry.issue, options) Chris@1295: else Chris@1295: redirect_to new_project_time_entry_path(@time_entry.project, options) Chris@1295: end Chris@1295: else Chris@1295: options = { Chris@1295: :time_entry => {:project_id => @time_entry.project_id, :issue_id => @time_entry.issue_id, :activity_id => @time_entry.activity_id}, Chris@1295: :back_url => params[:back_url] Chris@1295: } Chris@1295: redirect_to new_time_entry_path(options) Chris@1295: end Chris@1295: else Chris@1295: redirect_back_or_default project_time_entries_path(@time_entry.project) Chris@1295: end Chris@1295: } Chris@1295: format.api { render :action => 'show', :status => :created, :location => time_entry_url(@time_entry) } Chris@1295: end Chris@1295: else Chris@1295: respond_to do |format| Chris@1295: format.html { render :action => 'new' } Chris@1295: format.api { render_validation_errors(@time_entry) } Chris@1295: end Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: def edit Chris@1295: @time_entry.safe_attributes = params[:time_entry] Chris@1295: end Chris@1295: Chris@1295: def update Chris@1295: @time_entry.safe_attributes = params[:time_entry] Chris@1295: Chris@1295: call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry }) Chris@1295: Chris@1295: if @time_entry.save Chris@1295: respond_to do |format| Chris@1295: format.html { Chris@1295: flash[:notice] = l(:notice_successful_update) Chris@1295: redirect_back_or_default project_time_entries_path(@time_entry.project) Chris@1295: } Chris@1295: format.api { render_api_ok } Chris@1295: end Chris@1295: else Chris@1295: respond_to do |format| Chris@1295: format.html { render :action => 'edit' } Chris@1295: format.api { render_validation_errors(@time_entry) } Chris@1295: end Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: def bulk_edit Chris@1295: @available_activities = TimeEntryActivity.shared.active Chris@1295: @custom_fields = TimeEntry.first.available_custom_fields Chris@1295: end Chris@1295: Chris@1295: def bulk_update Chris@1295: attributes = parse_params_for_bulk_time_entry_attributes(params) Chris@1295: Chris@1295: unsaved_time_entry_ids = [] Chris@1295: @time_entries.each do |time_entry| Chris@1295: time_entry.reload Chris@1295: time_entry.safe_attributes = attributes Chris@1295: call_hook(:controller_time_entries_bulk_edit_before_save, { :params => params, :time_entry => time_entry }) Chris@1295: unless time_entry.save Chris@1295: # Keep unsaved time_entry ids to display them in flash error Chris@1295: unsaved_time_entry_ids << time_entry.id Chris@1295: end Chris@1295: end Chris@1295: set_flash_from_bulk_time_entry_save(@time_entries, unsaved_time_entry_ids) Chris@1295: redirect_back_or_default project_time_entries_path(@projects.first) Chris@1295: end Chris@1295: Chris@1295: def destroy Chris@1295: destroyed = TimeEntry.transaction do Chris@1295: @time_entries.each do |t| Chris@1295: unless t.destroy && t.destroyed? Chris@1295: raise ActiveRecord::Rollback Chris@1295: end Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: respond_to do |format| Chris@1295: format.html { Chris@1295: if destroyed Chris@1295: flash[:notice] = l(:notice_successful_delete) Chris@1295: else Chris@1295: flash[:error] = l(:notice_unable_delete_time_entry) Chris@1295: end Chris@1295: redirect_back_or_default project_time_entries_path(@projects.first) Chris@1295: } Chris@1295: format.api { Chris@1295: if destroyed Chris@1295: render_api_ok Chris@1295: else Chris@1295: render_validation_errors(@time_entries) Chris@1295: end Chris@1295: } Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: private Chris@1295: def find_time_entry Chris@1295: @time_entry = TimeEntry.find(params[:id]) Chris@1295: unless @time_entry.editable_by?(User.current) Chris@1295: render_403 Chris@1295: return false Chris@1295: end Chris@1295: @project = @time_entry.project Chris@1295: rescue ActiveRecord::RecordNotFound Chris@1295: render_404 Chris@1295: end Chris@1295: Chris@1295: def find_time_entries Chris@1295: @time_entries = TimeEntry.find_all_by_id(params[:id] || params[:ids]) Chris@1295: raise ActiveRecord::RecordNotFound if @time_entries.empty? Chris@1295: @projects = @time_entries.collect(&:project).compact.uniq Chris@1295: @project = @projects.first if @projects.size == 1 Chris@1295: rescue ActiveRecord::RecordNotFound Chris@1295: render_404 Chris@1295: end Chris@1295: Chris@1295: def set_flash_from_bulk_time_entry_save(time_entries, unsaved_time_entry_ids) Chris@1295: if unsaved_time_entry_ids.empty? Chris@1295: flash[:notice] = l(:notice_successful_update) unless time_entries.empty? Chris@1295: else Chris@1295: flash[:error] = l(:notice_failed_to_save_time_entries, Chris@1295: :count => unsaved_time_entry_ids.size, Chris@1295: :total => time_entries.size, Chris@1295: :ids => '#' + unsaved_time_entry_ids.join(', #')) Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: def find_optional_project_for_new_time_entry Chris@1295: if (project_id = (params[:project_id] || params[:time_entry] && params[:time_entry][:project_id])).present? Chris@1295: @project = Project.find(project_id) Chris@1295: end Chris@1295: if (issue_id = (params[:issue_id] || params[:time_entry] && params[:time_entry][:issue_id])).present? Chris@1295: @issue = Issue.find(issue_id) Chris@1295: @project ||= @issue.project Chris@1295: end Chris@1295: rescue ActiveRecord::RecordNotFound Chris@1295: render_404 Chris@1295: end Chris@1295: Chris@1295: def find_project_for_new_time_entry Chris@1295: find_optional_project_for_new_time_entry Chris@1295: if @project.nil? Chris@1295: render_404 Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: def find_optional_project Chris@1295: if !params[:issue_id].blank? Chris@1295: @issue = Issue.find(params[:issue_id]) Chris@1295: @project = @issue.project Chris@1295: elsif !params[:project_id].blank? Chris@1295: @project = Project.find(params[:project_id]) Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: # Returns the TimeEntry scope for index and report actions Chris@1295: def time_entry_scope Chris@1295: scope = TimeEntry.visible.where(@query.statement) Chris@1295: if @issue Chris@1295: scope = scope.on_issue(@issue) Chris@1295: elsif @project Chris@1295: scope = scope.on_project(@project, Setting.display_subprojects_issues?) Chris@1295: end Chris@1295: scope Chris@1295: end Chris@1295: Chris@1295: def parse_params_for_bulk_time_entry_attributes(params) Chris@1295: attributes = (params[:time_entry] || {}).reject {|k,v| v.blank?} Chris@1295: attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'} Chris@1295: attributes[:custom_field_values].reject! {|k,v| v.blank?} if attributes[:custom_field_values] Chris@1295: attributes Chris@1295: end Chris@1295: end