OSDN Git Service

t#32326:fix 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 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     raise Pettanr::NotWork unless @author.working_panel
54     @speech_balloon_template = SpeechBalloonTemplate.show params[:speech_balloon_template_id], @author
55     
56     @panel = Panel.edit(@author.working_panel, @author)
57     @speech_balloon = SpeechBalloon.new :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
58     @speech_balloon.boost
59     @speech_balloon.supply_default
60     @speech_balloon.get_balloon.supply_default 
61     @speech_balloon.get_speech.supply_default 
62     
63     respond_to do |format|
64       format.html {
65         render @speech_balloon_template.engine_name + '/speech_balloons/new'
66       }
67       format.json { render :json => @speech_balloon.to_json(SpeechBalloon.show_json_opt) }
68     end
69   end
70
71   def edit
72     @speech_balloon = SpeechBalloon.show(params[:id], @author)
73     @speech_balloon_template = @speech_balloon.speech_balloon_template
74     @panel = Panel.edit(@speech_balloon.panel.id, @author)
75     
76     @speech_balloon.boost
77     
78     respond_to do |format|
79       format.html {
80         render @speech_balloon_template.engine_name + '/speech_balloons/edit'
81       }
82     end
83   end
84
85   def create
86     raise Pettanr::NotWork unless @author.working_panel
87     SpeechBalloon.fold_extend_settings params
88     @panel = Panel.edit(@author.working_panel, @author)
89     @speech_balloon = SpeechBalloon.new 
90     @speech_balloon.attributes = params[:speech_balloon]
91     
92     @speech_balloon_template = @speech_balloon.speech_balloon_template
93     @speech_balloon.boost
94     
95     params[:speech_balloon][:balloon_attributes][:system_picture_id] = @speech_balloon.get_balloon.select_system_picture 
96     
97     @speech_balloon.overwrite @panel.id
98     
99     respond_to do |format|
100       if @speech_balloon.valid? and @speech_balloon.store(@author, params[:speech_balloon])
101         flash[:notice] = I18n.t('flash.notice.created', :model => Panel.model_name.human)
102         format.html { redirect_to @panel }
103         format.json { render json: @panel.panel_elements_as_json, status: :created, location: @panel }
104       else
105         flash[:notice] = I18n.t('flash.notice.not_created', :model => SpeechBalloon.model_name.human)
106         format.html {
107           render @speech_balloon_template.engine_name + '/speech_balloons/new'
108         }
109         format.json { render json: @speech_balloon.errors, status: :unprocessable_entity }
110       end
111     end
112   end
113
114   def update
115     SpeechBalloon.fold_extend_settings params
116     @speech_balloon = SpeechBalloon.show(params[:id], @author)
117     @speech_balloon.attributes = params[:speech_balloon]
118     
119     @speech_balloon_template = @speech_balloon.speech_balloon_template
120     @speech_balloon.boost
121     
122     params[:speech_balloon][:balloon_attributes][:system_picture_id] = @speech_balloon.get_balloon.select_system_picture 
123     
124     @panel = Panel.edit(@speech_balloon.panel.id, @author)
125     @speech_balloon.overwrite @panel.id
126     
127     respond_to do |format|
128       if @speech_balloon.valid? and @speech_balloon.store(@author, params[:speech_balloon])
129         flash[:notice] = I18n.t('flash.notice.updated', :model => SpeechBalloon.model_name.human)
130         format.html { redirect_to @speech_balloon }
131         format.json { head :ok }
132       else
133         flash[:notice] = I18n.t('flash.notice.not_updated', :model => SpeechBalloon.model_name.human)
134         format.html {
135           render @speech_balloon.speech_balloon_template.engine_name + '/speech_balloons/edit'
136         }
137         format.json { render json: @speech_balloon.errors, status: :unprocessable_entity }
138       end
139     end
140   end
141
142   def destroy
143     @speech_balloon = SpeechBalloon.show(params[:id], @author)
144     @panel = Panel.edit(@speech_balloon.panel.id, @author)
145     
146     respond_to do |format|
147       if @speech_balloon.remove @author
148         flash[:notice] = I18n.t('flash.notice.destroyed', :model => SpeechBalloon.model_name.human)
149         format.html { redirect_to @panel }
150         format.json { head :ok }
151       else
152         flash[:notice] = I18n.t('flash.notice.not_destroyed', :model => SpeechBalloon.model_name.human)
153         format.html { redirect_to @speech_balloon }
154         format.json { render json: @speech_balloon.errors, status: :unprocessable_entity }
155       end
156     end
157   end
158   
159 end