2021-04-18 14:34:55 +03:00
# frozen_string_literal: true
2020-07-27 11:49:23 +03:00
class DashboardsController < ApplicationController
menu_item :dashboards
before_action :find_dashboard , except : % i [ index new create ]
2022-06-05 19:10:05 +03:00
before_action :find_optional_project , only : % i [ index new create ]
2020-07-27 11:49:23 +03:00
2022-03-31 07:09:29 +03:00
accept_atom_auth :index , :show
2020-07-27 11:49:23 +03:00
accept_api_auth :index , :show , :create , :update , :destroy
rescue_from Query :: StatementInvalid , with : :query_statement_invalid
helper :queries
helper :issues
helper :activities
helper :watchers
helper :additionals_routes
helper :dashboards
helper :additionals_issues
helper :additionals_queries
2021-03-25 13:55:06 +03:00
helper :additionals_settings
2020-07-27 11:49:23 +03:00
include AdditionalsRoutesHelper
include AdditionalsQueriesHelper
include QueriesHelper
include WatchersHelper
include SortHelper
def index
case params [ :format ]
when 'xml' , 'json'
@offset , @limit = api_offset_and_limit
else
@limit = per_page_option
end
scope = Dashboard . visible
@query_count = scope . count
@query_pages = Paginator . new @query_count , @limit , params [ 'page' ]
@dashboards = scope . sorted
. limit ( @limit )
. offset ( @offset )
. to_a
respond_to do | format |
format . html { render_error status : 406 }
format . api
end
end
def show
respond_to do | format |
2021-06-21 15:52:57 +03:00
format . html { head :not_acceptable }
2020-07-27 11:49:23 +03:00
format . js if request . xhr?
format . api
end
end
def new
2022-05-30 20:15:10 +03:00
@dashboard = Dashboard . new project : @project ,
author : User . current
2020-07-27 11:49:23 +03:00
@dashboard . dashboard_type = assign_dashboard_type
@allowed_projects = @dashboard . allowed_target_projects
end
2022-10-22 14:12:09 +03:00
def edit
return render_403 unless @dashboard . editable?
@allowed_projects = @dashboard . allowed_target_projects
respond_to do | format |
format . html
format . api
end
end
2020-07-27 11:49:23 +03:00
def create
2021-04-18 14:34:55 +03:00
@dashboard = Dashboard . new author : User . current
2020-07-27 11:49:23 +03:00
@dashboard . safe_attributes = params [ :dashboard ]
@dashboard . dashboard_type = assign_dashboard_type
@dashboard . role_ids = params [ :dashboard ] [ :role_ids ] if params [ :dashboard ] . present?
@allowed_projects = @dashboard . allowed_target_projects
if @dashboard . save
respond_to do | format |
2022-09-09 14:10:21 +03:00
format . html do
flash [ :notice ] = l :notice_successful_create
redirect_to dashboard_link_path ( @project , @dashboard )
end
2022-09-18 15:49:32 +03:00
format . api { render action : :show , status : :created , location : dashboard_url ( @dashboard , project_id : @project ) }
2020-07-27 11:49:23 +03:00
end
else
respond_to do | format |
2022-09-18 15:49:32 +03:00
format . html { render :new }
2021-04-18 14:34:55 +03:00
format . api { render_validation_errors @dashboard }
2020-07-27 11:49:23 +03:00
end
end
end
def update
2022-05-21 00:26:42 +03:00
return render_403 unless @dashboard . editable?
2020-07-27 11:49:23 +03:00
2022-05-30 20:15:10 +03:00
# should be set before dashboar object has modified
@allowed_projects = @dashboard . allowed_target_projects
2020-07-27 11:49:23 +03:00
@dashboard . safe_attributes = params [ :dashboard ]
@dashboard . role_ids = params [ :dashboard ] [ :role_ids ] if params [ :dashboard ] . present?
2020-08-04 21:14:19 +03:00
@project = @dashboard . project if @project && @dashboard . project_id . present? && @dashboard . project != @project
2020-07-27 11:49:23 +03:00
if @dashboard . save
2021-04-18 14:34:55 +03:00
flash [ :notice ] = l :notice_successful_update
2020-07-27 11:49:23 +03:00
respond_to do | format |
2021-04-18 14:34:55 +03:00
format . html { redirect_to dashboard_link_path ( @project , @dashboard ) }
2022-05-02 19:03:48 +03:00
format . api { render_api_ok }
2020-07-27 11:49:23 +03:00
end
else
respond_to do | format |
2022-09-18 15:49:32 +03:00
format . html { render :edit }
2020-07-31 09:35:08 +03:00
format . api { render_validation_errors @dashboard }
2020-07-27 11:49:23 +03:00
end
end
end
def destroy
2022-05-21 00:26:42 +03:00
return render_403 unless @dashboard . deletable?
2020-07-27 11:49:23 +03:00
begin
@dashboard . destroy
2021-04-18 14:34:55 +03:00
flash [ :notice ] = l :notice_successful_delete
2020-07-27 11:49:23 +03:00
respond_to do | format |
format . html { redirect_to @project . nil? ? home_path : project_path ( @project ) }
2022-05-02 19:03:48 +03:00
format . api { render_api_ok }
2020-07-27 11:49:23 +03:00
end
rescue ActiveRecord :: RecordNotDestroyed
2021-04-18 14:34:55 +03:00
flash [ :error ] = l :error_remove_db_entry
2020-07-27 11:49:23 +03:00
redirect_to dashboard_path ( @dashboard )
end
end
def query_statement_invalid ( exception )
logger & . error " Query::StatementInvalid: #{ exception . message } "
2021-04-18 14:34:55 +03:00
session . delete additionals_query_session_key ( 'dashboard' )
2020-07-27 11:49:23 +03:00
render_error l ( :error_query_statement_invalid )
end
def update_layout_setting
block_settings = params [ :settings ] || { }
block_settings . each do | block , settings |
2021-04-18 14:34:55 +03:00
@dashboard . update_block_settings block , settings . to_unsafe_hash
2020-07-27 11:49:23 +03:00
end
@dashboard . save
@updated_blocks = block_settings . keys
end
# The block is added on top of the page
# params[:block] : id of the block to add
def add_block
@block = params [ :block ]
if @dashboard . add_block @block
@dashboard . save
respond_to do | format |
format . html { redirect_to dashboard_link_path ( @project , @dashboard ) }
format . js
end
else
render_error status : 422
end
end
# params[:block] : id of the block to remove
def remove_block
@block = params [ :block ]
@dashboard . remove_block @block
@dashboard . save
respond_to do | format |
format . html { redirect_to dashboard_link_path ( @project , @dashboard ) }
format . js
end
end
# Change blocks order
# params[:group] : group to order (top, left or right)
# params[:blocks] : array of block ids of the group
def order_blocks
@dashboard . order_blocks params [ :group ] , params [ :blocks ]
@dashboard . save
2021-06-21 15:52:57 +03:00
head :ok
2020-07-27 11:49:23 +03:00
end
private
def assign_dashboard_type
if params [ 'dashboard_type' ] . present?
params [ 'dashboard_type' ]
elsif params [ 'dashboard' ] . present? && params [ 'dashboard' ] [ 'dashboard_type' ] . present?
params [ 'dashboard' ] [ 'dashboard_type' ]
elsif @project . nil?
DashboardContentWelcome :: TYPE_NAME
else
DashboardContentProject :: TYPE_NAME
end
end
def find_dashboard
2021-04-18 14:34:55 +03:00
@dashboard = Dashboard . find params [ :id ]
2020-07-27 11:49:23 +03:00
raise :: Unauthorized unless @dashboard . visible?
if @dashboard . dashboard_type == DashboardContentProject :: TYPE_NAME && @dashboard . project . nil?
2021-07-23 18:04:39 +03:00
@dashboard . content_project = if params [ :dashboard ] . present? && params [ :dashboard ] [ :content_project_id ] . present?
find_project params [ :dashboard ] [ :content_project_id ]
else
find_project_by_project_id
end
2020-07-27 11:49:23 +03:00
else
@project = @dashboard . project
end
@can_edit = @dashboard & . editable?
rescue ActiveRecord :: RecordNotFound
render_404
end
end