<?php

function smarty_cms_function_vcard($params,&$smarty)
{
  $encode = TRUE;

  $vars = array();
  $vars['adrtype'] = 'pref';
  $vars['teltype'] = 'pref';
  $vars['rev'] = date('YmdTHMSZ');
  $address = array();
  $address['pobox'] = '';
  $address['extended'] = '';
  $address['street'] = '';
  $address['city'] = '';
  $address['state'] = '';
  $address['zip'] = '';
  $address['country'] = '';
  foreach( $params as $key => $value )
  {
    switch( $key )
      {
      case 'firstname':
      case 'lastname':
      case 'company':
      case 'position':
      case 'email':
      case 'url':
      case 'tel':
      case 'teltype':
      case 'adrtype':
      case 'label':
      case 'rev':
	$vars[$key] = trim($value);
	break;
	
      case 'pobox':
      case 'extended':
      case 'street':
      case 'city':
      case 'state':
      case 'zip':
      case 'country':
	$address[$key] = trim($value);
	break;

      case 'adr':
	if( !isset($vars['label']) )
	  $vars['label'] = trim($value);
	$vars[$key] = trim($value);
	break;

      case 'assign':
	// do nothing
	break;

      case 'noencode':
	$encode = FALSE;
	break;
      }
  }

  $tpl = <<<EOT
BEGIN:VCARD
N:{\$vars.lastname}, {\$vars.firstname}
FN:{\$vars.firstname} {\$vars.lastname}
{if isset(\$vars.company)}ORG:{\$vars.company}__NL__{/if}
{if isset(\$vars.position)}TITLE:{\$vars.position}__NL__{/if}
{if isset(\$vars.tel)}TEL;TYPE={\$vars.teltype}:{\$vars.tel}__NL__{/if}
{if isset(\$vars.adr)}ADR;TYPE={\$vars.adrtype}:{\$vars.adr}__NL__{/if}
{if isset(\$vars.label)}LABEL;TYPE={\$vars.adrtype}:{\$vars.label}__NL__{/if}
{if isset(\$vars.email)}EMAIL;TYPE=PREF.INTERNET:{\$vars.email}__NL__{/if}
{if isset(\$vars.url)}URL:{\$vars.url}__NL__{/if}
REV:{\$vars.rev}
END:VCARD
EOT;

  if( !isset($vars['adr']) )
    {
      $fnd = false;
      foreach( $address as $key => $value )
	{
	  if( $value != '' )
	    {
	      $fnd = true;
	      break;
	    }
	}
      if( $fnd )
	{
	  // address specified
	  $vars['adr'] = "{$address['pobox']};{$address['extended']};{$address['street']};{$address['city']};{$address['state']};{$address['zip']};{$address['country']}";
	  if( !isset($vars['label']) )
	    {
	      // build the label
$atpl = <<<EOT
{\$address.pobox|default:''}__NL__
{\$address.extended|default:''}__NL__
{\$address.street|default:''}__NL__
{if \$address.city != ''}{\$address.city}, {/if}{\$address.state|default:''} {\$address.zip|default:''}__NL__
{\$address.country|default:''}
EOT;

	      $smarty->assign('address',$address);
	      $smarty->_compile_source('address tmp',$atpl,$_compiled);
	      @ob_start();
	      $smarty->_eval('?>'.$_compiled);
	      $_label = @ob_get_contents();
	      @ob_end_clean();

              $_label = str_replace('__NL__',"\n",$_label);
              $_label = str_replace("\n",'\n',$_label);
              $_label = str_replace('\n\n','\n',$_label);
              $_label = str_replace('\n\n','\n',$_label);

	      $vars['label'] = $_label;
	    }
	}
    }

  $smarty->assign('vars',$vars);
  $smarty->_compile_source('address plugin',$tpl,$_compiled);
  @ob_start();
  $smarty->_eval('?>'.$_compiled);
  $_contents = @ob_get_contents();
  @ob_end_clean();

  $_contents = str_replace('__NL__',"\n",$_contents);
  $_contents = str_replace("\n\n","\n",$_contents);
 
  if( $encode ) $_contents = rawurlencode($_contents);

  if( isset($params['assign']) )
    {
      $smarty->assign(trim($params['assign']),$_contents);
      return;
    }
  return $_contents;
}


