a11yist/app/controllers/reports_controller.rb

184 lines
5.6 KiB
Ruby
Raw Permalink Normal View History

2024-09-05 22:54:38 +02:00
# frozen_string_literal: true
class ReportsController < ApplicationController
2024-11-24 22:08:36 +01:00
before_action :set_project, only: %i[index new create]
before_action :set_report, only: %i[show edit update destroy work]
# GET /reports
def index
2024-11-24 22:08:36 +01:00
@reports = @project.reports
end
# GET /reports/1
def show
2024-11-09 00:43:04 +01:00
@page_nav_mode = params[:pnm] == "c" ? :comment : :nav_tree
2024-07-20 16:52:12 +02:00
respond_to do |format|
2024-10-31 23:13:18 +01:00
format.html do
@current_page = page_param && @report.pages.find(page_param) || @report.pages.first
end
2024-07-20 16:52:12 +02:00
format.pdf
format.xlsx do
2024-09-05 22:54:38 +02:00
response.headers["Content-Disposition"] = %(attachment; filename="#{filename(@report, extension: 'xlsx')}")
2024-07-20 16:52:12 +02:00
render
end
2024-07-26 03:14:07 +02:00
format.xml do
2024-09-05 22:54:38 +02:00
response.headers["Content-Disposition"] = %(attachment; filename="#{filename(@report, extension: 'html')}")
render formats: [ :odt ], layout: false
2024-07-26 03:14:07 +02:00
end
format.rtf do
2024-09-05 22:54:38 +02:00
html = render_to_string(template: "reports/show", formats: [ :odt ],
2024-07-26 03:14:07 +02:00
layout: false)
rtf = "{\\rtf1\n#{PandocRuby.html(html).to_rtf}}"
2024-09-05 22:54:38 +02:00
send_data rtf, filename: filename(@report, extension: "rtf")
2024-07-26 03:14:07 +02:00
end
format.odt do
html = render_to_string(layout: false)
odt = PandocRuby.html(html).to_odt
2024-09-05 22:54:38 +02:00
send_data odt, filename: filename(@report, extension: "odt")
2024-07-26 03:14:07 +02:00
end
2024-07-20 16:52:12 +02:00
format.docx do
2024-09-05 22:54:38 +02:00
send_file(Docx::Document.new.generate do |document|
document.heading1(@report.name)
2024-10-31 23:13:18 +01:00
document.paragraph(@report.comment)
2024-09-05 22:54:38 +02:00
# document.heading1("slfkj")
# document.paragraph("sldkfj")
@report.elements.each do |element|
document.heading2(element.title)
element.success_criteria.each do |sc|
document.heading3(sc.title)
document.paragraph(sc.description_html)
end
end
end, filename: filename(@report, extension: "docx"),
type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
return
text = OpenXml::Docx::Elements::Text.new(@report.name)
run = OpenXml::Docx::Elements::Run.new
run.bold = true
run << text
paragraph = OpenXml::Docx::Elements::Paragraph.new
paragraph << run
document = OpenXml::Docx::Package.new
document.document << paragraph
document.save(Rails.root.join("tmp", "output.docx"))
send_file Rails.root.join("tmp", "output.docx")
return
template = Sablon.template(Rails.root.join("lib/templates/docx/report.docx"))
2024-07-20 16:52:12 +02:00
context = {
person: OpenStruct.new(first_name: @report.name),
skills: [],
education: [],
career: [],
referees: []
}
2024-09-05 22:54:38 +02:00
template.render_to_file(Rails.root.join("tmp/output.docx"), context)
send_file Rails.root.join("tmp/output.docx"),
filename: filename(@report, extension: "docx"),
type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
2024-07-20 16:52:12 +02:00
end
end
end
# GET /reports/new
def new
2024-11-24 22:08:36 +01:00
@report = Report.new(project_id: @project.id)
end
# GET /reports/1/edit
2024-09-05 22:54:38 +02:00
def edit; end
# POST /reports
def create
2024-11-24 22:08:36 +01:00
success = if params[:copy_from_id]
original = Report.find(params[:copy_from_id])
@report = original.dup
@report.pages = original.pages.map do |page|
page.dup.tap do |p|
p.elements = page.elements.map do |element|
element.dup.tap do |e|
e.success_criteria = element.success_criteria.map do |sc|
csc = sc.dup
SuccessCriterion.rich_text_association_names
.map { _1.to_s.sub(/^rich_text_/, "") }
.each do |a|
csc.send("#{a}=", sc.send(a).dup)
end
csc
end
e.screenshot = element.screenshot&.dup
Element.rich_text_association_names
.map { _1.to_s.sub(/^rich_text_/, "") }
.each do |a|
e.send("#{a}=", element.send(a).dup)
end
end
end
Page.rich_text_association_names
.map { _1.to_s.sub(/^rich_text_/, "") }
.each do |a|
p.send("#{a}=", page.send(a).dup)
end
end
end
@report.save
else
@report = Report.new(report_params.merge(project_id: @project.id))
@report.save
end
2024-11-24 22:08:36 +01:00
if success
redirect_to @report.reload, notice: "Report was successfully created."
else
render :new, status: :unprocessable_entity
end
end
# PATCH/PUT /reports/1
def update
if @report.update(report_params)
2024-09-05 22:54:38 +02:00
redirect_to @report, notice: "Report was successfully updated.", status: :see_other
else
render :edit, status: :unprocessable_entity
end
end
# DELETE /reports/1
def destroy
@report.destroy!
2024-11-24 22:08:36 +01:00
redirect_to project_url(@report.project), notice: "Report was successfully destroyed.", status: :see_other
end
def work
@report
end
private
# Use callbacks to share common setup or constraints between actions.
def set_report
@report = Report.find(params[:id])
end
# Only allow a list of trusted parameters through.
def report_params
2024-11-24 22:08:36 +01:00
params.require(:report).permit(:name, :comment, :url, :project_id)
end
2024-07-20 16:52:12 +02:00
2024-09-05 22:54:38 +02:00
def filename(report, extension: "html")
2024-07-20 16:52:12 +02:00
"#{report.name}-#{Time.current.strftime('%Y%m%d%H%M')}.#{extension}"
end
2024-10-31 23:13:18 +01:00
def page_param
params[:page_id]
end
2024-11-24 22:08:36 +01:00
def set_project
@project = Project.find(params[:project_id])
end
end