Upgrade to ExtJS 3.0.0 - Released 07/06/2009
[extjs.git] / examples / direct / php / router.php
diff --git a/examples/direct/php/router.php b/examples/direct/php/router.php
new file mode 100644 (file)
index 0000000..4f35bb1
--- /dev/null
@@ -0,0 +1,101 @@
+<?php\r
+require('config.php');\r
+\r
+class BogusAction {\r
+       public $action;\r
+       public $method;\r
+       public $data;\r
+       public $tid;\r
+}\r
+\r
+$isForm = false;\r
+$isUpload = false;\r
+if(isset($HTTP_RAW_POST_DATA)){\r
+       header('Content-Type: text/javascript');\r
+       $data = json_decode($HTTP_RAW_POST_DATA);\r
+}else if(isset($_POST['extAction'])){ // form post\r
+       $isForm = true;\r
+       $isUpload = $_POST['extUpload'] == 'true';\r
+       $data = new BogusAction();\r
+       $data->action = $_POST['extAction'];\r
+       $data->method = $_POST['extMethod'];\r
+    $data->tid = isset($_POST['extTID']) ? $_POST['extTID'] : null; // not set for upload\r
+       $data->data = array($_POST, $_FILES);\r
+}else{\r
+       die('Invalid request.');\r
+}\r
+\r
+function doRpc($cdata){\r
+    global $API;\r
+       try {\r
+               if(!isset($API[$cdata->action])){\r
+                       throw new Exception('Call to undefined action: ' . $cdata->action);\r
+               }\r
+\r
+               $action = $cdata->action;\r
+               $a = $API[$action];\r
+\r
+               doAroundCalls($a['before'], $cdata);\r
+\r
+               $method = $cdata->method;\r
+               $mdef = $a['methods'][$method];\r
+               if(!$mdef){\r
+                       throw new Exception("Call to undefined method: $method on action $action");\r
+               }\r
+               doAroundCalls($mdef['before'], $cdata);\r
+\r
+               $r = array(\r
+                       'type'=>'rpc',\r
+                       'tid'=>$cdata->tid,\r
+                       'action'=>$action,\r
+                       'method'=>$method\r
+               );\r
+\r
+               require_once("classes/$action.php");\r
+               $o = new $action();\r
+\r
+               $params = isset($cdata->data) && is_array($cdata->data) ? $cdata->data : array();\r
+\r
+               $r['result'] = call_user_func_array(array($o, $method), $params);\r
+\r
+               doAroundCalls($mdef['after'], $cdata, $r);\r
+               doAroundCalls($a['after'], $cdata, $r);\r
+       }\r
+       catch(Exception $e){\r
+               $r['type'] = 'exception';\r
+               $r['message'] = $e->getMessage();\r
+               $r['where'] = $e->getTraceAsString();\r
+       }\r
+       return $r;\r
+}\r
+\r
+\r
+function doAroundCalls(&$fns, &$cdata, &$returnData=null){\r
+       if(!$fns){\r
+               return;\r
+       }\r
+       if(is_array($fns)){\r
+               foreach($fns as $f){\r
+                       $f($cdata, $returnData);\r
+               }\r
+       }else{\r
+               $fns($cdata, $returnData);\r
+       }\r
+}\r
+\r
+$response = null;\r
+if(is_array($data)){\r
+       $response = array();\r
+       foreach($data as $d){\r
+               $response[] = doRpc($d);\r
+       }\r
+}else{\r
+       $response = doRpc($data);\r
+}\r
+if($isForm && $isUpload){\r
+       echo '<html><body><textarea>';\r
+       echo json_encode($response);\r
+       echo '</textarea></body></html>';\r
+}else{\r
+       echo json_encode($response);\r
+}\r