Project

General

Profile

« Previous | Next » 

Revision 1192840b

Added by Sjon Hortensius over 10 years ago

refactor attributes from Input to Element to make it a bit more generic

  • replaced Input::setAttribute with explicit setReadonly/setDisabled
  • introduced Input::toggles

fixes #37, refs #21

View differences:

usr/local/www/classes/Form.class.php
34 34

  
35 35
class Form extends Form_Element
36 36
{
37
	protected $_tagName = 'form';
38
	protected $_attributes = array(
39
		'class' => array('form-horizontal' => true),
40
		'method' => 'post',
41
		// Empty is interpreted by all browsers to submit to the current URI
42
		'action' => '',
43
	);
37 44
	protected $_sections = array();
38 45
	protected $_global = array();
39 46
	protected $_labelWidth = 2;
40 47

  
41 48
	public function __construct()
42 49
	{
43
		$this->addClass('form-horizontal');
44
		$this->setAttribute('method', 'post');
45

  
46 50
		$this->addGlobal(new Form_Button(
47 51
			'save',
48 52
			'Save'
49 53
		));
50

  
51
	return $this;
52 54
	}
53 55

  
54 56
	public function add(Form_Section $section)
......
67 69
		$this->_labelWidth = (int)$size;
68 70
	}
69 71

  
70
	public function setAction($uri)
72
	public function setAction($url)
71 73
	{
72
		$this->setAttribute('action', $uri);
74
		$this->_attributes['action'] = $url;
73 75

  
74 76
		return $this;
75 77
	}
......
93 95

  
94 96
	public function __toString()
95 97
	{
98
		$element = parent::__toString();
96 99
		$html = implode('', $this->_sections);
97 100

  
98 101
		if (isset($this->_submit))
......
105 108
		$html .= implode('', $this->_global);
106 109

  
107 110
		return <<<EOT
108
	<form {$this->getHtmlAttribute()}>
111
	{$element}
109 112
		{$html}
110 113
	</form>
111 114
EOT;
112 115
	}
113
}
116
}
usr/local/www/classes/Form/Button.class.php
28 28
*/
29 29
class Form_Button extends Form_Input
30 30
{
31
	protected $_tagSelfClosing = false;
32
	protected $_attributes = array(
33
		'class' => array(
34
			'btn' => true,
35
		),
36
		'type' => 'submit',
37
	);
38

  
31 39
	public function __construct($name, $title, $link = null)
32 40
	{
33 41
		// If we have a link; we're actually an <a class='btn'>
34
		if (isset($link)) {
35
			$this->setAttribute('href', htmlspecialchars($link));
42
		if (isset($link))
43
		{
44
			$this->_attributes['href'] = $link;
45
			$this->_tagName = 'a';
36 46
			$this->addClass('btn-default');
37
			$type = null;
38
		} else {
47
			unset($this->_attributes['type']);
48
		}
49
		else
50
		{
51
			$this->_tagSelfClosing = true;
52
			$this->_attributes['value'] = $title;
39 53
			$this->addClass('btn-primary');
40
			$this->setAttribute('value', $title);
41
			$type = 'submit';
42 54
		}
43 55

  
44
		parent::__construct($name, $title, $type);
45

  
46
		$this->removeClass('form-control')->addClass('btn');
56
		parent::__construct($name, $title, null);
47 57
	}
48 58

  
49 59
	protected function _getInput()
50 60
	{
51
		if (empty($this->getAttribute('href'))) {
52
			return parent::_getInput();
53
		}
61
		$input = parent::_getInput();
54 62

  
55
		$element = preg_replace('~^<input(.*)/>$~', 'a\1', parent::_getInput());
63
		if (!isset($this->_attributes['href']))
64
			return $input;
56 65

  
57
		return <<<EOT
58
	<{$element}>{$this->_title}</a>
59
EOT;
66
		return $input . htmlspecialchars($this->_title) .'</a>';
60 67
	}
61
}
68
}
usr/local/www/classes/Form/Checkbox.class.php
29 29

  
30 30
class Form_Checkbox extends Form_Input
31 31
{
32
	protected $_attributes = array(
33
		'class' => array('checkbox' => true),
34
	);
32 35
	protected $_description;
33 36

  
34 37
	public function __construct($name, $title, $description, $checked, $value = 'yes')
......
36 39
		parent::__construct($name, $title, 'checkbox', $value);
37 40

  
38 41
		$this->_description = $description;
39
		$this->removeClass('form-control');
40
		$this->addColumnClass('checkbox');
41 42

  
42
		if ($checked) {
43
			$this->setAttribute('checked', 'checked');
44
		}
43
		if ($checked)
44
			$this->_attributes['checked'] = 'checked';
45 45
	}
