2021-02-27 04:19:24 +00:00
class Admin :: PersonalPluginFieldsController < OrbitAdminController
require 'fileutils'
include Admin :: PersonalPluginFieldsHelper
2021-03-04 06:33:01 +00:00
before_action :set_personal_plugin_field , only : [ :show , :edit , :update , :destroy , :fields_setting , :update_fields_setting , :generate_plugin ]
2021-02-27 04:19:24 +00:00
def index
@personal_plugin_fields = PersonalPluginField . order_by ( :created_at = > 'desc' ) . page ( params [ :page ] ) . per ( 10 )
end
def new
@member = MemberProfile . find_by ( :uid = > params [ 'uid' ] ) rescue nil
@personal_plugin_field = PersonalPluginField . new
end
2021-03-04 09:38:15 +00:00
def download
zip_path = " tmp/ "
FileUtils . mkdir_p ( zip_path ) if ! Dir . exist? ( zip_path )
personal_plugin_field = PersonalPluginField . find ( params [ :personal_plugin_field_id ] ) rescue nil
if personal_plugin_field
zip_file_path = zip_path + " #{ personal_plugin_field . module_name } .zip "
zip_file = ZipFileGenerator . new ( zip_path + personal_plugin_field . module_name , zip_file_path )
begin
zip_file . write
rescue
File . delete ( zip_path + " #{ personal_plugin_field . module_name } .zip " )
zip_file . write
end
send_file ( zip_file_path )
end
end
2021-03-03 12:24:10 +00:00
def copy
@member = MemberProfile . find_by ( :uid = > params [ 'uid' ] ) rescue nil
attributes = PersonalPluginField . find ( params [ :personal_plugin_field_id ] ) . attributes rescue { }
attributes = attributes . except ( " _id " )
copy_attributes = { }
attributes . each do | k , v |
if ( PersonalPluginField . fields [ k ] . options [ :localize ] rescue false )
copy_attributes [ " #{ k } _translations " ] = v
else
copy_attributes [ k ] = v
end
end
#render :html => attributes and return
@personal_plugin_field = PersonalPluginField . new ( copy_attributes )
end
2021-02-27 04:19:24 +00:00
def create
personal_plugin_field = PersonalPluginField . create ( personal_plugin_field_params )
2021-03-04 06:33:01 +00:00
redirect_to admin_personal_plugin_fields_path
2021-02-27 04:19:24 +00:00
end
def edit
end
def destroy
@personal_plugin_field . destroy
redirect_to admin_personal_plugin_fields_path ( :page = > params [ :page ] )
end
def update
@personal_plugin_field . update_attributes ( personal_plugin_field_params )
@personal_plugin_field . save
2021-03-04 06:33:01 +00:00
redirect_to admin_personal_plugin_fields_path
2021-02-27 04:19:24 +00:00
end
def fields_setting
end
def update_fields_setting
field_params = params . require ( :personal_plugin_field ) . permit! rescue { }
@personal_plugin_field . update_attributes ( field_params )
@personal_plugin_field . save
redirect_to params [ :referer_url ]
end
def generate_plugin
2021-02-28 09:19:14 +00:00
template_dir_path = File . expand_path ( " ../../../../template_generator/ " , __FILE__ ) + " / "
2021-02-27 04:19:24 +00:00
cp_template_dir_path = " #{ Rails . root } /tmp/ #{ @personal_plugin_field . module_name } / "
FileUtils . rm_rf ( cp_template_dir_path )
FileUtils . cp_r ( template_dir_path , cp_template_dir_path )
dirs = Dir . glob ( " #{ cp_template_dir_path } */ " )
files = Dir . glob ( " #{ cp_template_dir_path } * " ) . select { | fn | File . file? ( fn ) }
2021-03-03 07:44:42 +00:00
begin
2021-02-27 04:19:24 +00:00
in_use_locales = Site . first . in_use_locales
2021-03-03 07:44:42 +00:00
primary_modal_fields = @personal_plugin_field . primary_modal_fields . select { | f | ( f [ :field_name ] . present? rescue false ) }
2021-02-27 04:19:24 +00:00
yml_files = Dir . glob ( " #{ cp_template_dir_path } config/locales/*.yml " )
yml_files . each do | yml_file |
locale = yml_file . split ( " / " ) . last . split ( " .yml " ) . first
yml_text = File . read ( yml_file )
translate_hash = { }
2021-03-03 07:44:42 +00:00
primary_modal_fields . each do | field_value |
if ( field_value [ :translation_name ] [ locale ] . present? rescue false )
2021-02-27 04:19:24 +00:00
translate_hash [ field_value [ :field_name ] ] = field_value [ :translation_name ] [ locale ]
end
end
@personal_plugin_field . related_modal_name . each_with_index do | related_modal_name , i |
field_values = @personal_plugin_field . related_modal_fields [ i ] . to_a rescue [ ]
sub_hash = { }
field_values . each do | field_value |
if field_value [ :field_name ] && ( field_value [ :translation_name ] [ locale ] . present? rescue false )
sub_hash [ field_value [ :field_name ] ] = field_value [ :translation_name ] [ locale ]
end
end
if related_modal_name . present? && sub_hash . present?
translate_hash [ related_modal_name ] = sub_hash
end
2021-03-01 15:44:52 +00:00
#sub_hash["modal_name"] = "關聯欄位翻譯"
2021-02-27 04:19:24 +00:00
end
2021-03-01 15:44:52 +00:00
author_name_translation = @personal_plugin_field . author_name_translations [ locale ] rescue " "
I18n . with_locale ( locale ) do
author_name_translation = I18n . t ( " personal_plugins.author " ) if author_name_translation . blank?
end
2021-03-03 12:24:10 +00:00
translate_hash [ " member_profile " ] = author_name_translation
2021-02-27 04:19:24 +00:00
col_name_translate_yaml = " "
if translate_hash . present?
col_name_translate_yaml = translate_hash . to_yaml . gsub ( " --- \n " , '' )
end
2021-03-01 15:44:52 +00:00
yml_text = gsub_text_by_key_value ( yml_text , " col_name_translate_yaml " , col_name_translate_yaml )
2021-02-28 09:19:14 +00:00
yml_text = yml_text . gsub ( " personal_plugin_template_translate " , @personal_plugin_field . title_translations [ locale ] )
2021-02-27 04:19:24 +00:00
File . open ( yml_file , 'w+' ) do | f |
f . write ( yml_text )
end
end
2021-02-28 09:19:14 +00:00
@blank_text = " "
2021-03-03 07:44:42 +00:00
slug_title = primary_modal_fields . select { | field_value | field_value [ :slug_title ] == " 1 " rescue false } . first [ :field_name ] . to_s rescue " "
plugin_template_related_files = primary_modal_fields . select { | field_value | field_value [ :field_type ] == " file " rescue false } . map { | v | v [ :field_name ] }
plugin_template_related_links = primary_modal_fields . select { | field_value | field_value [ :field_type ] == " link " rescue false } . map { | v | v [ :field_name ] }
plugin_template_related_members = primary_modal_fields . select { | field_value | field_value [ :field_type ] == " member " rescue false } . map { | v | v [ :field_name ] }
2021-02-28 09:19:14 +00:00
plugin_template_related_files_text = plugin_template_related_files . to_s
2021-03-03 07:44:42 +00:00
plugin_template_related_links_text = plugin_template_related_links . to_s
fields = plugin_template_related_files + plugin_template_related_links
plugin_template_related_files_fields = fields . map { | field_name |
2021-02-28 09:19:14 +00:00
" has_many : #{ field_name . pluralize } , :dependent => :destroy, :autosave => true \r \n " +
2021-03-01 15:44:52 +00:00
" accepts_nested_attributes_for : #{ field_name . pluralize } , :allow_destroy => true "
} . join ( " \r \n " )
2021-02-28 09:19:14 +00:00
plugin_template = @personal_plugin_field . primary_modal_name
backend_index_fields = @personal_plugin_field . backend_fields [ " index " ] rescue [ ]
backend_index_fields_contents = backend_index_fields . map do | field_name |
if field_name == slug_title
2021-03-03 07:44:42 +00:00
" \r \n <%= link_to #{ plugin_template } . #{ field_name } , page_for_ #{ plugin_template } ( #{ plugin_template } ), target: \" blank \" %> \r \n " +
" <div class= \" quick-edit \" > \r \n " +
" <ul class= \" nav nav-pills hide \" > \r \n " +
" <li><%= link_to t('edit'), edit_admin_ #{ plugin_template } _path( #{ plugin_template } ,:page => params[:page]) %></li> \r \n " +
" <li><%= link_to t(:delete_), admin_ #{ plugin_template } _path(id: #{ plugin_template } .id, :page => params[:page]), method: :delete, data: { confirm: 'Are you sure?' } %></li> \r \n " +
" </ul> \r \n " +
" </div> \r \n "
2021-03-01 15:44:52 +00:00
elsif field_name . include? ( " . " )
" <%= #{ plugin_template } . #{ field_name } rescue \" \" %> "
2021-03-03 12:24:10 +00:00
elsif fields . include? ( field_name ) || plugin_template_related_members . include? ( field_name ) || field_name == " member_profile " #file or link or member
2021-03-03 07:44:42 +00:00
" <%= #{ plugin_template } .display_field( \" #{ field_name } \" ).html_safe rescue \" \" %> "
2021-03-01 15:44:52 +00:00
else
2021-02-28 09:19:14 +00:00
" <%= #{ plugin_template } . #{ field_name } %> "
end
end
2021-03-04 06:33:01 +00:00
backend_profile_fields = @personal_plugin_field . backend_fields [ " profile " ] rescue [ ]
backend_profile_fields_contents = backend_profile_fields . map do | field_name |
if field_name == slug_title
" \r \n <%= link_to #{ plugin_template } . #{ field_name } , page_for_ #{ plugin_template } ( #{ plugin_template } ), target: \" blank \" %> \r \n " +
" <div class= \" quick-edit \" > \r \n " +
" <ul class= \" nav nav-pills hide \" > \r \n " +
" <li><%= link_to t('edit'), edit_admin_ #{ plugin_template } _path( #{ plugin_template } ,:page => params[:page]) %></li> \r \n " +
" <li><%= link_to t(:delete_), admin_ #{ plugin_template } _path(id: #{ plugin_template } .id, :page => params[:page]), method: :delete, data: { confirm: 'Are you sure?' } %></li> \r \n " +
" </ul> \r \n " +
" </div> \r \n "
elsif field_name . include? ( " . " )
" <%= #{ plugin_template } . #{ field_name } rescue \" \" %> "
elsif fields . include? ( field_name ) || plugin_template_related_members . include? ( field_name ) || field_name == " member_profile " #file or link or member
" <%= #{ plugin_template } .display_field( \" #{ field_name } \" ).html_safe rescue \" \" %> "
else
" <%= #{ plugin_template } . #{ field_name } %> "
end
end
2021-02-27 04:19:24 +00:00
col_name_to_show = @personal_plugin_field . frontend_fields [ " member_show " ] rescue [ ]
col_name_to_show_in_show_page = @personal_plugin_field . frontend_fields [ " show " ] rescue [ ]
col_name_to_show_in_index_page = @personal_plugin_field . frontend_fields [ " index " ] rescue [ ]
extra_translate_title = col_name_to_show_in_index_page . map { | field_name | #在個人外掛前台index頁面的欄位翻譯名稱hash
2021-03-01 15:44:52 +00:00
[ " th- #{ field_name } " , " I18n.t( \" #{ @personal_plugin_field . module_name } . #{ field_name } \" ) " ]
2021-02-28 09:19:14 +00:00
}
2021-03-03 07:44:42 +00:00
col_fields = get_fields_text ( primary_modal_fields )
2021-02-28 09:19:14 +00:00
col_related_fields = @personal_plugin_field . related_modal_fields . map { | field_values | get_fields_text ( field_values ) }
plugin_template_related = @personal_plugin_field . related_modal_name . select { | n | n . present? }
2021-03-01 15:44:52 +00:00
plugin_template_related_main_field = @personal_plugin_field . related_modal_fields . map { | field_values |
slug_titles = field_values . select { | field_value | ( field_value [ :slug_title ] == " 1 " rescue false ) }
if slug_titles . count == 0
slug_titles = field_values
2021-02-28 09:19:14 +00:00
end
2021-03-01 15:44:52 +00:00
slug_titles . map { | field_value | field_value [ :field_name ] } . select { | t | t . present? } . first
}
2021-03-03 07:44:42 +00:00
locale_fields , none_locale_fields , locale_fields_input_fields , none_locale_fields_input_fields = get_input_fields ( primary_modal_fields )
2021-03-01 15:44:52 +00:00
related_locale_fields = [ ]
related_none_locale_fields = [ ]
related_locale_fields_input_fields = [ ]
related_none_locale_fields_input_fields = [ ]
@personal_plugin_field . related_modal_fields . each_with_index do | field_values , i |
related_modal_name = @personal_plugin_field . related_modal_name [ i ]
f1 , f2 , f3 , f4 = get_input_fields ( field_values , related_modal_name , related_modal_name )
related_locale_fields << f1
related_none_locale_fields << f2
related_locale_fields_input_fields << f3
related_none_locale_fields_input_fields << f4
2021-02-27 04:19:24 +00:00
end
2021-03-01 15:44:52 +00:00
datetime_field_types_hash = { " year_month " = > " %Y/%m " , " date " = > " %Y/%m/%d " , " time " = > " %H:%M " , " date_time " = > " %Y/%m/%d %H:%M " }
2021-03-03 07:44:42 +00:00
datetime_fields = primary_modal_fields . select { | field_value | datetime_field_types_hash . keys . include? ( field_value [ :field_type ] ) } . map { | field_value |
2021-03-01 15:44:52 +00:00
[ field_value [ :field_name ] , datetime_field_types_hash [ field_value [ :field_type ] ] ]
} . to_h
value_case_codes = [ " value = #{ plugin_template } .send(field) rescue \" \" " ,
" if field.include?( \" . \" ) " ,
" #{ @blank_text } value = #{ plugin_template } " ,
" #{ @blank_text } field.split( \" . \" ).each{|f| value = value.send(f) rescue nil } " ,
" end " ,
" file_fields = #{ plugin_template_related_files } " ,
2021-03-03 07:44:42 +00:00
" link_fields = #{ plugin_template_related_links } " ,
" member_fields = #{ plugin_template_related_members } " ,
2021-03-01 15:44:52 +00:00
" if file_fields.include?(field) " ,
" #{ @blank_text } files = #{ plugin_template } .send(field.pluralize) " ,
" #{ @blank_text } value = files.map do |file| " ,
" #{ @blank_text * 2 } url = file.file.url " ,
" #{ @blank_text * 2 } title = (file.title.blank? ? File.basename(file.file.path) : file.title) " ,
" #{ @blank_text * 2 } \" <li><a href=' \# {url}'' target='_blank'> \# {title}</li> \" " ,
" #{ @blank_text } end " ,
" #{ @blank_text } value = value.join( \" \" ) " ,
2021-03-03 07:44:42 +00:00
" elsif link_fields.include?(field) " ,
" #{ @blank_text } links = #{ plugin_template } .send(field.pluralize) " ,
" #{ @blank_text } value = links.map do |link| " ,
" #{ @blank_text * 2 } url = link.url " ,
" #{ @blank_text * 2 } title = (link.title.blank? ? url : link.title) " ,
" #{ @blank_text * 2 } \" <li><a href=' \# {url}'' target='_blank'> \# {title}</li> \" " ,
" #{ @blank_text } end " ,
" #{ @blank_text } value = value.join( \" \" ) " ,
" elsif member_fields.include?(field) " ,
" #{ @blank_text } members = #{ plugin_template } .send(field.pluralize) " ,
" #{ @blank_text } value = members.map{|m| " ,
" #{ @blank_text * 2 } path = OrbitHelper.url_to_plugin_show(m.to_param, 'member') rescue ' # ' " ,
" #{ @blank_text * 2 } ((text_only rescue false) ? m.name : \" <a href=' \# {path}'> \# {m.name}</a> \" ) " ,
" #{ @blank_text } } " ,
" #{ @blank_text } join_text = (text_only rescue false) ? \" , \" : \" <br> \" " ,
" #{ @blank_text } value = value.join(join_text) " ,
2021-03-03 12:24:10 +00:00
" elsif field == \" member_profile \" && value.present? " ,
" #{ @blank_text } path = OrbitHelper.url_to_plugin_show(value.to_param, 'member') rescue ' # ' " ,
" #{ @blank_text } value = \" <a href=' \# {path}'> \# {value.name}</a> \" " ,
2021-03-01 15:44:52 +00:00
" end " ,
" strftime_hash = #{ datetime_fields } " ,
" if strftime_hash.keys.include?(field) " ,
" #{ @blank_text } value = value.strftime(strftime_hash[field]) rescue value " ,
" end "
] . join ( " \r \n " )
display_field_code = value_case_codes + " \r \n " +
" value = \" <a href=' \# {OrbitHelper.url_to_plugin_show( #{ plugin_template } .to_param,' #{ @personal_plugin_field . module_name } ')}' target='_blank'> \# {value}</a> \" if field == \" #{ slug_title } \" \r \n " +
" value "
value_case_codes += " \r \n value "
related_backend_index_fields = @personal_plugin_field . related_modal_fields . map { | field_values |
field_values . map { | field_value | ( field_value [ :field_name ] rescue nil ) } . select { | t | t . present? }
}
related_backend_index_fields_contents = related_backend_index_fields . map . with_index { | field_names , i |
related_modal_name = @personal_plugin_field . related_modal_name [ i ]
field_names . map { | field_name | " <%= #{ related_modal_name } . #{ field_name } %> " }
}
2021-03-03 07:44:42 +00:00
member_methods_define = primary_modal_fields . select { | f | f [ :field_type ] == " member " } . map { | field_value |
2021-03-01 15:44:52 +00:00
[ " def #{ field_value [ :field_name ] . pluralize } " ,
" MemberProfile.find(self. #{ field_value [ :field_name ] . singularize } _ids) rescue [] " ,
" end " ]
} . flatten
2021-03-03 07:44:42 +00:00
analysis_field_name = @personal_plugin_field . backend_fields [ :analysis ] [ 0 ] rescue " "
analysis_field_name = " year " if analysis_field_name . blank?
analysis_field_input_fields = " "
personal_plugin_template = @personal_plugin_field . module_name
iterate_step_text = " 1.minute "
if analysis_field_name . present?
field_type = primary_modal_fields . select { | f | f [ :field_name ] == analysis_field_name } . first [ :field_type ] rescue " date_time "
analysis_field_input_fields = [ " start " , " end " ] . map { | f |
" <span><%=t( \" #{ personal_plugin_template } .extend_translate. #{ f } _ #{ field_type } \" )%></span> " +
generate_input_field ( field_type , " #{ analysis_field_name } _ #{ f } " ) . gsub ( " <%= f. " , " <%= " ) . gsub ( " , :new_record => @ #{ plugin_template } .new_record? " , " " )
} . join ( " " )
if field_type == " date " || field_type == " date_time "
iterate_step_text = " 1.day "
elsif field_type == " year "
iterate_step_text = " 1 "
end
end
time_fields = primary_modal_fields . select { | f | f [ :field_type ] == " time " } . map { | f | f [ :field_name ] }
before_save_codes = " " #time_fields.map{|f| "self.#{f} = parse_time(self.#{f}.strftime('%H:%M'))"}.join("\r\n")
@match_pattern = { " personal_plugin_template " = > personal_plugin_template ,
2021-02-28 09:19:14 +00:00
" plugin_template " = > plugin_template ,
" plugin_template_related " = > plugin_template_related ,
2021-02-27 04:19:24 +00:00
" plugin_template_related_files_fields " = > plugin_template_related_files_fields ,
" col_name_to_show " = > col_name_to_show . to_s , #在會員前台頁面的顯示欄位
" col_name_to_show_in_show_page " = > col_name_to_show_in_show_page . to_s , #在個人外掛前台show頁面的顯示欄位
" col_name_to_show_in_index_page " = > col_name_to_show_in_index_page . to_s , #在個人外掛前台index頁面的顯示欄位
2021-02-28 09:19:14 +00:00
" extra_translate_title " = > extra_translate_title . map { | k , v | " \" #{ k } \" => #{ v } " } . join ( " , " ) . prepend ( " { " ) . concat ( " } " ) ,
" col_fields " = > col_fields ,
" col_related_fields " = > col_related_fields ,
" plugin_template_file " = > plugin_template_related_files ,
2021-03-03 07:44:42 +00:00
" plugin_template_link " = > plugin_template_related_links ,
2021-02-28 09:19:14 +00:00
" plugin_template_sort_hash " = > " nil " ,
" col_name_to_show_in_index_page_arr " = > col_name_to_show_in_index_page ,
2021-03-01 15:44:52 +00:00
" related_backend_index_fields " = > related_backend_index_fields ,
" related_backend_index_fields_contents " = > related_backend_index_fields_contents ,
2021-02-28 09:19:14 +00:00
" backend_index_fields " = > backend_index_fields ,
" backend_index_fields_contents " = > backend_index_fields_contents ,
2021-03-03 07:44:42 +00:00
" plugin_template_related_links_text " = > plugin_template_related_links_text ,
2021-02-28 09:19:14 +00:00
" plugin_template_related_files_text " = > plugin_template_related_files_text ,
" locale_fields " = > locale_fields ,
" none_locale_fields " = > none_locale_fields ,
" none_locale_fields_input_fields " = > none_locale_fields_input_fields ,
" locale_fields_input_fields " = > locale_fields_input_fields ,
2021-03-01 15:44:52 +00:00
" related_locale_fields " = > related_locale_fields ,
" related_none_locale_fields " = > related_none_locale_fields ,
" related_none_locale_fields_input_fields " = > related_none_locale_fields_input_fields ,
" related_locale_fields_input_fields " = > related_locale_fields_input_fields ,
" plugin_template_related_main_field " = > plugin_template_related_main_field ,
" backend_profile_fields " = > backend_profile_fields ,
" backend_profile_fields_contents " = > backend_profile_fields_contents ,
" value_case_codes " = > value_case_codes ,
" display_field_code " = > display_field_code ,
2021-03-03 07:44:42 +00:00
" member_methods_define " = > member_methods_define ,
" analysis_field_name " = > analysis_field_name ,
" analysis_field_input_fields " = > analysis_field_input_fields ,
" before_save_codes " = > before_save_codes ,
" time_fields_text " = > time_fields . to_s ,
2021-03-04 09:21:51 +00:00
" iterate_step_text " = > iterate_step_text ,
" plugin_template_related_members " = > plugin_template_related_members . to_s
2021-02-27 04:19:24 +00:00
}
2021-02-28 09:19:14 +00:00
max_length = @match_pattern . keys . map { | k | k . length } . max
@match_pattern = @match_pattern . sort_by { | k , v | ( v . class != Array ) ? ( max_length - k . length ) : - k . length }
@logs = [ ]
2021-02-27 04:19:24 +00:00
replace_files ( files )
#Thread.new do
2021-03-01 15:44:52 +00:00
replace_dirs ( dirs , false )
2021-02-27 04:19:24 +00:00
#end
2021-03-01 15:44:52 +00:00
copy_templates ( " #{ cp_template_dir_path } modules/ #{ @personal_plugin_field . module_name } / " )
2021-03-03 12:24:10 +00:00
#render :html => @logs#@match_pattern#@logs.join("<br>").html_safe#@match_pattern
2021-03-04 06:33:01 +00:00
@personal_plugin_field . update ( :log_text = > " " )
add_plugin ( " downloaded_extensions.rb " , @personal_plugin_field . module_name )
bundle_install
2021-03-03 12:24:10 +00:00
render :json = > { :success = > true , :url = > " /admin/ #{ plugin_template . pluralize } / " , :name = > @personal_plugin_field . title }
2021-03-03 07:44:42 +00:00
rescue = > e
@match_pattern = { " parse_again_start " = > " " , " parse_again_end " = > " " , " col_name_translate_yaml " = > " " }
replace_files ( files )
replace_dirs ( dirs , false )
2021-03-03 12:24:10 +00:00
error = e . backtrace . join ( " <br> " )
@personal_plugin_field . update ( :log_text = > error )
render :json = > { :success = > false , :error = > error }
2021-03-03 07:44:42 +00:00
end
2021-02-28 09:19:14 +00:00
end
2021-03-01 15:44:52 +00:00
def copy_templates ( source )
templates = Dir . glob ( 'app/templates/*/' ) . select { | f | File . basename ( f ) != 'mobile' }
templates . each do | template |
FileUtils . cp_r ( source , " #{ template } modules/. " )
end
end
def get_input_fields ( field_values , extra_field_name = nil , plugin_template = nil )
none_locale_fields = [ ]
locale_fields = [ ]
locale_fields_input_fields = [ ]
none_locale_fields_input_fields = [ ]
@index = 0
field_values . each do | field_value |
field_name = field_value [ :field_name ]
field_type = field_value [ :field_type ]
next if field_name . blank?
2021-03-03 07:44:42 +00:00
next if field_type == " file " || field_type == " link "
2021-03-01 15:44:52 +00:00
if ( field_value [ :localize ] == " 1 " rescue false )
locale_fields << field_name
input_field = generate_input_field ( field_type , field_name , true , extra_field_name , plugin_template )
locale_fields_input_fields << input_field
else
none_locale_fields << field_name
input_field = generate_input_field ( field_type , field_name , false , extra_field_name , plugin_template )
none_locale_fields_input_fields << input_field
end
@index += 1
end
return locale_fields , none_locale_fields , locale_fields_input_fields , none_locale_fields_input_fields
end
def generate_input_field ( field_type , field_name , localize = false , extra_field_name = nil , plugin_template = nil )
2021-02-28 09:19:14 +00:00
personal_plugin_template = @personal_plugin_field . module_name
2021-03-01 15:44:52 +00:00
plugin_template = @personal_plugin_field . primary_modal_name if plugin_template . nil?
2021-02-28 09:19:14 +00:00
input_field = " "
field_value_text = " @ #{ plugin_template } . #{ field_name } "
2021-03-03 07:44:42 +00:00
field_name_text = " \" #{ field_name } \" "
2021-02-28 09:19:14 +00:00
field_name_text = " locale " if localize
2021-03-01 15:44:52 +00:00
placeholder = " #{ personal_plugin_template } . #{ field_name } "
placeholder = " #{ personal_plugin_template } . #{ extra_field_name } . #{ field_name } " if extra_field_name
2021-02-28 09:19:14 +00:00
if localize
field_value_text = " @ #{ plugin_template } . #{ field_name } _translations[locale] "
end
case field_type
when " year "
2021-02-28 09:58:51 +00:00
input_field = datetime_picker_text ( plugin_template , field_name_text , " yyyy " ) #"<%= select_year((#{field_value_text} ? #{field_value_text}.to_i : DateTime.now.year), {:start_year => DateTime.now.year + 5, :end_year => 1930}, {:name => '#{plugin_template}[#{field_name}]',:class => 'span1'} ) %>"
2021-03-01 15:44:52 +00:00
when " year_month "
input_field = datetime_picker_text ( plugin_template , field_name_text , " yyyy/MM " )
2021-02-28 09:19:14 +00:00
when " date "
2021-02-28 09:58:51 +00:00
input_field = datetime_picker_text ( plugin_template , field_name_text , " yyyy/MM/dd " )
2021-02-28 09:19:14 +00:00
when " time "
2021-02-28 09:58:51 +00:00
input_field = datetime_picker_text ( plugin_template , field_name_text , " HH:mm " , true )
2021-02-28 09:19:14 +00:00
when " date_time "
2021-02-28 09:58:51 +00:00
input_field = datetime_picker_text ( plugin_template , field_name_text )
2021-02-28 09:19:14 +00:00
when " text_editor "
2021-03-01 15:44:52 +00:00
input_field = " <%= f.text_area #{ field_name_text } , class: \" input-block-level ckeditor \" , placeholder: t( \" #{ placeholder } \" ), value: ( #{ field_value_text } rescue nil) %> "
when " member "
input_field = " <%= render partial: 'admin/member_selects/email_selection_box', locals: {field: ' #{ plugin_template } [ #{ field_name . singularize } _ids][]', email_members: @ #{ plugin_template } . #{ field_name . pluralize } ,index:' #{ @index } ',select_name:' #{ field_name . pluralize } '} %> "
2021-02-28 09:19:14 +00:00
else #text_field
2021-03-01 15:44:52 +00:00
input_field = " <%= f.text_field #{ field_name_text } , class: \" input-block-level \" , placeholder: t( \" #{ placeholder } \" ), value: ( #{ field_value_text } rescue nil) %> "
2021-02-28 09:19:14 +00:00
end
if localize
2021-03-01 15:44:52 +00:00
input_field . prepend ( " <%= f.fields_for : #{ field_name } _translations do |f| %> \r \n " ) . concat ( " \r \n <% end %> " )
2021-02-28 09:19:14 +00:00
end
input_field
end
2021-02-28 09:58:51 +00:00
def datetime_picker_text ( plugin_template , field_name_text , format = " yyyy/MM/dd hh:mm " , timepicker = false )
extra_txt = " "
extra_txt = " :picker_type => \" time \" , " if timepicker
" <%= f.datetime_picker #{ field_name_text } ,:format => \" #{ format } \" , #{ extra_txt } :no_label => true, :new_record => @ #{ plugin_template } .new_record? %> "
end
2021-02-28 09:19:14 +00:00
def get_fields_text ( field_values )
fields_text = field_values . map do | field_value |
2021-03-03 07:44:42 +00:00
next if field_value [ :field_type ] == " file " || field_value [ :field_type ] == " link " || field_value [ :field_name ] . blank?
2021-02-28 09:19:14 +00:00
type = " String "
default = " \" \" "
2021-03-01 15:44:52 +00:00
field_name = field_value [ :field_name ]
2021-02-28 09:19:14 +00:00
case field_value [ :field_type ]
2021-03-03 07:44:42 +00:00
when " year "
type = " Integer "
default = " DateTime.now.year "
2021-02-28 09:19:14 +00:00
when " date "
type = " Date "
default = " Date.parse(DateTime.now.to_s) "
when " time "
2021-03-03 07:44:42 +00:00
type = " String "
default = " \" \" "
2021-02-28 09:19:14 +00:00
when " date_time "
type = " DateTime "
default = " DateTime.now "
2021-03-01 15:44:52 +00:00
when " year_month "
type = " DateTime "
default = " DateTime.now "
when " member "
type = " Array "
default = " [] "
field_name = " #{ field_name . singularize } _ids "
2021-02-28 09:19:14 +00:00
end
2021-03-01 15:44:52 +00:00
no_localize_types = [ " date " , " time " , " date_time " , " year_month " ]
field_text = " field : #{ field_name } , :type => #{ type } , :default => #{ default } "
field_text += " , :localize => true " if field_value [ :localize ] == " 1 " && ! no_localize_types . include? ( field_value [ :field_type ] )
2021-02-28 09:58:51 +00:00
field_text += " , as: :slug_title " if field_value [ :slug_title ] == " 1 "
field_text
2021-02-28 09:19:14 +00:00
end
2021-03-01 15:44:52 +00:00
fields_text . select { | t | t . present? } . join ( " \r \n " )
2021-02-28 09:19:14 +00:00
end
2021-03-01 15:44:52 +00:00
def replace_dirs ( dirs , is_array = true )
dirs . each_with_index do | dir , i |
if is_array
replace_dir ( dir , i )
else
replace_dir ( dir )
end
2021-02-28 09:19:14 +00:00
end
2021-02-27 04:19:24 +00:00
end
2021-03-01 15:44:52 +00:00
def replace_dir ( dir , current_index = nil )
dir = replace_file ( dir , current_index )
return true if dir . blank?
if dir . class == Array
2021-02-28 09:19:14 +00:00
replace_dirs ( dir )
2021-03-01 15:44:52 +00:00
return true
2021-02-28 09:19:14 +00:00
end
2021-02-27 04:19:24 +00:00
sub_dirs = Dir . glob ( " #{ dir } /*/ " )
files = Dir . glob ( " #{ dir } /* " ) . select { | fn | File . file? ( fn ) }
2021-03-01 15:44:52 +00:00
replace_files ( files , current_index )
2021-02-27 04:19:24 +00:00
if sub_dirs . present?
sub_dirs . each do | sub_dir |
2021-03-01 15:44:52 +00:00
replace_dir ( sub_dir , current_index )
2021-02-27 04:19:24 +00:00
end
else
return true
end
end
2021-03-01 15:44:52 +00:00
def replace_files ( files , current_index = nil )
2021-02-27 04:19:24 +00:00
files . each do | file |
2021-03-01 15:44:52 +00:00
replace_file ( file , current_index )
2021-02-27 04:19:24 +00:00
end
end
2021-03-01 15:44:52 +00:00
def replace_file ( file , current_index = nil )
2021-02-27 04:19:24 +00:00
isfile = File . file? ( file )
path = File . dirname ( file )
file_name = File . basename ( file )
new_filename = replace_text_with_pattern ( file_name , true )
if new_filename . blank?
FileUtils . rm_rf ( " #{ path } / #{ file_name } " )
elsif file_name != new_filename
2021-02-28 09:19:14 +00:00
if new_filename . class == Array
new_filename . each do | f |
2021-03-01 15:44:52 +00:00
next if f . blank?
2021-02-28 09:19:14 +00:00
FileUtils . cp_r ( " #{ path } / #{ file_name } " , " #{ path } / #{ f } " )
end
FileUtils . rm_rf ( " #{ path } / #{ file_name } " )
else
FileUtils . mv ( " #{ path } / #{ file_name } " , " #{ path } / #{ new_filename } " )
end
2021-02-27 04:19:24 +00:00
end
if new_filename . blank?
2021-03-01 15:44:52 +00:00
return " "
2021-02-27 04:19:24 +00:00
else
2021-02-28 09:19:14 +00:00
is_array = new_filename . class == Array
2021-02-27 04:19:24 +00:00
if isfile
2021-03-01 15:44:52 +00:00
files = Array ( new_filename )
files . each_with_index do | sub_file , i |
2021-02-28 09:19:14 +00:00
next if File . extname ( sub_file ) . match ( / (png|jpg) /i )
file_text = File . read ( " #{ path } / #{ sub_file } " )
if is_array
2021-03-01 15:44:52 +00:00
file_text = replace_text_with_pattern ( file_text , false , i , nil , true )
elsif current_index
file_text = replace_text_with_pattern ( file_text , false , current_index , nil , true )
2021-02-28 09:19:14 +00:00
else
file_text = replace_text_with_pattern ( file_text )
end
File . open ( " #{ path } / #{ sub_file } " , 'w+' ) { | f | f . write ( file_text ) }
end
2021-02-27 04:19:24 +00:00
end
2021-03-01 15:44:52 +00:00
if is_array
return new_filename . map { | sub_file | " #{ path } / #{ sub_file } " if sub_file . present? } . select { | f | f . present? }
else
return " #{ path } / #{ new_filename } "
end
2021-02-27 04:19:24 +00:00
end
end
2021-03-01 15:44:52 +00:00
def replace_text_with_pattern ( text , is_file = false , i = nil , sub_i = nil , inner = false )
2021-02-27 04:19:24 +00:00
new_text = text
@match_pattern . each do | k , v |
2021-02-28 09:19:14 +00:00
next if ! include_key ( new_text , k )
2021-02-27 04:19:24 +00:00
vv = v
2021-02-28 09:19:14 +00:00
vv = vv [ i ] if i && vv . class == Array
2021-03-01 15:44:52 +00:00
vv = vv [ sub_i ] if sub_i && vv . class == Array
2021-02-27 04:19:24 +00:00
if vv . class == String
2021-02-28 09:19:14 +00:00
if i && vv == " " && is_file
2021-02-27 04:19:24 +00:00
new_text = " "
else
new_text = gsub_text_by_key_value ( new_text , k , vv )
end
elsif vv . class == Array
2021-02-28 09:19:14 +00:00
if is_file
if v . count == 0
new_text = " "
break
else
new_text = vv . map { | sub_v | gsub_text_by_key_value ( new_text , k , sub_v ) }
break
end
2021-02-27 04:19:24 +00:00
end
2021-03-03 07:44:42 +00:00
new_text = new_text . gsub ( / parse_again_start((?:(?!parse_again_start).)+)parse_again_end /m ) do | ff |
2021-02-28 09:19:14 +00:00
parse_content = $1 #last match
result = ff
if include_key ( $1 , k )
start_index = 0
end_index = parse_content . length
start_index = 2 if parse_content [ 0 .. 1 ] == " \r \n "
end_index = end_index - 2 if parse_content [ ( end_index - 2 ) .. ( end_index - 1 ) ] == " \r \n "
parse_content = parse_content . slice ( start_index , end_index )
2021-03-01 15:44:52 +00:00
if inner
result = ( 0 ... vv . count ) . map { | ii | replace_text_with_pattern ( parse_content , false , i , ii , true ) } . join ( " " )
else
result = ( 0 ... vv . count ) . map { | ii | replace_text_with_pattern ( parse_content , false , ii ) } . join ( " " )
end
2021-02-28 09:19:14 +00:00
end
result
2021-02-27 04:19:24 +00:00
end
end
end
return new_text
end
2021-02-28 09:19:14 +00:00
def include_key ( text , key )
return text . include? ( key ) || text . include? ( key . classify )
end
2021-02-27 04:19:24 +00:00
def gsub_text_by_key_value ( text , k , v )
2021-03-01 15:44:52 +00:00
@blank_texts = [ ]
text . gsub ( / \ n( \ s*)[^ \ n]*( #{ k } | #{ k . classify } ) /m ) { | ff | @blank_texts << $1 . gsub ( / ( \ r \ n| \ n) / , '' ) }
i = 0
text = text . gsub ( k + " s " ) { | ff | v . pluralize . gsub ( " \n " , " \n #{ @blank_texts [ i ] } " ) }
text = text . gsub ( k ) { | ff | v . gsub ( " \n " , " \n #{ @blank_texts [ i ] } " ) }
text = text . gsub ( k . classify + " s " ) { | ff | v . classify . pluralize . gsub ( " \n " , " \n #{ @blank_texts [ i ] } " ) }
text = text . gsub ( k . classify ) { | ff | v . classify . gsub ( " \n " , " \n #{ @blank_texts [ i ] } " ) }
2021-02-27 04:19:24 +00:00
end
2021-03-03 12:24:10 +00:00
def get_all_gem_plugins
extention_files = [ " downloaded_extensions.rb " , " built_in_extensions.rb " ]
gem_plugins = extention_files . map { | f | ( File . read ( f ) . scan ( / \ n \ s*gem \ s*["'](.*)["'] \ s*, / ) . flatten rescue [ ] ) } . flatten
end
def check_plugin_exist
gem_plugins = get_all_gem_plugins
can_install = ! gem_plugins . include? ( params [ :plugin_name ] )
can_install = ( PersonalPluginField . find ( params [ :id ] ) . module_name == params [ :plugin_name ] rescue false ) if ! can_install
render :json = > { :can_install = > can_install }
end
2021-03-04 06:33:01 +00:00
def add_plugin ( extention_file , plugin_name )
txt = File . read ( extention_file ) rescue nil
if txt . nil?
File . open ( extention_file , 'w+' ) { | f | f . write ( " " ) }
txt = " "
end
txt_scan = txt . scan ( / ^[ \ n]*gem \ s*["'] #{ plugin_name } ["'].* \ n / )
if txt_scan . count != 1
delete_plugin ( extention_file , plugin_name )
txt = File . read ( extention_file ) rescue " "
txt = txt + " \r \n gem \" #{ plugin_name } \" , path: \" #{ Rails . root } /tmp/ #{ plugin_name } \" \r \n "
File . open ( extention_file , 'w+' ) do | f |
f . write ( txt )
end
end
end
def delete_plugin ( extention_file , plugin_name )
txt = File . read ( extention_file ) rescue nil
return if txt . nil?
txt_change = txt . gsub ( / ^[ \ n]*gem \ s*["'] #{ plugin_name } ["'].* \ n / , '' )
if txt_change != txt
File . open ( extention_file , 'w+' ) do | f |
f . write ( txt_change )
end
end
end
def check_modal_name
primary_modal_names = PersonalPluginField . where ( :id . ne = > params [ :id ] ) . pluck ( :primary_modal_name )
related_modal_names = PersonalPluginField . where ( :id . ne = > params [ :id ] ) . pluck ( :related_modal_name ) . flatten . uniq
other_modal_names = primary_modal_names + related_modal_names
personal_plugin_field = PersonalPluginField . where ( :id = > params [ :id ] ) . first
all_modal_names = PersonalPluginField . get_modal_names_cache
if personal_plugin_field . present?
except_modals = Dir . glob ( " tmp/ #{ personal_plugin_field . module_name } /app/models/*.rb " ) . map { | f |
fn = File . basename ( f )
fn . slice ( 0 , fn . length - 3 )
}
all_modal_names = all_modal_names - except_modals
end
all_modal_names = all_modal_names + other_modal_names
all_modal_names = all_modal_names . uniq
self_modal_names = params [ :modal_names ]
invalid_modal_names = self_modal_names . select { | n | all_modal_names . include? ( n ) }
if invalid_modal_names . count != 0
render :json = > { :success = > false , :invalid_modal_names = > invalid_modal_names }
else
render :json = > { :success = > true }
end
end
2021-02-27 04:19:24 +00:00
private
def personal_plugin_field_params
personal_plugin_field_params = params . require ( :personal_plugin_field ) . permit! rescue { }
if personal_plugin_field_params [ :related_modal_name ] . nil?
personal_plugin_field_params [ :related_modal_name ] = [ ]
end
if personal_plugin_field_params [ :primary_modal_fields ]
personal_plugin_field_params [ :primary_modal_fields ] = personal_plugin_field_params [ :primary_modal_fields ] . values
else
personal_plugin_field_params [ :primary_modal_fields ] = [ ]
end
if personal_plugin_field_params [ :related_modal_fields ]
personal_plugin_field_params [ :related_modal_fields ] = personal_plugin_field_params [ :related_modal_fields ] . values . map { | h | h . values }
else
personal_plugin_field_params [ :related_modal_fields ] = [ ]
end
return personal_plugin_field_params
end
def set_personal_plugin_field
2021-03-04 06:33:01 +00:00
PersonalPluginField . get_modal_names_cache
2021-02-27 04:19:24 +00:00
path = request . path . split ( '/' )
if path . last . include? '-'
uid = path [ - 1 ] . split ( " - " ) . last
uid = uid . split ( " ? " ) . first
else
uid = path [ - 2 ] . split ( " - " ) . last
uid = uid . split ( " ? " ) . first
end
@personal_plugin_field = PersonalPluginField . find_by ( :uid = > uid ) rescue PersonalPluginField . find ( params [ :id ] || params [ :personal_plugin_field_id ] )
end
2021-03-04 06:33:01 +00:00
def bundle_install
Bundler . with_clean_env { system ( " cd #{ Rails . root } && bundle install " ) }
%x( kill -s USR2 `cat tmp/pids/unicorn.pid` )
sleep 2
end
2021-02-27 04:19:24 +00:00
end