OSDN Git Service

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