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