2021-04-18 14:34:55 +03:00
# frozen_string_literal: true
2020-07-27 11:49:23 +03:00
require 'open-uri'
class DashboardAsyncBlocksController < ApplicationController
before_action :find_dashboard
before_action :find_block
helper :additionals_routes
helper :additionals_queries
2022-03-22 12:32:39 +03:00
helper :additionals_settings
2020-07-27 11:49:23 +03:00
helper :queries
helper :issues
helper :activities
helper :dashboards
include DashboardsHelper
2020-10-22 15:56:29 +03:00
# support for redmine_contacts_helpdesk plugin
2021-09-20 13:44:10 +03:00
if AdditionalsPlugin . active_contacts_helpdesk?
2020-10-22 15:56:29 +03:00
include HelpdeskHelper
helper :helpdesk
end
2020-07-29 17:20:42 +03:00
rescue_from Query :: StatementInvalid , with : :query_statement_invalid
rescue_from StandardError , with : :dashboard_with_invalid_block
2020-07-27 11:49:23 +03:00
def show
2020-07-28 19:15:13 +03:00
@settings [ :sort ] = params [ :sort ] if params [ :sort ] . present?
2020-07-27 11:49:23 +03:00
partial_locals = build_dashboard_partial_locals @block , @block_definition , @settings , @dashboard
respond_to do | format |
format . js do
render partial : partial_locals [ :async ] [ :partial ] ,
content_type : 'text/html' ,
locals : partial_locals
end
end
end
2020-07-28 19:15:13 +03:00
# abuse create for query list sort order support
def create
return render_403 if params [ :sort ] . blank?
partial_locals = build_dashboard_partial_locals @block , @block_definition , @settings , @dashboard
partial_locals [ :sort_options ] = { sort : params [ :sort ] }
respond_to do | format |
format . js do
render partial : 'update_order_by' ,
locals : partial_locals
end
end
end
2020-07-27 11:49:23 +03:00
private
def find_dashboard
@dashboard = Dashboard . find params [ :dashboard_id ]
raise :: Unauthorized unless @dashboard . visible?
if @dashboard . dashboard_type == DashboardContentProject :: TYPE_NAME && @dashboard . project . nil?
@dashboard . content_project = find_project_by_project_id
else
@project = @dashboard . project
deny_access if @project . present? && ! User . current . allowed_to? ( :view_project , @project )
end
@can_edit = @dashboard & . editable?
rescue ActiveRecord :: RecordNotFound
render_404
end
def find_block
@block = params [ 'block' ]
@block_definition = @dashboard . content . find_block @block
2020-07-28 22:35:10 +03:00
2020-07-27 11:49:23 +03:00
render_404 if @block . blank?
render_403 if @block_definition . blank?
@settings = @dashboard . layout_settings @block
end
def find_project_by_project_id
begin
@project = Project . find params [ :project_id ]
rescue ActiveRecord :: RecordNotFound
render_404
end
2021-04-18 14:34:55 +03:00
deny_access unless User . current . allowed_to? :view_project , @project
2020-07-28 22:35:10 +03:00
@project
2020-07-27 11:49:23 +03:00
end
2020-07-29 17:20:42 +03:00
def dashboard_with_invalid_block ( exception )
2021-03-25 13:55:06 +03:00
logger & . error " Invalid dashboard block for #{ @block } ( #{ exception . class . name } ): #{ exception . message } "
2020-07-29 17:20:42 +03:00
respond_to do | format |
format . html do
render template : 'dashboards/block_error' , layout : false
end
format . any { head @status }
end
end
2020-07-27 11:49:23 +03:00
end