1: | <?php
|
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | |
10: | |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
17: | |
18: | |
19: |
|
20: | defined('XOOPS_ROOT_PATH') || exit('Restricted access');
|
21: |
|
22: | |
23: | |
24: | |
25: | |
26: | |
27: | |
28: | |
29: | |
30: |
|
31: | class XoopsForm
|
32: | {
|
33: | |
34: | |
35: | |
36: | |
37: |
|
38: | |
39: | |
40: | |
41: | |
42: |
|
43: | public $_action;
|
44: |
|
45: | |
46: | |
47: | |
48: | |
49: |
|
50: | public $_method;
|
51: |
|
52: | |
53: | |
54: | |
55: | |
56: |
|
57: | public $_name;
|
58: |
|
59: | |
60: | |
61: | |
62: | |
63: |
|
64: | public $_title;
|
65: |
|
66: | |
67: | |
68: | |
69: | |
70: |
|
71: | public $_summary = '';
|
72: |
|
73: | |
74: | |
75: | |
76: | |
77: |
|
78: | public $_elements = array();
|
79: |
|
80: | |
81: | |
82: | |
83: | |
84: |
|
85: | public $_class = array();
|
86: |
|
87: | |
88: | |
89: | |
90: | |
91: |
|
92: | public $_extra = array();
|
93: |
|
94: | |
95: | |
96: | |
97: | |
98: |
|
99: | public $_required = array();
|
100: |
|
101: | |
102: | |
103: | |
104: | |
105: |
|
106: | public $_objid = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';
|
107: |
|
108: | |
109: | |
110: |
|
111: |
|
112: | |
113: | |
114: | |
115: | |
116: | |
117: | |
118: | |
119: | |
120: | |
121: |
|
122: | public function __construct($title, $name, $action, $method = 'post', $addtoken = false, $summary = '')
|
123: | {
|
124: | $this->_title = $title;
|
125: | $this->_name = $name;
|
126: | $this->_action = $action;
|
127: | $this->_method = $method;
|
128: | $this->_summary = $summary;
|
129: | if (false != $addtoken) {
|
130: | $this->addElement(new XoopsFormHiddenToken());
|
131: | }
|
132: | }
|
133: | |
134: | |
135: | |
136: |
|
137: | public function XoopsForm()
|
138: | {
|
139: | $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1);
|
140: | trigger_error("Should call parent::__construct in {$trace[0]['file']} line {$trace[0]['line']},", E_USER_DEPRECATED);
|
141: | self::__construct();
|
142: | }
|
143: | |
144: | |
145: | |
146: | |
147: | |
148: | |
149: | |
150: | |
151: | |
152: | |
153: | |
154: | |
155: |
|
156: | public function getObjectID($object, $hashinfo = 'sha1')
|
157: | {
|
158: |
|
159: | $var = array(
|
160: | 'name' => '',
|
161: | 'value' => '',
|
162: | 'func' => ''
|
163: | );
|
164: |
|
165: |
|
166: | if (!is_object($object)) {
|
167: | $object = $this;
|
168: | }
|
169: |
|
170: |
|
171: | $hashMethod = ('md5' === $hashinfo) ? 'md5' : 'sha1';
|
172: |
|
173: |
|
174: | $var['name'] = $hashMethod(get_class($object));
|
175: |
|
176: |
|
177: | foreach (get_object_vars($object) as $key => $value) {
|
178: | if ($key !== '_objid') {
|
179: | $var['value'] = $this->getArrayID($value, $key, $var['value'], $hashinfo);
|
180: | }
|
181: | }
|
182: |
|
183: |
|
184: | foreach (get_class_methods($object) as $key => $value) {
|
185: | $var['func'] = $this->getArrayID($value, $key, $var['func'], $hashinfo);
|
186: | }
|
187: |
|
188: |
|
189: | $this->_objid = $hashMethod(implode(':', $var));
|
190: |
|
191: | return $this->_objid;
|
192: | }
|
193: |
|
194: |
|
195: | |
196: | |
197: | |
198: | |
199: | |
200: | |
201: | |
202: |
|
203: | public function getArrayID($value, $key, $ret, $hashinfo = 'sha1')
|
204: | {
|
205: | switch ($hashinfo) {
|
206: | case 'md5':
|
207: | if (!isset($ret)) {
|
208: | $ret = '';
|
209: | }
|
210: | if (is_array($value)) {
|
211: | foreach ($value as $keyb => $valueb) {
|
212: | $ret = md5($ret . ':' . $this->getArrayID($valueb, $keyb, $ret, $hashinfo));
|
213: | }
|
214: | } else {
|
215: | $ret = md5($ret . ':' . $key . ':' . $value);
|
216: | }
|
217: |
|
218: | return $ret;
|
219: | default:
|
220: | if (!isset($ret)) {
|
221: | $ret = '';
|
222: | }
|
223: | if (is_array($value)) {
|
224: | foreach ($value as $keyb => $valueb) {
|
225: | $ret = sha1($ret . ':' . $this->getArrayID($valueb, $keyb, $ret, $hashinfo));
|
226: | }
|
227: | } else {
|
228: | $ret = sha1($ret . ':' . $key . ':' . $value);
|
229: | }
|
230: |
|
231: | return $ret;
|
232: | }
|
233: | }
|
234: |
|
235: | |
236: | |
237: | |
238: | |
239: | |
240: |
|
241: | public function getSummary($encode = false)
|
242: | {
|
243: | return $encode ? htmlspecialchars($this->_summary, ENT_QUOTES) : $this->_summary;
|
244: | }
|
245: |
|
246: | |
247: | |
248: | |
249: | |
250: | |
251: |
|
252: | public function getTitle($encode = false)
|
253: | {
|
254: | return $encode ? htmlspecialchars($this->_title, ENT_QUOTES) : $this->_title;
|
255: | }
|
256: |
|
257: | |
258: | |
259: | |
260: | |
261: | |
262: | |
263: | |
264: |
|
265: | public function getName($encode = true)
|
266: | {
|
267: | return $encode ? htmlspecialchars($this->_name, ENT_QUOTES) : $this->_name;
|
268: | }
|
269: |
|
270: | |
271: | |
272: | |
273: | |
274: | |
275: |
|
276: | public function getAction($encode = true)
|
277: | {
|
278: |
|
279: | return $encode ? htmlspecialchars(str_replace('&', '&', $this->_action), ENT_QUOTES) : $this->_action;
|
280: | }
|
281: |
|
282: | |
283: | |
284: | |
285: | |
286: |
|
287: | public function getMethod()
|
288: | {
|
289: | return (strtolower($this->_method) === 'get') ? 'get' : 'post';
|
290: | }
|
291: |
|
292: | |
293: | |
294: | |
295: | |
296: | |
297: | |
298: |
|
299: | public function addElement($formElement, $required = false)
|
300: | {
|
301: | if (is_string($formElement)) {
|
302: | $this->_elements[] = $formElement;
|
303: | } elseif (is_subclass_of($formElement, 'XoopsFormElement')) {
|
304: | $this->_elements[] = &$formElement;
|
305: | if (!$formElement->isContainer()) {
|
306: | if ($required) {
|
307: | $formElement->_required = true;
|
308: | $this->_required[] = &$formElement;
|
309: | }
|
310: | } else {
|
311: | $required_elements = &$formElement->getRequired();
|
312: | $count = count($required_elements);
|
313: | for ($i = 0; $i < $count; ++$i) {
|
314: | $this->_required[] = &$required_elements[$i];
|
315: | }
|
316: | }
|
317: | }
|
318: | }
|
319: |
|
320: | |
321: | |
322: | |
323: | |
324: | |
325: | |
326: |
|
327: | public function &getElements($recurse = false)
|
328: | {
|
329: | if (!$recurse) {
|
330: | return $this->_elements;
|
331: | } else {
|
332: | $ret = array();
|
333: | $count = count($this->_elements);
|
334: | for ($i = 0; $i < $count; ++$i) {
|
335: | if (is_object($this->_elements[$i])) {
|
336: | if (!$this->_elements[$i]->isContainer()) {
|
337: | $ret[] = &$this->_elements[$i];
|
338: | } else {
|
339: | $elements = &$this->_elements[$i]->getElements(true);
|
340: | $count2 = count($elements);
|
341: | for ($j = 0; $j < $count2; ++$j) {
|
342: | $ret[] = &$elements[$j];
|
343: | }
|
344: | unset($elements);
|
345: | }
|
346: | }
|
347: | }
|
348: |
|
349: | return $ret;
|
350: | }
|
351: | }
|
352: |
|
353: | |
354: | |
355: | |
356: | |
357: |
|
358: | public function getElementNames()
|
359: | {
|
360: | $ret = array();
|
361: | $elements = &$this->getElements(true);
|
362: | $count = count($elements);
|
363: | for ($i = 0; $i < $count; ++$i) {
|
364: | $ret[] = $elements[$i]->getName();
|
365: | }
|
366: |
|
367: | return $ret;
|
368: | }
|
369: |
|
370: | |
371: | |
372: | |
373: | |
374: | |
375: |
|
376: | public function &getElementByName($name)
|
377: | {
|
378: | $elements =& $this->getElements(true);
|
379: | $count = count($elements);
|
380: | for ($i = 0; $i < $count; ++$i) {
|
381: | if ($name == $elements[$i]->getName(false)) {
|
382: | return $elements[$i];
|
383: | }
|
384: | }
|
385: | $elt = null;
|
386: |
|
387: | return $elt;
|
388: | }
|
389: |
|
390: | |
391: | |
392: | |
393: | |
394: | |
395: |
|
396: | public function setElementValue($name, $value)
|
397: | {
|
398: | $ele = &$this->getElementByName($name);
|
399: | if (is_object($ele) && method_exists($ele, 'setValue')) {
|
400: | $ele->setValue($value);
|
401: | }
|
402: | }
|
403: |
|
404: | |
405: | |
406: | |
407: | |
408: |
|
409: | public function setElementValues($values)
|
410: | {
|
411: | if (!empty($values) && \is_array($values)) {
|
412: |
|
413: | $elements = &$this->getElements(true);
|
414: | $count = count($elements);
|
415: | for ($i = 0; $i < $count; ++$i) {
|
416: | $name = $elements[$i]->getName(false);
|
417: | if ($name && isset($values[$name]) && method_exists($elements[$i], 'setValue')) {
|
418: | $elements[$i]->setValue($values[$name]);
|
419: | }
|
420: | }
|
421: | }
|
422: | }
|
423: |
|
424: | |
425: | |
426: | |
427: | |
428: | |
429: | |
430: |
|
431: | public function getElementValue($name, $encode = false)
|
432: | {
|
433: | $ele = &$this->getElementByName($name);
|
434: | if (is_object($ele) && method_exists($ele, 'getValue')) {
|
435: | return $ele->getValue($encode);
|
436: | }
|
437: |
|
438: | return null;
|
439: | }
|
440: |
|
441: | |
442: | |
443: | |
444: | |
445: | |
446: |
|
447: | public function getElementValues($encode = false)
|
448: | {
|
449: |
|
450: | $elements = &$this->getElements(true);
|
451: | $count = count($elements);
|
452: | $values = array();
|
453: | for ($i = 0; $i < $count; ++$i) {
|
454: | $name = $elements[$i]->getName(false);
|
455: | if ($name && method_exists($elements[$i], 'getValue')) {
|
456: | $values[$name] = &$elements[$i]->getValue($encode);
|
457: | }
|
458: | }
|
459: |
|
460: | return $values;
|
461: | }
|
462: |
|
463: | |
464: | |
465: | |
466: | |
467: |
|
468: | public function setClass($class)
|
469: | {
|
470: | $class = trim($class);
|
471: | if (!empty($class)) {
|
472: | $this->_class[] = $class;
|
473: | }
|
474: | }
|
475: |
|
476: | |
477: | |
478: | |
479: | |
480: |
|
481: | public function setExtra($extra)
|
482: | {
|
483: | if (!empty($extra)) {
|
484: | $this->_extra[] = $extra;
|
485: | }
|
486: | }
|
487: |
|
488: | |
489: | |
490: | |
491: | |
492: |
|
493: | public function setSummary($summary)
|
494: | {
|
495: | if (!empty($summary)) {
|
496: | $this->summary = strip_tags($summary);
|
497: | }
|
498: | }
|
499: |
|
500: | |
501: | |
502: | |
503: | |
504: |
|
505: | public function &getClass()
|
506: | {
|
507: | if (empty($this->_class)) {
|
508: | return false;
|
509: | }
|
510: | $classes = array();
|
511: | foreach ($this->_class as $class) {
|
512: | $classes[] = htmlspecialchars($class, ENT_QUOTES);
|
513: | }
|
514: |
|
515: | return implode(' ', $classes);
|
516: | }
|
517: |
|
518: | |
519: | |
520: | |
521: | |
522: |
|
523: | public function &getExtra()
|
524: | {
|
525: | $extra = empty($this->_extra) ? '' : ' ' . implode(' ', $this->_extra);
|
526: |
|
527: | return $extra;
|
528: | }
|
529: |
|
530: | |
531: | |
532: | |
533: | |
534: |
|
535: | public function setRequired(XoopsFormElement $formElement)
|
536: | {
|
537: | $this->_required[] = &$formElement;
|
538: | }
|
539: |
|
540: | |
541: | |
542: | |
543: | |
544: |
|
545: | public function &getRequired()
|
546: | {
|
547: | return $this->_required;
|
548: | }
|
549: |
|
550: | |
551: | |
552: | |
553: | |
554: | |
555: | |
556: | |
557: |
|
558: | public function insertBreak($extra = null)
|
559: | {
|
560: | }
|
561: |
|
562: | |
563: | |
564: | |
565: | |
566: | |
567: | |
568: |
|
569: | public function render()
|
570: | {
|
571: | }
|
572: |
|
573: | |
574: | |
575: |
|
576: | public function display()
|
577: | {
|
578: | echo $this->render();
|
579: | }
|
580: |
|
581: | |
582: | |
583: | |
584: | |
585: | |
586: | |
587: | |
588: | |
589: | |
590: | |
591: | |
592: | |
593: | |
594: | |
595: | |
596: | |
597: | |
598: | |
599: | |
600: | |
601: | |
602: | |
603: | |
604: |
|
605: | public function renderValidationJS($withtags = true)
|
606: | {
|
607: | $js = '';
|
608: | if ($withtags) {
|
609: | $js .= "\n<!-- Start Form Validation JavaScript //-->\n<script type='text/javascript'>\n<!--//\n";
|
610: | }
|
611: | $formname = $this->getName();
|
612: | $js .= "function xoopsFormValidate_{$formname}() { var myform = window.document.{$formname}; ";
|
613: | $elements =& $this->getElements(true);
|
614: | foreach ($elements as $elt) {
|
615: | if (method_exists($elt, 'renderValidationJS')) {
|
616: | $js .= $elt->renderValidationJS();
|
617: | }
|
618: | }
|
619: | $js .= "return true;\n}\n";
|
620: | if ($withtags) {
|
621: | $js .= "//--></script>\n<!-- End Form Validation JavaScript //-->\n";
|
622: | }
|
623: |
|
624: | return $js;
|
625: | }
|
626: |
|
627: | |
628: | |
629: | |
630: | |
631: | |
632: |
|
633: | public function assign(XoopsTpl $tpl)
|
634: | {
|
635: | $i = -1;
|
636: | $elements = array();
|
637: | if (count($this->getRequired()) > 0) {
|
638: | $this->_elements[] = "<tr class='foot'><td colspan='2'>* = " . _REQUIRED . '</td></tr>';
|
639: | }
|
640: | foreach ($this->getElements() as $ele) {
|
641: | ++$i;
|
642: | if (is_string($ele)) {
|
643: | $elements[$i]['body'] = $ele;
|
644: | continue;
|
645: | }
|
646: | $ele_name = $ele->getName();
|
647: | $ele_description = $ele->getDescription();
|
648: | $n = $ele_name ?: $i;
|
649: | $elements[$n]['name'] = $ele_name;
|
650: | $elements[$n]['caption'] = $ele->getCaption();
|
651: | $elements[$n]['body'] = $ele->render();
|
652: | $elements[$n]['hidden'] = $ele->isHidden();
|
653: | $elements[$n]['required'] = $ele->isRequired();
|
654: | if ($ele_description != '') {
|
655: | $elements[$n]['description'] = $ele_description;
|
656: | }
|
657: | }
|
658: | $js = $this->renderValidationJS();
|
659: | $tpl->assign($this->getName(), array(
|
660: | 'title' => $this->getTitle(),
|
661: | 'name' => $this->getName(),
|
662: | 'action' => $this->getAction(),
|
663: | 'method' => $this->getMethod(),
|
664: | 'extra' => 'onsubmit="return xoopsFormValidate_' . $this->getName() . '();"' . $this->getExtra(),
|
665: | 'javascript' => $js,
|
666: | 'elements' => $elements,
|
667: | 'rendered' => $this->render(),
|
668: | ));
|
669: | }
|
670: | }
|
671: | |