<?PHP
/*
LookingGlass dgs-bootstraphtml.php
*/
define('dgs_void_tags', '<area><base><br><col><command><embed><hr><img><input><link><meta><param><source>');
define('dgs_CG_OPEN', '<div class="control-group">');
define('dgs_CG_CLOSE', '</div>');
define('dgs_HELP_OPEN', '<p class="help-block">');
define('dgs_HELP_CLOSE', '</p>');
$dgs_regNames = array();
function dgs_isVoidTag( $tagName ) {
return (stripos(dgs_void_tags,'<'.$element.'>')!==false);
}
function dgs_nextRegNameID( $regName ) {
global $dgs_regNames;
if(isset($dgs_regNames[$regName])) {
$dgs_regNames[$regName]++;
} else {
$dgs_regNames[$regName] = 0;
}
return $regName.$dgs_regNames[$regName];
}
function dgs_attributes($OPTIONS) {
$out = '';
foreach($OPTIONS as $parm => $val) {
if(($parm[0]!='_')&&(strtolower($parm)!='class')&&(!empty($val))) $out .= ' '.$parm.'="'.$val.'"';
}
return $out;
}
function dgs_element($type, $text, $OPTIONS=array(), $print=true) {
// create an element
// $OPTIONS = array of option settings {including but not limited to:}
// '_style' => {blank} | primary | success | info | warning | danger | inverse | link (string)
// '_size' => large | {blank} | small | mini (string)
// '_disabled' => true | false (boolean)
// '_element' => a | button | input (string)
// 'href' => {target URL} | #
// 'type' => {blank} | button | input | submit
// 'name' => {element name}
// 'id' => {element id}
// 'value' => {element value}
if ($text) {
$o = $OPTIONS;
if(!isset($o['_element'])) return false;
$element = strtolower($o['_element']);
$class = (($type!='icon')?$type:'');
$class .= ((!empty($o['_style']))?" $type-".$o['_style']:'');
$class .= ((!empty($o['_size']))?" $type-".$o['_size']:'');
$class .= (($o['_disabled']==true)?' disabled':'');
$class .= ((isset($o['class']))?' '.$o['class']:'');
$class = trim($class);
if($o['_disabled']==true)$o['disabled']='disabled';
$attr = dgs_attributes($o);
$out = vsprintf('<%1$s class="%2$s"%3$s>%4$s'.((!dgs_isVoidTag($element))?'</%1$s>':''), array(
$element,
$class,
$attr,
$text,
));
if(!empty($o['_help'])) $out.=dgs_HELP_OPEN.$o['_help'].dgs_HELP_CLOSE;
if ($print==true) {
echo $out;
return true;
} else {
return $out;
}
} else {
return false;
}
}
function dgs_button($text, $OPTIONS=array(), $print=true) {
// create a button
// $OPTIONS = array of option settings {including but not limited to:}
// '_style' => {blank} | primary | info | success | warning | danger | inverse | link
// '_size' => large | {blank} | small | mini
$o = array(
'_element' => 'button',
'type' => (empty($OPTIONS['type'])?'button':$OPTIONS['type']),
);
return dgs_element('btn', $text, array_merge($OPTIONS, $o), $print);
}
function dgs_buttonA($text, $OPTIONS=array(), $print=true) {
// create a button
// $OPTIONS = array of option settings {including but not limited to:}
// '_style' => {blank} | primary | info | success | warning | danger | inverse | link
// '_size' => large | {blank} | small | mini
$o = array(
'_element' => 'a',
'href' => (empty($OPTIONS['href'])?'#':$OPTIONS['href']),
);
return dgs_element('btn', $text, array_merge($OPTIONS, $o), $print);
}
function dgs_buttonI($text, $OPTIONS=array(), $print=true) {
// create a button
// $OPTIONS = array of option settings {including but not limited to:}
// '_style' => {blank} | primary | info | success | warning | danger | inverse | link
// '_size' => large | {blank} | small | mini
$o = array(
'_element' => 'input',
'type' => (empty($OPTIONS['type'])?'button':$OPTIONS['type']),
'value' => (empty($OPTIONS['value'])?((!empty($text))?$text:'Submit'):$OPTIONS['value']),
);
return dgs_element('btn', '', array_merge($OPTIONS, $o), $print);
}
function dgs_checkbox($name, $label, $value="1", $checked=false, $OPTIONS=array(), $print=true) {
// create a checkbox input
// wrapped in CLASS GROUP unless _inline option is true.
// $OPTIONS = array of option settings {including but not limited to:}
// _inline = false | true (boolean)
// _disabled = false | true (boolean)
// _help = {help block text, shown after element}
// id = {automatically assigned if left blank}
if(empty($name)) return false;
$o = array(
'checked' => (($checked)?'checked':''),
'value' => $value,
'id' => ((empty($OPTIONS['id']))?dgs_nextRegNameID($name):$OPTIONS['id']),
'_element' => 'input',
'type' => 'checkbox',
'name' => $name,
'class' => '',
'_help' => '',
);
$class = "checkbox".((!empty($OPTIONS['class']))?' '.$OPTIONS['class']:'').(((!empty($OPTIONS['_inline']))&&($OPTIONS['_inline']==true))?' inline':'');
$out = '<label class="'.$class.'">'.dgs_element('','', array_merge($OPTIONS, $o), false).'</label>';
if(!empty($OPTIONS['_help'])) $out.=dgs_HELP_OPEN.$OPTIONS['_help'].dgs_HELP_CLOSE;
if ((!empty($OPTIONS['_inline']))&&($OPTIONS['_inline']!=true)) $out = dgs_CG_OPEN.$out.dgs_CG_CLOSE;
if ($print) {
echo $out;
return true;
}
return $out;
}
function dgs_alert($text, $OPTIONS=array(), $print=true) {
// create a dismissable alert box
// $OPTIONS = array of option settings {including but not limited to:}
// '_style' => {blank} | "error" | "success" | "info"
$o = $OPTIONS;
$o['_size'] = ((strlen($text) > 80)?'block':'');
$o['_element'] = 'div';
$b = array(
'_element' => 'button',
'type' => 'button',
'data-dismiss' => 'alert'
);
$btn = dgs_element('close', '×', $b, false);
return dgs_element('alert', $btn.$text, $o, $print );
}
?>
preferences:
62.63 ms | 402 KiB | 5 Q