<?php
/*
 * Smarty plugin
 * -------------------------------------------------------------
 * File:     function.formatText.php
 * Type:     function
 * Name:     formatText
 * Purpose:  Formatuje tekst - wstawia <br /> za znaki końca linii, przycina, prawdopodobnie będzie wstawiać
 * -------------------------------------------------------------
 */

function smarty_function_formatText($params, &$smarty)
{
	require_once $smarty->_get_plugin_filepath('modifier', 'truncate');

	if (!isset($params['lenght'])) {
		$lenght = CONTENT_MAX_DISP_LEN;
	} else {
		$lenght = $params['lenght'];
		unset($params['lenght']);
	}

	if (!isset($params['etc'])) {
		$etc = '...';
	} else {
		$etc = $params['etc'];
		unset($params['etc']);
	}

	$string = $params['text'];
	unset($params['text']);

	if (!isset($params['moreTitle'])) {
		$wiecejT = "Zobacz więcej";
	} else {
		$wiecejT = $params['moreTitle'];
		unset($params['moreTitle']);
	}

	if (!isset($params['moreName'])) {
		$wiecejN = "&nbsp;";
	} else {
		$wiecejN = $params['moreName'];
		unset($params['moreName']);
	}

	$string = nl2br($string);

	if(isset($params['urlWiecej']))
		$params['moreUrl'] = $params['urlWiecej'];

	if (is_integer($lenght)) {
		if (!isset($params['moreUrl']) && !isset($params['noMoreUrl'])) {

			$moreArr = array();

			foreach ($params as $key => $val) {
				if($key != "limit")
					$moreArr[] = "$key,$val";
			}

			$params['moreUrl'] = implode(',', $moreArr);
			$params['moreUrl'] .= ".html";
		}
		
		$string = smarty_modifier_truncate($string, $lenght, $etc);
	}

	if (isset($params['moreUrl']) && $params['moreUrl']) {
		$more = ' <a class="more" href="' . $params['moreUrl'] . '" title="' . $wiecejT . '" rel="nofollow">'.$wiecejN.'</a>';
	} else {
		$more = '';
	}

	return $string . $more;
}

?>
