scinote-web/app/controllers/reports_controller.rb

466 lines
11 KiB
Ruby
Raw Normal View History

2016-02-12 23:52:43 +08:00
class ReportsController < ApplicationController
include TeamsHelper
2017-03-08 21:14:03 +08:00
include ReportActions
2016-02-12 23:52:43 +08:00
# Ignore CSRF protection just for PDF generation (because it's
# used via target='_blank')
2017-03-08 21:14:03 +08:00
protect_from_forgery with: :exception, except: :generate
2016-02-12 23:52:43 +08:00
before_action :load_vars, only: [
:edit,
:update
]
before_action :load_vars_nested, only: [
:index,
:new,
:create,
:edit,
:update,
:generate,
:destroy,
:save_modal,
:project_contents_modal,
:experiment_contents_modal,
2016-02-12 23:52:43 +08:00
:module_contents_modal,
:step_contents_modal,
:result_contents_modal,
2016-02-12 23:52:43 +08:00
:project_contents,
:module_contents,
:step_contents,
:result_contents
2016-02-12 23:52:43 +08:00
]
2017-03-08 21:14:03 +08:00
before_action :check_view_permissions, only: :index
2016-02-12 23:52:43 +08:00
before_action :check_create_permissions, only: [
:new,
:create,
:edit,
:update,
:generate,
:save_modal,
:project_contents_modal,
:experiment_contents_modal,
2016-02-12 23:52:43 +08:00
:module_contents_modal,
:step_contents_modal,
:result_contents_modal,
2016-02-12 23:52:43 +08:00
:project_contents,
:module_contents,
:step_contents,
:result_contents
2016-02-12 23:52:43 +08:00
]
2017-03-08 21:14:03 +08:00
before_action :check_destroy_permissions, only: :destroy
2016-02-12 23:52:43 +08:00
2017-03-08 21:14:03 +08:00
layout 'fluid'
2016-02-12 23:52:43 +08:00
2016-07-21 19:11:15 +08:00
# Index showing all reports of a single project
def index
2016-02-12 23:52:43 +08:00
end
# Report grouped by modules
2016-07-21 19:11:15 +08:00
def new
2016-02-12 23:52:43 +08:00
@report = nil
end
# Creating new report from the _save modal of the new page
def create
continue = true
begin
report_contents = JSON.parse(params.delete(:report_contents))
rescue
continue = false
end
@report = Report.new(report_params)
@report.project = @project
@report.user = current_user
@report.last_modified_by = current_user
2017-03-08 21:14:03 +08:00
if continue && @report.save_with_contents(report_contents)
2016-11-02 16:18:06 +08:00
# record an activity
Activity.create(
type_of: :create_report,
project: @report.project,
user: current_user,
message: I18n.t(
'activities.create_report',
user: current_user.full_name,
report: @report.name
)
)
2016-02-12 23:52:43 +08:00
respond_to do |format|
2017-03-08 21:14:03 +08:00
format.json do
2016-02-12 23:52:43 +08:00
render json: { url: project_reports_path(@project) }, status: :ok
2017-03-08 21:14:03 +08:00
end
2016-02-12 23:52:43 +08:00
end
else
respond_to do |format|
2017-03-08 21:14:03 +08:00
format.json do
2016-02-12 23:52:43 +08:00
render json: @report.errors, status: :unprocessable_entity
2017-03-08 21:14:03 +08:00
end
2016-02-12 23:52:43 +08:00
end
end
end
def edit
2016-07-21 19:11:15 +08:00
# cleans all the deleted report
current_team_switch(@report.project.team)
2016-07-21 19:11:15 +08:00
@report.cleanup_report
2016-10-11 22:46:30 +08:00
render 'reports/new.html.erb'
2016-02-12 23:52:43 +08:00
end
# Updating existing report from the _save modal of the new page
def update
continue = true
begin
report_contents = JSON.parse(params.delete(:report_contents))
rescue
continue = false
end
@report.last_modified_by = current_user
@report.assign_attributes(report_params)
2017-03-08 21:14:03 +08:00
if continue && @report.save_with_contents(report_contents)
2016-11-02 16:18:06 +08:00
# record an activity
Activity.create(
type_of: :edit_report,
project: @report.project,
user: current_user,
message: I18n.t(
'activities.edit_report',
user: current_user.full_name,
report: @report.name
)
)
2016-02-12 23:52:43 +08:00
respond_to do |format|
2017-03-08 21:14:03 +08:00
format.json do
2016-02-12 23:52:43 +08:00
render json: { url: project_reports_path(@project) }, status: :ok
2017-03-08 21:14:03 +08:00
end
2016-02-12 23:52:43 +08:00
end
else
respond_to do |format|
2017-03-08 21:14:03 +08:00
format.json do
2016-02-12 23:52:43 +08:00
render json: @report.errors, status: :unprocessable_entity
2017-03-08 21:14:03 +08:00
end
2016-02-12 23:52:43 +08:00
end
end
end
# Destroy multiple entries action
def destroy
begin
report_ids = JSON.parse(params[:report_ids])
rescue
render_404
end
report_ids.each do |report_id|
report = Report.find_by_id(report_id)
2017-03-08 21:14:03 +08:00
next unless report.present?
# record an activity
Activity.create(
type_of: :delete_report,
project: report.project,
user: current_user,
message: I18n.t(
'activities.delete_report',
user: current_user.full_name,
report: report.name
2016-11-02 16:18:06 +08:00
)
2017-03-08 21:14:03 +08:00
)
report.destroy
2016-02-12 23:52:43 +08:00
end
redirect_to project_reports_path(@project)
end
# Generation action
# Currently, only .PDF is supported
def generate
respond_to do |format|
2017-03-08 21:14:03 +08:00
format.pdf do
2016-02-12 23:52:43 +08:00
@html = params[:html]
2017-03-08 21:14:03 +08:00
@html = '<h1>No content</h1>' if @html.blank?
render pdf: 'report',
2016-02-12 23:52:43 +08:00
header: { right: '[page] of [topage]' },
2017-03-08 21:14:03 +08:00
template: 'reports/report.pdf.erb'
end
2016-02-12 23:52:43 +08:00
end
end
# Modal for saving the existsing/new report
def save_modal
# Assume user is updating existing report
@report = Report.find_by_id(params[:id])
@method = :put
# Case when saving a new report
if @report.blank?
@report = Report.new
@method = :post
@url = project_reports_path(@project, format: :json)
else
@url = project_report_path(@project, @report, format: :json)
end
2017-03-08 21:14:03 +08:00
render_403 and return unless params.include? :contents
2016-02-12 23:52:43 +08:00
@report_contents = params[:contents]
respond_to do |format|
2017-03-08 21:14:03 +08:00
format.json do
2016-02-12 23:52:43 +08:00
render json: {
2017-03-08 21:14:03 +08:00
html: render_to_string(
partial: 'reports/new/modal/save.html.erb'
)
2016-02-12 23:52:43 +08:00
}
2017-03-08 21:14:03 +08:00
end
2016-02-12 23:52:43 +08:00
end
end
# Modal for adding contents into project element
def project_contents_modal
respond_to do |format|
2017-03-08 21:14:03 +08:00
format.json do
2016-02-12 23:52:43 +08:00
render json: {
2017-03-08 21:14:03 +08:00
html: render_to_string(
partial: 'reports/new/modal/project_contents.html.erb',
2016-02-12 23:52:43 +08:00
locals: { project: @project }
2017-03-08 21:14:03 +08:00
)
2016-02-12 23:52:43 +08:00
}
2017-03-08 21:14:03 +08:00
end
2016-02-12 23:52:43 +08:00
end
end
# Experiment for adding contents into experiment element
def experiment_contents_modal
experiment = Experiment.find_by_id(params[:experiment_id])
respond_to do |format|
if experiment.blank?
format.json do
render json: {}, status: :not_found
end
else
format.json do
render json: {
html: render_to_string(
2016-08-05 14:29:49 +08:00
partial: 'reports/new/modal/experiment_contents.html.erb',
locals: { project: @project, experiment: experiment }
)
}
end
end
end
end
2016-02-12 23:52:43 +08:00
# Modal for adding contents into module element
def module_contents_modal
my_module = MyModule.find_by_id(params[:my_module_id])
2016-02-12 23:52:43 +08:00
respond_to do |format|
if my_module.blank?
format.json do
2016-02-12 23:52:43 +08:00
render json: {}, status: :not_found
end
2016-02-12 23:52:43 +08:00
else
format.json do
2016-02-12 23:52:43 +08:00
render json: {
html: render_to_string(
2017-03-08 21:14:03 +08:00
partial: 'reports/new/modal/module_contents.html.erb',
2016-02-12 23:52:43 +08:00
locals: { project: @project, my_module: my_module }
)
2016-02-12 23:52:43 +08:00
}
end
2016-02-12 23:52:43 +08:00
end
end
end
# Modal for adding contents into step element
def step_contents_modal
step = Step.find_by_id(params[:step_id])
respond_to do |format|
if step.blank?
format.json do
render json: {}, status: :not_found
end
else
format.json do
render json: {
html: render_to_string(
partial: 'reports/new/modal/step_contents.html.erb',
locals: { project: @project, step: step }
)
}
end
end
end
end
# Modal for adding contents into result element
def result_contents_modal
result = Result.find_by_id(params[:result_id])
respond_to do |format|
if result.blank?
format.json do
render json: {}, status: :not_found
end
else
format.json do
render json: {
html: render_to_string(
partial: 'reports/new/modal/result_contents.html.erb',
locals: { project: @project, result: result }
)
}
end
end
end
end
2016-02-12 23:52:43 +08:00
def project_contents
respond_to do |format|
elements = generate_project_contents_json
if elements_empty? elements
format.json { render json: {}, status: :no_content }
else
format.json {
render json: {
status: :ok,
elements: elements
}
}
end
end
end
2016-08-02 15:05:11 +08:00
def experiment_contents
experiment = Experiment.find_by_id(params[:id])
2016-08-05 14:29:49 +08:00
modules = (params[:modules].select { |_, p| p == "1" })
.keys
.collect(&:to_i)
2016-08-02 15:05:11 +08:00
respond_to do |format|
if experiment.blank?
format.json { render json: {}, status: :not_found }
elsif modules.blank?
format.json { render json: {}, status: :no_content }
else
elements = generate_experiment_contents_json(experiment, modules)
end
2016-08-02 15:05:11 +08:00
if elements_empty? elements
format.json { render json: {}, status: :no_content }
else
2016-08-05 14:29:49 +08:00
format.json do
2016-08-02 15:05:11 +08:00
render json: {
status: :ok,
elements: elements
}
2016-08-05 14:29:49 +08:00
end
2016-08-02 15:05:11 +08:00
end
end
end
2016-02-12 23:52:43 +08:00
def module_contents
my_module = MyModule.find_by_id(params[:id])
respond_to do |format|
if my_module.blank?
format.json { render json: {}, status: :not_found }
else
elements = generate_module_contents_json(my_module)
if elements_empty? elements
format.json { render json: {}, status: :no_content }
else
2017-03-08 21:14:03 +08:00
format.json do
2016-02-12 23:52:43 +08:00
render json: {
status: :ok,
elements: elements
}
2017-03-08 21:14:03 +08:00
end
2016-02-12 23:52:43 +08:00
end
end
end
end
def step_contents
step = Step.find_by_id(params[:id])
respond_to do |format|
if step.blank?
format.json { render json: {}, status: :not_found }
else
elements = generate_step_contents_json(step)
if elements_empty? elements
format.json { render json: {}, status: :no_content }
else
format.json {
render json: {
status: :ok,
elements: elements
}
}
end
end
end
end
def result_contents
result = Result.find_by_id(params[:id])
respond_to do |format|
if result.blank?
format.json { render json: {}, status: :not_found }
else
elements = generate_result_contents_json(result)
if elements_empty? elements
format.json { render json: {}, status: :no_content }
else
format.json {
render json: {
status: :ok,
elements: elements
}
}
end
end
end
end
2016-02-12 23:52:43 +08:00
private
def load_vars
@report = Report.find_by_id(params[:id])
2017-03-08 21:14:03 +08:00
render_404 unless @report
2016-02-12 23:52:43 +08:00
end
def load_vars_nested
@project = Project.find_by_id(params[:project_id])
2017-03-08 21:14:03 +08:00
render_404 unless @project
2016-02-12 23:52:43 +08:00
end
def check_view_permissions
2017-03-08 21:14:03 +08:00
render_403 unless can_view_reports(@project)
2016-02-12 23:52:43 +08:00
end
def check_create_permissions
2017-03-08 21:14:03 +08:00
render_403 unless can_create_new_report(@project)
2016-02-12 23:52:43 +08:00
end
def check_destroy_permissions
2017-03-08 21:14:03 +08:00
render_403 unless can_delete_reports(@project)
render_404 unless params.include? :report_ids
2016-02-12 23:52:43 +08:00
end
def report_params
2017-03-08 21:14:03 +08:00
params.require(:report)
.permit(:name, :description, :grouped_by, :report_contents)
2016-02-12 23:52:43 +08:00
end
2016-07-21 19:11:15 +08:00
end