Contacts page

This commit is contained in:
Eugene Korbut 2009-02-08 04:46:42 +10:00
parent 02a3200143
commit 0199223976
8 changed files with 44 additions and 51 deletions

View file

@ -1,68 +0,0 @@
require 'cdfutils'
require_association 'contact_group'
class Contact < ActiveRecord::Base
has_and_belongs_to_many :groups, :class_name => "ContactGroup", :join_table => "contact_contact_groups", :association_foreign_key => "contact_group_id", :foreign_key => "contact_id"
# Finder methods follow
def Contact.find_by_user(user_id)
find(:all, :conditions => ["customer_id = ?", user_id], :order => "fname asc", :limit => 10)
end
def Contact.find_by_user_email(user_id, email)
find(:first, :conditions => ["customer_id = #{user_id} and email = ?", email])
end
def Contact.find_by_group_user(user_id, grp_id)
result = Array.new
find(:all, :conditions => ["customer_id = ?", user_id], :order => "fname asc").each { |c|
begin
c.groups.find(grp_id)
result << c
rescue ActiveRecord::RecordNotFound
end
}
result
end
def Contact.find_by_user_letter(user_id, letter)
find_by_sql("select * from contacts where customer_id=#{user_id} and substr(UPPER(fname),1,1) = '#{letter}' order by fname")
end
def full_name
"#{fname}&nbsp;#{lname}"
end
def show_name
"#{fname} #{lname}"
end
def full_address
"#{fname} #{lname}<#{email}>"
end
protected
def validate
errors.add 'fname', _('Please enter your first name (2 to 20 characters).') unless self.fname =~ /^.{2,20}$/i
errors.add 'lname', _('Please enter your surname (2 to 20 characters).') unless self.lname =~ /^.{2,20}$/i
# Contact e-mail cannot be changed
unless self.new_record?
old_record = Contact.find(self.id)
errors.add 'email', _('Contacts email cannot be changed.') unless old_record.email == self.email
end
end
def validate_on_create
# Contact e-mail cannot be changed, so we only need to validate it on create
errors.add 'email', _('Please enter a valid email address.') unless valid_email?(self.email)
# Already existing e-mail in contacts for this user is not allowed
if self.new_record?
if Contact.find_first("email = '#{email}' and customer_id = #{customer_id}")
errors.add('email', _('An account for your email address already exists.'))
end
end
end
end

View file

@ -1,79 +0,0 @@
<h1><%=_('Edit/Create contact')%></h1>
<div id="header">
<ul id="primary">
<li><%=link_folders%></li>
<li><%=link_send_mail%></li>
<li><%=link_mail_prefs%></li>
<li><%=link_mail_filters%></li>
<li><span><%= _('Contacts') %></span>
<ul id="secondary">
<li><%=link_contact_list%></li>
<% if ret = @session["return_to"] %>
<li><%=link_to(_('Back to message'), ret) %></li>
<% end %>
</ul>
</li>
</ul>
</div>
<div id="tab_main">
<div id="tab_content">
<%=
form_tag(
link_contact_save,
'method' => 'post',
'class' => 'two_columns'
)
%>
<%= form_input(:hidden_field, 'contact', 'id') %>
<%= form_input(:hidden_field, 'contact', 'customer_id') %>
<table>
<%= form_input(:text_field, 'contact', 'fname', _('First name'), 'class'=>'two_columns') %>
<%= form_input(:text_field, 'contact', 'lname', _('Last name'), 'class'=>'two_columns') %>
<%= form_input((@contact.new_record? ? :text_field : :read_only_field), 'contact', 'email', _('E-mail'), 'class'=>'two_columns')%>
</table>
<% for group in @contactgroups %>
<input id="groups[<%=group.id%>]" type="hidden" name="groups[<%=group.id%>]" value="<%=@groups[group.id]%>">
<% end %>
<% if not(@contactgroups.empty?) %>
<%=_('Contact belong to these groups')%>:
<table class="list">
<tr>
<%
end
col = 1
for group in @contactgroups %>
<th>
<input id="groups[<%=group.id%>]" type="checkbox" name="groups[<%=group.id%>]" value="<%=@groups[group.id]%>" onclick="toggleCheckbox(this)"
<%=@groups[group.id] == 1 ? " checked " : " " %> >
&nbsp;<%=group.name %>
</th>
<% if col%2 == 0 %>
</tr>
<tr>
<% end
col = col + 1 %>
<% end %>
<% if col%2 == 0 and not(@contactgroups.empty?) %>
<th>&nbsp;</th>
<% end %>
<% if not(@contactgroups.empty?) %>
</tr>
</table>
<% end %>
<table class="edit">
<tr>
<td colspan=2 class="buttonBar">
<input type="submit" name="paction" value="<%=_('Save')%>"/>
<input type="submit" name="paction" value="<%=_('Save and add another')%>"/>
</td>
</tr>
</table>
<%= end_form_tag %>
</div>
</div>

