158 lines
5.6 KiB
Ruby
158 lines
5.6 KiB
Ruby
# encoding=utf-8
|
|
class StatisticsController < ApplicationController
|
|
# GET /statistics
|
|
# GET /statistics.json
|
|
layout 'base_statistic'
|
|
before_filter :get_date, :only => [:index, :new]
|
|
before_filter :require_login
|
|
before_filter :find_statistic, :only => [:show, :edit, :update, :destroy]
|
|
before_filter :require_manager, :only =>[:edit, :update, :destroy]
|
|
|
|
def index
|
|
type = (params[:type] == "reorder_popu" ? "size" : "created_at")
|
|
order = (params[:order] == "asc" ? "asc" : "desc")
|
|
if params[:sub_category_id].present?
|
|
@statistics = Statistic.where(:sub_category_id => params[:sub_category_id])
|
|
sub_category_id = '(' + params[:sub_category_id].to_a.join(",") + ')'
|
|
@statistics = Statistic.find_by_sql("SELECT statistics.*, (select sum(filesize) from attachments where attachments.container_type='Statistic' and
|
|
attachments.container_id = statistics.id group by attachments.container_id) as size from statistics where sub_category_id in #{sub_category_id} order by #{type} #{order}")
|
|
else
|
|
if params[:main_category_id].present?
|
|
@statistics = Statistic.find_by_sql("SELECT statistics.*, (select sum(filesize) from attachments where attachments.container_type='Statistic' and
|
|
attachments.container_id = statistics.id group by attachments.container_id) as size from statistics where main_category_id = #{params[:main_category_id]} order by #{type} #{order}")
|
|
else
|
|
@statistics = Statistic.find_by_sql("SELECT statistics.*, (select sum(filesize) from attachments where attachments.container_type='Statistic' and
|
|
attachments.container_id = statistics.id group by attachments.container_id) as size from statistics order by #{type} #{order}")
|
|
end
|
|
end
|
|
@statistics_count = @statistics.count
|
|
@limit = 10
|
|
@is_remote = true
|
|
@statistics_pages = Paginator.new @statistics_count, @limit, params['page'] || 1
|
|
@offset ||= @statistics_pages.offset
|
|
@statistics = paginateHelper @statistics, @limit
|
|
respond_to do |format|
|
|
format.js
|
|
format.html # index.html.erb
|
|
format.json { render json: @statistics }
|
|
end
|
|
end
|
|
|
|
def search
|
|
|
|
end
|
|
|
|
# GET /statistics/1
|
|
# GET /statistics/1.json
|
|
def show
|
|
@attachments = @statistic.attachments
|
|
respond_to do |format|
|
|
format.html # show.html.erb
|
|
format.json { render json: @statistic }
|
|
end
|
|
end
|
|
|
|
# GET /statistics/new
|
|
# GET /statistics/new.json
|
|
def new
|
|
@statistic = Statistic.new
|
|
@sub_categories = @main_categories.first.sub_categories
|
|
respond_to do |format|
|
|
format.html # new.html.erb
|
|
format.json { render json: @statistic }
|
|
end
|
|
end
|
|
|
|
# GET /statistics/1/edit
|
|
def edit
|
|
@main_categories = MainCategory.all
|
|
main_category = MainCategory.find(@statistic.main_category_id)
|
|
@sub_categories = main_category.sub_categories
|
|
end
|
|
|
|
# POST /statistics
|
|
# POST /statistics.json
|
|
def create
|
|
@statistic = Statistic.new(params[:statistic])
|
|
@statistic.user_id = User.current.id
|
|
@statistic.save_attachments_containers(params[:attachments], User.current, true)
|
|
respond_to do |format|
|
|
if @statistic.save
|
|
format.html { redirect_to @statistic, notice: '创建成功' }
|
|
format.json { render json: @statistic, status: :created, location: @statistic }
|
|
else
|
|
format.html { render action: "new" }
|
|
format.json { render json: @statistic.errors, status: :unprocessable_entity }
|
|
end
|
|
end
|
|
end
|
|
|
|
# PUT /statistics/1
|
|
# PUT /statistics/1.json
|
|
def update
|
|
|
|
respond_to do |format|
|
|
if @statistic.update_attributes(params[:statistic])
|
|
# @statistic.save_attachments_containers(params[:attachments], User.current, true)
|
|
if params[:attachments].present?
|
|
params[:attachments].each_value do |attachment|
|
|
temp_attachment = Attachment.where(:id => attachment['attachment_id']).first
|
|
if temp_attachment.present? && temp_attachment.container_id.blank?
|
|
temp_attachment.update_attributes(:container_id => @statistic.id, :container_type => "Statistic")
|
|
end
|
|
end
|
|
end
|
|
|
|
format.html { redirect_to @statistic, notice: '更新成功' }
|
|
format.json { head :no_content }
|
|
else
|
|
format.html { render action: "edit" }
|
|
format.json { render json: @statistic.errors, status: :unprocessable_entity }
|
|
end
|
|
end
|
|
end
|
|
|
|
# DELETE /statistics/1
|
|
# DELETE /statistics/1.json
|
|
def destroy
|
|
@statistic.destroy
|
|
|
|
respond_to do |format|
|
|
format.html { redirect_to statistics_url }
|
|
format.json { head :no_content }
|
|
end
|
|
end
|
|
|
|
def get_sub_category
|
|
if params[:main_category_id].present?
|
|
main_category = MainCategory.find(params[:main_category_id])
|
|
@sub_categories = main_category.sub_categories
|
|
else
|
|
@sub_categories = SubCategory.all
|
|
end
|
|
if params[:come_from] == "statistics_index"
|
|
redirect_to statistics_url(:come_from => "statistics_index", :sub_category_id => (@sub_categories.present? ? nil : @sub_categories.map(&:id)), :main_category_id => params[:main_category_id])
|
|
else
|
|
respond_to do |format|
|
|
format.js
|
|
end
|
|
end
|
|
end
|
|
|
|
private
|
|
def get_date
|
|
@main_categories = MainCategory.all
|
|
@sub_categories = params[:main_category_id].present? ? SubCategory.where(:main_category_id => params[:main_category_id]) : SubCategory.all
|
|
end
|
|
|
|
def find_statistic
|
|
@statistic = Statistic.find(params[:id])
|
|
rescue ActiveRecord::RecordNotFound
|
|
render_404
|
|
end
|
|
|
|
def require_manager
|
|
render_403 unless (User.current.id == @statistic.user_id || User.current.admin?)
|
|
end
|
|
end
|