21 defined(
'XOOPS_ROOT_PATH') or die('Restricted access');
79 var $_elements = array();
86 var $_extra = array();
93 var $_required = array();
101 var $_objid =
'da39a3ee5e6b4b0d3255bfef95601890afd80709';
119 $this->_name = $name;
122 $this->_summary = $summary;
123 if ($addtoken !=
false) {
138 function getObjectID($object, $hashinfo =
'sha1')
140 if (!is_object($object))
146 @
$var[
'name'] = md5(get_class($object));
148 foreach(get_object_vars($object) as $key => $value)
149 if ($key !=
'_objid')
150 @
$var[
'value'] = $this->getArrayID($value, $key,
$var[
'value'], $hashinfo);
152 foreach(get_class_methods($object) as $key => $value)
153 @
$var[
'func'] = $this->getArrayID($value, $key,
$var[
'func'], $hashinfo);
155 @$this->_objid = md5(
$var[
'name'].
":".
$var[
'func'].
":".
$var[
'value']);
156 return $this->_objid;
161 @$var[
'name'] = sha1(get_class($object));
163 foreach(get_object_vars($object) as $key => $value)
164 if ($key !=
'_objid')
165 @$var[
'value'] = $this->getArrayID($value, $key, $var[
'value'], $hashinfo);
167 foreach(get_class_methods($object) as $key => $value)
168 @$var[
'func'] = $this->getArrayID($value, $key, $var[
'func'], $hashinfo);
170 @$this->_objid = sha1($var[
'name'].
":".$var[
'func'].
":".$var[
'value']);
171 return $this->_objid;
179 if (is_array($value)) {
180 foreach ($value as $keyb => $valueb)
181 @
$ret = md5(
$ret.
":".$this->getArrayID($valueb, $keyb,
$ret, $hashinfo));
183 @
$ret = md5(
$ret.
":".$key.
":".$value);
188 if (is_array($value)) {
189 foreach ($value as $keyb => $valueb)
190 @
$ret = sha1(
$ret.
":".$this->getArrayID($valueb, $keyb,
$ret, $hashinfo));
192 @
$ret = sha1(
$ret.
":".$key.
":".$value);
205 function getSummary($encode =
false)
207 return $encode ? htmlspecialchars($this->_summary, ENT_QUOTES) : $this->_summary;
216 function getTitle($encode =
false)
218 return $encode ? htmlspecialchars($this->_title, ENT_QUOTES) : $this->_title;
229 function getName($encode =
true)
231 return $encode ? htmlspecialchars($this->_name, ENT_QUOTES) : $this->_name;
240 function getAction($encode =
true)
243 return $encode ? htmlspecialchars(str_replace(
'&',
'&', $this->_action), ENT_QUOTES) : $this->_action;
253 return (strtolower($this->_method) ==
'get') ?
'get' :
'post';
262 function addElement(&$formElement, $required =
false)
264 if (is_string($formElement)) {
265 $this->_elements[] = $formElement;
266 } elseif (is_subclass_of($formElement,
'xoopsformelement')) {
267 $this->_elements[] = &$formElement;
268 if (! $formElement->isContainer()) {
270 $formElement->_required =
true;
271 $this->_required[] = &$formElement;
274 $required_elements = &$formElement->getRequired();
275 $count = count($required_elements);
276 for(
$i = 0;
$i < $count;
$i ++) {
277 $this->_required[] = &$required_elements[
$i];
289 function &getElements($recurse =
false)
292 return $this->_elements;
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];
301 $elements = &$this->_elements[
$i]->getElements(
true);
302 $count2 = count($elements);
303 for(
$j = 0;
$j < $count2;
$j ++) {
319 function getElementNames()
322 $elements = &$this->getElements(
true);
323 $count = count($elements);
324 for(
$i = 0;
$i < $count;
$i ++) {
325 $ret[] = $elements[
$i]->getName();
336 function &getElementByName($name)
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];
355 function setElementValue($name, $value)
357 $ele = &$this->getElementByName($name);
358 if (is_object($ele) && method_exists($ele,
'setValue')) {
359 $ele->setValue($value);
368 function setElementValues($values)
370 if (is_array($values) && ! empty($values)) {
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]);
390 function getElementValue($name, $encode =
false)
392 $ele = &$this->getElementByName($name);
393 if (is_object($ele) && method_exists($ele,
'getValue')) {
394 return $ele->getValue($encode);
405 function getElementValues($encode =
false)
408 $elements = &$this->getElements(
true);
409 $count = count($elements);
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);
425 function setExtra($extra)
427 if (! empty($extra)) {
428 $this->_extra[] = $extra;
437 function setSummary($summary)
439 if (! empty($summary)) {
440 $this->summary = strip_tags($summary);
451 $extra = empty($this->_extra) ?
'' :
' ' . implode(
' ', $this->_extra);
460 function setRequired(&$formElement)
462 $this->_required[] = &$formElement;
470 function &getRequired()
472 return $this->_required;
483 function insertBreak($extra = null)
503 echo $this->render();
528 function renderValidationJS($withtags =
true)
532 $js .=
"\n<!-- Start Form Validation JavaScript //-->\n<script type='text/javascript'>\n<!--//\n";
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();
542 $js .=
"return true;\n}\n";
544 $js .=
"//--></script>\n<!-- End Form Validation JavaScript //-->\n";
559 if (count($this->getRequired()) > 0) {
560 $this->_elements[] =
"<tr class='foot'><td colspan='2'>* = " .
_REQUIRED .
"</td></tr>";
562 foreach($this->getElements() as $ele) {
564 if (is_string($ele)) {
565 $elements[
$i][
'body'] = $ele;
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;
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));