[Svn] r4265 - in branches/devel-addressbook/program: include js steps/addressbook

trac at roundcube.net trac at roundcube.net
Wed Nov 24 19:50:15 CET 2010


Author: thomasb
Date: 2010-11-24 12:50:15 -0600 (Wed, 24 Nov 2010)
New Revision: 4265

Modified:
   branches/devel-addressbook/program/include/rcube_addressbook.php
   branches/devel-addressbook/program/include/rcube_contacts.php
   branches/devel-addressbook/program/include/rcube_vcard.php
   branches/devel-addressbook/program/js/app.js
   branches/devel-addressbook/program/steps/addressbook/copy.inc
   branches/devel-addressbook/program/steps/addressbook/export.inc
   branches/devel-addressbook/program/steps/addressbook/import.inc
   branches/devel-addressbook/program/steps/addressbook/save.inc
Log:
Allow copying/importing/exporting of contacts

Modified: branches/devel-addressbook/program/include/rcube_addressbook.php
===================================================================
--- branches/devel-addressbook/program/include/rcube_addressbook.php	2010-11-24 15:46:17 UTC (rev 4264)
+++ branches/devel-addressbook/program/include/rcube_addressbook.php	2010-11-24 18:50:15 UTC (rev 4265)
@@ -141,6 +141,21 @@
     }
 
     /**
+     * Create new contact records for every item in the record set
+     */
+    function insertMultiple($recset, $check=false)
+    {
+        $ids = array();
+        if (is_object($recset) && is_a($recset, rcube_result_set)) {
+            while ($row = $recset->next()) {
+                if ($insert = $this->insert($row, $check))
+                    $ids[] = $insert;
+            }
+        }
+        return $ids;
+    }
+
+    /**
      * Update a specific contact record
      *
      * @param mixed Record identifier
@@ -265,5 +280,34 @@
         /* empty for address books don't supporting groups */
         return array();
     }
+
+
+    /**
+     * Utility function to return all values of a certain data column
+     * either as flat list or grouped by subtype
+     *
+     * @param string Col name
+     * @param array  Record data array as used for saving
+     * @param boolean True to return one array with all values, False for hash array with values grouped by type
+     * @return array List of column values
+     */
+    function get_col_values($col, $data, $flat = false)
+    {
+        $out = array();
+        foreach ($data as $c => $values) {
+            if (strpos($c, $col) === 0) {
+                if ($flat) {
+                    $out = array_merge($out, (array)$values);
+                }
+                else {
+                    list($f, $type) = explode(':', $c);
+                    $out[$type] = array_merge((array)$out[$type], (array)$values);
+                }
+            }
+        }
+      
+        return $out;
+    }
+    
 }
 

