1: | <?php |
2: | /** |
3: | * XOOPS form element |
4: | * |
5: | * You may not change or alter any portion of this comment or credits |
6: | * of supporting developers from this source code or any supporting source code |
7: | * which is considered copyrighted (c) material of the original comment or credit authors. |
8: | * This program is distributed in the hope that it will be useful, |
9: | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10: | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
11: | * |
12: | * @copyright (c) 2000-2017 XOOPS Project (www.xoops.org) |
13: | * @license GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html) |
14: | * @package kernel |
15: | * @subpackage form |
16: | * @since 2.0.0 |
17: | * @author Kazumi Ono (AKA onokazu) http://www.myweb.ne.jp/, http://jp.xoops.org/ |
18: | */ |
19: | |
20: | defined('XOOPS_ROOT_PATH') || exit('Restricted access'); |
21: | |
22: | /** |
23: | * A group of form elements |
24: | */ |
25: | class XoopsFormElementTray extends XoopsFormElement |
26: | { |
27: | const ORIENTATION_HORIZONTAL = 'horizontal'; |
28: | const ORIENTATION_VERTICAL = 'vertical'; |
29: | |
30: | /** |
31: | * array of form element objects |
32: | * |
33: | * @var array |
34: | * @access private |
35: | */ |
36: | private $_elements = array(); |
37: | |
38: | /** |
39: | * required elements |
40: | * |
41: | * @var array |
42: | */ |
43: | public $_required = array(); |
44: | |
45: | protected $orientation; |
46: | |
47: | /** |
48: | * HTML to seperate the elements |
49: | * |
50: | * @var string |
51: | * @access private |
52: | */ |
53: | private $_delimeter; |
54: | |
55: | /** |
56: | * constructor |
57: | * |
58: | * @param string $caption Caption for the group. |
59: | * @param string $delimeter HTML to separate the elements |
60: | * @param string $name |
61: | * |
62: | */ |
63: | public function __construct($caption, $delimeter = ' ', $name = '') |
64: | { |
65: | $this->setName($name); |
66: | $this->setCaption($caption); |
67: | $this->_delimeter = $delimeter; |
68: | } |
69: | |
70: | /** |
71: | * Is this element a container of other elements? |
72: | * |
73: | * @return bool true |
74: | */ |
75: | public function isContainer() |
76: | { |
77: | return true; |
78: | } |
79: | |
80: | /** |
81: | * Find out if there are required elements. |
82: | * |
83: | * @return bool |
84: | */ |
85: | public function isRequired() |
86: | { |
87: | return !empty($this->_required); |
88: | } |
89: | |
90: | /** |
91: | * Add an element to the group |
92: | * |
93: | * @param XoopsFormElement $formElement {@link XoopsFormElement} to add |
94: | * @param bool $required |
95: | * |
96: | */ |
97: | public function addElement(XoopsFormElement $formElement, $required = false) |
98: | { |
99: | $this->_elements[] = $formElement; |
100: | if (!$formElement->isContainer()) { |
101: | if ($required) { |
102: | $formElement->_required = true; |
103: | $this->_required[] = $formElement; |
104: | } |
105: | } else { |
106: | $required_elements = $formElement->getRequired(); |
107: | $count = count($required_elements); |
108: | for ($i = 0; $i < $count; ++$i) { |
109: | $this->_required[] = &$required_elements[$i]; |
110: | } |
111: | } |
112: | } |
113: | |
114: | /** |
115: | * get an array of "required" form elements |
116: | * |
117: | * @return array array of {@link XoopsFormElement}s |
118: | */ |
119: | public function &getRequired() |
120: | { |
121: | return $this->_required; |
122: | } |
123: | |
124: | /** |
125: | * Get an array of the elements in this group |
126: | * |
127: | * @param bool $recurse get elements recursively? |
128: | * @return XoopsFormElement[] Array of {@link XoopsFormElement} objects. |
129: | */ |
130: | public function &getElements($recurse = false) |
131: | { |
132: | if (!$recurse) { |
133: | return $this->_elements; |
134: | } else { |
135: | $ret = array(); |
136: | $count = count($this->_elements); |
137: | for ($i = 0; $i < $count; ++$i) { |
138: | if (!$this->_elements[$i]->isContainer()) { |
139: | $ret[] = &$this->_elements[$i]; |
140: | } else { |
141: | $elements = &$this->_elements[$i]->getElements(true); |
142: | $count2 = count($elements); |
143: | for ($j = 0; $j < $count2; ++$j) { |
144: | $ret[] = &$elements[$j]; |
145: | } |
146: | unset($elements); |
147: | } |
148: | } |
149: | |
150: | return $ret; |
151: | } |
152: | } |
153: | |
154: | /** |
155: | * Get the delimiter of this group |
156: | * |
157: | * @param bool $encode To sanitizer the text? |
158: | * @return string The delimiter |
159: | */ |
160: | public function getDelimeter($encode = false) |
161: | { |
162: | return $encode ? htmlspecialchars(str_replace(' ', ' ', $this->_delimeter), ENT_QUOTES) : $this->_delimeter; |
163: | } |
164: | |
165: | /** |
166: | * setOrientation() communicate to renderer the expected tray orientation |
167: | * \XoopsFormElementTray::ORIENTATION_HORIZONTAL for across |
168: | * \XoopsFormElementTray::ORIENTATION_VERTICAL for up and down |
169: | * |
170: | * If not set explicitly, a default value will be assigned on getOrientation() |
171: | * |
172: | * @param string $direction ORIENTATION constant |
173: | */ |
174: | public function setOrientation($direction) |
175: | { |
176: | if ($direction !== self::ORIENTATION_VERTICAL) { |
177: | $direction = self::ORIENTATION_HORIZONTAL; |
178: | } |
179: | $this->orientation = $direction; |
180: | } |
181: | |
182: | /** |
183: | * getOrientation() return the expected tray orientation |
184: | * |
185: | * The value will be assigned a default value if not previously set. |
186: | * |
187: | * The default logic considers the presence of an HTML br tag in _delimeter |
188: | * as implying ORIENTATION_VERTICAL for bc |
189: | * |
190: | * @return string either \XoopsFormElementTray::ORIENTATION_HORIZONTAL |
191: | * or \XoopsFormElementTray::ORIENTATION_VERTICAL\ |
192: | */ |
193: | public function getOrientation() |
194: | { |
195: | if (!isset($this->orientation)) { |
196: | if(false !== stripos($this->_delimeter, '<br')) { |
197: | $this->orientation = self::ORIENTATION_VERTICAL; |
198: | // strip tag as renderer should supply the relevant html |
199: | } else { |
200: | $this->orientation = self::ORIENTATION_HORIZONTAL; |
201: | } |
202: | } |
203: | $this->_delimeter = preg_replace('#<br ?\/?>#i', '', $this->_delimeter); |
204: | return $this->orientation; |
205: | } |
206: | |
207: | /** |
208: | * prepare HTML to output this group |
209: | * |
210: | * @return string HTML output |
211: | */ |
212: | public function render() |
213: | { |
214: | return XoopsFormRenderer::getInstance()->get()->renderFormElementTray($this); |
215: | } |
216: | } |
217: |