OSDN Git Service

work
[pettanr/pettanr.git] / app / controllers / speech_balloons_controller.rb
index 36ab839..6e95295 100644 (file)
@@ -1,66 +1,34 @@
 class SpeechBalloonsController < ApplicationController
-  before_filter :authenticate_user!, :only => [:index, :show]
-  before_filter :authenticate_admin!, :only => [:list, :browse, :create, :update, :destroy]
+  layout 'test' if MagicNumber['test_layout']
+  if MagicNumber['run_mode'] == 0
+    before_filter :authenticate_user, :only => [:new, :edit, :create, :update, :destroy]
+    before_filter :authenticate_author, :only => [:new, :edit, :create, :update, :destroy]
+  else
+    before_filter :authenticate_reader, :only => [:index, :show]
+    before_filter :authenticate_user, :only => [:new, :edit, :create, :update, :destroy]
+    before_filter :authenticate_author, :only => [:new, :edit, :create, :update, :destroy]
+  end
+  before_filter :authenticate_admin!, :only => [:list, :browse]
 
-  private
-  
-  def validate_param(ft)
-    res = nil
-    bl = ft[:size_count].to_i
-    tl = ft[:tail_count].to_i
-    if bl * tl == ft[:balloon_templates_attributes].size
-      flag = []
-      bl.times do |i|
-        flag[i] = []
-        tl.times do |j|
-          flag[i][j] = true
-        end
-      end
-      ft[:balloon_templates_attributes].each do |k, t|
-        flag[t[:size].to_i - 1][t[:tail].to_i - 1] = nil
-      end
-      if flag.flatten.compact.size == 0
-      else
-        res = "invalid variation"
-      end
-    else
-      res = "invalid template size"
-    end
-    res
+  def self.model
+    SpeechBalloon
   end
   
-  public
-  # GET /speech_balloons
-  # GET /speech_balloons.json
   def index
-    @speech_balloons = SpeechBalloon.all
-
-    respond_to do |format|
-      format.html # index.html.erb
-      format.json { render json: @speech_balloons }
-    end
+    public_list
   end
-
-  # GET /speech_balloons/1
-  # GET /speech_balloons/1.json
+  
   def show
-    @speech_balloon = SpeechBalloon.find(params[:id], include: {:balloon_templates => :speech_templates})
-
+    @item = SpeechBalloon.show(params[:id], @operators)
     respond_to do |format|
