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,60 +1636,56 @@ 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))
|
||||
{
|
||||
// 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).'...';
|
||||
else
|
||||
return $string;
|
||||
}
|
||||
if ($trunc == 'middle')
|
||||
{
|
||||
$newstring=dol_textishtml($string)?dol_string_nohtmltag($string,1):$string;
|
||||
if (dol_strlen($newstring,$stringencoding) > 2 && dol_strlen($newstring,$stringencoding) > ($size+1))
|
||||
{
|
||||
$size1=round($size/2);
|
||||
$size2=round($size/2);
|
||||
return dol_substr($newstring,0,$size1,$stringencoding).'...'.dol_substr($newstring,dol_strlen($newstring,$stringencoding) - $size2,$size2,$stringencoding);
|
||||
}
|
||||
else
|
||||
return $string;
|
||||
}
|
||||
if ($trunc == 'left')
|
||||
{
|
||||
$newstring=dol_textishtml($string)?dol_string_nohtmltag($string,1):$string;
|
||||
if (dol_strlen($newstring,$stringencoding) > ($size+1))
|
||||
return '...'.dol_substr($newstring,dol_strlen($newstring,$stringencoding) - $size,$size,$stringencoding);
|
||||
else
|
||||
return $string;
|
||||
}
|
||||
if ($trunc == 'wrap')
|
||||
{
|
||||
$newstring=dol_textishtml($string)?dol_string_nohtmltag($string,1):$string;
|
||||
if (dol_strlen($newstring,$stringencoding) > ($size+1))
|
||||
return dol_substr($newstring,0,$size,$stringencoding)."\n".dol_trunc(dol_substr($newstring,$size,dol_strlen($newstring,$stringencoding)-$size,$stringencoding),$size,$trunc);
|
||||
else
|
||||
return $string;
|
||||
}
|
||||
}
|
||||
else
|
||||
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+($nodot?0:1)))
|
||||
return dol_substr($newstring,0,$size,$stringencoding).($nodot?'':'...');
|
||||
else
|
||||
return $string;
|
||||
}
|
||||
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))
|
||||
{
|
||||
$size1=round($size/2);
|
||||
$size2=round($size/2);
|
||||
return dol_substr($newstring,0,$size1,$stringencoding).'...'.dol_substr($newstring,dol_strlen($newstring,$stringencoding) - $size2,$size2,$stringencoding);
|
||||
}
|
||||
else
|
||||
return $string;
|
||||
}
|
||||
elseif ($trunc == 'left')
|
||||
{
|
||||
$newstring=dol_textishtml($string)?dol_string_nohtmltag($string,1):$string;
|
||||
if (dol_strlen($newstring,$stringencoding) > ($size+1))
|
||||
return '...'.dol_substr($newstring,dol_strlen($newstring,$stringencoding) - $size,$size,$stringencoding);
|
||||
else
|
||||
return $string;
|
||||
}
|
||||
elseif ($trunc == 'wrap')
|
||||
{
|
||||
$newstring=dol_textishtml($string)?dol_string_nohtmltag($string,1):$string;
|
||||
if (dol_strlen($newstring,$stringencoding) > ($size+1))
|
||||
return dol_substr($newstring,0,$size,$stringencoding)."\n".dol_trunc(dol_substr($newstring,$size,dol_strlen($newstring,$stringencoding)-$size,$stringencoding),$size,$trunc);
|
||||
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
|
||||
@@ -49,64 +49,64 @@ if (! defined("NOLOGIN")) define("NOLOGIN",'1'); // If this page is
|
||||
*/
|
||||
class FunctionsTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
protected $savconf;
|
||||
protected $savuser;
|
||||
protected $savlangs;
|
||||
protected $savdb;
|
||||
protected $savconf;
|
||||
protected $savuser;
|
||||
protected $savlangs;
|
||||
protected $savdb;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* We save global variables into local variables
|
||||
*
|
||||
* @return CoreTest
|
||||
*/
|
||||
function FunctionsTest()
|
||||
{
|
||||
//$this->sharedFixture
|
||||
global $conf,$user,$langs,$db;
|
||||
$this->savconf=$conf;
|
||||
$this->savuser=$user;
|
||||
$this->savlangs=$langs;
|
||||
$this->savdb=$db;
|
||||
|
||||
print __METHOD__." db->type=".$db->type." user->id=".$user->id;
|
||||
//print " - db ".$db->db;
|
||||
print "\n";
|
||||
}
|
||||
|
||||
// Static methods
|
||||
public static function setUpBeforeClass()
|
||||
/**
|
||||
* Constructor
|
||||
* We save global variables into local variables
|
||||
*
|
||||
* @return CoreTest
|
||||
*/
|
||||
function FunctionsTest()
|
||||
{
|
||||
global $conf,$user,$langs,$db;
|
||||
//$db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
|
||||
//$this->sharedFixture
|
||||
global $conf,$user,$langs,$db;
|
||||
$this->savconf=$conf;
|
||||
$this->savuser=$user;
|
||||
$this->savlangs=$langs;
|
||||
$this->savdb=$db;
|
||||
|
||||
print __METHOD__."\n";
|
||||
print __METHOD__." db->type=".$db->type." user->id=".$user->id;
|
||||
//print " - db ".$db->db;
|
||||
print "\n";
|
||||
}
|
||||
|
||||
// Static methods
|
||||
public static function setUpBeforeClass()
|
||||
{
|
||||
global $conf,$user,$langs,$db;
|
||||
//$db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
|
||||
|
||||
print __METHOD__."\n";
|
||||
}
|
||||
public static function tearDownAfterClass()
|
||||
{
|
||||
global $conf,$user,$langs,$db;
|
||||
//$db->rollback();
|
||||
global $conf,$user,$langs,$db;
|
||||
//$db->rollback();
|
||||
|
||||
print __METHOD__."\n";
|
||||
print __METHOD__."\n";
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
/**
|
||||
*/
|
||||
protected function setUp()
|
||||
{
|
||||
global $conf,$user,$langs,$db;
|
||||
$conf=$this->savconf;
|
||||
$user=$this->savuser;
|
||||
$langs=$this->savlangs;
|
||||
$db=$this->savdb;
|
||||
global $conf,$user,$langs,$db;
|
||||
$conf=$this->savconf;
|
||||
$user=$this->savuser;
|
||||
$langs=$this->savlangs;
|
||||
$db=$this->savdb;
|
||||
|
||||
print __METHOD__."\n";
|
||||
print __METHOD__."\n";
|
||||
}
|
||||
/**
|
||||
*/
|
||||
/**
|
||||
*/
|
||||
protected function tearDown()
|
||||
{
|
||||
print __METHOD__."\n";
|
||||
print __METHOD__."\n";
|
||||
}
|
||||
|
||||
|
||||
@@ -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