XOOPS 2.5.6  Final
 All Classes Namespaces Files Functions Variables Pages
form.php
Go to the documentation of this file.
1 <?php
21 defined('XOOPS_ROOT_PATH') or die('Restricted access');
22 
32 class XoopsForm
33 {
44  var $_action;
45 
51  var $_method;
52 
58  var $_name;
59 
65  var $_title;
66 
72  var $_summary = '';
73 
79  var $_elements = array();
80 
86  var $_extra = array();
87 
93  var $_required = array();
94 
95 
101  var $_objid = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';
102 
116  function XoopsForm($title, $name, $action, $method = 'post', $addtoken = false, $summary = '')
117  {
118  $this->_title = $title;
119  $this->_name = $name;
120  $this->_action = $action;
121  $this->_method = $method;
122  $this->_summary = $summary;
123  if ($addtoken != false) {
124  $this->addElement(new XoopsFormHiddenToken());
125  }
126  }
127 
138  function getObjectID($object, $hashinfo = 'sha1')
139  {
140  if (!is_object($object))
141  $object = $this;
142 
143  switch($hashinfo) {
144  case "md5":
145 
146  @$var['name'] = md5(get_class($object));
147 
148  foreach(get_object_vars($object) as $key => $value)
149  if ($key != '_objid')
150  @$var['value'] = $this->getArrayID($value, $key, $var['value'], $hashinfo);
151 
152  foreach(get_class_methods($object) as $key => $value)
153  @$var['func'] = $this->getArrayID($value, $key, $var['func'], $hashinfo);
154 
155  @$this->_objid = md5($var['name'].":".$var['func'].":".$var['value']);
156  return $this->_objid;
157  break;
158 
159  default:
160 
161  @$var['name'] = sha1(get_class($object));
162 
163  foreach(get_object_vars($object) as $key => $value)
164  if ($key != '_objid')
165  @$var['value'] = $this->getArrayID($value, $key, $var['value'], $hashinfo);
166 
167  foreach(get_class_methods($object) as $key => $value)
168  @$var['func'] = $this->getArrayID($value, $key, $var['func'], $hashinfo);
169 
170  @$this->_objid = sha1($var['name'].":".$var['func'].":".$var['value']);
171  return $this->_objid;
172 
173  }
174  }
175 
176  function getArrayID($value, $key, $ret, $hashinfo = 'sha1') {
177  switch($hashinfo) {
178  case "md5":
179  if (is_array($value)) {
180  foreach ($value as $keyb => $valueb)
181  @$ret = md5($ret.":".$this->getArrayID($valueb, $keyb, $ret, $hashinfo));
182  } else {
183  @$ret = md5($ret.":".$key.":".$value);
184  }
185  return $ret;
186  break;
187  default:
188  if (is_array($value)) {
189  foreach ($value as $keyb => $valueb)
190  @$ret = sha1($ret.":".$this->getArrayID($valueb, $keyb, $ret, $hashinfo));
191  } else {
192  @$ret = sha1($ret.":".$key.":".$value);
193  }
194  return $ret;
195  break;
196  }
197  }
198 
205  function getSummary($encode = false)
206  {
207  return $encode ? htmlspecialchars($this->_summary, ENT_QUOTES) : $this->_summary;
208  }
209 
216  function getTitle($encode = false)
217  {
218  return $encode ? htmlspecialchars($this->_title, ENT_QUOTES) : $this->_title;
219  }
220 
229  function getName($encode = true)
230  {
231  return $encode ? htmlspecialchars($this->_name, ENT_QUOTES) : $this->_name;
232  }
233 
240  function getAction($encode = true)
241  {
242  // Convert &amp; to & for backward compatibility
243  return $encode ? htmlspecialchars(str_replace('&amp;', '&', $this->_action), ENT_QUOTES) : $this->_action;
244  }
245 
251  function getMethod()
252  {
253  return (strtolower($this->_method) == 'get') ? 'get' : 'post';
254  }
255 
262  function addElement(&$formElement, $required = false)
263  {
264  if (is_string($formElement)) {
265  $this->_elements[] = $formElement;
266  } elseif (is_subclass_of($formElement, 'xoopsformelement')) {
267  $this->_elements[] = &$formElement;
268  if (! $formElement->isContainer()) {
269  if ($required) {
270  $formElement->_required = true;
271  $this->_required[] = &$formElement;
272  }
273  } else {
274  $required_elements = &$formElement->getRequired();
275  $count = count($required_elements);
276  for($i = 0; $i < $count; $i ++) {
277  $this->_required[] = &$required_elements[$i];
278  }
279  }
280  }
281  }
282 
289  function &getElements($recurse = false)
290  {
291  if (! $recurse) {
292  return $this->_elements;
293  } else {
294  $ret = array();
295  $count = count($this->_elements);
296  for($i = 0; $i < $count; $i ++) {
297  if (is_object($this->_elements[$i])) {
298  if (! $this->_elements[$i]->isContainer()) {
299  $ret[] = &$this->_elements[$i];
300  } else {
301  $elements = &$this->_elements[$i]->getElements(true);
302  $count2 = count($elements);
303  for($j = 0; $j < $count2; $j ++) {
304  $ret[] = &$elements[$j];
305  }
306  unset($elements);
307  }
308  }
309  }
310  return $ret;
311  }
312  }
313 
319  function getElementNames()
320  {
321  $ret = array();
322  $elements = &$this->getElements(true);
323  $count = count($elements);
324  for($i = 0; $i < $count; $i ++) {
325  $ret[] = $elements[$i]->getName();
326  }
327  return $ret;
328  }
329 
336  function &getElementByName($name)
337  {
338  $elements = $this->getElements(true);
339  $count = count($elements);
340  for($i = 0; $i < $count; $i ++) {
341  if ($name == $elements[$i]->getName(false)) {
342  return $elements[$i];
343  }
344  }
345  $elt = null;
346  return $elt;
347  }
348 
355  function setElementValue($name, $value)
356  {
357  $ele = &$this->getElementByName($name);
358  if (is_object($ele) && method_exists($ele, 'setValue')) {
359  $ele->setValue($value);
360  }
361  }
362 
368  function setElementValues($values)
369  {
370  if (is_array($values) && ! empty($values)) {
371  // will not use getElementByName() for performance..
372  $elements = &$this->getElements(true);
373  $count = count($elements);
374  for($i = 0; $i < $count; $i ++) {
375  $name = $elements[$i]->getName(false);
376  if ($name && isset($values[$name]) && method_exists($elements[$i], 'setValue')) {
377  $elements[$i]->setValue($values[$name]);
378  }
379  }
380  }
381  }
382 
390  function getElementValue($name, $encode = false)
391  {
392  $ele = &$this->getElementByName($name);
393  if (is_object($ele) && method_exists($ele, 'getValue')) {
394  return $ele->getValue($encode);
395  }
396  return;
397  }
398 
405  function getElementValues($encode = false)
406  {
407  // will not use getElementByName() for performance..
408  $elements = &$this->getElements(true);
409  $count = count($elements);
410  $values = array();
411  for($i = 0; $i < $count; $i ++) {
412  $name = $elements[$i]->getName(false);
413  if ($name && method_exists($elements[$i], 'getValue')) {
414  $values[$name] = &$elements[$i]->getValue($encode);
415  }
416  }
417  return $values;
418  }
419 
425  function setExtra($extra)
426  {
427  if (! empty($extra)) {
428  $this->_extra[] = $extra;
429  }
430  }
431 
437  function setSummary($summary)
438  {
439  if (! empty($summary)) {
440  $this->summary = strip_tags($summary);
441  }
442  }
443 
449  function &getExtra()
450  {
451  $extra = empty($this->_extra) ? '' : ' ' . implode(' ', $this->_extra);
452  return $extra;
453  }
454 
460  function setRequired(&$formElement)
461  {
462  $this->_required[] = &$formElement;
463  }
464 
470  function &getRequired()
471  {
472  return $this->_required;
473  }
474 
483  function insertBreak($extra = null)
484  {
485  }
486 
494  function render()
495  {
496  }
497 
501  function display()
502  {
503  echo $this->render();
504  }
505 
528  function renderValidationJS($withtags = true)
529  {
530  $js = '';
531  if ($withtags) {
532  $js .= "\n<!-- Start Form Validation JavaScript //-->\n<script type='text/javascript'>\n<!--//\n";
533  }
534  $formname = $this->getName();
535  $js .= "function xoopsFormValidate_{$formname}() { var myform = window.document.{$formname}; ";
536  $elements = $this->getElements(true);
537  foreach($elements as $elt) {
538  if (method_exists($elt, 'renderValidationJS')) {
539  $js .= $elt->renderValidationJS();
540  }
541  }
542  $js .= "return true;\n}\n";
543  if ($withtags) {
544  $js .= "//--></script>\n<!-- End Form Validation JavaScript //-->\n";
545  }
546  return $js;
547  }
548 
555  function assign(&$tpl)
556  {
557  $i = - 1;
558  $elements = array();
559  if (count($this->getRequired()) > 0) {
560  $this->_elements[] = "<tr class='foot'><td colspan='2'>* = " . _REQUIRED . "</td></tr>";
561  }
562  foreach($this->getElements() as $ele) {
563  ++ $i;
564  if (is_string($ele)) {
565  $elements[$i]['body'] = $ele;
566  continue;
567  }
568  $ele_name = $ele->getName();
569  $ele_description = $ele->getDescription();
570  $n = $ele_name ? $ele_name : $i;
571  $elements[$n]['name'] = $ele_name;
572  $elements[$n]['caption'] = $ele->getCaption();
573  $elements[$n]['body'] = $ele->render();
574  $elements[$n]['hidden'] = $ele->isHidden();
575  $elements[$n]['required'] = $ele->isRequired();
576  if ($ele_description != '') {
577  $elements[$n]['description'] = $ele_description;
578  }
579  }
580  $js = $this->renderValidationJS();
581  $tpl->assign($this->getName(), array(
582  'title' => $this->getTitle() ,
583  'name' => $this->getName() ,
584  'action' => $this->getAction() ,
585  'method' => $this->getMethod() ,
586  'extra' => 'onsubmit="return xoopsFormValidate_' . $this->getName() . '();"' . $this->getExtra() ,
587  'javascript' => $js ,
588  'elements' => $elements));
589  }
590 }
591 
592 ?>