Overview

Classes

  • Ws_logged_inModel
  • Ws_permissionsModel
  • Ws_role_permModel
  • Ws_rolesModel
  • Ws_user_roleModel
  • Ws_userModel
  • WsAuth
  • WsauthController
  • WsChart
  • WsConfig
  • WsController
  • WsDatabase
  • WsForm
  • WsImage
  • WsLocalize
  • WsModel
  • WsModelForm
  • WsModelGridView
  • WsUrl

Functions

  • __autoload
  • callHook
  • WsErrorHandler
  • Overview
  • Class
  1: <?php
  2: /**
  3:  * WsForm
  4:  * Generates HTML form and form widgets.
  5:  *
  6:  * Example usage:
  7:  *
  8:  * <code>
  9:  * $form = new WsForm(WsUrl::link('controller', 'form_action_script'));
 10:  *
 11:  * // widgets
 12:  * $form->textInput(array('name' => 'text_input'));
 13:  * $form->textInput(array(
 14:  *     'name' => 'date_input',
 15:  *     'type' => 'date',
 16:  *     'placeholder' => 'enter date'
 17:  * ));
 18:  *
 19:  * $form->show();
 20:  * </code>
 21:  *
 22:  */
 23: class WsForm
 24: {
 25:     /**
 26:      * @var string $_action Url of form action
 27:      *
 28:      */
 29:     private $_action = '';
 30:     /**
 31:      * @var string $_id ID of form
 32:      *
 33:      */
 34:     private $_id = '';
 35:     /**
 36:      * @var string $_validationRules Form validation rules
 37:      *
 38:      */
 39:     private $_validationRules = '';
 40:     /**
 41:      * @var string $submitButtonText Text for submit button
 42:      *
 43:      */
 44:     public $submitButtonText = '';
 45:     /**
 46:      * @var string $_form Form body
 47:      *
 48:      */
 49:     protected $_form = '';
 50:     /**
 51:      * @var boolean $_formEnded Ensure that submit button is shown only once
 52:      *
 53:      */
 54:     private $_formEnded = false;
 55: 
 56: 
 57:     function __construct($action = '')
 58:     {
 59:         $this->_action = $action;
 60:         $this->_id = 'WsForm_'.uniqid();
 61: 
 62:         $this->_form = '<div class="row">';
 63:         $this->_form .= '<div class="column column-12">';
 64:         $this->_form .= '<form id="'.$this->_id.'" ';
 65:         $this->_form .= 'class="ws_form" ';
 66:         $this->_form .= 'method="POST" enctype="multipart/form-data" ';
 67:         $this->_form .= 'role="form" action="'.$this->_action.'">';
 68:         $this->_form .= '<fieldset>';
 69: 
 70:         // prevent CSRF attack
 71:         if (isset($_SESSION['ws_auth_token'])) {
 72:             $this->_form .= '<input type="hidden" name="csrf" value="';
 73:             $this->_form .= $_SESSION["ws_auth_token"];
 74:             $this->_form .= '">';
 75:         }
 76:     }
 77: 
 78: 
 79:     public function __toString()
 80:     {
 81:         if (!$this->_formEnded) {
 82:             $this->formEnd();
 83:         }
 84: 
 85:         return $this->_form;
 86:     }
 87: 
 88: 
 89:     /**
 90:      * Append submit button and validation function at the form end
 91:      *
 92:      */
 93:     private function formEnd()
 94:     {
 95:         $this->_form .= '<div class="row">';
 96:         $this->_form .= '<div class="column column-12 text-center">';
 97:         $this->_form .= '<input type="submit" class="button success" id="';
 98:         $this->_form .= $this->_id.'_submit"';
 99:         $this->_form .= ' value="'.$this->submitButtonText.'"/>';
100:         $this->_form .= '</div></div>';
101: 
102:         $this->_form .= '</fieldset>';
103:         $this->_form .= '</form>';
104: 
105:         $this->_form .= '</div></div>';
106: 
107:         $this->_form .= '<script>';
108:         // for form validation
109:         $this->_form .= '$("#'.$this->_id.'").validate({';
110:         $this->_form .= 'submitHandler: function(form) {form.submit();}';
111:         $this->_form .= '});';
112:         $this->_form .= '</script>';
113: 
114:         // append this only once
115:         $this->_formEnded = true;
116:     }
117: 
118:     /**
119:      * Display generated form on screen
120:      *
121:      */
122:     public function show()
123:     {
124:         if (!$this->_formEnded) {
125:             $this->formEnd();
126:         }
127: 
128:         echo $this->_form;
129:     }
130: 
131:     /**
132:      * Add hidden input element to form
133:      *
134:      * @param array $params HTML parameters for <input type="hidden">
135:      *
136:      */
137:     public function hiddenInput($params = array())
138:     {
139:         // name of the widget
140:         if (isset($params['name'])) {
141:             $name = $params['name'];
142:         } else {
143:             $name = uniqid('WsFormUIInput_').uniqid();
144:         }
145:         // id of widget
146:         if (isset($params['id'])) {
147:             $id = $params['id'];
148:         } else {
149:             $id = $this->_id.'_'.$name;
150:         }
151:         // value
152:         if (isset($params['value']) and !empty($params['value'])) {
153:             $value = $params['value'];
154:         } else {
155:             $value = '';
156:         }
157: 
158:         // add element
159:         $this->_form .= '<input type="hidden"
160:             name="'.$name.'" value="'.$value.'"
161:             id="'.$id.'"
162:         />';
163:     }
164: 
165: 
166:     /**
167:      * Add text input element to form
168:      *
169:      * @param array $params HTML parameters for <input type="text">
170:      *
171:      */
172:     public function textInput($params = array())
173:     {
174:         // type of input field
175:         if (isset($params['type'])) {
176:             $type = $params['type'];
177:         } else {
178:             $type = 'text';
179:         }
180: 
181:         // name of the widget
182:         if (isset($params['name'])) {
183:             $name = $params['name'];
184:         } else {
185:             $name = uniqid('WsFormUIInput_').uniqid();
186:         }
187:         // id of widget
188:         if (isset($params['id'])) {
189:             $id = $params['id'];
190:         } else {
191:             $id = $this->_id.'_'.$name;
192:         }
193: 
194:         // value of vidget
195:         if (isset($params['value'])) {
196:             $value = $params['value'];
197:         } else {
198:             $value = '';
199:         }
200: 
201: 
202:         // label
203:         if (isset($params['label'])) {
204:             $label = $params['label'];
205:         } else {
206:             $label = '';
207:         }
208: 
209:         // custom class
210:         if (isset($params['class'])) {
211:             $class .= ' '.$params['class'];
212:         } else {
213:             $class = '';
214:         }
215: 
216:         // max length
217:         if (isset($params['maxlength'])) {
218:             $maxlength = $params['maxlength'];
219:         } else {
220:             switch ($type) {
221:                 case 'date':
222:                     $maxlength = 11;
223:                     $class .= ' webiness_datepicker';
224:                     $type = 'text';
225:                     $ro = 'readonly';
226:                     break;
227:                 case 'time':
228:                     $class .= ' webiness_timepicker';
229:                     $maxlength = 8;
230:                     $type = 'text';
231:                     $ro = 'readonly';
232:                     break;
233:                 case 'datetime-local':
234:                     $class .= ' webiness_datetimepicker';
235:                     $maxlength = 20;
236:                     $type = 'text';
237:                     $ro = 'readonly';
238:                     break;
239:                 case 'number':
240:                     $class .= ' webiness_numericinput';
241:                     $maxlength = 32;
242:                     $type = 'number';
243:                     break;
244:                 case 'file':
245:                     $class .= ' inputfile';
246:                     break;
247:                 default:
248:                     $maxlength = 60;
249:             }
250:         }
251: 
252:         // placeholder
253:         if (isset($params['placeholder'])) {
254:             $placeholder = $params['placeholder'];
255:         } else {
256:             $placeholder = '';
257:         }
258: 
259:         // readonly widget
260:         if (isset($params['readonly']) and ($params['readonly'] == true)) {
261:             $ro = 'readonly';
262:         } else {
263:             $ro = '';
264:         }
265: 
266:         // value is required
267:         if (isset($params['required']) and ($params['required'] == true)) {
268:             $rq = 'required';
269:         } else {
270:             $rq = '';
271:         }
272: 
273:         // add text input element
274:         if ($label !== '' and $type !== 'file') {
275:             $this->_form .= '<div class="row">';
276:             $this->_form .= '<div class="column column-12">';
277:             $this->_form .= '<label class="text-left" for="'.$id.'">';
278:             $this->_form .= $label;
279:             $this->_form .= '</label>';
280:             $this->_form .= '</div>';
281:             $this->_form .= '</div>';
282:         }
283: 
284:         // display link to file if type is file and picture thumbnail if file is
285:         // picture
286:         if ($type === 'file') {
287:             $this->_form .= '<div class="row">';
288:             $this->_form .= '<div class="column column-12">';
289:             $this->_form .= '<label class="text-left">';
290:             $this->_form .= $label;
291:             $this->_form .= '</label>';
292:             $this->_form .= '</div>';
293:             $this->_form .= '</div>';
294:             $this->_form .= '<div class="row">';
295:             $this->_form .= '<div class="column column-6">';
296:             if (get_called_class() === 'WsModelForm') {
297:                 $file = 'runtime/'.$this->getModelName().'/'.$value;
298:                 $file_url = WsSERVER_ROOT.'/runtime/'.$this->getModelName().'/'.$value;
299:                 if (file_exists(WsROOT.'/'.$file) && is_file(WsROOT.'/'.$file)) {
300:                     // if file is image then show it
301:                     $img = new WsImage();
302:                     if ($img->read($file)) {
303:                         $this->_form .= '<img width=100 height=100 '
304:                             .'src="'.$file_url.'" />';
305:                     } else {
306:                         $this->_form .= '<a href="'
307:                             .WsUrl::link(WsSERVER_ROOT.'/'.$file_url).'">';
308:                         $this->_form .= $value;
309:                         $this->_form .= '</a>';
310:                     }
311:                     unset ($img, $file, $file_url);
312:                 } else {
313:                     $this->_form .= WsLocalize::msg('no file selected ');
314:                 }
315:             }
316:             $this->_form .= '<input type="file"'
317:                 .' name="'.$name.'" '
318:                 .' id="'.$id.'"'
319:                 .' class="'.$class.'"'
320:                 .' placeholder="'.$placeholder.'"'
321:                 .' '.$ro.' '.$rq.'/>';
322:             $this->_form .= '<label for="'.$id.'">'
323:                 .WsLocalize::msg('Choose a file').'</label>';
324:             $this->_form .= '</div></div>';
325:         } else {
326:             $this->_form .= '<div class="row">';
327:             $this->_form .= '<div class="column column-12">';
328:             $this->_form .= '<input type="'.$type.'"'
329:                 .' name="'.$name.'" value="'.$value.'"'
330:                 .' id="'.$id.'"'
331:                 .' class="'.$class.'"'
332:                 .' placeholder="'.$placeholder.'"'
333:                 .' maxlength='.$maxlength.' '.$ro.' '.$rq.'/>';
334:             $this->_form .= '</div></div>';
335:         }
336:     }
337: 
338: 
339:     /**
340:      * Add multiline text input to form
341:      *
342:      * @param array $params HTML parameters for <textarea>
343:      *
344:      */
345:     public function textareaInput($params = array())
346:     {
347:         // name of the widget
348:         if (isset($params['name'])) {
349:             $name = $params['name'];
350:         } else {
351:             $name = uniqid('WsFormUIInput_').uniqid();
352:         }
353:         // id of widget
354:         if (isset($params['id'])) {
355:             $id = $params['id'];
356:         } else {
357:             $id = $this->_id.'_'.$name;
358:         }
359: 
360:         // value of vidget
361:         if (isset($params['value'])) {
362:             $value = $params['value'];
363:         } else {
364:             $value = '';
365:         }
366: 
367:         // label
368:         if (isset($params['label'])) {
369:             $label = $params['label'];
370:         } else {
371:             $label = '';
372:         }
373: 
374:         // placeholder
375:         if (isset($params['placeholder'])) {
376:             $placeholder = $params['placeholder'];
377:         } else {
378:             $placeholder = '';
379:         }
380: 
381:         // custom class
382:         if (isset($params['class'])) {
383:             $class = 'webiness_textarea '.$params['class'];
384:         } else {
385:             $class = 'webiness_textarea';
386:         }
387: 
388:         // readonly widget
389:         if (isset($params['readonly']) and ($params['readonly'] == true)) {
390:             $ro = 'readonly';
391:         } else {
392:             $ro = '';
393:         }
394: 
395:         // value is required
396:         if (isset($params['required']) and ($params['required'] == true)) {
397:             $rq = 'required';
398:         } else {
399:             $rq = '';
400:         }
401: 
402:         // add text area element
403:         if ($label != '') {
404:             $this->_form .= '<div class="row">';
405:             $this->_form .= '<div class="column column-12">';
406:             $this->_form .= '<label class="text-left" for="'.$id.'">';
407:             $this->_form .= $label;
408:             $this->_form .= '</label>';
409:             $this->_form .= '</div>';
410:             $this->_form .= '</div>';
411:         }
412:         $this->_form .= '<div class="row">';
413:         $this->_form .= '<div class="column column-12">';
414:         $this->_form .= '
415:             <textarea rows=5
416:                 name="'.$name.'"
417:                 id="'.$id.'"
418:                 class="'.$class.'"
419:                 placeholder="'.$placeholder.'"
420:                 '.$ro.' '.$rq.'>';
421:         $this->_form .= $value;
422:         $this->_form .= '</textarea>';
423:         $this->_form .= '</div></div>';
424:     }
425: 
426: 
427:     /**
428:      * Add checkbox to the form
429:      *
430:      * @param array $params HTML parameters for <input type="checkbox">
431:      *
432:      */
433:     public function booleanInput($params = array())
434:     {
435:         // name of the widget
436:         if (isset($params['name'])) {
437:             $name = $params['name'];
438:         } else {
439:             $name = uniqid('WsFormUIInput_').uniqid();
440:         }
441:         // id of widget
442:         if (isset($params['id'])) {
443:             $id = $params['id'];
444:         } else {
445:             $id = $this->_id.'_'.$name;
446:         }
447: 
448:         // label
449:         if (isset($params['label'])) {
450:             $label = $params['label'];
451:         } else {
452:             $label = '';
453:         }
454: 
455:         // custom class
456:         if (isset($params['class'])) {
457:             $class = ' '.$params['class'];
458:         } else {
459:             $class = '';
460:         }
461: 
462:         // readonly widget
463:         if (isset($params['readonly']) and ($params['readonly'] == true)) {
464:             $ro = 'readonly';
465:         } else {
466:             $ro = '';
467:         }
468: 
469:         // is checked
470:         if (isset($params['checked']) and ($params['checked'] == true)) {
471:             $ch = 'checked';
472:         } else {
473:             $ch = '';
474:         }
475: 
476:         // add boolean element
477:         $this->_form .= '<div class="row">';
478:         $this->_form .= '<div class="column column-12">';
479: 
480:         if ($label != '') {
481:             $this->_form .= '<label for="'.$id.'">';
482:         }
483: 
484:         $this->_form .= '<input type="hidden" value="false" name="'.$name.'"/>';
485:         $this->_form .= '<input type="checkbox"
486:             name="'.$name.'"
487:             id="'.$id.'"
488:             value="true"
489:             data-val="true"
490:             class="ws_checkbox '.$class.'"
491:             '.$ro.'
492:             '.$ch.' />';
493: 
494:         if ($label != '') {
495:             $this->_form .= '<span>'.$label.'</span>';
496:             $this->_form .= '</label>';
497:         }
498: 
499:         $this->_form .= '</div>';
500:         $this->_form .= '</div>';
501:     }
502: 
503: 
504:     /**
505:      * Add selection box to the form
506:      *
507:      * @param array $list List of selections
508:      * @param array $params HTML parameters for <select> element
509:      *
510:      */
511:     public function selectInput($list, $params = array())
512:     {
513:         // name of the widget
514:         if (isset($params['name'])) {
515:             $name = $params['name'];
516:         } else {
517:             $name = uniqid('WsFormUIInput_').uniqid();
518:         }
519:         // id of widget
520:         if (isset($params['id'])) {
521:             $id = $params['id'];
522:         } else {
523:             $id = $this->_id.'_'.$name;
524:         }
525: 
526:         // label
527:         if (isset($params['label'])) {
528:             $label = $params['label'];
529:         } else {
530:             $label = '';
531:         }
532: 
533:         // value of vidget
534:         if (isset($params['value'])) {
535:             $value = $params['value'];
536:         } else {
537:             $value = '';
538:         }
539: 
540:         // custom class
541:         if (isset($params['class'])) {
542:             $class = 'webiness_select '.$params['class'];
543:         } else {
544:             $class = 'webiness_select';
545:         }
546: 
547:         // value is required
548:         if (isset($params['required']) and ($params['required'] == true)) {
549:             $rq = 'required';
550:         } else {
551:             $rq = '';
552:         }
553: 
554:         // add select element
555:         if ($label != '') {
556:             $this->_form .= '<div class="row">';
557:             $this->_form .= '<div class="column column-12">';
558:             $this->_form .= '<label class="text-left" for="'.$id.'">';
559:             $this->_form .= $label;
560:             $this->_form .= '</label>';
561:             $this->_form .= '</div>';
562:             $this->_form .= '</div>';
563:         }
564:         $this->_form .= '<div class="row">';
565:         $this->_form .= '<div class="column column-12">';
566:         $this->_form .= '
567:             <select
568:                 style="width: 100%"
569:                 name="'.$name.'"
570:                 id="'.$id.'"
571:                 class="'.$class.'"
572:                 '.$rq.' >';
573: 
574:         foreach ($list as $l) {
575:             if ($l['display'] === $value or $l['option'] === $value) {
576:                 $this->_form .= '<option value="'.$l['option']
577:                     .'" selected>'.$l['display'].'</option>';
578:             } else {
579:                 $this->_form .= '<option value="'.$l['option'].'">'
580:                     .$l['display'].'</option>';
581:             }
582:         }
583:         $this->_form .= '</select>';
584:         $this->_form .= '</div>';
585:         $this->_form .= '</div>';
586:     }
587: 
588: 
589:     /**
590:      * Append custom HTML to form
591:      *
592:      * @param string $html HTML to append to form
593:      *
594:      */
595:     public function appendHTML($html)
596:     {
597:         $this->_form .= $html;
598:     }
599: }
600: 
API documentation generated by ApiGen