"Fossies" - the Fresh Open Source Software Archive

Member "redmine-4.1.1/app/controllers/workflows_controller.rb" (6 Apr 2020, 5611 Bytes) of package /linux/www/redmine-4.1.1.tar.gz:


As a special service "Fossies" has tried to format the requested text file into HTML format (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "workflows_controller.rb": 4.1.0_vs_4.1.1.

    1 # frozen_string_literal: true
    2 
    3 # Redmine - project management software
    4 # Copyright (C) 2006-2019  Jean-Philippe Lang
    5 #
    6 # This program is free software; you can redistribute it and/or
    7 # modify it under the terms of the GNU General Public License
    8 # as published by the Free Software Foundation; either version 2
    9 # of the License, or (at your option) any later version.
   10 #
   11 # This program is distributed in the hope that it will be useful,
   12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
   13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14 # GNU General Public License for more details.
   15 #
   16 # You should have received a copy of the GNU General Public License
   17 # along with this program; if not, write to the Free Software
   18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
   19 
   20 class WorkflowsController < ApplicationController
   21   layout 'admin'
   22   self.main_menu = false
   23 
   24   before_action :require_admin
   25 
   26   def index
   27     @roles = Role.sorted.select(&:consider_workflow?)
   28     @trackers = Tracker.sorted
   29     @workflow_counts = WorkflowTransition.group(:tracker_id, :role_id).count
   30   end
   31 
   32   def edit
   33     find_trackers_roles_and_statuses_for_edit
   34 
   35     if request.post? && @roles && @trackers && params[:transitions]
   36       transitions = params[:transitions].deep_dup
   37       transitions.each do |old_status_id, transitions_by_new_status|
   38         transitions_by_new_status.each do |new_status_id, transition_by_rule|
   39           transition_by_rule.reject! {|rule, transition| transition == 'no_change'}
   40         end
   41       end
   42       WorkflowTransition.replace_transitions(@trackers, @roles, transitions)
   43       flash[:notice] = l(:notice_successful_update)
   44       redirect_to_referer_or workflows_edit_path
   45       return
   46     end
   47 
   48     if @trackers && @roles && @statuses.any?
   49       workflows = WorkflowTransition.
   50         where(:role_id => @roles.map(&:id), :tracker_id => @trackers.map(&:id)).
   51         preload(:old_status, :new_status)
   52       @workflows = {}
   53       @workflows['always'] = workflows.select {|w| !w.author && !w.assignee}
   54       @workflows['author'] = workflows.select {|w| w.author}
   55       @workflows['assignee'] = workflows.select {|w| w.assignee}
   56     end
   57   end
   58 
   59   def permissions
   60     find_trackers_roles_and_statuses_for_edit
   61 
   62     if request.post? && @roles && @trackers && params[:permissions]
   63       permissions = params[:permissions].deep_dup
   64       permissions.each { |field, rule_by_status_id|
   65         rule_by_status_id.reject! {|status_id, rule| rule == 'no_change'}
   66       }
   67       WorkflowPermission.replace_permissions(@trackers, @roles, permissions)
   68       flash[:notice] = l(:notice_successful_update)
   69       redirect_to_referer_or workflows_permissions_path
   70       return
   71     end
   72 
   73     if @roles && @trackers
   74       @fields = (Tracker::CORE_FIELDS_ALL - @trackers.map(&:disabled_core_fields).reduce(:&)).map {|field| [field, l("field_"+field.sub(/_id$/, ''))]}
   75       @custom_fields = @trackers.map(&:custom_fields).flatten.uniq.sort
   76       @permissions = WorkflowPermission.rules_by_status_id(@trackers, @roles)
   77       @statuses.each {|status| @permissions[status.id] ||= {}}
   78     end
   79   end
   80 
   81   def copy
   82     @roles = Role.sorted.select(&:consider_workflow?)
   83     @trackers = Tracker.sorted
   84 
   85     if params[:source_tracker_id].blank? || params[:source_tracker_id] == 'any'
   86       @source_tracker = nil
   87     else
   88       @source_tracker = Tracker.find_by_id(params[:source_tracker_id].to_i)
   89     end
   90     if params[:source_role_id].blank? || params[:source_role_id] == 'any'
   91       @source_role = nil
   92     else
   93       @source_role = Role.find_by_id(params[:source_role_id].to_i)
   94     end
   95     @target_trackers = params[:target_tracker_ids].blank? ?
   96         nil : Tracker.where(:id => params[:target_tracker_ids]).to_a
   97     @target_roles = params[:target_role_ids].blank? ?
   98         nil : Role.where(:id => params[:target_role_ids]).to_a
   99     if request.post?
  100       if params[:source_tracker_id].blank? || params[:source_role_id].blank? || (@source_tracker.nil? && @source_role.nil?)
  101         flash.now[:error] = l(:error_workflow_copy_source)
  102       elsif @target_trackers.blank? || @target_roles.blank?
  103         flash.now[:error] = l(:error_workflow_copy_target)
  104       else
  105         WorkflowRule.copy(@source_tracker, @source_role, @target_trackers, @target_roles)
  106         flash[:notice] = l(:notice_successful_update)
  107         redirect_to workflows_copy_path(:source_tracker_id => @source_tracker, :source_role_id => @source_role)
  108       end
  109     end
  110   end
  111 
  112   private
  113 
  114   def find_trackers_roles_and_statuses_for_edit
  115     find_roles
  116     find_trackers
  117     find_statuses
  118   end
  119 
  120   def find_roles
  121     ids = Array.wrap(params[:role_id])
  122     if ids == ['all']
  123       @roles = Role.sorted.select(&:consider_workflow?)
  124     elsif ids.present?
  125       @roles = Role.where(:id => ids).to_a
  126     end
  127     @roles = nil if @roles.blank?
  128   end
  129 
  130   def find_trackers
  131     ids = Array.wrap(params[:tracker_id])
  132     if ids == ['all']
  133       @trackers = Tracker.sorted.to_a
  134     elsif ids.present?
  135       @trackers = Tracker.where(:id => ids).to_a
  136     end
  137     @trackers = nil if @trackers.blank?
  138   end
  139 
  140   def find_statuses
  141     @used_statuses_only = (params[:used_statuses_only] == '0' ? false : true)
  142     if @trackers && @used_statuses_only
  143       role_ids = Role.all.select(&:consider_workflow?).map(&:id)
  144       status_ids = WorkflowTransition.where(
  145         :tracker_id => @trackers.map(&:id), :role_id => role_ids
  146       ).distinct.pluck(:old_status_id, :new_status_id).flatten.uniq
  147       @statuses = IssueStatus.where(:id => status_ids).sorted.to_a.presence
  148     end
  149     @statuses ||= IssueStatus.sorted.to_a
  150   end
  151 end