OSDN Git Service

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