NEW Can sort thumbs visible on product card

This commit is contained in:
Laurent Destailleur 2016-12-31 16:16:20 +01:00
parent f1961b0207
commit f46ce68d08
6 changed files with 188 additions and 127 deletions

View File

@ -1929,7 +1929,10 @@ abstract class CommonObject
*/ */
function updateRangOfLine($rowid,$rang) function updateRangOfLine($rowid,$rang)
{ {
$sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element_line.' SET rang = '.$rang; $fieldposition = 'rang';
if ($this->table_element_line == 'ecm_files') $fieldposition = 'position';
$sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element_line.' SET '.$fieldposition.' = '.$rang;
$sql.= ' WHERE rowid = '.$rowid; $sql.= ' WHERE rowid = '.$rowid;
dol_syslog(get_class($this)."::updateRangOfLine", LOG_DEBUG); dol_syslog(get_class($this)."::updateRangOfLine", LOG_DEBUG);

View File

@ -984,17 +984,20 @@ class FormFile
{ {
if ($filearrayindatabase[$key2]['name'] == $filearray[$key]['name']) if ($filearrayindatabase[$key2]['name'] == $filearray[$key]['name'])
{ {
$filearray[$key]['position_name']=($filearrayindatabase[$key2]['position']?$filearrayindatabase[$key2]['position']:'0').'_'.$filearrayindatabase[$key2]['name'];
$filearray[$key]['position']=$filearrayindatabase[$key2]['position']; $filearray[$key]['position']=$filearrayindatabase[$key2]['position'];
$filearray[$key]['cover']=$filearrayindatabase[$key2]['cover']; $filearray[$key]['cover']=$filearrayindatabase[$key2]['cover'];
$filearray[$key]['acl']=$filearrayindatabase[$key2]['acl']; $filearray[$key]['acl']=$filearrayindatabase[$key2]['acl'];
$filearray[$key]['rowid']=$filearrayindatabase[$key2]['rowid']; $filearray[$key]['rowid']=$filearrayindatabase[$key2]['rowid'];
$filearray[$key]['label']=$filearrayindatabase[$key2]['label'];
$found=1; $found=1;
break; break;
} }
} }
if (! $found)
if (! $found) // This happen in transition towerd version 6, or if files were added manually into os dir.
{ {
$filearray[$key]['position']=999999; // File not indexed are at end. So if we add a file, it will not replace an existing position $filearray[$key]['position']='999999'; // File not indexed are at end. So if we add a file, it will not replace an existing position
$filearray[$key]['cover']=0; $filearray[$key]['cover']=0;
$filearray[$key]['acl']=''; $filearray[$key]['acl']='';
@ -1035,6 +1038,10 @@ class FormFile
} }
} }
/*var_dump($filearray);
var_dump($sortfield);
var_dump($sortorder);*/
if ($sortfield && $sortorder) if ($sortfield && $sortorder)
{ {
$filearray=dol_sort_array($filearray, $sortfield, $sortorder); $filearray=dol_sort_array($filearray, $sortfield, $sortorder);

View File

@ -88,7 +88,7 @@ function dol_dir_list($path, $types="all", $recursive=0, $filter="", $excludefil
} }
// $reshook may contain returns stacked by other modules // $reshook may contain returns stacked by other modules
// $reshook is always empty with an array for can not lose returns stacked with other modules // $reshook is always empty with an array to not lose returns stacked with other modules
// $hookmanager->resArray may contain array stacked by other modules // $hookmanager->resArray may contain array stacked by other modules
if (! $nohook && ! empty($hookmanager->resArray)) // forced to use $hookmanager->resArray even if $hookmanager->resArray['nodes'] is empty if (! $nohook && ! empty($hookmanager->resArray)) // forced to use $hookmanager->resArray even if $hookmanager->resArray['nodes'] is empty
{ {
@ -210,7 +210,7 @@ function dol_dir_list($path, $types="all", $recursive=0, $filter="", $excludefil
* Scan a directory and return a list of files/directories. * Scan a directory and return a list of files/directories.
* Content for string is UTF8 and dir separator is "/". * Content for string is UTF8 and dir separator is "/".
* *
* @param string $path Starting path from which to search * @param string $path Starting path from which to search. Example: 'produit/MYPROD'
* @param string $filter Regex filter to restrict list. This regex value must be escaped for '/', since this char is used for preg_match function * @param string $filter Regex filter to restrict list. This regex value must be escaped for '/', since this char is used for preg_match function
* @param array|null $excludefilter Array of Regex for exclude filter (example: array('(\.meta|_preview\.png)$','^\.')) * @param array|null $excludefilter Array of Regex for exclude filter (example: array('(\.meta|_preview\.png)$','^\.'))
* @param string $sortcriteria Sort criteria ("","fullname","name","date","size") * @param string $sortcriteria Sort criteria ("","fullname","name","date","size")

View File

@ -37,8 +37,10 @@ $quality = 80;
*/ */
function image_format_supported($file) function image_format_supported($file)
{ {
$regeximgext='\.gif|\.jpg|\.jpeg|\.png|\.bmp|\.xpm|\.xbm'; // See also into product.class.php
// Case filename is not a format image // Case filename is not a format image
if (! preg_match('/(\.gif|\.jpg|\.jpeg|\.png|\.bmp)$/i',$file,$reg)) return -1; if (! preg_match('/('.$regeximgext.')$/i',$file,$reg)) return -1;
// Case filename is a format image but not supported by this PHP // Case filename is a format image but not supported by this PHP
$imgfonction=''; $imgfonction='';
@ -47,6 +49,8 @@ function image_format_supported($file)
if (strtolower($reg[1]) == '.jpg') $imgfonction = 'imagecreatefromjpeg'; if (strtolower($reg[1]) == '.jpg') $imgfonction = 'imagecreatefromjpeg';
if (strtolower($reg[1]) == '.jpeg') $imgfonction = 'imagecreatefromjpeg'; if (strtolower($reg[1]) == '.jpeg') $imgfonction = 'imagecreatefromjpeg';
if (strtolower($reg[1]) == '.bmp') $imgfonction = 'imagecreatefromwbmp'; if (strtolower($reg[1]) == '.bmp') $imgfonction = 'imagecreatefromwbmp';
if (strtolower($reg[1]) == '.xpm') $imgfonction = 'imagecreatefromxpm';
if (strtolower($reg[1]) == '.xbm') $imgfonction = 'imagecreatefromxbm';
if ($imgfonction) if ($imgfonction)
{ {
if (! function_exists($imgfonction)) if (! function_exists($imgfonction))

View File

@ -54,7 +54,7 @@ class Product extends CommonObject
*/ */
protected $table_ref_field = 'ref'; protected $table_ref_field = 'ref';
var $regeximgext='\.jpg|\.jpeg|\.bmp|\.gif|\.png|\.tiff'; var $regeximgext='\.gif|\.jpg|\.jpeg|\.png|\.bmp|\.xpm|\.xbm'; // See also into images.lib.php
/* /*
* @deprecated * @deprecated
@ -3691,14 +3691,15 @@ class Product extends CommonObject
} }
/** /**
* Affiche la premiere photo du produit * Return if at least one photo is available
* *
* @param string $sdir Repertoire a scanner * @param string $sdir Directory to scan
* @return boolean true si photo dispo, false sinon * @return boolean True if at least one photo is available, False if not
*/ */
function is_photo_available($sdir) function is_photo_available($sdir)
{ {
include_once DOL_DOCUMENT_ROOT .'/core/lib/files.lib.php'; include_once DOL_DOCUMENT_ROOT .'/core/lib/files.lib.php';
include_once DOL_DOCUMENT_ROOT .'/core/lib/images.lib.php';
global $conf; global $conf;
@ -3717,7 +3718,7 @@ class Product extends CommonObject
while (($file = readdir($handle)) !== false) while (($file = readdir($handle)) !== false)
{ {
if (! utf8_check($file)) $file=utf8_encode($file); // To be sure data is stored in UTF8 in memory if (! utf8_check($file)) $file=utf8_encode($file); // To be sure data is stored in UTF8 in memory
if (dol_is_file($dir.$file)) return true; if (dol_is_file($dir.$file) && image_format_supported($file) > 0) return true;
} }
} }
} }
@ -3729,7 +3730,7 @@ class Product extends CommonObject
* Show photos of a product (nbmax maximum), into several columns * Show photos of a product (nbmax maximum), into several columns
* TODO Move this into html.formproduct.class.php * TODO Move this into html.formproduct.class.php
* *
* @param string $sdir Directory to scan * @param string $sdir Directory to scan (full absolute path)
* @param int $size 0=original size, 1='small' use thumbnail if possible * @param int $size 0=original size, 1='small' use thumbnail if possible
* @param int $nbmax Nombre maximum de photos (0=pas de max) * @param int $nbmax Nombre maximum de photos (0=pas de max)
* @param int $nbbyrow Number of image per line or -1 to use div. Used only if size=1. * @param int $nbbyrow Number of image per line or -1 to use div. Used only if size=1.
@ -3747,17 +3748,27 @@ class Product extends CommonObject
include_once DOL_DOCUMENT_ROOT .'/core/lib/files.lib.php'; include_once DOL_DOCUMENT_ROOT .'/core/lib/files.lib.php';
include_once DOL_DOCUMENT_ROOT .'/core/lib/images.lib.php'; include_once DOL_DOCUMENT_ROOT .'/core/lib/images.lib.php';
$sortfield='position_name';
$sortorder='asc';
$dir = $sdir . '/'; $dir = $sdir . '/';
$pdir = '/'; $pdir = '/';
if (! empty($conf->global->PRODUCT_USE_OLD_PATH_FOR_PHOTO))
{
$dir .= get_exdir($this->id,2,0,0,$this,'product') . $this->id ."/photos/";
$pdir .= get_exdir($this->id,2,0,0,$this,'product') . $this->id ."/photos/";
}
else
{
$dir .= get_exdir(0,0,0,0,$this,'product').$this->ref.'/'; $dir .= get_exdir(0,0,0,0,$this,'product').$this->ref.'/';
$pdir .= get_exdir(0,0,0,0,$this,'product').$this->ref.'/'; $pdir .= get_exdir(0,0,0,0,$this,'product').$this->ref.'/';
if (! empty($conf->global->PRODUCT_USE_OLD_PATH_FOR_PHOTO))
{
$dirold .= get_exdir($this->id,2,0,0,$this,'product') . $this->id ."/photos/";
$pdirold .= get_exdir($this->id,2,0,0,$this,'product') . $this->id ."/photos/";
}
// Defined relative dir to DOL_DATA_ROOT
$relativedir = '';
if ($dir)
{
$relativedir = preg_replace('/^'.preg_quote(DOL_DATA_ROOT,'/').'/', '', $dir);
$relativedir = preg_replace('/^[\\/]/','',$relativedir);
$relativedir = preg_replace('/[\\/]$/','',$relativedir);
} }
$dirthumb = $dir.'thumbs/'; $dirthumb = $dir.'thumbs/';
@ -3766,19 +3777,56 @@ class Product extends CommonObject
$return ='<!-- Photo -->'."\n"; $return ='<!-- Photo -->'."\n";
$nbphoto=0; $nbphoto=0;
$dir_osencoded=dol_osencode($dir); $filearray=dol_dir_list($dir,"files",0,'','(\.meta|_preview\.png)$',$sortfield,(strtolower($sortorder)=='desc'?SORT_DESC:SORT_ASC),1);
if (file_exists($dir_osencoded))
if (! empty($conf->global->PRODUCT_USE_OLD_PATH_FOR_PHOTO)) // For backward compatiblity, we scan also old dirs
{ {
$handle=opendir($dir_osencoded); $filearrayold=dol_dir_list($dirold,"files",0,'','(\.meta|_preview\.png)$',$sortfield,(strtolower($sortorder)=='desc'?SORT_DESC:SORT_ASC),1);
if (is_resource($handle)) $filearray=array_merge($filearray, $filearrayold);
}
$filearrayindatabase = dol_dir_list_in_database($relativedir, '', null, 'name', SORT_ASC);
//var_dump($filearray);
//var_dump($filearrayindatabase);
// Complete filearray with properties found into $filearrayindatabase
foreach($filearray as $key => $val)
{ {
while (($file = readdir($handle)) !== false) $found=0;
// Search if it exists into $filearrayindatabase
foreach($filearrayindatabase as $key2 => $val2)
{
if ($filearrayindatabase[$key2]['name'] == $filearray[$key]['name'])
{
$filearray[$key]['position_name']=($filearrayindatabase[$key2]['position']?$filearrayindatabase[$key2]['position']:'0').'_'.$filearrayindatabase[$key2]['name'];
$filearray[$key]['position']=$filearrayindatabase[$key2]['position'];
$filearray[$key]['cover']=$filearrayindatabase[$key2]['cover'];
$filearray[$key]['acl']=$filearrayindatabase[$key2]['acl'];
$filearray[$key]['rowid']=$filearrayindatabase[$key2]['rowid'];
$filearray[$key]['label']=$filearrayindatabase[$key2]['label'];
$found=1;
break;
}
}
}
if (count($filearray))
{
if ($sortfield && $sortorder)
{
$filearray=dol_sort_array($filearray, $sortfield, $sortorder);
}
foreach($filearray as $key => $val)
{ {
$photo=''; $photo='';
$file = $val['name'];
if (! utf8_check($file)) $file=utf8_encode($file); // To be sure file is stored in UTF8 in memory //if (! utf8_check($file)) $file=utf8_encode($file); // To be sure file is stored in UTF8 in memory
if (dol_is_file($dir.$file) && preg_match('/('.$this->regeximgext.')$/i', $dir.$file)) //if (dol_is_file($dir.$file) && image_format_supported($file) >= 0)
if (image_format_supported($file) >= 0)
{ {
$nbphoto++; $nbphoto++;
$photo = $file; $photo = $file;
@ -3835,7 +3883,7 @@ class Product extends CommonObject
{ {
$return.= '<br>'; $return.= '<br>';
// On propose la generation de la vignette si elle n'existe pas et si la taille est superieure aux limites // On propose la generation de la vignette si elle n'existe pas et si la taille est superieure aux limites
if ($photo_vignette && preg_match('/('.$this->regeximgext.')$/i', $photo) && ($this->imgWidth > $maxWidth || $this->imgHeight > $maxHeight)) if ($photo_vignette && (image_format_supported($photo) > 0) && ($this->imgWidth > $maxWidth || $this->imgHeight > $maxHeight))
{ {
$return.= '<a href="'.$_SERVER["PHP_SELF"].'?id='.$this->id.'&amp;action=addthumb&amp;file='.urlencode($pdir.$viewfilename).'">'.img_picto($langs->trans('GenerateThumb'),'refresh').'&nbsp;&nbsp;</a>'; $return.= '<a href="'.$_SERVER["PHP_SELF"].'?id='.$this->id.'&amp;action=addthumb&amp;file='.urlencode($pdir.$viewfilename).'">'.img_picto($langs->trans('GenerateThumb'),'refresh').'&nbsp;&nbsp;</a>';
} }
@ -3881,7 +3929,6 @@ class Product extends CommonObject
if ($nbmax && $nbphoto >= $nbmax) break; if ($nbmax && $nbphoto >= $nbmax) break;
} }
} }
}
if ($size==1 || $size=='small') if ($size==1 || $size=='small')
{ {
@ -3897,8 +3944,6 @@ class Product extends CommonObject
if ($nbphoto) $return.= '</table>'; if ($nbphoto) $return.= '</table>';
} }
} }
closedir($handle);
} }
$this->nbphoto = $nbphoto; $this->nbphoto = $nbphoto;
@ -3917,6 +3962,7 @@ class Product extends CommonObject
function liste_photos($dir,$nbmax=0) function liste_photos($dir,$nbmax=0)
{ {
include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php'; include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
include_once DOL_DOCUMENT_ROOT.'/core/lib/images.lib.php';
$nbphoto=0; $nbphoto=0;
$tabobj=array(); $tabobj=array();
@ -3928,7 +3974,7 @@ class Product extends CommonObject
while (($file = readdir($handle)) !== false) while (($file = readdir($handle)) !== false)
{ {
if (! utf8_check($file)) $file=utf8_encode($file); // readdir returns ISO if (! utf8_check($file)) $file=utf8_encode($file); // readdir returns ISO
if (dol_is_file($dir.$file) && preg_match('/('.$this->regeximgext.')$/i', $dir.$file)) if (dol_is_file($dir.$file) && image_format_supported($file) >= 0)
{ {
$nbphoto++; $nbphoto++;
@ -3970,6 +4016,7 @@ class Product extends CommonObject
function delete_photo($file) function delete_photo($file)
{ {
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/images.lib.php';
$dir = dirname($file).'/'; // Chemin du dossier contenant l'image d'origine $dir = dirname($file).'/'; // Chemin du dossier contenant l'image d'origine
$dirthumb = $dir.'/thumbs/'; // Chemin du dossier contenant la vignette $dirthumb = $dir.'/thumbs/'; // Chemin du dossier contenant la vignette

View File

@ -62,7 +62,7 @@ $offset = $conf->liste_limit * $page;
$pageprev = $page - 1; $pageprev = $page - 1;
$pagenext = $page + 1; $pagenext = $page + 1;
if (! $sortorder) $sortorder="ASC"; if (! $sortorder) $sortorder="ASC";
if (! $sortfield) $sortfield="name"; if (! $sortfield) $sortfield="position_name";
$object = new Product($db); $object = new Product($db);