OSDN Git Service

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