/
QFormBase.class.php
1343 lines (1121 loc) · 50.4 KB
/
QFormBase.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
abstract class QFormBase extends QBaseClass {
///////////////////////////
// Protected Member Variables
///////////////////////////
protected $strFormId;
protected $intFormStatus;
protected $objControlArray;
protected $objPersistentControlArray = array();
protected $objGroupingArray;
protected $blnRenderedBodyTag = false;
protected $blnRenderedCheckableControlArray;
protected $strCallType;
protected $objDefaultWaitIcon = null;
protected $strFormAttributeArray = array();
protected $strIncludedJavaScriptFileArray = array();
protected $strIgnoreJavaScriptFileArray = array();
protected $strIncludedStyleSheetFileArray = array();
protected $strIgnoreStyleSheetFileArray = array();
private $pxyUrlHashProxy = null;
private $intUrlHashPollingInterval = null;
private $strUrlHashMethod = null;
private $objUrlHashParentObject = null;
protected $strUrlHash;
private $pxyPollingProxy = null;
private $intPollingInterval = null;
private $strPollingMethod = null;
private $objPollingParentObject = null;
protected $strPreviousRequestMode = false;
protected $strHtmlIncludeFilePath;
protected $strCssClass;
///////////////////////////
// Form Status Constants
///////////////////////////
const FormStatusUnrendered = 1;
const FormStatusRenderBegun = 2;
const FormStatusRenderEnded = 3;
///////////////////////////
// Form Preferences
///////////////////////////
public static $EncryptionKey = null;
public static $FormStateHandler = 'QFormStateHandler';
/////////////////////////
// Public Properties: GET
/////////////////////////
public function __get($strName) {
switch ($strName) {
case "FormId": return $this->strFormId;
case "CallType": return $this->strCallType;
case "DefaultWaitIcon": return $this->objDefaultWaitIcon;
case "FormStatus": return $this->intFormStatus;
case "HtmlIncludeFilePath": return $this->strHtmlIncludeFilePath;
case "CssClass": return $this->strCssClass;
case "UrlHash": return $this->strUrlHash;
default:
try {
return parent::__get($strName);
} catch (QCallerException $objExc) {
$objExc->IncrementOffset();
throw $objExc;
}
}
}
/////////////////////////
// Public Properties: SET
/////////////////////////
public function __set($strName, $mixValue) {
switch ($strName) {
case "HtmlIncludeFilePath":
// Passed-in value is null -- use the "default" path name of file".tpl.php"
if (!$mixValue)
$strPath = realpath(substr(QApplication::$ScriptFilename, 0, strrpos(QApplication::$ScriptFilename, '.php')) . '.tpl.php');
// Use passed-in value
else
$strPath = realpath($mixValue);
// Verify File Exists, and if not, throw exception
if (is_file($strPath)) {
$this->strHtmlIncludeFilePath = $strPath;
return $strPath;
} else
throw new QCallerException('Accompanying HTML Include File does not exist: "' . $mixValue . '"');
break;
case "CssClass":
try {
return ($this->strCssClass = QType::Cast($mixValue, QType::String));
} catch (QCallerException $objExc) {
$objExc->IncrementOffset();
throw $objExc;
}
default:
try {
return parent::__set($strName, $mixValue);
} catch (QCallerException $objExc) {
$objExc->IncrementOffset();
throw $objExc;
}
}
}
/////////////////////////
// Helpers for ControlId Generation
/////////////////////////
public function GenerateControlId() {
// $strToReturn = sprintf('control%s', $this->intNextControlId);
$strToReturn = sprintf('c%s', $this->intNextControlId);
$this->intNextControlId++;
return $strToReturn;
}
protected $intNextControlId = 1;
/////////////////////////
// Event Handlers
/////////////////////////
protected function Form_Run() {}
protected function Form_Load() {}
protected function Form_Create() {}
protected function Form_PreRender() {}
protected function Form_Validate() {return true;}
protected function Form_Exit() {}
public function PrepForVarExport() {
if ($this->objControlArray) foreach ($this->objControlArray as $objControl)
$objControl->PrepForVarExport();
}
public function IsCheckableControlRendered($strControlId) {
return array_key_exists($strControlId, $this->blnRenderedCheckableControlArray);
}
public static function Run($strFormId, $strAlternateHtmlFile = null) {
global $_FORM;
// Ensure strFormId is a class
$objClass = new $strFormId();
// Ensure strFormId is a subclass of QForm
if (!($objClass instanceof QForm))
throw new QCallerException('Object is not a subclass of QForm (note, it can NOT be a subclass of QFormBase): ' . $strFormId);
// See if we can get a Form Class out of PostData
$objClass = null;
if (array_key_exists('Qform__FormId', $_POST) && ($_POST['Qform__FormId'] == $strFormId) && array_key_exists('Qform__FormState', $_POST)) {
$strPostDataState = $_POST['Qform__FormState'];
if ($strPostDataState)
// We might have a valid form state -- let's see by unserializing this object
$objClass = QForm::Unserialize($strPostDataState);
// If there is no QForm Class, then we have an Invalid Form State
if (!$objClass) throw new QInvalidFormStateException($strFormId);
}
if ($objClass) {
// Globalize
$_FORM = $objClass;
$objClass->strCallType = $_POST['Qform__FormCallType'];
$objClass->intFormStatus = QFormBase::FormStatusUnrendered;
if ($objClass->strCallType == QCallType::Ajax)
QApplication::$RequestMode = QRequestMode::Ajax;
// Iterate through all the control modifications
$strModificationArray = explode("\n", trim($_POST['Qform__FormUpdates']));
if ($strModificationArray) foreach ($strModificationArray as $strModification) {
$strModification = trim($strModification);
if ($strModification) {
$intPosition = strpos($strModification, ' ');
$strControlId = substr($strModification, 0, $intPosition);
$strModification = substr($strModification, $intPosition + 1);
$intPosition = strpos($strModification, ' ');
if ($intPosition !== false) {
$strProperty = substr($strModification, 0, $intPosition);
$strValue = substr($strModification, $intPosition + 1);
} else {
$strProperty = $strModification;
$strValue = null;
}
switch ($strProperty) {
case 'Parent':
if ($strValue) {
if ($strValue == $objClass->FormId) {
$objClass->objControlArray[$strControlId]->SetParentControl(null);
} else {
$objClass->objControlArray[$strControlId]->SetParentControl($objClass->objControlArray[$strValue]);
}
} else {
// Remove all parents
$objClass->objControlArray[$strControlId]->SetParentControl(null);
$objClass->objControlArray[$strControlId]->SetForm(null);
$objClass->objControlArray[$strControlId] = null;
unset($objClass->objControlArray[$strControlId]);
}
break;
default:
if (array_key_exists($strControlId, $objClass->objControlArray))
$objClass->objControlArray[$strControlId]->__set($strProperty, $strValue);
break;
}
}
}
// Clear the RenderedCheckableControlArray
$objClass->blnRenderedCheckableControlArray = array();
$strCheckableControlList = trim($_POST['Qform__FormCheckableControls']);
$strCheckableControlArray = explode(' ', $strCheckableControlList);
foreach ($strCheckableControlArray as $strCheckableControl) {
$objClass->blnRenderedCheckableControlArray[trim($strCheckableControl)] = true;
}
// Iterate through all the controls
foreach ($objClass->objControlArray as $objControl) {
// If they were rendered last time and are visible (and if ServerAction, enabled), then Parse its post data
if (($objControl->Visible) &&
(($objClass->strCallType == QCallType::Ajax) || ($objControl->Enabled)) &&
($objControl->RenderMethod)) {
// Call each control's ParsePostData()
$objControl->ParsePostData();
}
// Reset the modified/rendered flags and the validation
// in ALL controls
$objControl->ResetFlags();
}
// Trigger Run Event (if applicable)
$objClass->Form_Run();
// Trigger Load Event (if applicable)
$objClass->Form_Load();
// Trigger a triggered control's Server- or Ajax- action (e.g. PHP method) here (if applicable)
$objClass->TriggerActions();
} else {
// We have no form state -- Create Brand New One
$objClass = new $strFormId();
// Globalize
$_FORM = $objClass;
// Setup HTML Include File Path, based on passed-in strAlternateHtmlFile (if any)
try {
$objClass->HtmlIncludeFilePath = $strAlternateHtmlFile;
} catch (QCallerException $objExc) {
$objExc->IncrementOffset();
throw $objExc;
}
// By default, this form is being created NOT via a PostBack
// So there is no CallType
$objClass->strCallType = QCallType::None;
$objClass->strFormId = $strFormId;
$objClass->intFormStatus = QFormBase::FormStatusUnrendered;
$objClass->objControlArray = array();
$objClass->objGroupingArray = array();
// Trigger Run Event (if applicable)
$objClass->Form_Run();
// Trigger Create Event (if applicable)
$objClass->Form_Create();
}
// Trigger PreRender Event (if applicable)
$objClass->Form_PreRender();
// Render the Page
switch ($objClass->strCallType) {
case QCallType::Ajax:
// Must use AJAX-based renderer
$objClass->RenderAjax();
break;
case QCallType::Server:
case QCallType::None:
case '':
// Server/Postback or New Page
// Make sure all controls are marked as not being on the page yet
foreach ($objClass->objControlArray as $objControl)
$objControl->ResetOnPageStatus();
// Use Standard Rendering
$objClass->Render();
break;
default:
throw new Exception('Unknown Form CallType: ' . $objClass->strCallType);
}
// Ensure that RenderEnd() was called during the Render process
switch ($objClass->intFormStatus) {
case QFormBase::FormStatusUnrendered:
throw new QCallerException('$this->RenderBegin() is never called in the HTML Include file');
case QFormBase::FormStatusRenderBegun:
throw new QCallerException('$this->RenderEnd() is never called in the HTML Include file');
case QFormBase::FormStatusRenderEnded:
break;
default:
throw new QCallerException('FormStatus is in an unknown status');
}
// Tigger Exit Event (if applicable)
$objClass->Form_Exit();
}
public function CallDataBinder($strMethodName, QPaginatedControl $objPaginatedControl, $objParentControl = null) {
try {
if ($objParentControl)
$objParentControl->$strMethodName($objPaginatedControl);
else
$this->$strMethodName($objPaginatedControl);
} catch (QCallerException $objExc) {
throw new QDataBindException($objExc);
}
}
protected function RenderAjaxHelper($objControl) {
if ($objControl)
$strToReturn = $objControl->RenderAjax(false);
if ($strToReturn)
$strToReturn .= "\r\n";
foreach ($objControl->GetChildControls() as $objChildControl) {
$strToReturn .= $this->RenderAjaxHelper($objChildControl);
$objChildControl->MarkAsRendered();
}
return $strToReturn;
}
protected function RenderAjax() {
// Update the Status
$this->intFormStatus = QFormBase::FormStatusRenderBegun;
// Create the Control collection
$strToReturn = '<controls>';
// Include each control (if applicable) that has been changed/modified
foreach ($this->GetAllControls() as $objControl)
if (!$objControl->ParentControl)
// $strToReturn .= $objControl->RenderAjax(false) . "\r\n";
$strToReturn .= $this->RenderAjaxHelper($objControl);
// First, go through all controls and gather up any JS or CSS to run or Form Attributes to modify
$strJavaScriptToAddArray = array();
$strStyleSheetToAddArray = array();
$strFormAttributeToModifyArray = array();
foreach ($this->GetAllControls() as $objControl) {
// Include any JavaScripts? The control would have a
// comma-delimited list of javascript files to include (if applicable)
if ($strScriptArray = $this->ProcessJavaScriptList($objControl->JavaScripts))
$strJavaScriptToAddArray = array_merge($strJavaScriptToAddArray, $strScriptArray);
// Include any StyleSheets? The control would have a
// comma-delimited list of stylesheet files to include (if applicable)
if ($strScriptArray = $this->ProcessStyleSheetList($objControl->StyleSheets))
$strStyleSheetToAddArray = array_merge($strStyleSheetToAddArray, $strScriptArray);
// Form Attributes?
if ($objControl->FormAttributes) {
foreach ($objControl->FormAttributes as $strKey=>$strValue) {
if (!array_key_exists($strKey, $this->strFormAttributeArray)) {
$this->strFormAttributeArray[$strKey] = $strValue;
$strFormAttributeToModifyArray[$strKey] = $strValue;
} else if ($this->strFormAttributeArray[$strKey] != $strValue) {
$this->strFormAttributeArray[$strKey] = $strValue;
$strFormAttributeToModifyArray[$strKey] = $strValue;
}
}
}
}
// Render the JS Commands to Execute
$strCommands = '';
// First, get all controls that need to run regC
$strControlIdToRegister = array();
foreach ($this->GetAllControls() as $objControl)
if ($objControl->Rendered)
array_push($strControlIdToRegister, '"' . $objControl->ControlId . '"');
if (count($strControlIdToRegister))
$strCommands .= sprintf('qc.regCA(new Array(%s)); ', implode(',', $strControlIdToRegister));
// Next, go through all controls and groupings for their GetEndScripts
foreach ($this->GetAllControls() as $objControl) {
if ($objControl->Rendered) {
$strJavaScript = $objControl->GetEndScript();
if (trim($strJavaScript))
$strCommands .= trim($strJavaScript);
}
}
foreach ($this->objGroupingArray as $objGrouping) {
$strRender = $objGrouping->Render();
if (trim($strRender))
$strCommands .= trim($strRender);
}
// Next, look to the Application object for any commands to run
$strCommands .= QApplication::RenderJavaScript(false);
// Finally, bring in "high priority commands"
// First, alter any <Form> settings that need to be altered
foreach ($strFormAttributeToModifyArray as $strKey=>$strValue)
$strCommands = sprintf('document.getElementById("%s").%s = "%s"; ', $this->strFormId, $strKey, $strValue) . $strCommands;
// Next, add any new CSS files that haven't yet been included to the end of the High Priority commands string
foreach ($strStyleSheetToAddArray as $strScript)
$strCommands = 'qc.loadStyleSheetFile("' . $strScript . '", "all"); ' . $strCommands;
// Next, add any new JS files that haven't yet been included to the BEGINNING of the High Priority commands string
// (already rendered HP commands up to this point will be placed into the callback)
foreach ($strJavaScriptToAddArray as $strScript) {
if ($strCommands)
$strCommands = 'qc.loadJavaScriptFile("' . $strScript . '", function() {' . $strCommands . '}); ';
else
$strCommands = 'qc.loadJavaScriptFile("' . $strScript . '", null); ';
}
// Set Up the Command Node
if (trim($strCommands))
$strCommands = '<command>' . QString::XmlEscape(trim($strCommands)) . '</command>';
// Persist Controls (if applicable)
foreach ($this->objPersistentControlArray as $objControl)
$objControl->Persist();
// Add in the form state
$strFormState = QForm::Serialize($this);
$strToReturn .= sprintf('<control id="Qform__FormState">%s</control>', $strFormState);
// close Control collection, Open the Command collection
$strToReturn .= '</controls><commands>';
$strToReturn .= $strCommands;
// close Command collection
$strToReturn .= '</commands>';
$strContents = trim(ob_get_contents());
if (strtolower(substr($strContents, 0, 5)) == 'debug') {
} else {
ob_clean();
// Response is in XML Format
header('Content-Type: text/xml');
// Output it and update render state
if (QApplication::$EncodingType)
printf("<?xml version=\"1.0\" encoding=\"%s\"?><response>%s</response>\r\n", QApplication::$EncodingType, $strToReturn);
else
printf("<?xml version=\"1.0\"?><response>%s</response>\r\n", $strToReturn);
}
// Update Render State
$this->intFormStatus = QFormBase::FormStatusRenderEnded;
}
/**
* @param Form $objForm
* @return string the Serialized Form
*/
public static function Serialize(QForm $objForm) {
// Get and then Update PreviousRequestMode
$strPreviousRequestMode = $objForm->strPreviousRequestMode;
$objForm->strPreviousRequestMode = QApplication::$RequestMode;
// Figure Out if we need to store state for back-button purposes
$blnBackButtonFlag = true;
if ($strPreviousRequestMode == QRequestMode::Ajax)
$blnBackButtonFlag = false;
// Create a Clone of the Form to Serialize
$objForm = clone($objForm);
// Cleanup Reverse Control->Form links
if ($objForm->objControlArray) foreach ($objForm->objControlArray as $objControl)
$objControl->SetForm(null);
// Use PHP "serialize" to serialize the form
$strSerializedForm = serialize($objForm);
// Setup and Call the FormStateHandler to retrieve the PostDataState to return
$strSaveCommand = array(QForm::$FormStateHandler, 'Save');
$strPostDataState = call_user_func_array($strSaveCommand, array($strSerializedForm, $blnBackButtonFlag));
// Return the PostDataState
return $strPostDataState;
}
/**
* @param string $strSerializedForm
* @return Form the Form object
*/
public static function Unserialize($strPostDataState) {
// Setup and Call the FormStateHandler to retrieve the Serialized Form
$strLoadCommand = array(QForm::$FormStateHandler, 'Load');
$strSerializedForm = call_user_func($strLoadCommand, $strPostDataState);
if ($strSerializedForm) {
// Unserialize and Cast the Form
$objForm = unserialize($strSerializedForm);
$objForm = QType::Cast($objForm, 'QForm');
// Reset the links from Control->Form
if ($objForm->objControlArray) foreach ($objForm->objControlArray as $objControl)
$objControl->SetForm($objForm);
// Return the Form
return $objForm;
} else
return null;
}
public function AddControl(QControl $objControl) {
$strControlId = $objControl->ControlId;
if (array_key_exists($strControlId, $this->objControlArray))
throw new QCallerException(sprintf('A control already exists in the form with the ID: %s', $strControlId));
if (array_key_exists($strControlId, $this->objGroupingArray))
throw new QCallerException(sprintf('A Grouping already exists in the form with the ID: %s', $strControlId));
$this->objControlArray[$strControlId] = $objControl;
}
public function GetControl($strControlId) {
if (array_key_exists($strControlId, $this->objControlArray))
return $this->objControlArray[$strControlId];
else
return null;
}
public function RemoveControl($strControlId) {
if (array_key_exists($strControlId, $this->objControlArray)) {
// Get the Control in Question
$objControl = $this->objControlArray[$strControlId];
// Remove all Child Controls as well
$objControl->RemoveChildControls(true);
// Remove this control from the parent
if ($objControl->ParentControl)
$objControl->ParentControl->RemoveChildControl($strControlId, false);
// Remove this control
unset($this->objControlArray[$strControlId]);
if (array_key_exists($strControlId, $this->objPersistentControlArray)) {
unset($this->objPersistentControlArray[$strControlId]);
$_SESSION[$this->strFormId . '_' . $strControlId] = null;
}
// Remove this control from any groups
foreach ($this->objGroupingArray as $strKey => $objGrouping)
$this->objGroupingArray[$strKey]->RemoveControl($strControlId);
}
}
public function GetAllControls() {
return $this->objControlArray;
}
public function PersistControl($objControl) {
$this->objPersistentControlArray[$objControl->ControlId] = $objControl;
}
public function AddGrouping(QControlGrouping $objGrouping) {
$strGroupingId = $objGrouping->GroupingId;
if (array_key_exists($strGroupingId, $this->objGroupingArray))
throw new QCallerException(sprintf('A Grouping already exists in the form with the ID: %s', $strGroupingId));
if (array_key_exists($strGroupingId, $this->objControlArray))
throw new QCallerException(sprintf('A Control already exists in the form with the ID: %s', $strGroupingId));
$this->objGroupingArray[$strGroupingId] = $objGrouping;
}
public function GetGrouping($strGroupingId) {
if (array_key_exists($strGroupingId, $this->objGroupingArray))
return $this->objGroupingArray[$strGroupingId];
else
return null;
}
public function RemoveGrouping($strGroupingId) {
if (array_key_exists($strGroupingId, $this->objGroupingArray)) {
// Remove this Grouping
unset($this->objGroupingArray[$strGroupingId]);
}
}
public function GetAllGroupings() {
return $this->objGroupingArray;
}
public function GetChildControls($objParentObject) {
$objControlArrayToReturn = array();
if ($objParentObject instanceof QForm) {
// They want all the ChildControls for this Form
// Basically, return all objControlArray QControls where the Qcontrol's parent is NULL
foreach ($this->objControlArray as $objChildControl) {
if (!($objChildControl->ParentControl))
array_push($objControlArrayToReturn, $objChildControl);
}
return $objControlArrayToReturn;
} else if ($objParentObject instanceof QControl) {
return $objParentObject->GetChildControls();
// THey want all the ChildControls for a specific Control
// Basically, return all objControlArray QControls where the Qcontrol's parent is the passed in parentobject
/* $strControlId = $objParentObject->ControlId;
foreach ($this->objControlArray as $objChildControl) {
$objParentControl = $objChildControl->ParentControl;
if (($objParentControl) && ($objParentControl->ControlId == $strControlId)) {
array_push($objControlArrayToReturn, $objChildControl);
}
}*/
} else
throw new CallerException('ParentObject must be either a QForm or QControl object');
}
public function EvaluateTemplate($strTemplate) {
global $_ITEM;
global $_CONTROL;
global $_FORM;
$_FORM = $this;
if ($strTemplate) {
QApplication::$ProcessOutput = false;
// Store the Output Buffer locally
$strAlreadyRendered = ob_get_contents();
ob_clean();
// Evaluate the new template
ob_start('__QForm_EvaluateTemplate_ObHandler');
require($strTemplate);
$strTemplateEvaluated = ob_get_contents();
ob_end_clean();
// Restore the output buffer and return evaluated template
print($strAlreadyRendered);
QApplication::$ProcessOutput = true;
return $strTemplateEvaluated;
} else
return null;
}
public function TriggerMethod($strId, $strMethodName) {
$strParameter = $_POST['Qform__FormParameter'];
$intPosition = strpos($strMethodName, ':');
if ($intPosition !== false) {
$strControlName = substr($strMethodName, 0, $intPosition);
$strMethodName = substr($strMethodName, $intPosition + 1);
$objControl = $this->objControlArray[$strControlName];
$objControl->$strMethodName($this->strFormId, $strId, $strParameter);
} else
$this->$strMethodName($this->strFormId, $strId, $strParameter);
}
protected function ValidateControlAndChildren(QControl $objControl) {
// Initially Assume Validation is True
$blnToReturn = true;
if ($objControl->Visible && $objControl->Enabled && $objControl->Display) {
// Recursive call on Child Controls
foreach ($objControl->GetChildControls() as $objChildControl)
// Only Enabled and Visible and Rendered controls should be validated
if (($objChildControl->Visible) && ($objChildControl->Enabled) && ($objChildControl->RenderMethod) && ($objChildControl->OnPage))
if (!$this->ValidateControlAndChildren($objChildControl))
$blnToReturn = false;
// Check the Control Itself
$objControl->ValidationReset();
if (!$objControl->Validate()) {
$objControl->MarkAsModified();
$blnToReturn = false;
}
}
return $blnToReturn;
}
protected function TriggerActions($strControlIdOverride = null) {
if (array_key_exists('Qform__FormControl', $_POST)) {
if ($strControlIdOverride)
$strId = $strControlIdOverride;
else
$strId = $_POST['Qform__FormControl'];
$strEvent = $_POST['Qform__FormEvent'];
if ($strId != '') {
// Does this Control which performed the action exist?
if (array_key_exists($strId, $this->objControlArray)) {
// Get the ActionControl as well as the Actions to Perform
$objActionControl = $this->objControlArray[$strId];
switch ($this->strCallType) {
case QCallType::Ajax:
$objActions = $objActionControl->GetAllActions($strEvent, 'QAjaxAction');
break;
case QCallType::Server:
$objActions = $objActionControl->GetAllActions($strEvent, 'QServerAction');
break;
default:
throw new Exception('Unknown Form CallType: ' . $this->strCallType);
}
// Validation Check
$blnValid = true;
$objControlsToValidate = array();
$mixCausesValidation = null;
// Figure out what the CausesValidation directive is
// Set $mixCausesValidation to the default one (e.g. the one defined on the control)
$mixCausesValidation = $objActionControl->CausesValidation;
// Next, go through the linked ajax/server actions to see if a causesvalidation override is set on any of them
if ($objActions) foreach ($objActions as $objAction) {
if (!is_null($objAction->CausesValidationOverride))
$mixCausesValidation = $objAction->CausesValidationOverride;
}
// Now, Do Something with mixCauseValidation...
// Starting Point is a QControl
if ($mixCausesValidation instanceof QControl) {
if (!$this->ValidateControlAndChildren($mixCausesValidation))
$blnValid = false;
// Starting Point is an Array of QControls
} else if (is_array($mixCausesValidation)) {
foreach (((array) $mixCausesValidation) as $objControlToValidate)
if (!$this->ValidateControlAndChildren($objControlToValidate))
$blnValid = false;
// Validate All the Controls on the Form
} else if ($mixCausesValidation === QCausesValidation::AllControls) {
foreach ($this->GetChildControls($this) as $objControl)
// Only Enabled and Visible and Rendered controls that are children of this form should be validated
if (($objControl->Visible) && ($objControl->Enabled) && ($objControl->RenderMethod) && ($objControl->OnPage))
if (!$this->ValidateControlAndChildren($objControl))
$blnValid = false;
// CausesValidation specifed by QCausesValidation directive
} else if ($mixCausesValidation == QCausesValidation::SiblingsAndChildren) {
// Get only the Siblings of the ActionControl's ParentControl
// If not ParentControl, tyhen the parent is the form itself
if (!($objParentObject = $objActionControl->ParentControl))
$objParentObject = $this;
// Get all the children of ParentObject
foreach ($this->GetChildControls($objParentObject) as $objControl)
// Only Enabled and Visible and Rendered controls that are children of ParentObject should be validated
if (($objControl->Visible) && ($objControl->Enabled) && ($objControl->RenderMethod) && ($objControl->OnPage))
if (!$this->ValidateControlAndChildren($objControl))
$blnValid = false;
// CausesValidation specifed by QCausesValidation directive
} else if ($mixCausesValidation == QCausesValidation::SiblingsOnly) {
// Get only the Siblings of the ActionControl's ParentControl
// If not ParentControl, tyhen the parent is the form itself
if (!($objParentObject = $objActionControl->ParentControl))
$objParentObject = $this;
// Get all the children of ParentObject
foreach ($this->GetChildControls($objParentObject) as $objControl)
// Only Enabled and Visible and Rendered controls that are children of ParentObject should be validated
if (($objControl->Visible) && ($objControl->Enabled) && ($objControl->RenderMethod) && ($objControl->OnPage)) {
$objControl->ValidationReset();
if (!$objControl->Validate()) {
$objControl->MarkAsModified();
$blnValid = false;
}
}
// No Validation Requested
} else {}
// Run Form-Specific Validation (if any)
if ($mixCausesValidation)
if (!$this->Form_Validate())
$blnValid = false;
// Go ahead and run the ServerActions or AjaxActions if Validation Passed and if there are Server/Ajax-Actions defined
if ($blnValid) {
if ($objActions) foreach ($objActions as $objAction) {
if ($strMethodName = $objAction->MethodName) {
$this->TriggerMethod($strId, $strMethodName);
}
}
}
} else
// Nope -- Throw an exception
throw new Exception(sprintf('Control passed by Qform__FormControl does not exist: %s', $strId));
}/* else {
// TODO: Code to automatically execute any PrimaryButton's onclick action, if applicable
// Difficult b/c of all the Qcodo hidden parameters that need to be set to get the action to work properly
// Javascript interaction of PrimaryButton works fine in Firefox... currently doens't work in IE 6.
}*/
}
}
protected function Render() {
require($this->HtmlIncludeFilePath);
}
protected function RenderChildren($blnDisplayOutput = true) {
$strToReturn = "";
foreach ($this->GetChildControls($this) as $objControl)
if (!$objControl->Rendered)
$strToReturn .= $objControl->Render($blnDisplayOutput);
if ($blnDisplayOutput) {
print($strToReturn);
return null;
} else
return $strToReturn;
}
// This exists to prevent inadverant "New"
protected function __construct() {}
protected function RenderBegin($blnDisplayOutput = true) {
// Ensure that RenderBegin() has not yet been called
switch ($this->intFormStatus) {
case QFormBase::FormStatusUnrendered:
break;
case QFormBase::FormStatusRenderBegun:
case QFormBase::FormStatusRenderEnded:
throw new QCallerException('$this->RenderBegin() has already been called');
break;
default:
throw new QCallerException('FormStatus is in an unknown status');
}
// Update FormStatus and Clear Included JS/CSS list
$this->intFormStatus = QFormBase::FormStatusRenderBegun;
$this->strIncludedJavaScriptFileArray = array();
$this->strIncludedStyleSheetFileArray = array();
// Figure out initial list of JavaScriptIncludes
$strJavaScriptArray = $this->ProcessJavaScriptList('_core/qcodo.js, _core/logger.js, _core/event.js, _core/post.js, _core/control.js');
if (!$strJavaScriptArray)
$strJavaScriptArray = array();
// Figure out initial list of StyleSheet includes
$strStyleSheetArray = array();
// Iterate through the form's ControlArray to Define FormAttributes and additional JavaScriptIncludes
foreach ($this->GetAllControls() as $objControl) {
// Include any JavaScripts? The control would have a
// comma-delimited list of javascript files to include (if applicable)
if ($strScriptArray = $this->ProcessJavaScriptList($objControl->JavaScripts))
$strJavaScriptArray = array_merge($strJavaScriptArray, $strScriptArray);
// Include any StyleSheets? The control would have a
// comma-delimited list of stylesheet files to include (if applicable)
if ($strScriptArray = $this->ProcessStyleSheetList($objControl->StyleSheets))
$strStyleSheetArray = array_merge($strStyleSheetArray, $strScriptArray);
// Form Attributes?
if ($objControl->FormAttributes) {
$this->strFormAttributeArray = array_merge($this->strFormAttributeArray, $objControl->FormAttributes);
}
}
// Create $strFormAttributes
$strFormAttributes = '';
foreach ($this->strFormAttributeArray as $strKey=>$strValue) {
$strFormAttributes .= sprintf(' %s="%s"', $strKey, $strValue);
}
QApplicationBase::$ProcessOutput = false;
$strOutputtedText = strtolower(trim(ob_get_contents()));
if (strpos($strOutputtedText, '<body') === false) {
$strToReturn = '<body>';
$this->blnRenderedBodyTag = true;
} else
$strToReturn = '';
QApplicationBase::$ProcessOutput = true;
if ($this->strCssClass)
$strFormAttributes .= ' class="' . $this->strCssClass . '"';
// Setup Rendered HTML
$strToReturn .= sprintf('<form method="post" id="%s" action="%s"%s>', $this->strFormId, QApplication::HtmlEntities(QApplication::$RequestUri), $strFormAttributes);
$strToReturn .= "\r\n";
// Include javascripts that need to be included
foreach ($strJavaScriptArray as $strScript) {
$strToReturn .= sprintf('<script type="text/javascript" src="%s/%s"></script>', __VIRTUAL_DIRECTORY__ . __JS_ASSETS__, $strScript);
$strToReturn .= "\r\n";
}
// Include styles that need to be included
foreach ($strStyleSheetArray as $strScript) {
$strToReturn .= sprintf('<style type="text/css" media="all">@import "%s/%s";</style>', __VIRTUAL_DIRECTORY__ . __CSS_ASSETS__, $strScript);
$strToReturn .= "\r\n";
}
// Perhaps a strFormModifiers as an array to
// allow controls to update other parts of the form, like enctype, onsubmit, etc.
// Return or Display
if ($blnDisplayOutput) {
print($strToReturn);
return null;
} else
return $strToReturn;
}
/**
* Primarily used by RenderBegin and by RenderAjax
* Given a comma-delimited list of javascript files, this will return an array of file that NEED to still
* be included because (1) it hasn't yet been included and (2) it hasn't been specified to be "ignored".
*
* This WILL update the internal $strIncludedJavaScriptFileArray array.
*
* @param string $strJavaScriptFileList
* @return string[] array of script files to include or NULL if none
*/
protected function ProcessJavaScriptList($strJavaScriptFileList) {
$strArrayToReturn = array();
// Is there a comma-delimited list of javascript files to include?
if ($strJavaScriptFileList = trim($strJavaScriptFileList)) {
$strScriptArray = explode(',', $strJavaScriptFileList);
// Iterate through the list of JavaScriptFiles to Include...
foreach ($strScriptArray as $strScript) {
if ($strScript = trim($strScript)) {
$blnIncludeFlag = true;
// Do NOT include if it has already been included
if (array_key_exists($strScript, $this->strIncludedJavaScriptFileArray))
$blnIncludeFlag = false;
// Do NOT include if it is _core and we are ignoring ALL _core AND this script is in _core
if ((array_search('_core', $this->strIgnoreJavaScriptFileArray) !== false) &&
(substr($strScript, 0, 5) == '_core'))
$blnIncludeFlag = false;
// Do NOT include if this script is in the IgnoreJavaScriptFileArray
if (array_search($strScript, $this->strIgnoreJavaScriptFileArray) !== false)
$blnIncludeFlag = false;
// Process if we are including
if ($blnIncludeFlag) {
$strArrayToReturn[$strScript] = $strScript;
$this->strIncludedJavaScriptFileArray[$strScript] = true;
}
}
}
}
if (count($strArrayToReturn))
return $strArrayToReturn;
return null;
}
/**
* Primarily used by RenderBegin and by RenderAjax
* Given a comma-delimited list of stylesheet files, this will return an array of file that NEED to still
* be included because (1) it hasn't yet been included and (2) it hasn't been specified to be "ignored".
*
* This WILL update the internal $strIncludedStyleSheetFileArray array.
*
* @param string $strStyleSheetFileList
* @return string[] array of stylesheet files to include or NULL if none
*/
protected function ProcessStyleSheetList($strStyleSheetFileList) {
$strArrayToReturn = array();
// Is there a comma-delimited list of StyleSheet files to include?