diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index 5e83f59ea36..98bf53743ba 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -1929,7 +1929,10 @@ abstract class CommonObject */ 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; dol_syslog(get_class($this)."::updateRangOfLine", LOG_DEBUG); diff --git a/htdocs/core/class/html.formfile.class.php b/htdocs/core/class/html.formfile.class.php index 78f5f3f5359..cd56b4d0b59 100644 --- a/htdocs/core/class/html.formfile.class.php +++ b/htdocs/core/class/html.formfile.class.php @@ -984,17 +984,20 @@ class FormFile { 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 (! $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]['acl']=''; @@ -1035,6 +1038,10 @@ class FormFile } } + /*var_dump($filearray); + var_dump($sortfield); + var_dump($sortorder);*/ + if ($sortfield && $sortorder) { $filearray=dol_sort_array($filearray, $sortfield, $sortorder); diff --git a/htdocs/core/lib/files.lib.php b/htdocs/core/lib/files.lib.php index 245bfe7d310..d097cc484ba 100644 --- a/htdocs/core/lib/files.lib.php +++ b/htdocs/core/lib/files.lib.php @@ -88,7 +88,7 @@ function dol_dir_list($path, $types="all", $recursive=0, $filter="", $excludefil } // $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 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. * 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 array|null $excludefilter Array of Regex for exclude filter (example: array('(\.meta|_preview\.png)$','^\.')) * @param string $sortcriteria Sort criteria ("","fullname","name","date","size") diff --git a/htdocs/core/lib/images.lib.php b/htdocs/core/lib/images.lib.php index 9cf745a0da0..00153a7849d 100644 --- a/htdocs/core/lib/images.lib.php +++ b/htdocs/core/lib/images.lib.php @@ -37,8 +37,10 @@ $quality = 80; */ 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 - 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 $imgfonction=''; @@ -47,6 +49,8 @@ function image_format_supported($file) if (strtolower($reg[1]) == '.jpg') $imgfonction = 'imagecreatefromjpeg'; if (strtolower($reg[1]) == '.jpeg') $imgfonction = 'imagecreatefromjpeg'; if (strtolower($reg[1]) == '.bmp') $imgfonction = 'imagecreatefromwbmp'; + if (strtolower($reg[1]) == '.xpm') $imgfonction = 'imagecreatefromxpm'; + if (strtolower($reg[1]) == '.xbm') $imgfonction = 'imagecreatefromxbm'; if ($imgfonction) { if (! function_exists($imgfonction)) diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php index a61406c90d7..0fd2be8ba7f 100644 --- a/htdocs/product/class/product.class.php +++ b/htdocs/product/class/product.class.php @@ -54,7 +54,7 @@ class Product extends CommonObject */ 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 @@ -3691,15 +3691,16 @@ class Product extends CommonObject } /** - * Affiche la premiere photo du produit + * Return if at least one photo is available * - * @param string $sdir Repertoire a scanner - * @return boolean true si photo dispo, false sinon + * @param string $sdir Directory to scan + * @return boolean True if at least one photo is available, False if not */ 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; $dir = $sdir; @@ -3717,7 +3718,7 @@ class Product extends CommonObject while (($file = readdir($handle)) !== false) { 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 * 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 $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. @@ -3747,140 +3748,186 @@ class Product extends CommonObject include_once DOL_DOCUMENT_ROOT .'/core/lib/files.lib.php'; include_once DOL_DOCUMENT_ROOT .'/core/lib/images.lib.php'; + $sortfield='position_name'; + $sortorder='asc'; + $dir = $sdir . '/'; $pdir = '/'; + $dir .= 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)) { - $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.'/'; - $pdir .= get_exdir(0,0,0,0,$this,'product').$this->ref.'/'; + $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/'; $pdirthumb = $pdir.'thumbs/'; $return =''."\n"; $nbphoto=0; - $dir_osencoded=dol_osencode($dir); - if (file_exists($dir_osencoded)) + $filearray=dol_dir_list($dir,"files",0,'','(\.meta|_preview\.png)$',$sortfield,(strtolower($sortorder)=='desc'?SORT_DESC:SORT_ASC),1); + + if (! empty($conf->global->PRODUCT_USE_OLD_PATH_FOR_PHOTO)) // For backward compatiblity, we scan also old dirs { - $handle=opendir($dir_osencoded); - if (is_resource($handle)) + $filearrayold=dol_dir_list($dirold,"files",0,'','(\.meta|_preview\.png)$',$sortfield,(strtolower($sortorder)=='desc'?SORT_DESC:SORT_ASC),1); + $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) + { + $found=0; + // Search if it exists into $filearrayindatabase + foreach($filearrayindatabase as $key2 => $val2) { - while (($file = readdir($handle)) !== false) - { - $photo=''; + 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=''; + $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) && image_format_supported($file) >= 0) + if (image_format_supported($file) >= 0) + { + $nbphoto++; + $photo = $file; + $viewfilename = $file; - if (dol_is_file($dir.$file) && preg_match('/('.$this->regeximgext.')$/i', $dir.$file)) - { - $nbphoto++; - $photo = $file; - $viewfilename = $file; + if ($size == 1 || $size == 'small') { // Format vignette - if ($size == 1 || $size == 'small') { // Format vignette + // Find name of thumb file + $photo_vignette=basename(getImageFileNameForSize($dir.$file, '_small')); + if (! dol_is_file($dirthumb.$photo_vignette)) $photo_vignette=''; + + // Get filesize of original file + $imgarray=dol_getImageSize($dir.$photo); - // Find name of thumb file - $photo_vignette=basename(getImageFileNameForSize($dir.$file, '_small')); - if (! dol_is_file($dirthumb.$photo_vignette)) $photo_vignette=''; - - // Get filesize of original file - $imgarray=dol_getImageSize($dir.$photo); + if ($nbbyrow > 0) + { + if ($nbphoto == 1) $return.= ''; - if ($nbbyrow > 0) - { - if ($nbphoto == 1) $return.= '
'; + if ($nbphoto % $nbbyrow == 1) $return.= ''; + $return.= ''; - $return.= ''; + if (($nbphoto % $nbbyrow) == 0) $return.= ''; + } + else if ($nbbyrow < 0) $return.=''; + } - if ($nbbyrow > 0) - { - $return.= ''; - if (($nbphoto % $nbbyrow) == 0) $return.= ''; - } - else if ($nbbyrow < 0) $return.=''; - } + if (empty($size)) { // Format origine + $return.= ''; - if (empty($size)) { // Format origine - $return.= ''; + if ($showfilename) $return.= '
'.$viewfilename; + if ($showaction) + { + if ($user->rights->produit->creer || $user->rights->service->creer) + { + // Link to resize + $return.= ''.img_picto($langs->trans("Resize"),DOL_URL_ROOT.'/theme/common/transform-crop-and-resize','',1).'   '; - if ($showfilename) $return.= '
'.$viewfilename; - if ($showaction) - { - if ($user->rights->produit->creer || $user->rights->service->creer) - { - // Link to resize - $return.= ''.img_picto($langs->trans("Resize"),DOL_URL_ROOT.'/theme/common/transform-crop-and-resize','',1).'   '; + // Link to delete + $return.= ''; + $return.= img_delete().''; + } + } + } - // Link to delete - $return.= ''; - $return.= img_delete().''; - } - } - } - - // On continue ou on arrete de boucler ? - if ($nbmax && $nbphoto >= $nbmax) break; - } - } + // On continue ou on arrete de boucler ? + if ($nbmax && $nbphoto >= $nbmax) break; + } } if ($size==1 || $size=='small') @@ -3897,8 +3944,6 @@ class Product extends CommonObject if ($nbphoto) $return.= '
'; + } + else if ($nbbyrow < 0) $return .= '
'; - if ($nbphoto % $nbbyrow == 1) $return.= '
'; - } - else if ($nbbyrow < 0) $return .= '
'; + $return.= "\n"; + + $relativefile=preg_replace('/^\//', '', $pdir.$photo); + if (empty($nolink)) + { + $urladvanced=getAdvancedPreviewUrl('product', $relativefile); + if ($urladvanced) $return.=''; + else $return.= ''; + } - $return.= "\n"; - - $relativefile=preg_replace('/^\//', '', $pdir.$photo); - if (empty($nolink)) - { - $urladvanced=getAdvancedPreviewUrl('product', $relativefile); - if ($urladvanced) $return.=''; - else $return.= ''; - } + // Show image (width height=$maxHeight) + // Si fichier vignette disponible et image source trop grande, on utilise la vignette, sinon on utilise photo origine + $alt=$langs->transnoentitiesnoconv('File').': '.$relativefile; + $alt.=' - '.$langs->transnoentitiesnoconv('Size').': '.$imgarray['width'].'x'.$imgarray['height']; + + if (empty($maxHeight) || $photo_vignette && $imgarray['height'] > $maxHeight) + { + $return.= ''; + $return.= ''; + } + else { + $return.= ''; + $return.= ''; + } - // Show image (width height=$maxHeight) - // Si fichier vignette disponible et image source trop grande, on utilise la vignette, sinon on utilise photo origine - $alt=$langs->transnoentitiesnoconv('File').': '.$relativefile; - $alt.=' - '.$langs->transnoentitiesnoconv('Size').': '.$imgarray['width'].'x'.$imgarray['height']; - - if (empty($maxHeight) || $photo_vignette && $imgarray['height'] > $maxHeight) - { - $return.= ''; - $return.= ''; - } - else { - $return.= ''; - $return.= ''; - } + if (empty($nolink)) $return.= ''; + $return.="\n"; - if (empty($nolink)) $return.= ''; - $return.="\n"; + if ($showfilename) $return.= '
'.$viewfilename; + if ($showaction) + { + $return.= '
'; + // On propose la generation de la vignette si elle n'existe pas et si la taille est superieure aux limites + if ($photo_vignette && (image_format_supported($photo) > 0) && ($this->imgWidth > $maxWidth || $this->imgHeight > $maxHeight)) + { + $return.= ''.img_picto($langs->trans('GenerateThumb'),'refresh').'  '; + } + if ($user->rights->produit->creer || $user->rights->service->creer) + { + // Link to resize + $return.= ''.img_picto($langs->trans("Resize"),DOL_URL_ROOT.'/theme/common/transform-crop-and-resize','',1).'   '; - if ($showfilename) $return.= '
'.$viewfilename; - if ($showaction) - { - $return.= '
'; - // 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)) - { - $return.= ''.img_picto($langs->trans('GenerateThumb'),'refresh').'  '; - } - if ($user->rights->produit->creer || $user->rights->service->creer) - { - // Link to resize - $return.= ''.img_picto($langs->trans("Resize"),DOL_URL_ROOT.'/theme/common/transform-crop-and-resize','',1).'   '; + // Link to delete + $return.= ''; + $return.= img_delete().''; + } + } + $return.= "\n"; - // Link to delete - $return.= ''; - $return.= img_delete().''; - } - } - $return.= "\n"; + if ($nbbyrow > 0) + { + $return.= '
'; } } - - closedir($handle); } $this->nbphoto = $nbphoto; @@ -3916,8 +3961,9 @@ class Product extends CommonObject */ 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; $tabobj=array(); @@ -3928,7 +3974,7 @@ class Product extends CommonObject while (($file = readdir($handle)) !== false) { 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++; @@ -3969,8 +4015,9 @@ class Product extends CommonObject */ 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 $dirthumb = $dir.'/thumbs/'; // Chemin du dossier contenant la vignette $filename = preg_replace('/'.preg_quote($dir,'/').'/i','',$file); // Nom du fichier diff --git a/htdocs/product/document.php b/htdocs/product/document.php index 07381787cb6..ae2782cc4de 100644 --- a/htdocs/product/document.php +++ b/htdocs/product/document.php @@ -62,7 +62,7 @@ $offset = $conf->liste_limit * $page; $pageprev = $page - 1; $pagenext = $page + 1; if (! $sortorder) $sortorder="ASC"; -if (! $sortfield) $sortfield="name"; +if (! $sortfield) $sortfield="position_name"; $object = new Product($db);