Copyright (C) 2010 - 2019 All Rights Reserved. Contributor(s): Mark J Crane */ class button { public static $collapse = 'hide-md-dn'; /** * Creates a button element based on the provided array of attributes. * * @param array $array An array containing button attributes, such as type, name, value, id, label, title, onclick, * etc. * * @return string The created button element as a string. */ public static function create($array) { global $settings; $button_icons = $settings->get('theme', 'button_icons', 'auto'); //parse styles into array if (!empty($array['style'])) { $tmp = explode(';', $array['style']); foreach ($tmp as $style) { if (!empty($style)) { $style = explode(':', $style); if (is_array($style) && @sizeof($style) == 2) { $styles[trim($style[0])] = trim($style[1]); } } } $array['style'] = $styles; unset($styles); } //button: open $button = ""; //link if (!empty($array['link'])) { $anchor = " $value) { if (substr_count($property, 'margin')) { $styles .= $property . ': ' . $value . '; '; } } $anchor .= $styles ? "style=" . self::quote($styles) . " " : null; unset($styles); } $anchor .= isset($array['disabled']) && $array['disabled'] ? "class='disabled' onclick='return false;' " : null; $anchor .= ">"; $button = $anchor . $button . ""; } return $button; } /** * Quotes a value by surrounding it with single or double quotes based on whether it contains single quotes. * * @param string $value The value to be quoted. * * @return string The quoted value. */ private static function quote($value) { return substr_count($value, "'") ? '"' . $value . '"' : "'" . $value . "'"; } /** * Escapes a URL by removing leading/trailing whitespace and encoding special characters. * * @param string $url The URL to escape. * * @return string The escaped URL. */ private static function escape_href(string $url): string { // clear whitespace $url = trim($url); return htmlspecialchars($url, ENT_QUOTES, 'UTF-8'); } } /* //usage echo button::create(['type'=>'button','label'=>$text['button-label'],'icon'=>'icon','name'=>'btn','id'=>'btn','value'=>'value','link'=>'url','target'=>'_blank','onclick'=>'javascript','onmouseover'=>'javascript','onmouseout'=>'javascript','class'=>'name','style'=>'css','title'=>$text['button-label'],'collapse'=>'class','disabled'=>false]); echo button::create([ 'type'=>'button', 'label'=>$text['button-label'], 'icon'=>'icon', 'name'=>'btn', 'id'=>'btn', 'value'=>'value', 'link'=>'url', 'target'=>'_blank', 'onclick'=>'javascript', 'onmouseover'=>'javascript', 'onmouseout'=>'javascript', 'class'=>'name', 'style'=>'css', 'title'=>$text['button-label'], 'collapse'=>'class', 'disabled'=>false ]); //options type 'button' (default) | 'submit' | 'link' label button text icon name with full vendor style and prefix (e.g. 'fa-solid fa-user' instead of 'fa-user' or 'user') value submitted value (if type is also set to 'submit') target '_blank' | '_self' (default) | etc onclick javascript onmouseover javascript (actually uses onmouseenter so doesn't bubble to child elements) onmouseout javascript (actually uses onmouseleave so doesn't bubble to child elements) class css class[es] style css style[s] title tooltip text (if not set, defaults to value of label) collapse overide the default hide class ('hide-md-dn') disabled boolean true/false, or a value that evaluates to a boolean //notes 1) all parameters are optional, but at least set a value for label or icon 2) overide the default hide class ('hide-md-dn') for all buttons that follow by using... button::$collapse = '...'; 3) setting either collapse (instance or default) to false (boolean) will cause the button label to always be visible //example: enable/disable buttons with javascript //javascript onclick='button_enable('disabled_button'); //button echo button::create(['type'=>'button', ... ,'id'=>'disabled_button','disabled'=>true]); //javascript onclick='button_disable('enabled_button'); //button echo button::create(['type'=>'button', ... ,'id'=>'enabled_button']); //enable button class button echo "\n"; //disable button class button echo "\n"; //note: the javascript functions above are already contained in the template.php file. */