* Copyright © 2014 Strangecode, LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
/*
* HTML()
*
* Tools for building HTML from PHP data.
*
* @author Quinn Comendant
* @version 1.0
* @since 11 Sep 2014 21:08:06
*
* Example of use:
---------------------------------------------------------------------
echo HTML::buttons(array(
array('name' => 'submit', 'value' => _("Save changes"), 'class' => 'small button', 'accesskey' => 's'),
array('name' => 'reset', 'value' => _("Reset"), 'class' => 'small button secondary', 'accesskey' => 'r'),
array('name' => 'cancel', 'value' => _("Cancel"), 'class' => 'small button secondary', 'accesskey' => 'c'),
));
---------------------------------------------------------------------
*/
class HTML {
/**
* Prints submit buttons based on given array of submit button names and titles. If the array includes an 'href' key, the
* button is created using a otherwise an is used.
*
* @access public
* @param array $buttons Array of buttons, the key being the button name, and value being the title of the button.
* @return void
* @author Quinn Comendant
* @version 1.0
* @since 12 Sep 2014 00:17:38
*/
static public function printButtons($buttons=array(), $class='button-group')
{
if (!isset($buttons[0]) || !is_array($buttons[0])) {
$app =& App::getInstance();
$app->logMsg(sprintf('Incorrect parameters passed to HTML::buttons(): %s', getDump($buttons)), LOG_DEBUG, __FILE__, __LINE__);
return false;
}
if (empty($buttons)) {
return '';
}
?> $b) {
$defaults = array();
$defaults['type'] = isset($b['type']) ? $b['type'] : 'submit';
$b = array_merge($defaults, $b);
if (isset($b['href'])) {
echo '- null)) as $key => $value) {
printf(' %s="%s"', $key, oTxt($value));
}
echo '>' . oTxt($b['value']) . '
';
} else if (isset($b['name'])) {
$defaults['id'] = isset($b['id']) ? $b['id'] : sprintf('sc-%s-button', $b['name']);
echo '- $value) {
printf(' %s="%s"', $key, oTxt($value));
}
echo ' />
';
} else {
$app->logMsg(sprintf('Button missing name or href: %s', getDump($b)), LOG_ERR, __FILE__, __LINE__);
continue;
}
}
?>
* @version 1.0
* @since 12 Sep 2014 11:43:23
*/
static public function getSelectOptions($db_table, $key_column, $val_column, $preselected, $first=false, $extra_clause='', $sql_format='SELECT %1$s, %2$s FROM %3$s %4$s')
{
$db =& DB::getInstance();
// Sometimes preselected comes as a comma list.
if (!is_array($preselected)) {
$preselected = array($preselected);
}
$options = array();
if (true === $first) {
// Include a blank first option.
$options[] = array(
'value' => '',
'selected' => in_array('', $preselected),
'text' => '',
);
} else if (is_array($first)) {
// When the 'blank' first option needs a specific key->val pair.
foreach ($first as $key => $val) {
$options[] = array(
'value' => $key,
'selected' => in_array($key, $preselected),
'text' => $val,
);
}
}
$db =& DB::getInstance();
$qid = $db->query(sprintf($sql_format, $key_column, $val_column, $db_table, $extra_clause), false);
while ($row = mysql_fetch_assoc($qid)) {
$options[] = array(
'value' => $row[$val_column],
'selected' => in_array($row[$val_column], $preselected),
'text' => $row[$key_column],
);
}
return $options;
}
/**
* Prints option fields for a select form. Works only with enum or set
* data types in table columns.
*
* @param string $db_table Database table to lookup
* @param string $db_col Database column to lookup
* @param string $preselected The currently selected value of the menu. compared to the $val_column
* @param bool $first Optional first item; set true for a blank item, array for item with name and value.
* @param bool $sort Sort the output.
*/
static public function getSelectOptionsEnum($db_table, $db_col, $preselected, $first=false, $sort=false)
{
// Sometimes preselected comes as a comma list.
if (!is_array($preselected)) {
$preselected = array($preselected);
}
$options = array();
if (true === $first) {
// Include a blank first option.
$options[] = array(
'value' => '',
'selected' => in_array('', $preselected),
'text' => '',
);
} else if (is_array($first)) {
// When the 'blank' first option needs a specific key->val pair.
foreach ($first as $key => $val) {
$options[] = array(
'value' => $key,
'selected' => in_array($key, $preselected),
'text' => $val,
);
}
}
$db =& DB::getInstance();
$values = $db->getEnumValues($db_table, $db_col, $sort);
foreach ($values as $v) {
$options[] = array(
'value' => $v,
'selected' => in_array($v, $preselected),
'text' => $v,
);
}
return $options;
}
/**
* Prints a select menu containing the specified values and keys of a table.
*
*/
static public function printSelectOptions($options)
{
if (!isset($options) || !is_array($options)) {
$app =& App::getInstance();
$app->logMsg(sprintf('Incorrect parameters passed to HTML::printSelectOptions(): %s', getDump($options)), LOG_DEBUG, __FILE__, __LINE__);
return false;
}
if (empty($options)) {
return '';
}
foreach ($options as $o) {
printf('',
oTxt($o['value']),
($o['selected'] ? ' selected' : ''),
oTxt($o['text'])
);
}
}
}