/
MJaxFormBase.class.php
285 lines (252 loc) · 10.3 KB
/
MJaxFormBase.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
<?php
/*
* This class will serve as the base form for the MJax Package
*/
class MJaxFormBase extends QBaseClass{
const FILEINFO_BASENAME = 'basename';
//------Static Vars------
protected static $strCurrAction = null;
public static $EncryptionKey = null;
public static $FormStateHandler = 'QFormStateHandler';
//------Instance------
protected $arrControls = array();
protected $intNextControlId = 1;
protected $strFormId = null;
protected $arrAsset = array();
protected $strCallType = null;
/////////////////////////
// Public Properties: GET
/////////////////////////
public function __get($strName) {
switch ($strName) {
case "FormId": return $this->strFormId;
case "CallType": return $this->strCallType;
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":
default:
try {
return parent::__set($strName, $mixValue);
} catch (QCallerException $objExc) {
$objExc->IncrementOffset();
throw $objExc;
}
}
}
/////////////////////////
// Helpers for ControlId Generation
/////////////////////////
public function GenerateControlId() {
$strToReturn = sprintf('c%s', $this->intNextControlId);
$this->intNextControlId++;
return $strToReturn;
}
public static function Run($strFormId, $strAlternateHtmlFile = null) {
// Ensure strFormId is a class
$objClass = new $strFormId();
$objClass->strFormId = $strFormId;
// Ensure strFormId is a subclass of QForm
if (!($objClass instanceof MJaxForm)){
throw new QCallerException('Object is not a subclass of MJaxForm (note, it can NOT be a subclass of QFormBase): ' . $strFormId);
}
//Check to see if the form has posted data
if (key_exists(MJaxFormPostData::MJaxForm__FormState, $_POST)){
//Tell the object and the application what the call type is
$strPostDataState = $_POST[MJaxFormPostData::MJaxForm__FormState];
if($strPostDataState){
// We might have a valid form state -- let's see by unserializing this object
$objClass = MJaxForm::Unserialize($strPostDataState);
}
$objClass->ParsePostData();
$objClass->strCallType = QCallType::Ajax;
QApplication::$RequestMode = QRequestMode::Ajax;
}elseif((key_exists(MJaxFormPostData::ACTION, $_POST)) && ($_POST[MJaxFormPostData::ACTION] == MJaxFormAction::CHANGE_PAGE)){
$objClass->strCallType = QCallType::Ajax;
QApplication::$RequestMode = QRequestMode::Ajax;
$objClass->Form_Create();
}else{
$objClass->strCallType = QCallType::None;
QApplication::$RequestMode = QRequestMode::Standard;
$objClass->Form_Create();
}
if(key_exists(MJaxFormPostData::ACTION, $_POST)){
self::$strCurrAction = $_POST[MJaxFormPostData::ACTION];
switch(self::$strCurrAction){
case(MJaxFormAction::CONTROL_EVENT):
$objClass->TriggerControlEvent($_POST[MJaxFormPostData::CONTROL_ID], $_POST[MJaxFormPostData::EVENT]);
break;
}
}
$objClass->Form_PreRender();
if($objClass->strCallType == QCallType::Ajax){
$objClass->RenderAjax();
}else{
$objClass->Render();
}
$objClass->Form_Exit();
}
public static function LocateTemplate($strFileName){
$arrPathInfo = pathinfo($strFileName);
if(strpos($strFileName, '.class') !== false){
$strFileName = str_replace(".class.php", ".tpl.php", $arrPathInfo[self::FILEINFO_BASENAME]);
}else{
$strFileName = str_replace(".php", ".tpl.php", $arrPathInfo[self::FILEINFO_BASENAME]);
}
$strAssetFile = $arrPathInfo['dirname'] . __REL_TPL_DIR__ . "/". $strFileName;
return $strAssetFile;
}
protected function Render(){
require(__DOCROOT__ . __PHP_ASSETS__ . '/header.inc.php');
require(self::LocateTemplate($_SERVER[MLCServer::SCRIPT_FILENAME]));
$strFormState = sprintf('<input type="hidden" name="%s" id="%s" value="%s" />', MJaxFormPostData::MJaxForm__FormState, MJaxFormPostData::MJaxForm__FormState, MJaxForm::Serialize($this));
_p($strFormState, false);
require(__DOCROOT__ . __PHP_ASSETS__ . '/footer.inc.php');
}
protected function RenderAjax(){
ob_clean();
// Output it and update render state
if (QApplication::$EncodingType)
_p(sprintf("<?xml version=\"1.0\" encoding=\"%s\"?>\r\n", QApplication::$EncodingType), false);
else
_p("<?xml version=\"1.0\"?>\r\n", false);
require(__DOCROOT__ . __PHP_ASSETS__ . '/header_ajax.inc.php');
require(self::LocateTemplate($_SERVER[MLCServer::SCRIPT_FILENAME]));
$strFormState = sprintf('<input type="hidden" name="%s" id="%s" value="%s" />', MJaxFormPostData::MJaxForm__FormState, MJaxFormPostData::MJaxForm__FormState, MJaxForm::Serialize($this));
_p($strFormState, false);
require(__DOCROOT__ . __PHP_ASSETS__ . '/footer_ajax.inc.php');
header('Content-Type: text/xml');
}
public function RenderControlJSCalls($blnPrint = true, $blnAjaxFormating = false){
//Cycle through all controls and get their JS calls for this time
$strRendered = '';
foreach($this->arrControls as $objControl){
$strRendered .= $objControl->RenderJSCalls($blnAjaxFormating);
}
if($blnAjaxFormating){
$strRendered = QString::XmlEscape(trim($strRendered));
}
if($blnPrint){
_p($strRendered, false);
}else{
return $strRendered;
}
}
public function TriggerControlEvent($strControlId, $strEvent){
if(!key_exists($strControlId, $this->arrControls)){
throw new QCallerException("Control '" . $strControlId . "' does not exist");
}
$this->arrControls[$strControlId]->TriggerEvent($strEvent);
}
public function RegisterControl($objControl){
$strControlId = $objControl->ControlId;
if(key_exists($strControlId, $this->arrControls)){
throw new QCallerException("A control with the Id '" . $strControlId . "' already exists");
}
$this->arrControls[$strControlId] = $objControl;
}
public function AddHeaderAsset($objAsset){
$this->arrAsset[] = $objAsset;
}
public function RenderHeaderAssets($blnPrint = true){
$strRender = '';
foreach($this->arrAsset as $objAsset){
$strRender .= $objAsset->__toString();
}
if($blnPrint){
_p($strRender, false);
}else{
return $strRender;
}
}
public function RenderControlRegisterJS($blnPrint = true, $blnAjaxFormating = false){
$strRendered = '';
if(!$blnAjaxFormating){
$strRendered .= "<script language='javascript'>\n\t";
}
$strRendered .= "$(document).ready(function(){\n\t";
$strRendered .= "MJax.ClearRegisteredControls();\n\t";
foreach($this->arrControls as $objControl){
$strRendered .= sprintf("MJax.RegisterControl('%s');\n\t", $objControl->ControlId);
}
$strRendered .= "});\n\t";
if(!$blnAjaxFormating){
$strRendered .= "</script>";
}
if($blnAjaxFormating){
$strRendered = QString::XmlEscape(trim($strRendered));
}
if($blnPrint){
_p($strRendered, false);
}else{
return $strRendered;
}
}
public function ParsePostData(){
foreach($this->arrControls as $objControl){
$objControl->ParsePostData();
}
}
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() {}
/**
* @param Form $objForm
* @return string the Serialized Form
*/
public static function Serialize(MJaxForm $objForm) {
// Create a Clone of the Form to Serialize
$objForm = clone($objForm);
// Cleanup Reverse Control->Form links
if($objForm->arrControls){
foreach ($objForm->arrControls 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(MJaxForm::$FormStateHandler, 'Save');
$strPostDataState = call_user_func_array($strSaveCommand, array($strSerializedForm, false));
// 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(MJaxForm::$FormStateHandler, 'Load');
$strSerializedForm = call_user_func($strLoadCommand, $strPostDataState);
if ($strSerializedForm) {
// Unserialize and Cast the Form
$objForm = unserialize($strSerializedForm);
$objForm = QType::Cast($objForm, 'MJaxForm');
// Reset the links from Control->Form
if ($objForm->arrControls){
foreach($objForm->arrControls as $objControl){
$objControl->SetForm($objForm);
}
}
// Return the Form
return $objForm;
} else
return null;
}
}
?>