OSDN Git Service

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