Adding fields to contacts - solved

Chen Naor chennaor@netvision.net.il
Sun, 28 Jan 2001 23:49:30 +0200


Hello Stephen,

I just finish adding the fields (home-address and home-phone) to the contact
database and pages.
It will be hard to give you the diffs because my code is a bit customize to
my client needs, but here is the explanation with the code parts you have to
add:
I added 2 fields 1. homeaddress 2.homephone.

1). Add to your db to table imp_addr the fields (in my case homeaddress &
homephone).
2). Change  horde/imp/lib/db.pgsql (or other one if you work with a
different db than postgresql).
--------------------------------------------------
function imp_get_addresses ($user, $server) {
   global $default;

   /* post: returns a 2d array of addresses where each
    element is an array in which element 0 is the address,
    element 1 is the nickname, and element 2 is the fullname.
    it returns false on failure.
    */

   if (!($db = imp_open_pg_db())) { return false; }
   if (!($result = pg_Exec($db, "select address, nickname, fullname,
homeaddress, homephone from $default->db_address_table where
username='$user@$server' order by nickname"))) { return false; }
   if (($rowcount = pg_NumRows($result))==0) { return false; }

   for ($i=0; $i<$rowcount; $i++) {

$return[$i]=array(0=>pg_Result($result,$i,"address"),1=>pg_Result($result,$i
,"nickname"),2=>pg_Result($result,$i,"fullname"),3=>pg_Result($result,$i,"ho
meaddress"),4=>pg_Result($result,$i,"homephone"));
   }
   return($return);
}
----------------------------------------------------
function imp_update_address ($old_address, $address, $nickname, $fullname,
$homeaddress, $homephone, $user, $server) {
   global $default;

   /* post: changes the entry for $old_address to $address, $nickname,
$fullname.
    returns true on success and false on failure
    */

   if (!($db = imp_open_pg_db())) { return false; }
   if (!($result = pg_Exec($db, "update $default->db_address_table set
address='$address', nickname='$nickname', fullname='$fullname',
homeaddress='$homeaddress', homephone='$homephone' where
username='$user@$server' and address='$old_address'"))) { return false; }
   pg_Exec($db, "COMMIT");
   return true;
}
--------------------------------------------------------
function imp_add_address ($address, $nickname, $fullname, $homeaddress,
$homephone, $user, $server) {
        global $default;

        /* post: adds $address, $nickname, $fullname, $homeaddress,
$homephone, to the addressbook for $user@$server
           returns true on success and false on failure
    */

        if (!($db = imp_open_pg_db())) { return false; }
        if (!($result = pg_Exec($db, "delete from $default->db_address_table
where username='$user@$server' and address='$address'"))) { ret
urn true; }
        if (!($result = pg_exec($db, "insert into $default->db_address_table
(username, address, nickname, fullname, homeaddress, homephone)
 values ('$user@$server', '$address', '$nickname', '$fullname',
'$homeaddress', '$homephone')"))) { return false; }
        pg_exec($db, "COMMIT");

        return true;
}
==========================
2). Change horde/imp/contact.php3.
----------------------------------------------
   case ADD_ADDR:
                if (!empty($new_address) && !empty($new_nickname) &&
!empty($new_fullname)) {
                        // escape/quote special characters as necessary
                        if ($new_nickname[0] == '"' &&
$new_nickname[strlen($new_nickname) - 1] == '"') {
                            $new_nickname = substr($new_nickname, 1,
strlen($new_nickname) - 2);
                        }
                        $nickname = $new_nickname;
                        $clean_nickname = imap_rfc822_write_address('', '',
$nickname);
                        $clean_nickname = substr($clean_nickname, 0,
strlen($clean_nickname)-4);
                        $clean_nickname = str_replace("'", "\\'",
$clean_nickname);
                        if ($new_fullname[0] == '"' &&
$new_fullname[strlen($new_fullname) - 1] == '"') {
                            $new_fullname = substr($new_fullname, 1,
strlen($new_fullname) - 2);
                        }
                        $fullname = $new_fullname;
                        $clean_fullname = imap_rfc822_write_address('', '',
$fullname);
                        $clean_fullname = substr($clean_fullname, 0,
strlen($clean_fullname)-4);
                        $clean_fullname = str_replace("'", "\\'",
$clean_fullname);
                        $clean_address = str_replace(',', '', $new_address);
                        $clean_address = str_replace("'", '',
$clean_address);

                        $errormsg = '';
                        if ($clean_address != $new_address) {
                                $errormsg .= $lang->error_address_chars;
                                echo '<script
language="javascript">alert(\'' . addslashes($errormsg) . '\')</script>';
                        }
                        $clean_address = addslashes($clean_address);
                //added by chen naor
                        $homeaddress = $new_homeaddress;
                        $clean_homeaddress = imap_rfc822_write_address('',
'', $homeaddress);
                        $clean_homeaddress = substr($clean_homeaddress, 0,
strlen($clean_homeaddress)-4);
                        $clean_homeaddress = str_replace("'", "\\'",
$clean_homeaddress);

                        $homephone = $new_homephone;
                        $clean_homephone = imap_rfc822_write_address('', '',
$homephone);
                        $clean_homephone = substr($clean_homephone, 0,
strlen($clean_homephone)-4);
                        $clean_homephone = str_replace("'", "\\'",
$clean_homephone);

                        if (!(imp_add_address($clean_address,
$clean_nickname, $clean_fullname, $clean_homeaddress, $clean_homephone,
$imp->user, $imp->server))) {
                                $errormsg .= $lang->error_create_address;
                        }
                }
                $new_address = '';
                break;

 case UPDATE_ADDR:
                if ($old_address && $new_address && $new_nickname &&
$new_fullname) {
                        // escape/quote special characters as necessary
                        if ($new_nickname[0] == '"' &&
$new_nickname[strlen($new_nickname) - 1] == '"') {
                            $new_nickname = substr($new_nickname, 1,
strlen($new_nickname) - 2);
                        }
                        $nickname = $new_nickname;
                        $clean_nickname = imap_rfc822_write_address('', '',
$nickname);
                        $clean_nickname = substr($clean_nickname, 0,
strlen($clean_nickname)-4);
                        $clean_nickname = str_replace("'", "\\'",
$clean_nickname);
                        if ($new_fullname[0] == '"' &&
$new_fullname[strlen($new_fullname) - 1] == '"') {
                            $new_fullname = substr($new_fullname, 1,
strlen($new_fullname) - 2);
                        }
                        $fullname = $new_fullname;
                        $clean_fullname = imap_rfc822_write_address('', '',
$fullname);
                        $clean_fullname = substr($clean_fullname, 0,
strlen($clean_fullname)-4);
                        $clean_fullname = str_replace("'", "\\'",
$clean_fullname);
                        $clean_address = str_replace(',', '', $new_address);
                        $clean_address = str_replace("'", '',
$clean_address);
                //added by chen naor
                        $homeaddress = $new_homeaddress;
                        $clean_homeaddress = imap_rfc822_write_address('',
'', $homeaddress);
                        $clean_homeaddress = substr($clean_homeaddress, 0,
strlen($clean_homeaddress)-4);
                        $clean_homeaddress = str_replace("'", "\\'",
$clean_homeaddress);

                        $homephone = $new_homephone;
                        $clean_homephone = imap_rfc822_write_address('', '',
$homephone);
                        $clean_homephone = substr($clean_homephone, 0,
strlen($clean_homephone)-4);
                        $clean_homephone = str_replace("'", "\\'",
$clean_homephone);

                        $errormsg = '';
                        if ($clean_address != $new_address) {
                                $errormsg .= $lang->error_address_chars;
                                echo '<script
language="javascript">alert(\'' . addslashes($errormsg) . '\')</script>';
                        }
                        $clean_address = addslashes($clean_address);
                        if (!(imp_update_address($old_address,
$clean_address, $clean_nickname, $clean_fullname, $clean_homeaddress,
$clean_homephone, $imp->user, $imp->server))) {
                                $errormsg = $lang->error_update_address;
                        }
                }
                $new_address = '';
                break;
        }
}
=========================
4). Make the same changes to horde/imp/addcontact.php3
=========================
5). Change horde/imp/templates/contacts/addr-php3.inc
add this to the table inside of the form:
--------------------------------------------------------------
<tr>
  <td><?php echo $lang->homeaddress_string ?>&nbsp;</td>
  <td bgcolor="<?php echo $default->item_bg ?>"><input type="text"
name="new_homeaddress" value="" size="40"></td>
</tr>
<tr>
  <td><?php echo $lang->homephone_string ?>&nbsp;</td>
  <td bgcolor="<?php echo $default->item_bg ?>"><input type="text"
name="new_homephone" value="" size="40"></td>
</tr>
================================
6). Make the same changes to
horde/imp/templates/contacts/add-contact-php3.inc
================================
7). Change horde/imp/templates/contacts/javascript.inc
-------------------------------------------------------
function ClearSelection() {
   window.document.addressbook.new_address.value = "";
   window.document.addressbook.new_nickname.value = "";
   window.document.addressbook.new_fullname.value = "";
   window.document.addressbook.new_homeaddress.value = "";
   window.document.addressbook.new_homephone.value = "";
}
----------------------------------------------------------
var Homeaddresses = new Array (
  <?php
  if (is_array($addresses)) {
    for ($i = 0; $i < count($addresses); $i++) {
      if ($i > 0) { echo ' , '; }
      echo '"' . addslashes($addresses[$i][3]) . '"';
    }
    echo "\n";
  }
  ?>
)

var Homephones = new Array (
  <?php
  if (is_array($addresses)) {
    for ($i = 0; $i < count($addresses); $i++) {
      if ($i > 0) { echo ' , '; }
      echo '"' . addslashes($addresses[$i][4]) . '"';
    }
    echo "\n";
  }
  ?>
)
-------------------------------------------------------------
function setFields_ins () {
   document.addressbook.new_address.value =
Addresses[document.addressbook.old_address.selectedIndex];
   document.addressbook.new_nickname.value =
Nicknames[document.addressbook.old_address.selectedIndex];
   document.addressbook.new_fullname.value =
Fullnames[document.addressbook.old_address.selectedIndex];
   document.addressbook.new_homeaddress.value =
Homeaddresses[document.addressbook.old_address.selectedIndex];
   document.addressbook.new_homephone.value =
Homephones[document.addressbook.old_address.selectedIndex];
}
==================================

This is it, a little bit long but it's working,
Good luck,

Chen Naor
Lilux Systems.
http://www.lilux.co.il
email: chen@lilux.co.il