This repository was archived by the owner on Feb 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTbs.php
2814 lines (2564 loc) · 76.4 KB
/
Tbs.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Tbs is a PHP class to generate Html components for twitter bootstrap layouts.
*
* @license MIT (see LICENSE for more info)
*
* @author Manuel Tancoigne
*/
class Tbs {
/**
* @var string Icon pack name (glyphicon, fa, icon,...)
*
* Link to glyphicons: http://glyphicons.com/examples-of-use/
* Link to FontAwesome icons: http://fontawesome.io/
*/
public $iconPack = 'glyphicon';
/**
* Defines the current form style.
* @var string
*/
public $formStyle = 'default';
/**
* Default label width in a horizontal form. Can be overriden by formOpen()
* @var int
*/
public $formWidth = 3;
/**
* CSS grid size
* @var int
*/
public $gridSize = 12;
/**
* Current navbar id
* @var string
*/
private $_navbarId = null;
/**
* Counter for some element's Ids.
* @var integer
*/
private $_idCounter = 0;
/**
* Creates a button
*
* @param string $content Button content
* @param string $url target url
* @param array $options List of options
*
* @return string Html code to be displayed
*
* @link http://getbootstrap.com/css/#buttons Link to the TBS documentation about this element
* ---
*
* Options:
* --------
* - class: string, *null
* Additionnal classes for the dropdown element
* - size: string, big|*standard|small|xsmall
* Button size
* - type: string, *standard|primary|success|info|warning|danger|link|submit|reset
* Button type
* - tag: string, *a|button|input
* Button tag
* - Other attributes that can apply to the "a" or "button" elements
*
* If no $url is provided, a button element is created instead of a link.
* Additionnal classes can be seen on the TBS CSS page: btn-block, active, disabled,...
* If tag is different than "a" and $url is set, tag will be "a"
*/
public function button($content, $url = null, $options = array()) {
$defaults = array(
'class' => null,
'size' => 'standard',
'type' => 'standard',
'tag' => 'a',
);
// Get options
$optionsList = $this->_getOptions($defaults, $options);
// Get attributes
$attributesList = $this->_getAttributes($defaults, $options);
// Base class
$attributesList['class'] = 'btn ' . $optionsList['class'];
// Size
switch (strtolower($optionsList['size'])) {
case 'big':
$attributesList['class'].=' btn-lg';
break;
case 'small':
$attributesList['class'].=' btn-sm';
break;
case 'xsmall':
$attributesList['class'].=' btn-xs';
break;
}
// Type
switch ($optionsList['type']) {
case 'primary':
$attributesList['class'].=' btn-primary';
break;
case 'success':
$attributesList['class'].=' btn-success';
break;
case 'info':
$attributesList['class'].=' btn-info';
break;
case 'warning':
$attributesList['class'].=' btn-warning';
break;
case 'danger':
$attributesList['class'].=' btn-danger';
break;
case 'submit':
$attributesList['class'].=' btn-primary';
$optionsList['tag'] = 'input';
$attributesList['type'] = 'submit';
$url = null;
break;
case 'reset':
$attributesList['class'].=' btn-danger';
$optionsList['tag'] = 'input';
$attributesList['type'] = 'reset';
$url = null;
break;
default:
$attributesList['class'].=' btn-default';
}
// Passing some options as attributes
$attributesList['href'] = $url;
// HTML Attributes
// Special button type for standard inputs
if ($optionsList['tag'] == 'input' && !isset($attributesList['type'])) {
$attributesList['type'] = 'button';
}
$attributes = $this->_prepareHTMLAttributes($attributesList);
switch ($optionsList['tag']) {
case 'a':
return "<a{$attributes}>{$content}</a>";
break;
case 'input':
return "<input{$attributes}/>";
break;
case 'button':
return "<button{$attributes}>{$content}</button>";
}
}
/**
* Creates a dropdown menu to be used with dropdown buttons, or alone...
* This method does not generate a button, just the dropdwon menu.
*
* @param array $list List of elements from dropdownItem()
* @param array $options List of options for this element
*
* @return string Html code to be displayed
*
* @link http://getbootstrap.com/components/#dropdowns Link to the TBS documentation about this element
* ---
*
* Options:
* --------
* - align: string, *right|left
* Toggles the dropdown orientation
* - class: string, *null
* Additionnal classes for the dropdown element
* - Other attributes that can apply to the "ul" element
*/
public function dropdown($list, $options = array()) {
// Defaults:
$defaults = array(
'class' => null,
'align' => 'right',
);
// Get Options
$optionsList = $this->_getOptions($defaults, $options);
// Get Attributes
$attributesList = $this->_getAttributes($defaults, $options);
// Add classes to attributes
$attributesList['class'] = "dropdown-menu {$optionsList['class']}";
// Align
$attributesList['class'] .= " dropdown-menu-{$optionsList['align']}";
// HTML Attributes
$attributes = $this->_prepareHTMLAttributes($attributesList);
return "<ul{$attributes}>\n\t".implode("\n\t", $list)."</ul>\n";
}
/**
* Creates a dropdown menu item
*
* @param string $title
* @param string $url
* @param array $options
*
* @return string Item to pass in dropdown()'s $list
* ---
*
* Options:
* --------
* - class: string, *null
* Additionnal classes for the dropdown element
* - type: string, *default|divider|header
*/
public function dropdownItem($title = null, $url = '#', $options = array()) {
$defaults = array(
'class' => null,
'type' => 'default',
);
$optionsList = $this->_getOptions($defaults, $options);
// Get Attributes
$attributesList = $this->_getAttributes($defaults, $options);
// Add classes to attributes
$attributesList['class'] = $optionsList['class'];
// Type
switch ($optionsList['type']) {
case 'divider':
$attributesList['class'].=' divider';
$title = null;
break;
case 'header':
$attributesList['class'].=' dropdown-header';
break;
default:
$title = $this->link($title, $url, array('tabindex' => '-1'));
}
// HTML Attributes
$attributes = $this->_prepareHTMLAttributes($attributesList);
return "<li{$attributes}>{$title}</li>";
}
/**
* Creates a group of buttons
*
* @param array $buttons List of buttons elements from button() or buttonDropdown()
* @param array $options List of options for this element
*
* @return string Html code to be displayed
*
* @link http://getbootstrap.com/components/#btn-groups Link to the TBS documentation about this element
* ---
*
* Options:
* --------
* - class: string *null
* Additionnal classes for the button group
* - size: string big|*standard|small|xsmall
* Button sizes. Don't define custom styles for the buttons, but define one for dropdowns.
*/
public function buttonGroup($buttons, $options = array()) {
// Defaults:
$defaults = array(
'class' => null,
'size' => 'standard',
);
// Get Options
$optionsList = $this->_getOptions($defaults, $options);
// Get Attributes
$attributesList = $this->_getAttributes($defaults, $options);
// Add classes to attributes
$attributesList['class'] = "btn-group {$optionsList['class']}";
// Size
switch ($optionsList['size']) {
case 'big':
$attributesList['class'].=' btn-group-lg';
break;
case 'small':
$attributesList['class'].=' btn-group-sm';
break;
case 'xsmall':
$attributesList['class'].=' btn-group-xs';
break;
default:
break;
}
// HTML Attributes
$attributes = $this->_prepareHTMLAttributes($attributesList);
$out = "<div{$attributes}>";
foreach ($buttons as $b) {
$out.="\n$b";
}
$out.="\n</div>";
return $out;
}
/**
* Creates a thumbnail
*
* @param string $src Image url
* @param array $options List of options for this element
*
* @return string Html code to be displayed
*
* @link http://getbootstrap.com/components/#thumbnails Link to the TBS documentation about this element
* ---
*
* Options:
* --------
* - class: string *null
* Additionnal classes for the button group
* - url string, *null
* Destination url
* - caption string, *null
* Image description
* - title string, *file name
* Title attribute
* - alt string, *title option
* Alt attribute
*
*/
public function thumbnail($src, $options = array()) {
$defaults = array(
'class' => null,
'url' => null,
'caption' => null,
'title' => null,
'alt' => null,
);
// Get Options
$optionsList = $this->_getOptions($defaults, $options);
// Get Attributes
$attributesList = $this->_getAttributes($defaults, $options);
// Add classes to attributes
$attributesList['class'] = "thumbnail {$optionsList['class']}";
// Title
if (!empty($optionsList['title'])) {
$title = $optionsList['title'];
} else {
$title = pathinfo($src, PATHINFO_FILENAME);
}
// Alt
if (!empty($optionsList['alt'])) {
$alt = $optionsList['alt'];
} else {
$alt = $title;
}
// Image:
$content = "<img data-src=\"{$src}\" title=\"{$title}\" alt=\"$alt\" />";
// Url
if (!empty($optionsList['url'])) {
$content = "<a href=\"{$optionsList['url']}\">$content</a>";
}
if (!empty($optionsList['caption'])) {
$content.='<div class="caption">' . $optionsList['caption'] . '</div>';
}
// HTML Attributes
$attributes = $this->_prepareHTMLAttributes($attributesList);
return "<div{$attributes}>{$content}</div>";
}
/**
* Creates a thumbnails list
*
* @param string $thumbnails List of thumbnails from thumbnail()
* @param array $options List of options for this element
*
* @return string Html code to be displayed
*
* @link http://getbootstrap.com/components/#thumbnails Link to the TBS documentation about this element
* ---
*
* Options:
* --------
* - mobileWidth: int, *6
* Grid size for mobile display
* - desktopWidth: int, *3
* Grid size for desktop displays
* - tabletWidth: int, *3
*/
public function thumbList($thumbnails, $options = array()) {
$defaults = array(
'mobileWidth' => 6,
'desktopWidth' => 3,
'tabletWidth' => 3,
);
// Get Options
$optionsList = $this->_getOptions($defaults, $options);
// Get Attributes
$attributesList = $this->_getAttributes($defaults, $options);
$content = null;
foreach ($thumbnails as $t) {
$content.="\n\t<div class=\"col-sm-{$optionsList['mobileWidth']} col-md-{$optionsList['tabletWidth']} col-lg-{$optionsList['desktopWidth']}\">\n\t\t$t\n\t</div>";
}
// HTML Attributes
$attributes = $this->_prepareHTMLAttributes($attributesList);
return "<div class=\"row\"{$attributes}>\n\t{$content}\n</div>";
}
/**
* Creates a toolbar
*
* @param array $buttonGroups List of buttonGroup() items
* @param array $options List of options for this element
*
* @return string Html code to be displayed
*
* @link http://getbootstrap.com/components/#btn-groups-toolbar Link to the TBS documentation about this element
* ---
*
* Options:
* --------
* - class: string *null
* Additionnal classes for the button group
* - Other attributes that can apply to a "div" element.
*
*/
public function toolbar($buttonGroups, $options = array()) {
$defaults = array(
'class' => null,
);
// Get Options
$optionsList = $this->_getOptions($defaults, $options);
// Get Attributes
$attributesList = $this->_getAttributes($defaults, $options);
// Add classes to attributes
$attributesList['class'] = "btn-toolbar {$optionsList['class']}";
// HTML Attributes
$attributes = $this->_prepareHTMLAttributes($attributesList);
$out = "<div{$attributes}>";
foreach ($buttonGroups as $v) {
$out.="\n$v";
}
$out.="\n</div>";
return $out;
}
/**
* Creates a button with a dropdown menu
*
* @param string title Button title
* @param array $dropdown Dropdown content from dropdown()
* @param array $buttonOptions List of options for the button (same as button())
* @param array $options List of options for the button wrapper
*
* @return string Html code to be displayed
*
* @link http://getbootstrap.com/components/#btn-dropdowns Link to the TBS documentation about this element
* ---
*
* Options:
* --------
* - class: string, *null
* Additionnal classes for the button
* - size: string, big|*standard|small|xsmall
* Button size
* - type: string, *standard|primary|success|info|warning|danger|link
* Button type
* - tag: string, *a|button|submit|input
* Button tag
* - url: string, *null
* Button's URL
* - split bool, *false
* Creates a split button dropdown
* - dropup bool, *false
* Creates a dropup variation.
* - disabled bool, *false
* Disables the button
* - Other attributes that can apply to a "div" element.
* If you want to make a split button with an URL, pass the "url" option in the $buttonOptions array
*
*/
public function buttonDropdown($title, $dropdown, $options = array()) {
$defaults = array(
'class' => null,
'size' => 'standard',
'type' => 'standard',
'tag' => 'a',
'url' => null,
'split' => false,
'dropup' => false,
'disabled' => false
);
// Get Options
$optionsList = $this->_getOptions($defaults, $options);
// Get Attributes
$attributesList = $this->_getAttributes($defaults, $options);
// Add classes to attributes
$attributesList['class'] = $optionsList['class'];
// Prepare button options
$btnOptions = array('class' => $optionsList['class']);
// Type will be passed to button();
$btnOptions['type'] = $optionsList['type'];
// Tag will be passed to button();
$btnOptions['tag'] = $optionsList['tag'];
// Url will be passed to button()
$url = $optionsList['url'];
// Disabled will be passed to button()
$btnOptions['disabled'] = $optionsList['disabled'];
// Split
$split = $optionsList['split'];
// Dropup
if ($optionsList['dropup'] === true) {
$attributesList['class'].=' dropup';
}
$buttons = array();
// Creating button
if ($split) {
$buttons[] = $this->button($title, $url, $btnOptions);
$caretOptions = $btnOptions;
$caretOptions['class'].=' dropdown-toggle';
$caretOptions['data-toggle'] = 'dropdown';
$buttons[] = $this->button('<span class="caret"></span><span class="sr-only">Toggle Dropdown</span>', null, $caretOptions);
} else {
// Updating button class and attributes:
$btnOptions['class'].=' dropdown-toggle';
$btnOptions['data-toggle'] = 'dropdown';
$buttons[] = $this->button($title . ' <span class="caret"></span>', $url, $btnOptions);
}
// Creating dropdown
$buttons[] = $dropdown;
return $this->buttonGroup($buttons, array('class' => $optionsList['class'], 'size' => $optionsList['size']));
}
/**
* Creates an input element to be used in forms
*
* @param string $name Input name
* @param string $type Input type in text|password|datetime|datetime-local|date|month|time|week|number|email|url|search|tel|color|button|submit|checkbox|radio|select|static
* @param string $value Value
* @param array $options List of options for this element
*
* @return string Html code to be displayed
*
* @link http://getbootstrap.com/css/#forms-controls Link to the TBS documentation about this element
* ---
*
* Options:
* --------
* - class: string, *null
* Additionnal class for the element
* - default bool, *false
* Default state for a radio or checkbox element
* - help string, *null
* Input description (useful for radios/checkboxes)
* - disabled bool, *false
* Defines if the element is disabled.
* - required bool, *false
* Defines if the field is required
* - value string, *null
* Input value
* - label string, *$name
* Label caption
* - caption string, *null
* Radio/checkbox caption
*
* To create a select, use "inputSelect()" instead
*
*/
public function input($name, $type, $options = array()) {
// Defaults:
$defaults = array(
'label' => $name,
'class' => null,
'default' => false,
'help' => null,
'disabled' => false,
'required' => false,
'value' => null,
'caption' => null,
);
// Get Options
$optionsList = $this->_getOptions($defaults, $options);
// Get Attributes
$attributesList = $this->_getAttributes($defaults, $options);
$attributesList['id'] = $name;
// Help/Description
$help = $optionsList['help'];
// Disabled state
$disabled = null;
if ($optionsList['disabled']) {
$disabled = ' disabled';
}
// Required state
$required = null;
if ($optionsList['required']) {
$required = ' required';
}
// Value
$value = trim($optionsList['value']);
// Value as attribute, to use only in some cases
$attrValue = $this->_cleanAttribute('value', $value);
// HTML Attributes
$attributes = $this->_prepareHTMLAttributes($attributesList);
// Checked state (radio/checkbox)
$checked = null;
if ($optionsList['default']) {
$checked = ' checked="checked"';
}
// Element
$element = null;
$textInputs = array('text', 'password', 'datetime', 'datetime-local', 'date', 'month', 'time', 'week', 'number', 'email', 'url', 'search', 'tel', 'color');
if (in_array($type, $textInputs)) {
// Input field
$class = $this->_cleanStrings(array('form-control', $optionsList['class']));
$element = "\n<input type=\"{$type}\" name=\"{$name}\" class=\"{$class}\"{$disabled}{$attrValue}{$attributes} />";
} elseif ($type === 'static') {
// Static field
$class = $this->_cleanStrings(array('form-control-static', $optionsList['class']));
$element = "\n<p class=\"{$class}\"$attributes>$value</p>";
} elseif ($type === 'checkbox') {
// Checkbox
$class = $this->_cleanStrings(array('checkbox', $optionsList['class']));
$element = "\n<div class=\"{$class}\">\n\t<label for=\"{$attributesList['id']}\">\n\t\t<input type=\"checkbox\" name=\"$name\"{$checked}{$attrValue}{$disabled}{$attributes} />{$optionsList['caption']}\n\t</label>\n</div>";
} elseif ($type === 'radio') {
// radio
$class = $this->_cleanStrings(array('radio', $optionsList['class']));
$element = "\n<div class=\"{$class}\">\n\t<label for=\"{$attributesList['id']}\">\n\t\t<input type=\"radio\" name=\"$name\"{$checked}{$attrValue}{$disabled}{$attributes} />{$optionsList['caption']}\n\t</label>\n</div>";
} elseif ($type === 'textarea') {
// Textareas
$class = $this->_cleanStrings(array('form-control', $optionsList['class']));
$element = "\n<textarea class=\"{$class}\" name=\"$name\"{$disabled}{$attributes}>$value</textarea>";
} elseif ($type === 'submit') {
// Updating options
$options['tag'] = 'input';
$options['type'] = 'submit';
$options['name'] = $name;
$options['value'] = $value;
$element = $this->button($value, null, $options);
} elseif ($type === 'reset') {
$options['tag'] = 'input';
$options['type'] = 'reset';
$options['name'] = $name;
$options['value'] = $value;
$optionsList['label'] = null;
$element = $this->button($value, null, $options);
} elseif ($type === 'button') {
$options['tag'] = 'input';
$options['type'] = 'standard';
$options['name'] = $name;
$options['value'] = $value;
$optionsList['label'] = null;
$element = $this->button($value, null, $options);
} elseif ($type === 'file') {
// Files
$element = "\n<input type=\"file\" name=\"$name\"{$disabled}{$attributes}/>";
} else {
// Default
$class = $this->_cleanStrings(array('form-control', $optionsList['class']));
$element = "\n<input type=\"text\" name=\"$name\" class=\"{$class}\"{$disabled}{$attrValue}{$attributes} />";
}
if (!empty($optionsList['label']) && !in_array($type, array('radio', 'checkbox', 'submit', 'reset', 'button'))) {
$out = "<div class=\"form-group\">\n";
$out.="\t<label for=\"{$attributesList['id']}\">{$optionsList['label']}</label>\n";
$out.=$element . '</div>';
return $out;
} else {
return $element;
}
}
/**
* Creates a select input
*
* @param string $name Input name
* @param array $list List of elements. Should be like array('caption'=>'value', 'caption'=>'value')
* @param array $options List of options for the select element
*
* @return string Html code to be displayed
*
* @link http://getbootstrap.com/css/#forms-controls Link to the TBS documentation about this element
* ---
*
* Options:
* --------
* - class: string, *null
* Additionnal classes for the button
* - default: string, *null
* Default element's value.
* - multiple: boolean, *false
* Defines if the list is multiple or not
* If you want to make a split button with an URL, pass the "url" option in the $buttonOptions array
*
*/
public function inputSelect($name, $list, $options = array()) {
// Defaults:
$defaults = array(
'class' => null,
'default' => null,
'multiple' => false,
);
// Get Options
$optionsList = $this->_getOptions($defaults, $options);
// Get Attributes
$attributesList = $this->_getAttributes($defaults, $options);
// Add classes to attributes
$attributesList['class'] = "form-control{$optionsList['class']}";
$default = $optionsList['default'];
// Multiple
$multiple = null;
if ($optionsList['multiple']) {
$multiple = ' multiple';
}
// HTML Attributes
$attributes = $this->_prepareHTMLAttributes($attributesList);
$out = "\n<select name=\"$name\"{$attributes}{$multiple}>";
$out.= $this->_makeSelectList($list, $default);
$out.="\n</select>";
return $out;
}
/**
* Creates a list of options for a select element. Can be really recursive, but to be valid,
* you can't have sub-option groups...
*
* @param array $list List of elements (array(group=>array(element1=>val1), element2=>val2,...)
* @param string $default Default value to be selected
*
* @return string Html code for the options list
*/
private function _makeSelectList($list, $default = null) {
$out = null;
foreach ($list as $c => $v) {
if (is_array($v)) {
$out.="\n<optgroup label=\"$c\">";
$out.=$this->_makeSelectList($v, $default);
$out.="\n</optgroup>";
} else {
$selected = null;
if ($v === $default) {
$selected = ' selected="selected"';
}
$out.="\n\t<option value=\"$v\"$selected>$c</option>";
}
}
return $out;
}
/**
* Opens a form
*
* @param array $name Form name
* @param array $options List of options for the button wrapper
*
* @return string Html code to be displayed
*
* @link http://getbootstrap.com/css/#forms Link to the TBS documentation about this element
* ---
*
* Options:
* --------
* - class: string, *null
* Additionnal classes for the button
* - style: string, *null|inline|horizontal
* Form display style
* - width int, *3|[1-11]
* Label width for horizontal forms
* - file: bool, *false
* Defines if the form contains a upload field.
* - method: string, *POST
* Default send method
*
*/
public function formOpen($name, $options = array()) {
// Defaults:
$defaults = array(
'class' => null,
'style' => $this->formStyle,
'width' => $this->formWidth,
'file' => false,
'method' => 'POST',
);
// Get Options
$optionsList = $this->_getOptions($defaults, $options);
// Get Attributes
$attributesList = $this->_getAttributes($defaults, $options);
// Add classes to attributes
$attributesList['class'] = "{$optionsList['class']}";
// Style
switch ($optionsList['style']) {
case 'horizontal':
$attributesList['class'].=' form-horizontal';
break;
case 'inline':
$attributesList['class'].=' form-inline';
break;
default:
break;
}
$this->formStyle = $optionsList['style'];
// File
if ($optionsList['file']) {
$attributesList['enctype'] = 'multipart/form-data';
}
// Default method
$attributesList['method'] = $optionsList['method'];
// Horizontal forms: label width
$this->formWidth = $optionsList['width'];
// HTML Attributes
$attributes = $this->_prepareHTMLAttributes($attributesList);
// Output
return "<form{$attributes} role=\"form\">\n";
}
/**
* Closes an opened form. You can add a submit and a reset button in the $option array
*
* @param array $options A list of options
*
* @return string HTML code to display
*
* ---
* Options:
* --------
* - submit bool, *false
* If true, a submit button will be created
* - reset bool, *false
* If true, a reset button will be created
*
*/
public function formClose($options = array()) {
// Defaults:
$defaults = array(
'submit' => false,
'reset' => false,
);
// Get Options
$optionsList = $this->_getOptions($defaults, $options);
$out = null;
// Submit
if ($optionsList['submit']) {
$out.=$this->input(null, 'submit');
}
// Reset
if ($optionsList['reset']) {
$out.=$this->input(null, 'reset');
}
return $out . "\n</form>";
}
/**
* Creates a form group : a label + an element. No need to use it for checkboxes.
* @param string $label Input label
* @param string $input Output from input() or inputSelect()
* @param array $options List of options
*
* @return string Html code to display
*
* @link http://getbootstrap.com/css/#forms Link to the TBS documentation about this element
* ---
* Options:
* --------
* - class: string, *null
* Additionnal classes for the button
* - style: string, *null|inline|horizontal
* Form display style
* - labelWidth : int, *2|[1-12]
* For horizontal display, the label width. Should be between 1 and 11
* in standard grid system.
*/
public function formGroup($label, $input, $options = array()) {
// Defaults:
$defaults = array(
'class' => null,
'style' => $this->formStyle,
'labelWidth' => $this->formWidth,
);
}
/**
* Creates an input group
*
* @param string $input Input Html element from input()
* @param array $options List of options for this element
*
* @return string Html code to be displayed
*
* @link http://getbootstrap.com/components/#input-groups Link to the TBS documentation about this element
* ---
*
* Options:
* --------
*
*
*/
public function inputGroup($input, $options = array()) {
// Defaults:
$defaults = array(
'class' => null,
);
}
/**
* Creates a nav item.
*
* @param type $content
* @param type $url
* @param type $options
* @return type
*
* ---
*
* Options
* -------
* - class: string, *null
* Additionnal classes for the ol element
* - disabled: bool, *false
* Defines if the item is enabled or not
* - active: bool, *false
* Defines an item as the current item
* - url string *#
* Url for this items
* - dropdown: bool, *false
* Defines is the item is a dropdown item. If so, define the dropdownContent option.
* - dropdownContent: string *null
* Dropdown content from dropdown(). Use only if dropdown is set to true
*
*/
public function navItem($content, $options = array()) {
$defaults = array(
'class' => null,
'disabled' => false,
'active' => false,
'url' => '#',
'dropdown' => false,
'dropdownContent' => null,
);
// Get Options
$optionsList = $this->_getOptions($defaults, $options);
// Get Attributes
$attributesList = $this->_getAttributes($defaults, $options);
// Add classes to attributes
$attributesList['class'] = $optionsList['class'];
// Enabled
if ($optionsList['disabled']) {
$attributesList['class'] .= ' disabled';
}
// Active
if ($optionsList['active']) {
$attributesList['class'] .= ' active';
}