OSDN Git Service

t#31471:add panel catch
[pettanr/pettanr.git] / app / controllers / panel_pictures_controller.rb
index 22f98d4..7c91306 100644 (file)
@@ -1,14 +1,15 @@
 class PanelPicturesController < ApplicationController
   layout 'test' if MagicNumber['test_layout']
   if MagicNumber['run_mode'] == 0
-    before_filter :authenticate_user, :only => []
+    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]
 
-  # GET /panel_pictures
-  # GET /panel_pictures.json
   def index
     @page = PanelPicture.page params[:page]
     @page_size = PanelPicture.page_size params[:page_size]
@@ -21,7 +22,7 @@ class PanelPicturesController < ApplicationController
   end
 
   def show
-    @panel_picture = PanelPicture.show(params[:id], [@author, @admin])
+    @panel_picture = PanelPicture.show(params[:id], [@user, @admin])
 
     respond_to do |format|
       format.html
@@ -47,4 +48,82 @@ class PanelPicturesController < ApplicationController
     end
   end
 
+  def new
+    @panel_picture = PanelPicture.new params[:panel_picture]
+    @panel_picture.supply_default
+    @panel = @panel_picture.panel
+
+    respond_to do |format|
+      format.html
+      format.json { render :json => @panel_picture.to_json(PanelPicture.show_json_opt) }
+    end
+  end
+
+  def edit
+    @panel_picture = PanelPicture.show(params[:id], @author)
+    @panel = Panel.edit(@panel_picture.panel.id, @author)
+    
+    respond_to do |format|
+      format.html
+    end
+  end
+
+  def create
+    @panel_picture = PanelPicture.new params[:panel_picture]
+    @panel_picture.supply_default
+    @panel_picture.overwrite 
+    @panel = Panel.edit(@panel_picture.panel.id, @author)
+    
+    respond_to do |format|
+      if @panel_picture.valid?
+        if @panel_picture.store @author
+          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
+          format.html { render action: "panels/new" }
+          format.json { render json: @panel.errors, status: :unprocessable_entity }
+        end
+      else
+        format.html { render action: "new" }
+        format.json { render json: @panel_picture.errors, status: :unprocessable_entity }
+      end
+    end
+  end
+
+  def update
+    @panel_picture = PanelPicture.show(params[:id], @author)
+    @panel_picture.attributes = params[:panel_picture]
+    @panel_picture.overwrite 
+    @panel = Panel.edit(@panel_picture.panel.id, @author)
+    
+    respond_to do |format|
+      if @panel_picture.store @author
+        flash[:notice] = I18n.t('flash.notice.updated', :model => PanelPicture.model_name.human)
+        format.html { redirect_to @panel_picture }
+        format.json { head :ok }
+      else
+        format.html { render action: "edit" }
+        format.json { render json: @panel_picture.errors, status: :unprocessable_entity }
+      end
+    end
+  end
+
+  def destroy
+    @panel_picture = PanelPicture.show(params[:id], @author)
+    @panel = Panel.edit(@panel_picture.panel.id, @author)
+    
+    respond_to do |format|
+      if @panel_picture.remove @author
+        flash[:notice] = I18n.t('flash.notice.destroyed', :model => PanelPicture.model_name.human)
+        format.html { redirect_to @panel }
+        format.json { head :ok }
+      else
+        flash[:notice] = I18n.t('flash.notice.not_destroyed', :model => PanelPicture.model_name.human)
+        format.html { redirect_to @panel_picture }
+        format.json { render json: @panel_picture.errors, status: :unprocessable_entity }
+      end
+    end
+  end
+  
 end