Project

General

Profile

Download (3.09 KB) Statistics
| Branch: | Tag: | Revision:
1 b40bcb23 Sjon Hortensius
<?php
2 d9575672 Sjon Hortensius
/*
3
	Form.class.php
4
5
	Copyright (C) 2015 Sjon Hortensius
6
	All rights reserved.
7
8
	Redistribution and use in source and binary forms, with or without
9
	modification, are permitted provided that the following conditions are met:
10
11
	1. Redistributions of source code must retain the above copyright notice,
12
	   this list of conditions and the following disclaimer.
13
14
	2. Redistributions in binary form must reproduce the above copyright
15
	   notice, this list of conditions and the following disclaimer in the
16
	   documentation and/or other materials provided with the distribution.
17
18
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
22
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
	POSSIBILITY OF SUCH DAMAGE.
28
*/
29 b40bcb23 Sjon Hortensius
30 a005a836 Sjon Hortensius
require_once('classes/Form/Element.class.php');
31
require_once('classes/Form/Input.class.php');
32
foreach (glob('classes/Form/*.class.php') as $file)
33
	require_once($file);
34 b40bcb23 Sjon Hortensius
35 a005a836 Sjon Hortensius
class Form extends Form_Element
36 b40bcb23 Sjon Hortensius
{
37 c2518204 Sjon Hortensius
	const LABEL_WIDTH = 2;
38
	const MAX_INPUT_WIDTH = 10;
39 1192840b Sjon Hortensius
	protected $_tagName = 'form';
40
	protected $_attributes = array(
41
		'class' => array('form-horizontal' => true),
42
		'method' => 'post',
43
		// Empty is interpreted by all browsers to submit to the current URI
44
		'action' => '',
45
	);
46 b40bcb23 Sjon Hortensius
	protected $_sections = array();
47 ee027864 Sjon Hortensius
	protected $_global = array();
48 a005a836 Sjon Hortensius
49 45d6ada5 Sjon Hortensius
	public function __construct($submit = null)
50 a005a836 Sjon Hortensius
	{
51 06da0d4e Sjon Hortensius
		if (!isset($submit))
52 38e06c66 Sjon Hortensius
			$submit = 'Save';
53
54
		if (gettype($submit) == 'string')
55 06da0d4e Sjon Hortensius
			$submit = new Form_Button(
56
				'save',
57 38e06c66 Sjon Hortensius
				$submit
58 06da0d4e Sjon Hortensius
			);
59
60 45d6ada5 Sjon Hortensius
		if (false !== $submit)
61
			$this->addGlobal($submit);
62 a005a836 Sjon Hortensius
	}
63 b40bcb23 Sjon Hortensius
64 a32c0623 Sjon Hortensius
	public function add(Form_Section $section)
65 b40bcb23 Sjon Hortensius
	{
66
		array_push($this->_sections, $section);
67
		$section->_setParent($this);
68
69
		return $section;
70
	}
71
72 1192840b Sjon Hortensius
	public function setAction($url)
73 a41cf2da Sjon Hortensius
	{
74 1192840b Sjon Hortensius
		$this->_attributes['action'] = $url;
75 8a197e58 Thane Gill
76
		return $this;
77 a41cf2da Sjon Hortensius
	}
78
79 ee027864 Sjon Hortensius
	public function addGlobal(Form_Input $input)
80 a005a836 Sjon Hortensius
	{
81 ee027864 Sjon Hortensius
		array_push($this->_global, $input);
82 a005a836 Sjon Hortensius
83 ee027864 Sjon Hortensius
		return $input;
84 a005a836 Sjon Hortensius
	}
85
86 4b219dfd Sjon Hortensius
	public function setMultipartEncoding()
87
	{
88
		$this->_attributes['enctype'] = 'multipart/form-data';
89
90
		return $this;
91
	}
92
93 a005a836 Sjon Hortensius
	protected function _setParent()
94
	{
95
		throw new Exception('Form does not have a parent');
96
	}
97
98 b40bcb23 Sjon Hortensius
	public function __toString()
99
	{
100 1192840b Sjon Hortensius
		$element = parent::__toString();
101 ee027864 Sjon Hortensius
		$html = implode('', $this->_sections);
102 9ba6f708 Sjon Hortensius
		$buttons = '';
103 69f9ff40 Sjon Hortensius
104 5fd8513d Sjon Hortensius
		foreach ($this->_global as $global)
105 69f9ff40 Sjon Hortensius
		{
106 9ba6f708 Sjon Hortensius
			if ($global instanceof Form_Button)
107
				$buttons .= $global;
108
			else
109
				$html .= $global;
110 69f9ff40 Sjon Hortensius
		}
111 b40bcb23 Sjon Hortensius
112 9ba6f708 Sjon Hortensius
		if (!empty($buttons))
113
		{
114
			$group = new Form_Element;
115 c2518204 Sjon Hortensius
			$group->addClass('col-sm-'. Form::MAX_INPUT_WIDTH, 'col-sm-offset-'. Form::LABEL_WIDTH);
116 9ba6f708 Sjon Hortensius
117
			$html .= $group . $buttons .'</div>';
118
		}
119 ee027864 Sjon Hortensius
120 b40bcb23 Sjon Hortensius
		return <<<EOT
121 1192840b Sjon Hortensius
	{$element}
122 ee027864 Sjon Hortensius
		{$html}
123 b40bcb23 Sjon Hortensius
	</form>
124
EOT;
125
	}
126 e22512fc Sjon Hortensius
}