46 46

  
47 47
	public function displayAsRadio()
48 48
	{
49
		return $this->setAttribute('type', 'radio');
49
		return $this->_attributes['type'] = 'radio';
50 50
	}
51 51

  
52 52
	protected function _getInput()
......
58 58

  
59 59
		return '<label>'. $input .' '. gettext($this->_description) .'</label>';
60 60
	}
61
}
61
}
usr/local/www/classes/Form/Element.class.php
27 27
	POSSIBILITY OF SUCH DAMAGE.
28 28
*/
29 29

  
30
class Form_Element
30
abstract class Form_Element
31 31
{
32
	protected $_attributes = array('class' => array());
32
	protected $_tagName;
33
	protected $_tagSelfClosing = false;
34
	protected $_attributes = array(
35
		'class' => array()
36
	);
33 37
	protected $_parent;
34 38

  
35 39
	public function addClass()
36 40
	{
37
		foreach (func_get_args() as $class) {
41
		foreach (func_get_args() as $class)
38 42
			$this->_attributes['class'][$class] = true;
39
		}
40 43

  
41 44
		return $this;
42 45
	}
......
48 51
		return $this;
49 52
	}
50 53

  
51
	public function getClasses()
52
	{
53
		return implode(' ', array_keys($this->getAttribute('class')));
54
	}
55

  
56
	public function setAttribute($key, $value = null)
57
	{
58
		$this->_attributes[$key] = $value;
59

  
60
		return $this;
61
	}
62

  
63
	public function getAttribute($name)
54
	public function __toString()
64 55
	{
65
		return $this->_attributes[$name];
66
	}
67

  
68
	public function removeAttribute($name)
69
	{
70
		unset($this->_attributes[$name]);
71

  
72
		return $this;
73
	}
74

  
75
	public function getHtmlAttribute()