-      format.html # show.html.erb
-      format.json {
-        render :json => @speech_balloon.to_json(include: {
-          :balloon_templates => {:include => :speech_templates}
-        })
-      }
-      format.jsonp {
-        render :json => "callback(" + @speech_balloon.to_json(include: {
-          :balloon_templates => {:include => :speech_templates}
-        }) + ")"
+      format.html {
+        @speech_balloon = @item
       }
+      format_prof format
+      format.json { render json: @item.to_json(SpeechBalloon.show_json_opt) }
     end
   end
-
+  
   def list
     @speech_balloons = SpeechBalloon.all
 
@@ -79,52 +47,111 @@ class SpeechBalloonsController < ApplicationController
     end
   end
 
-  # POST /speech_balloons
-  # POST /speech_balloons.json
+  def new
+    raise Pettanr::NotWork unless @operators.author.working_panel
+    @speech_balloon_template = SpeechBalloonTemplate.show params[:speech_balloon_template_id], @operators
+    
+    @panel = Panel.edit(@operators.author.working_panel, @operators)
+    @speech_balloon = SpeechBalloon.new :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
+    @speech_balloon.boost
+    @speech_balloon.supply_default
+    @speech_balloon.get_balloon.supply_default 
+    @speech_balloon.get_speech.supply_default 
+    
+    respond_to do |format|
+      format.html {
+        render @speech_balloon_template.engine_name + '/speech_balloons/new'
+      }
+      format.json { render :json => @speech_balloon.to_json(SpeechBalloon.show_json_opt) }
+    end
+  end
+
+  def edit
+    @speech_balloon = SpeechBalloon.show(params[:id], @operators.author)
+    @speech_balloon_template = @speech_balloon.speech_balloon_template
+    @panel = Panel.edit(@speech_balloon.panel.id, @operators.author)
+    
+    @speech_balloon.boost
+    
+    respond_to do |format|
+      format.html {
+        render @speech_balloon_template.engine_name + '/speech_balloons/edit'
+      }
+    end
+  end
+
   def create
-    @speech_balloon = SpeechBalloon.new(params[:speech_balloon])
-    if em = validate_param(params[:speech_balloon])
-      respond_to do |format|
-        format.json { render json: em, status: :unprocessable_entity }
-      end
-    else
-      respond_to do |format|
-        if @speech_balloon.save
-          format.html { redirect_to @speech_balloon, notice: 'Speech balloon was successfully created.' }
-          format.json { render json: @speech_balloon, status: :created, location: @speech_balloon }
-        else
-          format.html { render action: "new" }
-          format.json { render json: @speech_balloon.errors, status: :unprocessable_entity }
-        end
+    raise Pettanr::NotWork unless @operators.author.working_panel
+    SpeechBalloon.fold_extend_settings params
+    @panel = Panel.edit(@operators.author.working_panel, @author)
+    @speech_balloon = SpeechBalloon.new 
+    @speech_balloon.attributes = params[:speech_balloon]
+    
+    @speech_balloon_template = @speech_balloon.speech_balloon_template
+    @speech_balloon.boost
+    
+    params[:speech_balloon][:balloon_attributes][:system_picture_id] = @speech_balloon.get_balloon.select_system_picture 
+    
+    @speech_balloon.overwrite @panel.id
+    
+    respond_to do |format|
+      if @speech_balloon.valid? and @speech_balloon.store(@author, params[:speech_balloon])
+        flash[:notice] = I18n.t('flash.notice.created', :model => Panel.model_name.human)
+        format.html { redirect_to @panel }
+        format.json { render json: @panel.panel_elements_as_json, status: :created, location: @panel }
+      else
+        flash[:notice] = I18n.t('flash.notice.not_created', :model => SpeechBalloon.model_name.human)
+        format.html {
+          render @speech_balloon_template.engine_name + '/speech_balloons/new'
+        }
+        format.json { render json: @speech_balloon.errors, status: :unprocessable_entity }
       end
     end
   end
 
-  # PUT /speech_balloons/1
-  # PUT /speech_balloons/1.json
   def update
-    @speech_balloon = SpeechBalloon.find(params[:id])
-
+    SpeechBalloon.fold_extend_settings params
+    @speech_balloon = SpeechBalloon.show(params[:id], @operators.author)
+    @speech_balloon.attributes = params[:speech_balloon]
+    
+    @speech_balloon_template = @speech_balloon.speech_balloon_template
+    @speech_balloon.boost
+    
+    params[:speech_balloon][:balloon_attributes][:system_picture_id] = @speech_balloon.get_balloon.select_system_picture 
+    
+    @panel = Panel.edit(@speech_balloon.panel.id, @operators.author)
+    @speech_balloon.overwrite @panel.id
+    
     respond_to do |format|
-      if @speech_balloon.update_attributes(params[:speech_balloon])
-        format.html { redirect_to @speech_balloon, notice: 'Speech balloon was successfully updated.' }
+      if @speech_balloon.valid? and @speech_balloon.store(@operators.author, params[:speech_balloon])
+        flash[:notice] = I18n.t('flash.notice.updated', :model => SpeechBalloon.model_name.human)
+        format.html { redirect_to @speech_balloon }
         format.json { head :ok }
       else
-        format.html { render action: "edit" }
+        flash[:notice] = I18n.t('flash.notice.not_updated', :model => SpeechBalloon.model_name.human)
+        format.html {
+          render @speech_balloon.speech_balloon_template.engine_name + '/speech_balloons/edit'
+        }
         format.json { render json: @speech_balloon.errors, status: :unprocessable_entity }
       end
     end
   end
 
-  # DELETE /speech_balloons/1
-  # DELETE /speech_balloons/1.json
   def destroy
-    @speech_balloon = SpeechBalloon.find(params[:id])
-    @speech_balloon.destroy
-
+    @speech_balloon = SpeechBalloon.show(params[:id], @operators.author)
+    @panel = Panel.edit(@speech_balloon.panel.id, @operators.author)
+    
     respond_to do |format|
-      format.html { redirect_to speech_balloons_url }
-      format.json { head :ok }
+      if @speech_balloon.remove @operators.author
+        flash[:notice] = I18n.t('flash.notice.destroyed', :model => SpeechBalloon.model_name.human)
+        format.html { redirect_to @panel }
+        format.json { head :ok }
+      else
+        flash[:notice] = I18n.t('flash.notice.not_destroyed', :model => SpeechBalloon.model_name.human)
+        format.html { redirect_to @speech_balloon }
+        format.json { render json: @speech_balloon.errors, status: :unprocessable_entity }
+      end
     end
   end
+  
 end