<?php

/**
 * Goweb Sitemanager 5 Framework
 *
 * @version    5.1.3
 * @category   Goweb
 * @package    Goweb_View
 * @subpackage Helper
 * @author     I&D <dev@goweb.pt>, Pedro Pinto <ppinto@goweb.pt>
 * @copyright  2010 (c) Goweb (http://www.goweb.pt)
 */

goweb::loadClass('Zend_View_Helper_Abstract');

/**
 * Helper para truncate de texto
 *
 * @version    5.1.3
 * @category   Goweb
 * @package    Goweb_View
 * @subpackage Helper
 * @author     I&D <dev@goweb.pt>, Pedro Pinto <ppinto@goweb.pt>
 * @copyright  2010 (c) Goweb (http://www.goweb.pt)
 */
class Goweb_View_Helper_Truncate extends Zend_View_Helper_Abstract{
	
function truncate($string, $length, $addstring=""){
	$addstring = " " . $addstring;
	 
	if (strlen($string) > $length) {
	if( !empty( $string ) && $length>0 ) {
	$isText = true;
	$ret = "";
	$i = 0;
	 
	$currentChar = "";
	$lastSpacePosition = -1;
	$lastChar = "";
	 
	$tagsArray = array();
	$currentTag = "";
	$tagLevel = 0;
	 
	$noTagLength = strlen( strip_tags( $string ) );
	 
	// Parser loop
	for( $j=0; $j<strlen( $string ); $j++ ) {
	 
	$currentChar = substr( $string, $j, 1 );
	$ret .= $currentChar;
	 
	// Lesser than event
	if( $currentChar == "<") $isText = false;
	 
	// Character handler
	if( $isText ) {
	 
	// Memorize last space position
	if( $currentChar == " " ) { $lastSpacePosition = $j; }
	else { $lastChar = $currentChar; }
	 
	$i++;
	} else {
	$currentTag .= $currentChar;
	}
	 
	// Greater than event
	if( $currentChar == ">" ) {
	$isText = true;
	 
	// Opening tag handler
	if( ( strpos( $currentTag, "<" ) !== FALSE ) &&
	( strpos( $currentTag, "/>" ) === FALSE ) &&
	( strpos( $currentTag, "</") === FALSE ) ) {
	 
	// Tag has attribute(s)
	if( strpos( $currentTag, " " ) !== FALSE ) {
	$currentTag = substr( $currentTag, 1, strpos( $currentTag, " " ) - 1 );
	} else {
	// Tag doesn't have attribute(s)
	$currentTag = substr( $currentTag, 1, -1 );
	}
	 
	array_push( $tagsArray, $currentTag );
	 
	} else if( strpos( $currentTag, "</" ) !== FALSE ) {
	array_pop( $tagsArray );
	}
	 
	$currentTag = "";
	}
	 
	if( $i >= $length) {
	break;
	}
	}
	 
	// Cut HTML string at last space position
	if( $length < $noTagLength ) {
	if( $lastSpacePosition != -1 ) {
	$ret = substr( $string, 0, $lastSpacePosition );
	} else {
	$ret = substr( $string, $j );
	}
	}
	 
	// Close broken XHTML elements
	while( sizeof( $tagsArray ) != 0 ) {
	$aTag = array_pop( $tagsArray );
	$ret .= "</" . $aTag . ">\n";
	}
	 
	} else {
	$ret = "";
	}
	 
	// only add string if text was cut
	if ( strlen($string) > $length ) {
	return( $ret.$addstring );
	}
	else {
	return ( $res );
	}
	}
	else {
	return ( $string );
	}
	}
}