orbit-basic/app/controllers/admin/assets_controller.rb

147 lines
3.9 KiB
Ruby

class Admin::AssetsController < OrbitBackendController
open_for_user
open_for_admin
before_filter :setup_vars
def index
if params[:filter]
@assets = current_user.assets.where(title: /#{params[:filter]}/i)
else
@assets = current_user.assets.desc(:created_at)
end
if params[:type] == "image"
@assets = @assets.where(:content_type => /^image/)
end
@tag = ModuleTag.new
@module_app = ModuleApp.find(params[:module_app_id]) rescue nil
raise ModuleAppError, 'Can not find ModuleApp' if @module_app.nil?
@module_app_id = @module_app.id rescue nil
@assets = @assets.page(params[:page]).per(10)
render :layout => "assets"
end
def show
#TODO
end
def new
@asset = Asset.new
@asset_categories = AssetCategory.all
@tags = get_tags
respond_to do |format|
format.html {}
format.js { render 'js/show_pop_up', :locals => {:partial => 'admin/assets/new'} }
end
end
def edit
@asset = Asset.find(params[:id])
@tags = get_tags
render layout: false
end
def create
@asset = Asset.new(params[:asset])
@asset.filename = @asset.title rescue nil
if @asset.filename && @asset.save
# respond_to do |format|
# format.js {
# if params[:uploader]
# render 'create'
# else
# render 'js/remove_pop_up_and_reload_content', :locals => {:function => 'replaceWith', :id => "asset_#{@asset.id}", :value => @asset, :values => nil, :partial => 'admin/assets/asset', :locals => nil}
# end
# }
# end
# render :json=>{"url"=>@asset.data.url,"title"=>"xxx","desc"=>"sss"}.to_json
render :layout=>false
else
flash[:error] = t('create.fail')
@tags = get_tags
respond_to do |format|
format.js {
if params[:uploader]
render 'file_upload'
else
render 'js/reload_pop_up', :locals => {:value => @asset, :values => nil, :partial => 'admin/assets/new', :locals => {:is_html => false}}
end
}
end
end
end
def update
@asset = Asset.find(params[:id])
if @asset.update_attributes(params[:asset])
render layout: false
else
flash[:error] = t('update.fail')
@tags = get_tags
respond_to do |format|
format.html { render :action => :edit }
format.js { render 'js/reload_pop_up', :locals => {:value => @asset, :values => nil, :partial => 'admin/assets/edit', :locals => {:is_html => false}} }
end
end
end
def destroy
@asset = Asset.find(params[:id])
@asset.destroy
respond_to do |format|
format.js { render 'js/remove_element', :locals => {:id => "asset_#{@asset.id}"} }
end
end
def delete
if params[:ids]
asset = Asset.any_in(:_id => params[:ids]).destroy_all
end
redirect_to assets_url(:filter => params[:filter], :direction => params[:direction], :sort => params[:sort], :sort_options => params[:sort_options])
end
def delete_files
@files = params[:files]
@files.each do |file|
asset = Asset.find(file)
asset.destroy
end
@assets = (params[:sort] || @filter) ? get_sorted_and_filtered("asset") : Asset.all.desc(:created_at).page(params[:page]).per(10)
if params[:type] == "images"
@assets = @assets.where(:content_type => /^image/)
end
# render :json =>{"success"=>true}.to_json
end
def file_upload
@files = params['files']
a = Array.new
@files.each do |file|
@asset = current_user.assets.new
@asset.data = file
@asset.title_translations = {:en => file.original_filename, :zh_tw => file.original_filename}
# @asset.title_translations[:zh_tw] = file.original_filename
@asset.save!
a << {"url"=>@asset.data.url}
end
render :json=>{"files"=>a}.to_json
end
def file_select
@assets = Asset.all
render :layout => false
end
def check_permission
true
end
end