function smarty_cms_help_function_vcard()
{
  $txt = <<<EOT
<h3>What Does This Do?</h3>
<p>This is a simple plugin capable of generating a formatted, or encoded vcard (V3) for use on websites.</p>
<h3>Features and Limitations:</h3>
  <ul>
     <li>Supports a single address or telephone number.</li>
     <li>Automatically builds the ADR tag and LABEL Tag.</li>
     <li>Supports specifying the adress and/or telephone type.</li>
     <li>Automatically builds the FN tag.</li>
     <li>Optional URL encoding.</li>
  </ul>
<h3>How Do I Use It?</h3>
<h4>A downloadable vcard</h4>
<ul>
   <li>Create a simple page template with just {content}</li>
   <li>Create a new page using the template created above.  Disable the wysiwyg for this page.  Set the alias for ths page to &quot;myvcard&quot;</li>
   <li>In the page content of the new page add:
  <pre><code>{vcard noencode=1 firstname=John lastname=Doe company='Acme Corporation' position='grunt worker' email='jdoe@acme.org' url='http://www.acme.org/people/jdoe' tel='1 (000) 5551212'}</code></pre>
   </li>
   <li>In other pages, or page templates, use {cms_selflink} to create a link to your vcard.  Something like: {cms_selflink page=myvcard text='My Vcard'}</li>
</ul>
<h4>A vcard String</h4>
  <p>Vcards can be generated as a string for use in supplying to another service (such as posting a vcard to an address registry, or generating a qrcode). This is similar to the above procedure except the noencode=1 parameter is removed.  You may also add the assign parameter.</p>
  <pre><code>{vcard firstname=John lastname=Doe company='Acme Corporation' position='grunt worker' email='jdoe@acme.org' url='http://www.acme.org/people/jdoe' tel='1 (000) 5551212'}</code></pre>
<h3>Parameters:</h3>
<ul>
 <li>firstname : <em>(required)</em> The persons given name.</li>
 <li>lastname : <em>(required)</em> The persons family name.</li>
 <li>company : <em>(optional)</em> An organizational name.</li>
 <li>position : <em>(optional)</em> An optional position within the organization.</li>
 <li>email : <em>(optional)</em> An email address.</li>
 <li>url : <em>(optional)</em> A website address.</li>
 <li>tel : <em>(optional)</em> Telephone number.</li>
 <li>teltype : <em>(optional)</em> - Telephone type (i.e: home,work,mobile) default: pref</li>
 <li>adrtype : <em>(optional)</em> - Address type (i.e: home,work,mobile) default: pref</li>
 <li>label : <em>(optional) - Human readable address.  If not specified the adr field is used.</li>
 <li>rev : <em>(optional) - Revision number.  If not specified the current date and time are used.</li>
 <li>pobox : <em>(optional) - Post Office Box portion of the address.  This parameter is ignored if adr is specified.</li>
 <li>extended : <em>(optional) - Extended portion of the address.  This parameter is ignored if adr is specified.</li>
 <li>street : <em>(optional) - Street portion of the address.  This parameter is ignored if adr is specified.</li>
 <li>city : <em>(optional) - City portion of the address.  This parameter is ignored if adr is specified.</li>
 <li>state : <em>(optional) - State portion of the address.  This parameter is ignored if adr is specified.</li>
 <li>zip : <em>(optional) - Postal code portion of the address.  This parameter is ignored if adr is specified.</li>
 <li>country : <em>(optional) - Country name portion of the address.  This parameter is ignored if adr is specified.</li>
 <li>adr : <em>(optional) - The ability to override the complete address string.  This is a formatted string with the various addesss portions specified in order and separated with a semicolon.  If it is not specified, the adr field is built from the various other parameters.</li>
 <li>assign : <em>(optional)</em> - Assign the output of the tag to the named smarty variable instead of to the page.</li>
 <li>noencode : <em>(optional)</em> - If specified, and a nonzero value the output will not be url encoded.</li>
</ul>
EOT;

  echo $txt;
}

function smarty_cms_about_function_vcard()
{
  ?>
  <p>Author: Robert Campbell &lt;calguy1000@gmail.com&gt;</p>
  <p>Version: 1.0</p>
  <p>Change History:
   <ul>
      <li>1.0 - Initial Version.</li>
   </ul>
  </p>
  <?php
}

?>