diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index 0a4014f1855..c3e4eb98a8d 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -7827,6 +7827,7 @@ function getDictvalue($tablename, $field, $id, $checkentity = false, $rowidfield */ function colorIsLight($stringcolor) { + $stringcolor = str_replace('#', '', $stringcolor); $res = -1; if (!empty($stringcolor)) { @@ -7897,3 +7898,213 @@ function roundUpToNextMultiple($n, $x = 5) { return (ceil($n)%$x === 0) ? ceil($n) : round(($n+$x/2)/$x)*$x; } + +/** + * @param string $label label of badge no html : use in alt attribute for accessibility + * @param string $html optional : label of badge with html + * @param string $type type of badge : Primary Secondary Success Danger Warning Info Light Dark status0 status1 status2 status3 status4 status5 status6 status7 status8 status9 + * @param string $mode default '' , pill, dot + * @param string $url the url for link + * @param array $params various params for future : recommended rather than adding more fuction arguments + * @return string html badge + */ +function dolGetBadge($label, $html = '', $type = 'primary', $mode = '', $url = '', $params = array()) +{ + + $attr=array( + 'class'=>'badge'.(!empty($mode)?' badge-'.$mode:'').(!empty($type)?' badge-'.$type:'') + ); + + if(empty($html)){ + $html = $label; + } + + if(!empty($url)){ + $attr['href'] = $url; + } + + if($mode==='dot') + { + $attr['class'].= ' classfortooltip'; + $attr['title'] = $html; + $attr['aria-label'] = $label; + $html=''; + } + + // Override attr + if(!empty($params['attr']) && is_array($params['attr'])){ + foreach($params['attr']as $key => $value){ + $attr[$key] = $value; + } + } + + // TODO: add hook + + // escape all attribute + $attr = array_map('dol_escape_htmltag', $attr); + + $TCompiledAttr = array(); + foreach($attr as $key => $value){ + $TCompiledAttr[] = $key.'="'.$value.'"'; + } + + $compiledAttributes = !empty($TCompiledAttr)?implode(' ', $TCompiledAttr):''; + + $tag = !empty($url)?'a':'span'; + + return '<'.$tag.' '.$compiledAttributes.'>'.$html.''; +} + + +/** + * @param string $statusLabel label of badge no html : use in alt attribute for accessibility + * @param string $statusLabelShort short label of badge no html + * @param string $html optional : label of badge with html + * @param string $statusType status0 status1 status2 status3 status4 status5 status6 status7 status8 status9 : image name or badge name + * @param int $displayMode for retrocompatibility 0=Long label, 1=Short label, 2=Picto + Short label, 3=Picto, 4=Picto + Long label, 5=Short label + Picto, 6=Long label + Picto + * @param string $url the url for link + * @param array $params various params for future : recommended rather than adding more function arguments + * @return string html status + */ +function dolGetStatus($statusLabel = '', $statusLabelShort = '', $html = '', $statusType = 'status0', $displayMode = 0, $url = '', $params = array()) +{ + global $conf; + + // image's filename are still in French + $statusImg=array( + 'status0' => 'statut0' + ,'status1' => 'statut1' + ,'status2' => 'statut2' + ,'status3' => 'statut3' + ,'status4' => 'statut4' + ,'status5' => 'statut5' + ,'status6' => 'statut6' + ,'status7' => 'statut7' + ,'status8' => 'statut8' + ,'status9' => 'statut9' + ); + + // TODO : add a hook + + if($displayMode==0){ + $return = !empty($html)?$html:$statusLabel; + } + elseif($displayMode===1){ + $return = !empty($html)?$html:(!empty($statusLabelShort)?$statusLabelShort:$statusLabel); + } + // use status with images + elseif(empty($conf->global->MAIN_STATUS_USES_CSS)){ + $return = ''; + $htmlLabel = ''.(!empty($html)?$html:$statusLabel).''; + $htmlLabelShort = ''.(!empty($html)?$html:(!empty($statusLabelShort)?$statusLabelShort:$statusLabel)).''; + + if(!empty($statusImg[$statusType])){ + $htmlImg = img_picto($statusLabel, $statusImg[$statusType]); + }else{ + $htmlImg = img_picto($statusLabel, $statusType); + } + + + if($displayMode === 2){ + $return = $htmlImg .' '. $htmlLabel; + } + elseif($displayMode === 3){ + $return = $htmlImg; + } + elseif($displayMode === 4){ + $return = $htmlImg .' '. $htmlLabel; + } + elseif($displayMode === 5){ + $return = $htmlLabelShort .' '. $htmlImg; + } + else{ // $displayMode >= 6 + $return = $htmlLabel .' '. $htmlImg; + } + } + // Use new badge + elseif(!empty($conf->global->MAIN_STATUS_USES_CSS) && !empty($displayMode)){ + + $statusLabelShort = !empty($statusLabelShort)?$statusLabelShort:$statusLabel; + + if($displayMode == 3){ + $return = dolGetBadge($statusLabel, '', $statusType, 'dot'); + } + elseif($displayMode === 5){ + $return = dolGetBadge($statusLabelShort, $html, $statusType); + } + else{ + $return = dolGetBadge($statusLabel, $html, $statusType); + } + } + + return $return; +} + + +/** + * @param string $label label of button no html : use in alt attribute for accessibility $html is not empty + * @param string $html optional : content with html + * @param string $actionType default, delete, danger + * @param string $url the url for link + * @param string $id attribute id of button + * @param int $userRight user action right + * @param array $params various params for future : recommended rather than adding more function arguments + * @return string html button + */ +function dolGetButtonAction($label, $html = '', $actionType = 'default', $url = '', $id = '', $userRight = 1, $params = array()) +{ + + + $class = 'butAction' ; + if($actionType == 'danger' || $actionType == 'delete'){ + $class = 'butActionDelete' ; + } + + $attr=array( + 'class' => $class + ,'href' => empty($url)?'':$url + ); + + if(empty($html)){ + $html = $label; + }else{ + $attr['aria-label'] = $label; + } + + + if(empty($userRight)){ + $attr['class'] = 'butActionRefused'; + $attr['href'] = ''; + } + + if(empty($id)){ + $attr['id'] = $id; + } + + // Override attr + if(!empty($params['attr']) && is_array($params['attr'])){ + foreach($params['attr'] as $key => $value){ + $attr[$key] = $value; + } + } + + if(isset($attr['href']) && empty($attr['href'])){ + unset($attr['href']); + } + + // TODO : add a hook + + // escape all attribute + $attr = array_map('dol_escape_htmltag', $attr); + + $TCompiledAttr = array(); + foreach($attr as $key => $value){ + $TCompiledAttr[] = $key.'="'.$value.'"'; + } + + $compiledAttributes = !empty($TCompiledAttr)?implode(' ', $TCompiledAttr):''; + + $tag = !empty($attr['href'])?'a':'span'; + + return '
<'.$tag.' '.$compiledAttributes.'>'.$html.'
'; +} diff --git a/htdocs/core/lib/functions2.lib.php b/htdocs/core/lib/functions2.lib.php index 06e551d2ad6..99823fba3af 100644 --- a/htdocs/core/lib/functions2.lib.php +++ b/htdocs/core/lib/functions2.lib.php @@ -2200,6 +2200,108 @@ function colorStringToArray($stringcolor, $colorifnotfound = array(88,88,88)) return array(hexdec($reg[1]),hexdec($reg[2]),hexdec($reg[3])); } +/** + * @param string $color the color you need to valid + * @param boolean $allow_white in case of white isn't valid + * @return boolean + */ +function colorValidateHex($color, $allow_white = true) +{ + + if(!$allow_white && ($color === '#fff' || $color === '#ffffff') ) return false; + + if(preg_match('/^#[a-f0-9]{6}$/i', $color)) //hex color is valid + { + return true; + } + return false; +} + + +/** + * @param string $hex color in hex + * @param integer $steps Steps should be between -255 and 255. Negative = darker, positive = lighter + * @return string + */ +function colorAdjustBrightness($hex, $steps) +{ + // Steps should be between -255 and 255. Negative = darker, positive = lighter + $steps = max(-255, min(255, $steps)); + + // Normalize into a six character long hex string + $hex = str_replace('#', '', $hex); + if (strlen($hex) == 3) { + $hex = str_repeat(substr($hex, 0, 1), 2).str_repeat(substr($hex, 1, 1), 2).str_repeat(substr($hex, 2, 1), 2); + } + + // Split into three parts: R, G and B + $color_parts = str_split($hex, 2); + $return = '#'; + + foreach ($color_parts as $color) { + $color = hexdec($color); // Convert to decimal + $color = max(0, min(255, $color + $steps)); // Adjust color + $return .= str_pad(dechex($color), 2, '0', STR_PAD_LEFT); // Make two char hex code + } + + return $return; +} + +/** + * @param string $hex color in hex + * @param integer $percent 0 to 100 + * @return string + */ +function colorDarker($hex, $percent) +{ + $steps = intval(255 * $percent / 100) * -1; + return colorAdjustBrightness($hex, $steps); +} + +/** + * @param string $hex color in hex + * @param integer $percent 0 to 100 + * @return string + */ +function colorLighten($hex, $percent) +{ + $steps = intval(255 * $percent / 100); + return colorAdjustBrightness($hex, $steps); +} + + +/** + * @param string $hex color in hex + * @param float $alpha 0 to 1 + * @param bool $returnArray set to 1 to return an array instead of string + * @return string + */ +function colorHexToRgb($hex, $alpha = false, $returnArray = false) +{ + $string = ''; + $hex = str_replace('#', '', $hex); + $length = strlen($hex); + $rgb = array(); + $rgb['r'] = hexdec($length == 6 ? substr($hex, 0, 2) : ($length == 3 ? str_repeat(substr($hex, 0, 1), 2) : 0)); + $rgb['g'] = hexdec($length == 6 ? substr($hex, 2, 2) : ($length == 3 ? str_repeat(substr($hex, 1, 1), 2) : 0)); + $rgb['b'] = hexdec($length == 6 ? substr($hex, 4, 2) : ($length == 3 ? str_repeat(substr($hex, 2, 1), 2) : 0)); + if ( $alpha !== false ) { + $rgb['a'] = floatval($alpha); + $string = 'rgba('.implode(',', $rgb).')'; + } + else{ + $string = 'rgb('.implode(',', $rgb).')'; + } + + if($returnArray){ + return $rgb; + } + else{ + return $string; + } +} + + /** * Applies the Cartesian product algorithm to an array * Source: http://stackoverflow.com/a/15973172 diff --git a/htdocs/theme/eldy/_badges.css.php b/htdocs/theme/eldy/_badges.css.php new file mode 100644 index 00000000000..7384fffed5f --- /dev/null +++ b/htdocs/theme/eldy/_badges.css.php @@ -0,0 +1,201 @@ + +/*