demarches-normaliennes/app/controllers/admin/procedures_controller.rb

295 lines
8.7 KiB
Ruby
Raw Normal View History

class Admin::ProceduresController < AdminController
2016-01-07 15:29:29 +01:00
include SmartListing::Helper::ControllerExtensions
helper SmartListing::Helper
2015-10-26 15:54:20 +01:00
before_action :retrieve_procedure, only: [:show, :edit]
2015-10-26 15:54:20 +01:00
def index
2016-01-07 15:29:29 +01:00
@procedures = smart_listing_create :procedures,
current_administrateur.procedures.publiees.order(published_at: :desc),
2017-06-12 13:49:51 +02:00
partial: "admin/procedures/list",
array: true
2016-01-07 15:29:29 +01:00
active_class
2015-10-26 15:54:20 +01:00
end
def archived
2016-01-07 15:29:29 +01:00
@procedures = smart_listing_create :procedures,
current_administrateur.procedures.archivees.order(published_at: :desc),
2017-06-12 13:49:51 +02:00
partial: "admin/procedures/list",
array: true
2016-01-07 15:29:29 +01:00
archived_class
2016-01-07 15:29:29 +01:00
render 'index'
end
def draft
@procedures = smart_listing_create :procedures,
2017-07-11 15:16:06 +02:00
current_administrateur.procedures.brouillons.order(created_at: :desc),
2017-06-12 13:49:51 +02:00
partial: "admin/procedures/list",
array: true
draft_class
render 'index'
end
2015-10-26 15:54:20 +01:00
def show
end
def edit
@path = @procedure.path || @procedure.default_path
@available = @procedure.path_available?(@path)
@mine = @procedure.path_is_mine?(@path)
2015-10-26 15:54:20 +01:00
end
2017-06-28 18:18:51 +02:00
def hide
procedure = current_administrateur.procedures.find(params[:id])
2017-06-28 18:18:51 +02:00
procedure.hide!
2018-09-18 12:06:27 +02:00
flash.notice = "Démarche supprimée, en cas d'erreur #{helpers.contact_link('contactez nous', tags: 'démarche supprimée')}"
2017-06-28 18:18:51 +02:00
redirect_to admin_procedures_draft_path
end
def destroy
procedure = current_administrateur.procedures.find(params[:id])
return render json: {}, status: 401 if procedure.publiee_ou_archivee?
procedure.destroy
2018-09-05 14:48:42 +02:00
flash.notice = 'Démarche supprimée'
redirect_to admin_procedures_draft_path
end
2015-10-26 15:54:20 +01:00
def new
@procedure ||= Procedure.new
@procedure.module_api_carto ||= ModuleAPICarto.new
@available = true
@mine = true
2015-10-26 15:54:20 +01:00
end
def create
@procedure = Procedure.new(procedure_params)
@procedure.module_api_carto = ModuleAPICarto.new(create_module_api_carto_params) if @procedure.valid?
@path = params.require(:procedure).permit(:path)[:path]
@available = !ProcedurePath.exists?(path: @path)
@mine = ProcedurePath.mine?(current_administrateur, @path)
2015-10-26 15:54:20 +01:00
if !@procedure.validate
flash.now.alert = @procedure.errors.full_messages
2015-10-26 15:54:20 +01:00
return render 'new'
elsif Flipflop.publish_draft? && !ProcedurePath.valid?(Procedure.last, @path)
# FIXME: The code abow is a horrible hack that we need until we migrated path directly on procedure model
flash.now.alert = 'Lien de la démarche invalide.'
return render 'new'
else
@procedure.save!
if Flipflop.publish_draft?
@procedure.publish_with_path!(@path)
end
flash.notice = 'Démarche enregistrée.'
2015-10-26 15:54:20 +01:00
end
2015-11-26 18:41:41 +01:00
redirect_to admin_procedure_types_de_champ_path(procedure_id: @procedure.id)
2015-10-26 15:54:20 +01:00
end
def update
@procedure = current_administrateur.procedures.find(params[:id])
path = params.require(:procedure).permit(:path)[:path]
2015-10-26 15:54:20 +01:00
if !@procedure.update(procedure_params)
flash.alert = @procedure.errors.full_messages
elsif Flipflop.publish_draft? && @procedure.brouillon?
if ProcedurePath.valid?(@procedure, path)
@procedure.publish_with_path!(path)
reset_procedure
flash.notice = 'Démarche modifiée. Tous les brouillons de cette démarche ont été supprimés.'
else
flash.alert = 'Lien de la démarche invalide.'
end
else
reset_procedure
flash.notice = 'Démarche modifiée. Tous les brouillons de cette démarche ont été supprimés.'
2015-10-26 15:54:20 +01:00
end
redirect_to edit_admin_procedure_path(id: @procedure.id)
2015-10-26 15:54:20 +01:00
end
def publish
2016-06-30 10:24:01 +02:00
procedure = current_administrateur.procedures.find(params[:procedure_id])
2018-09-07 18:39:41 +02:00
if !ProcedurePath.valid?(procedure, params[:procedure_path])
2018-09-05 14:48:42 +02:00
flash.alert = 'Lien de la démarche invalide'
return redirect_to admin_procedures_path
end
2018-09-07 18:42:12 +02:00
if procedure.publish_or_reopen!(params[:procedure_path])
2018-09-05 14:48:42 +02:00
flash.notice = "Démarche publiée"
redirect_to admin_procedures_path
else
@mine = false
render '/admin/procedures/publish', formats: 'js'
end
rescue ActiveRecord::RecordNotFound
2018-09-05 14:48:42 +02:00
flash.alert = 'Démarche inexistante'
redirect_to admin_procedures_path
end
2015-11-26 18:41:41 +01:00
def transfer
2018-03-06 13:44:29 +01:00
admin = Administrateur.find_by(email: params[:email_admin].downcase)
2016-07-22 12:25:23 +02:00
if admin.nil?
render '/admin/procedures/transfer', formats: 'js', status: 404
else
procedure = current_administrateur.procedures.find(params[:procedure_id])
clone_procedure = procedure.clone(admin, false)
2016-07-22 12:25:23 +02:00
clone_procedure.save
2018-09-05 14:48:42 +02:00
flash.now.notice = "La démarche a correctement été clonée vers le nouvel administrateur."
render '/admin/procedures/transfer', formats: 'js', status: 200
end
end
def archive
2016-06-30 10:24:01 +02:00
procedure = current_administrateur.procedures.find(params[:procedure_id])
procedure.archive!
2018-09-05 14:48:42 +02:00
flash.notice = "Démarche archivée"
redirect_to admin_procedures_path
rescue ActiveRecord::RecordNotFound
2018-09-05 14:48:42 +02:00
flash.alert = 'Démarche inexistante'
redirect_to admin_procedures_path
2015-11-26 18:41:41 +01:00
end
2016-06-15 11:34:05 +02:00
def clone
procedure = Procedure.find(params[:procedure_id])
new_procedure = procedure.clone(current_administrateur, cloned_from_library?)
if new_procedure.save
2018-09-05 14:48:42 +02:00
flash.notice = 'Démarche clonée'
2016-06-15 11:34:05 +02:00
redirect_to edit_admin_procedure_path(id: new_procedure.id)
else
if cloned_from_library?
flash.alert = new_procedure.errors.full_messages
redirect_to new_from_existing_admin_procedures_path
else
flash.alert = new_procedure.errors.full_messages
redirect_to admin_procedures_path
end
2016-06-15 11:34:05 +02:00
end
rescue ActiveRecord::RecordNotFound
2018-09-05 14:48:42 +02:00
flash.alert = 'Démarche inexistante'
2016-06-15 11:34:05 +02:00
redirect_to admin_procedures_path
end
SIGNIFICANT_DOSSIERS_THRESHOLD = 30
def new_from_existing
significant_procedure_ids = Procedure
.publiees_ou_archivees
.joins(:dossiers)
.group("procedures.id")
.having("count(dossiers.id) >= ?", SIGNIFICANT_DOSSIERS_THRESHOLD)
.pluck('procedures.id')
@grouped_procedures = Procedure
.includes(:administrateur, :service)
.where(id: significant_procedure_ids)
.group_by(&:organisation_name)
.sort_by { |_, procedures| procedures.first.created_at }
end
def active_class
@active_class = 'active'
end
def archived_class
@archived_class = 'active'
end
def draft_class
@draft_class = 'active'
end
def path_list
json_path_list = ProcedurePath
2018-09-07 18:28:27 +02:00
.find_with_path(params[:request])
2018-09-13 18:59:51 +02:00
.pluck('procedure_paths.path', :administrateur_id)
2018-05-22 16:24:24 +02:00
.map do |path, administrateur_id|
{
2018-05-22 16:24:24 +02:00
label: path,
mine: administrateur_id == current_administrateur.id
}
end.to_json
render json: json_path_list
end
2018-09-04 14:53:18 +02:00
def check_availability
path = params[:procedure][:path]
procedure_id = params[:procedure][:id]
if procedure_id.present?
procedure = current_administrateur.procedures.find(procedure_id)
@available = procedure.path_available?(path)
@mine = procedure.path_is_mine?(path)
else
@available = !ProcedurePath.exists?(path: path)
@mine = ProcedurePath.mine?(current_administrateur, path)
end
end
def delete_logo
procedure = Procedure.find(params[:id])
procedure.remove_logo!
procedure.save
flash.notice = 'le logo a bien été supprimé'
redirect_to edit_admin_procedure_path(procedure)
end
def delete_deliberation
procedure = Procedure.find(params[:id])
procedure.deliberation.purge_later
flash.notice = 'la délibération a bien été supprimée'
redirect_to edit_admin_procedure_path(procedure)
end
2018-09-24 09:49:51 +02:00
def delete_notice
procedure = Procedure.find(params[:id])
procedure.notice.purge_later
flash.notice = 'la notice a bien été supprimée'
redirect_to edit_admin_procedure_path(procedure)
end
2015-10-26 15:54:20 +01:00
private
def cloned_from_library?
params[:from_new_from_existing].present?
end
def procedure_params
2018-05-31 10:59:38 +02:00
editable_params = [:libelle, :description, :organisation, :direction, :lien_site_web, :cadre_juridique, :deliberation, :notice, :web_hook_url, :euro_flag, :logo, :auto_archive_on]
2018-05-30 18:45:46 +02:00
if @procedure&.locked?
2017-03-14 10:45:59 +01:00
params.require(:procedure).permit(*editable_params)
else
2018-04-24 16:02:36 +02:00
params.require(:procedure).permit(*editable_params, :duree_conservation_dossiers_dans_ds, :duree_conservation_dossiers_hors_ds, :lien_demarche, :for_individual, :individual_with_siret, :ask_birthday, module_api_carto_attributes: [:id, :use_api_carto, :quartiers_prioritaires, :cadastre]).merge(administrateur_id: current_administrateur.id)
end
end
def create_module_api_carto_params
params.require(:procedure).require(:module_api_carto_attributes).permit(:id, :use_api_carto, :quartiers_prioritaires, :cadastre)
2015-10-26 15:54:20 +01:00
end
end