OSDN Git Service

- スマートフォン対応レポジトリからマージ
[elecoma/elecoma.git] / app / controllers / accounts_controller.rb
1 # -*- coding: utf-8 -*-
2 class AccountsController < BaseController
3   include AccountsControllerExtend
4   ssl_required :login, :signup, :signup_confirm, :signup_complete, :history, :history_list, :history_show, :edit, :edit_confirm, :edit_complete, :delivery, :delivery_list, :delivery_new, :delivery_new_popup, :delivery_create, :delivery_edit, :delivery_edit_popup, :delivery_update, :delivery_complete, :delivery_destroy, :activate, :withdraw, :withdraw_confirm, :withdraw_complete, :reminder, :salvage, :salvage_complete
5   before_filter :login_check, :except => [
6     :activate, :kiyaku, :kiyaku_intro, :login, :logout, :reminder,
7     :reminder_complete, :reminder_hint, :salvage, :salvage_complete,
8     :signup, :signup_complete, :signup_confirm,
9     :regmailst, :get_address
10   ]
11   before_filter :load_seo_mypage_index, :only => [:history, :history_list, :edit, :delivery, :withdraw]
12   before_filter :redirect_for_login_user, :only => [
13     :login, :signup, :signup_confirm, :signup_complete, :kiyaku, :kiyaku_intro, :salvage, :salvage_complete
14   ]
15   before_filter :get_user_navigation_list
16
17   EMAIL_PATTERN = /^(([^@\s]+)@((?:[-a-z0-9]+\.)*[a-z]{2,})|)$/i
18
19   def login
20     if request.request_method == :post
21       if params[:customer]
22         if params[:customer][:email] == ''
23           flash.now[:notice] = "メールアドレスを入力して下さい。"
24           return
25         end
26         if params[:customer][:password] == ''
27           flash.now[:notice] = "パスワードを入力して下さい。"
28           return
29         end
30       else
31         flash.now[:notice] = "メールアドレスを入力してください"
32         return
33       end
34       customer = Customer.find_by_email_and_password(params[:customer][:email], params[:customer][:password])
35       if customer.nil?
36         flash.now[:notice] = "メールアドレスもしくはパスワードが正しくありません。"
37         return
38       elsif customer.activate == Customer::KARITOUROKU
39         flash.now[:notice] = '会員登録が完了していません。登録確認メールに書かれた URL にアクセスして会員登録を完了してください。'
40       # ログインを携帯とPCに分けないようにする
41       #elsif !customer.same_mobile_carrier?(request.mobile)
42       #  flash.now[:notice] = '登録時の端末でログインしてください。'
43       else
44         Cart.transaction do
45           set_login_customer(customer)
46           # ログイン前に買い物していれば、その内容を取り込む
47           unless @carts.empty?
48             Cart.delete_all(['customer_id=?', customer.id])
49             @carts.each_with_index do |cart, i|
50               cart.customer = customer
51               cart.position = i
52               cart.save
53             end
54           end
55           unless params[:reminder_id].blank?
56             cookies[:reminder_id] = {
57               :value => customer.email,
58               :expires => 14.days.from_now,
59               :path => '/'
60             }
61             customer.save
62           end
63           unless params[:auto_login].blank?
64             cookies[:auto_login] = {
65               :value => customer.generate_cookie!(request.remote_ip),
66               :expires => 14.days.from_now,
67               :path => '/'
68             }
69             customer.save
70           end
71         end
72         # 直前にいたページ or トップページへリダイレクト
73         if session[:return_to]
74           # login_check で飛ばされた場合
75           redirect_to :controller=>session[:return_to]["controller"],
76                       :action=>session[:return_to]["action"],
77                       :dir_name=>session[:return_to]["dir_name"],
78                       :id=>session[:return_to][:id]
79           session[:return_to] = nil
80         else
81           # 普通に来た場合
82           if request.mobile?
83             if request.mobile.respond_to?('smartphone?')
84               redirect_to :controller => "accounts", :action=>"myindex"# '/account/myindex_mobile'
85             else
86               redirect_to :controller => "accounts", :action=>"myindex_mobile"
87             end
88           else
89             redirect_to :controller => "portal", :action => "show"
90           end
91         end
92       end
93     else
94       if (cookie = cookies[:reminder_id])
95         @customer = Customer.new(:email => cookie)
96         @reminder_id = 1
97       end
98       if (cookie = cookies[:auto_login])
99         @customer = Customer.find_by_cookie(cookie)
100         @auto_login = 1
101       end
102     end
103   end
104
105   def logout
106     @login_customer.update_attributes(:cookie => nil) if @login_customer and @login_customer.cookie
107     session[:carts] = nil
108     reset_session
109     request.env["HTTP_REFERER"] ||= url_for(:controller=>:portal, :action=>:show)
110     redirect_to :action => 'login'
111   end
112
113   # モバイル専用規約入口
114   def kiyaku_intro
115     redirect_to :action => :kiyaku unless request.mobile? && !request.mobile.respond_to?('smartphone?')
116   end
117
118   def kiyaku
119     params[:position] ||= 1 if request.mobile? && !request.mobile.respond_to?('smartphone?')
120     # 章を指定されたらそこだけ出す。さもなくば全て。
121     if params[:position]
122       kiyaku = Kiyaku.find_by_position(params[:position])
123       raise ActiveRecord::RecordNotFound if kiyaku.nil?
124       @next = Kiyaku.minimum(:position, :conditions => ['position > ?', kiyaku.position])
125       @prev = Kiyaku.maximum(:position, :conditions => ['position < ?', kiyaku.position])
126       @kiyakus = [kiyaku]
127     else
128       @kiyakus = Kiyaku.find(:all, :order=>'position')
129       @kiyaku_content = ''
130       @kiyakus.each do |k|
131         @kiyaku_content = @kiyaku_content + k.name + "\n\n" + k.content + "\n\n"
132       end
133     end
134   end
135
136   def signup
137     @stage = (params[:stage] || 0).to_i
138     params[:done] && !params[:back] and return signup_confirm
139     @customer = Customer.new
140     if request.method == :post # 2 ページ目とか
141       get_customer
142     end
143   end
144
145   def signup_confirm
146     @customer = Customer.new
147     get_customer
148     @customer.editting = false if request.mobile? && !request.mobile.respond_to?('smartphone?')
149     unless @customer.valid?
150       render :action => :signup
151       return
152     end
153     @password = @customer.raw_password
154     render :action => :signup_confirm
155   end
156
157   def signup_complete
158     if params[:back]
159       signup
160       return render(:action => 'signup')
161     end
162
163     @customer = Customer.new(params[:customer])
164     unless params[:password].blank?
165       @customer.set_password params[:password]
166     end
167
168     # モバイル
169     @customer.set_mobile request.mobile
170
171     # 登録直後は仮登録状態
172     # 登録キーを生成してメールを送る
173     @customer.activate = Customer::KARITOUROKU
174     @customer.generate_activation_key!
175
176     # カートを保存
177     @customer.carts.clear
178     @customer.carts << @carts
179
180     #ポイント付与
181     unless Shop.find(:first).blank?
182       @customer.point = Shop.find(:first).point_at_admission
183     end
184
185     unless @customer.valid?
186       render :action => 'signup'
187       return
188     end
189
190     begin
191       url = url_for(:action => :activate, :activation_key => @customer.activation_key, :aff_id=>session[:aff_id])
192       logger.debug "######## #{url} ########"
193       Notifier::deliver_activate(@customer, url)
194     rescue => e
195       flash.now[:error] = 'メールの送信に失敗しました'
196       render :action => 'signup'
197       return
198     end
199
200     # 全てが終わったら保存
201     @customer.save
202   end
203
204   # 購入履歴
205   def history
206     history_list
207     render :action => 'history_list'
208   end
209
210   def history_list
211     # paginate する
212     @orders = Order.paginate(:page => params[:page],
213                              :per_page => request.mobile ? 5 : 20,
214                              :conditions => ['customer_id=?', @login_customer.id],
215                              :order=>'received_at desc',
216                              :limit => 20)
217     @orders.total_entries = 20 if request.mobile? && @orders.total_entries > 20
218   end
219
220   def history_show
221     @order = @login_customer.orders.find_by_id(params[:id].to_i)
222     raise ActiveRecord::RecordNotFound unless @order
223     @order_delivery = @order.order_deliveries[0]
224   end
225
226   # 会員登録内容変更
227   def edit
228     @stage = (params[:stage] || 0).to_i
229     params[:done] && !params[:back] and return edit_confirm
230     @customer = Customer.find(@login_customer.id)
231     if request.method == :get # 1 ページ目
232       @customer.email_confirm = @customer.email
233     end
234     if request.method == :post # 2 ページ目とか
235       get_customer
236     end
237   end
238
239   def edit_confirm
240     @customer = Customer.find(@login_customer.id)
241     get_customer
242     @customer.editting = false if request.mobile?
243     unless @customer.valid?
244       render :action => :edit
245       return
246     end
247     # 入力が正しい時だけ次へ引き継ぐ
248     @password = @customer.raw_password
249     render :action => :edit_confirm
250   end
251
252   def edit_complete
253     if params[:back]
254       edit
255       return render(:action => 'edit')
256     end
257     @customer = Customer.find(@login_customer.id)
258     @customer.attributes = params[:customer]
259     unless @customer.valid?
260       render :action => 'edit'
261       return
262     end
263     @customer.set_mobile request.mobile
264     @customer.save
265     set_login_customer(@customer)
266   end
267
268   def delivery
269     @delivery_addresses = @login_customer.delivery_addresses
270     render :action => 'delivery_list'
271   end
272
273   def delivery_list
274     redirect_to :action => :delivery
275   end
276
277   def delivery_new
278     @stage = (params[:stage] || 0).to_i
279     params[:done] && !params[:back] and return delivery_create
280     @delivery_address = @login_customer.delivery_addresses.build
281     if request.method == :post
282       get_delivery_address
283     end
284     render :action => 'delivery_new' unless performed?
285   end
286
287   def delivery_new_popup
288     @popup = true
289     delivery_new
290     render :action => 'delivery_new_popup' unless performed?
291   end
292
293   def delivery_create
294     @stage = params[:stage]
295     @popup = !params[:popup].blank? && params[:popup] == "true"
296     customer = @login_customer
297     @delivery_address = customer.delivery_addresses.build(params[:delivery_address])
298     unless @delivery_address.valid?
299       return render(:action => 'delivery_new')
300     end
301     render :action => 'delivery_confirm'
302   end
303
304   def delivery_edit
305     @stage = (params[:stage] || 0).to_i
306     params[:done] && !params[:back] and return delivery_update
307     @id = params[:id].to_i
308     @delivery_address = find_delivery_address @login_customer, params[:id].to_i
309     if request.method == :post
310       get_delivery_address
311     end
312     render :action => 'delivery_edit' unless performed?
313   end
314
315   def delivery_edit_popup
316     @popup = true
317     delivery_edit
318     render :action => 'delivery_edit_popup' unless performed?
319   end
320
321   def delivery_update
322     @popup = !params[:popup].blank? && params[:popup] == "true"
323     @id = params[:id].to_i
324     @delivery_address = find_delivery_address @login_customer, params[:id].to_i
325     @delivery_address.attributes = params[:delivery_address]
326     unless @delivery_address.valid?
327       return render(:action => 'delivery_edit')
328     end
329     if @popup
330       render :action => 'delivery_confirm_popup'
331     else
332       render :action => 'delivery_confirm'
333     end
334   end
335
336   def delivery_complete
337     @popup = !params[:popup].blank? && params[:popup] == "true"
338     if params[:id].blank?
339       # 新規
340       if params[:back]
341         return (@popup ? delivery_new_popup : delivery_new)
342       end
343       @delivery_address = @login_customer.delivery_addresses.build()
344       # 最大の position + 1 を割り当てる
345       max_position = @login_customer.delivery_addresses.map(&:position).map(&:to_i).max
346       @delivery_address.position = max_position + 1
347     else
348       # 更新
349       if params[:back]
350         return (@popup ? delivery_edit_popup : delivery_edit)
351       end
352       @delivery_address = find_delivery_address @login_customer, params[:id].to_i
353     end
354     @delivery_address.attributes = params[:delivery_address] if @delivery_address
355     if @delivery_address && @delivery_address.save
356       flash.now[:notice] = 'データを保存しました。'
357     else
358       flash.now[:error] = 'データの保存に失敗しました。'
359     end
360     if @popup
361       render :action => 'close_popup'
362     else
363       if params[:backurl] # カートから来た時
364         redirect_to :controller => 'cart', :action => 'shipping'
365       else
366         redirect_to :action => 'delivery'
367       end
368     end
369   end
370
371   def delivery_destroy
372     @delivery_address = find_delivery_address @login_customer, params[:id].to_i
373     if @delivery_address and @delivery_address.destroy
374       flash.now[:notice] = '削除しました。'
375     else
376       flash.now[:notice] = '削除に失敗しました。'
377     end
378     if params[:backurl] # カートから来た時
379       redirect_to params[:backurl]
380     else
381       redirect_to :action => 'delivery'
382     end
383   end
384
385   def activate
386     key = params[:activation_key]
387     unless key.blank?
388       @customer = Customer.activate_email(key)
389       if @customer
390         @customer.reachable = true
391         save_carts(@customer.carts)
392         @customer.carts.clear
393       end
394     end
395   end
396
397   def withdraw
398   end
399
400   def withdraw_confirm
401   end
402
403   def withdraw_complete
404     unless request.post?
405       head :method_not_allowed
406       return
407     end
408     @login_customer.withdraw
409     set_login_customer(nil)
410   end
411
412   def reminder
413     redirect_to :action => :salvage
414   end
415
416   def salvage
417     flash.now[:notice] = flash.now[:error] = nil
418   end
419
420   def salvage_complete
421     @input = Customer.new(params[:input])
422     @customer = Customer.find_by_email_and_activate(@input.email, Customer::TOUROKU)
423     @input.id = @customer.id if @customer # メールアドレス重複エラー回避
424     columns = [:email, :family_name, :first_name, :birthday, :tel01, :tel02, :tel03]
425     @input.attributes = {:password_confirm=>"dummy", :email_confirm=>"dummy@example.com"}
426     @input.target_columns = columns
427     unless @input.valid?
428       render :action => "salvage"
429       return
430     end
431     correct = @customer && columns.all?{ |c| @customer[c] == @input[c] }
432     unless correct
433       flash.now[:notice] = "入力された情報が正しくありません"
434       render :action => "salvage"
435       return
436     end
437     @password = @customer.regenerate_password!
438     unless @customer.save
439       flash.now[:notice] = "パスワードの再発行に失敗しました"
440       render :action => "salvage"
441       return
442     end
443     #メールの送信
444     begin
445       if request.mobile?
446         Notifier::deliver_mobile_reminder(@customer, @password)
447       else
448         Notifier::deliver_reminder(@customer, @password)
449       end
450     rescue
451       flash.now[:notice] = "メールの送信に失敗しました"
452       render :action => "salvage"
453       return
454     end
455   end
456
457   def regmailst
458     @target_domain = Shop.first.docomo_sender_address
459     @return_to = params[:return_to]
460     @return_to ||= request.env["HTTP_REFERER"]
461     @return_to ||= url_for(:action=>:kiyaku_intro, :only_path => false)
462   end
463
464   private
465
466   def get_shop_info
467     @shop = Shop.find(:first)
468   end
469
470   def find_customer_by_email(email)
471     customer = Customer.find_by_email(email)
472     return customer
473   end
474
475   def find_delivery_address customer, id
476     id or raise ActiveRecord::RecordNotFound
477     conditions = ["#{DeliveryAddress.table_name}.id = ? and #{Customer.table_name}.id = ?",
478       id, customer.id]
479     da = DeliveryAddress.find(:first, :conditions => conditions, :include => :customer)
480     da or raise ActiveRecord::RecordNotFound
481   end
482
483   def load_seo_mypage_index
484     @seo = Seo.find(:first, :conditions=>{ :page_type => Seo::MYPAGE_INDEX})
485   end
486
487   def get_delivery_address
488     @popup ||= !params[:popup].blank? && params[:popup] == "true"
489
490     @delivery_address.attributes = params[:delivery_address]||{}
491     if params[:back]
492       @delivery_address.target_columns = []
493       @stage -= 1
494       if @stage < 0
495         if @popup
496           redirect_to :controller => :cart, :action => :shipping
497         else
498           redirect_to :action => :delivery
499         end
500       end
501       return
502     end
503     if params[:delivery_address]
504       @delivery_address.target_columns = params[:delivery_address].keys.map(&:to_s)
505     end
506     #@delivery_address.update_address!(@delivery_address.zipcode_first_changed?)
507     @delivery_address.update_address!
508     @stage += 1 if @delivery_address.valid?
509   end
510
511   def get_customer
512     @customer.attributes = params[:c] if params[:c]
513     if params[:customer]
514       @customer.attributes = params[:customer]
515       @customer.target_columns = params[:customer].keys.map(&:to_s)
516     end
517     @customer.set_mobile request.mobile # 携帯電話の種類
518     @customer.editting = true
519     if params[:back]
520       @customer.target_columns = []
521       @stage -= 1
522       return
523     end
524     @customer.update_address!(false)
525     @stage += 1 if @customer.valid? && @stage
526   end
527
528   def redirect_for_login_user
529     redirect_to :controller => :portal, :action => :show if @login_customer
530   end
531
532   def get_user_navigation_list
533     plugins = PaymentPlugin.find(:all, :conditions => ["enable = ? ", true], :order => :id)
534     @user_navigation_list = Array.new
535     plugins.each do |plugin|
536       obj = plugin.get_plugin_instance
537       @user_navigation_list << obj.user_navigation_list if obj
538     end
539     logger.debug @user_navigation_list
540     @user_navigation_list.flatten!
541   end
542
543 end