View file

@ -1,26 +0,0 @@
<h1><%=_('Add multiple contacts')%></h1>
<% if @flash["errors"] and not @flash["errors"].empty?%>
<%= _('Errors')%>
<ul>
<% @flash["errors"].each do |message| %>
<li><%= message %>
<% end %>
</ul>
<% end %>
<form action="<%=link_import_preview%>" enctype="multipart/form-data" method="post">
<%= radio_button("contact", "file_type", "1")%> <%= _('Comma-separated (CSV) file')%>
<%= radio_button("contact", "file_type", "2")%> <%= _('Tab-delimited text file')%>
<table>
<tr>
<th><label for="contact[data]"><%=_('Select file')%></label></th>
<td><input type="file" name="contact[data]"/></td>
</tr>
<tr>
<td colspan=2>
<input type="submit" value="<%=_('Import')%>"/>
<input type="button" value="<%= _('Back to contacts')%>" onclick="window.location='<%=link_contact_list%>'">
<input type="button" value="<%= _('Back to folders')%>" onclick="window.location='<%=link_main_index%>'">
</td>
</tr>
</table>
</form>

View file

@ -1,11 +0,0 @@
<script language="javascript">
<% for to in @tos %>
respondTo("<%=to.full_address%>", "<%=to.id%>");
<% end %>
<% for cc in @ccs %>
respondCC("<%=cc.full_address%>");
<% end %>
<% for bcc in @bccs %>
respondBCC("<%=bcc.full_address%>");
<% end %>
</script>

View file

@ -1,43 +0,0 @@
<h1><%= _('Contacts You Are About To Import')%></h1>
<% if @flash["errors"] and not @flash["errors"].empty?%>
<%= _('Errors')%>
<ul>
<% @flash["errors"].each do |message| %>
<li><%= message %>
<% end %>
</ul>
<% end %>
<form action="<%=link_contact_import%>" method="post">
<table class="list">
<tr>
<th>&nbsp;</th>
<th width="100px"><%= _('First name')%></th>
<th width="100px"><%= _('Last name')%></th>
<th><%= _('E-mail')%></th>
</tr>
<%
for i in 0...@contacts.length
contact = @contacts[i]
%>
<tr class="<%= alternator %>">
<td><%=i+1%></td>
<td><input type="text" name="contact[<%=i%>][fname]" value="<%=contact.fname%>" size="15" /></td>
<td><input type="text" name="contact[<%=i%>][lname]" value="<%=contact.lname%>" size="15" /></td>
<td><input type="text" name="contact[<%=i%>][email]" value="<%=contact.email%>" size="45" /></td>
</tr>
<% end %>
<tr>
<td colspan=4 class="buttonBar">
<input type="submit" value="<%= _('Import')%>">
<input type="button" value="<%= _('Choose another file')%>" onclick="window.location='<%=link_contact_add_multiple%>'">
<input type="button" value="<%= _('Back to contacts')%>" onclick="window.location='<%=link_contact_list%>'">
<input type="button" value="<%= _('Back to folders')%>" onclick="window.location='<%=link_main_index%>'">
</td>
</tr>
</table>
</form>

View file

