Project

General

Profile

Download (3.03 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
	const LABEL_WIDTH = 2;
38
	const MAX_INPUT_WIDTH = 10;
39
	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
	protected $_sections = array();
47
	protected $_global = array();
48

    
49
	public function __construct($submit = null)
50
	{
51
		if (!isset($submit))
52
			$submit = new Form_Button(
53
				'save',
54
				'Save'
55
			);
56

    
57
		if (false !== $submit)
58
			$this->addGlobal($submit);
59
	}
60

    
61
	public function add(Form_Section $section)
62
	{
63
		array_push($this->_sections, $section);
64
		$section->_setParent($this);
65

    
66
		return $section;
67
	}
68

    
69
	public function setAction($url)
70
	{
71
		$this->_attributes['action'] = $url;
72

    
73
		return $this;
74
	}
75

    
76
	public function addGlobal(Form_Input $input)
77
	{
78
		array_push($this->_global, $input);
79

    
80
		return $input;
81
	}
82

    
83
	public function setMultipartEncoding()
84
	{
85
		$this->_attributes['enctype'] = 'multipart/form-data';
86

    
87
		return $this;
88
	}
89

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

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

    
101
		foreach ($this->_global as $global)
102
		{
103
			if ($global instanceof Form_Button)
104
				$buttons .= $global;
105
			else
106
				$html .= $global;
107
		}
108

    
109
		if (!empty($buttons))
110
		{
111
			$group = new Form_Element;
112
			$group->addClass('col-sm-'. Form::MAX_INPUT_WIDTH, 'col-sm-offset-'. Form::LABEL_WIDTH);
113

    
114
			$html .= $group . $buttons .'</div>';
115
		}
116

    
117
		return <<<EOT
118
	{$element}
119
		{$html}
120
	</form>
121
EOT;
122
	}
123
}
    (1-1/1)