OSDN Git Service

4aede9dbd75da6c5752b17c3e410af8b336df51f
[pettanr/pettanr.git] / app / controllers / speech_balloons_controller.rb
1 class SpeechBalloonsController < 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     SpeechBalloon
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 = SpeechBalloon.show(params[:id], @operators)
30     respond_to do |format|
31       format.html {
32         @speech_balloon = @item
33       }
34       format_prof format
35       format.json { render json: @item.to_json(SpeechBalloon.show_json_opt) }
36     end
37   end
38   
39   def new
40     raise Pettanr::NotWork unless @operators.author.working_panel
41     @speech_balloon_template = SpeechBalloonTemplate.show params[:speech_balloon_template_id], @operators
42     
43     @panel = Panel.edit(@operators.author.working_panel, @operators)
44     @speech_balloon = SpeechBalloon.new :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
45     @speech_balloon.boost
46     @speech_balloon.supply_default
47     @speech_balloon.get_balloon.supply_default 
48     @speech_balloon.get_speech.supply_default 
49     
50     respond_to do |format|
51       format.html {
52         render @speech_balloon_template.engine_name + '/speech_balloons/new'
53       }
54       format.json { render :json => @speech_balloon.to_json(SpeechBalloon.show_json_opt) }
55     end
56   end
57
58   def edit
59     @speech_balloon = SpeechBalloon.show(params[:id], @operators)
60     @speech_balloon_template = @speech_balloon.speech_balloon_template
61     @panel = Panel.edit(@speech_balloon.panel.id,@operators)
62     
63     @speech_balloon.boost
64     
65     respond_to do |format|
66       format.html {
67         render @speech_balloon_template.engine_name + '/speech_balloons/edit'
68       }
69     end
70   end
71
72   def create
73     raise Pettanr::NotWork unless @operators.author.working_panel
74     SpeechBalloon.fold_extend_settings params
75     @panel = Panel.edit(@operators.author.working_panel, @operators)
76     @speech_balloon = SpeechBalloon.new 
77     @speech_balloon.attributes = params[:speech_balloon]
78     
79     @speech_balloon_template = @speech_balloon.speech_balloon_template
80     @speech_balloon.boost
81     
82     params[:speech_balloon][:balloon_attributes][:system_picture_id] = @speech_balloon.get_balloon.select_system_picture 
83     
84     @speech_balloon.overwrite @panel.id
85     
86     respond_to do |format|
87       if @speech_balloon.valid? and @speech_balloon.store(@operators, params[:speech_balloon])
88         flash[:notice] = I18n.t('flash.notice.created', :model => Panel.model_name.human)
89         format.html { redirect_to @panel }
90         format.json { render json: @panel.panel_elements_as_json, status: :created, location: @panel }
91       else
92         flash[:notice] = I18n.t('flash.notice.not_created', :model => SpeechBalloon.model_name.human)
93         format.html {
94           render @speech_balloon_template.engine_name + '/speech_balloons/new'
95         }
96         format.json { render json: @speech_balloon.errors, status: :unprocessable_entity }
97       end
98     end
99   end
100
101   def update
102     SpeechBalloon.fold_extend_settings params
103     @speech_balloon = SpeechBalloon.show(params[:id], @operators)
104     @speech_balloon.attributes = params[:speech_balloon]
105     
106     @speech_balloon_template = @speech_balloon.speech_balloon_template
107     @speech_balloon.boost
108     
109     params[:speech_balloon][:balloon_attributes][:system_picture_id] = @speech_balloon.get_balloon.select_system_picture 
110     
111     @panel = Panel.edit(@speech_balloon.panel.id, @operators)
112     @speech_balloon.overwrite @panel.id
113     
114     respond_to do |format|
115       if @speech_balloon.valid? and @speech_balloon.store(@operators, params[:speech_balloon])
116         flash[:notice] = I18n.t('flash.notice.updated', :model => SpeechBalloon.model_name.human)
117         format.html { redirect_to @speech_balloon }
118         format.json { head :ok }
119       else
120         flash[:notice] = I18n.t('flash.notice.not_updated', :model => SpeechBalloon.model_name.human)
121         format.html {
122           render @speech_balloon.speech_balloon_template.engine_name + '/speech_balloons/edit'
123         }
124         format.json { render json: @speech_balloon.errors, status: :unprocessable_entity }
125       end
126     end
127   end
128
129   def destroy
130     @speech_balloon = SpeechBalloon.show(params[:id], @operators)
131     @panel = Panel.edit(@speech_balloon.panel.id, @operators)
132     
133     respond_to do |format|
134       if @speech_balloon.remove @operators.author
135         flash[:notice] = I18n.t('flash.notice.destroyed', :model => SpeechBalloon.model_name.human)
136         format.html { redirect_to @panel }
137         format.json { head :ok }
138       else
139         flash[:notice] = I18n.t('flash.notice.not_destroyed', :model => SpeechBalloon.model_name.human)
140         format.html { redirect_to @speech_balloon }
141         format.json { render json: @speech_balloon.errors, status: :unprocessable_entity }
142       end
143     end
144   end
145   
146 end