OSDN Git Service

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