forked from Wavyzz/dolibarr
New: dol_trunc has an option to make "exact" truncation (without adding
... if truncation is done).
This commit is contained in:
@@ -1636,28 +1636,28 @@ function dol_print_graph($htmlid,$width,$height,$data,$showlegend=0,$type='pie',
|
||||
* MAIN_DISABLE_TRUNC=1 can disable all truncings
|
||||
*
|
||||
* @param string $string String to truncate
|
||||
* @param int $size Max string size. 0 for no limit.
|
||||
* @param string $trunc Where to trunc: right, left, middle, wrap
|
||||
* @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 (size must be a 2 power), wrap
|
||||
* @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
|
||||
*/
|
||||
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;
|
||||
|
||||
if ($size==0) return $string;
|
||||
if (empty($conf->global->MAIN_DISABLE_TRUNC))
|
||||
{
|
||||
if ($size==0 || ! empty($conf->global->MAIN_DISABLE_TRUNC)) return $string;
|
||||
|
||||
// We go always here
|
||||
if ($trunc == 'right')
|
||||
{
|
||||
$newstring=dol_textishtml($string)?dol_string_nohtmltag($string,1):$string;
|
||||
if (dol_strlen($newstring,$stringencoding) > ($size+1))
|
||||
return dol_substr($newstring,0,$size,$stringencoding).'...';
|
||||
if (dol_strlen($newstring,$stringencoding) > ($size+($nodot?0:1)))
|
||||
return dol_substr($newstring,0,$size,$stringencoding).($nodot?'':'...');
|
||||
else
|
||||
return $string;
|
||||
}
|
||||
if ($trunc == 'middle')
|
||||
elseif ($trunc == 'middle')
|
||||
{
|
||||
$newstring=dol_textishtml($string)?dol_string_nohtmltag($string,1):$string;
|
||||
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
|
||||
return $string;
|
||||
}
|
||||
if ($trunc == 'left')
|
||||
elseif ($trunc == 'left')
|
||||
{
|
||||
$newstring=dol_textishtml($string)?dol_string_nohtmltag($string,1):$string;
|
||||
if (dol_strlen($newstring,$stringencoding) > ($size+1))
|
||||
@@ -1677,7 +1677,7 @@ function dol_trunc($string,$size=40,$trunc='right',$stringencoding='UTF-8')
|
||||
else
|
||||
return $string;
|
||||
}
|
||||
if ($trunc == 'wrap')
|
||||
elseif ($trunc == 'wrap')
|
||||
{
|
||||
$newstring=dol_textishtml($string)?dol_string_nohtmltag($string,1):$string;
|
||||
if (dol_strlen($newstring,$stringencoding) > ($size+1))
|
||||
@@ -1685,11 +1685,7 @@ function dol_trunc($string,$size=40,$trunc='right',$stringencoding='UTF-8')
|
||||
else
|
||||
return $string;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return $string;
|
||||
}
|
||||
else return 'BadParam3CallingDolTrunc';
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1480,7 +1480,7 @@ class Product extends CommonObject
|
||||
|
||||
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);
|
||||
|
||||
$result[$j] = array($idx,isset($tab[$year.$month])?$tab[$year.$month]:0);
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
<?php
|
||||
/* Copyright (C) 2010 Laurent Destailleur <eldy@users.sourceforge.net>
|
||||
*
|
||||
* 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
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
*/
|
||||
* 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
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
*/
|
||||
|
||||
/**
|
||||
* \file test/phpunit/SecurityTest.php
|
||||
@@ -153,6 +153,41 @@ class FunctionsTest extends PHPUnit_Framework_TestCase
|
||||
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;
|
||||
}
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user