OSDN Git Service

180bed5234e51b70e042e380fcf1460f408fbd6a
[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 => [
7       :index, :show, :by_panel, :by_author, :count, :count_by_panel, :count_by_author
8     ]
9     before_filter :authenticate_user, :only => [:new, :edit, :create, :update, :destroy]
10     before_filter :authenticate_author, :only => [:new, :edit, :create, :update, :destroy]
11   end
12
13   def self.model
14     PanelPicture
15   end
16   
17   def index
18     filer_list
19   end
20   
21   def by_panel
22     filer_list
23   end
24   
25   def by_author
26     filer_list
27   end
28   
29   def show
30     @item = PanelPicture.show(params[:id], @operators)
31
32     respond_to do |format|
33       format.html {
34         @panel_picture = @item
35       }
36       format_prof format
37       format.json { render :json => @item.to_json(PanelPicture.show_json_opt) }
38     end
39   end
40
41   def count
42     list_count
43   end
44   
45   def count_by_panel
46     list_count
47   end
48   
49   def count_by_author
50     list_count
51   end
52   
53   def new
54     raise Pettanr::NotWork unless @operators.author.working_panel
55     @picture = Picture.show params[:picture_id], @operators
56     raise ActiveRecord::Forbidden unless @picture.enable?
57     @panel = Panel.edit(@operators.author.working_panel, @operators)
58     
59     @item = PanelPicture.new :panel_id => @panel.id, :picture_id => @picture.id
60     @item.supply_default
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       format.json { render :json => @panel_picture.to_json(PanelPicture.show_json_opt) }
68     end
69   end
70
71   def edit
72     @item = PanelPicture.show(params[:id], @operators)
73     @panel = Panel.edit(@item.panel.id, @operators)
74     
75     respond_to do |format|
76       format.html {
77         mounted = 1
78         form_manager = Pettanr::Application::manifest.form_managers[@item.form_name]
79         @form = form_manager.open @item, @operators, mounted
80       }
81     end
82   end
83
84   def create
85     raise Pettanr::NotWork unless @operators.author.working_panel
86     @panel = Panel.edit(@operators.author.working_panel, @operators)
87     
88     @panel_picture = PanelPicture.new 
89     @panel_picture.attributes = params[:panel_picture]
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.valid?
97         if @panel_picture.store @operators
98           flash[:notice] = I18n.t('flash.notice.created', :model => Panel.model_name.human)
99           format.html { redirect_to @panel }
100           format.json { render json: @panel.panel_elements_as_json, status: :created, location: @panel }
101         else
102           flash[:notice] = I18n.t('flash.notice.not_created', :model => PanelPicture.model_name.human)
103           format.html { render action: "new" }
104           format.json { render json: @panel.errors, status: :unprocessable_entity }
105         end
106       else
107         flash[:notice] = I18n.t('flash.notice.not_created', :model => PanelPicture.model_name.human)
108         format.html { render action: "new" }
109         format.json { render json: @panel_picture.errors, status: :unprocessable_entity }
110       end
111     end
112   end
113
114   def update
115     @panel_picture = PanelPicture.show(params[:id], @operators)
116     @panel_picture.attributes = params[:panel_picture]
117     @panel = Panel.edit(@panel_picture.panel.id, @operators)
118     @panel_picture.overwrite @panel.id
119     
120     @picture = Picture.show @panel_picture.picture_id, @operators
121     raise ActiveRecord::Forbidden unless @picture.enable?
122     
123     respond_to do |format|
124       if @panel_picture.store @operators
125         flash[:notice] = I18n.t('flash.notice.updated', :model => PanelPicture.model_name.human)
126         format.html { redirect_to @panel_picture }
127         format.json { head :ok }
128       else
129         flash[:notice] = I18n.t('flash.notice.not_updated', :model => PanelPicture.model_name.human)
130         format.html { render action: "edit" }
131         format.json { render json: @panel_picture.errors, status: :unprocessable_entity }
132       end
133     end
134   end
135
136   def destroy
137     @panel_picture = PanelPicture.show(params[:id], @operators)
138     @panel = Panel.edit(@panel_picture.panel.id, @operators)
139     
140     respond_to do |format|
141       if @panel_picture.remove @operators
142         flash[:notice] = I18n.t('flash.notice.destroyed', :model => PanelPicture.model_name.human)
143         format.html { redirect_to @panel }
144         format.json { head :ok }
145       else
146         flash[:notice] = I18n.t('flash.notice.not_destroyed', :model => PanelPicture.model_name.human)
147         format.html { redirect_to @panel_picture }
148         format.json { render json: @panel_picture.errors, status: :unprocessable_entity }
149       end
150     end
151   end
152   
153 end