OSDN Git Service

t#32025:comic rename
[pettanr/pettanr.git] / app / controllers / scroll_panels_controller.rb
diff --git a/app/controllers/scroll_panels_controller.rb b/app/controllers/scroll_panels_controller.rb
new file mode 100644 (file)
index 0000000..5e0b739
--- /dev/null
@@ -0,0 +1,157 @@
+class ScrollPanelsController < ApplicationController
+  layout 'test' if MagicNumber['test_layout']
+  if MagicNumber['run_mode'] == 0
+    before_filter :authenticate_user, :only => [:new, :create, :edit, :update, :destroy]
+    before_filter :authenticate_author, :only => [:new, :create, :edit, :update, :destroy]
+  else
+    before_filter :authenticate_reader, :only => [:index, :show, :scroll]
+    before_filter :authenticate_user, :only => [:new, :create, :edit, :update, :destroy]
+    before_filter :authenticate_author, :only => [:new, :create, :edit, :update, :destroy]
+  end
+  before_filter :authenticate_admin!, :only => [:list, :browse]
+
+  def index
+    @page = ScrollPanel.page params[:page]
+    @page_size = ScrollPanel.page_size params[:page_size]
+    @scroll_panels = ScrollPanel.list(@page, @page_size)
+
+    respond_to do |format|
+      format.html {
+        @paginate = ScrollPanel.list_paginate(@page, @page_size)
+      }
+      format.json { render :json => @scroll_panels.to_json(ScrollPanel.list_json_opt) }
+    end
+  end
+
+  def show
+    @scroll_panel = ScrollPanel.show(params[:id], [@user, @admin])
+
+    respond_to do |format|
+      format.html # show.html.erb
+      format.json { render json: @scroll_panel.scroll_panel_as_json(@author) }
+    end
+  end
+  
+  def scroll
+    @scroll = Scroll.show(params[:id], [@user, @admin])
+    cnt = ScrollPanel.count(:conditions => ['scroll_id = ?', @scroll.id]).to_i
+    @offset = ScrollPanel.offset cnt, params[:offset]
+    @panel_count = ScrollPanel.panel_count cnt, params[:count]
+    @scroll_panels = ScrollPanel.play_list(@scroll, @author, @offset, @panel_count)
+    respond_to do |format|
+      format.html {
+        @prev_offset = if @offset > 0
+          if @offset - @panel_count < 0
+            0
+          else
+            @offset - @panel_count
+          end
+        else
+          nil
+        end
+        @next_offset = if @offset + @panel_count > cnt
+          nil
+        else
+          @offset + @panel_count
+        end
+        if @author
+          @new_panels = Panel.mylist(@author, 1, 5)
+        end
+      }
+      format.json {render text: ScrollPanel.list_as_json_text(@scroll_panels, @author)}
+      format.jsonp {
+        render :json => "callback(" + @scroll_panels.to_json_list + ");"
+      }
+    end
+  end
+  
+  def list
+    @scroll_panels = ScrollPanel.all
+
+    respond_to do |format|
+      format.html { render layout: 'system' }# index.html.erb
+      format.json { render json: @scroll_panels }
+    end
+  end
+
+  def browse
+    @scroll_panel = ScrollPanel.find(params[:id])
+
+    respond_to do |format|
+      format.html { render layout: 'system' } # show.html.erb
+      format.json { render json: @scroll_panel }
+    end
+  end
+  
+  def new
+    @scroll_panel = ScrollPanel.new 
+    @scroll_panel.supply_default
+    respond_to do |format|
+      format.html # new.html.erb
+      format.js
+      format.json { render json: @scroll_panel.scroll_panel_as_json(@author) }
+    end
+  end
+
+  def edit
+    @scroll_panel = ScrollPanel.edit(params[:id], @author)
+    respond_to do |format|
+      format.html 
+      format.js
+    end
+  end
+
+  def create
+    @scroll_panel = ScrollPanel.new 
+    @scroll_panel.supply_default
+    @scroll_panel.attributes = params[:scroll_panel]
+    @scroll_panel.overwrite @author
+    @scroll = Scroll.edit(@scroll_panel.scroll_id, @author) if @scroll_panel.scroll_id
+    @panel = Panel.show(@scroll_panel.panel_id, @author) if @scroll_panel.panel_id
+    
+    respond_to do |format|
+      if @scroll_panel.store
+        flash[:notice] = I18n.t('flash.notice.created', :model => ScrollPanel.model_name.human)
+        format.html { redirect_to action: :scroll, id: @scroll_panel.scroll_id }
+        format.json { render json: @scroll_panel.scroll_panel_as_json(@author) }
+      else
+        flash[:notice] = I18n.t('flash.notice.not_created', :model => ScrollPanel.model_name.human)
+        format.html { render action: "new" }
+        format.json { render json: @scroll_panel.errors, status: :unprocessable_entity }
+      end
+    end
+  end
+  
+  def update
+    @scroll_panel = ScrollPanel.edit(params[:id], @author)
+    ot = @scroll_panel.t
+    @scroll_panel.attributes = params[:scroll_panel]
+    @scroll_panel.overwrite @author
+    respond_to do |format|
+      if @scroll_panel.store ot
+        flash[:notice] = I18n.t('flash.notice.updated', :model => ScrollPanel.model_name.human)
+        format.html { redirect_to action: :scroll, id: @scroll_panel.scroll_id }
+        format.json { head :ok }
+      else
+        flash[:notice] = I18n.t('flash.notice.not_updated', :model => ScrollPanel.model_name.human)
+        format.html { render action: "edit" }
+        format.json { render json: @scroll_panel.errors, status: :unprocessable_entity }
+      end
+    end
+  end
+
+  def destroy
+    @scroll_panel = ScrollPanel.edit(params[:id], @author)
+    respond_to do |format|
+      if @scroll_panel.destroy_and_shorten
+        flash[:notice] = I18n.t('flash.notice.destroyed', :model => ScrollPanel.model_name.human)
+        format.html { redirect_to :controller => 'scroll_panels', :action => :scroll, :id => @scroll_panel.scroll_id }
+        format.json { head :ok }
+      else
+        flash[:notice] = I18n.t('flash.notice.not_destroyed', :model => ScrollPanel.model_name.human)
+        format.html { redirect_to scroll_panel_path(@scroll_panel) }
+        format.json { render json: @scroll_panel.errors, status: :unprocessable_entity }
+      end
+    end
+  end
+end