1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Containers for containing form elements |
5
|
|
|
* |
6
|
|
|
* Ntentan Framework |
7
|
|
|
* Copyright (c) 2008-2012 James Ekow Abaka Ainooson |
8
|
|
|
* |
9
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining |
10
|
|
|
* a copy of this software and associated documentation files (the |
11
|
|
|
* "Software"), to deal in the Software without restriction, including |
12
|
|
|
* without limitation the rights to use, copy, modify, merge, publish, |
13
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to |
14
|
|
|
* permit persons to whom the Software is furnished to do so, subject to |
15
|
|
|
* the following conditions: |
16
|
|
|
* |
17
|
|
|
* The above copyright notice and this permission notice shall be |
18
|
|
|
* included in all copies or substantial portions of the Software. |
19
|
|
|
* |
20
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
21
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
22
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
23
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
24
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
25
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
26
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
27
|
|
|
* |
28
|
|
|
* @author James Ainooson <[email protected]> |
29
|
|
|
* @copyright Copyright 2010 James Ekow Abaka Ainooson |
30
|
|
|
* @license MIT |
31
|
|
|
*/ |
32
|
|
|
|
33
|
|
|
namespace ntentan\honam\engines\php\helpers\form; |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The container class. This abstract class provides the necessary |
38
|
|
|
* basis for implementing form element containers. The container |
39
|
|
|
* is a special element which contains other form elements. |
40
|
|
|
*/ |
41
|
|
|
abstract class Container extends Element |
42
|
|
|
{ |
43
|
|
|
|
44
|
|
|
const RENDER_MODE_ALL = 'all'; |
45
|
|
|
const RENDER_MODE_HEAD = 'head'; |
46
|
|
|
const RENDER_MODE_FOOT = 'foot'; |
47
|
|
|
|
48
|
|
|
protected $renderMode = self::RENDER_MODE_ALL; |
49
|
|
|
protected $elements = array(); |
50
|
|
|
|
51
|
|
|
abstract protected function renderHead(); |
|
|
|
|
52
|
|
|
|
53
|
|
|
abstract protected function renderFoot(); |
|
|
|
|
54
|
|
|
|
55
|
|
|
public function setRenderMode($renderMode) |
56
|
|
|
{ |
57
|
|
|
$this->renderMode = $renderMode; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
private function addElement($element) |
61
|
|
|
{ |
62
|
|
|
$this->elements[] = $element; |
63
|
|
|
$element->parent = $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Method for adding an element to the form container. |
68
|
|
|
* @return Container |
69
|
|
|
*/ |
70
|
|
|
public function add() |
71
|
|
|
{ |
72
|
|
|
$arguments = func_get_args(); |
73
|
|
|
foreach ($arguments as $element) { |
74
|
|
|
$this->addElement($element); |
75
|
|
|
} |
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* This method sets the data for the fields in this container. The parameter |
81
|
|
|
* passed to this method is a structured array which has field names as keys |
82
|
|
|
* and the values as value. |
83
|
|
|
*/ |
84
|
|
|
public function setData($data) |
85
|
|
|
{ |
86
|
|
|
if (is_array($data)) { |
87
|
|
|
foreach ($this->elements as $element) { |
88
|
|
|
$element->setData($data); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function render() |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
switch ($this->renderMode) { |
97
|
|
|
case self::RENDER_MODE_HEAD; |
98
|
|
|
return $this->renderHead(); |
99
|
|
|
case self::RENDER_MODE_FOOT: |
100
|
|
|
return $this->renderFoot(); |
101
|
|
|
default: |
102
|
|
|
return $this->renderHead() |
103
|
|
|
. TemplateEngine::render('elements.tpl.php', array('elements' => $this->getElements())) |
104
|
|
|
. $this->renderFoot(); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
//! Returns an array of all the Elements found in this container. |
109
|
|
|
public function getElements() |
110
|
|
|
{ |
111
|
|
|
return $this->elements; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function __toString() |
|
|
|
|
115
|
|
|
{ |
116
|
|
|
return $this->render(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function isContainer() |
120
|
|
|
{ |
121
|
|
|
return true; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a
@return
doc comment to communicate to implementors of these methods what they are expected to return.