OSDN Git Service

t#32025:comic rename
[pettanr/pettanr.git] / app / controllers / scroll_panels_controller.rb
1 class ScrollPanelsController < ApplicationController
2   layout 'test' if MagicNumber['test_layout']
3   if MagicNumber['run_mode'] == 0
4     before_filter :authenticate_user, :only => [:new, :create, :edit, :update, :destroy]
5     before_filter :authenticate_author, :only => [:new, :create, :edit, :update, :destroy]
6   else
7     before_filter :authenticate_reader, :only => [:index, :show, :scroll]
8     before_filter :authenticate_user, :only => [:new, :create, :edit, :update, :destroy]
9     before_filter :authenticate_author, :only => [:new, :create, :edit, :update, :destroy]
10   end
11   before_filter :authenticate_admin!, :only => [:list, :browse]
12
13   def index
14     @page = ScrollPanel.page params[:page]
15     @page_size = ScrollPanel.page_size params[:page_size]
16     @scroll_panels = ScrollPanel.list(@page, @page_size)
17
18     respond_to do |format|
19       format.html {
20         @paginate = ScrollPanel.list_paginate(@page, @page_size)
21       }
22       format.json { render :json => @scroll_panels.to_json(ScrollPanel.list_json_opt) }
23     end
24   end
25
26   def show
27     @scroll_panel = ScrollPanel.show(params[:id], [@user, @admin])
28
29     respond_to do |format|
30       format.html # show.html.erb
31       format.json { render json: @scroll_panel.scroll_panel_as_json(@author) }
32     end
33   end
34   
35   def scroll
36     @scroll = Scroll.show(params[:id], [@user, @admin])
37     cnt = ScrollPanel.count(:conditions => ['scroll_id = ?', @scroll.id]).to_i
38     @offset = ScrollPanel.offset cnt, params[:offset]
39     @panel_count = ScrollPanel.panel_count cnt, params[:count]
40     @scroll_panels = ScrollPanel.play_list(@scroll, @author, @offset, @panel_count)
41     respond_to do |format|
42       format.html {
43         @prev_offset = if @offset > 0
44           if @offset - @panel_count < 0
45             0
46           else
47             @offset - @panel_count
48           end
49         else
50           nil
51         end
52         @next_offset = if @offset + @panel_count > cnt
53           nil
54         else
55           @offset + @panel_count
56         end
57         if @author
58           @new_panels = Panel.mylist(@author, 1, 5)
59         end
60       }
61       format.json {render text: ScrollPanel.list_as_json_text(@scroll_panels, @author)}
62       format.jsonp {
63         render :json => "callback(" + @scroll_panels.to_json_list + ");"
64       }
65     end
66   end
67   
68   def list
69     @scroll_panels = ScrollPanel.all
70
71     respond_to do |format|
72       format.html { render layout: 'system' }# index.html.erb
73       format.json { render json: @scroll_panels }
74     end
75   end
76
77   def browse
78     @scroll_panel = ScrollPanel.find(params[:id])
79
80     respond_to do |format|
81       format.html { render layout: 'system' } # show.html.erb
82       format.json { render json: @scroll_panel }
83     end
84   end
85   
86   def new
87     @scroll_panel = ScrollPanel.new 
88     @scroll_panel.supply_default
89     respond_to do |format|
90       format.html # new.html.erb
91       format.js
92       format.json { render json: @scroll_panel.scroll_panel_as_json(@author) }
93     end
94   end
95
96   def edit
97     @scroll_panel = ScrollPanel.edit(params[:id], @author)
98     respond_to do |format|
99       format.html 
100       format.js
101     end
102   end
103
104   def create
105     @scroll_panel = ScrollPanel.new 
106     @scroll_panel.supply_default
107     @scroll_panel.attributes = params[:scroll_panel]
108     @scroll_panel.overwrite @author
109     @scroll = Scroll.edit(@scroll_panel.scroll_id, @author) if @scroll_panel.scroll_id
110     @panel = Panel.show(@scroll_panel.panel_id, @author) if @scroll_panel.panel_id
111     
112     respond_to do |format|
113       if @scroll_panel.store
114         flash[:notice] = I18n.t('flash.notice.created', :model => ScrollPanel.model_name.human)
115         format.html { redirect_to action: :scroll, id: @scroll_panel.scroll_id }
116         format.json { render json: @scroll_panel.scroll_panel_as_json(@author) }
117       else
118         flash[:notice] = I18n.t('flash.notice.not_created', :model => ScrollPanel.model_name.human)
119         format.html { render action: "new" }
120         format.json { render json: @scroll_panel.errors, status: :unprocessable_entity }
121       end
122     end
123   end
124   
125   def update
126     @scroll_panel = ScrollPanel.edit(params[:id], @author)
127     ot = @scroll_panel.t
128     @scroll_panel.attributes = params[:scroll_panel]
129     @scroll_panel.overwrite @author
130     respond_to do |format|
131       if @scroll_panel.store ot
132         flash[:notice] = I18n.t('flash.notice.updated', :model => ScrollPanel.model_name.human)
133         format.html { redirect_to action: :scroll, id: @scroll_panel.scroll_id }
134         format.json { head :ok }
135       else
136         flash[:notice] = I18n.t('flash.notice.not_updated', :model => ScrollPanel.model_name.human)
137         format.html { render action: "edit" }
138         format.json { render json: @scroll_panel.errors, status: :unprocessable_entity }
139       end
140     end
141   end
142
143   def destroy
144     @scroll_panel = ScrollPanel.edit(params[:id], @author)
145     respond_to do |format|
146       if @scroll_panel.destroy_and_shorten
147         flash[:notice] = I18n.t('flash.notice.destroyed', :model => ScrollPanel.model_name.human)
148         format.html { redirect_to :controller => 'scroll_panels', :action => :scroll, :id => @scroll_panel.scroll_id }
149         format.json { head :ok }
150       else
151         flash[:notice] = I18n.t('flash.notice.not_destroyed', :model => ScrollPanel.model_name.human)
152         format.html { redirect_to scroll_panel_path(@scroll_panel) }
153         format.json { render json: @scroll_panel.errors, status: :unprocessable_entity }
154       end
155     end
156   end
157 end