OSDN Git Service

t#32402:add file prof
[pettanr/pettanr.git] / app / controllers / ground_pictures_controller.rb
1 class GroundPicturesController < ApplicationController
2   layout 'test' if MagicNumber['test_layout']
3   if MagicNumber['run_mode'] == 0
4     before_filter :authenticate_user, :only => [:new, :edit, :create, :update, :destroy]
5     before_filter :authenticate_author, :only => [:new, :edit, :create, :update, :destroy]
6   else
7     before_filter :authenticate_reader, :only => [:index, :show]
8     before_filter :authenticate_user, :only => [:new, :edit, :create, :update, :destroy]
9     before_filter :authenticate_author, :only => [:new, :edit, :create, :update, :destroy]
10   end
11
12   def index
13     @page = GroundPicture.page params[:page]
14     @page_size = GroundPicture.page_size params[:page_size]
15     @ground_pictures = GroundPicture.list(@page, @page_size)
16
17     respond_to do |format|
18       format.html {
19         @paginate = GroundPicture.list_paginate(@page, @page_size)
20       }
21       format.json { render :json => @ground_pictures.to_json(GroundPicture.list_json_opt) }
22     end
23   end
24
25   def show
26     @ground_picture = GroundPicture.show(params[:id], [@user, @admin])
27     respond_to do |format|
28       format.html # show.html.erb
29       format.prof  { render :template => 'top/prof', :layout => true }
30       format.json { render json: @ground_picture.to_json(GroundPicture.show_json_opt) }
31     end
32   end
33   
34   def new
35     raise Pettanr::NotWork unless @author.working_panel
36     @picture = Picture.show params[:picture_id], @author
37     raise ActiveRecord::Forbidden unless @picture.enable?
38     @panel = Panel.edit(@author.working_panel, @author)
39     
40     @ground_picture = GroundPicture.new :panel_id => @panel.id, :picture_id => @picture.id
41     @ground_picture.supply_default
42
43     respond_to do |format|
44       format.html
45       format.json { render :json => @ground_picture.to_json(GroundPicture.show_json_opt) }
46     end
47   end
48
49   def edit
50     @ground_picture = GroundPicture.show(params[:id], @author)
51     @panel = Panel.edit(@ground_picture.panel.id, @author)
52     
53     respond_to do |format|
54       format.html
55     end
56   end
57
58   def create
59     raise Pettanr::NotWork unless @author.working_panel
60     @panel = Panel.edit(@author.working_panel, @author)
61     
62     @ground_picture = GroundPicture.new
63     @ground_picture.attributes = params[:ground_picture]
64     @ground_picture.overwrite @panel.id
65     
66     @picture = Picture.show @ground_picture.picture_id, @author
67     raise ActiveRecord::Forbidden unless @picture.enable?
68     
69     respond_to do |format|
70       if @ground_picture.valid?
71         if @ground_picture.store @author
72           flash[:notice] = I18n.t('flash.notice.created', :model => Panel.model_name.human)
73           format.html { redirect_to @panel }
74           format.json { render json: @panel.panel_elements_as_json, status: :created, location: @panel }
75         else
76           flash[:notice] = I18n.t('flash.notice.not_created', :model => GroundPicture.model_name.human)
77           format.html { render action: "new" }
78           format.json { render json: @panel.errors, status: :unprocessable_entity }
79         end
80       else
81         flash[:notice] = I18n.t('flash.notice.not_created', :model => GroundPicture.model_name.human)
82         format.html { render action: "new" }
83         format.json { render json: @ground_picture.errors, status: :unprocessable_entity }
84       end
85     end
86   end
87
88   def update
89     @ground_picture = GroundPicture.show(params[:id], @author)
90     @ground_picture.attributes = params[:ground_picture]
91     @panel = Panel.edit(@ground_picture.panel.id, @author)
92     @ground_picture.overwrite @panel.id
93     
94     @picture = Picture.show @ground_picture.picture_id, @author
95     raise ActiveRecord::Forbidden unless @picture.enable?
96     
97     respond_to do |format|
98       if @ground_picture.store @author
99         flash[:notice] = I18n.t('flash.notice.updated', :model => GroundPicture.model_name.human)
100         format.html { redirect_to @ground_picture }
101         format.json { head :ok }
102       else
103         flash[:notice] = I18n.t('flash.notice.not_updated', :model => GroundPicture.model_name.human)
104         format.html { render action: "edit" }
105         format.json { render json: @ground_picture.errors, status: :unprocessable_entity }
106       end
107     end
108   end
109
110   def destroy
111     @ground_picture = GroundPicture.show(params[:id], @author)
112     @panel = Panel.edit(@ground_picture.panel.id, @author)
113     
114     respond_to do |format|
115       if @ground_picture.remove @author
116         flash[:notice] = I18n.t('flash.notice.destroyed', :model => GroundPicture.model_name.human)
117         format.html { redirect_to @panel }
118         format.json { head :ok }
119       else
120         flash[:notice] = I18n.t('flash.notice.not_destroyed', :model => GroundPicture.model_name.human)
121         format.html { redirect_to @ground_picture }
122         format.json { render json: @ground_picture.errors, status: :unprocessable_entity }
123       end
124     end
125   end
126   
127 end