[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/components/com_contact/src/View/Contact/ -> VcfView.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Site
   5   * @subpackage  com_contact
   6   *
   7   * @copyright   (C) 2010 Open Source Matters, Inc. <https://www.joomla.org>
   8   * @license     GNU General Public License version 2 or later; see LICENSE.txt
   9   */
  10  
  11  namespace Joomla\Component\Contact\Site\View\Contact;
  12  
  13  use Joomla\CMS\Factory;
  14  use Joomla\CMS\MVC\View\AbstractView;
  15  use Joomla\CMS\MVC\View\GenericDataException;
  16  
  17  // phpcs:disable PSR1.Files.SideEffects
  18  \defined('_JEXEC') or die;
  19  // phpcs:enable PSR1.Files.SideEffects
  20  
  21  /**
  22   * View to create a VCF for a contact item
  23   *
  24   * @since  1.6
  25   */
  26  class VcfView extends AbstractView
  27  {
  28      /**
  29       * The contact item
  30       *
  31       * @var   \Joomla\CMS\Object\CMSObject
  32       */
  33      protected $item;
  34  
  35      /**
  36       * Execute and display a template script.
  37       *
  38       * @param   string  $tpl  The name of the template file to parse; automatically searches through the template paths.
  39       *
  40       * @return  string  A string if successful
  41       *
  42       * @throws  GenericDataException
  43       */
  44      public function display($tpl = null)
  45      {
  46          // Get model data.
  47          $item = $this->get('Item');
  48  
  49          // Check for errors.
  50          if (count($errors = $this->get('Errors'))) {
  51              throw new GenericDataException(implode("\n", $errors), 500);
  52          }
  53  
  54          $this->document->setMimeEncoding('text/directory', true);
  55  
  56          // Compute lastname, firstname and middlename
  57          $item->name = trim($item->name);
  58  
  59          // "Lastname, Firstname Middlename" format support
  60          // e.g. "de Gaulle, Charles"
  61          $namearray = explode(',', $item->name);
  62  
  63          if (count($namearray) > 1) {
  64              $lastname = $namearray[0];
  65              $card_name = $lastname;
  66              $name_and_midname = trim($namearray[1]);
  67  
  68              $firstname = '';
  69  
  70              if (!empty($name_and_midname)) {
  71                  $namearray = explode(' ', $name_and_midname);
  72  
  73                  $firstname = $namearray[0];
  74                  $middlename = (count($namearray) > 1) ? $namearray[1] : '';
  75                  $card_name = $firstname . ' ' . ($middlename ? $middlename . ' ' : '') . $card_name;
  76              }
  77          } else {
  78              // "Firstname Middlename Lastname" format support
  79              $namearray = explode(' ', $item->name);
  80  
  81              $middlename = (count($namearray) > 2) ? $namearray[1] : '';
  82              $firstname = array_shift($namearray);
  83              $lastname = count($namearray) ? end($namearray) : '';
  84              $card_name = $firstname . ($middlename ? ' ' . $middlename : '') . ($lastname ? ' ' . $lastname : '');
  85          }
  86  
  87          $rev = date('c', strtotime($item->modified));
  88  
  89          Factory::getApplication()->setHeader('Content-disposition', 'attachment; filename="' . $card_name . '.vcf"', true);
  90  
  91          $vcard = [];
  92          $vcard[] .= 'BEGIN:VCARD';
  93          $vcard[] .= 'VERSION:3.0';
  94          $vcard[]  = 'N:' . $lastname . ';' . $firstname . ';' . $middlename;
  95          $vcard[]  = 'FN:' . $item->name;
  96          $vcard[]  = 'TITLE:' . $item->con_position;
  97          $vcard[]  = 'TEL;TYPE=WORK,VOICE:' . $item->telephone;
  98          $vcard[]  = 'TEL;TYPE=WORK,FAX:' . $item->fax;
  99          $vcard[]  = 'TEL;TYPE=WORK,MOBILE:' . $item->mobile;
 100          $vcard[]  = 'ADR;TYPE=WORK:;;' . $item->address . ';' . $item->suburb . ';' . $item->state . ';' . $item->postcode . ';' . $item->country;
 101          $vcard[]  = 'LABEL;TYPE=WORK:' . $item->address . "\n" . $item->suburb . "\n" . $item->state . "\n" . $item->postcode . "\n" . $item->country;
 102          $vcard[]  = 'EMAIL;TYPE=PREF,INTERNET:' . $item->email_to;
 103          $vcard[]  = 'URL:' . $item->webpage;
 104          $vcard[]  = 'REV:' . $rev . 'Z';
 105          $vcard[]  = 'END:VCARD';
 106  
 107          echo implode("\n", $vcard);
 108      }
 109  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer