-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form.php
1256 lines (1096 loc) · 30.5 KB
/
Form.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
class Form {
public $model_array=false;
/**
* Form Declaration
*
* Creates the opening portion of the form.
*
* @param string the URI segments of the form destination
* @param array a key/value pair of attributes
* @param array a key/value pair hidden data
* @return string
*/
function form_open($action = '', $attributes = array(), $hidden = array())
{
/*$CI =& get_instance();*/
$attributes = $this->_attributes_to_string($attributes);
if (stripos($attributes, 'method=') === FALSE)
{
$attributes .= ' method="post"';
}
//if codeigniter is there
if (function_exists('get_instance')) {
$CI =& get_instance();
if (stripos($attributes, 'accept-charset=') === FALSE)
{
$attributes .= ' accept-charset="'.strtolower(config_item('charset')).'"';
}
// Add CSRF field if enabled, but leave it out for GET requests and requests to external websites
if ($CI->config->item('csrf_protection') === TRUE && strpos($action, $CI->config->base_url()) !== FALSE && ! stripos($form, 'method="get"'))
{
$hidden[$CI->security->get_csrf_token_name()] = $CI->security->get_csrf_hash();
}
}
$form = '<form action="'.$action.'"'.$attributes.">\n";
if (is_array($hidden))
{
foreach ($hidden as $name => $value)
{
$form .= '<input type="hidden" name="'.$name.'" value="'.$this->html_escape($value).'" style="display:none;" />'."\n";
}
}
return $form;
}
/**
* Form Declaration with model variable
*
* Creates the opening portion of the form.
*
* @param string the URI segments of the form destination
* @param array a key/value pair of attributes
* @param array a key/value pair hidden data
* @return string
*/
function form_model($model, $action = '', $attributes = array(), $hidden = array()){
$this->model_array=$model;
return $this->form_open_multipart($action, $attributes, $hidden);
}
// ------------------------------------------------------------------------
/**
* Form Declaration - Multipart type
*
* Creates the opening portion of the form, but with "multipart/form-data".
*
* @param string the URI segments of the form destination
* @param array a key/value pair of attributes
* @param array a key/value pair hidden data
* @return string
*/
function form_open_multipart($action = '', $attributes = array(), $hidden = array())
{
if (is_string($attributes))
{
$attributes .= ' enctype="multipart/form-data"';
}
else
{
$attributes['enctype'] = 'multipart/form-data';
}
return $this->form_open($action, $attributes, $hidden);
}
// ------------------------------------------------------------------------
/**
* Hidden Input Field
*
* Generates hidden fields. You can pass a simple key/value string or
* an associative array with multiple values.
*
* @param mixed $name Field name
* @param string $value Field value
* @param bool $recursing
* @return string
*/
function form_hidden($name, $value = '', $recursing = FALSE)
{
static $form;
if ($recursing === FALSE)
{
$form = "\n";
}
if (is_array($name))
{
foreach ($name as $key => $val)
{
/* Set model value */
$model_value=$this->set_model_value($name);
if(!empty($model_value) && empty($val)) {
$val=$model_value;
}
$this->form_hidden($key, $val, TRUE);
}
return $form;
}
if ( ! is_array($value))
{
/* Set model value */
$model_value=$this->set_model_value($name);
if(!empty($model_value) && empty($value)) {
$value=$model_value;
}
$form .= '<input type="hidden" name="'.$name.'" value="'.$this->html_escape($value)."\" />\n";
}
else
{
foreach ($value as $k => $v)
{
$k = is_int($k) ? '' : $k;
/* Set model value */
$model_value=$this->set_model_value($name.'['.$k.']');
if(!empty($model_value) && empty($v)) {
$v=$model_value;
}
$this->form_hidden($name.'['.$k.']', $v, TRUE);
}
}
return $form;
}
// ------------------------------------------------------------------------
/**
* Text Input Field
*
* @param mixed
* @param string
* @param mixed
* @return string
*/
function form_input($name, $data = '', $value = '', $extra = '')
{
$defaults = array(
'type' => 'text',
'value' => $value
);
/*set name*/
if(is_array($data)) {
unset($data['name']);
$data=array("name"=>$name)+$data;
}
else {
$data['name']=$name;
}
/* Set model value, But dont fill value for password field */
if(!isset($data['type']) || $data['type']!='password') {
$model_value=$this->set_model_value($name);
if(!empty($model_value) && empty($value)) {
$data['value']=$model_value;
}
}
return '<input '.$this->_parse_form_attributes($data, $defaults).$this->_attributes_to_string($extra)." />\n";
}
// ------------------------------------------------------------------------
/**
* Password Field
*
* Identical to the input function but adds the "password" type
*
* @param mixed
* @param string
* @param mixed
* @return string
*/
function form_password($name, $data = '', $value = '', $extra = '')
{
is_array($data) OR $data = array('name' => $data);
$data['type'] = 'password';
return $this->form_input($name, $data, $value, $extra);
}
// ------------------------------------------------------------------------
/**
* Upload Field
*
* Identical to the input function but adds the "file" type
*
* @param mixed
* @param string
* @param mixed
* @return string
*/
function form_upload($name, $data = '', $value = '', $extra = '')
{
$defaults = array('type' => 'file');
is_array($data) OR $data = array('name' => $name);
if(is_array($data)) {
/* Overwrite name in data */
unset($data['name']);
$data=array("name"=>$name)+$data;
}
return '<input '.$this->_parse_form_attributes($data, $defaults).$this->_attributes_to_string($extra)." />\n";
}
// ------------------------------------------------------------------------
/**
* Textarea field
*
* @param mixed $data
* @param string $value
* @param mixed $extra
* @return string
*/
function form_textarea($name, $data = '', $value = '', $extra = '')
{
$defaults = array();
/* Overwrite name from data */
if(is_array($data)) {
unset($data['name']);
$data=array('name'=>$name)+$data;
}
else {
$data['name']=$name;
}
if ( ! is_array($data) OR ! isset($data['value']))
{
$val = $value;
}
else
{
$val = $data['value'];
unset($data['value']); // textareas don't use the value attribute
}
/* Set model value */
$model_value=$this->set_model_value($name);
if(!empty($model_value) && empty($val)) {
$val=$model_value;
}
return '<textarea '.$this->_parse_form_attributes($data, $defaults).$this->_attributes_to_string($extra).'>'
.$this->html_escape($val)
."</textarea>\n";
}
// ------------------------------------------------------------------------
/**
* Multi-select menu
*
* @param string
* @param array
* @param mixed
* @param mixed
* @return string
*/
function form_multiselect($name, $options = array(), $selected = array(), $data='', $extra = '')
{
$extra = $this->_attributes_to_string($extra);
if (stripos($extra, 'multiple') === FALSE)
{
$extra .= ' multiple="multiple"';
}
return $this->form_dropdown($name, $data, $options, $selected, $extra,'multiselect');
}
// --------------------------------------------------------------------
/**
* Drop-down Menu
*
* @param mixed $data
* @param mixed $options
* @param mixed $selected
* @param mixed $extra
* @return string
*/
function form_dropdown($name, $options = array(), $selected = array(), $data = '', $extra = '', $multiple=false)
{
$defaults = array();
/* Set name */
if(is_array($data)) {
unset($data['name']);
$data=array('name'=>$name)+$data;
}
else {
$data['name']=$name;
}
/* Set selected */
if (empty($selected) && $this->model_array) {
$selected=$this->set_model_value($name, $multiple);
}
if (is_array($data))
{
if (isset($data['selected']))
{
$selected = $data['selected'];
unset($data['selected']); // select tags don't have a selected attribute
}
if (isset($data['options']))
{
$options = $data['options'];
unset($data['options']); // select tags don't use an options attribute
}
}
else
{
$defaults = array('name' => $data);
}
is_array($selected) OR $selected = array($selected);
is_array($options) OR $options = array($options);
// If no selected state was submitted we will attempt to set it automatically
if (empty($selected))
{
if (is_array($data))
{
if (isset($data['name'], $_POST[$data['name']]))
{
$selected = array($_POST[$data['name']]);
}
}
elseif (isset($_POST[$data]))
{
$selected = array($_POST[$data]);
}
}
$extra = $this->_attributes_to_string($extra);
$multiple = (count($selected) > 1 && stripos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
$form = '<select '.rtrim($this->_parse_form_attributes($data, $defaults)).$extra.$multiple.">\n";
foreach ($options as $key => $val)
{
$key = (string) $key;
if (is_array($val))
{
if (empty($val))
{
continue;
}
$form .= '<optgroup label="'.$key."\">\n";
foreach ($val as $optgroup_key => $optgroup_val)
{
$sel = in_array($optgroup_key, $selected) ? ' selected="selected"' : '';
$form .= '<option value="'.$this->html_escape($optgroup_key).'"'.$sel.'>'
.(string) $optgroup_val."</option>\n";
}
$form .= "</optgroup>\n";
}
else
{
$form .= '<option value="'.$this->html_escape($key).'"'
.(in_array($key, $selected) ? ' selected="selected"' : '').'>'
.(string) $val."</option>\n";
}
}
return $form."</select>\n";
}
//----------------------------------------------------------------------
function form_country_dropdown($name, $selected = array(), $data = '', $extra = '', $multiple=false) {
$country_list=array(
""=>"Select Country",
'Afghanistan'=>'Afghanistan',
'Aland Islands'=>'Aland Islands',
'Albania'=>'Albania',
'American Samoa'=>'American Samoa',
'Andorra'=>'Andorra',
'Angola'=>'Angola',
'Anguilla'=>'Anguilla',
'Antarctica'=>'Antarctica',
'Antigua and Barbuda'=>'Antigua and Barbuda',
'Argentina'=>'Argentina',
'Armenia'=>'Armenia',
'Aruba'=>'Aruba',
'Australia'=>'Australia',
'Austria'=>'Austria',
'Azerbaijan'=>'Azerbaijan',
'Bahamas'=>'Bahamas',
'Bahrain'=>'Bahrain',
'Bangladesh'=>'Bangladesh',
'Barbados'=>'Barbados',
'Belarus'=>'Belarus',
'Belgium'=>'Belgium',
'Belize'=>'Belize',
'Benin'=>'Benin',
'Bermuda'=>'Bermuda',
'Bhutan'=>'Bhutan',
'Bolivia'=>'Bolivia',
'Bosnia and Herzegovina'=>'Bosnia and Herzegovina',
'Botswana'=>'Botswana',
'Bouvet Island'=>'Bouvet Island',
'Brazil'=>'Brazil',
'British Indian Ocean Territory'=>'British Indian Ocean Territory',
'British Virgin Islands'=>'British Virgin Islands',
'Brunei'=>'Brunei',
'Bulgaria'=>'Bulgaria',
'Burkina Faso'=>'Burkina Faso',
'Burundi'=>'Burundi',
'Cambodia'=>'Cambodia',
'Cameroon'=>'Cameroon',
'Canada'=>'Canada',
'Cape Verde'=>'Cape Verde',
'Caribbean Netherlands'=>'Caribbean Netherlands',
'Cayman Islands'=>'Cayman Islands',
'Central African Republic'=>'Central African Republic',
'Chad'=>'Chad',
'Chile'=>'Chile',
'China'=>'China',
'Christmas Island'=>'Christmas Island',
'Cocos (Keeling) Islands'=>'Cocos (Keeling) Islands',
'Colombia'=>'Colombia',
'Comoros'=>'Comoros',
'Congo (Brazzaville)'=>'Congo (Brazzaville)',
'Congo (Kinshasa)'=>'Congo (Kinshasa)',
'Cook Islands'=>'Cook Islands',
'Costa Rica'=>'Costa Rica',
'Croatia'=>'Croatia',
'Cuba'=>'Cuba',
'Curaçao'=>'Curaçao',
'Cyprus'=>'Cyprus',
'Czech Republic'=>'Czech Republic',
'Denmark'=>'Denmark',
'Djibouti'=>'Djibouti',
'Dominica'=>'Dominica',
'Dominican Republic'=>'Dominican Republic',
'Ecuador'=>'Ecuador',
'Egypt'=>'Egypt',
'El Salvador'=>'El Salvador',
'Equatorial Guinea'=>'Equatorial Guinea',
'Eritrea'=>'Eritrea',
'Estonia'=>'Estonia',
'Ethiopia'=>'Ethiopia',
'Falkland Islands'=>'Falkland Islands',
'Faroe Islands'=>'Faroe Islands',
'Fiji'=>'Fiji',
'Finland'=>'Finland',
'France'=>'France',
'French Guiana'=>'French Guiana',
'French Polynesia'=>'French Polynesia',
'French Southern Territories'=>'French Southern Territories',
'Gabon'=>'Gabon',
'Gambia'=>'Gambia',
'Georgia'=>'Georgia',
'Germany'=>'Germany',
'Ghana'=>'Ghana',
'Gibraltar'=>'Gibraltar',
'Greece'=>'Greece',
'Greenland'=>'Greenland',
'Grenada'=>'Grenada',
'Guadeloupe'=>'Guadeloupe',
'Guam'=>'Guam',
'Guatemala'=>'Guatemala',
'Guernsey'=>'Guernsey',
'Guinea'=>'Guinea',
'Guinea-Bissau'=>'Guinea-Bissau',
'Guyana'=>'Guyana',
'Haiti'=>'Haiti',
'Heard Island and McDonald Islands'=>'Heard Island and McDonald Islands',
'Honduras'=>'Honduras',
'Hong Kong S.A.R., China'=>'Hong Kong S.A.R., China',
'Hungary'=>'Hungary',
'Iceland'=>'Iceland',
'India'=>'India',
'Indonesia'=>'Indonesia',
'Iran'=>'Iran',
'Iraq'=>'Iraq',
'Ireland'=>'Ireland',
'Isle of Man'=>'Isle of Man',
'Israel'=>'Israel',
'Italy'=>'Italy',
'Ivory Coast'=>'Ivory Coast',
'Jamaica'=>'Jamaica',
'Japan'=>'Japan',
'Jersey'=>'Jersey',
'Jordan'=>'Jordan',
'Kazakhstan'=>'Kazakhstan',
'Kenya'=>'Kenya',
'Kiribati'=>'Kiribati',
'Kuwait'=>'Kuwait',
'Kyrgyzstan'=>'Kyrgyzstan',
'Laos'=>'Laos',
'Latvia'=>'Latvia',
'Lebanon'=>'Lebanon',
'Lesotho'=>'Lesotho',
'Liberia'=>'Liberia',
'Libya'=>'Libya',
'Liechtenstein'=>'Liechtenstein',
'Lithuania'=>'Lithuania',
'Luxembourg'=>'Luxembourg',
'Macao S.A.R., China'=>'Macao S.A.R., China',
'Macedonia'=>'Macedonia',
'Madagascar'=>'Madagascar',
'Malawi'=>'Malawi',
'Malaysia'=>'Malaysia',
'Maldives'=>'Maldives',
'Mali'=>'Mali',
'Malta'=>'Malta',
'Marshall Islands'=>'Marshall Islands',
'Martinique'=>'Martinique',
'Mauritania'=>'Mauritania',
'Mauritius'=>'Mauritius',
'Mayotte'=>'Mayotte',
'Mexico'=>'Mexico',
'Micronesia'=>'Micronesia',
'Moldova'=>'Moldova',
'Monaco'=>'Monaco',
'Mongolia'=>'Mongolia',
'Montenegro'=>'Montenegro',
'Montserrat'=>'Montserrat',
'Morocco'=>'Morocco',
'Mozambique'=>'Mozambique',
'Myanmar'=>'Myanmar',
'Namibia'=>'Namibia',
'Nauru'=>'Nauru',
'Nepal'=>'Nepal',
'Netherlands'=>'Netherlands',
'Netherlands Antilles'=>'Netherlands Antilles',
'New Caledonia'=>'New Caledonia',
'New Zealand'=>'New Zealand',
'Nicaragua'=>'Nicaragua',
'Niger'=>'Niger',
'Nigeria'=>'Nigeria',
'Niue'=>'Niue',
'Norfolk Island'=>'Norfolk Island',
'Northern Mariana Islands'=>'Northern Mariana Islands',
'North Korea'=>'North Korea',
'Norway'=>'Norway',
'Oman'=>'Oman',
'Pakistan'=>'Pakistan',
'Palau'=>'Palau',
'Palestinian Territory'=>'Palestinian Territory',
'Panama'=>'Panama',
'Papua New Guinea'=>'Papua New Guinea',
'Paraguay'=>'Paraguay',
'Peru'=>'Peru',
'Philippines'=>'Philippines',
'Pitcairn'=>'Pitcairn',
'Poland'=>'Poland',
'Portugal'=>'Portugal',
'Puerto Rico'=>'Puerto Rico',
'Qatar'=>'Qatar',
'Reunion'=>'Reunion',
'Romania'=>'Romania',
'Russia'=>'Russia',
'Rwanda'=>'Rwanda',
'Saint Barthélemy'=>'Saint Barthélemy',
'Saint Helena'=>'Saint Helena',
'Saint Kitts and Nevis'=>'Saint Kitts and Nevis',
'Saint Lucia'=>'Saint Lucia',
'Saint Martin (French part)'=>'Saint Martin (French part)',
'Saint Pierre and Miquelon'=>'Saint Pierre and Miquelon',
'Saint Vincent and the Grenadines'=>'Saint Vincent and the Grenadines',
'Samoa'=>'Samoa',
'San Marino'=>'San Marino',
'Sao Tome and Principe'=>'Sao Tome and Principe',
'Saudi Arabia'=>'Saudi Arabia',
'Senegal'=>'Senegal',
'Serbia'=>'Serbia',
'Seychelles'=>'Seychelles',
'Sierra Leone'=>'Sierra Leone',
'Singapore'=>'Singapore',
'Sint Maarten'=>'Sint Maarten',
'Slovakia'=>'Slovakia',
'Slovenia'=>'Slovenia',
'Solomon Islands'=>'Solomon Islands',
'Somalia'=>'Somalia',
'South Africa'=>'South Africa',
'South Georgia and the South Sandwich Islands'=>'South Georgia and the South ,Sandwich ',
'South Korea'=>'South Korea',
'South Sudan'=>'South Sudan',
'Spain'=>'Spain',
'Sri Lanka'=>'Sri Lanka',
'Sudan'=>'Sudan',
'Suriname'=>'Suriname',
'Svalbard and Jan Mayen'=>'Svalbard and Jan Mayen',
'Swaziland'=>'Swaziland',
'Sweden'=>'Sweden',
'Switzerland'=>'Switzerland',
'Syria'=>'Syria',
'Taiwan'=>'Taiwan',
'Tajikistan'=>'Tajikistan',
'Tanzania'=>'Tanzania',
'Thailand'=>'Thailand',
'Timor-Leste'=>'Timor-Leste',
'Togo'=>'Togo',
'Tokelau'=>'Tokelau',
'Tonga'=>'Tonga',
'Trinidad and Tobago'=>'Trinidad and Tobago',
'Tunisia'=>'Tunisia',
'Turkey'=>'Turkey',
'Turkmenistan'=>'Turkmenistan',
'Turks and Caicos Islands'=>'Turks and Caicos Islands',
'Tuvalu'=>'Tuvalu',
'U.S. Virgin Islands'=>'U.S. Virgin Islands',
'Uganda'=>'Uganda',
'Ukraine'=>'Ukraine',
'United Arab Emirates'=>'United Arab Emirates',
'United Kingdom'=>'United Kingdom',
'United States'=>'United States',
'United States Minor Outlying Islands'=>'United States Minor Outlying Islands',
'Uruguay'=>'Uruguay',
'Uzbekistan'=>'Uzbekistan',
'Vanuatu'=>'Vanuatu',
'Vatican'=>'Vatican',
'Venezuela'=>'Venezuela',
'Vietnam'=>'Vietnam',
'Wallis and Futuna'=>'Wallis and Futuna',
'Western Sahara'=>'Western Sahara',
'Yemen'=>'Yemen',
'Zambia'=>'Zambia',
'Zimbabwe'=>'Zimbabwe',
);
return $this->form_dropdown($name, $country_list, $selected,$data, $extra, $multiple);
}
// --------------------------------------------------------------------
/*
Create combobox from database result array
accept 7 parameter.
1 - name of select box or combobox.
2 - array of values. ex. array( array('key'=>'key1', 'val'=>'value1'), array('key'=>'key2', 'val'=>'value2') )
3 - option value column name of select box or combobox in above example key will be right argumant.
4 - text which will display in select box or combobox in above example val is right argument.
5 - bydefault selected value in select box or combobox can be array also.
6 - other HTML or css attributes.
7 - 'SELECT' will be first option or not.
8 - Attach attribute to each option tag
array('Attribute name','array or string','separator') -
ex1. array('data-opt',invoice_value')
ex2. array('data-opt',array('invoice_value','invoice_numver'),',')
*/
function form_dropdown_fromdatabase($name,$array,$key,$value,$selected=false,$other=false,$defaultoption="SELECT", $datavalue=false) {
/* Set selected */
if (empty($selected) && $this->model_array) {
$selected=$this->set_model_value($name, isset($other['multiple']));
}
$other=$this->_attributes_to_string($other);
$output = "<select name=\"{$name}\" ".$other.">";
if($defaultoption) {
$output .= "<option value=\"\">SELECT</option>";
}
if(empty($array)) {
$output .= "</select>";
return $output;
}
$keys=array_column($array,$key);
$sap_char=' ';
if(is_array($value)) {
$args=array();
$args[]="combine";
foreach($value as $val) {
$args[]=array_column($array,$val);
}
/* pass separator array */
foreach ($args[1] as $key=>$value) {
$saperator[$key]=$sap_char;
}
$args[]=$saperator;
$vals=call_user_func_array('array_map',$args);
}
else {
$vals=array_column($array,$value);
}
$new_array=array_combine($keys,$vals);
$opt_attr=array();
/* for setting extra attribute with dynamic value */
if($datavalue) {
/* attribute name */
$attr=$datavalue[0];
/* attribute value column */
$attr_value=$datavalue[1];
/* create separator array */
$data_saperator=array();
/* if multiple column value need to supply */
if(is_array($attr_value)) {
/* column value separator */
$datasap_char=$datavalue[2];
$val_columns=array();
$val_columns[]="combine";
/* add each column value into val column */
foreach($attr_value as $attr_val) {
$val_columns[]=array_column($array,$attr_val);
}
/* pass separator array */
foreach ($attr_value as $key=>$blank) {
$data_saperator[$key]=$datasap_char;
}
$val_columns[]=$data_saperator;
/* combine multiple column in to string */
$opt_attr=call_user_func_array('array_map',$val_columns);
}
else {
$opt_attr=array_column($array,$attr_value);
}
}
$k=0;
foreach ($new_array as $key => $value) {
$str_attr='';
if($datavalue) {
$str_attr=' '.$datavalue[0].'="'.$opt_attr[$k].'" ';
}
if(is_array($selected)) {
if (in_array($key,$selected)) {
$output .= "<option value=\"{$key}\" ".$str_attr." selected>{$value}</option>";
}
else {
$output .= "<option value=\"{$key}\" ".$str_attr.">{$value}</option>";
}
}
else {
if ($selected !== false && $selected == $key) {
$output .= "<option value=\"{$key}\" ".$str_attr." selected>{$value}</option>";
}
else {
$output .= "<option value=\"{$key}\" ".$str_attr.">{$value}</option>";
}
}
$k++;
}
$output .= "</select>";
return $output;
}
// ------------------------------------------------------------------------
/**
* Checkbox Field
*
* @param mixed
* @param string
* @param bool
* @param mixed
* @return string
*/
function form_checkbox($name, $value = '', $data = '', $checked = FALSE, $extra = '')
{
$defaults = array('type' => 'checkbox','name'=>$name,'value' => $value);
/* Set name */
if(is_array($data)) {
unset($data['name']);
$data=array('name'=>$name)+$data;
}
else {
$data['name']=$name;
}
if (is_array($data) && array_key_exists('checked', $data))
{
$checked = $data['checked'];
if ($checked == FALSE)
{
unset($data['checked']);
}
else
{
$data['checked'] = 'checked';
}
}
if ($checked == TRUE)
{
$defaults['checked'] = 'checked';
}
else
{
unset($defaults['checked']);
}
/* Set value from model */
$checked=$this->set_model_value($name, true);
if(!empty($value)) {
if(is_array($checked)) {
if(array_search($value,$checked)!==false) {
$data['checked'] = 'checked';
}
}
elseif(is_string($checked) && $checked == $value) {
$data['checked'] = 'checked';
}
}
return '<input '.$this->_parse_form_attributes($data, $defaults).$this->_attributes_to_string($extra)." />\n";
}
// ------------------------------------------+------------------------------
/**
* Radio Button
*
* @param mixed
* @param string
* @param bool
* @param mixed
* @return string
*/
function form_radio($name, $value = '', $data = '', $checked = FALSE, $extra = '')
{
is_array($data) OR $data = array('name' => $data);
$data['type'] = 'radio';
return $this->form_checkbox($name, $value, $data, $checked, $extra);
}
// ------------------------------------------------------------------------
/**
* Submit Button
*
* @param mixed
* @param string
* @param mixed
* @return string
*/
function form_submit($data = '', $value = '', $extra = '')
{
$defaults = array(
'type' => 'submit',
'name' => is_array($data) ? '' : $data,
'value' => $value
);
return '<input '.$this->_parse_form_attributes($data, $defaults).$this->_attributes_to_string($extra)." />\n";
}
// ------------------------------------------------------------------------
/**
* Reset Button
*
* @param mixed
* @param string
* @param mixed
* @return string
*/
function form_reset($data = '', $value = '', $extra = '')
{
$defaults = array(
'type' => 'reset',
'name' => is_array($data) ? '' : $data,
'value' => $value
);
return '<input '.$this->_parse_form_attributes($data, $defaults).$this->_attributes_to_string($extra)." />\n";
}
// ------------------------------------------------------------------------
/**
* Form Button
*
* @param mixed
* @param string
* @param mixed
* @return string
*/
function form_button($data = '', $content = '', $extra = '')
{
$defaults = array(
'name' => is_array($data) ? '' : $data,
'type' => 'button'
);
if (is_array($data) && isset($data['content']))
{
$content = $data['content'];
unset($data['content']); // content is not an attribute
}
return '<button '.$this->_parse_form_attributes($data, $defaults).$this->_attributes_to_string($extra).'>'
.$content
."</button>\n";
}
// ------------------------------------------------------------------------
/**
* Form Label Tag
*
* @param string The text to appear onscreen
* @param string The id the label applies to
* @param array Additional attributes
* @return string
*/
function form_label($label_text = '', $id = '', $attributes = array())
{
$label = '<label';
if ($id !== '')
{
$label .= ' for="'.$id.'"';
}
if (is_array($attributes) && count($attributes) > 0)
{
foreach ($attributes as $key => $val)
{
$label .= ' '.$key.'="'.$val.'"';
}
}
return $label.'>'.$label_text.'</label>';
}
// ------------------------------------------------------------------------
/**
* Fieldset Tag