@ -1,114 +0,0 @@
<h1><%= _('Contacts')%></h1>
<div id="header">
<ul id="primary">
<li><%=link_folders%></li>
<li><%=link_send_mail%></li>
<li><%=link_mail_prefs%></li>
<li><%=link_mail_filters%></li>
<li><span><%= _('Contacts') %></span>
<ul id="secondary">
<li><%=link_contact_add_one%></li>
<li><%=link_contact_add_multiple%></li>
<% if ret = @session["return_to"] %>
<li><%=link_to(_('Back to message'), ret) %></li>
<% end %>
</ul>
</li>
</ul>
</div>
<div id="tab_main">
<div id="tab_content">
<% if @flash["alert"] %><ul><li><%= @flash["alert"] %></li></ul><% end %>
<form action="<%=link_contact_choose%>?mode=<%=@mode%>" method="post">
<input type="hidden" name="mode" value="<%=@mode%>"/>
<% if @group_id and not @group_id.nil? %>
<input type="hidden" name="group_id" value="<%=@group_id%>"/>
<% end %>
<table class="list">
<tr>
<td colspan="4" id="alphaListHeader">
<%
letters = CDF::CONFIG[:contact_letters]
for letterIndex in 0...letters.size
letter = letters[letterIndex] %>
<%= link_to(letter, :controller=>"contact", :action=>"listLetter", :id=>letterIndex, :params=>{"mode"=>@mode, "group_id"=>(@group_id ? @group_id : nil)}) %>
<% end %>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<%= link_to(_('Show all'), :controller=>"contact", :action=>"list", :id=>(@group_id ? @group_id : nil), :params=>{"mode"=>@mode})%>
</td>
</tr>
<tr>
<td colspan="3"><%= @contact_pages.basic_html(self, 2, false, {"mode"=>@mode}) %></td>
</tr>
<% if @mode == "choose" %>
<tr>
<th><%= _('To&nbsp;CC&nbsp;BCC')%></th>
<th><%= _('Name')%></th>
<th><%= _('E-mail')%></th>
</tr>
<% for contact in @contacts %>
<tr class="<%= alternator %>">
<td><input type="checkbox" name="contacts_to[<%=contact.id%>]" value="1"/>
<input type="checkbox" name="contacts_cc[<%=contact.id%>]" value="1"/>
<input type="checkbox" name="contacts_bcc[<%=contact.id%>]" value="1"/></td>
<td><%=contact.full_name%></td>
<td><%=contact.email%></td>
</tr>
<% end %>
<tr class="rowsep"><td colspan="3"><%=_('Groups')%>:</td></tr>
<% for group in @contactgroups %>
<tr class="<%= alternator %>">
<td><input type="checkbox" name="groups_to[<%=group.id%>]" value="1"/>
<input type="checkbox" name="groups_cc[<%=group.id%>]" value="1"/>
<input type="checkbox" name="groups_bcc[<%=group.id%>]" value="1"/></td>
<td><%=group.name%></td>
<td>&nbsp;</td>
</tr>
<% end %>
<tr>
<td colspan=3 class="buttonBar">
<input type="submit" value="<%= _('choose')%>">
<input type="button" value="<%= _('cancel')%>" onclick="javascript:window.close();">
</td>
</tr>
<% elsif @mode == "groups"%>
<tr>
<th></th>
<th width="200px"><%= _('Name')%></th>
<th><%= _('E-mail')%></th>
</tr>
<% for contact in @contacts %>
<input type="hidden" id="contacts_for_group[<%=contact.id%>]" name="contacts_for_group[<%=contact.id%>]" value="<%=@contacts_for_group[contact.id]%>" >
<tr class="<%= alternator %>">
<td><input type="checkbox" id="contacts_for_group[<%=contact.id%>]" name="contacts_for_group[<%=contact.id%>]"
value="<%=@contacts_for_group[contact.id]%>" onclick="toggleCheckbox(this)"
<%=@contacts_for_group[contact.id] == 1 ? " checked " : " " %> ></td>
<td><%=contact.full_name%></td>
<td><%=contact.email%></td>
</tr>
<% end %>
<tr>
<td colspan=2 class="buttonBar">
<input type="submit" value="<%= _('Save')%>">
<input type="button" value="<%= _('Back to groups')%>" onclick="window.location='<%=link_contact_group_list%>'">
</td>
</tr>
<% else %>
<tr>
<th width="200px"><%= _('Name')%></th>
<th><%= _('E-mail')%></th>
<th>&nbsp;</th>
</tr>
<% for contact in @contacts %>
<tr class="<%= alternator %>">
<td><%= link_to(contact.full_name, :controller=>"/contacts/contact", :action => "edit", :id => contact.id ) %></td>
<td><%= link_to( contact.email, :controller => "/webmail", :action => "compose", :params => { "mail[to]" => contact.email } ) %></td>
<td><%= link_to(_('delete'), {:controller=>'/contacts/contact', :action=>'delete', :id=>contact.id}, {:confirm=>sprintf(_('DELETE CONTACT?\r\n\Name - %s\r\nE-mail - %s'), contact.show_name, contact.email)})%></td>
</tr>
<% end %>
<% end %>
</table>
</form>
</div>
</div>

