socialforge/app/controllers/statistics_controller.rb

129 lines
4.3 KiB
Ruby

class StatisticsController < ApplicationController
# GET /statistics
# GET /statistics.json
layout 'base_statistic'
before_filter :get_date, :only => [:index, :new]
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])
@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 = #{params[:sub_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
@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
@statistic = Statistic.find(params[:id])
@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
@statistic = Statistic.find(params[:id])
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: 'Statistic was successfully created.' }
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
@statistic = Statistic.find(params[:id])
respond_to do |format|
if @statistic.update_attributes(params[:statistic])
@statistic.save_attachments_containers(params[:attachments], User.current, true)
format.html { redirect_to @statistic, notice: 'Statistic was successfully updated.' }
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 = Statistic.find(params[:id])
@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
respond_to do |format|
format.js
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
end