OSDN Git Service

t#31715:speech_balloon boost
[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 index
14     @page = SpeechBalloon.page params[:page]
15     @page_size = SpeechBalloon.page_size params[:page_size]
16     @speech_balloons = SpeechBalloon.list(@page, @page_size)
17
18     respond_to do |format|
19       format.html {
20         @paginate = SpeechBalloon.list_paginate(@page, @page_size)
21       }
22       format.json { render json: @speech_balloons.to_json(SpeechBalloon.list_json_opt) }
23     end
24   end
25   
26   def show
27     @speech_balloon = SpeechBalloon.show(params[:id], [@user, @admin])
28     respond_to do |format|
29       format.html # show.html.erb
30       format.json { render json: @speech_balloon.to_json(SpeechBalloon.show_json_opt) }
31     end
32   end
33   
34   def list
35     @speech_balloons = SpeechBalloon.all
36
37     respond_to do |format|
38       format.html { render layout: 'system' }
39       format.json { render json: @speech_balloons }
40     end
41   end
42
43   def browse
44     @speech_balloon = SpeechBalloon.find(params[:id])
45
46     respond_to do |format|
47       format.html { render layout: 'system' }
48       format.json { render json: @speech_balloon }
49     end
50   end
51
52   def new
53     @speech_balloon_template = SpeechBalloonTemplate.show params[:speech_balloon_template_id], @author
54     
55     @panel = Panel.edit(@author.working_panel, @author)
56     @speech_balloon = SpeechBalloon.new :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
57     @speech_balloon.boost
58     @speech_balloon.supply_default
59     @speech_balloon.get_balloon.supply_default 
60     @speech_balloon.get_speech.supply_default 
61     
62     respond_to do |format|
63       format.html {
64         render @speech_balloon_template.engine_name + '/speech_balloons/new'
65       }
66       format.json { render :json => @speech_balloon.to_json(SpeechBalloon.show_json_opt) }
67     end
68   end
69
70   def edit
71     @speech_balloon = SpeechBalloon.show(params[:id], @author)
72     @speech_balloon_template = @speech_balloon.speech_balloon_template
73     @panel = Panel.edit(@speech_balloon.panel.id, @author)
74     
75     @speech_balloon.boost
76     
77     respond_to do |format|
78       format.html {
79         render @speech_balloon_template.engine_name + '/speech_balloons/edit'
80       }
81     end
82   end
83
84   def create
85     SpeechBalloon.fold_extend_settings params
86     @panel = Panel.edit(@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       sbv = @speech_balloon.extend_speech_balloon.valid?
99       bv = @speech_balloon.get_balloon.extend_balloon.valid?
100       sv = @speech_balloon.get_speech.extend_speech.valid?
101       if @speech_balloon.valid? and @speech_balloon.get_balloon.valid? and @speech_balloon.get_speech.valid? and sbv and bv and sv
102         if @speech_balloon.store @author, params[:speech_balloon]
103           flash[:notice] = I18n.t('flash.notice.created', :model => Panel.model_name.human)
104           format.html { redirect_to @panel }
105           format.json { render json: @panel.panel_elements_as_json, status: :created, location: @panel }
106         else
107           flash[:notice] = I18n.t('flash.notice.not_created', :model => SpeechBalloon.model_name.human)
108           format.html {
109             render @speech_balloon_template.engine_name + '/speech_balloons/new'
110           }
111           format.json { render json: @speech_balloon.errors, status: :unprocessable_entity }
112         end
113       else
114         flash[:notice] = I18n.t('flash.notice.not_created', :model => SpeechBalloon.model_name.human)
115         format.html {
116           render @speech_balloon_template.engine_name + '/speech_balloons/new'
117         }
118         format.json { render json: @speech_balloon.errors, status: :unprocessable_entity }
119       end
120     end
121   end
122
123   def update
124     SpeechBalloon.fold_extend_settings params
125     @speech_balloon = SpeechBalloon.show(params[:id], @author)
126     @speech_balloon.attributes = params[:speech_balloon]
127     
128     @speech_balloon_template = @speech_balloon.speech_balloon_template
129     @speech_balloon.boost
130     
131     params[:speech_balloon][:balloon_attributes][:system_picture_id] = @speech_balloon.get_balloon.select_system_picture 
132     
133     @panel = Panel.edit(@speech_balloon.panel.id, @author)
134     @speech_balloon.overwrite @panel.id
135     
136     respond_to do |format|
137       sbv = @speech_balloon.extend_speech_balloon.valid?
138       bv = @speech_balloon.get_balloon.extend_balloon.valid?
139       sv = @speech_balloon.get_speech.extend_speech.valid?
140       if @speech_balloon.valid? and @speech_balloon.get_balloon.valid? and @speech_balloon.get_speech.valid? and sbv and bv and sv
141         if @speech_balloon.store @author, params[:speech_balloon]
142           flash[:notice] = I18n.t('flash.notice.updated', :model => SpeechBalloon.model_name.human)
143           format.html { redirect_to @speech_balloon }
144           format.json { head :ok }
145         else
146           flash[:notice] = I18n.t('flash.notice.not_updated', :model => SpeechBalloon.model_name.human)
147           format.html {
148             render @speech_balloon.speech_balloon_template.engine_name + '/speech_balloons/edit'
149           }
150           format.json { render json: @speech_balloon.errors, status: :unprocessable_entity }
151         end
152       else
153         flash[:notice] = I18n.t('flash.notice.not_updated', :model => SpeechBalloon.model_name.human)
154         format.html {
155           render @speech_balloon.speech_balloon_template.engine_name + '/speech_balloons/edit'
156         }
157         format.json { render json: @speech_balloon.errors, status: :unprocessable_entity }
158       end
159     end
160   end
161
162   def destroy
163     @speech_balloon = SpeechBalloon.show(params[:id], @author)
164     @panel = Panel.edit(@speech_balloon.panel.id, @author)
165     
166     respond_to do |format|
167       if @speech_balloon.remove @author
168         flash[:notice] = I18n.t('flash.notice.destroyed', :model => SpeechBalloon.model_name.human)
169         format.html { redirect_to @panel }
170         format.json { head :ok }
171       else
172         flash[:notice] = I18n.t('flash.notice.not_destroyed', :model => SpeechBalloon.model_name.human)
173         format.html { redirect_to @speech_balloon }
174         format.json { render json: @speech_balloon.errors, status: :unprocessable_entity }
175       end
176     end
177   end
178   
179 end