OSDN Git Service

t#31896:
[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.prof  { render :template => 'top/prof', :layout => true }
31       format.prof  { render :template => 'top/prof', :layout => true }
32       format.json { render json: @speech_balloon.to_json(SpeechBalloon.show_json_opt) }
33     end
34   end
35   
36   def list
37     @speech_balloons = SpeechBalloon.all
38
39     respond_to do |format|
40       format.html { render layout: 'system' }
41       format.json { render json: @speech_balloons }
42     end
43   end
44
45   def browse
46     @speech_balloon = SpeechBalloon.find(params[:id])
47
48     respond_to do |format|
49       format.html { render layout: 'system' }
50       format.json { render json: @speech_balloon }
51     end
52   end
53
54   def new
55     raise Pettanr::NotWork unless @author.working_panel
56     @speech_balloon_template = SpeechBalloonTemplate.show params[:speech_balloon_template_id], @author
57     
58     @panel = Panel.edit(@author.working_panel, @author)
59     @speech_balloon = SpeechBalloon.new :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
60     @speech_balloon.boost
61     @speech_balloon.supply_default
62     @speech_balloon.get_balloon.supply_default 
63     @speech_balloon.get_speech.supply_default 
64     
65     respond_to do |format|
66       format.html {
67         render @speech_balloon_template.engine_name + '/speech_balloons/new'
68       }
69       format.json { render :json => @speech_balloon.to_json(SpeechBalloon.show_json_opt) }
70     end
71   end
72
73   def edit
74     @speech_balloon = SpeechBalloon.show(params[:id], @author)
75     @speech_balloon_template = @speech_balloon.speech_balloon_template
76     @panel = Panel.edit(@speech_balloon.panel.id, @author)
77     
78     @speech_balloon.boost
79     
80     respond_to do |format|
81       format.html {
82         render @speech_balloon_template.engine_name + '/speech_balloons/edit'
83       }
84     end
85   end
86
87   def create
88     raise Pettanr::NotWork unless @author.working_panel
89     SpeechBalloon.fold_extend_settings params
90     @panel = Panel.edit(@author.working_panel, @author)
91     @speech_balloon = SpeechBalloon.new 
92     @speech_balloon.attributes = params[:speech_balloon]
93     
94     @speech_balloon_template = @speech_balloon.speech_balloon_template
95     @speech_balloon.boost
96     
97     params[:speech_balloon][:balloon_attributes][:system_picture_id] = @speech_balloon.get_balloon.select_system_picture 
98     
99     @speech_balloon.overwrite @panel.id
100     
101     respond_to do |format|
102       if @speech_balloon.valid? and @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     end
114   end
115
116   def update
117     SpeechBalloon.fold_extend_settings params
118     @speech_balloon = SpeechBalloon.show(params[:id], @author)
119     @speech_balloon.attributes = params[:speech_balloon]
120     
121     @speech_balloon_template = @speech_balloon.speech_balloon_template
122     @speech_balloon.boost
123     
124     params[:speech_balloon][:balloon_attributes][:system_picture_id] = @speech_balloon.get_balloon.select_system_picture 
125     
126     @panel = Panel.edit(@speech_balloon.panel.id, @author)
127     @speech_balloon.overwrite @panel.id
128     
129     respond_to do |format|
130       if @speech_balloon.valid? and @speech_balloon.store(@author, params[:speech_balloon])
131         flash[:notice] = I18n.t('flash.notice.updated', :model => SpeechBalloon.model_name.human)
132         format.html { redirect_to @speech_balloon }
133         format.json { head :ok }
134       else
135         flash[:notice] = I18n.t('flash.notice.not_updated', :model => SpeechBalloon.model_name.human)
136         format.html {
137           render @speech_balloon.speech_balloon_template.engine_name + '/speech_balloons/edit'
138         }
139         format.json { render json: @speech_balloon.errors, status: :unprocessable_entity }
140       end
141     end
142   end
143
144   def destroy
145     @speech_balloon = SpeechBalloon.show(params[:id], @author)
146     @panel = Panel.edit(@speech_balloon.panel.id, @author)
147     
148     respond_to do |format|
149       if @speech_balloon.remove @author
150         flash[:notice] = I18n.t('flash.notice.destroyed', :model => SpeechBalloon.model_name.human)
151         format.html { redirect_to @panel }
152         format.json { head :ok }
153       else
154         flash[:notice] = I18n.t('flash.notice.not_destroyed', :model => SpeechBalloon.model_name.human)
155         format.html { redirect_to @speech_balloon }
156         format.json { render json: @speech_balloon.errors, status: :unprocessable_entity }
157       end
158     end
159   end
160   
161 end