orbit-basic/app/controllers/admin/users_new_interface_control...

240 lines
6.9 KiB
Ruby

class Admin::UsersNewInterfaceController < OrbitMemberController
helper MemberHelper
before_filter :set_attribute,:except => [:index,:show]
open_for_visitor :only => [:index, :show]
def index
get_tags
get_sub_roles
@roles = Role.excludes('disabled' => true).asc("_id")
page_num = params[:page] || 1
@users = []
@filter = params[:filter]
new_filter = params[:new_filter]
if @filter && params[:clear]
@filter.delete(params[:type])
elsif @filter && new_filter
if @filter.has_key?(new_filter[:type]) && @filter[new_filter[:type]].include?(new_filter[:id].to_s)
@filter[new_filter[:type]].delete(new_filter[:id].to_s)
elsif @filter.has_key?(new_filter[:type])
@filter[new_filter[:type]] << new_filter[:id].to_s
else
@filter.merge!({new_filter[:type] => [new_filter[:id].to_s]})
end
elsif new_filter
@filter = {new_filter[:type] => [new_filter[:id].to_s]}
end
if @filter.blank?
render case params[:at]
when 'summary'
@users=User.not_guest_user.page(page_num).per(12).asc("_id").includes('avatar')
"index_summary"
when 'thumbnail'
@users=User.not_guest_user.page(page_num).per(36).asc("_id").includes('avatar')
"index_thumbnail"
else
@users=User.not_guest_user.page(page_num).per(10).asc("_id").includes('avatar')
"index"
end
else
render case params[:at]
when 'summary'
@users=User.not_guest_user.any_in(:role_ids=>@filter['role']).page(page_num).per(12).asc("_id").includes('avatar')
"index_summary"
when 'thumbnail'
@users=User.not_guest_user.any_in(:role_ids=>@filter['role']).page(page_num).per(36).asc("_id").includes('avatar')
"index_thumbnail"
else
@users=User.not_guest_user.any_in(:role_ids=>@filter['role']).page(page_num).per(10).asc("_id").includes('avatar')
"index"
end
end
end
def show
@user = User.find(params[:id])
@plugins = OrbitApp::Plugin::Registration.all
@profile_data = []
@teacher_data = []
@student_data = []
@staff_data = []
attribute_values = @user.attribute_values.asc("_id")
attribute_values.each{|att_val|
@profile_data.push({:name => att_val.attribute_field.title,:value =>att_val.get_value_by_locale(I18n.locale.to_s)}) if att_val.attribute_field.attribute.key=="profile" rescue false
}
if(!params[:show_plugin_profile].nil?)
@right_partial = OrbitApp::Plugin::Registration.find_by_key(params[:show_plugin_profile]).profile_partial_path rescue 'plugin_summary'
else
@right_partial = "plugin_summary"
end
# @user.attribute_values.each{|att_val|
# @teacher_data.push({:name => att_val.attribute_field.title,:value =>att_val[I18n.locale]})
# }
# @user.attribute_values.each{|att_val|
# @student_data.push({:name => att_val.attribute_field.title,:value =>att_val[I18n.locale]})
# }
# @user.attribute_values.each{|att_val|
# @staff_data.push({:name => att_val.attribute_field.title,:value =>att_val[I18n.locale]})
# }
# [ {:name=> "1",:value =>"Matt"},{:name=> "2",:value =>"Mark"},{:name=> "3",:value =>"Luke"},{:name=> "4",:value =>"John"} ]
get_info_and_roles
end
def new
@user = User.new
# @disable_all_field = true
@form_index = 0
get_info_and_roles
end
def create
# puts params.to_yaml
# attribute_values_key = params[:user].has_key?('new_attribute_values') ? 'new_attribute_values' : 'attribute_values'
# attribute_values = params[:user].delete(attribute_values_key)
@user = User.new(params["user"])
@checkuser = User.where(:user_id => params[:user][:user_id]).first
if !@checkuser.blank?
flash.now[:error] = t("users.user_id_error")
end
# attribute_values.to_hash.each{|key,value|
# @user.attribute_values.build(value)
# }
# @user.rebuild_sub_roles_from_attribute_values!(attribute_values)
# binding.pry
if !flash.now[:error] && @user.save
flash[:notice] = t('create.success.user')
redirect_to :action => :index
else
@user.attribute_values = nil
@form_index = 0
render :action => :new , :notice => flash.now[:error]
end
end
def temp_edit
@user = User.find(params[:id])
@form_index = 0
get_info_and_roles
end
def edit
# @disable_all_field = false
@user = User.find(params[:id])
@form_index = 0
get_info_and_roles
end
def edit_passwd
# @disable_all_field = false
@user = User.find(params[:users_new_interface_id])
end
def update
@user = User.find(params[:id])
if params[:edit_type] == 'edit_passwd'
@checkuser = User.where(:user_id => params[:user][:user_id]).first
if !@checkuser.blank? && @checkuser.id != @user.id
flash.now[:error] = t("users.user_id_error")
elsif @user.admin && current_user.id == @user.id
flash.now[:error] = t("users.admin_change_password")
end
if !flash.now[:error] && @user.update_attributes(params[:user])
redirect_to :action => :show
else
render :action => :edit_passwd, :notice => flash.now[:error]
end
else
@user.update_attributes(params[:user])
attribute_values_key = params[:user].has_key?('new_attribute_values') ? 'new_attribute_values' : 'attribute_values'
attribute_values = params[:user].delete(attribute_values_key)
# @user.rebuild_sub_roles_from_attribute_values!(attribute_values)
@user.save
redirect_to :action => :show
end
# # Update changes to the avatar
# @user.remove_avatar! if params[:file]
# @user.avatar = params[:file] if params[:file]
# if @user.id.to_s.eql?(session['warden.user.user.key'][1].to_s) && @user.admin != params[:user][:admin].to_i.to_b
# flash.now[:error] = t(:cant_revoke_self_admin)
# end
# if !flash[:error] && @user.update_attributes(params[:user])
# flash[:notice] = t('update.success.user')
# redirect_to :action => :index
# else
# get_info_and_roles
# render :action => :edit
# end
end
def destroy
if params[:id].eql?(session['warden.user.user.key'][1].to_s)
flash[:error] = t(:cant_delete_self)
else
@user = User.find(params[:id])
@user.destroy
end
redirect_to :action => :index
end
protected
def get_tags
@sub_role_tags = SubRoleTag.all
end
def get_sub_roles
@sub_roles = SubRole.all
end
def get_info_and_roles
@infos = Info.excludes('disabled' => true)
@roles = Role.excludes('disabled' => true)
end
def set_attribute
@class = 'users'
end
end