[ Index ]
 

Code source de Horde 3.1.3

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/Horde/MIME/Viewer/ -> vcard.php (source)

   1  <?php
   2  /**
   3   * The MIME_Viewer_vcard class renders out vCards in HTML format.
   4   *
   5   * $Horde: framework/MIME/MIME/Viewer/vcard.php,v 1.34.10.7 2006/01/01 21:28:25 jan Exp $
   6   *
   7   * Copyright 2002-2006 Jan Schneider <jan@horde.org>
   8   *
   9   * See the enclosed file COPYING for license information (LGPL). If you
  10   * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
  11   *
  12   * @author  Jan Schneider <jan@horde.org>
  13   * @since   Horde 2.0
  14   * @package Horde_MIME_Viewer
  15   */
  16  class MIME_Viewer_vcard extends MIME_Viewer {
  17  
  18      /**
  19       * Render out the vcard contents.
  20       *
  21       * @param array $params  Any parameters the Viewer may need.
  22       *
  23       * @return string  The rendered contents.
  24       */
  25      function render($params = null)
  26      {
  27          global $registry, $prefs;
  28  
  29          require_once 'Horde/iCalendar.php';
  30  
  31          $app = false;
  32          $data = $this->mime_part->getContents();
  33          $html = '';
  34          $import_msg = null;
  35          $title = _("vCard");
  36  
  37          $iCal = &new Horde_iCalendar();
  38          if (!$iCal->parsevCalendar($data)) {
  39              $import_msg = _("There was an error importing the contact data.");
  40          }
  41  
  42          if (Util::getFormData('import') &&
  43              Util::getFormData('source') &&
  44              $registry->hasMethod('contacts/import')) {
  45              $source = Util::getFormData('source');
  46              $contacts = $registry->call('contacts/import', array($data, 'text/x-vcard', $source));
  47              if (is_a($contacts, 'PEAR_Error')) {
  48                  $import_msg = _("There was an error importing the contact data.");
  49              } else {
  50                  if ($iCal->getComponentCount() == 1) {
  51                      $import_msg = _("The contact was successfully added to your address book.");
  52                  } else {
  53                      $import_msg = _("Contacts were successfully added to your address book.");
  54                  }
  55              }
  56          }
  57  
  58          $html  = Util::bufferOutput('include', $registry->get('templates', 'horde') . '/common-header.inc');
  59          $html .= '<table cellspacing="1" border="0" cellpadding="1">';
  60          if (!is_null($import_msg)) {
  61              $html .= '<tr><td colspan="2" class="header">' . $import_msg . '</td></tr><tr><td>&nbsp;</td></tr>';
  62          } elseif ($registry->hasMethod('contacts/import') &&
  63                    $registry->hasMethod('contacts/sources')) {
  64              $html .= '<tr><td colspan="2" class="header"><form action="' . $_SERVER['PHP_SELF'] . '" method="get" name="vcard_import">' . Util::formInput();
  65              foreach ($_GET as $key => $val) {
  66                  $html .= '<input type="hidden" name="' . htmlspecialchars($key) . '" value="' . htmlspecialchars($val) . '" />';
  67              }
  68  
  69              $sources = $registry->call('contacts/sources', array(true));
  70              if (count($sources) > 1) {
  71                  $html .= '<input type="submit" class="button" name="import" value="' . _("Add to address book:") . '" />';
  72                  $html .= '<select name="source">';
  73                  foreach ($sources as $key => $label) {
  74                      $selected = ($key == $prefs->getValue('add_source')) ? ' selected="selected"' : '';
  75                      $html .= '<option value="' . htmlspecialchars($key) . '"' . $selected . '>' . htmlspecialchars($label) . '</option>';
  76                  }
  77              } else {
  78                  $html .= '<input type="submit" class="button" name="import" value="' . _("Add to my address book") . '" />';
  79                  $source = array_keys($sources);
  80                  $html .= '<input type="hidden" name="source" value="' . htmlspecialchars($source[0]) . '" />';
  81              }
  82  
  83              $html .= '</form></td></tr><tr><td>&nbsp;</td></tr>';
  84          }
  85  
  86          $i = 0;
  87          foreach ($iCal->getComponents() as $vc) {
  88              if ($i > 0) {
  89                  $html .= '<tr><td>&nbsp;</td></tr>';
  90              }
  91              ++$i;
  92  
  93              $html .= '<tr><td colspan="2" class="header">';
  94              $fullname = $vc->getAttributeDefault('FN', false);
  95              if ($fullname !== false) {
  96                  $html .= $fullname;
  97              }
  98              $html .= '</td></tr>';
  99  
 100              $n = $vc->printableName();
 101              if (!empty($n)) {
 102                  $html .= $this->_row(_("Name"), $n);
 103              }
 104  
 105              $aliases = $vc->getAttributeValues('ALIAS');
 106              if (!is_a($aliases, 'PEAR_Error')) {
 107                  $html .= $this->_row(_("Alias"), implode('<br />', $aliases));
 108              }
 109              $birthdays = $vc->getAttributeValues('BDAY');
 110              if (!is_a($birthdays, 'PEAR_Error')) {
 111                  $html .= $this->_row(_("Birthday"), date('Y-m-d', $birthdays[0]));
 112              }
 113  
 114              $labels = $vc->getAllAttributes('LABEL');
 115              foreach ($labels as $label) {
 116                  if (isset($label['params']['TYPE'])) {
 117                      foreach ($label['params']['TYPE'] as $type) {
 118                          $label['params'][String::upper($type)] = true;
 119                      }
 120                  }
 121                  if (isset($label['params']['HOME'])) {
 122                      $html .= $this->_row(_("Home Address"), nl2br($label['value']));
 123                  } elseif (isset($label['params']['WORK'])) {
 124                      $html .= $this->_row(_("Work Address"), nl2br($label['value']));
 125                  } else {
 126                      $html .= $this->_row(_("Address"), nl2br($label['value']));
 127                  }
 128              }
 129  
 130              // For vCard 3.0.
 131              $adrs = $vc->getAllAttributes('ADR');
 132              foreach ($adrs as $item) {
 133                  if (isset($item['params']['TYPE'])) {
 134                      if (!is_array($item['params']['TYPE'])) {
 135                          $item['params']['TYPE'] = array($item['params']['TYPE']);
 136                      }
 137                  } else {
 138                      $item['params']['TYPE'] = array();
 139                      if (isset($item['params']['WORK'])) {
 140                          $item['params']['TYPE'][] = 'WORK';
 141                      }
 142                      if (isset($item['params']['HOME'])) {
 143                          $item['params']['TYPE'][] = 'HOME';
 144                      }
 145                  }
 146                  $address = $item['values'];
 147                  foreach ($item['params']['TYPE'] as $adr) {
 148                      if (String::upper($adr) == 'HOME') {
 149                          $a = array();
 150                          if (isset($address[VCARD_ADR_STREET]))   { $a[] = $address[VCARD_ADR_STREET]; }
 151                          if (isset($address[VCARD_ADR_LOCALITY])) { $a[] = $address[VCARD_ADR_LOCALITY]; }
 152                          if (isset($address[VCARD_ADR_REGION]))   { $a[] = $address[VCARD_ADR_REGION]; }
 153                          if (isset($address[VCARD_ADR_POSTCODE])) { $a[] = $address[VCARD_ADR_POSTCODE]; }
 154                          if (isset($address[VCARD_ADR_COUNTRY]))  { $a[] = $address[VCARD_ADR_COUNTRY]; }
 155                          $html .= $this->_row(_("Home Address"), nl2br(implode("\n", $a)));
 156  
 157                      } elseif (String::upper($adr) == 'WORK') {
 158                          $a = array();
 159                          if (isset($address[VCARD_ADR_STREET]))   { $a[] = $address[VCARD_ADR_STREET]; }
 160                          if (isset($address[VCARD_ADR_LOCALITY])) { $a[] = $address[VCARD_ADR_LOCALITY]; }
 161                          if (isset($address[VCARD_ADR_REGION]))   { $a[] = $address[VCARD_ADR_REGION]; }
 162                          if (isset($address[VCARD_ADR_POSTCODE])) { $a[] = $address[VCARD_ADR_POSTCODE]; }
 163                          if (isset($address[VCARD_ADR_COUNTRY]))  { $a[] = $address[VCARD_ADR_COUNTRY]; }
 164                          $html .= $this->_row(_("Work Address"), nl2br(implode("\n", $a)));
 165  
 166                      } else {
 167                          $a = array();
 168                          if (isset($address[VCARD_ADR_STREET]))   { $a[] = $address[VCARD_ADR_STREET]; }
 169                          if (isset($address[VCARD_ADR_LOCALITY])) { $a[] = $address[VCARD_ADR_LOCALITY]; }
 170                          if (isset($address[VCARD_ADR_REGION]))   { $a[] = $address[VCARD_ADR_REGION]; }
 171                          if (isset($address[VCARD_ADR_POSTCODE])) { $a[] = $address[VCARD_ADR_POSTCODE]; }
 172                          if (isset($address[VCARD_ADR_COUNTRY]))  { $a[] = $address[VCARD_ADR_COUNTRY]; }
 173                          $html .= $this->_row(_("Address"), nl2br(implode("\n", $a)));
 174                      }
 175                  }
 176              }
 177  
 178              $numbers = $vc->getAllAttributes('TEL');
 179  
 180              foreach ($numbers as $number) {
 181                  if (isset($number['params']['TYPE'])) {
 182                      foreach ($number['params']['TYPE'] as $type) {
 183                          $number['params'][String::upper($type)] = true;
 184                      }
 185                  }
 186                  if (isset($number['params']['FAX'])) {
 187                      $html .= $this->_row(_("Fax"), $number['value']);
 188                  } else {
 189                      if (isset($number['params']['HOME'])) {
 190                          $html .= $this->_row(_("Home Phone"), $number['value']);
 191                      } elseif (isset($number['params']['WORK'])) {
 192                          $html .= $this->_row(_("Work Phone"), $number['value']);
 193                      } elseif (isset($number['params']['CELL'])) {
 194                          $html .= $this->_row(_("Cell Phone"), $number['value']);
 195                      } else {
 196                          $html .= $this->_row(_("Phone"), $number['value']);
 197                      }
 198                  }
 199              }
 200  
 201              $addresses = $vc->getAllAttributes('EMAIL');
 202              $emails = array();
 203              foreach ($addresses as $address) {
 204                  if (isset($address['params']['TYPE'])) {
 205                      foreach ($address['params']['TYPE'] as $type) {
 206                          $address['params'][String::upper($type)] = true;
 207                      }
 208                  }
 209                  $email = '<a href="';
 210                  if ($registry->hasMethod('mail/compose')) {
 211                      $email .= $registry->call('mail/compose', array(array('to' => $address['value'])));
 212                  } else {
 213                      $email .= 'mailto:' . $address['value'];
 214                  }
 215                  $email .= '">' . $address['value'] . '</a>';
 216                  if (isset($address['params']['PREF'])) {
 217                      array_unshift($emails, $email);
 218                  } else {
 219                      $emails[] = $email;
 220                  }
 221              }
 222  
 223              if (count($emails)) {
 224                  $html .= $this->_row(_("Email"), implode('<br />', $emails));
 225              }
 226  
 227              $title = $vc->getAttributeValues('TITLE');
 228              if (!is_a($title, 'PEAR_Error')) {
 229                  $html .= $this->_row(_("Title"), $title[0]);
 230              }
 231  
 232              $role = $vc->getAttributeValues('ROLE');
 233              if (!is_a($role, 'PEAR_Error')) {
 234                  $html .= $this->_row(_("Role"), $role[0]);
 235              }
 236  
 237              $org = $vc->getAttributeValues('ORG');
 238              if (!is_a($org, 'PEAR_Error')) {
 239                  $html .= $this->_row(_("Company"), $org[0]);
 240              }
 241  
 242              $notes = $vc->getAttributeValues('NOTE');
 243              if (!is_a($notes, 'PEAR_Error')) {
 244                  $html .= $this->_row(_("Notes"), nl2br($notes[0]));
 245              }
 246  
 247              $url = $vc->getAttributeValues('URL');
 248              if (!is_a($url, 'PEAR_Error')) {
 249                  $html .= $this->_row(_("URL"), '<a href="' . $url[0]. '" target="_blank">' . $url[0] . '</a>');
 250              }
 251          }
 252  
 253          $html .= '</table>';
 254          $html .= Util::bufferOutput('include', $registry->get('templates', 'horde') . '/common-footer.inc');
 255  
 256          return $html;
 257      }
 258  
 259      function _row($label, $value)
 260      {
 261          return '<tr><td class="item" valign="top">' . $label . '</td><td class="item" valign="top">' . $value . "</td></tr>\n";
 262      }
 263  
 264      /**
 265       * Return the MIME content type of the rendered content.
 266       *
 267       * @return string  The content type of the output.
 268       */
 269      function getType()
 270      {
 271          return 'text/html; charset=' . NLS::getCharset();
 272      }
 273  
 274  }


Généré le : Sun Feb 25 18:01:28 2007 par Balluche grâce à PHPXref 0.7