OSDN Git Service

04b164fa4745b1e44fff0da613a3518aa952267a
[pettanr/pettanr.git] / app / controllers / ground_pictures_controller.rb
1 class GroundPicturesController < ApplicationController
2   layout 'test' if MagicNumber['test_layout']
3   if MagicNumber['run_mode'] == 0
4     before_filter :authenticate_user, :only => [:new, :edit, :create, :update, :destroy]
5     before_filter :authenticate_author, :only => [:new, :edit, :create, :update, :destroy]
6   else
7     before_filter :authenticate_reader, :only => [:index, :show]
8     before_filter :authenticate_user, :only => [:new, :edit, :create, :update, :destroy]
9     before_filter :authenticate_author, :only => [:new, :edit, :create, :update, :destroy]
10   end
11
12   def index
13     @page = GroundPicture.page params[:page]
14     @page_size = GroundPicture.page_size params[:page_size]
15     @ground_pictures = GroundPicture.list(@page, @page_size)
16
17     respond_to do |format|
18       format.html {
19         @paginate = GroundPicture.list_paginate(@page, @page_size)
20         render :template => 'system/filer', :locals => {
21           :items => @ground_pictures, :model => GroundPicture, 
22           :roles => [@user, @admin], :pager => @paginate
23         }
24       }
25       format.json { render :json => @ground_pictures.to_json(GroundPicture.list_json_opt) }
26     end
27   end
28
29   def show
30     @ground_picture = GroundPicture.show(params[:id], [@user, @admin])
31     respond_to do |format|
32       format.html # show.html.erb
33       format.prof  { render :template => 'top/prof', :layout => true }
34       format.json { render json: @ground_picture.to_json(GroundPicture.show_json_opt) }
35     end
36   end
37   
38   def new
39     raise Pettanr::NotWork unless @author.working_panel
40     @picture = Picture.show params[:picture_id], @author
41     raise ActiveRecord::Forbidden unless @picture.enable?
42     @panel = Panel.edit(@author.working_panel, @author)
43     
44     @ground_picture = GroundPicture.new :panel_id => @panel.id, :picture_id => @picture.id
45     @ground_picture.supply_default
46
47     respond_to do |format|
48       format.html
49       format.json { render :json => @ground_picture.to_json(GroundPicture.show_json_opt) }
50     end
51   end
52
53   def edit
54     @ground_picture = GroundPicture.show(params[:id], @author)
55     @panel = Panel.edit(@ground_picture.panel.id, @author)
56     
57     respond_to do |format|
58       format.html
59     end
60   end
61
62   def create
63     raise Pettanr::NotWork unless @author.working_panel
64     @panel = Panel.edit(@author.working_panel, @author)
65     
66     @ground_picture = GroundPicture.new
67     @ground_picture.attributes = params[:ground_picture]
68     @ground_picture.overwrite @panel.id
69     
70     @picture = Picture.show @ground_picture.picture_id, @author
71     raise ActiveRecord::Forbidden unless @picture.enable?
72     
73     respond_to do |format|
74       if @ground_picture.valid?
75         if @ground_picture.store @author
76           flash[:notice] = I18n.t('flash.notice.created', :model => Panel.model_name.human)
77           format.html { redirect_to @panel }
78           format.json { render json: @panel.panel_elements_as_json, status: :created, location: @panel }
79         else
80           flash[:notice] = I18n.t('flash.notice.not_created', :model => GroundPicture.model_name.human)
81           format.html { render action: "new" }
82           format.json { render json: @panel.errors, status: :unprocessable_entity }
83         end
84       else
85         flash[:notice] = I18n.t('flash.notice.not_created', :model => GroundPicture.model_name.human)
86         format.html { render action: "new" }
87         format.json { render json: @ground_picture.errors, status: :unprocessable_entity }
88       end
89     end
90   end
91
92   def update
93     @ground_picture = GroundPicture.show(params[:id], @author)
94     @ground_picture.attributes = params[:ground_picture]
95     @panel = Panel.edit(@ground_picture.panel.id, @author)
96     @ground_picture.overwrite @panel.id
97     
98     @picture = Picture.show @ground_picture.picture_id, @author
99     raise ActiveRecord::Forbidden unless @picture.enable?
100     
101     respond_to do |format|
102       if @ground_picture.store @author
103         flash[:notice] = I18n.t('flash.notice.updated', :model => GroundPicture.model_name.human)
104         format.html { redirect_to @ground_picture }
105         format.json { head :ok }
106       else
107         flash[:notice] = I18n.t('flash.notice.not_updated', :model => GroundPicture.model_name.human)
108         format.html { render action: "edit" }
109         format.json { render json: @ground_picture.errors, status: :unprocessable_entity }
110       end
111     end
112   end
113
114   def destroy
115     @ground_picture = GroundPicture.show(params[:id], @author)
116     @panel = Panel.edit(@ground_picture.panel.id, @author)
117     
118     respond_to do |format|
119       if @ground_picture.remove @author
120         flash[:notice] = I18n.t('flash.notice.destroyed', :model => GroundPicture.model_name.human)
121         format.html { redirect_to @panel }
122         format.json { head :ok }
123       else
124         flash[:notice] = I18n.t('flash.notice.not_destroyed', :model => GroundPicture.model_name.human)
125         format.html { redirect_to @ground_picture }
126         format.json { render json: @ground_picture.errors, status: :unprocessable_entity }
127       end
128     end
129   end
130   
131 end