diff --git a/app/controllers/admin/members_controller.rb b/app/controllers/admin/members_controller.rb index 2a7fde6..c9427dd 100644 --- a/app/controllers/admin/members_controller.rb +++ b/app/controllers/admin/members_controller.rb @@ -1,9 +1,12 @@ class Admin::MembersController < OrbitMemberController before_action :show_member_profile, only: [:show, :edit, :update, :edit_passwd, :edit_privilege] - + helper MemberHelper helper OrbitBackendHelper + require 'spreadsheet' + require 'rubyXL' + def index @roles = Role.excludes(disabled: true) page_num = params[:page] || 1 @@ -91,6 +94,346 @@ class Admin::MembersController < OrbitMemberController get_info_and_roles end + + def make_alpha_from_numbers(number) + + @numeric = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + + if number < @numeric.size + @a = @numeric[number] + else + @dev_by = (number/@numeric.size).floor + @a = "#{make_alpha_from_numbers(@dev_by-1)}#{make_alpha_from_numbers(number-(@dev_by*@numeric.size))}"; + end + end + + + def import_data_check(file) + + profile_data_row = ["email","sid","office_tel","sex","birthday"] + profile_lang_data_row = ["first_name","last_name","address","autobiography"] + + @field_type = {'text_field' => 'typeA', 'select' => 'typeB', 'date' => 'typeC', 'text_area' => 'typeD', 'radio_button' => 'typeE', 'checkbox' => 'typeE', 'address' => ''} + + @date_type = {'format1' => '%Y/%m/%d %H:%M', 'format2' => '%Y/%m/%d', 'format3' => '%Y/%m', 'format4' => '%Y'} + + @error_msg = Array.new + + workbook = RubyXL::Parser.parse(file.tempfile) + sheet = workbook['Member'].extract_data + sheet.each_with_index do |row,index| + if index == 0 + @field_data = row + end + if index > 2 + + @user_id = row[@field_data.index('user_id')] + if !@user_id.blank? + check_id = User.where(:user_name => @user_id).first + @error_msg << "#{make_alpha_from_numbers(@field_data.index('user_id'))}#{index+1}欄位帳號已存在。" if !check_id.blank? + @error_msg << "#{make_alpha_from_numbers(@field_data.index('user_id'))}#{index+1}欄位格式不符。" if @user_id.size < 3 + end + + @password = row[@field_data.index('password')] + if !@password.blank? and !@user_id.blank? + @error_msg << "#{make_alpha_from_numbers(@field_data.index('password'))}#{index+1}欄位格式不符。" if @password.size < 8 + elsif @password.blank? and !@user_id.blank? + @error_msg << "#{make_alpha_from_numbers(@field_data.index('password'))}#{index+1}欄位格式不符。" + end + + @error_msg << "#{make_alpha_from_numbers(@field_data.index('birthday'))}#{index+1}欄位日期格式不符。" if row[@field_data.index("birthday")].class.to_s != 'DateTime' and !row[@field_data.index('birthday')].blank? + + + + @infos = MemberInfo.all + @infos.each do |info| + info.member_profile_fields.each do |member_profile_field| + + + + if member_profile_field.markup == 'select' or member_profile_field.markup == 'radio_button' + + @option_list = member_profile_field[@field_type[member_profile_field.markup]]['option_list'].collect{|p| p[0]} + + @rowname = "#{info.key}-#{member_profile_field.key}" + @error_msg << "#{make_alpha_from_numbers(@field_data.index(@rowname))}#{index+1}欄位格式不符。" if !@option_list.include?("#{row[@field_data.index(@rowname)]}") and !row[@field_data.index(@rowname)].blank? + + elsif member_profile_field.markup == 'checkbox' + + @rowname = "#{info.key}-#{member_profile_field.key}" + @error_msg << "#{make_alpha_from_numbers(@field_data.index(@rowname))}#{index+1}欄位格式不符。" if row[@field_data.index(@rowname)].to_s.split(',').last == row[@field_data.index(@rowname)] and !row[@field_data.index(@rowname)].blank? + + elsif member_profile_field.markup == 'date' + @rowname = "#{info.key}-#{member_profile_field.key}" + @error_msg << "#{make_alpha_from_numbers(@field_data.index(@rowname))}#{index+1}欄位日期格式不符。" if row[@field_data.index(@rowname)].class.to_s != 'DateTime' and !row[@field_data.index(@rowname)].blank? + end + + end + + end + + + @roles = Role.all + @roles.each do |role| + + role_statuses = RoleStatus.where(role_id: role.id, :disable=> false) + if !role_statuses.blank? + @rowname = "#{role.key}-status" + if !@field_data.index(@rowname).blank? + @error_msg << "#{make_alpha_from_numbers(@field_data.index(@rowname))}#{index+1}欄位格式不符。" if row[@field_data.index(@rowname)].to_s.split(',').last == row[@field_data.index(@rowname)] and !row[@field_data.index(@rowname)].blank? + end + end + + role.attribute_fields.asc(:created_at).each do |attribute_field| + + + if attribute_field.markup == 'select' or attribute_field.markup == 'radio_button' + + @option_list = attribute_field[@field_type[attribute_field.markup]]['option_list'].collect{|p| p[0]} + + @rowname = "#{role.key}-#{attribute_field.key}" + if !@field_data.index(@rowname).blank? + @error_msg << "#{make_alpha_from_numbers(@field_data.index(@rowname))}#{index+1}欄位格式不符。" if !@option_list.include?("#{row[@field_data.index(@rowname)]}") and !row[@field_data.index(@rowname)].blank? + end + + elsif attribute_field.markup == 'checkbox' + @rowname = "#{role.key}-#{attribute_field.key}" + if !@field_data.index(@rowname).blank? + @error_msg << "#{make_alpha_from_numbers(@field_data.index(@rowname))}#{index+1}欄位格式不符。" if row[@field_data.index(@rowname)].to_s.split(',').last == row[@field_data.index(@rowname)] and !row[@field_data.index(@rowname)].blank? + end + elsif attribute_field.markup == 'date' + @rowname = "#{role.key}-#{attribute_field.key}" + if !@field_data.index(@rowname).blank? + @error_msg << "#{make_alpha_from_numbers(@field_data.index(@rowname))}#{index+1}欄位日期格式不符。" if row[@field_data.index(@rowname)].class.to_s != 'DateTime' and !row[@field_data.index(@rowname)].blank? + end + end + + end + + end + + end + end + + end + + def import_members + + if !params[:file].blank? + + import_data_check(params[:file]) + + if @error_msg.blank? + + profile_data_row = ["email","sid","office_tel","sex","birthday"] + profile_lang_data_row = ["first_name","last_name","address","autobiography"] + + @field_type = {'text_field' => 'typeA', 'select' => 'typeB', 'date' => 'typeC', 'text_area' => 'typeD', 'radio_button' => 'typeE', 'checkbox' => 'typeE', 'address' => ''} + + @date_type = {'format1' => '%Y/%m/%d %H:%M', 'format2' => '%Y/%m/%d', 'format3' => '%Y/%m', 'format4' => '%Y'} + + workbook = RubyXL::Parser.parse(params[:file].tempfile) + sheet = workbook['Member'].extract_data + sheet.each_with_index do |row,index| + if index == 0 + @field_data = row + end + if index > 2 + if !row[0].blank? and !row[1].blank? + + @member_profile = MemberProfile.new + + @site_in_use_locales.each_with_index do |locale, i| + + @member_profile.first_name_translations = @member_profile.first_name_translations.merge!( "#{locale}" => row[@field_data.index("first_name(#{locale})")]) if !row[@field_data.index("first_name(#{locale})")].blank? + @member_profile.last_name_translations = @member_profile.last_name_translations.merge!( "#{locale}" => row[@field_data.index("last_name(#{locale})")]) if !row[@field_data.index("last_name(#{locale})")].blank? + @member_profile.address_translations = @member_profile.address_translations.merge!( "#{locale}" => row[@field_data.index("address(#{locale})")]) if !row[@field_data.index("address(#{locale})")].blank? + @member_profile.autobiography_translations = @member_profile.autobiography_translations.merge!( "#{locale}" => row[@field_data.index("autobiography(#{locale})")]) if !row[@field_data.index("autobiography(#{locale})")].blank? + + end + + @member_profile.email = row[@field_data.index("email")] + @member_profile.sid = row[@field_data.index("sid")] + @member_profile.office_tel = row[@field_data.index("office_tel")] + @member_profile.sex = ( row[@field_data.index("sex")] == 'male' or row[@field_data.index("sex")] == 'female' ) ? row[@field_data.index("sex")] : 'unknown' + @member_profile.birthday = row[@field_data.index("birthday")].class.to_s == 'DateTime' ? row[@field_data.index("birthday")] : '' + + @member_profile.save + + @infos = MemberInfo.all + @infos.each do |info| + info.member_profile_fields.each do |member_profile_field| + + value = member_profile_field.member_profile_field_values.build + value.member_profile_id = @member_profile.id + + if member_profile_field.markup == 'text_field' or member_profile_field.markup == 'text_area' + if member_profile_field[@field_type[member_profile_field.markup]]['cross_lang'] == 'true' + @rowname = "#{info.key}-#{member_profile_field.key}" + value["val"] = row[@field_data.index(@rowname)] if !row[@field_data.index(@rowname)].blank? + else + @site_in_use_locales.each_with_index do |locale, i| + @rowname = "#{info.key}-#{member_profile_field.key}(#{locale})" + value["#{locale}"] = row[@field_data.index(@rowname)] if !row[@field_data.index(@rowname)].blank? + end + end + elsif member_profile_field.markup == 'checkbox' + @rowname = "#{info.key}-#{member_profile_field.key}" + value["val"] = row[@field_data.index(@rowname)].to_s.split(',') if !row[@field_data.index(@rowname)].blank? + elsif member_profile_field.markup == 'date' + @rowname = "#{info.key}-#{member_profile_field.key}" + value["val"] = row[@field_data.index(@rowname)].class.to_s == 'DateTime' ? row[@field_data.index(@rowname)].to_datetime.strftime(@date_type[member_profile_field['typeC']['format']]) : '' + elsif member_profile_field.markup == 'address' + @site_in_use_locales.each_with_index do |locale, i| + @rowname = "#{info.key}-#{member_profile_field.key}(#{locale})" + value["#{locale}"] = row[@field_data.index(@rowname)] if !row[@field_data.index(@rowname)].blank? + end + else + @rowname = "#{info.key}-#{member_profile_field.key}" + value["val"] = "#{row[@field_data.index(@rowname)]}" + end + + value.save + + end + + end + + @role_status_data = [] + @roles = Role.all + @roles.each do |role| + + role_statuses = RoleStatus.where(role_id: role.id, :disable=> false).asc(:_id) + if !role_statuses.blank? + @rowname = "#{role.key}-status" + + if !@field_data.index(@rowname).blank? + if !row[@field_data.index(@rowname)].blank? + @status_value = row[@field_data.index(@rowname)].to_s.split(',') + role_statuses.each do |role_status| + if @status_value.include?(role_status.key) + @role_status_data << role_status.id + end + end + end + end + + end + + @role_add_value = 0 + + role.attribute_fields.asc(:created_at).each do |attribute_field| + + value = attribute_field.attribute_values.build + value.member_profile_id = @member_profile.id + + if attribute_field.markup == 'text_field' or attribute_field.markup == 'text_area' + if attribute_field[@field_type[attribute_field.markup]]['cross_lang'] == 'true' + @rowname = "#{role.key}-#{attribute_field.key}" + if !@field_data.index(@rowname).blank? + if !row[@field_data.index(@rowname)].blank? + value["val"] = row[@field_data.index(@rowname)] + @role_add_value = @role_add_value + 1 + end + end + else + @site_in_use_locales.each_with_index do |locale, i| + @rowname = "#{role.key}-#{attribute_field.key}(#{locale})" + if !@field_data.index(@rowname).blank? + if !row[@field_data.index(@rowname)].blank? + value["#{locale}"] = row[@field_data.index(@rowname)] + @role_add_value = @role_add_value + 1 + end + end + end + end + elsif attribute_field.markup == 'checkbox' + @rowname = "#{role.key}-#{attribute_field.key}" + if !@field_data.index(@rowname).blank? + if !row[@field_data.index(@rowname)].blank? + value["val"] = row[@field_data.index(@rowname)].to_s.split(',') + @role_add_value = @role_add_value + 1 + end + end + elsif attribute_field.markup == 'date' + @rowname = "#{role.key}-#{attribute_field.key}" + if !@field_data.index(@rowname).blank? + value["val"] = row[@field_data.index(@rowname)].class.to_s == 'DateTime' ? row[@field_data.index(@rowname)].to_datetime.strftime(@date_type[attribute_field['typeC']['format']]) : '' + end + elsif attribute_field.markup == 'address' + @site_in_use_locales.each_with_index do |locale, i| + @rowname = "#{role.key}-#{attribute_field.key}(#{locale})" + if !@field_data.index(@rowname).blank? + if !row[@field_data.index(@rowname)].blank? + value["#{locale}"] = row[@field_data.index(@rowname)] + @role_add_value = @role_add_value + 1 + end + end + end + else + @rowname = "#{role.key}-#{attribute_field.key}" + if !@field_data.index(@rowname).blank? + if !row[@field_data.index(@rowname)].blank? + value["val"] = "#{row[@field_data.index(@rowname)]}" + @role_add_value = @role_add_value + 1 + end + end + end + + value.save + + end + + if @role_add_value > 0 + role.member_profiles << @member_profile + role.save + end + + end + + if !@role_status_data.blank? + @member_profile.role_status_ids = @role_status_data + @member_profile.save + end + + @user = User.new( + user_name: row[@field_data.index("user_id")], + password: row[@field_data.index("password")], + member_profile_id: @member_profile.id, + approved: true + ) + @user.save + + end + + end + + end + + redirect_to admin_members_path + + else + + render 'import_error_msg' + + end + + end + end + + def download_export + + @users = User.all + @member_role = params[:member_role].blank? ? Role.all.asc(:_id) : params[:member_role] + @member_roles = @member_role.collect{|a| a[0]} + respond_to do |format| + format.xlsx { + response.headers['Content-Disposition'] = 'attachment; filename="member_import_data.xlsx"' + } + end + + end + def new if has_access? @member = MemberProfile.new