Input field functions for templates

Source code

```break <?php /** * version $Id: tpl_inputs.php,v 1.1 2004/07/21 00:07:53 emilis Exp $ * Author: Emilis Dambauskas (emilis.d@gmail.com) * Copyright: 2003–2004 Emilis Dambauskas under {@link http://opensource.org/licenses/bsd-license.php BSD license} */

/** * HTML that is used before input label * @var string $TPL_INPUT_BEGIN * @package tpl_inputs */ $GLOBALS[‘TPL_INPUT_BEGIN’] = ‘<tr><td class="tpl_input_label">’;

/** * HTML that is used between label and field HTML * @var string $TPL_INPUT_SEPARATOR * @package tpl_inputs */ $GLOBALS[‘TPL_INPUT_SEPARATOR’] = ‘</td><td class="tpl_input_field">’;

/** * HTML that is used after field HTML * @var string $TPL_INPUT_END * @package tpl_inputs */ $GLOBALS[‘TPL_INPUT_END’] = ‘</td></tr>’;

/** * Can tpl_inputs apply smart formating to labels? * @var boolean $TPL_INPUT_END * @package tpl_inputs */ $GLOBALS[‘TPL_INPUT_SMART_LABELS’] = TRUE;

/** * Month label array * @var array $TPL_INPUT_MONTH_LABELS * @package tpl_inputs */ $GLOBALS[‘TPL_INPUT_MONTH_LABELS’] = array(‘Jan’,’Feb’,’Mar’,’Apr’,’May’,’Jun’,’Jul’,’Aug’,’Sep’,’Oct’,’Nov’,’Dec’);

/** * Generates HTML for form input * * @function tpl_input * @package tpl_inputs * @return string HTML * @param optional string $label Input field label * @param optional string $field Form field HTML * @param optional string $before_field HTML to prepend to field * @param optional string $after_field HTML to append to form field
* @use $TPL_INPUT_BEGIN
* @use $TPL_INPUT_SEPARATOR * @use $TPL_INPUT_END * @use $TPL_INPUT_SMART_LABELS */ function tpl_input($label = ‘ ’, $field = ‘ ’, $before_field = NULL, $after_field = NULL) {

global $TPL_INPUT_BEGIN, $TPL_INPUT_SEPARATOR, $TPL_INPUT_END, $TPL_INPUT_SMART_LABELS;

// apply smart label formatting
if ($TPL_INPUT_SMART_LABELS) {

    // color ending * red
    if ($label{strlen($label) - 1} == '*') {

        $label = substr($label, 0, -1);
        $label .= '&nbsp;<span style="color: red; font-weight: bold;">*</span>';
    }
}

return $TPL_INPUT_BEGIN.$label.$TPL_INPUT_SEPARATOR.$before_field.$field.$after_field.$TPL_INPUT_END; }

/** * Generates type=”text” input field HTML * * @function tpl_input_text * @return string HTML * @param string $label field label * @param string $name form field name * @param optional string $value form field value * @param optional string $extra extra attributes for form field * @param optional string $before HTML to prepend before form field * @param optional string $after HTML to append after form field
*/ function tpl_input_text($label, $name, $value = NULL, $extra = NULL, $before = NULL, $after = NULL) {

$html = '<input type="text" id="'.$name.'" name="'.$name.'" value="'.addcslashes($value,'"').'" '.$extra.'>';

return tpl_input($label, $html, $before, $after); }

/** * Generates type=”password” input field HTML * * @function tpl_input_password * @return string HTML * @param string $label field label * @param string $name form field name * @param optional string $value form field value * @param optional string $extra extra attributes for form field * @param optional string $before HTML to prepend before form field * @param optional string $after HTML to append after form field
*/ function tpl_input_password($label, $name, $value = NULL, $extra = NULL, $before = NULL, $after = NULL) {

$html = '<input type="password" id="'.$name.'" name="'.$name.'" value="'.addcslashes($value,'"').'" '.$extra.'>';

return tpl_input($label, $html, $before, $after); }

/** * Generates textarea input field HTML * * @function tpl_input_textarea * @return string HTML * @param string $label field label * @param string $name form field name * @param optional string $value form field value * @param optional string $extra extra attributes for form field * @param optional string $before HTML to prepend before form field * @param optional string $after HTML to append after form field
*/ function tpl_input_textarea($label, $name, $value = NULL, $extra = NULL, $before = NULL, $after = NULL) {

$html = '<textarea id="'.$name.'" name="'.$name.'" '.$extra.'>'.htmlspecialchars($value).'</textarea>';

return tpl_input($label, $html, $before, $after); }

/** * Generates file input field HTML * * @function tpl_input_file * @return string HTML * @param string $label field label * @param string $name form field name * @param optional string $value form field value * @param optional string $extra extra attributes for form field * @param optional string $before HTML to prepend before form field * @param optional string $after HTML to append after form field
*/ function tpl_input_file($label, $name, $value = NULL, $extra = NULL, $before = NULL, $after = NULL) {

$html = '<input type="file" id="'.$name.'" name="'.$name.'" '.$extra.'>';

return tpl_input($label, $html, $before, $after); }

/** * Generates select input field HTML * * @function tpl_input_select * @return string HTML * @param string $label field label * @param string $name form field name * @param mixed $values option value list * @param optional mixed $labels option label list * @param optional string $value form field value * @param optional string $extra extra attributes for form field * @param optional string $before HTML to prepend before form field * @param optional string $after HTML to append after form field
*/ function tpl_input_select($label, $name, $values, $labels = NULL, $value = NULL, $extra = NULL, $before = NULL, $after = NULL) {

$html = '<select id="'.$name.'" name="'.$name.'" '.$extra.'>';
$values = (array) $values;
$labels = (array) $labels;

foreach ($values as $i => $val) {

    $html .= '<option value="'.$val.'"';
    if ($val == $value)
        $html .= ' selected';
    if (sizeof($labels))
        $html .= '>'.$labels[$i].'</option>';
    else
        $html .= '>'.$val.'</option>';
}
$html .= '</select>';

return tpl_input($label, $html, $before, $after); }

/** * Generates multiple select input field HTML * * @function tpl_input_multiple * @return string HTML * @param string $label field label * @param string $name form field name * @param mixed $values option value list * @param optional mixed $labels option label list * @param optional mixed $selected selected form field values * @param optional string $all_label label for checkbox that selects all values * @param optional string $extra extra attributes for form field * @param optional string $before HTML to prepend before form field * @param optional string $after HTML to append after form field
*/ function tpl_input_multiple($label, $name, $values, $labels = NULL, $selected = NULL, $all_label = NULL, $extra = NULL, $before = NULL, $after = NULL) {

$html = '';

if ($all_label !== NULL) {

    $html .= '<input type="checkbox" onClick="el=document.getElementById(\''.$name.'\');for (i=0;i<el.length;i++) {el.options[i].selected = this.checked;}" id="all_'.$name.'_check">';
    $html .= $all_label.'<br>';
    $extra = 'onClick="el=document.getElementById(\'all_'.$name.'_check\');el.checked=false;" '.$extra;
}

$html .= '<select multiple id="'.$name.'" name="'.$name.'[]" '.$extra.'>';

foreach ($values as $i => $val) {

    $html .= '<option value="'.$val.'"';
    if (sizeof($selected) && in_array($val, $selected))
        $html .= ' selected';
    if (sizeof($labels))
        $html .= '>'.$labels[$i].'</option>';
    else
        $html .= '>'.$val.'</option>';
}
$html .= '</select>';

return tpl_input($label, $html, $before, $after); }

/** * Generates multiple radio input field HTML * * @function tpl_input_multiple * @return string HTML * @param string $label field label * @param string $name form field name * @param mixed $values option value list * @param optional mixed $labels option label list * @param optional mixed $checked selected form field value * @param optional boolean $vertical should the radio inputs be presented in vertical (default FALSE) * @param optional string $extra extra attributes for form field * @param optional string $before HTML to prepend before form field * @param optional string $after HTML to append after form field
*/ function tpl_input_radio($label, $name, $values, $labels = NULL, $checked = NULL, $vertical = FALSE, $extra = NULL, $before = NULL, $after = NULL) {

$html = '';
if ($vertical)
    $separator = '<br>';
else
    $separator = ' ';

foreach ($values as $i => $val) {

    if ($i)
        $html .= $separator;
    
    $html .= '<input type="radio" name="'.$name.'" value="'.$val.'" '.$extra;
    if ($val == $checked)
        $html .= ' checked';
    $html .= '>';
    
    if ($labels)
        $html .= $labels[$i];
    else
        $html .= $val;
}

return tpl_input($label, $html, $before, $after); }

/** * Generates date input field HTML * * @function tpl_input_date * @return string HTML * @param string $label field label * @param string $prefix form field name prefix * @param optional string $format Format for input field * @param optional mixed $value field value * @param optional mixed $default_value default field value (defaults to now, if null empty value will be preselected) * @param optional int $year_from year select box min value * @param optional int $year_to year select box max value * @param optional string $extra extra attributes for form field * @param optional string $before HTML to prepend before form field * @param optional string $after HTML to append after form field
*/ function tpl_input_date($label, $prefix, $format = ‘Y-M-d’, $value = NULL, $default_value = ‘now’, $year_from = NULL, $year_to = NULL, $extra = ‘’, $before = NULL, $after = NULL) {

$html = tpl_input_only_date($prefix, $format, $value, $default_value, $year_from, $year_to, $extra);

return tpl_input($label, $html, $before, $after); }

/** * =========================================================================== */

function tpl_input_only_date($prefix, $format = ‘Y-M-d’, $value = NULL, $default_value = ‘now’, $year_from = NULL, $year_to = NULL, $extra = NULL) {

$html = '';

// set value to timestamp
if ($value || $value === 0) {

    if (!is_numeric($value))
        $value = strtotime($value);
}
else if ($default_value !== NULL)
    $value = strtotime($default_value);
else
    $value = NULL;

// read format
$format = explode('-', $format);
foreach ($format as $i => $input_type) {

    if ($i) echo ' ';
    
    switch ($input_type) {

        case 'Y':
            $html .= tpl_input_only_year($prefix, $value, $year_from, $year_to, $extra);
        break;
        case 'd':
            $html .= tpl_input_only_day($prefix, $value, $extra);
        break;
        default:
            $html .= tpl_input_only_month($prefix, $input_type, $value, $extra);
    }
}

return $html; }

function tpl_input_only_year($prefix, $value = NULL, $year_from = NULL, $year_to = NULL, $extra = NULL) {

$html = '<select name="'.$prefix.'_year" '.$extra.'>';

// value
if ($value)
    $value = date('Y', $value);

// default year_from and year_to
if ($year_from === NULL)
    $year_from = date('Y', strtotime('last year'));
if ($year_to === NULL)
    $year_to = date('Y', strtotime('+1 year'));

$html .= '<option value=""> </option>';
for ($year = $year_from; $year <= $year_to; $year++) {

    $html .= '<option value="'.$year.'"';
    if ($year == $value)
        $html .= ' selected';
    $html .= '>'.$year.'</option>';
}
$html .= '</select> ';

return $html; }

function tpl_input_only_day($prefix, $value = NULL, $extra = NULL) {

// value
if ($value)
    $value = date('d', $value);

$html = '<select name="'.$prefix.'_day" '.$extra.'>';
$html .= '<option value=""> </option>';
for ($day = 1; $day < 32; $day++) {

    $html .= '<option value="'.$day.'"';
    if ($day == $value)
        $html .= ' selected';
    $html .= '>'.$day.'</option>';
}
$html .= '</select> ';

return $html; }

function tpl_input_only_month($prefix, $format = ‘n’, $value = NULL, $extra = NULL) {

// create months array according to format
if ($format == '*') {

    global $TPL_INPUT_MONTH_LABELS;
    $months = $TPL_INPUT_MONTH_LABELS;
} else {

    for ($i = 1; $i <= 12; $i++)
        $months[] = date($format, strtotime('2000-'.$i.'-25'));
}

// create value according to month array format
if ($value || $value === 0)
    $value = date('n',$value) - 1;

$html = '<select name="'.$prefix.'_month" '.$extra.'>';
$html .= '<option value=""> </option>';
foreach ($months as $i => $month) {

    $i++;
    $html .= '<option value="'.$i.'"';
    if ($i == $value)
        $html .= ' selected';
    $html .= '>'.$month.'</option>';
}
$html .= '</select> ';

return $html; }

function tpl_input_time_from_input($prefix, $form_fields) {

$date = '';
if (!(@$form_fields[$prefix.'_year'] && @$form_fields[$prefix.'_month'] && @$form_fields[$prefix.'_day']))
    return FALSE;

$date .= $form_fields[$prefix.'_year'].'-'.$form_fields[$prefix.'_month'].'-'.$form_fields[$prefix.'_day'];

if (@$form_fields[$prefix.'_hour'] && @$form_fields[$prefix.'_min'] && @$form_fields[$prefix.'_sec'])
    $date .= ' '.$form_fields[$prefix.'_hour'].':'.$form_fields[$prefix.'_min'].':'.$form_fields[$prefix.'_sec'];

return strtotime($date); }

```