-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe107HelperForm_class.php
2116 lines (1959 loc) · 79.1 KB
/
e107HelperForm_class.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
/**
* e107 Form Helper class
* <b>CVS Details:</b>
* <ul>
* <li>$Source: e:\_repository\e107_plugins/e107helpers/e107HelperForm_class.php,v $</li>
* <li>$Date: 2008/05/29 22:04:28 $</li>
* </ul>
* @author $Author: Neil $
* @version $Revision: 1.12.2.3 $
* @copyright Released under the terms and conditions of the GNU General Public License (http://gnu.org).
* @package e107HelperForm
*/
/**
* A Helper class for the e107 CMS system.
* <p>Aimed at providing methods for creating standard HTML form components.</p>
* <p>In addition, e107 specific form components (e.g. User Class list) and customized tags (e.g. colour selection pallette) are supported.</p>
* @package e107HelperForm
*/
class e107HelperForm {
/**#@+
* @access private
*/
var $_version; // Form version number
var $_tags; // Array of tag objects
var $_hiddenTags; // Array of tag objects
var $_numTags; // Number of tag objects currently held
var $_formTag; // Opening form tag
var $_formName; // Name of the form we are processing
var $_formType; // Type of the form we are processing
var $_formMode; // The mode (create, edit, etc.) the form is currently being processed for
var $_formError; // Boolean flag indicating if the form is in error post submission
var $_formErrorTop; // Show form errors at top of form (true) or against each field (false)
var $_formHTML; // The entire form HTML
var $_batchMode; // Is the form a batch form (repeating fields?) - actually the numnber of repeats of the form, default is 1
var $_tableStart; // table style (table HTML tag)
var $_errorClass; // error text CSS class
var $_helpClass; // help text CSS class
var $_labelClass; // label text CSS class
var $_messageClass; // message text CSS class
var $_promptClass; // prompt text CSS class
var $_submitStyle; // style attributes for submit buttons
var $_errorPrefix; // Marker text to display before an error message
var $_prettyPrint; // newlines character for nice output
var $_logger; // A reference to the logging object
var $_xmlTag; // Current XML tag being processed
var $_xmlName; // Name of the current XML tag (== pref or DB column name)
var $_xmlStyle; // Name of the current style attribute being processed
var $_xmlAttribute; // Name of the current attribute being processed
var $_xmlDefault; // Default field value
var $_xmlKey; // Key part of a value when set as a name/value pair (e.g. for OPTION tags)
var $_xmlEvent; // Name of the current event being processed
var $_xmlParam; // Name of parameter for the callback function
var $_xmlClass; // Class for the callback function
var $_dbFormatCallback; // User callback function for formatting string
var $_dbFormatCallbackClass; // User callback function for formatting string
var $_dbPrintPageFunc; // User function for item print page
var $_dbPrintPageURL; // User class for item print page
var $_dbWhereCallback; // User callback function for DB query
var $_dbWhereCallbackClass; // User callback function for DB query
var $_dbWhereParam; // User callback function for DB query
var $_dbData; // Database data column(s) to be read for selection list
var $_dbIndex; // Database index column used for updates/deletes
var $_dbIndexValue; // Database index value used for updates/deletes
var $_dbOrder; // Database order for selection list
var $_dbPattern; // PHP code to generate string for selection list
var $_dbStyle; // Database selection list style
var $_dbTable; // Database table to be read/updated
var $_dbJoin; // Database join SQL
var $_dbWhere; // Database where clause
/**#@-*/
/**
* Constructor : creates an instance of an e107 Helper Form and initializes private variables
*/
function __construct() {
global $pref;
$this->_logger = $GLOBALS["e107HelperLoggerFactory"]->getLogger(get_class($this));
$this->_tags = array();
$this->_hiddenTags = array();
$this->_numTags = 0;
$this->_formTag = "";
$this->_formName = "";
$this->_formType = "";
$this->_formError = false;
$this->_formErrorTop = false;
$this->_formHTML = "";
$this->_batchMode = 1;
$this->_tableStart = "<table style='width:100%' class='fborder' summary='Input form'>";
$this->_errorClass = $pref["helper_style_error_class"];
$this->_helpClass = $pref["helper_style_help_class"];
$this->_labelClass = $pref["helper_style_label_class"];
$this->_messageClass = $pref["helper_style_message_class"];
$this->_promptClass = $pref["helper_style_prompt_class"];
$this->_submitStyle = $pref["helper_style_submit_style"];
$this->_errorPrefix = "* ";
$this->_prettyPrint = "\n";
$this->_xmlTag = "";
$this->_xmlName = "";
$this->_xmlStyle = "";
$this->_xmlAttribute = "";
$this->_xmlDefault = "";
$this->_xmlKey = "";
$this->_xmlEvent = "";
$this->_xmlParam = "";
$this->_xmlClass = "";
$this->_dbFormatCallback = false;
$this->_dbFormatCallbackClass = false;
$this->_dbPrintPageURL = false;
$this->_dbWhereCallback = false;
$this->_dbWhereCallbackClass = false;
$this->_dbWhereParam = "";
$this->_dbData = "";
$this->_dbIndex = "";
$this->_dbIndexValue = "";
$this->_dbId = "";
$this->_dbOrder = "";
$this->_dbPattern = "";
$this->_dbStyle = "";
$this->_dbTable = "";
$this->_dbWhere = false;
}
// *********************************************************************************************
// Public creation methods
// *********************************************************************************************
/**
* Creates a form tag from an XML form definition.
* <p>This method must be called to eanble the form to be correctly built, validated and submitted.</p>
* <p>It is normally the first method to be called when creating a form using PHP calls.</p>
* @param string relative file path and name of the XML file defining the form to create
* @see processForm()
* @see getFormHTML()
*/
function createFormFromXML($xmlFile) {
eval(HELPER_LOGGER_TRACE_METHOD_ENTRY);
$xml_parser = xml_parser_create();
xml_set_object($xml_parser, $this);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "cdata");
if (false != $data = file_get_contents($xmlFile.".xml")) {
if (!xml_parse($xml_parser, $data, true)) {
die(sprintf("XML error (%d) : %s at line %d column %d",
xml_get_error_code($xml_parser),
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser),
xml_get_current_column_number($xml_parser)
)
);
}
} else {
die("could not open XML input file : ".$xmlFile.".xml");
}
xml_parser_free($xml_parser);
eval(HELPER_LOGGER_TRACE_METHOD_EXIT);
}
function _translateValue($att, $convert=true) {
eval(HELPER_LOGGER_TRACE_METHOD_ENTRY);
if (defined($att)) {
$att = constant($att);
}
// Look for contstant within text, surrounded by { and }
if (preg_match_all("/{\w*}/", $att, $match)) {
while (count($match[0]) > 0) {
$s = array_pop($match[0]);
$r = substr($s, 1, -1);
if ($r == "THIS_ID") {
$att = str_replace($s, $this->_getDBIndexValue(), $att);
} else if (defined($r)) {
$att = str_replace($s, constant($r), $att);
}
}
}
eval(HELPER_LOGGER_TRACE_METHOD_EXIT);
return $convert ? strtolower($att) : $att;
}
function startElement($parser, $tag, $attrs) {
eval(HELPER_LOGGER_TRACE_METHOD_ENTRY);
global $pref;
//print "<br>";
//print "$tag<br>";
//print_r($attrs);
//print "<br>";
$tag = strtolower($tag);
switch ($tag) {
case "e107helperform" : {
$this->_version = $this->_translateValue($attrs["VERSION"]);
break;
}
case "form" : {
$name = $this->_translateValue($attrs["NAME"]);
$type = $this->_translateValue($attrs["TYPE"]);
$action = $this->_translateValue($attrs["ACTION"]);
$method = $this->_translateValue($attrs["METHOD"]);
$target = $this->_translateValue($attrs["TARGET"]);
$enctype = $this->_translateValue($attrs["ENCTYPE"]);
$this->createForm($name, $type, $action, $method, $target, $enctype);
break;
}
case "batch" : {
$this->_batchMode = $this->_translateValue(varset($pref[$attrs["PREFNAME"]], $attrs["OCCURS"]));
$name = HELPER_ID_BATCH_GROUP;
$type = "batchitemstart";
$label = "";
$prompt = "";
$help = HELPER_LAN_16;
$class = $this->_translateValue($attrs["CLASS"], false);
$type = $type=="" ? $tag : $type;
$this->createTag($name, $type, $label, $prompt, $help, $class);
$this->addEvent($name, "click", "expandit('".HELPER_ID_BATCH_GROUP."_div{ITEMNO}')");
$this->addStyle($name, "cursor", "pointer");
$this->_xmlName = $attrs["NAME"];
$this->_xmlTag = $this->_translateValue($tag, false);
break;
}
case "batchcount" : {
$name = $this->_translateValue($attrs["NAME"], false);
$type = "integer";
$label = HELPER_LAN_BATCH_1;
$prompt = HELPER_LAN_BATCH_2;
$help = HELPER_LAN_BATCH_3;
$class = $this->_translateValue($attrs["CLASS"], false);
$type = $type=="" ? $tag : $type;
$this->createTag($name, $type, $label, $prompt, $help, $class);
$this->setMandatory($name, "true");
$this->setMinValue($name, "1");
$this->addAttribute($name, "size", "3");
$this->setDefault($name, "1");
$this->_xmlName = $attrs["NAME"];
$this->_xmlTag = $this->_translateValue($tag, false);
break;
}
case "errorsattop" : {
$this->setErrorsAtTop($attrs["VALUE"]);
break;
}
// Let all button tags fall through to the generic button tag handler
case "button" :
case "submit" :
{
$name = $this->_translateValue($attrs["NAME"]);
$type = $this->_translateValue($attrs["TYPE"]);
$label = $this->_translateValue($attrs["LABEL"], false);
$prompt = $this->_translateValue($attrs["PROMPT"], false);
$help = $this->_translateValue($attrs["HELP"], false);
$class = $this->_translateValue($attrs["CLASS"], false);
$type = $type=="" ? $tag : $type;
//print "..$name, $type, $label, $prompt, $help, $class<br>";
$this->createButtonTag($name, $type, $label, $prompt, $help, $class);
$this->_xmlName = $name;
$this->_xmlTag = $this->_translateValue($tag, false);
break;
}
// Let all tags fall through to the generic tag handler
case "accesstable" :
case "autotext" :
case "button" :
case "calendar" :
case "calendartime" :
case "checkbox" :
case "color" :
case "customfields" :
case "decimal" :
case "dirlist" :
case "duallist" :
case "dualtable" :
case "file" :
case "filelist" :
case "hidden" :
case "image" :
case "integer" :
case "list" :
case "numeric" :
case "radio" :
case "submit" :
case "table" :
case "tag" :
case "text" :
case "textarea" :
case "time" :
{
$name = $this->_translateValue($attrs["NAME"]);
$type = $this->_translateValue($attrs["TYPE"]);
$label = $this->_translateValue($attrs["LABEL"], false);
$prompt = $this->_translateValue($attrs["PROMPT"], false);
$help = $this->_translateValue($attrs["HELP"], false);
$class = $this->_translateValue($attrs["CLASS"], false);
$type = $type=="" ? $tag : $type;
//print "..$name, $type, $label, $prompt, $help, $class<br>";
$this->createTag($name, $type, $label, $prompt, $help, $class);
$this->_xmlName = $name;
$this->_xmlTag = $this->_translateValue($tag, false);
break;
}
// Let all static tags fall through to the generic static tag handler
case "div" :
case "notelist" :
case "p" :
case "span" :
case "statictag" :
{
$name = $this->_translateValue($attrs["NAME"]);
$type = $this->_translateValue($attrs["TYPE"]);
$text = $this->_translateValue($attrs["TEXT"], false);
$class = $this->_translateValue($attrs["CLASS"], false);
$type = $type=="" ? $tag : $type;
//print "..$name, $type, $text, $class<br>";
$this->createStaticTag($name, $type, $text, $class);
$this->_xmlName = $attrs["NAME"];
$this->_xmlTag = $this->_translateValue($tag, false);
break;
}
case "attribute" : {
$this->_xmlAttribute = $attrs["NAME"];
$this->_xmlTag = $tag;
break;
}
case "style" : {
$this->_xmlStyle = $attrs["NAME"];
$this->_xmlTag = $this->_translateValue($tag, false);
break;
}
case "default" : {
$var = $this->_translateValue($attrs["VAR"]);
if ($var != "e107pref") {
$this->_xmlDefault = $attrs["VAR"];
}
$this->_xmlTag = $this->_translateValue($tag, false);
break;
}
case "value" : {
$this->_xmlKey = $this->_translateValue($attrs["KEY"], false);
$this->_xmlTag = $this->_translateValue($tag, false);
break;
}
case "event" : {
$this->_xmlEvent = $attrs["NAME"];
$this->_xmlTag = $this->_translateValue($tag, false);
break;
}
case "callback" : {
$this->_xmlClass = $attrs["CLASS"];
$this->_xmlTag = $this->_translateValue($tag, false);
break;
}
case "listcallback" : {
$this->_xmlClass = $attrs["CLASS"];
$this->_xmlTag = $this->_translateValue($tag, false);
break;
}
case "callbackparam" : {
$this->_xmlParam = $attrs["NAME"];
$this->_xmlTag = $this->_translateValue($tag, false);
break;
}
case "dbwherecallback" : {
$this->_xmlClass = $attrs["CLASS"];
$this->_xmlParam = $attrs["PARAM"];
$this->_xmlTag = $this->_translateValue($tag, false);
break;
}
case "dbformatcallback" : {
$this->_xmlClass = $attrs["CLASS"];
$this->_xmlTag = $this->_translateValue($tag, false);
break;
}
case "dbprintpageurl" : {
$this->_xmlTag = $this->_translateValue($tag, false);
break;
}
default : {
$this->_xmlTag = $this->_translateValue($tag, false);
break;
}
}
eval(HELPER_LOGGER_TRACE_METHOD_EXIT);
}
function endElement($parser, $name) {
eval(HELPER_LOGGER_TRACE_METHOD_ENTRY);
$this->_xmlTag = "";
eval(HELPER_LOGGER_TRACE_METHOD_EXIT);
}
function cdata($parser, $cdata) {
eval(HELPER_LOGGER_TRACE_METHOD_ENTRY);
global $pref;
if (strlen($cdata)==0) {
// return;
}
$cdata = $this->_translateValue($cdata, false);
//if (strlen($this->_xmlTag) > 0) debug($this->_xmlName." ".$this->_xmlTag."..".$this->_xmlName." $cdata");
switch ($this->_xmlTag) {
case "dbformatcallback" : {
$this->setDBFormatCallback($this->_xmlClass, $cdata);
break;
}
case "dbprintpageurl" : {
$this->setDBPrintPageURL($cdata);
break;
}
case "dbwherecallback" : {
$this->setDBWhereCallback($this->_xmlClass, $this->_xmlParam, $cdata);
break;
}
case "dbtable" : {
$this->setDBTable($this->_translateValue($cdata, false));
break;
}
case "index" : {
$this->setDBIndex($this->_translateValue($cdata, false));
break;
}
case "data" : {
$this->setDBData($this->_translateValue($cdata, false));
break;
}
case "join" : {
$this->setDBJoin($this->_translateValue($cdata, false));
break;
}
case "where" : {
$this->setDBWhere($this->_translateValue($cdata, false));
break;
}
case "order" : {
$this->setDBOrder($this->_translateValue($cdata, false));
break;
}
case "pattern" : {
$this->setDBPattern($this->_translateValue($cdata, false));
break;
}
case "liststyle" : {
$this->setDBStyle($this->_translateValue($cdata, false));
break;
}
case "help" : {
$this->setHelpClass($this->_translateValue($cdata, false));
break;
}
case "prompt" : {
$this->setPromptClass($this->_translateValue($cdata, false));
break;
}
case "label" : {
$this->setLabelClass($this->_translateValue($cdata, false));
break;
}
case "default" : {
$this->setDefault($this->_xmlName, $this->_translateValue($cdata, false));
break;
}
case "callback" : {
$this->setCallback($this->_xmlName, $this->_xmlClass, $cdata);
break;
}
case "listcallback" : {
$this->setListCallback($this->_xmlName, $this->_xmlClass, $cdata);
break;
}
case "callbackparam" : {
$this->setCallbackParam($this->_xmlName, $this->_xmlParam, $this->_translateValue($cdata, false));
break;
}
case "breaks" : {
$this->setBreaks($this->_xmlName, $this->_translateValue($cdata, false));
break;
}
case "nopath" : {
$this->setNoPath($this->_xmlName, $this->_translateValue($cdata, false));
break;
}
case "dir" : {
$this->setDir($this->_xmlName, $this->_translateValue($cdata, false));
break;
}
case "subdir" : {
$this->setSubDir($this->_xmlName, $this->_translateValue($cdata, false));
break;
}
case "mandatory" : {
$this->setMandatory($this->_xmlName, $this->_translateValue($cdata, false));
break;
}
case "minlength" : {
$this->setMinLength($this->_xmlName, $this->_translateValue($cdata, false));
break;
}
case "maxlength" : {
$this->setMaxLength($this->_xmlName, $this->_translateValue($cdata, false));
break;
}
case "minvalue" : {
$this->setMinValue($this->_xmlName, $this->_translateValue($cdata, false));
break;
}
case "maxvalue" : {
$this->setMaxValue($this->_xmlName, $this->_translateValue($cdata, false));
break;
}
case "includeblank" : {
$this->setIncludeBlank($this->_xmlName, $this->_translateValue($cdata, false));
break;
}
case "attribute" : {
$this->addAttribute($this->_xmlName, $this->_xmlAttribute, $this->_translateValue($cdata, false));
break;
}
case "style" : {
$this->addStyle($this->_xmlName, $this->_xmlStyle, $this->_translateValue($cdata, false));
break;
}
case "event" : {
$this->addEvent($this->_xmlName, $this->_xmlEvent, $this->_translateValue($cdata, false));
break;
}
case "paragraph" : {
$this->addParagraphText($this->_xmlName, $this->_translateValue($cdata, false));
break;
}
case "dateformat" : {
$this->addDateFormat($this->_xmlName, $this->_translateValue($cdata, false));
break;
}
case "bbcodes" : {
$this->setBBCodes($this->_xmlName, $this->_translateValue($cdata, false));
break;
}
case "omitclass" : {
$this->omitClass($this->_xmlName, $this->_translateValue($cdata, false));
break;
}
case "value" : {
if (strlen($this->_xmlKey) > 0) {
$this->addValue($this->_xmlName, $this->_xmlKey, $this->_translateValue($cdata, false));
$this->_xmlKey = "";
} else {
$this->addValue($this->_xmlName, $this->_translateValue($cdata, false));
}
break;
}
default : {
break;
}
}
eval(HELPER_LOGGER_TRACE_METHOD_EXIT);
}
/**
* Creates a form tag.
* <p>This method must be called to eanble the form to be correctly built, validated and submitted.</p>
* <p>It is normally the first method to be called when creating a form using PHP calls.</p>
* The <var>$type</var> is an important parameter as it determines what action will be taken when The
* form has been submitted and validated. Possible values are:</p>
* <ul>
* <li>HELPER_FORM_TYPE_E107_PREF<br/>
* - The data will be added to the e107 preferences array (<var>$pref</var>)</li>
* <li>HELPER_LAN_FORM_TYPE_DB_CREATE_ROW<br/>
* - The data will be added to the specified database table as a new row</li>
* <li>HELPER_LAN_FORM_TYPE_DB_UPD_ROW<br/>
* - The specified database table will be updated with the data</li>
* <li>HELPER_LAN_FORM_TYPE_DB_DEL_ROW<br/>
* - The matching row from the database table will be deleted</li>
* </ul>
* <p>The above values are pre-defined for you and should be entered, where required, exactly as they are
* detailed above.</p>
* @param string a unique name used to identify the form
* @param string the type of the form, defaults to an e107 Preferences form
* @param string form action (URL), defaults to current URL
* @param string form method (GET/POST), defaults to POST
* @param string form target (optional)
* @param string form encoding type (optional)
* @param string JavaScript for the onsubmit event (optional)
* @param string CSS class(es) (optional)
*/
function createForm($name, $type=HELPER_FORM_TYPE_E107_PREF, $action="", $method="post", $target="", $enctype="", $onsubmit="", $cssclass="") {
eval(HELPER_LOGGER_TRACE_METHOD_ENTRY);
$this->_formName = $name;
$this->_formType = $type;
$action = $action ? "action='$action'" : "action='".e_SELF."?".e_QUERY."'";
$method = $method ? " method='$method'" : " method='post'";
$target = $target ? " target='$target'" : "";
$enctype = $enctype ? " enctype='$enctype'" : "";
$onsubmit = $onsubmit ? " onsubmit='$onsubmit'" : "";
$cssclass = $cssclass ? " class='$cssclass'" : "";
$this->_formTag = "<form id='$name' $action$method$target$enctype$onsubmit$cssclass>";
$this->_formTag .= "<div><input type='hidden' name='".$this->_formName."' value='true'/></div>";
eval(HELPER_LOGGER_TRACE_METHOD_EXIT);
}
/**
* Creates a tag.
* Creates an instance of a specific tag.
* @param $name a unique name so that the tag can be accessed by the calling application
* also used for the name of the tag on the form
* @param $tagType the type of the tag to be created, e.g. text, textarea, dropdown, accesstable, etc.
* @param $label a label for the tag (optional)
* @param $prompt prompt text for the tag (optional)
* @param $help help text for the tag (optional)
* @param $class the CSS class to be used for this tag (optional)
* @return true
*/
function createTag($name, $tagType, $label="", $prompt="", $help="", $class="") {
eval(HELPER_LOGGER_TRACE_METHOD_ENTRY);
// Create the tag object
$tempTag = new e107HelperTagObj($tagType, $this->_formName);
$tempTag->addAttribute("name", $name);
$tempTag->addAttribute("id", $name);
// Set optional parameter values
if (strlen($label) > 0) {
$tempTag->addLabel($label);
}
if (strlen($prompt) > 0) {
$tempTag->addPrompt($prompt);
}
if (strlen($help) > 0) {
$tempTag->addHelp($help);
}
if (strlen($class) > 0) {
$tempTag->addCSSClass($class);
}
$tempTag->setBatchTag($this->_batchMode != 1);
$tempArray = array($name => $tempTag);
if ($tagType == "hidden") {
$this->_hiddenTags = array_merge($this->_hiddenTags, $tempArray);
} else {
$this->_tags = array_merge($this->_tags, $tempArray);
}
$this->numTags = count($this->_tags);
eval(HELPER_LOGGER_TRACE_METHOD_EXIT);
return true;
}
/**
* Creates a button tag.
* Creates an instance of a specific button tag.
* @param $name a unique name so that the tag can be accessed by the calling application
* also used for the name of the tag on the form
* @param $tagType the type of the tag to be created, e.g. button, submit
* @param $label a label for the tag (optional)
* @param $prompt prompt text for the tag (optional)
* @param $help help text for the tag (optional)
* @param $class the CSS class to be used for this tag (optional)
* @return true if the tag was created OK
*/
function createButtonTag($name, $tagType, $label="", $prompt="", $help="", $class="") {
eval(HELPER_LOGGER_TRACE_METHOD_ENTRY);
// Create the tag object
$tempTag = new e107HelperButtonTagObj($tagType, $this->_formName);
$tempTag->addAttribute("name", $name);
$tempTag->addAttribute("id", $name);
// Set optional parameter values
if (strlen($label) > 0) {
$tempTag->addLabel($label);
}
if (strlen($prompt) > 0) {
$tempTag->addPrompt($prompt);
}
if (strlen($help) > 0) {
$tempTag->addHelp($help);
}
if (strlen($class) > 0) {
$tempTag->addCSSClass($class);
}
$tempArray = array($name => $tempTag);
if ($tagType == "hidden") {
$this->_hiddenTags = array_merge($this->_hiddenTags, $tempArray);
} else {
$this->_tags = array_merge($this->_tags, $tempArray);
}
$this->numTags = count($this->_tags);
eval(HELPER_LOGGER_TRACE_METHOD_EXIT);
return true;
}
/**
* Creates a static tag
* A static tag is one which does not return data to the server
* @param $name a unique name so that the tag can be accessed by the calling application
* also used for the id of the tag on the page
* @param $tagType the type of the tag to be created, e.g. H1, SPAN, etc.
* @param $text text to be displayed (optional)
* @param $class the CSS class to be used for this tag (optional)
* @return true if the tag was created OK
*/
function createStaticTag($name, $tagType, $text="", $class="") {
eval(HELPER_LOGGER_TRACE_METHOD_ENTRY);
$tempTag = new e107HelperStaticTagObj($name, $tagType);
$tempTag->addAttribute("id", $name);
// Set optional parameter values
if (strlen($text) > 0) {
$tempTag->addText($text);
}
if (strlen($class) > 0) {
$tempTag->addCSSClass($class);
}
$tempTag->setBatchTag($this->_batchMode != 1);
$tempArray = array($name => $tempTag);
$this->_tags = array_merge($this->_tags, $tempArray);
$this->numTags = count($this->_tags);
eval(HELPER_LOGGER_TRACE_METHOD_EXIT);
return true;
}
// *********************************************************************************************
// Public processing methods
// *********************************************************************************************
/**
* The main method to call to process a form.
* <p>If this is the first call to the form then the HTML will be generated. If the form has been submitted
* then field validation will take place, if there are errors they will be included in the generated HTML.
* If no errors are found then the form data will be processed according to the type of form that is being
* processed (e.g. preferences will be set, database record will be created, updated or deleted).</p>
* <p>To include the generated HTML in your web page you must call the getFormHTML() method.</p>
* @see getFormHTML()
* @param bool indicates if prompt text (text below a label) should be shown or not
* @param bool indicates if help text (text below a field) should be shown or not
* @param bool should form fields be blanked after update
* @return array an associative array containing:
* - formOK => a bool that indicates whether or not the form was successfully processed (true)
* or not (false))
* - id => the record ID if a DB insert/update/delete was performed
* - action => the action that was performed (e.g. update, create, delete)
*/
function processForm($showPrompt=false, $showHelp=false, $blankAfterUpdate=true) {
eval(HELPER_LOGGER_TRACE_METHOD_ENTRY);
//debug($_POST);
//debug($_FILES);
//debug($this->getFormMode());
$result = array();
// Has the form been submitted
if ($this->getFormMode() != HELPER_FORM_MODE_DISPLAY) {
// Form requires some sort of pre-processing
switch ($this->getFormMode()) {
case HELPER_FORM_MODE_PREF_SAVE : {
global $pref;
if ($this->validateForm()) {
// Form is a set of valid e107 preference values
$keys = array_keys($this->_tags);
foreach ($keys as $key) {
// If it's a tag that can submit data, set it to the e107 preferences array
if (is_a($this->_tags[$key], "e107HelperTagObj")) {
if (ini_get("magic_quotes_gpc")) {
$pref[$key] = stripslashes($this->getCurrentValue($key, 0));
} else {
$pref[$key] = $this->getCurrentValue($key, 0);
}
}
}
save_prefs();
$result = array("message"=>HELPER_LAN_03);
}
break;
}
case HELPER_FORM_MODE_DB_VALIDATE : {
if ($this->validateForm()) {
$result = array("formOK"=>true);
} else {
$result = array("formOK"=>false);
}
break;
}
case HELPER_FORM_MODE_DB_CREATE : {
if ($this->validateForm()) {
$result = $this->_insertRowToDB();
}
break;
}
case HELPER_FORM_MODE_DB_UPDATE : {
if ($this->validateForm()) {
$result = $this->_updateRowInDB();
if ($result["id"] != 0 && $blankAfterUpdate) {
$this->_setTagValuesFromDefault();
}
} else {
// Change form mode back to 'edit' mode for re-display of the form
$this->setFormMode(HELPER_FORM_MODE_DB_EDIT);
}
break;
}
case HELPER_FORM_MODE_DB_EDIT : {
$result = $this->_setTagValuesFromDB($this->_getDBIndexValue());
break;
}
case HELPER_FORM_MODE_DB_DELETE : {
$result = $this->_deleteRowFromDB();
break;
}
case HELPER_FORM_MODE_DB_PRINT : {
if ($this->_getDBPrintPageURL()) {
//headerx("location:".e_BASE."print.php?plugin:".str_replace("{ID}", $this->_getDBIndexValue(), $this->_getDBPrintPageURL()));
$url = e_BASE."print.php?plugin:".str_replace("{ID}", $this->_getDBIndexValue(), $this->_getDBPrintPageURL());
e107::redirect($url);
}
break;
}
default : {
break;
}
}
} else {
if ($this->_formType == HELPER_FORM_TYPE_E107_PREF) {
$result = $this->_setTagValuesFromPrefs();
}
}
$this->generateHTML($showPrompt, $showHelp, $result["message"], varset($result["sql"], false));
eval(HELPER_LOGGER_TRACE_METHOD_EXIT);
if ($this->_logger->isTrace()) {
$this->_logger->trace(HELPER_LOGGER_METHOD_RETURN, $this->isFormOK());
}
return array(
HELPER_RESPONSE_FORM_OK=>$this->isFormOK(),
HELPER_RESPONSE_ACTION=>$result["dbaction"],
HELPER_RESPONSE_ID=>$result["id"]
);
}
/**
* Validates the tags on the form.
* <p>Calls the validation method for each tag.<p>
* @return bool true if there are no errors on the form, otherwise false
*/
function validateForm() {
eval(HELPER_LOGGER_TRACE_METHOD_ENTRY);
//debug($_POST);
$upload = array();
// Process each tag
$keys = array_keys($this->_tags);
// For each batch input group
for ($loop=0; $loop<$this->_batchMode; $loop++) {
// If this batch group has been submitted
if ($loop==0 || varset($_POST[HELPER_ID_BATCH_GROUP][$loop], false)) {
// Process each item in the group
foreach ($this->_tags as $key=>$tag) {
// Only process common fields once in 1st iteration
if ($loop == 0 || $tag->isBatchTag()) {
// If it's a tag that can submit data, validate it
if (is_a($tag, "e107HelperTagObj")) {
$tag->setIX($loop);
$this->_formError = !$tag->validate($this->getFormMode()) || $this->_formError;
}
if ($tag->_tagType == "file") {
$upload[$loop] = $tag;
}
}
}
}
}
// Check for file uploads if no form errors
if (!$this->_formError && count($upload) > 0) {
foreach ($upload as $ix=>$tag) {
$tag->setIX($ix);
$this->_formError = !$tag->processTag_file($this->getFormMode());
}
}
eval(HELPER_LOGGER_TRACE_METHOD_EXIT);
return !$this->_formError;
}
// *********************************************************************************************
// Public HTML generation methods
// *********************************************************************************************
/**
* Get the generated HTML for the form being processed
* @return string the generated HTML for the form being processed
*/
function getFormHTML() {
eval(HELPER_LOGGER_TRACE_METHOD_ENTRY);
eval(HELPER_LOGGER_TRACE_METHOD_EXIT);
return $this->_formHTML;
}
/**
* Generate the complete HTML for the form, including any error messages
* @param bool flag to indicate if prompt text should be shown (<val>true</val>) or not (default is <val>false</val>)
* @param bool flag to indicate if help text should be shown (<val>true</val>) or not (default is <val>false</val>)
* @param mixed message text to be displayed at the top of the page, defaults to <val>false</val>
* to indicate no text to display
*/
function generateHTML($showPrompt=false, $showHelp=false, $message=false, $sql=false) {
eval(HELPER_LOGGER_TRACE_METHOD_ENTRY);
// If we're in edit mode, turn of any batch processing
if ($this->_formMode == HELPER_FORM_MODE_DB_EDIT) {
$this->_batchMode = 1;
}
$text = "";
// Check for message text
if ($message) {
$text .= "<div class='".$this->_messageClass."' style='cursor:pointer;' onclick='expandit(\"helper_sql\");'>$message</div>";
}
if ($sql) {
$text .= "<div class='".$this->_messageClass."' id='helper_sql' style='display:none;'>$sql</div>";
}
if ($this->_formType == HELPER_FORM_TYPE_DB_ROW) {
$text .= $this->generateItemSelection();
}
// Generate the form/table header tags
$text .= $this->_prettyPrint;
$text .= $this->_formTag.$this->_prettyPrint;
// Check for errors if to be displayed at the top of the form
if ($this->isFormInError() && $this->_showErrorsAtTop()) {
$errtext .= $this->getErrorText();
// Display the errors
$text .= "<div>";
$text .= $this->_tableStart.$this->_prettyPrint;
$text .= "<tr><td class='".$this->_labelClass." ".$this->_errorClass."' colspan='2'>";
$text .= HELPER_LAN_ERR_VAL_01."<br/>$errtext";
$text .= "</td></tr>";
$text .= "</table></div>".$this->_prettyPrint;
}
// Generate table row for each tag
$keys = array_keys($this->_tags);
// Process all non-batch group items
$text .= "<div>";
$text .= $this->_tableStart.$this->_prettyPrint;
foreach ($this->_tags as $key=>$tag) {
if (($this->_batchMode == 1 || (!$tag->isBatchTag())) && $key != HELPER_ID_BATCH_GROUP) {
if (is_a($tag, "e107HelperTagObj")) {
if (strlen($tag->getLabel()) > 0) {
$text .= $this->getLabelHTML($tag, $showPrompt).$this->_prettyPrint;
$text .= $this->getTagHTML($tag, $showHelp, 0).$this->_prettyPrint;
} else {
$text .= $this->getTagHTML($tag, false, $loop, true).$this->_prettyPrint;
}
} else if (is_a($tag, "e107HelperStaticTagObj")) {
$text .= $this->getStaticTagHTML($key, false).$this->_prettyPrint;
}
} else {
//debug($tag);
}
}
$text .= "</table></div>".$this->_prettyPrint;
// Now process all batch tags for however many groups are required
if ($this->_batchMode > 1) {
for ($loop=0; $loop<$this->_batchMode; $loop++) {
$text .= "<div>";
$text .= $this->_tableStart.$this->_prettyPrint;
foreach ($this->_tags as $key=>$tag) {
if ($tag->isBatchTag() || $key == HELPER_ID_BATCH_GROUP) {
$tag->setIX($loop);
if ($loop > 0 && $key==HELPER_ID_BATCH_GROUP) {
$text .= $this->getTagHTML($tag, $false, $loop, true).$this->_prettyPrint;
$text .= "</table></div>".$this->_prettyPrint;
$style = ($tag->getValue(false) == $tag->getCurrentValue(false)) ? " style='display:none;'" : "";
$text .= "<div id='".HELPER_ID_BATCH_GROUP."_div$loop'$style>";
$text .= $this->_tableStart.$this->_prettyPrint;
} else {
if (is_a($tag, "e107HelperTagObj")) {
if (strlen($tag->getLabel()) > 0) {
$text .= $this->getLabelHTML($tag, $showPrompt).$this->_prettyPrint;
$text .= $this->getTagHTML($tag, $showHelp, $loop).$this->_prettyPrint;
} else {
$text .= $this->getTagHTML($tag, false, $loop, true).$this->_prettyPrint;
}
} else if (is_a($tag, "e107HelperStaticTagObj")) {