personal-book/app/controllers/admin/books_controller.rb

221 lines
5.3 KiB
Ruby

class Admin::BooksController < OrbitMemberController
layout "member_plugin"
include Admin::PersonalBooksHelper
before_action :set_book, only: [:show, :edit , :update, :destroy]
before_action :set_plugin
before_action :get_settings,:only => [:new, :edit, :setting]
before_action :need_access_right
before_action :allow_admin_only, :only => [:index, :setting]
def index
@writing_books = Book.order_by(:year=>'desc').page(params[:page]).per(10)
respond_to do |format|
format.html # index.html.erb
format.js { }
format.xml { render :xml => @books }
end
end
def analysis_report
role = params[:role_id]
year_start = params[:year_start].to_i
year_end = params[:year_end].to_i
graph_by = params[:graph_by]
@data = get_chart_data(year_start,year_end,role,params[:graph_by])
render :layout => false
end
def download_excel
year_start = params[:year_start].to_i
year_end = params[:year_end].to_i
@data = get_data_for_excel(year_start,year_end)
respond_to do |format|
format.xlsx {
response.headers['Content-Disposition'] = 'attachment; filename="books.xlsx"'
}
end
end
def show
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @book }
end
end
def edit
@member = @book.member_profile
if params[:desktop]
render :layout => false
end
end
def update
@member = @book.member_profile
@book.update_attributes(book_params)
@book.save
if params[:desktop] == "true"
render json: {"data" => get_paper_list}.to_json
else
redirect_to params['referer_url']
end
end
def new
@book = Book.new
@member = MemberProfile.find_by(:uid=>params['uid']) rescue nil
if params[:desktop]
render :layout => false
end
end
def create
if !book_params['member_profile_id'].blank?
@member = MemberProfile.find(book_params['member_profile_id']) rescue nil
@book = Book.new(book_params)
@book.save
if params[:desktop] == "true"
render json: {"data" => get_paper_list}.to_json
else
redirect_to params['referer_url']
end
elsif !params[:author_members].blank?
params[:author_members].each do |author_member|
book_params['member_profile_id'] = author_member
@book = Book.new(book_params)
@book.save
if params[:desktop] == "true"
render json: {"data" => get_paper_list}.to_json
end
end
redirect_to params['referer_url']
else
book_params['member_profile_id'] = User.find(current_user.id).member_profile_id
@book = Book.new(book_params)
@book.save
if params[:desktop] == "true"
render json: {"data" => get_paper_list}.to_json
end
redirect_to params['referer_url']
end
end
def destroy
@book.destroy
respond_to do |format|
format.html { redirect_to(admin_books_url) }
# format.xml { head :ok }
format.js
format.json {render json: {"success" => true}}
end
end
def excel_format
respond_to do |format|
format.xlsx {
response.headers['Content-Disposition'] = 'attachment; filename="book_format.xlsx"'
}
end
end
def import_from_excel
workbook = RubyXL::Parser.parse(params["import_file"].tempfile)
sheet = workbook[0]
if sheet.count <= 503
sheet.each_with_index do |row, i|
next if i < 3
user = User.where(:user_name => row.cells[0].value).first rescue nil
if !user.nil?
mp = user.member_profile
import_this_book(row,mp)
end
end
redirect_to admin_books_url
else
redirect_to admin_books_url(:error => "1")
end
end
def toggle_hide
if params[:ids]
@books = Book.any_in(_id: params[:ids])
@books.each do |book|
book.is_hidden = params[:disable]
book.save
end
end
render json: {"success"=>true}
end
def setting
end
def frontend_setting
@member = MemberProfile.find_by(:uid=>params['uid']) rescue nil
@intro = BookIntro.find_by(:member_profile_id=>@member.id) rescue nil
@intro = @intro.nil? ? BookIntro.new({:member_profile_id=>@member.id}) : @intro
end
def update_frontend_setting
@member = MemberProfile.find(intro_params['member_profile_id']) rescue nil
@intro = BookIntro.find_by(:member_profile_id=>@member.id) rescue nil
@intro = @intro.nil? ? BookIntro.new({:member_profile_id=>@member.id}) : @intro
@intro.update_attributes(intro_params)
@intro.save
redirect_to URI.encode('/admin/members/'+@member.to_param+'/Book')
end
def get_settings
@author_types = BookAuthorType.all
@book_types = BookType.all
end
def set_plugin
@plugin = OrbitApp::Plugin::Registration.all.select{|plugin| plugin.app_name.eql? 'Book'}.first
end
private
def set_book
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
@book = Book.find_by(:uid => uid) rescue Book.find(params[:id])
end
def intro_params
params.require(:book_intro).permit! rescue nil
end
def book_params
params.require(:book).permit! rescue nil
end
end