76
	{
77
		/* Will overwright _attributes['class'] with string Therefore you cannot 
78
		 * delete or add classes once getHtmlAttribute() has been called. */
79
		if (empty($this->_attributes['class'])) {
80
			$this->removeAttribute('class');
81
		} else {
82
			$this->_attributes['class'] = $this->getClasses();
83
		}
84

  
85 56
		$attributes = '';
86
		foreach ($this->_attributes as $key => $value) {
87
			$attributes .= ' ' . $key . (isset($value) ? '="' . htmlspecialchars($value) . '"' : '');
57
		foreach ($this->_attributes as $key => $value)
58
		{
59
			if (is_array($value))
60
			{
61
				// Used for classes. If it's empty, we don't want the attribute at all
62
				if (!empty($value))
63
					$value = implode(' ', array_keys($value));
64
				else
65
					$value = null;
66
			}
67

  
68
			if ($value === null)
69
				continue;
70

  
71
			$attributes .= ' '. $key;
72
			if ($value !== true)
73
				$attributes .= '="' . htmlspecialchars($value) . '"';
88 74
		}
89 75

  
90
		return $attributes;
76
		return '<'. $this->_tagName . $attributes . ($this->_tagSelfClosing ? '/' : '') .'>';
91 77
	}
92 78

  
93 79
	protected function _setParent(Form_Element $parent)
94 80
	{
95 81
		$this->_parent = $parent;
96 82
	}
97
}
83
}
usr/local/www/classes/Form/Group.class.php
28 28
*/
29 29
class Form_Group extends Form_Element
30 30
{
31
	protected $_tagName = 'div';
32
	protected $_attributes = array(
33
		'class' => array('form-group' => true),
34
	);
31 35
	protected $_title;
32 36
	protected $_inputs = array();
33 37
	protected $_labelTarget;
......
36 40
	public function __construct($title)
37 41
	{
38 42
		$this->_title = gettext($title);
39
		$this->addClass('form-group');
40

  
41
		return $this;
42 43
	}
43 44

  
44 45
	public function add(Form_Input $input)
......
73 74

  
74 75
	public function __toString()
75 76
	{
77
		$element = parent::__toString();
78

  
76 79
		// Automatically determine width for inputs without explicit set
77 80
		$spaceLeft = 12 - $this->getLabelWidth();
78 81
		$missingWidth = array();
......
90 93
		foreach ($missingWidth as $input)
91 94
			$input->setWidth($spaceLeft / count($missingWidth));
92 95

  
93
		$target = $this->_labelTarget->getAttribute('name');
96
		$target = $this->_labelTarget->getName();
94 97
		$inputs = implode('', $this->_inputs);
95 98
		$help = isset($this->_help) ? '<div class="col-sm-'. (12 - $this->getLabelWidth()) .' col-sm-offset-'. $this->getLabelWidth() .'"><span class="help-block">'. gettext($this->_help). '</span></div>' : '';
96 99

  
97 100
		return <<<EOT
98
	<div {$this->getHtmlAttribute()}>
101
	{$element}
99 102
		<label for="{$target}" class="col-sm-{$this->getLabelWidth()} control-label">
100 103
			{$this->_title}
101 104
		</label>
......
104 107
	</div>
105 108
EOT;
106 109
	}
107
}
110
}
usr/local/www/classes/Form/Input.class.php
28 28
*/
29 29
class Form_Input extends Form_Element
30 30
{
31
	protected $_tagName = 'input';
32
	protected $_tagSelfClosing = true;
33
	protected $_attributes = array(
34
		'class' => array('form-control' => true),
35
		'name' => null,
36
		'id' => null,
37
		'title' => null,
38
	);
31 39
	protected $_title;
32 40
	protected $_help;
33 41
	protected $_helpParams = array();
......
36 44

  
37 45
	public function __construct($name, $title, $type = 'text', $value = null, array $attributes = array())
38 46
	{
39
		$this->setAttribute('name', $name);
40
		$this->setAttribute('id', $name);
41
		$this->_title = htmlspecialchars($title);
42
		$this->addClass('form-control');
47
		$this->_attributes['name'] = $name;
48
		$this->_attributes['id'] = $name;
49
		$this->_title = $title;
43 50

  
44
		if (isset($type)) {
45
			$this->setAttribute('type', $type);
46
		}
47

  
48
		if (isset($value)) {
49
			$this->setAttribute('value', $value);
50
		}
51
		if (isset($type))
52
			$this->_attributes['type'] = $type;
51 53

  
52
		foreach($attributes as $name => $value) {
53
			$this->setAttribute($name, $value);
54
		}
54
		if (isset($value))
55
			$this->_attributes['value'] = $value;
55 56

  
56
		return $this;
57
		foreach ($attributes as $name => $value)
58
			$this->_attributes[$name] = $value;
57 59
	}
58 60

  
59 61
	public function getTitle()
......
61 63
		return $this->_title;
62 64
	}
63 65

  
66
	public function getName()
67
	{
68
		return $this->_attributes['name'];
69
	}
70

  
64 71
	public function setHelp($help, array $params = array())
