|
@@ -0,0 +1,238 @@
|
|
|
|
|
+<?php
|
|
|
|
|
+
|
|
|
|
|
+Lib::loadClass('ApiDataSourceTodo');// TODO: @see Entity/Source/Mysql from feature-schema-install
|
|
|
|
|
+Lib::loadClass('Api_TransactionHelper');
|
|
|
|
|
+
|
|
|
|
|
+class Api_Table {
|
|
|
|
|
+
|
|
|
|
|
+ private $_apiUser;
|
|
|
|
|
+ private $_dataSourceName;
|
|
|
|
|
+ private $_tblName;
|
|
|
|
|
+ private $_tblSchema;
|
|
|
|
|
+
|
|
|
|
|
+ public function setUser($user) {
|
|
|
|
|
+ $this->_apiUser = $user;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function execute($request) {
|
|
|
|
|
+ if (!$this->_apiUser->isAdmin()) {
|
|
|
|
|
+ throw new HttpException("Forbidden", 403);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ IF(V::get('DBG','',$_GET)){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">request->segments (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($request->segments);echo'</pre>';}
|
|
|
|
|
+ if (empty($request->segments) || !is_array($request->segments)) return;
|
|
|
|
|
+
|
|
|
|
|
+ if (count($request->segments) < 2) {
|
|
|
|
|
+ throw new HttpException("Data source and table name not specified", 400);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $this->_dataSourceName = array_shift($request->segments);
|
|
|
|
|
+ $this->_tblName = array_shift($request->segments);
|
|
|
|
|
+
|
|
|
|
|
+ if (empty($request->segments)) {
|
|
|
|
|
+ return $this->tableSchemaAction($request);
|
|
|
|
|
+ }
|
|
|
|
|
+ else {
|
|
|
|
|
+ $action = array_shift($request->segments);
|
|
|
|
|
+ $actionName = "{$action}Action";
|
|
|
|
|
+ if (!method_exists($this, $actionName)) {
|
|
|
|
|
+ throw new HttpException("Method not exists", 404);
|
|
|
|
|
+ }
|
|
|
|
|
+ return $this->{$actionName}($request);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // return document tree - array of arrays
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private function itemsAction($request) {
|
|
|
|
|
+ IF(V::get('DBG','',$_GET)){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">request (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($request);echo'</pre>';}
|
|
|
|
|
+ $dataSource = $this->getDataSource();
|
|
|
|
|
+ if (!$dataSource) {
|
|
|
|
|
+ throw new HttpException("DataSource not exists", 404);
|
|
|
|
|
+ }
|
|
|
|
|
+ $items = $dataSource->getItems($request->args);
|
|
|
|
|
+ IF(V::get('DBG','',$_GET)){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">items (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($items);echo'</pre>';}
|
|
|
|
|
+ $itemsDocument = new stdClass();
|
|
|
|
|
+ $itemsDocument->items = array();
|
|
|
|
|
+ foreach ($items as $item) {
|
|
|
|
|
+ $itemWrap = new stdClass();
|
|
|
|
|
+ $itemWrap->item = $item;
|
|
|
|
|
+ $itemsDocument->items[] = $itemWrap;
|
|
|
|
|
+ }
|
|
|
|
|
+ IF(V::get('DBG','',$_GET)){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">itemsDocument (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($itemsDocument);echo'</pre>';}
|
|
|
|
|
+ return $itemsDocument;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private function itemAction($request) {
|
|
|
|
|
+ IF(V::get('DBG','',$_GET)){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">request (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($request);echo'</pre>';}
|
|
|
|
|
+ if (empty($request->segments)) {
|
|
|
|
|
+ throw new HttpException("Item id not set", 400);
|
|
|
|
|
+ }
|
|
|
|
|
+ $itemId = array_shift($request->segments);
|
|
|
|
|
+ $dataSource = $this->getDataSource();
|
|
|
|
|
+ if (!$dataSource) {
|
|
|
|
|
+ throw new HttpException("DataSource not exists", 404);
|
|
|
|
|
+ }
|
|
|
|
|
+ $item = $dataSource->getItem($itemId);
|
|
|
|
|
+ IF(V::get('DBG','',$_GET)){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">item (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($item);echo'</pre>';}
|
|
|
|
|
+ $itemsDocument = new stdClass();
|
|
|
|
|
+ $itemsDocument->items = array();
|
|
|
|
|
+ $itemWrap = new stdClass();
|
|
|
|
|
+ $itemWrap->item = $item;
|
|
|
|
|
+ $itemsDocument->items[] = $itemWrap;
|
|
|
|
|
+ IF(V::get('DBG','',$_GET)){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">itemsDocument (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($itemsDocument);echo'</pre>';}
|
|
|
|
|
+ return $itemsDocument;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Http PUT xml file like this:
|
|
|
|
|
+ <new_record generate-id="d70e4" system_cache__appinfo:id="default_default_objects_types___d20e419">
|
|
|
|
|
+ <ID/>
|
|
|
|
|
+ <TYPE>default_default_objects_types:MAGAZYN</TYPE>
|
|
|
|
|
+ </new_record>
|
|
|
|
|
+ */
|
|
|
|
|
+ private function newitemAction($request) {
|
|
|
|
|
+ IF(V::get('DBG','',$_GET)){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">request (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($request);echo'</pre>';}
|
|
|
|
|
+ $dataSource = $this->getDataSource();
|
|
|
|
|
+ if (!$dataSource) {
|
|
|
|
|
+ throw new HttpException("DataSource not exists", 404);
|
|
|
|
|
+ }
|
|
|
|
|
+ $requestBodyXml = file_get_contents('php://input');
|
|
|
|
|
+ IF(V::get('DBG_ERROR_LOG','',$_REQUEST)){trigger_error("request body:'{$requestBodyXml}'", E_USER_NOTICE);}
|
|
|
|
|
+ IF(V::get('DBG_ERROR_LOG','',$_REQUEST)){trigger_error("request headers:'" . json_encode(getallheaders()) . "'", E_USER_NOTICE);}
|
|
|
|
|
+ IF(V::get('DBG_ERROR_LOG','',$_REQUEST)){trigger_error("request post:'" . json_encode($_POST) . "'", E_USER_NOTICE);}
|
|
|
|
|
+ //IF(V::get('DBG_ERROR_LOG','',$_REQUEST)){trigger_error("request server:" . json_encode($_SERVER), E_USER_NOTICE);}
|
|
|
|
|
+ if (empty($requestBodyXml)) {
|
|
|
|
|
+ throw new HttpException("Empty request body", 404);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $useTransactionKey = V::get('handle', '', $_GET);
|
|
|
|
|
+ if (!empty($useTransactionKey)) {
|
|
|
|
|
+ $handlerPattern = '/^([a-zA-Z0-9\-_]+)$/';
|
|
|
|
|
+ $matches = array();
|
|
|
|
|
+ if (!preg_match($handlerPattern, $useTransactionKey, $matches)) {
|
|
|
|
|
+ throw new HttpException("Wrong handler argument", 400);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $apiTransactionHelper = new Api_TransactionHelper();
|
|
|
|
|
+ $actionId = $apiTransactionHelper->createAction($useTransactionKey, 'INSERT', $this->_tblName, $requestBodyXml);
|
|
|
|
|
+
|
|
|
|
|
+ $item = array();
|
|
|
|
|
+
|
|
|
|
|
+ $tags = array();
|
|
|
|
|
+ $parserXml = xml_parser_create();
|
|
|
|
|
+ xml_parser_set_option($parserXml, XML_OPTION_CASE_FOLDING, 0);
|
|
|
|
|
+ xml_parser_set_option($parserXml, XML_OPTION_SKIP_WHITE, 1);
|
|
|
|
|
+ if (0 == xml_parse_into_struct($parserXml, $requestBodyXml, $tags)) {
|
|
|
|
|
+ throw new Exception("Error parsing xml");
|
|
|
|
|
+ }
|
|
|
|
|
+ xml_parser_free($parserXml);
|
|
|
|
|
+ if (empty($tags)) {
|
|
|
|
|
+ throw new Exception("Empty structure from request");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $rootTagName = V::get('tag', '', $tags[0]);
|
|
|
|
|
+ if ('new_record' != $rootTagName) {
|
|
|
|
|
+ throw new Exception("Empty structure from request");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $tagsTotal = count($tags);
|
|
|
|
|
+ for ($i = 1; $i < $tagsTotal - 1; $i++) {
|
|
|
|
|
+ $vTag = $tags[$i];
|
|
|
|
|
+ if ($vTag['level'] != 2) {
|
|
|
|
|
+ throw new Exception("Structure error: only 2 level is allowed");
|
|
|
|
|
+ }
|
|
|
|
|
+ $item[$vTag['tag']] = V::get('value', null, $vTag);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (empty($item)) {
|
|
|
|
|
+ throw new Exception("Empty item from request");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $apiTransactionHelper->setActionObject($actionId, $item);
|
|
|
|
|
+
|
|
|
|
|
+ $newItemId = $dataSource->createItem($item);
|
|
|
|
|
+
|
|
|
|
|
+ $apiTransactionHelper->setActionResult($actionId, $newItemId);
|
|
|
|
|
+
|
|
|
|
|
+ IF(V::get('DBG','',$_GET)){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">item (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($item);echo'</pre>';}
|
|
|
|
|
+ $itemCreated = $dataSource->getItem($newItemId);
|
|
|
|
|
+ $itemsDocument = new stdClass();
|
|
|
|
|
+ $itemsDocument->items = array();
|
|
|
|
|
+ $itemWrap = new stdClass();
|
|
|
|
|
+ $itemWrap->item = $itemCreated;
|
|
|
|
|
+ $itemsDocument->items[] = $itemWrap;
|
|
|
|
|
+ IF(V::get('DBG','',$_GET)){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">itemsDocument (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($itemsDocument);echo'</pre>';}
|
|
|
|
|
+ return $itemsDocument;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private function newitemResultAction($request) {
|
|
|
|
|
+ IF(V::get('DBG','',$_GET)){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">request (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($request);echo'</pre>';}
|
|
|
|
|
+ $dataSource = $this->getDataSource();
|
|
|
|
|
+ if (!$dataSource) {
|
|
|
|
|
+ throw new HttpException("DataSource not exists", 404);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $useTransactionKey = array_shift($request->segments);
|
|
|
|
|
+ if (empty($useTransactionKey)) {
|
|
|
|
|
+ throw new HttpException("Handler argument not defined", 400);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!empty($useTransactionKey)) {
|
|
|
|
|
+ $handlerPattern = '/^([a-zA-Z0-9\-_]+)$/';
|
|
|
|
|
+ $matches = array();
|
|
|
|
|
+ if (!preg_match($handlerPattern, $useTransactionKey, $matches)) {
|
|
|
|
|
+ throw new HttpException("Wrong handler argument", 400);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $apiTransactionHelper = new Api_TransactionHelper();
|
|
|
|
|
+ $actionInfo = $apiTransactionHelper->getLastActionInfo($useTransactionKey);
|
|
|
|
|
+ if (empty($actionInfo)) {
|
|
|
|
|
+ throw new HttpException("Transaction not exists", 404);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $resultItemId = V::get('RESULT_ID', '', $actionInfo);
|
|
|
|
|
+ $itemCreated = $dataSource->getItem($resultItemId);
|
|
|
|
|
+ $itemsDocument = new stdClass();
|
|
|
|
|
+ $itemsDocument->TransactionResult = new stdClass();
|
|
|
|
|
+ $itemsDocument->TransactionResult->Status = 'SUCCESS';
|
|
|
|
|
+ $itemsDocument->TransactionResponse = new stdClass();
|
|
|
|
|
+ $itemsDocument->TransactionResponse->InsertResult = array();
|
|
|
|
|
+ $itemWrap = new stdClass();
|
|
|
|
|
+ $itemWrap->item = $itemCreated;
|
|
|
|
|
+ $itemsDocument->TransactionResponse->InsertResult[] = $itemWrap;
|
|
|
|
|
+ IF(V::get('DBG','',$_GET)){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">itemsDocument (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($itemsDocument);echo'</pre>';}
|
|
|
|
|
+ return $itemsDocument;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private function tableSchemaAction($request) {
|
|
|
|
|
+ IF(V::get('DBG','',$_GET)){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">request (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($request);echo'</pre>';}
|
|
|
|
|
+ $dataSource = $this->getDataSource();
|
|
|
|
|
+ if (!$dataSource) {
|
|
|
|
|
+ throw new HttpException("DataSource not exists", 404);
|
|
|
|
|
+ }
|
|
|
|
|
+ $schema = $dataSource->getSchema();
|
|
|
|
|
+ $schemaDocument = $schema;
|
|
|
|
|
+ return $schemaDocument;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private function getDataSource() {
|
|
|
|
|
+ if (!$this->_dataSource) {
|
|
|
|
|
+
|
|
|
|
|
+ // TODO: get data source from Factory
|
|
|
|
|
+ {
|
|
|
|
|
+ if ('default_db' == $this->_dataSourceName) {
|
|
|
|
|
+ $this->_dataSource = new ApiDataSourceTodo($this->_dataSourceName);
|
|
|
|
|
+ $this->_dataSource->setTable($this->_tblName);
|
|
|
|
|
+ }
|
|
|
|
|
+ else if ('931' == $this->_dataSourceName) {
|
|
|
|
|
+ $this->_dataSource = new ApiDataSourceTodo($this->_dataSourceName);
|
|
|
|
|
+ $this->_dataSource->setTable($this->_tblName);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+ return $this->_dataSource;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+}
|