Modified: branches/devel-addressbook/program/include/rcube_contacts.php
===================================================================
--- branches/devel-addressbook/program/include/rcube_contacts.php	2010-11-24 15:46:17 UTC (rev 4264)
+++ branches/devel-addressbook/program/include/rcube_contacts.php	2010-11-24 18:50:15 UTC (rev 4265)
@@ -393,8 +393,8 @@
      */
     function insert($save_data, $check=false)
     {
-        if (is_object($save_data) && is_a($save_data, rcube_result_set))
-            return $this->insert_recset($save_data, $check);
+        if (!is_array($save_data))
+            return false;
 
         $insert_id = $existing = false;
 
@@ -438,20 +438,6 @@
 
 
     /**
-     * Insert new contacts for each row in set
-     */
-    function insert_recset($result, $check=false)
-    {
-        $ids = array();
-        while ($row = $result->next()) {
-            if ($insert = $this->insert($row, $check))
-                $ids[] = $insert;
-        }
-        return $ids;
-    }
-
-
-    /**
      * Update a specific contact record
      *
      * @param mixed Record identifier
@@ -494,6 +480,7 @@
         $record['ID'] = $sql_arr[$this->primary_key];
         
         if ($sql_arr['vcard']) {
+          unset($save_arr['email']);
           $vcard = new rcube_vcard($sql_arr['vcard']);
           $record += $vcard->get_assoc() + $sql_arr;
         }
@@ -515,7 +502,7 @@
             list($field, $section) = explode(':', $key);
             foreach ((array)$values as $value) {
                 if (is_array($value) || strlen($value))
-                    $vcard->set($field, $value, strtoupper($section));
+                    $vcard->set($field, $value, $section);
             }
         }
         $out['vcard'] = $vcard->export();

Modified: branches/devel-addressbook/program/include/rcube_vcard.php
===================================================================
--- branches/devel-addressbook/program/include/rcube_vcard.php	2010-11-24 15:46:17 UTC (rev 4264)
+++ branches/devel-addressbook/program/include/rcube_vcard.php	2010-11-24 18:50:15 UTC (rev 4265)
@@ -34,6 +34,8 @@
   );
   private $fieldmap = array('phone' => 'TEL', 'birthday' => 'BDAY', 'website' => 'URL', 'notes' => 'NOTE', 'email' => 'EMAIL', 'address' => 'ADR', 'gender' => 'X-GENDER', 'maidenname' => 'X-MAIDENNAME', 'gender' => 'X-GENDER');
   private $typemap = array('iPhone' => 'mobile', 'CELL' => 'mobile');
+  private $phonetypemap = array('HOME1' => 'HOME', 'BUSINESS1' => 'WORK', 'BUSINESS2' => 'WORK2', 'WORKFAX' => 'BUSINESSFAX');
+  private $addresstypemap = array('BUSINESS' => 'WORK');
   private $immap = array('X-JABBER' => 'jabber', 'X-ICQ' => 'icq', 'X-MSN' => 'msn', 'X-AIM' => 'aim', 'X-YAHOO' => 'yahoo');
 
   public $business = false;
@@ -193,6 +195,8 @@
    */
   public function set($field, $value, $type = 'HOME')
   {
+    $field = strtolower($field);
+    $type = strtoupper($type);
     $typemap = array_flip($this->typemap);
     
     switch ($field) {
@@ -247,12 +251,19 @@
         break;
 
       case 'address':
+        if ($this->addresstypemap[$type])
+          $type = $this->addresstypemap[$type];
+
         $value = $value[0] ? $value : array('', '', $value['street'], $value['locality'], $value['region'], $value['zipcode'], $value['country']);
+
         // fall through if not empty
         if (!strlen(join('', $value)))
           break;
 
       default:
+        if ($field == 'phone' && $this->phonetypemap[$type])
+          $type = $this->phonetypemap[$type];
+
         if ($tag = $this->fieldmap[$field]) {
           $index = count($this->raw[$tag]);
           $this->raw[$tag][$index] = (array)$value;

Modified: branches/devel-addressbook/program/js/app.js
===================================================================
--- branches/devel-addressbook/program/js/app.js	2010-11-24 15:46:17 UTC (rev 4264)
+++ branches/devel-addressbook/program/js/app.js	2010-11-24 18:50:15 UTC (rev 4265)
@@ -4017,11 +4017,12 @@
         else
           label.html(colprop.label);
 
+        var name_suffix = colprop.limit != 1 ? '[]' : '';
         if (colprop.type == 'text' || colprop.type == 'date') {
           input = $('<input>')
             .addClass('ff_'+col)
             .attr('type', 'text')
-            .attr('name', '_'+col+'[]')
+            .attr('name', '_'+col+name_suffix)
             .attr('size', colprop.size)
             .appendTo(cell);
 
@@ -4034,7 +4035,7 @@
             input = $('<input>')
               .addClass('ff_'+childcol)
               .attr('type', 'text')
-              .attr('name', '_'+childcol+'[]')
+              .attr('name', '_'+childcol+name_suffix)
               .attr('size', cp.size)
               .appendTo(cell);
             cell.append(" ");
@@ -4046,7 +4047,7 @@
         else if (colprop.type == 'select') {
           input = $('<select>')
             .addClass('ff_'+col)
-            .attr('name', '_'+col+'[]')
+            .attr('name', '_'+col+name_suffix)
             .appendTo(cell);
           
           var options = input.attr('options');

Modified: branches/devel-addressbook/program/steps/addressbook/copy.inc
===================================================================
--- branches/devel-addressbook/program/steps/addressbook/copy.inc	2010-11-24 15:46:17 UTC (rev 4264)
+++ branches/devel-addressbook/program/steps/addressbook/copy.inc	2010-11-24 18:50:15 UTC (rev 4265)
@@ -48,7 +48,7 @@
           'record' => $a_record, 'source' => $target, 'group' => $target_group));
 
         if (!$plugin['abort']) {
-          if ($insert_id = $TARGET->insert($a_record, false)) {
+          if ($insert_id = $TARGET->insert($plugin['record'], false)) {
             $ids[] = $insert_id;
             $success++;
           }

Modified: branches/devel-addressbook/program/steps/addressbook/export.inc
===================================================================
--- branches/devel-addressbook/program/steps/addressbook/export.inc	2010-11-24 15:46:17 UTC (rev 4264)
+++ branches/devel-addressbook/program/steps/addressbook/export.inc	2010-11-24 18:50:15 UTC (rev 4265)
@@ -30,11 +30,16 @@
 header('Content-Disposition: attachment; filename="rcube_contacts.vcf"');
 
 while ($result && ($row = $result->next())) {
+  // copy values into vcard object
   $vcard = new rcube_vcard($row['vcard']);
-  $vcard->set('displayname', $row['name']);
-  $vcard->set('firstname', $row['firstname']);
-  $vcard->set('surname', $row['surname']);
-  $vcard->set('email', $row['email']);
+  $vcard->reset();
+  foreach ($row as $key => $values) {
+      list($field, $section) = explode(':', $key);
+      foreach ((array)$values as $value) {
+          if (is_array($value) || strlen($value))
+              $vcard->set($field, $value, strtoupper($section));
+      }
+  }
   
   echo $vcard->export();
 }

Modified: branches/devel-addressbook/program/steps/addressbook/import.inc
===================================================================
--- branches/devel-addressbook/program/steps/addressbook/import.inc	2010-11-24 15:46:17 UTC (rev 4264)
+++ branches/devel-addressbook/program/steps/addressbook/import.inc	2010-11-24 18:50:15 UTC (rev 4265)
@@ -150,13 +150,8 @@
         }
       }
       
-      $a_record = array(
-        'name' => $vcard->displayname,
-        'firstname' => $vcard->firstname,
-        'surname' => $vcard->surname,
-        'email' => $email,
-        'vcard' => $vcard->export(),
-      );
+      $a_record = $vcard->get_assoc();
+      $a_record['vcard'] = $vcard->export();
       
       $plugin = $RCMAIL->plugins->exec_hook('contact_create', array('record' => $a_record, 'source' => null));
       $a_record = $plugin['record'];

Modified: branches/devel-addressbook/program/steps/addressbook/save.inc
===================================================================
--- branches/devel-addressbook/program/steps/addressbook/save.inc	2010-11-24 15:46:17 UTC (rev 4264)
+++ branches/devel-addressbook/program/steps/addressbook/save.inc	2010-11-24 18:50:15 UTC (rev 4265)
@@ -76,15 +76,13 @@
 }
 
 // Validity checks
-foreach ($a_record as $field => $values) {
-  if (strpos($field, 'email') === 0) {
-    foreach ((array)$values as $email) {
-      $_email = idn_to_ascii($email);
-      if (!check_email($_email, false)) {
-        $OUTPUT->show_message('emailformaterror', 'warning', array('email' => $email));
-        rcmail_overwrite_action($return_action);
-        return;
-      }
+foreach ($CONTACTS->get_col_values('email', $a_record, false) as $email) {
+  if (strlen($email)) {
+    $_email = idn_to_ascii($email);
+    if (!check_email($_email, false)) {
+      $OUTPUT->show_message('emailformaterror', 'warning', array('email' => $email));
+      rcmail_overwrite_action($return_action);
+      return;
     }
   }
 }
@@ -133,10 +131,16 @@
 // insert a new contact
 else {
   // check for existing contacts
-  $existing = $CONTACTS->search('email', $a_record['email'], true, false);
+  $existing = false;
+  foreach ($CONTACTS->get_col_values('email', $a_record, true) as $email) {
+      if (($res = $CONTACTS->search('email', $email, true, false)) && $res->count) {
+          $existing = true;
+          break;
+      }
+  }
 
   // show warning message
-  if ($existing->count) {
+  if ($existing) {
     $OUTPUT->show_message('contactexists', 'warning', null, false);
     rcmail_overwrite_action('add');
     return;

_______________________________________________
http://lists.roundcube.net/mailman/listinfo/svn



More information about the Svn mailing list