video-program/app/controllers/admin/video_programs_controller.rb

107 lines
3.3 KiB
Ruby
Raw Permalink Normal View History

2024-08-13 16:06:11 +00:00
# encoding: utf-8
require 'rubyXL'
class Admin::VideoProgramsController < OrbitAdminController
before_action ->(module_app = @app_title) { set_variables module_app }
before_action :set_video_program, only: [:edit, :destroy]
def initialize
super
@app_title = "video_program"
end
def index
VideoProgram.remove_expired_status
@tags = @module_app.tags
@table_fields = [:status, :category, :title, "video_program.start_date", "video_program.end_date", :last_modified]
@current_user = current_user
if params[:sort].blank?
params[:sort] = 'post_date'
params[:order] = 'desc'
end
@categories = @module_app.categories.enabled.authorized(current_user)
@filter_fields = filter_fields(@categories, @tags)
@video_programs = VideoProgram.where(:category_id.ne=>nil)
.order_by(sort)
.with_categories(filters("category"))
.with_tags(filters("tag"))
.with_status(filters("status"))
@video_programs = search_data(@video_programs,[:title]).page(params[:page]).per(10)
if request.xhr?
render :partial => "index"
end
end
def new
@tags = @module_app.tags
@statuses = []
@video_program = VideoProgram.new
end
def create
bps = video_program_params
video_program = VideoProgram.new(bps)
if !bps['video_program_links_attributes'].nil?
bps['video_program_links_attributes'].each do |idx,link|
bps['video_program_links_attributes'].delete(idx.to_s) if link['url'].blank?
end
end
video_program.create_user_id = current_user.id
video_program.update_user_id = current_user.id
video_program.save
redirect_to params['referer_url']
end
def edit
if can_edit_or_delete?(@video_program)
@tags = @module_app.tags
@categories = @module_app.categories.enabled.authorized(current_user)
@statuses = []
else
render_401
end
end
def update
uid = params[:id].split('-').last
video_program = VideoProgram.find_by(:uid=>uid)
bps = video_program_params
bps[:tags] = bps[:tags].blank? ? [] : bps[:tags]
if !bps['video_program_links_attributes'].nil?
bps['video_program_links_attributes'].each do |idx,link|
bps['video_program_links_attributes'].delete(idx.to_s) if link['url'].blank?
end
end
bps[:update_user_id] = current_user.id
video_program.update_attributes!(bps)
now_video_program_page = VideoProgram.where(:title.ne => "")
.order_by(sort).map(&:id).map.with_index.select{|v,i| v==video_program.id}[0][1] rescue nil
now_video_program_page = now_video_program_page.nil? ? 0 : ((now_video_program_page+1).to_f/10).ceil
redirect_to admin_video_programs_path(:page=>now_video_program_page)
end
def destroy
@video_program.destroy
redirect_to "/admin/video_programs"
end
# def delete
# if params[:ids]
# VideoProgram.any_in(:uid => params[:ids]).destroy_all
# end
# redirect_to "/admin/video_programs"
# end
private
def set_video_program
@video_program = VideoProgram.find(params[:id])
end
def video_program_params
params.require(:video_program).permit!
end
end