65 72
	{
66 73
		$this->_help = $help;
......
76 83

  
77 84
	public function setWidth($size)
78 85
	{
79
		if ($size < 1 || $size > 12) {
86
		if ($size < 1 || $size > 12)
80 87
			throw new Exception('Incorrect size, pass a number between 1 and 12');
81
		}
82 88

  
83 89
		$this->removeColumnClass('col-sm-'. $this->_columnWidth);
84 90

  
......
111 117
		return 'class="'. implode(' ', array_keys($this->_columnClasses)).'"';
112 118
	}
113 119

  
120
	public function setReadonly()
121
	{
122
		$this->_attributes['readonly'] = 'readonly';
123
	}
124

  
125
	public function setDisabled()
126
	{
127
		$this->_attributes['disabled'] = 'disabled';
128
	}
129

  
130
	public function toggles($selector, $type = 'collapse')
131
	{
132
		$this->_attributes['data-target'] = $selector;
133
		$this->_attributes['data-toggle'] = $type;
134
	}
135

  
114 136
	protected function _getInput()
115 137
	{
116
		return "<input{$this->getHtmlAttribute()}/>";
138
		return parent::__toString();
117 139
	}
118 140

  
119 141
	public function __toString()
120 142
	{
121 143
		$input = $this->_getInput();
122 144

  
123
		if (isset($this->_help)) {
145
		// No classes => no element. This is useful for global inputs
146
		if (!isset($this->_help) && empty($this->_columnClasses))
147
			return (string)$input;
148

  
149
		if (isset($this->_help))
150
		{
124 151
			$help = gettext($this->_help);
125 152

  
126 153
			if (!empty($this->_helpParams))
......
128 155

  
129 156
			$help = '<span class="help-block">'. $help .'</span>';
130 157

  
131
		} else {
132
			$columnClass = $this->getColumnHtmlClass();
133

  
134
			// No classes => no element. This is useful for global inputs
135
			if (empty($columnClass))
136
				return (string)$input;
137 158
		}
138 159

  
139 160
		return <<<EOT
140 161
	<div {$this->getColumnHtmlClass()}>
141 162
		{$input}
163

  
142 164
		{$help}
143 165
	</div>
144 166
EOT;
145 167
	}
146
}
168
}
usr/local/www/classes/Form/Section.class.php
28 28
*/
29 29
class Form_Section extends Form_Element
30 30
{
31
	protected $_tagName = 'div';
32
	protected $_attributes = array(
33
		'class' => array(
34
			'panel' => true,
35
			'panel-default' => true,
36
		),
37
	);
31 38
	protected $_title;
32 39
	protected $_groups = array();
33 40

  
34 41
	public function __construct($title)
35 42
	{
36 43
		$this->_title = $title;
37
		$this->addClass('panel', 'panel-default');
38

  
39
		return $this;
40 44
	}
41 45

  
42 46
	public function add(Form_Group $group)
......
66 70

  
67 71
	public function __toString()
68 72
	{
73
		$element = parent::__toString();
69 74
		$title = gettext($this->_title);
70 75
		$body = implode('', $this->_groups);
71 76

  
72 77
		return <<<EOT
73
	<div {$this->getHtmlAttribute()}>
78
	{$element}
74 79
		<div class="panel-heading">
75 80
			<h2 class="panel-title">{$title}</h2>
76 81
		</div>
......
80 85
	</div>
81 86
EOT;
82 87
	}
83
}
88
}
usr/local/www/classes/Form/Select.class.php
29 29

  
30 30
class Form_Select extends Form_Input
31 31
{
32
	protected $_tagName = 'select';
32 33
	protected $_values;
33 34
	protected $_value;
34 35

  
35 36
	public function __construct($name, $title, $value, array $values, $allowMultiple = false)
36 37
	{
37
		if ($allowMultiple) {
38
			$this->setAttribute('multiple', 'multiple');
39
			$name = $name . '[]';
40
		}
38
		if ($allowMultiple)
39
			$name .= '[]';
41 40

  
42 41
		parent::__construct($name, $title, null);
43 42

  
43
		if ($allowMultiple)
44
			$this->_attributes['multiple'] = 'multiple';
45

  
44 46
		$this->_value = $value;
45 47
		$this->_values = $values;
46 48
	}
47 49

  
48 50
	protected function _getInput()
49 51
	{
50
		$element = preg_replace('~^<input(.*)/>$~', 'select\1', parent::_getInput());
52
		$element = parent::_getInput();
51 53

  
52 54
		$options = '';
53
		foreach ($this->_values as $value => $name) {
54
			$selected = (is_array($this->_value) && array_key_exists($value, $this->_value) || $this->_value == $value);
55
		foreach ($this->_values as $value => $name)
56
		{
57
			if (isset($this->_attributes['multiple']))
58
				$selected = in_array($value, (array)$this->_value);
59
			else
60
				$selected = ($this->_value == $value);
61

  
55 62
			$options .= '<option value="'. htmlspecialchars($value) .'"'.($selected ? ' selected' : '').'>'. gettext($name) .'</option>';
56 63
		}
57 64

  
58 65
		return <<<EOT
59
	<{$element}>
66
	{$element}
60 67
		{$options}
61 68
	</select>
62 69
EOT;
63 70
	}
64
}
71
}
usr/local/www/classes/Form/Textarea.class.php
28 28
*/
29 29
class Form_Textarea extends Form_Input
30 30
{
31
	protected $_tagName = 'textarea';
31 32
	protected $_value;
33
	protected $_attributes = array(
34
		'rows' => 5,
35
	);
32 36

  
33 37
	public function __construct($name, $title, $value)
34 38
	{
35 39
		parent::__construct($name, $title, null);
36 40

  
37 41
		$this->_value = $value;
38
		$this->setAttribute('rows', 5);
39 42
	}
40 43

  
41 44
	protected function _getInput()
42 45
	{
43
		$element = preg_replace('~^<input(.*)/>$~', 'textarea\1', parent::_getInput());
46
		$element = parent::_getInput();
44 47

  
45 48
		return <<<EOT
46
	<{$element}>
49
	{$element}
47 50
		{$options}
48 51
	</textarea>
49 52
EOT;
usr/local/www/system_advanced_admin.php
281 281
	'HTTP',
282 282
	($pconfig['webguiproto']=='http'),
283 283
	'http'
284
))->displayAsRadio()->setAttribute('data-toggle', 'collapse')->setAttribute('data-target', '#ssl-certificate');
284
))->displayAsRadio()->toggles('#ssl-certificate');
285 285

  
286 286
$group->add($input = new Form_Checkbox(
287 287
	'protocol',
......
289 289
	'HTTPS',
290 290
	($pconfig['webguiproto']=='https'),
291 291
	'https'
292
))->displayAsRadio()->setAttribute('data-toggle', 'collapse')->setAttribute('data-target', '#ssl-certificate');
292
))->displayAsRadio()->toggles('#ssl-certificate');
293 293

  
294 294
$section->add($group);
295 295

  
296 296
if (!$certs_available)
297 297
{
298
	$input->setAttribute('disabled', 'disabled');
298
	$input->setDisabled();
299 299
	$input->setHelp('No Certificates have been defined. You must '.
300 300
	'<a href="system_certmanager.php">'. gettext("Create or Import").'</a> '.
301 301
	'a Certificate before SSL can be enabled.');
usr/local/www/system_authservers.php
378 378
));
379 379

  
380 380
if ($act == 'edit')
381
	$input->setAttribute('readonly', 'readonly');
