-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.class.php
More file actions
144 lines (127 loc) · 2.66 KB
/
input.class.php
File metadata and controls
144 lines (127 loc) · 2.66 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
/**
* Input element
* ----
*/
require_once 'form/input.text.class.php';
require_once 'form/input.radio.class.php';
require_once 'form/input.checkbox.class.php';
require_once 'form/input.submit.class.php';
/*
$input = new NyaaFormInputCheckbox( );
$input->setName( 'email' );
$input->setValue( "hajime@avap.co.jp" );
$input->setId('email1');
$input->setLabel('email1');
$input->addClass('test');
$input->addStyle('color','red');
echo $input->toHtml( );
echo $input->toLabel( );
echo htmlspecialchars($input->toHtml( ));
echo htmlspecialchars($input->toLabel( ));
*/
class NyaaFormInput extends NyaaObject
{
public $value;
public $name;
public $type;
public $default;
public $classes = array();
public $styles = array();
public $template = '<input type=":type" id=":id" name=":name" value=":value" class=":class" style=":style" :etc/>';
public $label = "";
public $labelTemplate = '<label for=":id">:label</label>';
function __toString( )
{
return $this->toHtml( );
return get_class($this);
}
/**
* Input element factory
*
* option type:
* text
* radio
* checkbox
* submit
*/
static function factory( $option )
{
$file = dirname(__FILE__).'/input.'.$option['type'].'.class.php';
$class = 'NyaaFormInput'.ucfirst($option['type']);
require_once $file;
$input = new $class;
foreach( $option as $k=>$v )
{
$input->setProp($k, $v);
}
return $input;
}
function setId( $id )
{
$this->id = $id;
}
function setName( $name )
{
$this->name = $name;
}
function setValue( $value )
{
$this->value = $value;
}
function setType( $type )
{
$this->type = $type;
}
function setDefault( $value )
{
$this->default = $value;
}
function setLabel( $label )
{
$this->label = $label;
}
function addClass( $class )
{
$this->classes[] = $class;
}
function addStyle( $key, $style )
{
$this->styles[$key] = $style;
}
function setProp( $key, $value )
{
return call_user_func(array($this,'set'.ucfirst($key)), $value);
}
function getProp($name)
{
$value = isset($this->$name) ? $this->$name: null;
if( empty($value) ) switch($name)
{
case 'id':
return $this->getProp('name');
break;
case 'value':
return $this->getProp('default');
break;
case 'class':
return implode(' ', $this->classes);
break;
case 'style':
$aWork = array( );
foreach($this->styles as $k=>$v) $aWork[] = "$k:$v;";
return implode(' ', $aWork);
break;
}
return $value;
}
function toHtml( )
{
return preg_replace('/:([a-zA-Z_]+)/e', '$this->getProp("\1")', $this->template );
}
function toLabel( )
{
return preg_replace('/:([a-zA-Z_]+)/e', '$this->getProp("\1")', $this->labelTemplate );
}
}
?>