OSDN Git Service

fix ctl list
[pettanr/pettanr.git] / app / controllers / application_controller.rb
1 class ApplicationController < ActionController::Base
2   protect_from_forgery
3   layout :devise_layout if MagicNumber['test_layout']
4   before_filter :bf
5   
6   def devise_layout
7     if devise_controller?
8       case resource_name
9       when :admin
10         'guest'
11       when :user
12         'guest'
13       when :demand_user
14         'guest'
15       end
16     else
17       'application'
18     end
19   end
20   
21   def bf
22     @server_result = {
23       :location => {:controller => params[:controller], :action => params[:action]}
24     }
25     @server_result[:location][:id] = params[:id] if params[:id]
26     if Admin.count.to_i == 0 or License.count.to_i == 0
27       if params[:controller] == 'system' and params[:action] == 'start'
28       else
29         redirect_to :controller => '/system', :action => 'start'
30       end
31     else
32       user = if user_signed_in?
33         current_user
34       else
35         nil
36       end
37       author = if user
38         user.author
39       else
40         nil
41       end
42       artist = if user
43         user.artist
44       else
45         nil
46       end
47       admin = if admin_signed_in?
48         current_admin
49       else
50         nil
51       end
52       demand_user = if demand_user_signed_in?
53         current_demand_user
54       else
55         nil
56       end
57       @operators = Operator.new [user, author, artist, admin, demand_user]
58     end
59   end
60   
61   def authenticate_reader
62     authenticate_user! unless @operators.reader?
63   end
64   
65   def authenticate_user
66     authenticate_user! unless @operators.user?
67   end
68   
69   def authenticate_resource_reader
70     authenticate_user! unless @operators.resource_reader?
71   end
72   
73   def authenticate_author
74     if @operators.author
75       true
76     else
77       respond_to do |format|
78         format.html { redirect_to main_app.new_author_path, :status => :found }
79         format.js { render "authors/new" }
80         format.json { 
81           raise ActiveRecord::Forbidden
82         }
83       end
84       false
85     end
86   end
87       
88   def authenticate_artist
89     if @operators.artist
90       true
91     else
92       respond_to do |format|
93         format.html { redirect_to main_app.new_artist_path, :status => :found }
94         format.js { render "artists/new" }
95         format.json { 
96           raise ActiveRecord::Forbidden
97         }
98       end
99       false
100     end
101   end
102   
103   def self.controller
104     Pettanr::Application::manifest.controller_managers[self.model.item_name]
105   end
106   
107   def self.profiler_manager
108     Pettanr::Application::manifest.profiler_managers[self.model.item_name]
109   end
110   
111   def public_list 
112     action_name = params[:action]
113     @action = self.class.controller.open(action_name, params, @operators)
114     @items = @action.items 
115     respond_to do |format|
116       format.html {
117         @filer = @action.filer
118         render :template => 'system/filer', :locals => {
119           :filer => @filer
120         }
121       }
122       format.json { render json: @items.to_json(self.class.model.list_json_opt) }
123       format.atom 
124       format.rss
125     end
126   end
127   
128   def my_list params
129     controller_name = params[:controller]
130     action_name = params[:action]
131     controller = Pettanr::Application::manifest.controller_managers[controller_name]
132     @action = controller.open(action_name, params, @operators)
133     @items = @action.items 
134
135     respond_to do |format|
136       format.html {
137         @filer = @action.filer
138         render :template => 'system/filer', :locals => {
139           :filer => @filer
140         }
141       }
142       format.json { render json: @items.to_json(self.class.model.list_json_opt) }
143     end
144   end
145   
146   def has_many_list 
147     @parent_item = self.class.model.show(params[:id], @operators)
148     controller_name = self.class.model.item_name
149     action_name = params[:action]
150     controller = Pettanr::Application::manifest.controller_managers[controller_name]
151     @action = controller.open(action_name, params, @operators)
152     many_name = @action.list.association_model_name
153     @items = @action.list.items @parent_item
154     respond_to do |format|
155       format.html {
156         @filer =  Pettanr::Application::manifest.filer_managers[many_name].open(many_name, @items, @operators, @action.list.paginate(@parent_item))
157         render :template => 'system/filer', :locals => {
158           :filer => @filer
159         }
160       }
161       format.json { render json: @items.to_json(self.class.model.list_json_opt) }
162     end
163   end
164   
165   def filter_list
166     controller_name = self.class.model.item_name
167     action_name = params[:action]
168     controller = Pettanr::Application::manifest.controller_managers[controller_name]
169     @action = controller.open(action_name, params, @operators)
170     @filter_item = @action.list.from_model.show(params[:id], @operators)
171     item_model_name = @action.list.item_name
172     @items = @action.list.items @filter_item
173     respond_to do |format|
174       format.html {
175         @filer =  Pettanr::Application::manifest.filer_managers[item_model_name].open(item_model_name, @items, @operators, @action.list.paginate(@filter_item))
176         render :template => 'system/filer', :locals => {
177           :filer => @filer
178         }
179       }
180       format.json { render json: @items.to_json(self.class.model.list_json_opt) }
181     end
182   end
183   
184   def format_filer format
185     format.html {
186       @paginate = @@model.list_paginate(@page, @page_size)
187       render :template => 'system/filer', :locals => {
188         :items => @items, :model => @@model, 
189         :operators => @operators, :pager => @paginate
190       }
191     }
192   end
193   
194   def format_prof format
195     format.prof { 
196       @profiler = self.class.profiler_manager.open(@item, @operators)
197       render :template => 'system/prof', :locals => {
198         :profiler => @profiler
199       }
200     }
201   end
202   
203   def assist_items item_name, list_name
204     list_manager = Pettanr::Application::manifest.list_managers[item_name]
205     list = list_manager.open list_name, 1, 5, @operators
206     list.items
207   end
208   
209   def assist_filer item_name, items
210     filer_manager = Pettanr::Application::manifest.filer_managers[item_name]
211     filer_manager.open(item_name, items, @operators, nil)
212   end
213   
214   def set_image(file)
215     if file.respond_to?(:read)
216       file.read
217     else
218       Base64.decode64(file.to_s.gsub(' ', '+'))
219     end
220   end
221   
222   def ymd_to_time ymd_str
223     return nil if ymd_str.blank?
224     date = nil
225     begin
226       date = Time.parse(ymd_str[0..3] + '/' + ymd_str[4..5] + '/' + ymd_str[6..7])
227     rescue
228       date = nil
229     end
230     date
231   end
232   
233   def export_url demander_url, action, token, date
234     u = demander_url + (demander_url[-1] == '/' ? '' : '/')
235     prm = '?auth_token=' + token
236     prm = prm + '&date=' + date.strftime("%Y%m%d") unless date.blank?
237     u = URI.join(u, action + '.json' + prm)
238     u.to_s
239   end
240   
241   def export_from_provider url
242     res = nil
243     begin
244       json = RestClient.get url
245       res = JSON.parse json
246     rescue
247     end
248     res
249   end
250   
251   def export_by action, provider_status, ymd
252     t = ymd_to_time ymd
253     url = export_url provider_status.provider.demander_url, action, provider_status.token, t
254     export_from_provider(url)
255   end
256   
257     rescue_from Pettanr::NotWork, :with => :render_not_work
258     def render_not_work(exception = nil)
259       if exception
260         logger.info "Rendering , :: #{exception.message}"
261       end
262       respond_to do |format|
263         format.html { 
264           render :file => "#{Rails.root}/public/not_work.html", :layout => false
265         }
266         format.json { 
267           render :text => "400 Not work", :status => 400
268         }
269       end
270     end
271     
272   if Rails.env == 'production'
273     rescue_from ActiveRecord::RecordNotFound, :with => :render_404
274     rescue_from ActiveRecord::Forbidden, :with => :render_403
275     
276     private
277     def render_404(exception = nil)
278       if exception
279         logger.info "Rendering 404: #{exception.message}"
280       end
281       respond_to do |format|
282         format.html { 
283           render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
284         }
285         format.json { 
286           render :text => "404 Not found", :status => 404
287         }
288       end
289     end
290     
291     def render_403(exception = nil)
292       if exception
293         logger.info "Rendering 403: #{exception.message}"
294       end
295       respond_to do |format|
296         format.html { 
297           render :file => "#{Rails.root}/public/403.html", :status => 404, :layout => false
298         }
299         format.json { 
300           render :text => "403 Forbidden", :status => 403
301         }
302       end
303     end
304   end
305 end