1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10:
11: namespace Kotchasan;
12:
13: 14: 15: 16: 17: 18: 19:
20: class Form extends \Kotchasan\KBase
21: {
22: 23: 24: 25: 26: 27:
28: public $ajax;
29: 30: 31: 32: 33: 34:
35: public $gform;
36: 37: 38: 39: 40:
41: public $javascript;
42: 43: 44: 45: 46:
47: private $attributes;
48: 49: 50: 51: 52:
53: private $tag;
54:
55: 56: 57: 58: 59:
60: public static function button($attributes = array())
61: {
62: $obj = new static();
63: if (isset($attributes['tag']) && $attributes['tag'] == 'input') {
64: $obj->tag = 'input';
65: } else {
66: $obj->tag = 'button';
67: }
68: unset($attributes['tag']);
69: $attributes['type'] = 'button';
70: $obj->attributes = $attributes;
71: return $obj;
72: }
73:
74: 75: 76: 77: 78:
79: public static function checkbox($attributes = array())
80: {
81: $obj = new static();
82: $obj->tag = 'input';
83: $attributes['type'] = 'checkbox';
84: $obj->attributes = $attributes;
85: return $obj;
86: }
87:
88: 89: 90: 91: 92:
93: public static function color($attributes = array())
94: {
95: $obj = new static();
96: $obj->tag = 'input';
97: $attributes['type'] = 'text';
98: $attributes['class'] = 'color';
99: $obj->attributes = $attributes;
100: return $obj;
101: }
102:
103: 104: 105: 106: 107:
108: public static function currency($attributes = array())
109: {
110: $obj = new static();
111: $obj->tag = 'input';
112: $attributes['type'] = 'text';
113: $attributes['class'] = 'currency';
114: $obj->attributes = $attributes;
115: return $obj;
116: }
117:
118: 119: 120: 121: 122:
123: public static function date($attributes = array())
124: {
125: $obj = new static();
126: $obj->tag = 'input';
127: $attributes['type'] = 'date';
128: $obj->attributes = $attributes;
129: return $obj;
130: }
131:
132: 133: 134: 135: 136:
137: public static function email($attributes = array())
138: {
139: $obj = new static();
140: $obj->tag = 'input';
141: $attributes['type'] = 'email';
142: $obj->attributes = $attributes;
143:
144: return $obj;
145: }
146:
147: 148: 149: 150: 151:
152: public static function file($attributes = array())
153: {
154: $obj = new static();
155: $obj->tag = 'input';
156: $attributes['type'] = 'file';
157: $attributes['class'] = 'g-file';
158: $obj->attributes = $attributes;
159: return $obj;
160: }
161:
162: 163: 164: 165: 166: 167:
168: public static function get2Input()
169: {
170: $hiddens = array();
171: foreach (self::$request->getQueryParams() as $key => $value) {
172: if ($value != '' && !preg_match('/.*?(username|password|token|time).*?/', $key) && preg_match('/^[_]+([^0-9]+)$/', $key, $match)) {
173: $hiddens[$match[1]] = '<input type="hidden" name="_'.$match[1].'" value="'.htmlspecialchars($value).'">';
174: }
175: }
176: foreach (self::$request->getParsedBody() as $key => $value) {
177: if ($value != '' && !preg_match('/.*?(username|password|token|time).*?/', $key) && preg_match('/^[_]+([^0-9]+)$/', $key, $match)) {
178: $hiddens[$match[1]] = '<input type="hidden" name="_'.$match[1].'" value="'.htmlspecialchars($value).'">';
179: }
180: }
181: return $hiddens;
182: }
183:
184: 185: 186: 187: 188:
189: public static function hidden($attributes = array())
190: {
191: $obj = new static();
192: $obj->tag = 'input';
193: $attributes['type'] = 'hidden';
194: $obj->attributes = $attributes;
195: return $obj;
196: }
197:
198: 199: 200: 201: 202:
203: public static function number($attributes = array())
204: {
205: $obj = new static();
206: $obj->tag = 'input';
207: $attributes['type'] = 'number';
208: $obj->attributes = $attributes;
209: return $obj;
210: }
211:
212: 213: 214: 215: 216:
217: public static function integer($attributes = array())
218: {
219: $obj = new static();
220: $obj->tag = 'input';
221: $attributes['type'] = 'integer';
222: $obj->attributes = $attributes;
223: return $obj;
224: }
225:
226: 227: 228: 229: 230:
231: public static function password($attributes = array())
232: {
233: $obj = new static();
234: $obj->tag = 'input';
235: $attributes['type'] = 'password';
236: $obj->attributes = $attributes;
237: return $obj;
238: }
239:
240: 241: 242: 243: 244:
245: public static function radio($attributes = array())
246: {
247: $obj = new static();
248: $obj->tag = 'input';
249: $attributes['type'] = 'radio';
250: $obj->attributes = $attributes;
251: return $obj;
252: }
253:
254: 255: 256: 257: 258:
259: public static function range($attributes = array())
260: {
261: $obj = new static();
262: $obj->tag = 'input';
263: $attributes['type'] = 'range';
264: $obj->attributes = $attributes;
265: return $obj;
266: }
267:
268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282:
283: public function render()
284: {
285: $prop = array();
286: $event = array();
287: foreach ($this->attributes as $k => $v) {
288: switch ($k) {
289: case 'itemClass':
290: case 'itemId':
291: case 'labelClass':
292: case 'label':
293: case 'comment':
294: case 'unit':
295: case 'value':
296: case 'dataPreview':
297: case 'previewSrc':
298: case 'accept':
299: case 'options':
300: case 'optgroup':
301: case 'multiple':
302: case 'validator':
303: case 'result':
304: case 'checked':
305: case 'datalist':
306: case 'button':
307: $$k = $v;
308: break;
309: case 'title':
310: $prop['title'] = 'title="'.strip_tags($v).'"';
311: break;
312: default:
313: if ($k == 'id') {
314: $id = $v;
315: } elseif (is_int($k)) {
316: $prop[$v] = $v;
317: } elseif ($v === true) {
318: $prop[$k] = $k;
319: } elseif ($v === false) {
320: } elseif (preg_match('/^on([a-z]+)/', $k, $match)) {
321: $event[$match[1]] = $v;
322: } elseif (!is_array($v)) {
323: $prop[$k] = $k.'="'.$v.'"';
324: $$k = $v;
325: }
326: break;
327: }
328: }
329: if (isset($id)) {
330: if (empty($name)) {
331: $name = $id;
332: $prop['name'] = 'name="'.$name.'"';
333: }
334: $id = trim(preg_replace('/[\[\]]+/', '_', $id), '_');
335: $prop['id'] = 'id="'.$id.'"';
336: }
337: if (isset(Html::$form)) {
338: if (isset($id) && Html::$form->gform) {
339: if (isset($validator)) {
340: $js = array();
341: $js[] = '"'.$id.'"';
342: $js[] = '"'.$validator[0].'"';
343: $js[] = $validator[1];
344: if (isset($validator[2])) {
345: $js[] = '"'.$validator[2].'"';
346: $js[] = empty($validator[3]) || $validator[3] === null ? 'null' : '"'.$validator[3].'"';
347: $js[] = '"'.Html::$form->attributes['id'].'"';
348: }
349: $this->javascript[] = 'new GValidator('.implode(', ', $js).');';
350: unset($validator);
351: }
352: foreach ($event as $on => $func) {
353: $this->javascript[] = '$G("'.$id.'").addEvent("'.$on.'", '.$func.');';
354: }
355: } elseif (!Html::$form->gform) {
356: foreach ($event as $on => $func) {
357: $prop['on'.$on] = 'on'.$on.'="'.$func.'()"';
358: }
359: }
360: }
361: if ($this->tag == 'select') {
362: unset($prop['type']);
363: if (isset($multiple)) {
364: $value = isset($value) ? $value : array();
365: } else {
366: $value = isset($value) ? $value : null;
367: }
368: if (isset($options) && is_array($options)) {
369: $datas = array();
370: foreach ($options as $k => $v) {
371: if (is_array($value)) {
372: $sel = in_array($k, $value) ? ' selected' : '';
373: } else {
374: $sel = $value == $k ? ' selected' : '';
375: }
376: if (is_int($k)) {
377: $datas[] = '<option value='.$k.$sel.'>'.$v.'</option>';
378: } else {
379: $datas[] = '<option value="'.$k.'"'.$sel.'>'.$v.'</option>';
380: }
381: }
382: $value = implode('', $datas);
383: } elseif (isset($optgroup) && is_array($optgroup)) {
384: $datas = array();
385: foreach ($optgroup as $group_label => $options) {
386: $datas[] = '<optgroup label="'.$group_label.'">';
387: foreach ($options as $k => $v) {
388: if (is_array($value)) {
389: $sel = in_array($k, $value) ? ' selected' : '';
390: } else {
391: $sel = $value == $k ? ' selected' : '';
392: }
393: $datas[] = '<option value="'.$k.'"'.$sel.'>'.$v.'</option>';
394: }
395: $datas[] = '</optgroup>';
396: }
397: $value = implode('', $datas);
398: }
399: } elseif (isset($value)) {
400: if ($this->tag === 'textarea') {
401: $value = str_replace(array('{', '}', '&'), array('{', '}', '&'), htmlspecialchars($value));
402: } elseif ($this->tag != 'button') {
403: if (is_int($value)) {
404: $prop['value'] = 'value='.$value;
405: } else {
406: $prop['value'] = 'value="'.str_replace('&', '&', htmlspecialchars($value)).'"';
407: }
408: }
409: }
410: if (empty($prop['title'])) {
411: if (!empty($comment)) {
412: $prop['title'] = 'title="'.strip_tags($comment).'"';
413: } elseif (!empty($label)) {
414: $prop['title'] = 'title="'.strip_tags($label).'"';
415: }
416: }
417: if (isset($dataPreview)) {
418: $prop['data-preview'] = 'data-preview="'.$dataPreview.'"';
419: }
420: if (isset($result)) {
421: $prop['data-result'] = 'data-result="result_'.$result.'"';
422: }
423: if (isset($accept) && is_array($accept)) {
424: $prop['accept'] = 'accept="'.Mime::getAccept($accept).'"';
425: }
426: if (isset($multiple)) {
427: $prop['multiple'] = 'multiple';
428: }
429: if (isset($checked) && isset($value) && $checked == $value) {
430: $prop['checked'] = 'checked';
431: }
432: if (isset($datalist) && is_array($datalist)) {
433: if (empty($prop['list'])) {
434: $list = $id.'-datalist';
435: } else {
436: $list = $prop['list'];
437: }
438: $prop['list'] = 'list="'.$list.'"';
439: $prop['autocomplete'] = 'autocomplete="off"';
440: }
441: $prop = implode(' ', $prop);
442: if ($this->tag == 'input') {
443: $element = '<'.$this->tag.' '.$prop.'>';
444: } elseif (isset($value)) {
445: $element = '<'.$this->tag.' '.$prop.'>'.$value.'</'.$this->tag.'>';
446: } else {
447: $element = '<'.$this->tag.' '.$prop.'></'.$this->tag.'>';
448: }
449: if (isset($datalist) && is_array($datalist)) {
450: $element .= '<datalist id="'.$list.'">';
451: foreach ($datalist as $k => $v) {
452: if (is_int($k)) {
453: $element .= '<option value='.$k.'>'.$v.'</option>';
454: } else {
455: $element .= '<option value="'.$k.'">'.$v.'</option>';
456: }
457: }
458: $element .= '</datalist>';
459: }
460: if (empty($itemClass)) {
461: $input = empty($comment) ? '' : '<div class="item"'.(empty($itemId) ? '' : ' id="'.$itemId.'"').'>';
462: $input = empty($unit) ? '' : '<div class="wlabel">';
463: if (empty($labelClass) && empty($label)) {
464: $input .= $element;
465: } elseif (isset($type) && ($type === 'checkbox' || $type === 'radio')) {
466: if (!empty($button)) {
467: $label = '<span>'.$label.'</span>';
468: }
469: $input .= '<label'.(empty($labelClass) ? '' : ' class="'.$labelClass.'"').'>'.$element.$label.'</label>';
470: } else {
471: $input .= '<label'.(empty($labelClass) ? '' : ' class="'.$labelClass.'"').'>'.(empty($label) ? '' : $label.' ').$element.'</label>';
472: }
473: if (!empty($unit)) {
474: $input .= '<span class="label">'.$unit.'</span></div>';
475: }
476: if (!empty($comment)) {
477: $input .= '<div class="comment"'.(empty($id) ? '' : ' id="result_'.$id.'"').'>'.$comment.'</div>';
478: }
479: } else {
480: if (!empty($unit)) {
481: $itemClass .= ' wlabel';
482: }
483: $input = '<div class="'.$itemClass.'"'.(empty($itemId) ? '' : ' id="'.$itemId.'"').'>';
484: if (isset($type) && $type === 'checkbox') {
485: $input .= '<label'.(empty($labelClass) ? '' : ' class="'.$labelClass.'"').'>'.$element.' '.(isset($label) ? $label : '').'</label>';
486: } else {
487: if (isset($dataPreview)) {
488: $input .= '<div class="file-preview" id="'.$dataPreview.'">';
489: if (isset($previewSrc)) {
490: if (preg_match_all('/\.([a-z0-9]+)(\?|$)/i', $previewSrc, $match)) {
491: $ext = strtoupper($match[1][0]);
492: if (in_array($ext, array('JPG', 'JPEG', 'GIF', 'PNG', 'BMP', 'WEBP', 'TIFF', 'ICO'))) {
493: $input .= '<a href="'.$previewSrc.'" target="preview" class="file-thumb" style="background-image:url('.$previewSrc.')"></a>';
494: } else {
495: $input .= '<a href="'.$previewSrc.'" target="preview" class="file-thumb">'.$ext.'</a>';
496: }
497: }
498: }
499: $input .= '</div>';
500: }
501: if (isset($label) && isset($id)) {
502: $input .= '<label for="'.$id.'">'.$label.'</label>';
503: }
504: $input .= '<span'.(empty($labelClass) ? '' : ' class="'.$labelClass.'"').'>'.$element.'</span>';
505: if (!empty($unit)) {
506: $input .= '<span class=label>'.$unit.'</span>';
507: }
508: }
509: if (!empty($comment)) {
510: $input .= '<div class="comment"'.(empty($id) ? '' : ' id="result_'.$id.'"').'>'.$comment.'</div>';
511: }
512: $input .= '</div>';
513: }
514: return $input;
515: }
516:
517: 518: 519: 520: 521:
522: public static function reset($attributes = array())
523: {
524: $obj = new static();
525: if (isset($attributes['tag']) && $attributes['tag'] == 'input') {
526: $obj->tag = 'input';
527: } else {
528: $obj->tag = 'button';
529: }
530: unset($attributes['tag']);
531: $attributes['type'] = 'reset';
532: if (isset($attributes['name']) && $attributes['name'] == 'reset') {
533: unset($attributes['name']);
534: }
535: if (isset($attributes['id']) && $attributes['id'] == 'reset') {
536: unset($attributes['id']);
537: }
538: $obj->attributes = $attributes;
539: return $obj;
540: }
541:
542: 543: 544: 545: 546:
547: public static function select($attributes = array())
548: {
549: $obj = new static();
550: $obj->tag = 'select';
551: $obj->attributes = $attributes;
552:
553: return $obj;
554: }
555:
556: 557: 558: 559: 560:
561: public static function submit($attributes = array())
562: {
563: $obj = new static();
564: if (isset($attributes['tag']) && $attributes['tag'] == 'input') {
565: $obj->tag = 'input';
566: } else {
567: $obj->tag = 'button';
568: }
569: unset($attributes['tag']);
570: $attributes['type'] = 'submit';
571: if (isset($attributes['name']) && $attributes['name'] == 'submit') {
572: unset($attributes['name']);
573: }
574: if (isset($attributes['id']) && $attributes['id'] == 'submit') {
575: unset($attributes['id']);
576: }
577: $obj->attributes = $attributes;
578: return $obj;
579: }
580:
581: 582: 583: 584: 585:
586: public static function tel($attributes = array())
587: {
588: $obj = new static();
589: $obj->tag = 'input';
590: $attributes['type'] = 'tel';
591: $obj->attributes = $attributes;
592: return $obj;
593: }
594:
595: 596: 597: 598: 599: 600: 601: 602: 603: 604: 605:
606: public static function text($attributes = array())
607: {
608: $obj = new static();
609: $obj->tag = 'input';
610: $attributes['type'] = 'text';
611: $obj->attributes = $attributes;
612: return $obj;
613: }
614:
615: 616: 617: 618: 619:
620: public static function textarea($attributes = array())
621: {
622: $obj = new static();
623: $obj->tag = 'textarea';
624: $obj->attributes = $attributes;
625: return $obj;
626: }
627:
628: 629: 630: 631: 632:
633: public static function time($attributes = array())
634: {
635: $obj = new static();
636: $obj->tag = 'input';
637: $attributes['type'] = 'time';
638: $obj->attributes = $attributes;
639: return $obj;
640: }
641:
642: 643: 644: 645: 646:
647: public static function url($attributes = array())
648: {
649: $obj = new static();
650: $obj->tag = 'input';
651: $attributes['type'] = 'url';
652: $obj->attributes = $attributes;
653: return $obj;
654: }
655: }
656: