register_modules/app/controllers/admin/admin_modules_controller.rb

186 lines
4.5 KiB
Ruby

class Admin::AdminModulesController < OrbitAdminController
include RegisteredModuleHelper
def initialize
super
@app_title = "register_module"
end
def index
@registered_modules = RegisteredModule.where(:completed => true).page(params[:page]).per(10)
@table_fields = table_fields
if request.xhr?
render :partial => "index"
end
end
def uploadfiles
@rm = RegisteredModule.find(params[:module_id])
@categories = @module_app.categories.enabled
@tags = @module_app.tags
end
def approve_modules
@rm = RegisteredModule.find(params[:module_id])
@categories = @module_app.categories.enabled
@tags = @module_app.tags
render :layout => false
end
def install_site_list
rm = RegisteredModule.find(params[:id]) rescue nil
@registered_sites = site_list_without_module(rm)
render :layout => false
end
def uninstall_site_list
rm = RegisteredModule.find(params[:id]) rescue nil
slwm = site_list_with_module(rm, nil)
@registered_sites = slwm.first
@times = slwm.last
render :layout => false
end
def revoke_list
rm = RegisteredModule.find(params[:id]) rescue nil
slwm = site_list_with_module(rm, true)
@registered_sites = slwm.first
@times = slwm.last
render :layout => false
end
def grant_list
rm = RegisteredModule.find(params[:id]) rescue nil
slwm = site_list_with_module(rm, false)
@registered_sites = slwm.first
@times = slwm.last
render :layout => false
end
def update
rm = RegisteredModule.find(params[:id])
p = registered_module_params
p[:module_show_files] = rm.module_show_files + p[:module_show_files]
rm.update_attributes(p)
rm.save
if !params[:images_to_destroy].blank?
images = ModuleShowFile.where(:id.in => params[:images_to_destroy])
images.destroy_all
end
if request.xhr?
render :json => {"success" => true}.to_json
else
redirect_to admin_admin_modules_path
end
end
def install_module
websites = params[:websites]
rm = RegisteredModule.find(params[:module]) rescue nil
if !websites.blank?
p1 = fork{install_modules_on_websites(websites,rm)}
Process.detach(p1)
end
render :json=>{"success" => true}.to_json
end
def uninstall_module
websites = params[:websites]
rm = RegisteredModule.find(params[:module]) rescue nil
if !websites.blank?
# uninstall_modules_on_websites(websites,rm)
p1 = fork{uninstall_modules_on_websites(websites,rm)}
Process.detach(p1)
end
render :json=>{"success" => true}.to_json
end
def revoke_module
websites = params[:websites]
rm = RegisteredModule.find(params[:module]) rescue nil
websites.each do |website|
im = InstalledModule.where(:registered_site_id => website,:r_module => rm.id).first rescue nil
if !im.nil?
im.permission_granted = false
im.save
end
end
render :json=>{"success" => true}.to_json
end
def grant_module
websites = params[:websites]
rm = RegisteredModule.find(params[:module]) rescue nil
websites.each do |website|
im = InstalledModule.where(:registered_site_id => website,:r_module => rm.id).first rescue nil
if !im.nil?
im.permission_granted = true
im.save
end
end
render :json=>{"success" => true}.to_json
end
def upload_module_image
file = ModuleShowFile.create(show_file_params)
render :json => {"success" => true,"id" => file.id.to_s}.to_json
end
private
def show_file_params
params.require(:module_show_file).permit!
end
def site_list_without_module(rm)
registered_sites = []
if !rm.nil?
rss = RegisteredSite.active
rss.each do |rs|
im = InstalledModule.where(:r_module => rm.id, :registered_site_id => rs.id)
if im.count == 0
registered_sites << rs
end
end
end
registered_sites
end
def site_list_with_module(rm,access=nil)
registered_sites = []
times = []
if !rm.nil?
rss = RegisteredSite.active
rss.each do |rs|
if access.nil?
im = InstalledModule.where(:r_module => rm.id, :registered_site_id => rs.id)
else
im = InstalledModule.where(:r_module => rm.id, :registered_site_id => rs.id,:permission_granted => access)
end
if im.count > 0
registered_sites << rs
times << im.first.created_at.strftime("%Y-%m-%d %H:%M") rescue ""
end
end
end
[registered_sites, times]
end
def registered_module_params
p = params.require(:registered_module).permit!
if p[:module_show_files].present?
images = []
p[:module_show_files].each do |id|
image = ModuleShowFile.find(id) rescue nil
images << image if !image.nil?
end
p[:module_show_files] = images
end
p
end
def table_fields
[:module_name_, :category, :developed_by, :actions]
end
end