Changeset 5010 in subversion


Ignore:
Timestamp:
Aug 3, 2011 5:20:52 AM (22 months ago)
Author:
alec
Message:
  • Added possibility to extend rcube_vcard's fieldmap
  • Skip empty values in vCard export
Location:
trunk/roundcubemail/program
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/roundcubemail/program/include/rcube_contacts.php

    r4932 r5010  
    582582        $write_sql = array(); 
    583583        $record = $this->get_record($id, true); 
     584console($save_cols); 
    584585        $save_cols = $this->convert_save_data($save_cols, $record); 
    585  
     586console($save_cols); 
    586587        foreach ($save_cols as $col => $value) { 
    587588            $write_sql[] = sprintf("%s=%s", $this->db->quoteIdentifier($col), $this->db->quote($value)); 
  • trunk/roundcubemail/program/include/rcube_vcard.php

    r5004 r5010  
    3434    'N' => array(array('','','','','')), 
    3535  ); 
    36   private $fieldmap = array( 
     36  static private $fieldmap = array( 
    3737    'phone'    => 'TEL', 
    3838    'birthday' => 'BDAY', 
     
    7070   * Constructor 
    7171   */ 
    72   public function __construct($vcard = null, $charset = RCMAIL_CHARSET, $detect = false) 
    73   { 
     72  public function __construct($vcard = null, $charset = RCMAIL_CHARSET, $detect = false, $fieldmap = array()) 
     73  { 
     74    if (!empty($fielmap)) 
     75      $this->extend_fieldmap($fieldmap); 
     76 
    7477    if (!empty($vcard)) 
    7578      $this->load($vcard, $charset, $detect); 
     
    147150 
    148151    // convert from raw vcard data into associative data for Roundcube 
    149     foreach (array_flip($this->fieldmap) as $tag => $col) { 
     152    foreach (array_flip(self::$fieldmap) as $tag => $col) { 
    150153      foreach ((array)$this->raw[$tag] as $i => $raw) { 
    151154        if (is_array($raw)) { 
    152155          $k = -1; 
    153156          $key = $col; 
    154  
    155           $subtype = $typemap[$raw['type'][++$k]] ? $typemap[$raw['type'][$k]] : strtolower($raw['type'][$k]); 
    156           while ($k < count($raw['type']) && ($subtype == 'internet' || $subtype == 'pref')) 
     157          $subtype = ''; 
     158 
     159          if (!empty($raw['type'])) { 
    157160            $subtype = $typemap[$raw['type'][++$k]] ? $typemap[$raw['type'][$k]] : strtolower($raw['type'][$k]); 
     161            while ($k < count($raw['type']) && ($subtype == 'internet' || $subtype == 'pref')) 
     162              $subtype = $typemap[$raw['type'][++$k]] ? $typemap[$raw['type'][$k]] : strtolower($raw['type'][$k]); 
     163          } 
    158164 
    159165          // read vcard 2.1 subtype 
     
    168174 
    169175          // force subtype if none set 
    170           if (preg_match('/^(email|phone|address|website)/', $key) && !$subtype) 
     176          if (!$subtype && preg_match('/^(email|phone|address|website)/', $key)) 
    171177            $subtype = 'other'; 
    172178 
     
    221227  { 
    222228    if (!$fields) 
    223       $fields = array_merge(array_values($this->fieldmap), array_keys($this->immap), array('FN','N','ORG','NICKNAME','EMAIL','ADR','BDAY')); 
     229      $fields = array_merge(array_values(self::$fieldmap), array_keys($this->immap), array('FN','N','ORG','NICKNAME','EMAIL','ADR','BDAY')); 
    224230 
    225231    foreach ($fields as $f) 
     
    324330          $type = $this->phonetypemap[$type]; 
    325331 
    326         if (($tag = $this->fieldmap[$field]) && (is_array($value) || strlen($value))) { 
     332        if (($tag = self::$fieldmap[$field]) && (is_array($value) || strlen($value))) { 
    327333          $index = count($this->raw[$tag]); 
    328334          $this->raw[$tag][$index] = (array)$value; 
     
    391397 
    392398  /** 
     399   * Extends fieldmap definition 
     400   */ 
     401  public function extend_fieldmap($map) 
     402  { 
     403    if (is_array($map)) 
     404      self::$fieldmap = array_merge($map, self::$fieldmap); 
     405  } 
     406 
     407 
     408  /** 
    393409   * Factory method to import a vcard file 
    394410   * 
     
    421437      if (preg_match('/^END:VCARD$/i', $line)) { 
    422438        // parse vcard 
    423         $obj = new rcube_vcard(self::cleanup($vcard_block), $charset, true); 
     439        $obj = new rcube_vcard(self::cleanup($vcard_block), $charset, true, self::$fieldmap); 
    424440        if (!empty($obj->displayname) || !empty($obj->email)) 
    425441          $out[] = $obj; 
     
    621637        } 
    622638 
     639        // skip empty entries 
     640        if (self::is_empty($value)) 
     641          continue; 
     642 
    623643        $vcard .= self::vcard_quote($type) . $attr . ':' . self::vcard_quote($value) . "\n"; 
    624644      } 
     
    669689      return strtr($s, array("\r" => '', '\\\\' => '\\', '\n' => "\n", '\N' => "\n", '\,' => ',', '\;' => ';', '\:' => ':')); 
    670690    } 
     691  } 
     692 
     693 
     694  /** 
     695   * Check if vCard entry is empty: empty string or an array with 
     696   * all entries empty. 
     697   * 
     698   * @param mixed $value Attribute value (string or array) 
     699   * 
     700   * @return bool True if the value is empty, False otherwise 
     701   */ 
     702  private static function is_empty($value) 
     703  { 
     704    foreach ((array)$value as $v) { 
     705      if (((string)$v) !== '') { 
     706        return false; 
     707      } 
     708    } 
     709 
     710    return true; 
    671711  } 
    672712 
  • trunk/roundcubemail/program/steps/addressbook/export.inc

    r4850 r5010  
    7878    // copy values into vcard object 
    7979    else { 
    80         $vcard = new rcube_vcard($row['vcard']); 
     80        $vcard = new rcube_vcard(); 
     81        $vcard->extend_fieldmap($CONTACTS->vcard_map); 
     82        $vcard->load($row['vcard']); 
    8183        $vcard->reset(); 
    8284 
  • trunk/roundcubemail/program/steps/addressbook/import.inc

    r4890 r5010  
    139139 
    140140  // let rcube_vcard do the hard work :-) 
    141   $vcards = rcube_vcard::import(file_get_contents($_FILES['_file']['tmp_name'])); 
     141  $vcard_o = new rcube_vcard(); 
     142  $vcard_o->extend_fieldmap($CONTACTS->vcard_map); 
     143 
     144  $vcards = $vcard_o->import(file_get_contents($_FILES['_file']['tmp_name'])); 
    142145 
    143146  // no vcards detected 
Note: See TracChangeset for help on using the changeset viewer.