New: dol_trunc has an option to make "exact" truncation (without adding

... if truncation is done).
This commit is contained in:
Laurent Destailleur
2011-12-17 16:44:48 +01:00
parent cda132ea8c
commit 1aa75b847b
3 changed files with 134 additions and 103 deletions

View File

@@ -1636,28 +1636,28 @@ function dol_print_graph($htmlid,$width,$height,$data,$showlegend=0,$type='pie',
* MAIN_DISABLE_TRUNC=1 can disable all truncings * MAIN_DISABLE_TRUNC=1 can disable all truncings
* *
* @param string $string String to truncate * @param string $string String to truncate
* @param int $size Max string size. 0 for no limit. * @param int $size Max string size visible. 0 for no limit. finale string size can be 1 more (if size was max+1) or 3 more (if we added ...)
* @param string $trunc Where to trunc: right, left, middle, wrap * @param string $trunc Where to trunc: right, left, middle (size must be a 2 power), wrap
* @param string $stringencoding Tell what is source string encoding * @param string $stringencoding Tell what is source string encoding
* @param int $nodot Truncation do not add ... after truncation. So it's an exact truncation.
* @return string Truncated string * @return string Truncated string
*/ */
function dol_trunc($string,$size=40,$trunc='right',$stringencoding='UTF-8') function dol_trunc($string,$size=40,$trunc='right',$stringencoding='UTF-8',$nodot=0)
{ {
global $conf; global $conf;
if ($size==0) return $string; if ($size==0 || ! empty($conf->global->MAIN_DISABLE_TRUNC)) return $string;
if (empty($conf->global->MAIN_DISABLE_TRUNC))
{
// We go always here // We go always here
if ($trunc == 'right') if ($trunc == 'right')
{ {
$newstring=dol_textishtml($string)?dol_string_nohtmltag($string,1):$string; $newstring=dol_textishtml($string)?dol_string_nohtmltag($string,1):$string;
if (dol_strlen($newstring,$stringencoding) > ($size+1)) if (dol_strlen($newstring,$stringencoding) > ($size+($nodot?0:1)))
return dol_substr($newstring,0,$size,$stringencoding).'...'; return dol_substr($newstring,0,$size,$stringencoding).($nodot?'':'...');
else else
return $string; return $string;
} }
if ($trunc == 'middle') elseif ($trunc == 'middle')
{ {
$newstring=dol_textishtml($string)?dol_string_nohtmltag($string,1):$string; $newstring=dol_textishtml($string)?dol_string_nohtmltag($string,1):$string;
if (dol_strlen($newstring,$stringencoding) > 2 && dol_strlen($newstring,$stringencoding) > ($size+1)) if (dol_strlen($newstring,$stringencoding) > 2 && dol_strlen($newstring,$stringencoding) > ($size+1))
@@ -1669,7 +1669,7 @@ function dol_trunc($string,$size=40,$trunc='right',$stringencoding='UTF-8')
else else
return $string; return $string;
} }
if ($trunc == 'left') elseif ($trunc == 'left')
{ {
$newstring=dol_textishtml($string)?dol_string_nohtmltag($string,1):$string; $newstring=dol_textishtml($string)?dol_string_nohtmltag($string,1):$string;
if (dol_strlen($newstring,$stringencoding) > ($size+1)) if (dol_strlen($newstring,$stringencoding) > ($size+1))
@@ -1677,7 +1677,7 @@ function dol_trunc($string,$size=40,$trunc='right',$stringencoding='UTF-8')
else else
return $string; return $string;
} }
if ($trunc == 'wrap') elseif ($trunc == 'wrap')
{ {
$newstring=dol_textishtml($string)?dol_string_nohtmltag($string,1):$string; $newstring=dol_textishtml($string)?dol_string_nohtmltag($string,1):$string;
if (dol_strlen($newstring,$stringencoding) > ($size+1)) if (dol_strlen($newstring,$stringencoding) > ($size+1))
@@ -1685,11 +1685,7 @@ function dol_trunc($string,$size=40,$trunc='right',$stringencoding='UTF-8')
else else
return $string; return $string;
} }
} else return 'BadParam3CallingDolTrunc';
else
{
return $string;
}
} }

View File

@@ -1480,7 +1480,7 @@ class Product extends CommonObject
for ($j = 0 ; $j < 12 ; $j++) for ($j = 0 ; $j < 12 ; $j++)
{ {
$idx=ucfirst(dol_trunc(dol_print_date(dol_mktime(12,0,0,$month,1,$year),"%b"),3)); $idx=ucfirst(dol_trunc(dol_print_date(dol_mktime(12,0,0,$month,1,$year),"%b"),3,'right','UTF-8',1));
$monthnum=sprintf("%02s",$month); $monthnum=sprintf("%02s",$month);
$result[$j] = array($idx,isset($tab[$year.$month])?$tab[$year.$month]:0); $result[$j] = array($idx,isset($tab[$year.$month])?$tab[$year.$month]:0);

View File

@@ -1,20 +1,20 @@
<?php <?php
/* Copyright (C) 2010 Laurent Destailleur <eldy@users.sourceforge.net> /* Copyright (C) 2010 Laurent Destailleur <eldy@users.sourceforge.net>
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or * the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/ */
/** /**
* \file test/phpunit/SecurityTest.php * \file test/phpunit/SecurityTest.php
@@ -153,6 +153,41 @@ class FunctionsTest extends PHPUnit_Framework_TestCase
return true; return true;
} }
/**
*/
public function testDolTrunc()
{
// Default trunc (will add ... if truncation truncation or keep last char if only one char)
$input="éeéeéeàa";
$after=dol_trunc($input,3);
$this->assertEquals("éeé...",$after);
$after=dol_trunc($input,2);
$this->assertEquals("ée...",$after);
$input="éeé";
$after=dol_trunc($input,3);
$this->assertEquals("éeé",$after);
$after=dol_trunc($input,2);
$this->assertEquals("éeé",$after);
$after=dol_trunc($input,1);
$this->assertEquals("é...",$after);
// Trunc with no ...
$input="éeéeéeàa";
$after=dol_trunc($input,3,'right','UTF-8',1);
$this->assertEquals("éeé",$after);
$after=dol_trunc($input,2,'right','UTF-8',1);
$this->assertEquals("ée",$after);
$input="éeé";
$after=dol_trunc($input,3,'right','UTF-8',1);
$this->assertEquals("éeé",$after);
$after=dol_trunc($input,2,'right','UTF-8',1);
$this->assertEquals("ée",$after);
$after=dol_trunc($input,1,'right','UTF-8',1);
$this->assertEquals("é",$after);
$input="éeéeéeàa";
$after=dol_trunc($input,4,'middle');
$this->assertEquals("ée...àa",$after);
return true;
}
} }
?> ?>