negos_app/app/controllers/public/customers_controller.rb

153 lines
3.3 KiB
Ruby
Executable File

class Public::CustomersController < ApplicationController
layout "public"
before_filter :auth_customer, :except => [:new, :create, :confirm, :show, :parrainage, :mail, :find_parrain]
def parrainage
cookies.permanent[:mlm_token] = params[:mlm_token]
redirect_to "/"
end
def index
@customers = Customer.all
end
def show
per_page = (params[:per_page] and params[:per_page] != "") ? params[:per_page] : 10
page = (params[:page] and params[:page] != "") ? params[:page] : 1
@customer = Customer.find(params[:id])
@annonces = @customer.annonces.valid_to_show.order("list_head DESC, created_at DESC").page(page).per(per_page).all
end
def new
@customer = Customer.new()
if cookies[:mlm_token] and @parent = Customer.find_parrain(cookies[:mlm_token])
@customer.parent_code = @parent.mlm_token.upcase
elsif params[:p] and @parent = Customer.find_parrain(params[:p])
@customer.parent_code = @parent.mlm_token.upcase
end
end
def show_details
@no_search = true
if current_customer.mlm_children_ids.include?(params[:id].to_i)
@customer = Customer.find(params[:id])
end
end
def edit
@customer = Customer.find(params[:id])
end
def create
params[:step] = "login"
@no_search = true
@customer = Customer.new(params.require(:customer).permit!)
if @customer.save
CustomerMailer.confirm(@customer).deliver
CustomerMailer.notify_ins(@customer).deliver
@customer.authenticate(params[:password])
cookies[:customer_auth_token] = @customer.token
if session[:for_annonce]
redirect_to new_public_annonce_path(:ins => "ins_finish"), notice: "Vous êtes désormais inscrit."
else
redirect_to public_my_account_path(:ins => "ins_finish"), notice: "Vous êtes désormais inscrit."
end
else
render :action => "new"
end
end
def confirm
@customer = Customer.find_by_token(params[:id])
@customer.enabled = true
@customer.save(:validate => false)
CustomerMailer.confirm_ins(@customer).deliver
cookies[:customer_auth_token] = @customer.token
redirect_to public_my_account_path, notice: "Votre adresse mail a bien été validée, merci beaucoup."
#redirect_to "/"
end
def update
@customer = Customer.find(params[:id])
if params[:order]
@customer.force_address = true
end
if @customer == current_customer
if @customer.update_attributes(params.require(:customer).permit!)
redirect_to public_my_account_path
else
if params[:customer][:step2] or params[:customer][:step3]
render :template => "public/my_account/index"
else
render :template => "public/my_account/edit_infos"
end
end
end
end
def mail
@customer = Customer.find(params[:id])
@annonce_message = AnnonceMessage.new(:destinataire => @customer)
if params[:annonce_id] and @annonce = @customer.annonces.where(:id => params[:annonce_id]).first
@annonce_message.annonce = @annonce
end
@annonce_message.expediteur = current_customer if current_customer
if @annonce_message.expediteur
@annonce_message.expediteur_mail = current_customer.email
@annonce_message.tel = current_customer.tel_number
end
end
end