Projekt

Obecné

Profil

Stáhnout (13.5 KB) Statistiky
| Větev: | Tag: | Revize:
8baed187 Michal Kliment
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Form helper class.
*
* $Id: form.php 1970 2008-02-06 21:54:29Z Shadowhand $
*
* @package Core
* @author Kohana Team
* @copyright (c) 2007-2008 Kohana Team
* @license http://kohanaphp.com/license.html
*/
class form {

/**
* Generates an opening HTML form tag.
*
* @param string form action attribute
* @param array extra attributes
* @param array hidden fields to be created immediately after the form tag
* @return string
*/
public static function open($action = '', $attr = array(), $hidden = NULL)
{
// Make sure that the method is always set
empty($attr['method']) and $attr['method'] = 'post';

if ($attr['method'] != 'post' AND $attr['method'] != 'get')
{
// If the method is invalid, use post
$attr['method'] = 'post';
}

if (empty($action) OR ! is_string($action))
{
// Use the current URL as the default action
c1bdc1c4 Michal Kliment
$action = url::base().url::current(TRUE);
8baed187 Michal Kliment
}
elseif (strpos($action, '://') === FALSE)
{
// Make the action URI into a URL
$action = url::site($action);
}

// Form opening tag
$form = '<form action="'.$action.'"'.form::attributes($attr).'>'."\n";

// Add hidden fields immediate after opening tag
empty($hidden) or $form .= form::hidden($hidden);

return $form;
}

/**
* Generates an opening HTML form tag that can be used for uploading files.
*
* @param string form action attribute
* @param array extra attributes
* @param array hidden fields to be created immediately after the form tag
* @return string
*/
public static function open_multipart($action, $attr = array(), $hidden = array())
{
// Set multi-part form type
$attr['enctype'] = 'multipart/form-data';

return form::open($action, $attr, $hidden);
}

/**
* Generates a fieldset opening tag.
*
* @param array html attributes
* @param string a string to be attached to the end of the attributes
* @return string
*/
public static function open_fieldset($data = NULL, $extra = '')
{
if ( ! empty($extra))
{
// Make sure there is 1 space before extra
$extra = ' '.ltrim($extra);
}

return '<fieldset'.html::attributes((array) $data).$extra.'>'."\n";
}

/**
* Generates a fieldset closing tag.
*
* @return string
*/
public static function close_fieldset()
{
return '</fieldset>'."\n";
}

/**
* Generates a legend tag for use with a fieldset.
*
* @param string legend text
* @param array HTML attributes
* @param string a string to be attached to the end of the attributes
* @return string
*/
public static function legend($text = '', $data = NULL, $extra = '')
{
if ( ! empty($extra))
{
// Make sure there is 1 space before extra
$extra = ' '.ltrim($extra);
}

return '<legend'.form::attributes((array) $data).$extra.'>'.$text.'</legend>'."\n";
}

/**
* Generates hidden form fields.
* You can pass a simple key/value string or an associative array with multiple values.
*
* @param string|array input name (string) or key/value pairs (array)
* @param string input value, if using an input name
* @return string
*/
public static function hidden($data, $value = '', $extra = '')
{
if ( ! is_array($data))
{
$data = array
(
$data => $value
);
}
if ( ! empty($extra))
{
// Make sure there is 1 space before extra
$extra = ' '.ltrim($extra);
}

$input = '';
foreach($data as $name => $value)
{
$attr = array
(
'type' => 'hidden',
'name' => $name,
'value' => $value
);

$input .= form::input($attr, $value, $extra)."\n";
}

return $input;
}

/**
* Creates an HTML form input tag. Defaults to a text type.
*
* @param string|array input name or an array of HTML attributes
* @param string input value, when using a name
* @param string a string to be attached to the end of the attributes
* @return string
*/
public static function input($data, $value = '', $extra = '')
{
if ( ! is_array($data))
{
$data = array('name' => $data);
}

// Type and value are required attributes
$data += array
(
'type' => 'text',
'value' => $value
);

if ( ! empty($extra))
{
// Make sure there is 1 space before extra
$extra = ' '.ltrim($extra);
}

// For safe form data
$data['value'] = html::specialchars($data['value']);

return '<input'.form::attributes($data).$extra.' />';
}

/**
* Creates a HTML form password input tag.
*
* @param string|array input name or an array of HTML attributes
* @param string input value, when using a name
* @param string a string to be attached to the end of the attributes
* @return string
*/
public static function password($data, $value = '', $extra = '')
{
if ( ! is_array($data))
{
$data = array('name' => $data);
}

$data['type'] = 'password';

return form::input($data, $value, $extra);
}

/**
* Creates an HTML form upload input tag.
*
* @param string|array input name or an array of HTML attributes
* @param string input value, when using a name
* @param string a string to be attached to the end of the attributes
* @return string
*/
public static function upload($data, $value = '', $extra = '')
{
if ( ! is_array($data))
{
$data = array('name' => $data);
}

$data['type'] = 'file';

return form::input($data, $value, $extra);
}

/**
* Creates an HTML form textarea tag.
*
* @param string|array input name or an array of HTML attributes
* @param string input value, when using a name
* @param string a string to be attached to the end of the attributes
* @return string
*/
public static function textarea($data, $value = '', $extra = '')
{
if ( ! is_array($data))
{
$data = array('name' => $data);
}

// Use the value from $data if possible, or use $value
$value = isset($data['value']) ? $data['value'] : $value;

// Value is not part of the attributes
unset($data['value']);

if ( ! empty($extra))
{
// Make sure there is 1 space before extra
$extra = ' '.ltrim($extra);
}

return '<textarea'.form::attributes($data).$extra.'>'.html::specialchars($value).'</textarea>';
}

/**
* Creates an HTML form select tag, or "dropdown menu".
*
* @param string|array input name or an array of HTML attributes
* @param array select options, when using a name
* @param string option key that should be selected by default
* @param string a string to be attached to the end of the attributes
* @return string
*/
public static function dropdown($data, $options = array(), $selected = array(), $extra = '')
{
if ( ! is_array($data))
{
$data = array('name' => $data);
}

if ( ! empty($extra))
{
// Make sure there is 1 space before extra
$extra = ' '.ltrim($extra);
}

// Selected value should always be a string
if (!is_array($selected))
$selected = array($selected);

$input = '<select'.form::attributes($data).$extra.'>'."\n";
foreach ($options as $key => $val)
{
// Key should always be a string
$key = (string) $key;

if (is_array($val))
{
$input .= '<optgroup label="'.$key.'">'."\n";
foreach ($val as $inner_key => $inner_val)
{
// Inner key should always be a string
$inner_key = (string) $inner_key;

$sel = (in_array($inner_key,$selected)) ? ' selected="selected"' : '';
$input .= '<option value="'.$inner_key.'"'.$sel.'>'.$inner_val.'</option>'."\n";
}
$input .= '</optgroup>'."\n";
}
else
{
$sel = (in_array($key,$selected)) ? ' selected="selected"' : '';
$input .= '<option value';
if ($key!='')
$input.= '="'.$key.'"';
$input .= $sel.'>'.$val.'</option>'."\n";
}
}
$input .= '</select>';

return $input;
}

/**
* Creates an HTML form checkbox input tag.
*
* @param string|array input name or an array of HTML attributes
* @param string input value, when using a name
* @param boolean make the checkbox checked by default
* @param string a string to be attached to the end of the attributes
* @return string
*/
public static function checkbox($data, $value = '', $checked = FALSE, $extra = '')
{
if ( ! is_array($data))
{
$data = array('name' => $data);
}

$data['type'] = 'checkbox';

if ($checked == TRUE OR (isset($data['checked']) AND $data['checked'] == TRUE))
{
$data['checked'] = 'checked';
}
else
{
unset($data['checked']);
}

return form::input($data, $value, $extra);
}

/**
* Creates an HTML form radio input tag.
*
* @param string|array input name or an array of HTML attributes
* @param string input value, when using a name
* @param boolean make the radio selected by default
* @param string a string to be attached to the end of the attributes
* @return string
*/
public static function radio($data = '', $value = '', $checked = FALSE, $extra = '')
{
if ( ! is_array($data))
{
$data = array('name' => $data);
}

$data['type'] = 'radio';

if ($checked == TRUE OR (isset($data['checked']) AND $data['checked'] == TRUE))
{
$data['checked'] = 'checked';
}
else
{
unset($data['checked']);
}

return form::input($data, $value, $extra);
}

/**
* Creates an HTML form submit input tag.
*
* @param string|array input name or an array of HTML attributes
* @param string input value, when using a name
* @param string a string to be attached to the end of the attributes
* @return string
*/
public static function submit($data = '', $value = '', $extra = '')
{
if ( ! is_array($data))
{
$data = array('name' => $data);
}

$data['type'] = 'submit';

return form::input($data, $value, $extra);
}

/**
* Creates an HTML form imagebutton input tag.
*
* @param string|array input name or an array of HTML attributes
* @param string source of image
* @param string a string to be attached to the end of the attributes
* @return string
*/
public static function imagebutton($data = '', $src = '', $extra = '')
{
if ( ! is_array($data))
{
$data = array('name' => $data);
}

$data += array
(
'type' => 'image',
'src' => $src
);


return '<input '.form::attributes($data).form::attributes($extra).' />';
}

/**
* Creates an HTML form button input tag.
*
* @param string|array input name or an array of HTML attributes
* @param string input value, when using a name
* @param string a string to be attached to the end of the attributes
* @return string
*/
public static function button($data = '', $value = '', $extra = '')
{
if ( ! is_array($data))
{
$data = array('name' => $data);
}

$data += array
(
'type' => 'button'
);

if (isset($data['value']))
{
$value = arr::remove('value', $data);
}

if ( ! empty($extra))
{
// Make sure there is 1 space before extra
$extra = ' '.ltrim($extra);
}

return '<button'.form::attributes($data).$extra.'>'.html::specialchars($value).'</button>';
}

/**
* Closes an open form tag.
*
* @param string string to be attached after the closing tag
* @return string
*/
public static function close($extra = '')
{
return '</form>'."\n".$extra;
}

/**
* Creates an HTML form label tag.
*
* @param string|array label "for" name or an array of HTML attributes
* @param string label text or HTML
* @param string a string to be attached to the end of the attributes
* @return string
*/
public static function label($data = '', $text = '', $extra = '')
{
if ( ! is_array($data))
{
if (strpos($data, '[') !== FALSE)
{
$data = preg_replace('/\[.*\]/', '', $data);
}

$data = empty($data) ? array() : array('for' => $data);
}

if ( ! empty($extra))
{
// Make sure there is 1 space before extra
$extra = ' '.ltrim($extra);
}

return '<label'.form::attributes($data).$extra.'>'.$text.'</label>';
}

/**
* Sorts a key/value array of HTML attributes, putting form attributes first,
* and returns an attribute string.
*
* @param array HTML attributes array
* @return string
*/
public static function attributes($attr)
{
if (empty($attr))
return '';
//napovedu odstranime - nechceme ji renderovat v inputu...
unset($attr['help']);
unset($attr['additional_info']);

if ( ! empty($attr['name']) AND
(
(strpos($attr['name'], '[') === FALSE) OR
(preg_match("/^\w+\[\]$/", $attr['name']))
) AND empty($attr['id']))
{
$attr['id'] = str_replace('[]', '', $attr['name']);
}

$order = array
(
'type',
'id',
'name',
'value',
'src',
'size',
'maxlength',
'rows',
'cols',
'accept',
'tabindex',
'accesskey',
'align',
'alt',
'title',
'class',
'style',
'selected',
'checked',
'readonly',
'disabled'
);

$sorted = array();
foreach($order as $key)
{
if (isset($attr[$key]))
{
$sorted[$key] = $attr[$key];
}
}

$sorted = array_merge($sorted, $attr);

return html::attributes($sorted);
}
/**
* Creates hidden input names and values from input array
*
* @author Michal Kliment
* @param type $array
* @param type $input
* @param type $prefix
*/
public static function create_hiddens (&$array, $input, $prefix)
{
foreach ($input as $key => $val)
{
$name = $prefix !='' ? $prefix."[".$key."]" : $key;
if (!is_array($val))
{
$array[] = array
(
'name' => $name,
'value' => $val
);
}
else
self::create_hiddens ($array, $val, $name);
}
}

} // End form