diff --git a/app/controllers/admin/patchfiles_controller.rb b/app/controllers/admin/patchfiles_controller.rb index 1830a91..900c9d2 100644 --- a/app/controllers/admin/patchfiles_controller.rb +++ b/app/controllers/admin/patchfiles_controller.rb @@ -20,18 +20,25 @@ class Admin::PatchfilesController < OrbitAdminController end end def edit - params["filefield"]["file_show"].each_with_index do |(key,value),index| - @key = @file_to_show.file_show.keys[index] - @file_to_show.file_show[@key] = value.to_i + params["filefield"]["file_show"].each do |name,status| + @file_to_show.file_show[name][:status] = status.to_i + end + params["filefield"]["sub_file_fields"].each do |name,files| + files.each do |index,file_and_status| + file_and_status.each do |file_name,status| + @file_to_show["file_show"][name]["files"][file_name]["status"] = status.to_i + end + end end if params["filefield"]["recovery_btn"] == "true" @file_to_show.recovery = "true" - @file_to_show.update - redirect_to admin_patchfiles_path and return else - @file_to_show.update - redirect_to admin_patchfiles_path and return + @file_to_show.recovery = "false" end + @file_to_show.num = 0 + @file_to_show.update + @file_to_show = Filefield.where("title"=>@app_title).first + redirect_to admin_patchfiles_path and return end def showckeditor end @@ -203,13 +210,22 @@ class Admin::PatchfilesController < OrbitAdminController else @default_field = Filefield.new() @file_to_show = Filefield.where("title"=>@app_title).first - @default_field.file_show.each do |check,istrue| - if @file_to_show.file_show[check] == nil - @file_to_show.file_show[check] = istrue + @default_field.file_show.each do |name,property| + if @file_to_show.file_show[name] == nil + @file_to_show.file_show[name] = property @file_to_show.save @file_to_show = Filefield.where("title"=>@app_title).first end end + @default_field.file_show.each do |name,property| + property[:files].each do |file_name,sub_property| + if @file_to_show.file_show[name][:files][file_name] == nil + @file_to_show.file_show[name][:files][file_name] = sub_property + @file_to_show.save + @file_to_show = Filefield.where("title"=>@app_title).first + end + end + end end end end \ No newline at end of file diff --git a/app/models/filefield.rb b/app/models/filefield.rb index dd7fb09..36b6b9d 100644 --- a/app/models/filefield.rb +++ b/app/models/filefield.rb @@ -1,25 +1,15 @@ # encoding: utf-8 require 'fileutils' class Filefield - include Mongoid::Document include Mongoid::Timestamps - - - field :title, :type=> String ,default:"patchfile" - field :file_show, :type=> Hash ,default:{"ckeditor"=>0,"site-preference(two-site-logo)"=>0,"member"=>0,"accessibility"=>0} - field :recovery, :type=> String , default:"false" - after_update :updatefile - - - def enabled_for?(lang) - if lang.nil? - return true - else - return self.choose_lang.include?(lang) - end - end - def lastpath dir_path + @ckeditor_path = Rails.root.to_s + '/app/assets/javascripts/ckeditor/' + @model_path = Rails.root.to_s + '/app/models/' + @helper_path = Rails.root.to_s + '/app/helpers/' + @views_path = Rails.root.to_s + '/app/views/' + @controller_path = Rails.root.to_s + '/app/controllers/' + @app_path = File.expand_path(__dir__) + def self.prevpath(dir_path) dir_path = dir_path.gsub('\\','/') dir_arr= dir_path.split('/') dir_str='' @@ -32,178 +22,154 @@ class Filefield end return dir_str end - def updatefile - if self.recovery == "true" - self.file_show.each do |key,value| - if value == 1 - @model_path = Rails.root.to_s + '/app/models/' - @helper_path = Rails.root.to_s + '/app/helpers/' - @views_path = Rails.root.to_s + '/app/views/' - @controller_path = Rails.root.to_s + '/app/controllers/' - if key == "ckeditor" - @ckeditor_path = Rails.root.to_s + '/app/assets/javascripts/ckeditor/' - puts 'recovery CKEDITOR' - begin - FileUtils.cd(@ckeditor_path) - FileUtils.cp('config.js.erb_back' , 'config.js.erb') - rescue - puts Dir.pwd - puts 'error recovery' - end - elsif key == "site-preference(two-site-logo)" - puts 'recovery site' - begin - FileUtils.cd(@model_path) - FileUtils.cp('site.rb_back' , 'site.rb') - FileUtils.cd(@helper_path) - FileUtils.cp('application_helper.rb_back' , 'application_helper.rb') - FileUtils.cd(@views_path+'admin/sites/') - FileUtils.cp('preference.html.erb_back','preference.html.erb') - rescue - puts Dir.pwd - Dir.chdir(Rails.root.to_s) - puts 'error recovery' - end - elsif key == "member" - puts 'recovery member' - begin - FileUtils.cd(@model_path) - FileUtils.cp('member_profile.rb_back' , 'member_profile.rb') - FileUtils.cd(@controller_path+'admin/') - FileUtils.cp('members_controller.rb_back' , 'members_controller.rb') - FileUtils.cd(@views_path+'admin/members/') - FileUtils.cp('_user_basic_passwd.html.erb_back' , '_user_basic_passwd.html.erb') - rescue - puts Dir.pwd - Dir.chdir(Rails.root.to_s) - puts 'error recovery' - end - elsif key == "accessibility" - puts 'recovery accessibility' - begin - FileUtils.cd(@views_path+'layouts/') - FileUtils.cp('back_end.html.erb_back' , 'back_end.html.erb') - FileUtils.cd(@views_path+'page_parts/') - FileUtils.cp('_form.html.erb_back' , '_form.html.erb') - rescue - puts Dir.pwd - Dir.chdir(Rails.root.to_s) - puts 'error recovery' - end - end - Dir.chdir(Rails.root.to_s) - FileUtils.cd(Rails.root.to_s) - end - end + @app_path = self.prevpath(@app_path) + @app_path = self.prevpath(@app_path) + field :title, :type=> String ,default:"patchfile" + #field :file_show, :type=> Hash ,default:{"ckeditor"=>0,"site-preference(two-site-logo)"=>0,"member"=>0,"accessibility"=>0} + field :file_show, :type=> Hash ,default:{"ckeditor"=>{:files=>{'config@js@erb'=>{:status=>0, :sc=>@app_path+'/updatefiles/', :dest=> @ckeditor_path}},:status=>0} , + "site-preference(two-site-logo)"=>{:files=>{'site@rb'=>{:status=>0, :sc=>@app_path+'/updatefiles/', :dest=>@model_path}, + 'application_helper@rb'=>{:status=>0, :sc=>@app_path+'/updatefiles/', :dest=>@helper_path}, + 'preference@html@erb'=>{:status=>0, :sc=>@app_path+'/updatefiles/admin/', :dest=>@views_path+'admin/sites/'} + },:status=>0} , + "member"=>{:files=>{'member_profile@rb'=>{:status=>0, :sc=>@app_path+'/updatefiles/', :dest=>@model_path}, + 'members_controller@rb'=>{:status=>0, :sc=>@app_path+'/updatefiles/admin/', :dest=>@controller_path+'admin/'}, + '_user_basic_passwd@html@erb'=>{:status=>0, :sc=>@app_path+'/updatefiles/admin/', :dest=>@views_path+'admin/members/'} + },:status=>0} , + "accessibility"=>{:files=>{'back_end@html@erb'=>{:status=>0, :sc=>@app_path+'/updatefiles/accessibility/', :dest=>@views_path+'layouts/'}, + '_form@html@erb'=>{:status=>0, :sc=>@app_path+'/updatefiles/accessibility/', :dest=>@views_path+'page_parts/'}, + 'jquery-ui-1@12@1/'=>{:status=>0, :sc=>@app_path+'/updatefiles/accessibility/', :dest=>Rails.root.to_s + '/app/assets/stylesheets/lib/jquery-ui-1.12.1/'} + },:status=>0} + } + field :recovery, :type=> String , default:"false" + field :last_message , :type=> String , default:"" + field :num , default: 0 + after_update :updatefile + def enabled_for?(lang) + if lang.nil? + return true + else + return self.choose_lang.include?(lang) + end + end + + def backup_file(file_name,file_path) + FileUtils.cd(file_path) + if (file_name.include? "/") == false + FileUtils.cp(file_name , file_name+'_back') else - self.file_show.each do |key,value| - if value == 1 - @model_path = Rails.root.to_s + '/app/models/' - @helper_path = Rails.root.to_s + '/app/helpers/' - @views_path = Rails.root.to_s + '/app/views/' - @controller_path = Rails.root.to_s + '/app/controllers/' - if key == "ckeditor" - @app_path = File.expand_path(__dir__) - @app_path = lastpath(@app_path) - @app_path = lastpath(@app_path) - @ckeditor_path = Rails.root.to_s + '/app/assets/javascripts/ckeditor/' - puts 'copying CKEDITOR' - begin - Dir.chdir(@ckeditor_path) - File.rename('config.js.erb','config.js.erb_back') - rescue - puts Dir.pwd - end - FileUtils.cd(@app_path+'/updatefiles/') - begin - FileUtils.cp('config.js.erb' , @ckeditor_path) - puts @ckeditor_path - rescue - puts Dir.pwd - puts 'error copy' - end - elsif key == "site-preference(two-site-logo)" - @app_path = File.expand_path(__dir__) - @app_path = lastpath(@app_path) - @app_path = lastpath(@app_path) - puts @app_path - puts 'copying site' - begin - Dir.chdir(@model_path) - File.rename('site.rb','site.rb_back') - Dir.chdir(@helper_path) - File.rename('application_helper.rb','application_helper.rb_back') - Dir.chdir(@views_path+'admin/sites/') - File.rename('preference.html.erb','preference.html.erb_back') - rescue - puts Dir.pwd - end - FileUtils.cd(@app_path+'/updatefiles/') - begin - FileUtils.cp('site.rb' , @model_path) - FileUtils.cp('application_helper.rb' , @helper_path) - FileUtils.cd('admin/') - FileUtils.cp('preference.html.erb',@views_path+'admin/sites/') - rescue - puts Dir.pwd - Dir.chdir(Rails.root.to_s) - puts 'error copy' - end - elsif key == "member" - @app_path = File.expand_path(__dir__) - @app_path = lastpath(@app_path) - @app_path = lastpath(@app_path) - begin - Dir.chdir(@model_path) - File.rename('member_profile.rb','member_profile.rb_back') - Dir.chdir(@controller_path+'admin/') - File.rename('members_controller.rb','members_controller.rb_back') - Dir.chdir(@views_path+'admin/members/') - File.rename('_user_basic_passwd.html.erb','_user_basic_passwd.html.erb_back') - rescue - puts Dir.pwd - end - FileUtils.cd(@app_path+'/updatefiles/') - puts 'copying member' - begin - FileUtils.cp('member_profile.rb' , @model_path) - FileUtils.cd('admin/') - FileUtils.cp('members_controller.rb' , @controller_path+'admin/') - FileUtils.cp('_user_basic_passwd.html.erb' , @views_path+'admin/members/') - rescue - puts Dir.pwd - Dir.chdir(Rails.root.to_s) - puts 'error copy' - end - elsif key == "accessibility" - @app_path = File.expand_path(__dir__) - @app_path = lastpath(@app_path) - @app_path = lastpath(@app_path) - begin - Dir.chdir(@views_path+'layouts/') - File.rename('back_end.html.erb','back_end.html.erb_back') - Dir.chdir(@views_path+'page_parts/') - File.rename('_form.html.erb','_form.html.erb_back') - rescue - puts Dir.pwd - end - FileUtils.cd(@app_path+'/updatefiles/accessibility/') - puts 'copying accessibility' - begin - FileUtils.cp('back_end.html.erb' , @views_path+'layouts/') - FileUtils.cp('_form.html.erb' , @views_path+'page_parts/') - FileUtils.cp_r('jquery-ui-1.12.1/' ,Rails.root.to_s + '/app/assets/stylesheets/lib/jquery-ui-1.12.1/') - rescue - puts Dir.pwd - Dir.chdir(Rails.root.to_s) - puts 'error copy' - end - end - Dir.chdir(Rails.root.to_s) - FileUtils.cd(Rails.root.to_s) - end - end + @back_foler =file_path.dup #very important,don't just use '=',it will change original value when it change + @back_foler[-1] = "_back/" + FileUtils.cp_r(file_name , @back_foler) end end - + def recover_file(file_name,file_path) + FileUtils.cd(file_path) + if (file_name.include? "/") == false + FileUtils.cp(file_name+'_back', file_name) + else + FileUtils.cd('..') + @back_foler = file_name.dup #very important,don't just use '=',it will change original value when it change + @back_foler[-1] = "_back/" + FileUtils.cp_r(@back_foler, file_name) + end + end + def copy_file(file_name,source_file_path,dest_file_path) + FileUtils.cd(source_file_path) + if (file_name.include? "/") == false + FileUtils.cp(file_name , dest_file_path) + else + FileUtils.cp_r(file_name , dest_file_path) + end + end + def updatefile + if self.num == 0 + self.last_message = "" + if self.recovery == "true" + self.file_show.each do |name,property| + if property[:status].to_i == 1 + puts 'recovering all files of '+ name + self.last_message += ('recovering all files of '+ name+'') + else + @i = 0 + property[:files].each do |file_name,sub_property| + if sub_property[:status].to_i == 1 + @i = @i + 1 + end + end + if(@i != 0) + puts 'recovering part of files:'+ name + self.last_message += ('recovering part of files:'+ name+'') + end + end + property[:files].each do |file_name,sub_property| + file_name = file_name.gsub("@",".") + if sub_property[:status].to_i == 1 + puts 'recovering '+file_name + self.last_message += ('recovering '+file_name+'') + begin + recover_file(file_name,sub_property[:dest]) + puts 'finish recovery' + self.last_message += 'finish recovery' + rescue + puts Dir.pwd + puts 'error recovery' + self.last_message += ('Dir: '+Dir.pwd+'') + self.last_message += 'error recovery' + end + end + end + end + else + self.file_show.each do |name,property| + if property[:status].to_i == 1 + puts 'copying all files of '+ name + self.last_message += ('copying all files of '+name+'') + else + @i = 0 + property[:files].each do |file_name,sub_property| + if sub_property[:status].to_i == 1 + @i = @i + 1 + end + end + if(@i != 0) + puts 'copying part of files:'+ name + self.last_message += ('copying part of files: '+name+'') + end + end + property[:files].each do |file_name,sub_property| + file_name = file_name.gsub("@",".") + if sub_property[:status].to_i == 1 + puts 'backuping '+file_name + self.last_message += ('backuping '+file_name+'') + begin + backup_file(file_name,sub_property[:dest]) + puts 'finish backup' + self.last_message += 'finish backup' + rescue + puts Dir.pwd + puts 'error backup' + self.last_message += ('Dir: '+Dir.pwd+'') + self.last_message += 'error backup' + end + puts 'copying '+file_name + self.last_message += ('copying '+file_name+'') + begin + copy_file(file_name,sub_property[:sc],sub_property[:dest]) + puts 'finish copy' + self.last_message += 'finish copy' + rescue + puts Dir.pwd + self.last_message += ('Dir: '+Dir.pwd+'') + puts 'error copy' + self.last_message += 'error copy' + end + end + end + end + end + self.num = 1 + self.save + end + Dir.chdir(Rails.root.to_s) + end end diff --git a/app/views/admin/patchfiles/index.html.erb b/app/views/admin/patchfiles/index.html.erb index 11b0fa4..809a48d 100644 --- a/app/views/admin/patchfiles/index.html.erb +++ b/app/views/admin/patchfiles/index.html.erb @@ -5,20 +5,41 @@ <%=form_for @file_to_show ,:url=>{:controller=>"patchfiles" ,:action=>"edit"} do |file|%> <%= file.fields_for :file_show do |filefield|%>
"+ - "按下確認鍵後,你將更新你所選的檔案。\n"+str+"
" - +""+ - "After click confirm button,you will update the selected file.\n"+str+"
" - +""+ + "按下確認鍵後,你將更新你所選的檔案。"+str+"
" + +""+ + "After click confirm button,you will update the selected file."+str+"
" + +""+ - "按下確認鍵後,你將還原你所選的檔案。\n"+str+"
" + ""+ + "按下確認鍵後,你將還原你所選的檔案。"+str+"
" +""+ - "After click confirm button,you will recover the selected file.\n"+str+"
" + ""+ + "After click confirm button,you will recover the selected file."+str+"
" +"