OSDN Git Service

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