View file

@ -1,398 +0,0 @@
class Contacts::ContactController < ApplicationController
uses_component_template_root
model :customer
model :contact
model :contact_group
helper :pagination
layout :select_layout
def index
redirect_to(:action =>"list")
end
def list
@contact_pages = Paginator.new(self, Contact.count("customer_id = #{logged_user}"), CDF::CONFIG[:contacts_per_page], @params['page'])
@contacts = Contact.find(:all, :conditions=>["customer_id = #{logged_user}"], :order=>['fname'], :limit=>CDF::CONFIG[:contacts_per_page], :offset=>@contact_pages.current.offset)
if @params["mode"] == "groups"
if @params["id"] and not @params["id"].nil? and not @params["id"] == ''
@group_id = @params["id"].to_i
@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
end
def listLetter
letters = CDF::CONFIG[:contact_letters]
@contact_pages = Paginator.new(self, Contact.count(
["customer_id = %s and substr(UPPER(fname),1,1) = '%s'", logged_user, letters[@params['id'].to_i]]), CDF::CONFIG[:contacts_per_page], @params['page'])
@contacts = Contact.find(:all, :conditions=>["customer_id = %s and substr(UPPER(fname),1,1) = '%s'", logged_user, letters[@params['id'].to_i]],
:order=>['fname'], :limit=>CDF::CONFIG[:contacts_per_page], :offset=>@contact_pages.current.offset)
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
@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
render :action => "list"
end
def add
@contact = Contact.new
@contact.customer_id = logged_user
# load related lists
loadLists
# Init groups: because of checkbox
# Set all to 0 => unchecked
@groups = Hash.new
@contactgroups.each {|g|
@groups[g.id] = 0
}
end
def add_multiple
@contact = Contact.new
@contact["file_type"] = "1"
end
def add_from_mail
cstr = @params['cstr']
retmsg = @params['retmsg']
@session["return_to"] = url_for(:controller=>'/webmail/webmail',
: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
if @contact = Contact.find_by_user_email(logged_user, email)
# load related lists
loadLists
@contact.fname, @contact.lname = fname, lname
# 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
# load related lists
loadLists
# Init groups: because of checkbox
# Set all to 0 => unchecked
@groups = Hash.new
@contactgroups.each {|g|
@groups[g.id] = 0
}
end
render :action => "add"
end
def import_preview
file = @params["contact"]["data"]
flash["errors"] = Array.new
if file.size == 0
flash["errors"] << _('You haven\'t selected file or the file is empty')
@contact = Contact.new
@contact["file_type"] = @params["contact"]["file_type"]
render :action => "add_multiple"
end
file_type = @params["contact"]["file_type"]
if file_type.nil? or file_type == '1'
separator = ','
else
separator = /\t/
end
@contacts = Array.new
emails = Array.new
file.each {|line|
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
# 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!')
else
emails << cont.email
end
@contacts << cont
}
end
def import
contacts_count = @params["contact"].length
contacts_to_import = @params["contact"]
@contacts = Array.new
emails = Array.new
flash["errors"] = Array.new
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"]
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!')
else
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
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
@contacts << contact
end # for
# 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
def choose
if @params["mode"] == "groups"
save_groups
end
@tos, @ccs, @bccs = Array.new, Array.new, Array.new
@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"]
@params["groups_to"].each{ |id,value|
ContactGroup.find(id).contacts.each {|c| @tos << c} if value == "1" } if @params["groups_to"]
@params["groups_cc"].each{ |id,value|
ContactGroup.find(id).contacts.each {|c| @ccs << c} if value == "1" } if @params["groups_cc"]
@params["groups_bcc"].each{ |id,value|
ContactGroup.find(id).contacts.each {|c| @bccs << c} if value == "1" } if @params["groups_bcc"]
end
def save_groups
contacts_for_group = @params["contacts_for_group"]
group_id = @params["group_id"]
contact_group = ContactGroup.find(group_id)
contacts_for_group.each { |contact_id,value|
contact = Contact.find(contact_id)
if value == "1" and not contact_group.contacts.include?(contact)
contact_group.contacts << contact
end
if value == "0" and contact_group.contacts.include?(contact)
contact_group.contacts.delete(contact)
end
}
redirect_to(:action=>"list", :id=>group_id, :params=>{"mode"=>@params["mode"]})
end
def edit
@contact = Contact.find(@params["id"])
# load related lists
loadLists
# 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
}
render :action => "add"
end
# Insert or update
def save
logger.info("BEGIN")
if @params["contact"]["id"] == ""
# New contact
@contact = Contact.create(@params["contact"])
else
# Edit existing
@contact = Contact.find(@params["contact"]["id"])
@contact.attributes = @params["contact"]
end
@contactgroups = ContactGroup.find_by_user(logged_user)
# Groups displayed
groups = @params['groups']
tempGroups = Array.new
tempGroups.concat(@contact.groups)
@contactgroups.each { |cgroup|
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
if groups["#{cgroup.id}"] == "0" and includesCGroup
@contact.groups.delete(cgroup)
end
}
if @contact.save
if @params["paction"] == _('Save')
redirect_to :controller => "/contacts/contact", :action =>"list"
else
redirect_to :controller => "/contacts/contact", :action =>"add"
end
else
loadLists
@groups = Hash.new
@contactgroups.each {|g|
if @contact.groups.include?(g)
@groups[g.id] = 1
else
@groups[g.id] = 0
end
}
render :action => "add"
end
end
def delete
Contact.destroy(@params['id'])
redirect_to(:action=>'list')
end
protected
def secure_user?() true end
def additional_scripts()
add_s = ''
if action_name == "choose"
add_s<<'<script type="text/javascript" src="/javascripts/global.js"></script>'
add_s<<'<script type="text/javascript" src="/javascripts/contact_choose.js"></script>'
end
add_s
end
def onload_function()
if action_name == "choose"
"javascript:respondToCaller();"
else
""
end
end
private
def select_layout
if @params["mode"] == "choose"
@mode = "choose"
@contactgroups = ContactGroup.find_by_user(logged_user)
'chooser'
elsif @params["mode"] == "groups"
@mode = "groups"
'public'
else
@mode = "normal"
'public'
end
end
def loadLists
if @contactgroups.nil?
@contactgroups = ContactGroup.find_by_user(logged_user)
end
end
end

View file

@ -1,40 +0,0 @@
module Contacts::ContactHelper
def link_import_preview() "/contacts/contact/import_preview" end
def link_main_index() "/webmail/webmail/folders" end
def link_contact_save() "/contacts/contact/save" end
def link_contact_import() "/contacts/contact/import" end
def link_contact_choose() "/contacts/contact/choose" end
def link_contact_list
link_to(_('List'), :controller => "/contacts/contact", :action => "list")
end
def link_contact_add_one
link_to(_('Add one contact'), :controller => "/contacts/contact", :action => "add")
end
def link_contact_add_multiple
link_to(_('Add multiple'), :controller => "/contacts/contact", :action => "add_multiple")
end
def link_contact_group_list
link_to(_('Groups'), :controller => "/contacts/contact_group", :action => "list")
end
def link_folders
link_to(_('Folders'), :controller=>"/webmail/webmail", :action=>"messages")
end
def link_send_mail
link_to(_('Compose'), :controller=>"/webmail/webmail", :action=>"compose")
end
def link_mail_prefs
link_to(_('Preferences'), :controller=>"/webmail/webmail", :action=>"prefs")
end
def link_mail_filters
link_to(_('Filters'), :controller=>"/webmail/webmail", :action=>"filters")
end
end