381
	$input->setReadonly();
382 382

  
383 383
$section->addInput($input = new Form_Select(
384 384
	'type',
385 385
	'Type',
386 386
	$pconfig['type'],
387 387
	$auth_server_types
388
))->setAttribute('data-toggle', 'collapse')->setAttribute('data-target', '.toggle-type');
388
))->toggles('.toggle-type');
389 389

  
390 390
if ($act == 'edit')
391
	$input->setAttribute('disabled', 'disabled');
391
	$input->setDisabled();
392 392

  
393 393
$form->add($section);
394 394
$section = new Form_Section('LDAP Server Settings');
......
486 486
	'Enable Extended Query',
487 487
	null,
488 488
	$pconfig['ldap_extended_enabled']
489
))->setAttribute('data-toggle', 'collapse')->setAttribute('data-target', '.toggle-extended');
489
))->toggles('.toggle-extended');
490 490

  
491 491
$group->add(new Form_Input(
492 492
	'ldap_extended_query',
......
501 501
	'Bind anonymous',
502 502
	'Use anonymous binds to resolve distinguished names',
503 503
	$pconfig['ldap_anon']
504
))->setAttribute('data-toggle', 'collapse')->setAttribute('data-target', '.toggle-anon');
504
))->toggles('.toggle-anon');
505 505

  
506 506
$group = new Form_Group('Bind credentials');
507 507
$group->addClass('toggle-anon collapse');
usr/local/www/system_groupmanager.php
280 280
));
281 281

  
282 282
if ($pconfig['gtype'] == "system")
283
	$input->setAttribute('readonly', 'readonly');
283
	$input->setReadonly();
