145 lines
4.2 KiB
Ruby
145 lines
4.2 KiB
Ruby
# -*- coding: utf-8 -*-
|
|
class ContactsController < ApplicationController
|
|
before_filter :authenticate_user!
|
|
|
|
def index
|
|
@contacts = Contact.accessible_by(current_ability, :index)
|
|
@nav_active = "overview"
|
|
end
|
|
|
|
def new
|
|
prepare_for_new(nil)
|
|
end
|
|
|
|
def create
|
|
c = Contact.new
|
|
c.firstname = params[:contact][:firstname]
|
|
c.lastname = params[:contact][:lastname]
|
|
|
|
# FIXME: DONT CARE ABOUT BUDDIES (for now)
|
|
# Do we have a buddy? If so, share the address.
|
|
# if params[:contact][:has_buddy] = 1
|
|
# # look up buddy address
|
|
# buddy_fn = params[:buddy_firstname]
|
|
# buddy_ln = params[:buddy_lastname]
|
|
# addr = Address.by_first_and_last_name(buddy_fn, buddy_ln)
|
|
# if addr.nil?
|
|
# flash[:error] = "Den Buddy " + buddy_fn + " " + buddy_ln + " gibt's nicht."
|
|
# # preserve all contact data to avoid user having to enter anything twice
|
|
# prepare_for_new(params[:contact])
|
|
# render(:action => "new") and return
|
|
# end
|
|
# # Buddy's address is OK.
|
|
# c.address = addr
|
|
# else # It's someone without a buddy.
|
|
c.build_address
|
|
c.address.street = params[:contact][:address_attributes][:street]
|
|
c.address.housenr = params[:contact][:address_attributes][:housenr]
|
|
c.address.zipcode = params[:contact][:address_attributes][:zipcode]
|
|
c.address.place = params[:contact][:address_attributes][:place]
|
|
c.address.country = params[:contact][:address_attributes][:country]
|
|
# end
|
|
|
|
# birthdate comes with the year as optional value
|
|
c.birth_year = params[:birth_year].to_i unless params[:birth_year].blank?
|
|
c.birth_month = params[:birth_month].to_i
|
|
c.birth_day = params[:birth_day].to_i
|
|
|
|
# emails come as: ["0", {"address"=>"a@b.c"}]
|
|
params[:contact][:emails_attributes].map {|k,vs| vs}.each { |val|
|
|
unless val["address"].blank?
|
|
email = c.emails.build
|
|
email.desc = val["desc"]
|
|
email.address = val["address"]
|
|
end
|
|
}
|
|
|
|
# phones come as: {"0"=>{"nr"=>"+49123456789"}, "1"=>{"nr"=>"..."}}
|
|
params[:contact][:phones_attributes].map {|k,vs| vs}.each { |val|
|
|
unless val["nr"].blank?
|
|
phone = c.phones.build
|
|
phone.desc = val["desc"]
|
|
phone.nr = val["nr"]
|
|
end
|
|
}
|
|
|
|
# add the user id for CanCan
|
|
c.user_id = current_user.id
|
|
|
|
if c.save
|
|
flash[:notice] = 'Kontakt hervorragend angelegt!'
|
|
redirect_to :action => 'index'
|
|
else
|
|
flash[:error] = 'Fehler beim Speichern eines neuen Kontakts'
|
|
render :action => 'new'
|
|
end
|
|
end
|
|
|
|
def show
|
|
@contact = Contact.find(params[:id])
|
|
end
|
|
|
|
def edit
|
|
@contact = Contact.find(params[:id])
|
|
@contact.build_address if @contact.address.nil?
|
|
setup_months_and_days
|
|
end
|
|
|
|
def update
|
|
c = Contact.find(params[:id])
|
|
c.update_attributes(params[:contact])
|
|
|
|
# birthdate comes w/ day + month separated from year
|
|
c.birth_month = params[:birth_month].to_i if c.birth_month != params[:birth_month]
|
|
c.birth_day = params[:birth_day].to_i if c.birth_day != params[:birth_day]
|
|
|
|
if c.save!
|
|
flash[:notice] = 'Kontakt aktualisiert.'
|
|
redirect_to :action => :index
|
|
else
|
|
flash[:error] = 'Fehler beim Aktualisieren des Kontakts'
|
|
render :action => :edit
|
|
end
|
|
end
|
|
|
|
def destroy
|
|
end
|
|
|
|
private
|
|
|
|
# does everything needed to render :new.
|
|
# param: a pre filled Contact map (params) or 'nil' if we don't know anything, yet.
|
|
def prepare_for_new(contact)
|
|
@contact = Contact.new
|
|
if contact.nil?
|
|
@contact.build_address
|
|
email = @contact.emails.build
|
|
phone = @contact.phones.build
|
|
else
|
|
@contact.fill_attributes_from_map(contact)
|
|
end
|
|
setup_months_and_days
|
|
@nav_active = "new_contact"
|
|
end
|
|
|
|
def setup_months_and_days
|
|
# list of valid months and days
|
|
@months = Array.new
|
|
@months << ['Januar', 1]
|
|
@months << ['Februar', 2]
|
|
@months << ['März', 3]
|
|
@months << ['April', 4]
|
|
@months << ['Mai', 5]
|
|
@months << ['Juni', 6]
|
|
@months << ['Juli', 7]
|
|
@months << ['August', 8]
|
|
@months << ['September', 9]
|
|
@months << ['Oktober', 10]
|
|
@months << ['November', 11]
|
|
@months << ['Dezember', 12]
|
|
@days = Array.new
|
|
31.times do |d|
|
|
@days << [d + 1, d + 1]
|
|
end
|
|
end
|
|
end
|