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

250 lines
6.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, :delete_logo, :delete_deliberation, :delete_notice, :monavis, :update_monavis, :publish_validate, :publish]
2015-10-26 15:54:20 +01:00
def index
if current_administrateur.procedures.count != 0
@procedures = smart_listing_create :procedures,
current_administrateur.procedures.publiees.order(published_at: :desc),
partial: "admin/procedures/list",
array: true
2016-01-07 15:29:29 +01:00
active_class
else
redirect_to new_from_existing_admin_procedures_path
end
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
if @procedure.brouillon?
@procedure_lien = commencer_test_url(path: @procedure.path)
else
@procedure_lien = commencer_url(path: @procedure.path)
end
@procedure.path = @procedure.suggested_path(current_administrateur)
@current_administrateur = current_administrateur
end
def edit
2015-10-26 15:54:20 +01:00
end
def destroy
procedure = current_administrateur.procedures.find(params[:id])
2018-10-01 13:24:37 +02:00
if procedure.publiee_ou_archivee?
return render json: {}, status: 401
end
2019-01-23 16:13:14 +01:00
procedure.reset!
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(for_individual: true)
2015-10-26 15:54:20 +01:00
end
def create
@procedure = Procedure.new(procedure_params.merge(administrateurs: [current_administrateur]))
2015-10-26 15:54:20 +01:00
2018-10-25 17:42:09 +02:00
if !@procedure.save
flash.now.alert = @procedure.errors.full_messages
render 'new'
else
flash.notice = 'Démarche enregistrée.'
current_administrateur.instructeur.assign_to_procedure(@procedure)
2018-11-14 16:28:02 +01:00
2019-01-17 12:04:05 +01:00
redirect_to champs_procedure_path(@procedure)
2015-10-26 15:54:20 +01:00
end
end
def update
@procedure = current_administrateur.procedures.find(params[:id])
2015-10-26 15:54:20 +01:00
if !@procedure.update(procedure_params)
flash.now.alert = @procedure.errors.full_messages
render 'edit'
2019-03-06 14:42:27 +01:00
elsif @procedure.brouillon?
reset_procedure
2018-10-25 17:42:09 +02:00
flash.notice = 'Démarche modifiée. Tous les dossiers de cette démarche ont été supprimés.'
redirect_to edit_admin_procedure_path(id: @procedure.id)
2018-10-25 17:42:09 +02:00
else
flash.notice = 'Démarche modifiée.'
redirect_to edit_admin_procedure_path(id: @procedure.id)
2015-10-26 15:54:20 +01:00
end
end
def publish_validate
@procedure.assign_attributes(publish_params)
end
def publish
@procedure.assign_attributes(publish_params)
@procedure.publish_or_reopen!(current_administrateur)
2016-06-30 10:24:01 +02:00
flash.notice = "Démarche publiée"
redirect_to admin_procedures_path
rescue ActiveRecord::RecordInvalid
render 'publish_validate', formats: :js
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])
procedure.clone(admin, false)
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.valid?
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(:administrateurs, :service)
.where(id: significant_procedure_ids)
.group_by(&:organisation_name)
.sort_by { |_, procedures| procedures.first.created_at }
2019-03-27 18:31:28 +01:00
render layout: 'application'
end
def monavis
end
def update_monavis
if !@procedure.update(procedure_params)
flash.now.alert = @procedure.errors.full_messages
else
flash.notice = 'le champ MonAvis a bien été mis à jour'
end
render 'monavis'
end
def active_class
@active_class = 'active'
end
def archived_class
@archived_class = 'active'
end
def draft_class
@draft_class = 'active'
end
def delete_logo
2019-08-28 13:11:58 +02:00
@procedure.logo.purge_later
flash.notice = 'le logo a bien été supprimé'
redirect_to edit_admin_procedure_path(@procedure)
end
def delete_deliberation
@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.notice.purge_later
2018-09-24 09:49:51 +02:00
flash.notice = 'la notice a bien été supprimée'
redirect_to edit_admin_procedure_path(@procedure)
2018-09-24 09:49:51 +02:00
end
2015-10-26 15:54:20 +01:00
private
def cloned_from_library?
params[:from_new_from_existing].present?
end
def publish_params
params.permit(:path, :lien_site_web)
end
def procedure_params
2019-07-17 15:34:10 +02:00
editable_params = [:libelle, :description, :organisation, :direction, :lien_site_web, :cadre_juridique, :deliberation, :notice, :web_hook_url, :euro_flag, :logo, :auto_archive_on, :monavis_embed]
permited_params = if @procedure&.locked?
2017-03-14 10:45:59 +01:00
params.require(:procedure).permit(*editable_params)
else
params.require(:procedure).permit(*editable_params, :duree_conservation_dossiers_dans_ds, :duree_conservation_dossiers_hors_ds, :for_individual, :path)
end
permited_params
end
2015-10-26 15:54:20 +01:00
end