XOOPS  2.6.0
Form.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 
26 abstract class Form implements ContainerInterface
27 {
33  private $action;
34 
40  private $method;
41 
47  private $name;
48 
54  private $title;
55 
61  private $display = '';
62 
68  private $elements = array();
69 
75  private $extra = array();
76 
82  private $required = array();
83 
87  private $summary = '';
88 
101  public function __construct($title, $name, $action, $method = 'post', $addtoken = false, $display = '')
102  {
103  $this->title = $title;
104  $this->name = $name;
105  $this->action = $action;
106  $this->method = $method;
107  $this->display = $display;
108  if ($addtoken != false) {
109  $this->addElement(new Token());
110  }
111  }
112 
120  public function getDisplay($encode = false)
121  {
122  return $encode ? htmlspecialchars($this->display, ENT_QUOTES) : $this->display;
123  }
124 
132  public function getTitle($encode = false)
133  {
134  return $encode ? htmlspecialchars($this->title, ENT_QUOTES) : $this->title;
135  }
136 
144  public function setTitle($title)
145  {
146  $this->title = $title;
147  }
148 
158  public function getName($encode = true)
159  {
160  return $encode ? htmlspecialchars($this->name, ENT_QUOTES) : $this->name;
161  }
162 
170  public function setAction($value = '')
171  {
172  $this->action = $value;
173  }
174 
182  public function getAction($encode = true)
183  {
184  // Convert &amp; to & for backward compatibility
185  return $encode ? htmlspecialchars(str_replace('&amp;', '&', $this->action), ENT_QUOTES) : $this->action;
186  }
187 
193  public function getMethod()
194  {
195  return (strtolower($this->method) == 'get') ? 'get' : 'post';
196  }
197 
206  public function addElement(Element $formElement, $required = false)
207  {
208  /* @var Element $formElement */
209  $this->elements[] = $formElement;
210  if ($formElement instanceof ContainerInterface) {
211  /* @var $formElement ContainerInterface */
212  $required_elements = $formElement->getRequired();
213  $count = count($required_elements);
214  for ($i = 0; $i < $count; ++$i) {
215  $this->required[] = $required_elements[$i];
216  }
217  } else {
218  if ($required && !$formElement instanceof Raw) {
219  $formElement->setRequired();
220  $this->required[] = $formElement;
221  }
222  }
223  }
224 
232  public function getElements($recurse = false)
233  {
234  if (!$recurse) {
235  return $this->elements;
236  } else {
237  $ret = array();
238  foreach ($this->elements as $ele) {
239  if ($ele instanceof ContainerInterface) {
240  /* @var $ele ContainerInterface */
241  $elements = $ele->getElements(true);
242  foreach ($elements as $ele2) {
243  $ret[] = $ele2;
244  }
245  unset($elements);
246  unset($ele2);
247  } else {
248  $ret[] = $ele;
249  }
250  unset($ele);
251  }
252  return $ret;
253  }
254  }
255 
261  public function getElementNames()
262  {
263  $ret = array();
264  $elements = $this->getElements(true);
265  foreach ($elements as $ele) {
266  /* @var Element $ele */
267  $ret[] = $ele->getName();
268  unset($ele);
269  }
270  return $ret;
271  }
272 
280  public function getElementByName($name)
281  {
282  $elements = $this->getElements(true);
283  foreach ($elements as $ele) {
284  /* @var Element $ele */
285  if ($name == $ele->getName()) {
286  return $ele;
287  }
288  }
289  $ele = null;
290  return $ele;
291  }
292 
301  public function setElementValue($name, $value)
302  {
303  $ele = $this->getElementByName($name);
304  if (is_object($ele)) {
305  $ele->setValue($value);
306  }
307  }
308 
316  public function setElementValues($values)
317  {
318  if (is_array($values) && !empty($values)) {
319  // will not use getElementByName() for performance..
320  $elements = $this->getElements(true);
321  foreach ($elements as $ele) {
322  /* @var $ele Element */
323  $name = $ele->getName();
324  if ($name && isset($values[$name])) {
325  $ele->setValue($values[$name]);
326  }
327  }
328  }
329  }
330 
339  public function getElementValue($name, $encode = false)
340  {
341  $ele = $this->getElementByName($name);
342  return $ele->getValue($encode);
343  }
344 
352  public function getElementValues($encode = false)
353  {
354  // will not use getElementByName() for performance..
355  $elements = $this->getElements(true);
356  $values = array();
357  foreach ($elements as $ele) {
358  /* @var Element $ele */
359  $name = $ele->getName();
360  if ($name) {
361  $values[$name] = $ele->getValue($encode);
362  }
363  }
364  return $values;
365  }
366 
374  public function setExtra($extra)
375  {
376  if (!empty($extra)) {
377  $this->extra[] = $extra;
378  }
379  }
380 
388  public function setSummary($summary)
389  {
390  if (!empty($summary)) {
391  $this->summary = strip_tags($summary);
392  }
393  }
394 
400  public function getExtra()
401  {
402  $extra = empty($this->extra) ? '' : ' ' . implode(' ', $this->extra);
403  return $extra;
404  }
405 
413  public function setRequired(Element $formElement)
414  {
415  $this->required[] = $formElement;
416  }
417 
423  public function getRequired()
424  {
425  return $this->required;
426  }
427 
435  abstract public function render();
436 
442  public function display()
443  {
444  echo $this->render();
445  }
446 
471  public function renderValidationJS($withtags = true)
472  {
473  $js = '';
474  if ($withtags) {
475  $js .= "\n<!-- Start Form Validation JavaScript //-->\n<script type='text/javascript'>\n<!--//\n";
476  }
477  $formname = $this->getName();
478  $js .= "function xoopsFormValidate_{$formname}() { var myform = window.document.{$formname}; ";
479  $elements = $this->getElements(true);
480  foreach ($elements as $ele) {
481  /* @var Element $ele */
482  $js .= $ele->renderValidationJS();
483  }
484  $js .= "return true;\n}\n";
485  if ($withtags) {
486  $js .= "//--></script>\n";
487  $js .= "<!-- End Form Validation JavaScript //-->\n";
488  }
489  return $js;
490  }
491 
499  public function assign(\XoopsTpl $tpl)
500  {
501  $i = -1;
502  $elements = array();
503  if (count($this->getRequired()) > 0) {
504  $this->elements[] =
505  new Raw("<tr class='foot'><td colspan='2'>* = " . \XoopsLocale::REQUIRED . "</td></tr>");
506  }
507  foreach ($this->getElements() as $ele) {
508  ++$i;
509  /* @var Element $ele */
510  $ele_name = $ele->getName();
511  $ele_description = $ele->getDescription();
512  $n = $ele_name ? $ele_name : $i;
513  $elements[$n]['name'] = $ele_name;
514  $elements[$n]['caption'] = $ele->getCaption();
515  $elements[$n]['body'] = $ele->render();
516  $elements[$n]['hidden'] = $ele->isHidden();
517  $elements[$n]['required'] = $ele->isRequired();
518  if ($ele_description != '') {
519  $elements[$n]['description'] = $ele_description;
520  }
521  }
522  $js = $this->renderValidationJS();
523  $tpl->assign($this->getName(), array(
524  'title' => $this->getTitle(), 'name' => $this->getName(), 'action' => $this->getAction(),
525  'method' => $this->getMethod(),
526  'extra' => 'onsubmit="return xoopsFormValidate_' . $this->getName() . '();"' . $this->getExtra(),
527  'javascript' => $js, 'elements' => $elements
528  ));
529  }
530 }
getElementValue($name, $encode=false)
Definition: Form.php:339
getElementValues($encode=false)
Definition: Form.php:352
setExtra($extra)
Definition: Form.php:374
$tpl
Definition: Xoops.php:73
$i
Definition: dialog.php:68
assign(\XoopsTpl $tpl)
Definition: Form.php:499
addElement(Element $formElement, $required=false)
Definition: Form.php:206
setTitle($title)
Definition: Form.php:144
setRequired($bool=true)
Definition: Element.php:493
getAction($encode=true)
Definition: Form.php:182
getDisplay($encode=false)
Definition: Form.php:120
getElementNames()
Definition: Form.php:261
setElementValues($values)
Definition: Form.php:316
getName($encode=true)
Definition: Form.php:158
getTitle($encode=false)
Definition: Form.php:132
setElementValue($name, $value)
Definition: Form.php:301
setRequired(Element $formElement)
Definition: Form.php:413
setAction($value= '')
Definition: Form.php:170
__construct($title, $name, $action, $method= 'post', $addtoken=false, $display= '')
Definition: Form.php:101
getElementByName($name)
Definition: Form.php:280
getElements($recurse=false)
Definition: Form.php:232
renderValidationJS($withtags=true)
Definition: Form.php:471
const REQUIRED
Definition: en_US.php:956
setSummary($summary)
Definition: Form.php:388