OSDN Git Service

77637240fec2233dcd6ab4ca36cf011217700612
[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     @item = GroundPicture.show(params[:id], [@user, @admin])
31     respond_to do |format|
32       format.html {
33         @ground_picture = @item
34       }
35       format_prof format
36       format.json { render json: @item.to_json(GroundPicture.show_json_opt) }
37     end
38   end
39   
40   def new
41     raise Pettanr::NotWork unless @author.working_panel
42     @picture = Picture.show params[:picture_id], @author
43     raise ActiveRecord::Forbidden unless @picture.enable?
44     @panel = Panel.edit(@author.working_panel, @author)
45     
46     @ground_picture = GroundPicture.new :panel_id => @panel.id, :picture_id => @picture.id
47     @ground_picture.supply_default
48
49     respond_to do |format|
50       format.html
51       format.json { render :json => @ground_picture.to_json(GroundPicture.show_json_opt) }
52     end
53   end
54
55   def edit
56     @ground_picture = GroundPicture.show(params[:id], @author)
57     @panel = Panel.edit(@ground_picture.panel.id, @author)
58     
59     respond_to do |format|
60       format.html
61     end
62   end
63
64   def create
65     raise Pettanr::NotWork unless @author.working_panel
66     @panel = Panel.edit(@author.working_panel, @author)
67     
68     @ground_picture = GroundPicture.new
69     @ground_picture.attributes = params[:ground_picture]
70     @ground_picture.overwrite @panel.id
71     
72     @picture = Picture.show @ground_picture.picture_id, @author
73     raise ActiveRecord::Forbidden unless @picture.enable?
74     
75     respond_to do |format|
76       if @ground_picture.valid?
77         if @ground_picture.store @author
78           flash[:notice] = I18n.t('flash.notice.created', :model => Panel.model_name.human)
79           format.html { redirect_to @panel }
80           format.json { render json: @panel.panel_elements_as_json, status: :created, location: @panel }
81         else
82           flash[:notice] = I18n.t('flash.notice.not_created', :model => GroundPicture.model_name.human)
83           format.html { render action: "new" }
84           format.json { render json: @panel.errors, status: :unprocessable_entity }
85         end
86       else
87         flash[:notice] = I18n.t('flash.notice.not_created', :model => GroundPicture.model_name.human)
88         format.html { render action: "new" }
89         format.json { render json: @ground_picture.errors, status: :unprocessable_entity }
90       end
91     end
92   end
93
94   def update
95     @ground_picture = GroundPicture.show(params[:id], @author)
96     @ground_picture.attributes = params[:ground_picture]
97     @panel = Panel.edit(@ground_picture.panel.id, @author)
98     @ground_picture.overwrite @panel.id
99     
100     @picture = Picture.show @ground_picture.picture_id, @author
101     raise ActiveRecord::Forbidden unless @picture.enable?
102     
103     respond_to do |format|
104       if @ground_picture.store @author
105         flash[:notice] = I18n.t('flash.notice.updated', :model => GroundPicture.model_name.human)
106         format.html { redirect_to @ground_picture }
107         format.json { head :ok }
108       else
109         flash[:notice] = I18n.t('flash.notice.not_updated', :model => GroundPicture.model_name.human)
110         format.html { render action: "edit" }
111         format.json { render json: @ground_picture.errors, status: :unprocessable_entity }
112       end
113     end
114   end
115
116   def destroy
117     @ground_picture = GroundPicture.show(params[:id], @author)
118     @panel = Panel.edit(@ground_picture.panel.id, @author)
119     
120     respond_to do |format|
121       if @ground_picture.remove @author
122         flash[:notice] = I18n.t('flash.notice.destroyed', :model => GroundPicture.model_name.human)
123         format.html { redirect_to @panel }
124         format.json { head :ok }
125       else
126         flash[:notice] = I18n.t('flash.notice.not_destroyed', :model => GroundPicture.model_name.human)
127         format.html { redirect_to @ground_picture }
128         format.json { render json: @ground_picture.errors, status: :unprocessable_entity }
129       end
130     end
131   end
132   
133 end