XOOPS  2.6.0
ElementTray.php
Go to the documentation of this file.
1 <?php
2 /*
3  You may not change or alter any portion of this comment or credits
4  of supporting developers from this source code or any supporting source code
5  which is considered copyrighted (c) material of the original comment or credit authors.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 */
11 
12 namespace Xoops\Form;
13 
25 class ElementTray extends Element implements ContainerInterface
26 {
32  protected $elements = array();
33 
39  protected $required = array();
40 
46  protected $delimiter;
47 
55  public function __construct($caption, $delimiter = "&nbsp;", $name = "")
56  {
57  $this->setName($name);
58  $this->setCaption($caption);
59  $this->delimiter = $delimiter;
60  }
61 
67  public function isRequired()
68  {
69  return !empty($this->required);
70  }
71 
80  public function addElement(Element $formElement, $required = false)
81  {
82  $this->elements[] = $formElement;
83  if ($formElement instanceof ContainerInterface) {
84  /* @var $formElement ContainerInterface */
85  $required_elements = $formElement->getRequired();
86  $count = count($required_elements);
87  for ($i = 0; $i < $count; ++$i) {
88  $this->required[] = $required_elements[$i];
89  }
90  } else {
91  if ($required) {
92  $formElement->setRequired();
93  $this->required[] = $formElement;
94  }
95  }
96  }
97 
103  public function getRequired()
104  {
105  return $this->required;
106  }
107 
115  public function getElements($recursively = false)
116  {
117  if (!$recursively) {
118  return $this->elements;
119  } else {
120  $ret = array();
121  foreach ($this->elements as $ele) {
122  if ($ele instanceof ContainerInterface) {
123  /* @var ContainerInterface $ele */
124  $elements = $ele->getElements(true);
125  foreach ($elements as $ele2) {
126  $ret[] = $ele2;
127  }
128  unset($elements);
129  unset($ele2);
130  } else {
131  $ret[] = $ele;
132  }
133  unset($ele);
134  }
135  return $ret;
136  }
137  }
138 
146  public function getDelimiter($encode = false)
147  {
148  return $encode ? htmlspecialchars(str_replace('&nbsp;', ' ', $this->delimiter)) : $this->delimiter;
149  }
150 
156  public function render()
157  {
158  $count = 0;
159  $ret = "";
160  foreach ($this->getElements() as $ele) {
161  /* @var Element $ele */
162  if ($count > 0) {
163  $ret .= $this->getDelimiter();
164  }
165  if ($ele->getCaption() != '') {
166  $ret .= $ele->getCaption() . "&nbsp;";
167  }
168  $ret .= $ele->render() . NWLINE;
169  if (!$ele->isHidden()) {
170  ++$count;
171  }
172  }
173  return $ret;
174  }
175 }
$i
Definition: dialog.php:68
setCaption($caption)
Definition: Element.php:396
__construct($caption, $delimiter="&nbsp;", $name="")
Definition: ElementTray.php:55
setRequired($bool=true)
Definition: Element.php:493
getElements($recursively=false)
addElement(Element $formElement, $required=false)
Definition: ElementTray.php:80
getDelimiter($encode=false)