<?php
/**
 * Smarty plugin
 *
 * @category Smarty
 * @license /_licenses/untermStrich/agb_normal.pdf
 * @author Christian Koller
 * @copyright 2009 untermStrich
 * @package Smarty
 * @subpackage plugins
 * @version $Id$
*/

/**
 * Smarty email simple send function plugin
 *
 * Purpose:  Shows a file linke
 *
 * @param   array   $params     Parameters:
 *                              <ul>
 *                                  <li>value   array   Values to show</li>
 *                              </ul>
 * @param   object  &$smarty    The smarty object
 * @return  string  The field source
 */
function smarty_function_emailsimple(array $params, US_Smarty &$smarty)
{
    if (!isset($params['email'])) {
        return 'javascript: return false;';
    }
    
    $CI = CI_Controller::get_instance();

    $CI->load->library('Idna_convert', '', 'idna_convert');
    $CI->load->model('Email_accounts_model', 'email_accounts', true);

    $force_mailto = (isset($params['force_mailto'])) ? $params['force_mailto'] : false;

    if (!$force_mailto && $CI->email_accounts->has_accounts($CI->login->get_user_id(), Email_accounts_model::FILTER_MY_SMTP_RIGHTS, Email_accounts_model::TYPE_SMTP)) {
        if (isset($params['address_id'])) {
            if (isset($params['emails_id'])) {
                return 'javascript:usComm.sendmail_start(\'address\', '.intval($params['address_id']).', '.intval($params['emails_id']).');';
            } else {
                return 'javascript:usComm.sendmail_start(\'address\', '.intval($params['address_id']).');';
            }
        } elseif (isset($params['staff_id'])) {
            return 'javascript:usComm.sendmail_start(\'staff\', '.intval($params['staff_id']).');';
        } else {
            return 'javascript:usComm.sendmail_start(\'staff\', \''.$CI->idna_convert->encode($params['email']).'\');';
        }
    } else {
        return 'mailto:'.$CI->idna_convert->encode($params['email']);
    }
}