284 284

  
285 285
$section->addInput(new Form_Input(
286 286
	'description',
usr/local/www/system_groupmanager_addprivs.php
157 157
	$a_group['priv'],
158 158
	$priv_list,
159 159
	true
160
))->setHelp('Hold down CTRL (pc)/COMMAND (mac) key to select')->setAttribute('size', 30);
160
))->setHelp('Hold down CTRL (pc)/COMMAND (mac) key to select');
161 161

  
162 162
$form->add($section);
163 163

  
usr/local/www/system_usermanager.php
432 432
));
433 433

  
434 434
if ($ro)
435
	$input->setAttribute('disabled', 'disabled');
435
	$input->setDisabled();
436 436

  
437 437
$group = new Form_Group('Password');
438 438
$group->add(new Form_Input(
......
456 456
))->setHelp('User\'s full name, for your own information only');
457 457

  
458 458
if ($ro)
459
	$input->setAttribute('disabled', 'disabled');
459
	$input->setDisabled();
460 460

  
461 461
$section->addInput(new Form_Input(
462 462
	'expires',
......
498 498
		if ($priv['group'])
499 499
		{
500 500
			$group = new Form_Group('Inherited from '. $priv['group']);
501
			$input->setAttribute('disabled', 'disabled');
501
			$input->setDisabled();
502 502
		}
503 503
		else
504 504
			$group = new Form_Group('Revoke privilege');
usr/local/www/vpn_ipsec_mobile.php
388 388
	'Virtual Address Pool',
389 389
	'Provide a virtual IP address to clients',
390 390
	$pconfig['pool_enable']
391
))->setAttribute('data-toggle', 'collapse')->setAttribute('data-target', '.toggle-pool_enable');
391
))->toggles('.toggle-pool_enable');
392 392

  
393 393
// TODO: Refactor this manual setup
394 394
$group = new Form_Group('');
......
437 437
	'DNS Default Domain',
438 438
	'Provide a default domain name to clients',
439 439
	$pconfig['dns_domain_enable']
440
))->setAttribute('data-toggle', 'collapse')->setAttribute('data-target', '.toggle-dns_domain');
440
))->toggles('.toggle-dns_domain');
441 441

  
442 442
$group = new Form_Group('');
443 443
$group->addClass('toggle-dns_domain collapse');
......
459 459
	'Split DNS',
460 460
	'Provide a list of split DNS domain names to clients. Enter a space separated list.',
461 461
	$pconfig['dns_split_enable']
462
))->setAttribute('data-toggle', 'collapse')->setAttribute('data-target', '.toggle-dns_split');
462
))->toggles('.toggle-dns_split');
463 463

  
464 464
$group = new Form_Group('');
465 465
$group->addClass('toggle-dns_split collapse');
......
481 481
	'DNS Servers',
482 482
	'Provide a DNS server list to clients',
483 483
	$pconfig['dns_server_enable']
484
))->setAttribute('data-toggle', 'collapse')->setAttribute('data-target', '.toggle-dns_server_enable');
484
))->toggles('.toggle-dns_server_enable');
485 485

  
486 486
for ($i = 1; $i <= 4; $i++)
487 487
{
......
506 506
	'WINS Servers',
507 507
	'Provide a WINS server list to clients',
508 508
	$pconfig['wins_server_enable']
509
))->setAttribute('data-toggle', 'collapse')->setAttribute('data-target', '.toggle-wins_server_enable');
509
))->toggles('.toggle-wins_server_enable');
510 510

  
511 511
for ($i = 1; $i <= 2; $i++)
512 512
{
......
532 532
	'Phase2 PFS Group',
533 533
	'Provide the Phase2 PFS group to clients ( overrides all mobile phase2 settings )',
534 534
	$pconfig['pfs_group_enable']
535
))->setAttribute('data-toggle', 'collapse')->setAttribute('data-target', '.toggle-pfs_group');
535
))->toggles('.toggle-pfs_group');
536 536

  
537 537
$group = new Form_Group('Group');
538 538
$group->addClass('toggle-pfs_group collapse');
......
554 554
	'Login Banner',
555 555
	'Provide a login banner to clients',
556 556
	$pconfig['login_banner_enable']
557
))->setAttribute('data-toggle', 'collapse')->setAttribute('data-target', '.toggle-login_banner');
557
))->toggles('.toggle-login_banner');
558 558

  
559 559
$group = new Form_Group('');
560 560
$group->addClass('toggle-login_banner collapse');

Also available in: Unified diff