/
FormBuilder.php
1755 lines (1547 loc) · 50.6 KB
/
FormBuilder.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
/**
* PepisCMS
*
* Simple content management system
*
* @package PepisCMS
* @author Piotr Polak
* @copyright Copyright (c) 2007-2018, Piotr Polak
* @license See license.txt
* @link http://www.polak.ro/
*/
defined('BASEPATH') or exit('No direct script access allowed');
/**
* FormBuilder - utility for generating web forms
*
* Form builder handles form generation, validation and saving.
* It can read data from any source that implements Entitable interface.
*
* @since 0.1.4
*
*/
class FormBuilder extends ContainerAware
{
const POST_ID_FIELD_NAME = 'form_builder_id';
/**
* Textfield input
* @var int
*/
const TEXTFIELD = 0;
/**
* Checkbox
* @var int
*/
const CHECKBOX = 1;
/**
* Selectbox
* @var int
*/
const SELECTBOX = 2;
/**
* Radio button
* @var int
*/
const RADIO = 3;
/**
* Textarea input
* @var int
*/
const TEXTAREA = 4;
/**
* Image field
* @var int
*/
const IMAGE = 5;
/**
* Rich text editor
* @var int
*/
const RTF = 6;
/**
* Rich text editor
* @var int
*/
const RTF_FULL = 66;
/**
* Multiple items can be checked
* @var int
*/
const MULTIPLESELECT = 7;
/**
* Hidden field
* @var int
*/
const HIDDEN = 8;
/**
* Date field
* @var int
*/
const DATE = 9;
/**
* File field
* @var int
*/
const FILE = 10;
/**
* Password field
* @var int
*/
const PASSWORD = 11;
/**
* Multiple radios, one can be checked
* @var int
*/
const MULTIPLECHECKBOX = 12;
/**
* Textfield with autocomplete
* @var int
*/
const TEXTFIELD_AUTOCOMPLETE = 14;
/**
* Selectbox with autocomplete
*/
const SELECTBOX_AUTOCOMPLETE = 15;
/**
* Line break for floating forms, not really an input
*/
const LINE_BREAK = 16;
/**
* Timestamp input
*/
const TIMESTAMP = 17;
/**
* Colorpicker input
*/
const COLORPICKER = 18;
/**
* This callback is called after retrieving the data but before rendering the form.
* The callback function takes must take the OBJECT parameter as reference.
*
* @var int
*/
const CALLBACK_BEFORE_RENDER = 500;
/**
* This callback is called before saving the data.
* The callback function must take the ARRAY parameter as reference.
*
* @var int
*/
const CALLBACK_BEFORE_SAVE = 501;
/**
* This callback is called after saving the data.
* The callback function must take the ARRAY parameter as reference.
*
* @var int
*/
const CALLBACK_AFTER_SAVE = 502;
/**
* This callback is called when data save fails. It can be using for rollback operations.
* The callback function takes must take the ARRAY parameter as reference.
*
* @var int
*/
const CALLBACK_ON_SAVE_FAILURE = 503;
/**
* This callback is called on save.
* This kind of callback should be used when no feed object specified of when you want to overwrite the default SAVE operation.
* The callback function takes must take the ARRAY parameter as reference and MUST return true or false.
* If the function returns false, it should also set FormBuilder validation error message.
*
* @var int
*/
const CALLBACK_ON_SAVE = 504;
/**
* This callback is called on read.
* This kind of callback should be used when no feed object specified of when you want to overwrite the default READ operation.
* The callback function takes must take the OBJECT parameter as reference and to FILL it.
* The callback does not need to return anything.
*
* @var int
*/
const CALLBACK_ON_READ = 505;
/**
* Database ONE TO MANY relation constant
*
* @var int
*/
const FOREIGN_KEY_ONE_TO_MANY = 600;
/**
* Database MANY TO MANY relation constant
*
* @var int
*/
const FOREIGN_KEY_MANY_TO_MANY = 601;
/**
* Table title
* @var string
*/
private $title;
/**
* Object from which the data object will be extracted
* @var object
*/
private $feed_object;
/**
* Form rendering object
* @var object
*/
private $renderer;
/**
* List of fields
* @var array
*/
private $fields;
/**
* List of input groups, associative array
* @var array
*/
private $input_groups;
/**
* List of upload fields
* @var array
*/
private $file_upload_fields;
/**
* ID of the entity
* @var int
*/
private $id;
/**
* Unique form instance id
* @var string
*/
private $instance_code;
/**
* When set to true, the form is displayed in read-only mode
* @var bool
*/
private $read_only;
/**
* The object containing fields values
* @var bool|object
*/
private $object;
/**
* The URL displayed as back link
* @var bool|string
*/
private $back_link;
/**
* List of callbacks
* @var bool
*/
private $callbacks;
/**
* Upload real hdd path
* @var string
*/
private $default_uploads_path;
/**
* Upload display (web) path
* @var string
*/
private $default_upload_display_path;
/**
* Validation message container
* @var string
*/
private $validation_error_message;
/**
* Indicates whether the library should redirect user to the back URL on save success
* @var bool
*/
private $redirect_on_save_success;
/**
* Indicates whether the library should set simple session message on success
* @var bool
*/
private $use_simple_session_message_on_save_success;
/**
* Indicates whether the apply button is enabled
* @var bool
*/
private $is_apply_button_enabled;
/**
* Indicates whether the the default save button is enabled
* @var bool
*/
private $is_submit_button_enabled;
/**
* Form action, default itself
* @var string
*/
private $form_action;
/**
* Submit button label
* @var string
*/
private $form_submit_label;
/**
* When set to false, the old files are not removed when overwritten by new ones
* @var bool
*/
private $is_overwrite_files_on_upload;
/**
* Contains a list of upload warnings
* @var array
*/
private $upload_warnings;
/**
* Default constructor
*/
public function __construct()
{
$this->load->language('formbuilder');
$this->load->library('form_validation');
$this->load->library('SimpleSessionMessage');
$this->load->library('upload');
$this->clear();
}
/**
* Resets the FormBuilder internal data
*
* @return FormBuilder
*/
public function clear()
{
$this->callbacks = array();
$this->is_overwrite_files_on_upload = true;
$this->form_submit_label = false;
$this->form_action = '';
$this->is_submit_button_enabled = true;
$this->is_apply_button_enabled = false;
$this->use_simple_session_message_on_save_success = true;
$this->redirect_on_save_success = true;
$this->validation_error_message = false;
$this->back_link = false;
$this->object = false;
$this->read_only = false;
$this->instance_code = null;
$this->id = false;
$this->file_upload_fields = array();
$this->input_groups = array();
$this->fields = array();
$this->renderer = null;
$this->feed_object = false;
$this->title = false;
$this->default_uploads_path = $this->default_upload_display_path = $this->config->item('uploads_path');
$this->upload_warnings = array();
return $this;
}
/**
* Returns the list of possible input types
*
* @return array
*/
public static function getInputTypes()
{
$types = array();
$types[self::TEXTFIELD] = 'TEXTFIELD';
$types[self::CHECKBOX] = 'CHECKBOX';
$types[self::SELECTBOX] = 'SELECTBOX';
$types[self::RADIO] = 'RADIO';
$types[self::TEXTAREA] = 'TEXTAREA';
$types[self::IMAGE] = 'IMAGE';
$types[self::RTF] = 'RTF';
$types[self::RTF_FULL] = 'RTF_FULL';
$types[self::MULTIPLESELECT] = 'MULTIPLESELECT';
$types[self::HIDDEN] = 'HIDDEN';
$types[self::DATE] = 'DATE';
$types[self::FILE] = 'FILE';
$types[self::PASSWORD] = 'PASSWORD';
$types[self::MULTIPLECHECKBOX] = 'MULTIPLECHECKBOX';
$types[self::TEXTFIELD_AUTOCOMPLETE] = 'TEXTFIELD_AUTOCOMPLETE';
$types[self::SELECTBOX_AUTOCOMPLETE] = 'SELECTBOX_AUTOCOMPLETE';
//$types[self::LINE_BREAK] = 'LINE_BREAK'; // Not really an input
$types[self::TIMESTAMP] = 'TIMESTAMP';
return $types;
}
/**
* Returns a list of upload warnings
*
* @return array
*/
public function getUploadWarnings()
{
return $this->upload_warnings;
}
/**
* Sets the table title
*
* @param string $title
* @return FormBuilder
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Returns the table title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Sets form action
*
* @param string $action
* @return FormBuilder
*/
public function setAction($action)
{
$this->form_action = $action;
return $this;
}
/**
* Returns the form action
*
* @return string
*/
public function getAction()
{
return $this->form_action;
}
/**
* Sets submit label
*
* @param string $label
* @return FormBuilder
*/
public function setSubmitLabel($label)
{
$this->form_submit_label = $label;
return $this;
}
/**
* Returns submit label
*
* @return string
*/
public function getSubmitLabel()
{
if (!$this->form_submit_label) {
$this->form_submit_label = $this->lang->line('global_button_save_and_close');
}
return $this->form_submit_label;
}
/**
* Enable or disable default submit, default is true
*
* @param bool $is_submit_button_enabled
* @return FormBuilder
*/
public function setSubmitButtonEnabled($is_submit_button_enabled = true)
{
$this->is_submit_button_enabled = $is_submit_button_enabled;
return $this;
}
/**
* Returns true if the default submit button is enabled
*
* @return bool
*/
public function isSubmitButtonEnabled()
{
return $this->is_submit_button_enabled;
}
/**
* Enable or disable apply button, default is false
*
* @param bool $is_apply_button_enabled
* @return FormBuilder
*/
public function setApplyButtonEnabled($is_apply_button_enabled = true)
{
$this->is_apply_button_enabled = $is_apply_button_enabled;
return $this;
}
/**
* Returns true if apply button is enabled
*
* @return bool
*/
public function isApplyButtonEnabled()
{
return $this->is_apply_button_enabled;
}
/**
* Tells whether the apply action has been fired
*
* @return bool
*/
public function isApplyEventFired()
{
return ($this->input->get('apply', null) !== null
&& $this->input->get('apply') == '');
}
/**
* Returns the list of input groups
*
* @return array
*/
public function getInputGroups()
{
return $this->input_groups;
}
/**
* Returns form definition, alias to getDefinition()
*
* @return array
*/
public function getFields()
{
return $this->getDefinition();
}
/**
* Returns form definition
*
* @return array
*/
public function getDefinition()
{
return $this->fields;
}
/**
* Returns form definition
*
* @param string $field_name
* @return array
*/
public function getField($field_name)
{
return (isset($this->fields[$field_name]) ? $this->fields[$field_name] : false);
}
/**
* Returns field names
*
* @return array
*/
public function getFieldNames()
{
$out = array();
foreach ($this->fields as &$field) {
$out[] = $field['field'];
}
return $out;
}
/**
* Adds a definition of a new field
*
* @param array $field
* @param bool $label
* @param bool $type
* @param bool $default_value
* @param bool $rules
* @param bool $values
* @return FormBuilder
*/
public function addField($field, $label = false, $type = false, $default_value = false, $rules = false, $values = false)
{
// If the first element is array, then setting field by definition
if (is_array($field)) {
return $this->addFieldByDefinition($field);
}
if ($type === false) {
$type = FormBuilder::TEXTFIELD;
}
if ($rules === false) {
$rules = 'required';
}
$field_definition = array(
'field' => $field,
'label' => $label,
'input_type' => $type,
'input_default_value' => $default_value,
'values' => $values,
'validation_rules' => $rules
);
return $this->addFieldByDefinition($field_definition);
}
/**
* Returns field default configuration.
* This method should be static.
*/
public function getFieldDefaults()
{
$defaults = array();
$defaults['field'] = false; // Field name
$defaults['label'] = false; // Field label
$defaults['description'] = false; // Field description
// Display options
$defaults['show_in_form'] = true; // Display in form?
$defaults['show_in_grid'] = true; // Display in grid?
// Foreign key
$defaults['foreign_key_relationship_type'] = FormBuilder::FOREIGN_KEY_ONE_TO_MANY;
$defaults['foreign_key_table'] = false;
$defaults['foreign_key_field'] = 'id';
$defaults['foreign_key_label_field'] = 'id';
$defaults['foreign_key_accept_null'] = false;
$defaults['foreign_key_where_conditions'] = false;
$defaults['foreign_key_junction_table'] = false;
$defaults['foreign_key_junction_id_field_left'] = false;
$defaults['foreign_key_junction_id_field_right'] = false;
$defaults['foreign_key_junction_where_conditions'] = false;
//
// Input specific
//
$defaults['input_type'] = FormBuilder::TEXTFIELD; // Input type, see FormBuilder constants
$defaults['input_default_value'] = false; // Default value for field
$defaults['values'] = false; // Values for to select among them, must be an associative array
$defaults['validation_rules'] = 'required'; // Validation rules
$defaults['input_is_editable'] = true;
$defaults['input_group'] = false;
$defaults['input_css_class'] = false;
$defaults['options'] = array();
// File upload
$defaults['upload_complete_callback'] = false;
$defaults['upload_path'] = $this->default_uploads_path;
$defaults['upload_display_path'] = $this->default_upload_display_path;
$defaults['upload_allowed_types'] = false;
$defaults['upload_encrypt_name'] = false;
//
// Grid specific
//
$defaults['grid_formating_callback'] = false;
$defaults['grid_is_orderable'] = true;
$defaults['grid_css_class'] = false;
$defaults['filter_type'] = false;
$defaults['filter_values'] = false;
$defaults['filter_condition'] = 'like';
// Autocomplete
$defaults['autocomplete_source'] = '';
return $defaults;
}
/**
* Sets whether the the old files are not removed when overwritten by new ones
*
* @param bool $is_overwrite_files_on_upload
* @return FormBuilder
*/
public function setOverwriteFilesOnUpload($is_overwrite_files_on_upload = true)
{
$this->is_overwrite_files_on_upload = $is_overwrite_files_on_upload;
return $this;
}
/**
* Tells whether the the old files are not removed when overwritten by new ones
*
* @return bool
*/
public function isOverwriteFilesOnUpload()
{
return $this->is_overwrite_files_on_upload;
}
/**
* Sets fields by definition
*
* @param array $fields
* @return FormBuilder
*/
public function setDefinition($fields)
{
// Resetting values
$this->fields = array();
$this->input_groups = array();
foreach ($fields as $key => &$field) {
// Make it work with associative
if ($key && (!isset($field['field']) || $field['field'] === false)) {
$field['field'] = $key;
}
$this->addFieldByDefinition($field);
}
return $this;
}
/**
* Sets field by array definition
* @param array $field
* @return FormBuilder
*/
public function addFieldByDefinition($field)
{
if (!isset($field['field']) || !$field['field']) {
return false;
}
$defaults = $this->getFieldDefaults();
foreach ($defaults as $name => $value) {
if (is_callable($value)) {
continue;
}
if (isset($field[$name])) {
$defaults[$name] = $field[$name];
}
unset($field[$name]); // Saving memory and preventing strange errors when some keys have null value
}
if (!$defaults['show_in_form']) {
return $this;
}
// Useful for debugging, prevents from using misspelled keys
$unused_keys = array_keys($field);
if (count($unused_keys)) {
foreach ($unused_keys as $unused_key) {
trigger_error('FormBuilder definition contains unknown key: ' . $unused_key, E_USER_NOTICE);
}
}
// Input groups
if (!$defaults['input_group']) {
$defaults['input_group'] = 'default';
}
if (!isset($this->input_groups[$defaults['input_group']])) {
$this->input_groups[$defaults['input_group']] = array(
'label' => ucfirst(str_replace('_', ' ', $defaults['input_group'])),
'description' => false,
'fields' => array()
);
}
$this->input_groups[$defaults['input_group']]['fields'][] = $defaults['field'];
if ($defaults['input_type'] == FormBuilder::IMAGE || $defaults['input_type'] == FormBuilder::FILE) {
$this->file_upload_fields[$defaults['field']] = $defaults['field'];
$defaults['validation_rules'] = '';
if ($defaults['upload_allowed_types'] === false && $defaults['input_type'] == FormBuilder::IMAGE) {
$defaults['upload_allowed_types'] = 'jpg|jpeg|gif|png|bmp';
}
}
$defaults['label'] = $defaults['label'] !== false ?
$defaults['label'] : ucfirst(str_replace('_', ' ', $defaults['field']));
$this->fields[$defaults['field']] = $defaults;
return $this;
}
/**
* Sets the feed object implementing feedable interface
*
* @param EntitableInterface $feed_object
* @return FormBuilder
*/
public function setFeedObject(EntitableInterface $feed_object)
{
$this->feed_object = $feed_object;
return $this;
}
/**
* Returns feed object used by formbuilder
*
* @return EntitableInterface
*/
public function getFeedObject()
{
return $this->feed_object;
}
/**
* Sets table used by default Generic_model
*
* @param string $tableName
* @param array $acceptedPostFields
* @param bool|string $idFieldName
* @return FormBuilder
*/
public function setTable($tableName, $acceptedPostFields = array(), $idFieldName = false)
{
// Initializes a cloned Generic Model with a specified table
$feed_object = clone $this->Generic_model;
$feed_object->setTable($tableName);
// Specify id field name, otherwise use the default value from Generic Model
if ($idFieldName) {
$feed_object->setIdFieldName($idFieldName);
}
$feed_object->setAcceptedPostFields($acceptedPostFields);
$this->setFeedObject($feed_object);
return $this;
}
/**
* Adds accepted post field, useful in callbacks
*
* @param string $field
* @return FormBuilder
* @throws Exception
*/
public function addAcceptedPostField($field)
{
if ($this->feed_object instanceof Generic_model) {
$this->feed_object->addAcceptedPostField($field);
return $this;
}
throw new Exception("Feed object must be an instance of Generic_model to use this method");
}
/**
* Returns unique id of the form instance
*
* @return Object
*/
public function getInstanceCode()
{
if ($this->instance_code === null) {
// TODO Rewrite POST parameter access
$this->instance_code = isset($_POST['form_builder_instance_code']) ?
$_POST['form_builder_instance_code'] : time() . '' . rand(10000, 99999);
}
return $this->instance_code;
}
/**
* Sets ID used for representing working object using the feed object
*
* @param int $id
* @return FormBuilder
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
/**
* Returns ID used for representing working object
*
* @return int
*/
public function getId()
{
if (!$this->id) {
// TODO Rewrite POST parameter access
$this->id = isset($_POST[self::POST_ID_FIELD_NAME]) ? $_POST[self::POST_ID_FIELD_NAME] : false;
}
return $this->id;
}
/**
* The form is rendered for reading only when this set to true
*
* @param bool $read_only
* @return FormBuilder
*/
public function setReadOnly($read_only = true)
{
$this->read_only = $read_only;
return $this;
}
/**
* Tells whether the form is read only
*
* @return bool
*/
public function isReadOnly()
{
return $this->read_only;
}
/**
* Sets form renderer
*
* @param FormRenderableInterface $renderer
* @return FormBuilder
*/
public function setRenderer(FormRenderableInterface $renderer)
{
$this->renderer = $renderer;
return $this;
}
/**
* Returns form renderer
*
* @return FormRenderableInterface
*/
public function getRenderer()
{
// Initializing default renderer
if ($this->renderer == null) {
$this->renderer = new DefaultFormRenderer();
}
return $this->renderer;
}
/**
* Sets link that is used with the back button
*
* @param string $back_link
* @return FormBuilder
*/
public function setBackLink($back_link)
{
$this->back_link = $back_link;
if (!preg_match('#^https?://#i', $this->back_link)) {
$this->back_link = base_url() . $this->back_link;
}
return $this;
}
/**
* Sets link that is used with the back button
*
* @return bool|string
*/
public function getBackLink()
{
return $this->back_link;
}
/**
* Sets to use simple session message on success
*
* @param bool
*/