forked from spoon/library
/
hidden.php
128 lines (108 loc) · 2.82 KB
/
hidden.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
/**
* Spoon Library
*
* This source file is part of the Spoon Library. More information,
* documentation and tutorials can be found @ http://www.spoon-library.com
*
* @package spoon
* @subpackage form
*
*
* @author Davy Hellemans <davy@spoon-library.com>
* @since 0.1.1
*/
/**
* Creates hidden form element.
*
* @package spoon
* @subpackage form
*
*
* @author Davy Hellemans <davy@spoon-library.com>
* @author Dieter Vanden Eynde <dieter.vandeneynde@wijs.be>
* @since 1.0.0
*/
class SpoonFormHidden extends SpoonFormAttributes
{
/**
* Value of this hidden field
*
* @var string
*/
private $value;
/**
* Class constructor.
*
* @param string $name The name.
* @param string[optional] $value The initial value.
*/
public function __construct($name, $value = null)
{
// obligated fields
$this->attributes['id'] = SpoonFilter::toCamelCase((string) $name, '_', true);
$this->attributes['name'] = (string) $name;
// value
if($value !== null) $this->value = (string) $value;
}
/**
* Retrieve the initial or submitted value.
*
* @param bool[optional] $allowHTML Is HTML allowed?
* @return string
*/
public function getValue($allowHTML = null)
{
// redefine default value
$value = $this->value;
// added to form
if($this->isSubmitted())
{
// post/get data
$data = $this->getMethod(true);
// submitted by post/get (may be empty)
if(isset($data[$this->attributes['name']]))
{
// value
$value = $data[$this->getName()];
$value = is_scalar($value) ? (string) $value : 'Array';
if(!$allowHTML) $value = (Spoon::getCharset() == 'utf-8') ? SpoonFilter::htmlspecialchars($value) : SpoonFilter::htmlentities($value);
}
}
return $value;
}
/**
* Checks if this field has any content (except spaces).
*
* @return bool
*/
public function isFilled()
{
// post/get data
$data = $this->getMethod(true);
$value = isset($data[$this->getName()]) ? $data[$this->getName()] : '';
$value = is_array($value) ? 'Array' : trim((string) $value);
return $value != '';
}
/**
* Parses the html for this hidden field.
*
* @return string
* @param SpoonTemplate[optional] $template The template to parse the element in.
*/
public function parse($template = null)
{
// start html generation
$output = '<input type="hidden" value="' . $this->getValue(false) . '"';
// build attributes
$attributes = array();
if(isset($this->attributes['id'])) $attributes['[id]'] = $this->attributes['id'];
$attributes['[name]'] = $this->attributes['name'];
$attributes['[value]'] = $this->getValue();
// add attributes
$output .= $this->getAttributesHTML($attributes) . ' />';
// parse hidden field
if($template !== null) $template->assign('hid' . SpoonFilter::toCamelCase($this->attributes['name']), $output);
return $output;
}
}