Project

General

Profile

Download (2.94 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
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

    
30
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

    
35
class Form extends Form_Element
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
	);
44
	protected $_sections = array();
45
	protected $_global = array();
46
	protected $_labelWidth = 2;
47

    
48
	public function __construct()
49
	{
50
		$this->addGlobal(new Form_Button(
51
			'save',
52
			'Save'
53
		));
54
	}
55

    
56
	public function add(Form_Section $section)
57
	{
58
		array_push($this->_sections, $section);
59
		$section->_setParent($this);
60

    
61
		return $section;
62
	}
63

    
64
	public function setLabelWidth($size)
65
	{
66
		if ($size < 1 || $size > 12)
67
			throw new Exception('Incorrect size, pass a number between 1 and 12');
68

    
69
		$this->_labelWidth = (int)$size;
70
	}
71

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

    
76
		return $this;
77
	}
78

    
79
	public function getLabelWidth()
80
	{
81
		return $this->_labelWidth;
82
	}
83

    
84
	public function addGlobal(Form_Input $input)
85
	{
86
		array_push($this->_global, $input);
87

    
88
		return $input;
89
	}
90

    
91
	protected function _setParent()
92
	{
93
		throw new Exception('Form does not have a parent');
94
	}
95

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

    
101
		if (isset($this->_submit))
102
		{
103
			$this->_submit->setWidth(12 - $this->getLabelWidth());
104
			$this->_submit->addColumnClass('col-sm-offset-'. $this->_labelWidth);
105
			$$html .= $this->_submit;
106
		}
107

    
108
		$html .= implode('', $this->_global);
109

    
110
		return <<<EOT
111
	{$element}
112
		{$html}
113
	</form>
114
EOT;
115
	}
116
}
(1-1/2)