2009-02-08 16:00:26 +01:00
|
|
|
class ContactsController < ApplicationController
|
2011-06-25 18:19:07 +02:00
|
|
|
|
|
|
|
theme :theme_resolver
|
|
|
|
|
2009-01-07 20:27:12 +01:00
|
|
|
layout :select_layout
|
2011-06-25 18:19:07 +02:00
|
|
|
|
2009-01-07 20:27:12 +01:00
|
|
|
def index
|
2009-02-09 19:24:02 +01:00
|
|
|
if params[:letter] && params[:letter].any?
|
|
|
|
@contacts = Contact.for_customer(logged_user).letter(params[:letter]).paginate :page => params[:page],
|
|
|
|
:per_page => CDF::CONFIG[:contacts_per_page]
|
|
|
|
else
|
|
|
|
@contacts = Contact.for_customer(logged_user).paginate :page => params[:page], :per_page => CDF::CONFIG[:contacts_per_page]
|
2009-01-07 20:27:12 +01:00
|
|
|
end
|
|
|
|
end
|
2011-06-25 18:19:07 +02:00
|
|
|
|
2009-01-07 20:27:12 +01:00
|
|
|
def listLetter
|
|
|
|
letters = CDF::CONFIG[:contact_letters]
|
|
|
|
@contact_pages = Paginator.new(self, Contact.count(
|
2009-02-07 19:46:42 +01:00
|
|
|
["customer_id = %s and substr(UPPER(fname),1,1) = '%s'", logged_user, letters[params['id'].to_i]]), CDF::CONFIG[:contacts_per_page], params['page'])
|
2011-06-25 18:19:07 +02:00
|
|
|
@contacts = Contact.find(:all, :conditions=>["customer_id = %s and substr(UPPER(fname),1,1) = '%s'", logged_user, letters[params['id'].to_i]],
|
2009-01-07 20:27:12 +01:00
|
|
|
:order=>['fname'], :limit=>CDF::CONFIG[:contacts_per_page], :offset=>@contact_pages.current.offset)
|
2011-06-25 18:19:07 +02:00
|
|
|
|
2009-02-07 19:46:42 +01:00
|
|
|
if params["mode"] == "groups"
|
|
|
|
if params["group_id"] and not params["group_id"].nil? and not params["group_id"] == ''
|
|
|
|
@group_id = params["group_id"].to_i
|
2009-01-07 20:27:12 +01:00
|
|
|
@contacts_for_group = Hash.new
|
|
|
|
for contact in @contacts
|
|
|
|
@contacts_for_group[contact.id] = 0 # initialize
|
|
|
|
for gr in contact.groups
|
|
|
|
if gr.contact_group_id.to_i == @group_id
|
|
|
|
@contacts_for_group[contact.id] = 1 # checked
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-06-25 18:19:07 +02:00
|
|
|
|
2009-01-07 20:27:12 +01:00
|
|
|
render :action => "list"
|
|
|
|
end
|
2011-06-25 18:19:07 +02:00
|
|
|
|
2009-02-08 16:11:49 +01:00
|
|
|
def new
|
2009-01-07 20:27:12 +01:00
|
|
|
@contact = Contact.new
|
|
|
|
@contact.customer_id = logged_user
|
2011-06-25 18:19:07 +02:00
|
|
|
|
2009-01-07 20:27:12 +01:00
|
|
|
# load related lists
|
|
|
|
loadLists
|
2011-06-25 18:19:07 +02:00
|
|
|
|
2009-01-07 20:27:12 +01:00
|
|
|
# Init groups: because of checkbox
|
|
|
|
# Set all to 0 => unchecked
|
|
|
|
@groups = Hash.new
|
|
|
|
@contactgroups.each {|g|
|
|
|
|
@groups[g.id] = 0
|
|
|
|
}
|
|
|
|
end
|
2011-06-25 18:19:07 +02:00
|
|
|
|
2009-01-07 20:27:12 +01:00
|
|
|
def add_multiple
|
|
|
|
@contact = Contact.new
|
|
|
|
@contact["file_type"] = "1"
|
|
|
|
end
|
2011-06-25 18:19:07 +02:00
|
|
|
|
2009-01-07 20:27:12 +01:00
|
|
|
def add_from_mail
|
2009-02-07 19:46:42 +01:00
|
|
|
cstr = params['cstr']
|
|
|
|
retmsg = params['retmsg']
|
2011-03-25 01:06:13 +01:00
|
|
|
session["return_to"] = url_for(:controller=>'/webmail/webmail',
|
2009-01-07 20:27:12 +01:00
|
|
|
:action=>'folders',
|
|
|
|
:msg_id=>retmsg)
|
|
|
|
# parse string
|
|
|
|
if i = cstr.index("<")
|
|
|
|
name, email = cstr.slice(0, i), cstr.slice((i+1)..(cstr.strip().index(">")-1))
|
|
|
|
fname = name.split().first
|
|
|
|
lname = name.split().last if name.split().size() > 1
|
|
|
|
else
|
|
|
|
fname, lname, email = "", "", cstr
|
|
|
|
end
|
2011-06-25 18:19:07 +02:00
|
|
|
|
2009-01-07 20:27:12 +01:00
|
|
|
if @contact = Contact.find_by_user_email(logged_user, email)
|
|
|
|
# load related lists
|
|
|
|
loadLists
|
2011-06-25 18:19:07 +02:00
|
|
|
|
2009-01-07 20:27:12 +01:00
|
|
|
@contact.fname, @contact.lname = fname, lname
|
2011-06-25 18:19:07 +02:00
|
|
|
|
2009-01-07 20:27:12 +01:00
|
|
|
# groups = @contact.groups
|
|
|
|
@groups = Hash.new
|
|
|
|
@contactgroups.each {|g|
|
|
|
|
groupSelected = false
|
|
|
|
@contact.groups.each {|gr|
|
|
|
|
if gr.contact_group_id.to_i == g.id.to_i
|
|
|
|
groupSelected = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
}
|
|
|
|
if groupSelected
|
|
|
|
@groups[g.id] = 1 # checked
|
|
|
|
else
|
|
|
|
@groups[g.id] = 0 # unchecked
|
|
|
|
end
|
|
|
|
}
|
|
|
|
else
|
|
|
|
@contact = Contact.new("fname"=>fname, "lname" => lname, "email" => email)
|
|
|
|
@contact.customer_id = logged_user
|
2011-06-25 18:19:07 +02:00
|
|
|
|
2009-01-07 20:27:12 +01:00
|
|
|
# load related lists
|
|
|
|
loadLists
|
2011-06-25 18:19:07 +02:00
|
|
|
|
2009-01-07 20:27:12 +01:00
|
|
|
# Init groups: because of checkbox
|
|
|
|
# Set all to 0 => unchecked
|
|
|
|
@groups = Hash.new
|
|
|
|
@contactgroups.each {|g|
|
|
|
|
@groups[g.id] = 0
|
|
|
|
}
|
2011-06-25 18:19:07 +02:00
|
|
|
end
|
2011-03-25 01:06:13 +01:00
|
|
|
render :action => "new"
|
2009-01-07 20:27:12 +01:00
|
|
|
end
|
2011-06-25 18:19:07 +02:00
|
|
|
|
2009-01-07 20:27:12 +01:00
|
|
|
def import_preview
|
2009-02-07 19:46:42 +01:00
|
|
|
file = params["contact"]["data"]
|
2011-06-25 18:19:07 +02:00
|
|
|
|
2009-01-07 20:27:12 +01:00
|
|
|
flash["errors"] = Array.new
|
2011-06-25 18:19:07 +02:00
|
|
|
|
2009-01-07 20:27:12 +01:00
|
|
|
if file.size == 0
|
|
|
|
flash["errors"] << _('You haven\'t selected file or the file is empty')
|
|
|
|
@contact = Contact.new
|
2009-02-07 19:46:42 +01:00
|
|
|
@contact["file_type"] = params["contact"]["file_type"]
|
2009-01-07 20:27:12 +01:00
|
|
|
render :action => "add_multiple"
|
|
|
|
end
|
2011-06-25 18:19:07 +02:00
|
|
|
|
2009-02-07 19:46:42 +01:00
|
|
|
file_type = params["contact"]["file_type"]
|
2009-01-07 20:27:12 +01:00
|
|
|
if file_type.nil? or file_type == '1'
|
|
|
|
separator = ','
|
|
|
|
else
|
|
|
|
separator = /\t/
|
2011-06-25 18:19:07 +02:00
|
|
|
|
2009-01-07 20:27:12 +01:00
|
|
|
end
|
2011-06-25 18:19:07 +02:00
|
|
|
|
2009-01-07 20:27:12 +01:00
|
|
|
@contacts = Array.new
|
|
|
|
emails = Array.new
|
2011-06-25 18:19:07 +02:00
|
|
|
|
|
|
|
file.each {|line|
|
2009-01-07 20:27:12 +01:00
|
|
|
cdata = line.strip.chomp.split(separator)
|
|
|
|
cont = Contact.new
|
|
|
|
cont.fname = cdata[0].to_s.strip.chomp
|
|
|
|
cont.lname = cdata[1].to_s.strip.chomp
|
|
|
|
cont.email = cdata[2].to_s.strip.chomp
|
2011-06-25 18:19:07 +02:00
|
|
|
|
2009-01-07 20:27:12 +01:00
|
|
|
# Check for duplicate emails in the file
|
|
|
|
if emails.include?(cont.email)
|
|
|
|
flash["errors"] << sprintf(_('Contact %'), file.lineno.to_s) + ": " + _('The e-mail duplicates the e-mail of another record!')
|
2011-06-25 18:19:07 +02:00
|
|
|
else
|
2009-01-07 20:27:12 +01:00
|
|
|
emails << cont.email
|
|
|
|
end
|
2011-06-25 18:19:07 +02:00
|
|
|
|
2009-01-07 20:27:12 +01:00
|
|
|
@contacts << cont
|
|
|
|
}
|
2011-06-25 18:19:07 +02:00
|
|
|
|
2009-01-07 20:27:12 +01:00
|
|
|
end
|
2011-06-25 18:19:07 +02:00
|
|
|
|
2009-01-07 20:27:12 +01:00
|
|
|
def import
|
2009-02-07 19:46:42 +01:00
|
|
|
contacts_count = params["contact"].length
|
|
|
|
contacts_to_import = params["contact"]
|
2009-01-07 20:27:12 +01:00
|
|
|
@contacts = Array.new
|
|
|
|
emails = Array.new
|
2011-06-25 18:19:07 +02:00
|
|
|
|
2009-01-07 20:27:12 +01:00
|
|
|
flash["errors"] = Array.new
|
2011-06-25 18:19:07 +02:00
|
|
|
|
2009-01-07 20:27:12 +01:00
|
|
|
for i in 0...contacts_count
|
|
|
|
contact = Contact.new
|
|
|
|
contact.customer_id = logged_user
|
|
|
|
contact.fname = contacts_to_import[i.to_s]["fname"]
|
|
|
|
contact.lname = contacts_to_import[i.to_s]["lname"]
|
|
|
|
contact.email = contacts_to_import[i.to_s]["email"]
|
2011-06-25 18:19:07 +02:00
|
|
|
|
2009-01-07 20:27:12 +01:00
|
|
|
begin
|
|
|
|
# Check for duplicate emails in the submitted data
|
|
|
|
if emails.include?(contact.email)
|
|
|
|
flash["errors"] << sprintf(_('Contact %'), (i+1).to_s) + ": " + _('The e-mail duplicates the e-mail of another record!')
|
2011-06-25 18:19:07 +02:00
|
|
|
else
|
2009-01-07 20:27:12 +01:00
|
|
|
emails << contact.email
|
|
|
|
end
|
|
|
|
# Check if contact is valid
|
|
|
|
contact.valid?
|
|
|
|
rescue CDF::ValidationError => e
|
|
|
|
if not contact.errors.empty?
|
|
|
|
["fname", "lname", "email"].each do |attr|
|
|
|
|
attr_errors = contact.errors.on(attr)
|
|
|
|
attr_errors = [attr_errors] unless attr_errors.nil? or attr_errors.is_a? Array
|
2011-06-25 18:19:07 +02:00
|
|
|
|
2009-01-07 20:27:12 +01:00
|
|
|
if not attr_errors.nil?
|
|
|
|
attr_errors.each do |msg|
|
|
|
|
flash["errors"] << l(:contact_addmultiple_errorforcontact, (i+1).to_s) + ": " + l(msg)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end # rescue
|
2011-06-25 18:19:07 +02:00
|
|
|
|
2009-01-07 20:27:12 +01:00
|
|
|
@contacts << contact
|
|
|
|
end # for
|
2011-06-25 18:19:07 +02:00
|
|
|
|
2009-01-07 20:27:12 +01:00
|
|
|
# If there are validation errors - display them
|
|
|
|
if not flash["errors"].nil? and not flash["errors"].empty?
|
|
|
|
render :action => "import_preview"
|
|
|
|
else
|
|
|
|
# save
|
|
|
|
begin
|
|
|
|
for contact in @contacts
|
|
|
|
Contact.create(contact.attributes)
|
|
|
|
end
|
|
|
|
# Set message for successful import
|
|
|
|
flash["alert"] = Array.new
|
|
|
|
flash["alert"] << l(:contact_addmultiple_success, @contacts.length.to_s)
|
|
|
|
keep_flash()
|
|
|
|
redirect_to(:action=>"list")
|
|
|
|
rescue Exception => exc
|
|
|
|
flash["errors"] << exc
|
|
|
|
render :action => "import_preview"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-06-25 18:19:07 +02:00
|
|
|
|
2009-01-07 20:27:12 +01:00
|
|
|
def choose
|
2009-02-07 19:46:42 +01:00
|
|
|
if params["mode"] == "groups"
|
2009-01-07 20:27:12 +01:00
|
|
|
save_groups
|
|
|
|
end
|
2011-06-25 18:19:07 +02:00
|
|
|
|
2009-01-07 20:27:12 +01:00
|
|
|
@tos, @ccs, @bccs = Array.new, Array.new, Array.new
|
2011-06-25 18:19:07 +02:00
|
|
|
|
2009-02-07 19:46:42 +01:00
|
|
|
params["contacts_to"].each{ |id,value| @tos << Contact.find(id) if value == "1" } if params["contacts_to"]
|
|
|
|
params["contacts_cc"].each{ |id,value| @ccs << Contact.find(id) if value == "1" } if params["contacts_cc"]
|
|
|
|
params["contacts_bcc"].each{ |id,value| @bccs << Contact.find(id) if value == "1" } if params["contacts_bcc"]
|
2011-06-25 18:19:07 +02:00
|
|
|
|
|
|
|
params["groups_to"].each{ |id,value|
|
2009-02-07 19:46:42 +01:00
|
|
|
ContactGroup.find(id).contacts.each {|c| @tos << c} if value == "1" } if params["groups_to"]
|
2011-06-25 18:19:07 +02:00
|
|
|
params["groups_cc"].each{ |id,value|
|
2009-02-07 19:46:42 +01:00
|
|
|
ContactGroup.find(id).contacts.each {|c| @ccs << c} if value == "1" } if params["groups_cc"]
|
2011-06-25 18:19:07 +02:00
|
|
|
params["groups_bcc"].each{ |id,value|
|
2009-02-07 19:46:42 +01:00
|
|
|
ContactGroup.find(id).contacts.each {|c| @bccs << c} if value == "1" } if params["groups_bcc"]
|
2009-01-07 20:27:12 +01:00
|
|
|
end
|
2011-06-25 18:19:07 +02:00
|
|
|
|
2009-01-07 20:27:12 +01:00
|
|
|
def save_groups
|
2009-02-07 19:46:42 +01:00
|
|
|
contacts_for_group = params["contacts_for_group"]
|
|
|
|
group_id = params["group_id"]
|
2009-01-07 20:27:12 +01:00
|
|
|
contact_group = ContactGroup.find(group_id)
|
2011-06-25 18:19:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
contacts_for_group.each { |contact_id,value|
|
2009-01-07 20:27:12 +01:00
|
|
|
contact = Contact.find(contact_id)
|
2011-06-25 18:19:07 +02:00
|
|
|
if value == "1" and not contact_group.contacts.include?(contact)
|
|
|
|
contact_group.contacts << contact
|
2009-01-07 20:27:12 +01:00
|
|
|
end
|
2011-06-25 18:19:07 +02:00
|
|
|
if value == "0" and contact_group.contacts.include?(contact)
|
|
|
|
contact_group.contacts.delete(contact)
|
2009-01-07 20:27:12 +01:00
|
|
|
end
|
|
|
|
}
|
2011-03-27 20:53:06 +02:00
|
|
|
redirect_to(:action=>"index", :id=>group_id, :params=>{"mode"=>params["mode"]})
|
2009-01-07 20:27:12 +01:00
|
|
|
end
|
2011-06-25 18:19:07 +02:00
|
|
|
|
2009-01-07 20:27:12 +01:00
|
|
|
def edit
|
2009-02-07 19:46:42 +01:00
|
|
|
@contact = Contact.find(params["id"])
|
2009-01-07 20:27:12 +01:00
|
|
|
# load related lists
|
|
|
|
loadLists
|
2011-06-25 18:19:07 +02:00
|
|
|
|
2009-01-07 20:27:12 +01:00
|
|
|
# groups = @contact.groups
|
|
|
|
@groups = Hash.new
|
|
|
|
@contactgroups.each {|g|
|
|
|
|
groupSelected = false
|
|
|
|
@contact.groups.each {|gr|
|
|
|
|
if gr.contact_group_id.to_i == g.id.to_i
|
|
|
|
groupSelected = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
}
|
|
|
|
if groupSelected
|
|
|
|
@groups[g.id] = 1 # checked
|
|
|
|
else
|
|
|
|
@groups[g.id] = 0 # unchecked
|
|
|
|
end
|
2011-06-25 18:19:07 +02:00
|
|
|
}
|
2011-03-27 20:53:06 +02:00
|
|
|
render :action => "new"
|
2009-01-07 20:27:12 +01:00
|
|
|
end
|
2011-06-25 18:19:07 +02:00
|
|
|
|
2009-01-07 20:27:12 +01:00
|
|
|
# Insert or update
|
2009-02-08 16:11:49 +01:00
|
|
|
def create
|
2009-02-07 19:46:42 +01:00
|
|
|
if params["contact"]["id"] == ""
|
2009-01-07 20:27:12 +01:00
|
|
|
# New contact
|
2009-02-07 19:46:42 +01:00
|
|
|
@contact = Contact.create(params["contact"])
|
2009-01-07 20:27:12 +01:00
|
|
|
else
|
|
|
|
# Edit existing
|
2009-02-07 19:46:42 +01:00
|
|
|
@contact = Contact.find(params["contact"]["id"])
|
|
|
|
@contact.attributes = params["contact"]
|
2009-01-07 20:27:12 +01:00
|
|
|
end
|
2011-06-25 18:19:07 +02:00
|
|
|
|
2009-01-07 20:27:12 +01:00
|
|
|
@contactgroups = ContactGroup.find_by_user(logged_user)
|
|
|
|
# Groups displayed
|
2009-02-07 19:46:42 +01:00
|
|
|
groups = params['groups']
|
2009-01-07 20:27:12 +01:00
|
|
|
tempGroups = Array.new
|
|
|
|
tempGroups.concat(@contact.groups)
|
2011-06-25 18:19:07 +02:00
|
|
|
|
|
|
|
@contactgroups.each { |cgroup|
|
2009-01-07 20:27:12 +01:00
|
|
|
includesCGroup = false
|
|
|
|
tempGroups.each {|gr|
|
|
|
|
if gr.contact_group_id.to_i == cgroup.id.to_i
|
|
|
|
includesCGroup = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
}
|
|
|
|
if groups["#{cgroup.id}"] == "1" and not includesCGroup
|
|
|
|
@contact.groups << cgroup
|
|
|
|
end
|
2011-06-25 18:19:07 +02:00
|
|
|
|
2009-01-07 20:27:12 +01:00
|
|
|
if groups["#{cgroup.id}"] == "0" and includesCGroup
|
|
|
|
@contact.groups.delete(cgroup)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
if @contact.save
|
2011-03-27 20:53:06 +02:00
|
|
|
if params["paction"] == t(:save)
|
|
|
|
redirect_to :action =>:index
|
2009-01-07 20:27:12 +01:00
|
|
|
else
|
2011-03-27 20:53:06 +02:00
|
|
|
redirect_to :action => :new
|
2009-01-07 20:27:12 +01:00
|
|
|
end
|
|
|
|
else
|
|
|
|
loadLists
|
|
|
|
@groups = Hash.new
|
|
|
|
@contactgroups.each {|g|
|
|
|
|
if @contact.groups.include?(g)
|
|
|
|
@groups[g.id] = 1
|
|
|
|
else
|
|
|
|
@groups[g.id] = 0
|
2011-06-25 18:19:07 +02:00
|
|
|
end
|
2009-01-07 20:27:12 +01:00
|
|
|
}
|
2011-03-27 20:53:06 +02:00
|
|
|
render :action => :new
|
2009-01-07 20:27:12 +01:00
|
|
|
end
|
|
|
|
end
|
2011-06-25 18:19:07 +02:00
|
|
|
|
2009-01-07 20:27:12 +01:00
|
|
|
def delete
|
2009-02-07 19:46:42 +01:00
|
|
|
Contact.destroy(params['id'])
|
2011-03-27 20:53:06 +02:00
|
|
|
redirect_to(:action=>'index')
|
2009-01-07 20:27:12 +01:00
|
|
|
end
|
2011-06-25 18:19:07 +02:00
|
|
|
|
2009-01-07 20:27:12 +01:00
|
|
|
protected
|
|
|
|
def secure_user?() true end
|
2011-06-25 18:19:07 +02:00
|
|
|
def additional_scripts()
|
2009-01-07 20:27:12 +01:00
|
|
|
add_s = ''
|
|
|
|
if action_name == "choose"
|
2011-06-25 18:19:07 +02:00
|
|
|
add_s<<'<script type="text/javascript" src="/javascripts/global.js"></script>'
|
|
|
|
add_s<<'<script type="text/javascript" src="/javascripts/contact_choose.js"></script>'
|
2009-01-07 20:27:12 +01:00
|
|
|
end
|
2011-06-25 18:19:07 +02:00
|
|
|
add_s
|
|
|
|
end
|
|
|
|
|
2009-01-07 20:27:12 +01:00
|
|
|
def onload_function()
|
|
|
|
if action_name == "choose"
|
2011-06-25 18:19:07 +02:00
|
|
|
"javascript:respondToCaller();"
|
2009-01-07 20:27:12 +01:00
|
|
|
else
|
|
|
|
""
|
2011-06-25 18:19:07 +02:00
|
|
|
end
|
2009-01-07 20:27:12 +01:00
|
|
|
end
|
|
|
|
private
|
|
|
|
def select_layout
|
2009-02-07 19:46:42 +01:00
|
|
|
if params["mode"] == "choose"
|
2009-01-07 20:27:12 +01:00
|
|
|
@mode = "choose"
|
|
|
|
@contactgroups = ContactGroup.find_by_user(logged_user)
|
|
|
|
'chooser'
|
2009-02-07 19:46:42 +01:00
|
|
|
elsif params["mode"] == "groups"
|
2009-01-07 20:27:12 +01:00
|
|
|
@mode = "groups"
|
|
|
|
'public'
|
|
|
|
else
|
|
|
|
@mode = "normal"
|
|
|
|
'public'
|
|
|
|
end
|
|
|
|
end
|
2011-06-25 18:19:07 +02:00
|
|
|
|
2009-01-07 20:27:12 +01:00
|
|
|
def loadLists
|
|
|
|
if @contactgroups.nil?
|
|
|
|
@contactgroups = ContactGroup.find_by_user(logged_user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|