techacademy/app/controllers/admin/all_page_controller.rb

154 lines
4.3 KiB
Ruby

class Admin::AllPageController < ApplicationController
before_action :authenticate_admin!
layout 'admin'
def index
@all_pages = AllPage.where('type_of != 3')
get_pp_pages
end
def show
end
def new
@all_page = AllPage.new
@articles = Article.all
end
def create
@all_page = AllPage.new(all_page_params)
@all_page.updated_by = current_admin.id
@all_page.updated_at = Time.now
@all_page.published = false
@articles = Article.all
if @all_page.save
if params[:publish] == '1'
params[:id] = @all_page.id
publish
end
redirect_to action: 'index'
else
render :new
end
end
def edit
@all_page = AllPage.find(params[:id])
@articles = Article.all
end
def update
@all_page = AllPage.find(params[:id])
@all_page.updated_by = current_admin.id
@all_page.updated_at = Time.now
@all_page.published = false
if @all_page.update_attributes(all_page_params)
if params[:publish] == '1'
params[:id] = @all_page.id
publish
end
else
@articles = Article.all
@all_page.errors.full_messages.each do |msg|
puts "<li>#{msg}</li>"
end
render :edit
end
end
def destroy
if @all_page = AllPage.find(params[:id])
@all_page.destroy
end
redirect_to action: 'index'
end
def pp_priority_up
pp_this = PublishedPage.find(params[:id])
pp_this_prio = pp_this.priority
pp_last = PublishedPage.where('(type_of = 1 OR type_of = 2) AND priority < ?', pp_this_prio).order('priority DESC').first
pp_this.priority = pp_last.priority
pp_this.save
pp_last.priority = pp_this_prio
pp_last.save
get_pp_pages
end
def pp_priority_down
pp_this = PublishedPage.find(params[:id])
pp_this_prio = pp_this.priority
pp_last = PublishedPage.where('(type_of = 1 OR type_of = 2) AND priority > ?', pp_this_prio).order('priority ASC').first
pp_this.priority = pp_last.priority
pp_this.save
pp_last.priority = pp_this_prio
pp_last.save
get_pp_pages
end
def publish
if @all_page = AllPage.find(params[:id])
if @published_page = @all_page.published_page
@published_page.name = @all_page.name
@published_page.title = @all_page.title
@published_page.meta_description = @all_page.meta_description
@published_page.all_page_id = @all_page.id
@published_page.nofollow = @all_page.nofollow
@published_page.type_of = @all_page.type_of
@published_page.full_text = @all_page.full_text
@published_page.article_id = @all_page.article_id
@published_page.updated_by = current_admin.id
@published_page.updated_at = Time.now
else
@published_page = PublishedPage.new
@published_page.name = @all_page.name
@published_page.title = @all_page.title
@published_page.meta_description = @all_page.meta_description
@published_page.all_page_id = @all_page.id
@published_page.nofollow = @all_page.nofollow
@published_page.type_of = @all_page.type_of
@published_page.full_text = @all_page.full_text
@published_page.article_id = @all_page.article_id
@published_page.priority = PublishedPage.all.size + 1;
@published_page.updated_by = current_admin.id
@published_page.updated_at = Time.now
@published_page.created_by = current_admin.id
@published_page.created_at = Time.now
end
if @published_page.save
@all_page.published = true
@all_page.save
else
if @published_page.errors
@published_page.errors.full_messages.each do |msg|
puts msg
end
end
end
end
end
def unpublish
if @all_page = AllPage.find(params[:id])
if @all_page.published_page
@all_page.published_page.destroy
end
@all_page.published = false
@all_page.save
end
end
def get_pp_pages
@published_pages = PublishedPage.where('type_of = 1 OR type_of = 2').order('priority ASC')
@pp_first = PublishedPage.where('type_of = 1 OR type_of = 2').order('priority ASC').first
@pp_last = PublishedPage.where('type_of = 1 OR type_of = 2').order('priority DESC').first
end
private
def all_page_params
params.require(:all_page).permit(:name, :article_id, :title, :type_of, :full_text)
end
end