OSDN Git Service

t#:
[pettanr/pettanr.git] / app / controllers / panel_pictures_controller.rb
index 94ebf94..cf55a8f 100644 (file)
 class PanelPicturesController < ApplicationController
-  before_filter :authenticate_user!, :only => [:index, :show]
-  before_filter :authenticate_admin!, :only => [:list, :browse]
+  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
 
-  # GET /panel_pictures
-  # GET /panel_pictures.json
+  def self.model
+    PanelPicture
+  end
+  
   def index
-    @page = PanelPicture.page params[:page]
-    @page_size = PanelPicture.page_size params[:page_size]
-    @panel_pictures = PanelPicture.list({}, @page, @page_size)
+    public_list
+  end
+
+  def show
+    @item = PanelPicture.show(params[:id], @operators)
 
     respond_to do |format|
-      format.html # index.html.erb
-      format.json { render :json => @panel_pictures.to_json(PanelPicture.list_json_opt) }
+      format.html {
+        @panel_picture = @item
+      }
+      format_prof format
+      format.json { render :json => @item.to_json(PanelPicture.show_json_opt) }
     end
   end
 
-  def list
-    @panel_pictures = PanelPicture.all
+  def new
+    raise Pettanr::NotWork unless @operators.author.working_panel
+    @picture = Picture.show params[:picture_id], @operators
+    raise ActiveRecord::Forbidden unless @picture.enable?
+    @panel = Panel.edit(@operators.author.working_panel, @operators)
+    
+    @panel_picture = PanelPicture.new :panel_id => @panel.id, :picture_id => @picture.id
+    @panel_picture.supply_default
+
+    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], @operators)
+    @panel = Panel.edit(@panel_picture.panel.id, @operators)
+    
     respond_to do |format|
-      format.html { render layout: 'system' }
-      format.json { render json: @panel_pictures }
+      format.html
     end
   end
 
+  def create
+    raise Pettanr::NotWork unless @author.working_panel
+    @panel = Panel.edit(@operators.author.working_panel, @operators)
+    
+    @panel_picture = PanelPicture.new 
+    @panel_picture.attributes = params[:panel_picture]
+    @panel_picture.overwrite @panel.id
+    
+    @picture = Picture.show @panel_picture.picture_id, @operators
+    raise ActiveRecord::Forbidden unless @picture.enable?
+    
+    respond_to do |format|
+      if @panel_picture.valid?
+        if @panel_picture.store @operators
+          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 => PanelPicture.model_name.human)
+          format.html { render action: "new" }
+          format.json { render json: @panel.errors, status: :unprocessable_entity }
+        end
+      else
+        flash[:notice] = I18n.t('flash.notice.not_created', :model => PanelPicture.model_name.human)
+        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], @operators)
+    @panel_picture.attributes = params[:panel_picture]
+    @panel = Panel.edit(@panel_picture.panel.id, @operators)
+    @panel_picture.overwrite @panel.id
+    
+    @picture = Picture.show @panel_picture.picture_id, @operators
+    raise ActiveRecord::Forbidden unless @picture.enable?
+    
+    respond_to do |format|
+      if @panel_picture.store @operators
+        flash[:notice] = I18n.t('flash.notice.updated', :model => PanelPicture.model_name.human)
+        format.html { redirect_to @panel_picture }
+        format.json { head :ok }
+      else
+        flash[:notice] = I18n.t('flash.notice.not_updated', :model => PanelPicture.model_name.human)
+        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], @operators)
+    @panel = Panel.edit(@panel_picture.panel.id, @operators)
+    
+    respond_to do |format|
+      if @panel_picture.remove @operators
+        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