2018-02-09 15:19:17 +08:00
# encoding=utf-8
2018-02-05 12:03:35 +08:00
class StatisticsController < ApplicationController
# GET /statistics
# GET /statistics.json
2018-02-06 17:41:39 +08:00
layout 'base_statistic'
2018-02-08 15:10:53 +08:00
before_filter :get_date , :only = > [ :index , :new ]
2018-02-09 11:27:32 +08:00
before_filter :require_login
before_filter :find_statistic , :only = > [ :show , :edit , :update , :destroy ]
before_filter :require_manager , :only = > [ :edit , :update , :destroy ]
2018-02-06 17:41:39 +08:00
2018-02-05 12:03:35 +08:00
def index
2018-02-09 09:55:21 +08:00
type = ( params [ :type ] == " reorder_popu " ? " size " : " created_at " )
order = ( params [ :order ] == " asc " ? " asc " : " desc " )
2018-02-08 15:12:36 +08:00
if params [ :sub_category_id ] . present?
@statistics = Statistic . where ( :sub_category_id = > params [ :sub_category_id ] )
2018-02-09 17:00:12 +08:00
sub_category_id = '(' + params [ :sub_category_id ] . to_a . join ( " , " ) + ')'
2018-02-08 16:16:28 +08:00
@statistics = Statistic . find_by_sql ( " SELECT statistics.*, (select sum(filesize) from attachments where attachments.container_type='Statistic' and
2018-02-09 17:00:12 +08:00
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}")
2018-02-08 15:12:36 +08:00
else
2018-02-09 18:12:53 +08:00
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
2018-02-09 09:55:21 +08:00
attachments . container_id = statistics . id group by attachments . container_id ) as size from statistics order by #{type} #{order}")
2018-02-09 18:12:53 +08:00
end
2018-02-08 15:12:36 +08:00
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
2018-02-05 12:03:35 +08:00
respond_to do | format |
2018-02-08 15:12:36 +08:00
format . js
2018-02-05 12:03:35 +08:00
format . html # index.html.erb
format . json { render json : @statistics }
end
end
2018-02-07 16:19:16 +08:00
def search
end
2018-02-05 12:03:35 +08:00
# GET /statistics/1
# GET /statistics/1.json
def show
2018-02-05 15:13:03 +08:00
@attachments = @statistic . attachments
2018-02-05 12:03:35 +08:00
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
2018-02-08 10:28:45 +08:00
@sub_categories = @main_categories . first . sub_categories
2018-02-05 12:03:35 +08:00
respond_to do | format |
format . html # new.html.erb
format . json { render json : @statistic }
end
end
# GET /statistics/1/edit
def edit
2018-02-08 15:10:53 +08:00
@main_categories = MainCategory . all
main_category = MainCategory . find ( @statistic . main_category_id )
@sub_categories = main_category . sub_categories
2018-02-05 12:03:35 +08:00
end
# POST /statistics
# POST /statistics.json
def create
@statistic = Statistic . new ( params [ :statistic ] )
2018-02-08 11:24:04 +08:00
@statistic . user_id = User . current . id
2018-02-05 15:13:03 +08:00
@statistic . save_attachments_containers ( params [ :attachments ] , User . current , true )
2018-02-05 12:03:35 +08:00
respond_to do | format |
if @statistic . save
2018-02-09 15:19:17 +08:00
format . html { redirect_to @statistic , notice : '创建成功' }
2018-02-05 12:03:35 +08:00
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
2018-02-09 15:19:17 +08:00
2018-02-05 12:03:35 +08:00
respond_to do | format |
if @statistic . update_attributes ( params [ :statistic ] )
2018-02-09 15:03:28 +08:00
# @statistic.save_attachments_containers(params[:attachments], User.current, true)
2018-02-09 16:08:52 +08:00
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
2018-02-09 15:03:28 +08:00
end
end
2018-02-09 16:08:52 +08:00
2018-02-09 15:19:17 +08:00
format . html { redirect_to @statistic , notice : '更新成功' }
2018-02-05 12:03:35 +08:00
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
2018-02-07 16:01:14 +08:00
def get_sub_category
2018-02-08 16:25:49 +08:00
if params [ :main_category_id ] . present?
2018-02-08 15:37:30 +08:00
main_category = MainCategory . find ( params [ :main_category_id ] )
@sub_categories = main_category . sub_categories
2018-02-08 16:25:49 +08:00
else
@sub_categories = SubCategory . all
2018-02-08 15:37:30 +08:00
end
2018-02-09 18:34:47 +08:00
if params [ :come_from ] == " statistics_index "
2018-02-09 18:38:07 +08:00
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 ] )
2018-02-09 18:34:47 +08:00
else
respond_to do | format |
2018-02-09 17:00:12 +08:00
format . js
end
2018-02-07 16:01:14 +08:00
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
2018-02-09 15:03:28 +08:00
2018-02-09 11:27:32 +08:00
def find_statistic
2018-02-09 15:19:17 +08:00
@statistic = Statistic . find ( params [ :id ] )
rescue ActiveRecord :: RecordNotFound
render_404
2018-02-09 11:27:32 +08:00
end
def require_manager
render_403 unless ( User . current . id == @statistic . user_id || User . current . admin? )
end
2018-02-05 12:03:35 +08:00
end