2023-06-14 09:13:33 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-02-09 21:33:12 +08:00
|
|
|
module Users
|
|
|
|
module Settings
|
|
|
|
class TeamsController < ApplicationController
|
2019-11-12 18:26:18 +08:00
|
|
|
include ActionView::Helpers::TextHelper
|
|
|
|
include ActionView::Helpers::UrlHelper
|
|
|
|
include ApplicationHelper
|
|
|
|
include InputSanitizeHelper
|
|
|
|
|
2023-06-14 09:13:33 +08:00
|
|
|
before_action :load_user, only: %i(
|
|
|
|
index
|
|
|
|
datatable
|
|
|
|
new
|
|
|
|
create
|
|
|
|
show
|
|
|
|
users_datatable
|
|
|
|
)
|
|
|
|
|
|
|
|
before_action :load_team, only: %i(
|
|
|
|
show
|
|
|
|
users_datatable
|
|
|
|
name_html
|
|
|
|
description_html
|
|
|
|
update
|
|
|
|
destroy
|
|
|
|
)
|
2017-02-09 21:33:12 +08:00
|
|
|
|
2018-01-09 22:28:27 +08:00
|
|
|
before_action :check_create_team_permission,
|
|
|
|
only: %i(new create)
|
|
|
|
|
2023-06-14 09:13:33 +08:00
|
|
|
before_action :set_breadcrumbs_items, only: %i(index show)
|
|
|
|
|
2018-04-05 22:30:02 +08:00
|
|
|
layout 'fluid'
|
|
|
|
|
2017-02-09 21:33:12 +08:00
|
|
|
def index
|
2022-05-13 21:45:24 +08:00
|
|
|
@member_of = @user.teams.count
|
2017-02-09 21:33:12 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def datatable
|
2023-07-18 19:36:41 +08:00
|
|
|
render json: ::TeamsDatatable.new(view_context, @user)
|
2017-02-09 21:33:12 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
|
|
|
@new_team = Team.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
@new_team = Team.new(create_params)
|
|
|
|
@new_team.created_by = @user
|
|
|
|
|
|
|
|
if @new_team.save
|
|
|
|
# Redirect to new team page
|
2019-11-24 06:20:38 +08:00
|
|
|
redirect_to team_path(@new_team)
|
2017-02-09 21:33:12 +08:00
|
|
|
else
|
|
|
|
render :new
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
|
|
|
@user_team = UserTeam.find_by(user: @user, team: @team)
|
|
|
|
end
|
|
|
|
|
|
|
|
def users_datatable
|
2023-07-18 19:36:41 +08:00
|
|
|
render json: ::TeamUsersDatatable.new(view_context, @team, @user)
|
2017-02-09 21:33:12 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def name_html
|
2023-06-21 20:13:20 +08:00
|
|
|
render json: {
|
|
|
|
html: render_to_string(
|
|
|
|
partial: 'users/settings/teams/name_modal_body',
|
2023-07-05 18:43:23 +08:00
|
|
|
locals: { team: @team },
|
|
|
|
formats: :html
|
2023-06-21 20:13:20 +08:00
|
|
|
)
|
|
|
|
}
|
2017-02-09 21:33:12 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def description_html
|
2023-06-21 20:13:20 +08:00
|
|
|
render json: {
|
|
|
|
html: render_to_string(
|
|
|
|
partial: 'users/settings/teams/description_modal_body',
|
2023-07-05 18:43:23 +08:00
|
|
|
locals: { team: @team },
|
|
|
|
formats: :html
|
2023-06-21 20:13:20 +08:00
|
|
|
)
|
|
|
|
}
|
2017-02-09 21:33:12 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2023-07-18 19:36:41 +08:00
|
|
|
if @team.update(update_params)
|
|
|
|
@team.update(last_modified_by: current_user)
|
|
|
|
render json: {
|
|
|
|
status: :ok,
|
|
|
|
html: custom_auto_link(
|
|
|
|
@team.tinymce_render(:description),
|
|
|
|
simple_format: false,
|
|
|
|
tags: %w(img),
|
|
|
|
team: current_team
|
|
|
|
)
|
|
|
|
}
|
|
|
|
else
|
|
|
|
render json: @team.errors, status: :unprocessable_entity
|
2017-02-09 21:33:12 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
@team.destroy
|
|
|
|
|
|
|
|
flash[:notice] = I18n.t(
|
|
|
|
'users.settings.teams.edit.modal_destroy_team.flash_success',
|
|
|
|
team: @team.name
|
|
|
|
)
|
|
|
|
|
|
|
|
# Redirect back to all teams page
|
|
|
|
redirect_to action: :index
|
|
|
|
end
|
|
|
|
|
2023-03-22 16:42:54 +08:00
|
|
|
def switch
|
2023-06-13 17:51:00 +08:00
|
|
|
team = current_user.teams.find_by(id: params[:team_id])
|
2023-03-22 16:42:54 +08:00
|
|
|
|
|
|
|
if team && current_user.update(current_team_id: team.id)
|
|
|
|
flash[:success] = t('users.settings.changed_team_flash',
|
|
|
|
team: current_user.current_team.name)
|
|
|
|
render json: { current_team: team.id }
|
|
|
|
else
|
|
|
|
render json: { message: t('users.settings.changed_team_error_flash') }, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-09 21:33:12 +08:00
|
|
|
private
|
|
|
|
|
2018-01-09 22:28:27 +08:00
|
|
|
def check_create_team_permission
|
2018-01-10 17:47:45 +08:00
|
|
|
render_403 unless can_create_teams?
|
2018-01-09 22:28:27 +08:00
|
|
|
end
|
|
|
|
|
2017-02-09 21:33:12 +08:00
|
|
|
def load_user
|
|
|
|
@user = current_user
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_team
|
2023-06-14 09:13:33 +08:00
|
|
|
@team = Team.find_by(id: params[:id])
|
2022-05-19 19:46:03 +08:00
|
|
|
render_403 unless can_manage_team?(@team)
|
2017-02-09 21:33:12 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_params
|
|
|
|
params.require(:team).permit(
|
|
|
|
:name,
|
|
|
|
:description
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_params
|
|
|
|
params.require(:team).permit(
|
|
|
|
:name,
|
|
|
|
:description
|
|
|
|
)
|
|
|
|
end
|
2023-06-14 09:13:33 +08:00
|
|
|
|
|
|
|
def set_breadcrumbs_items
|
|
|
|
@breadcrumbs_items = []
|
|
|
|
|
|
|
|
@breadcrumbs_items.push({
|
|
|
|
label: t('breadcrumbs.teams'),
|
|
|
|
url: teams_path
|
|
|
|
})
|
|
|
|
if @team
|
|
|
|
@breadcrumbs_items.push({
|
|
|
|
label: @team.name,
|
|
|
|
url: team_path(@team)
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
2017-02-09 21:33:12 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|