OSDN Git Service

work
[pettanr/pettanr.git] / app / controllers / panel_pictures_controller.rb
1 class PanelPicturesController < 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 self.model
13     PanelPicture
14   end
15   
16   def index
17     public_list
18   end
19
20   def show
21     @item = PanelPicture.show(params[:id], @operators)
22
23     respond_to do |format|
24       format.html {
25         @panel_picture = @item
26       }
27       format_prof format
28       format.json { render :json => @item.to_json(PanelPicture.show_json_opt) }
29     end
30   end
31
32   def new
33     raise Pettanr::NotWork unless @operators.author.working_panel
34     @picture = Picture.show params[:picture_id], @operators
35     raise ActiveRecord::Forbidden unless @picture.enable?
36     @panel = Panel.edit(@operators.author.working_panel, @operators)
37     
38     @panel_picture = PanelPicture.new :panel_id => @panel.id, :picture_id => @picture.id
39     @panel_picture.supply_default
40
41     respond_to do |format|
42       format.html
43       format.json { render :json => @panel_picture.to_json(PanelPicture.show_json_opt) }
44     end
45   end
46
47   def edit
48     @panel_picture = PanelPicture.show(params[:id], @operators)
49     @panel = Panel.edit(@panel_picture.panel.id, @operators)
50     
51     respond_to do |format|
52       format.html
53     end
54   end
55
56   def create
57     raise Pettanr::NotWork unless @operators.author.working_panel
58     @panel = Panel.edit(@operators.author.working_panel, @operators)
59     
60     @panel_picture = PanelPicture.new 
61     @panel_picture.attributes = params[:panel_picture]
62     @panel_picture.overwrite @panel.id
63     
64     @picture = Picture.show @panel_picture.picture_id, @operators
65     raise ActiveRecord::Forbidden unless @picture.enable?
66     
67     respond_to do |format|
68       if @panel_picture.valid?
69         if @panel_picture.store @operators
70           flash[:notice] = I18n.t('flash.notice.created', :model => Panel.model_name.human)
71           format.html { redirect_to @panel }
72           format.json { render json: @panel.panel_elements_as_json, status: :created, location: @panel }
73         else
74           flash[:notice] = I18n.t('flash.notice.not_created', :model => PanelPicture.model_name.human)
75           format.html { render action: "new" }
76           format.json { render json: @panel.errors, status: :unprocessable_entity }
77         end
78       else
79         flash[:notice] = I18n.t('flash.notice.not_created', :model => PanelPicture.model_name.human)
80         format.html { render action: "new" }
81         format.json { render json: @panel_picture.errors, status: :unprocessable_entity }
82       end
83     end
84   end
85
86   def update
87     @panel_picture = PanelPicture.show(params[:id], @operators)
88     @panel_picture.attributes = params[:panel_picture]
89     @panel = Panel.edit(@panel_picture.panel.id, @operators)
90     @panel_picture.overwrite @panel.id
91     
92     @picture = Picture.show @panel_picture.picture_id, @operators
93     raise ActiveRecord::Forbidden unless @picture.enable?
94     
95     respond_to do |format|
96       if @panel_picture.store @operators
97         flash[:notice] = I18n.t('flash.notice.updated', :model => PanelPicture.model_name.human)
98         format.html { redirect_to @panel_picture }
99         format.json { head :ok }
100       else
101         flash[:notice] = I18n.t('flash.notice.not_updated', :model => PanelPicture.model_name.human)
102         format.html { render action: "edit" }
103         format.json { render json: @panel_picture.errors, status: :unprocessable_entity }
104       end
105     end
106   end
107
108   def destroy
109     @panel_picture = PanelPicture.show(params[:id], @operators)
110     @panel = Panel.edit(@panel_picture.panel.id, @operators)
111     
112     respond_to do |format|
113       if @panel_picture.remove @operators
114         flash[:notice] = I18n.t('flash.notice.destroyed', :model => PanelPicture.model_name.human)
115         format.html { redirect_to @panel }
116         format.json { head :ok }
117       else
118         flash[:notice] = I18n.t('flash.notice.not_destroyed', :model => PanelPicture.model_name.human)
119         format.html { redirect_to @panel_picture }
120         format.json { render json: @panel_picture.errors, status: :unprocessable_entity }
121       end
122     end
123   end
124   
125 end