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
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
15 before_filter :get_user_navigation_list
17 EMAIL_PATTERN = /^(([^@\s]+)@((?:[-a-z0-9]+\.)*[a-z]{2,})|)$/i
20 if request.request_method == :post
22 if params[:customer][:email] == ''
23 flash.now[:notice] = "メールアドレスを入力して下さい。"
26 if params[:customer][:password] == ''
27 flash.now[:notice] = "パスワードを入力して下さい。"
31 flash.now[:notice] = "メールアドレスを入力してください"
34 customer = Customer.find_by_email_and_password(params[:customer][:email], params[:customer][:password])
36 flash.now[:notice] = "メールアドレスもしくはパスワードが正しくありません。"
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] = '登録時の端末でログインしてください。'
45 set_login_customer(customer)
46 # ログイン前に買い物していれば、その内容を取り込む
48 Cart.delete_all(['customer_id=?', customer.id])
49 @carts.each_with_index do |cart, i|
50 cart.customer = customer
55 unless params[:reminder_id].blank?
56 cookies[:reminder_id] = {
57 :value => customer.email,
58 :expires => 14.days.from_now,
63 unless params[:auto_login].blank?
64 cookies[:auto_login] = {
65 :value => customer.generate_cookie!(request.remote_ip),
66 :expires => 14.days.from_now,
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
83 if request.mobile.respond_to?('smartphone?')
84 redirect_to :controller => "accounts", :action=>"myindex"# '/account/myindex_mobile'
86 redirect_to :controller => "accounts", :action=>"myindex_mobile"
89 redirect_to :controller => "portal", :action => "show"
94 if (cookie = cookies[:reminder_id])
95 @customer = Customer.new(:email => cookie)
98 if (cookie = cookies[:auto_login])
99 @customer = Customer.find_by_cookie(cookie)
106 @login_customer.update_attributes(:cookie => nil) if @login_customer and @login_customer.cookie
107 session[:carts] = nil
109 request.env["HTTP_REFERER"] ||= url_for(:controller=>:portal, :action=>:show)
110 redirect_to :action => 'login'
115 redirect_to :action => :kiyaku unless request.mobile? && !request.mobile.respond_to?('smartphone?')
119 params[:position] ||= 1 if request.mobile? && !request.mobile.respond_to?('smartphone?')
120 # 章を指定されたらそこだけ出す。さもなくば全て。
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])
128 @kiyakus = Kiyaku.find(:all, :order=>'position')
131 @kiyaku_content = @kiyaku_content + k.name + "\n\n" + k.content + "\n\n"
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 ページ目とか
146 @customer = Customer.new
148 @customer.editting = false if request.mobile? && !request.mobile.respond_to?('smartphone?')
149 unless @customer.valid?
150 render :action => :signup
153 @password = @customer.raw_password
154 render :action => :signup_confirm
160 return render(:action => 'signup')
163 @customer = Customer.new(params[:customer])
164 unless params[:password].blank?
165 @customer.set_password params[:password]
169 @customer.set_mobile request.mobile
173 @customer.activate = Customer::KARITOUROKU
174 @customer.generate_activation_key!
177 @customer.carts.clear
178 @customer.carts << @carts
181 unless Shop.find(:first).blank?
182 @customer.point = Shop.find(:first).point_at_admission
185 unless @customer.valid?
186 render :action => 'signup'
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)
195 flash.now[:error] = 'メールの送信に失敗しました'
196 render :action => 'signup'
207 render :action => 'history_list'
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',
217 @orders.total_entries = 20 if request.mobile? && @orders.total_entries > 20
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]
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
234 if request.method == :post # 2 ページ目とか
240 @customer = Customer.find(@login_customer.id)
242 @customer.editting = false if request.mobile?
243 unless @customer.valid?
244 render :action => :edit
248 @password = @customer.raw_password
249 render :action => :edit_confirm
255 return render(:action => 'edit')
257 @customer = Customer.find(@login_customer.id)
258 @customer.attributes = params[:customer]
259 unless @customer.valid?
260 render :action => 'edit'
263 @customer.set_mobile request.mobile
265 set_login_customer(@customer)
269 @delivery_addresses = @login_customer.delivery_addresses
270 render :action => 'delivery_list'
274 redirect_to :action => :delivery
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
284 render :action => 'delivery_new' unless performed?
287 def delivery_new_popup
290 render :action => 'delivery_new_popup' unless performed?
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')
301 render :action => 'delivery_confirm'
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
312 render :action => 'delivery_edit' unless performed?
315 def delivery_edit_popup
318 render :action => 'delivery_edit_popup' unless performed?
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')
330 render :action => 'delivery_confirm_popup'
332 render :action => 'delivery_confirm'
336 def delivery_complete
337 @popup = !params[:popup].blank? && params[:popup] == "true"
338 if params[:id].blank?
341 return (@popup ? delivery_new_popup : delivery_new)
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
350 return (@popup ? delivery_edit_popup : delivery_edit)
352 @delivery_address = find_delivery_address @login_customer, params[:id].to_i
354 @delivery_address.attributes = params[:delivery_address] if @delivery_address
355 if @delivery_address && @delivery_address.save
356 flash.now[:notice] = 'データを保存しました。'
358 flash.now[:error] = 'データの保存に失敗しました。'
361 render :action => 'close_popup'
363 if params[:backurl] # カートから来た時
364 redirect_to :controller => 'cart', :action => 'shipping'
366 redirect_to :action => 'delivery'
372 @delivery_address = find_delivery_address @login_customer, params[:id].to_i
373 if @delivery_address and @delivery_address.destroy
374 flash.now[:notice] = '削除しました。'
376 flash.now[:notice] = '削除に失敗しました。'
378 if params[:backurl] # カートから来た時
379 redirect_to params[:backurl]
381 redirect_to :action => 'delivery'
386 key = params[:activation_key]
388 @customer = Customer.activate_email(key)
390 @customer.reachable = true
391 save_carts(@customer.carts)
392 @customer.carts.clear
403 def withdraw_complete
405 head :method_not_allowed
408 @login_customer.withdraw
409 set_login_customer(nil)
413 redirect_to :action => :salvage
417 flash.now[:notice] = flash.now[:error] = nil
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
428 render :action => "salvage"
431 correct = @customer && columns.all?{ |c| @customer[c] == @input[c] }
433 flash.now[:notice] = "入力された情報が正しくありません"
434 render :action => "salvage"
437 @password = @customer.regenerate_password!
438 unless @customer.save
439 flash.now[:notice] = "パスワードの再発行に失敗しました"
440 render :action => "salvage"
446 Notifier::deliver_mobile_reminder(@customer, @password)
448 Notifier::deliver_reminder(@customer, @password)
451 flash.now[:notice] = "メールの送信に失敗しました"
452 render :action => "salvage"
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)
467 @shop = Shop.find(:first)
470 def find_customer_by_email(email)
471 customer = Customer.find_by_email(email)
475 def find_delivery_address customer, id
476 id or raise ActiveRecord::RecordNotFound
477 conditions = ["#{DeliveryAddress.table_name}.id = ? and #{Customer.table_name}.id = ?",
479 da = DeliveryAddress.find(:first, :conditions => conditions, :include => :customer)
480 da or raise ActiveRecord::RecordNotFound
483 def load_seo_mypage_index
484 @seo = Seo.find(:first, :conditions=>{ :page_type => Seo::MYPAGE_INDEX})
487 def get_delivery_address
488 @popup ||= !params[:popup].blank? && params[:popup] == "true"
490 @delivery_address.attributes = params[:delivery_address]||{}
492 @delivery_address.target_columns = []
496 redirect_to :controller => :cart, :action => :shipping
498 redirect_to :action => :delivery
503 if params[:delivery_address]
504 @delivery_address.target_columns = params[:delivery_address].keys.map(&:to_s)
506 #@delivery_address.update_address!(@delivery_address.zipcode_first_changed?)
507 @delivery_address.update_address!
508 @stage += 1 if @delivery_address.valid?
512 @customer.attributes = params[:c] if params[:c]
514 @customer.attributes = params[:customer]
515 @customer.target_columns = params[:customer].keys.map(&:to_s)
517 @customer.set_mobile request.mobile # 携帯電話の種類
518 @customer.editting = true
520 @customer.target_columns = []
524 @customer.update_address!(false)
525 @stage += 1 if @customer.valid? && @stage
528 def redirect_for_login_user
529 redirect_to :controller => :portal, :action => :show if @login_customer
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
539 logger.debug @user_navigation_list
540 @user_navigation_list.flatten!