OSDN Git Service

work
[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]
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   before_filter :authenticate_admin!, :only => [:list, :browse]
12
13   def self.model
14     SpeechBalloon
15   end
16   
17   def index
18     public_list
19   end
20   
21   def show
22     @item = SpeechBalloon.show(params[:id], @operators)
23     respond_to do |format|
24       format.html {
25         @speech_balloon = @item
26       }
27       format_prof format
28       format.json { render json: @item.to_json(SpeechBalloon.show_json_opt) }
29     end
30   end
31   
32   def list
33     @speech_balloons = SpeechBalloon.all
34
35     respond_to do |format|
36       format.html { render layout: 'system' }
37       format.json { render json: @speech_balloons }
38     end
39   end
40
41   def browse
42     @speech_balloon = SpeechBalloon.find(params[:id])
43
44     respond_to do |format|
45       format.html { render layout: 'system' }
46       format.json { render json: @speech_balloon }
47     end
48   end
49
50   def new
51     raise Pettanr::NotWork unless @operators.author.working_panel
52     @speech_balloon_template = SpeechBalloonTemplate.show params[:speech_balloon_template_id], @operators
53     
54     @panel = Panel.edit(@operators.author.working_panel, @operators)
55     @speech_balloon = SpeechBalloon.new :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
56     @speech_balloon.boost
57     @speech_balloon.supply_default
58     @speech_balloon.get_balloon.supply_default 
59     @speech_balloon.get_speech.supply_default 
60     
61     respond_to do |format|
62       format.html {
63         render @speech_balloon_template.engine_name + '/speech_balloons/new'
64       }
65       format.json { render :json => @speech_balloon.to_json(SpeechBalloon.show_json_opt) }
66     end
67   end
68
69   def edit
70     @speech_balloon = SpeechBalloon.show(params[:id], @operators.author)
71     @speech_balloon_template = @speech_balloon.speech_balloon_template
72     @panel = Panel.edit(@speech_balloon.panel.id, @operators.author)
73     
74     @speech_balloon.boost
75     
76     respond_to do |format|
77       format.html {
78         render @speech_balloon_template.engine_name + '/speech_balloons/edit'
79       }
80     end
81   end
82
83   def create
84     raise Pettanr::NotWork unless @operators.author.working_panel
85     SpeechBalloon.fold_extend_settings params
86     @panel = Panel.edit(@operators.author.working_panel, @author)
87     @speech_balloon = SpeechBalloon.new 
88     @speech_balloon.attributes = params[:speech_balloon]
89     
90     @speech_balloon_template = @speech_balloon.speech_balloon_template
91     @speech_balloon.boost
92     
93     params[:speech_balloon][:balloon_attributes][:system_picture_id] = @speech_balloon.get_balloon.select_system_picture 
94     
95     @speech_balloon.overwrite @panel.id
96     
97     respond_to do |format|
98       if @speech_balloon.valid? and @speech_balloon.store(@author, params[:speech_balloon])
99         flash[:notice] = I18n.t('flash.notice.created', :model => Panel.model_name.human)
100         format.html { redirect_to @panel }
101         format.json { render json: @panel.panel_elements_as_json, status: :created, location: @panel }
102       else
103         flash[:notice] = I18n.t('flash.notice.not_created', :model => SpeechBalloon.model_name.human)
104         format.html {
105           render @speech_balloon_template.engine_name + '/speech_balloons/new'
106         }
107         format.json { render json: @speech_balloon.errors, status: :unprocessable_entity }
108       end
109     end
110   end
111
112   def update
113     SpeechBalloon.fold_extend_settings params
114     @speech_balloon = SpeechBalloon.show(params[:id], @operators.author)
115     @speech_balloon.attributes = params[:speech_balloon]
116     
117     @speech_balloon_template = @speech_balloon.speech_balloon_template
118     @speech_balloon.boost
119     
120     params[:speech_balloon][:balloon_attributes][:system_picture_id] = @speech_balloon.get_balloon.select_system_picture 
121     
122     @panel = Panel.edit(@speech_balloon.panel.id, @operators.author)
123     @speech_balloon.overwrite @panel.id
124     
125     respond_to do |format|
126       if @speech_balloon.valid? and @speech_balloon.store(@operators.author, params[:speech_balloon])
127         flash[:notice] = I18n.t('flash.notice.updated', :model => SpeechBalloon.model_name.human)
128         format.html { redirect_to @speech_balloon }
129         format.json { head :ok }
130       else
131         flash[:notice] = I18n.t('flash.notice.not_updated', :model => SpeechBalloon.model_name.human)
132         format.html {
133           render @speech_balloon.speech_balloon_template.engine_name + '/speech_balloons/edit'
134         }
135         format.json { render json: @speech_balloon.errors, status: :unprocessable_entity }
136       end
137     end
138   end
139
140   def destroy
141     @speech_balloon = SpeechBalloon.show(params[:id], @operators.author)
142     @panel = Panel.edit(@speech_balloon.panel.id, @operators.author)
143     
144     respond_to do |format|
145       if @speech_balloon.remove @operators.author
146         flash[:notice] = I18n.t('flash.notice.destroyed', :model => SpeechBalloon.model_name.human)
147         format.html { redirect_to @panel }
148         format.json { head :ok }
149       else
150         flash[:notice] = I18n.t('flash.notice.not_destroyed', :model => SpeechBalloon.model_name.human)
151         format.html { redirect_to @speech_balloon }
152         format.json { render json: @speech_balloon.errors, status: :unprocessable_entity }
153       end
154     end
155   end
156   
157 end