Table.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. Lib::loadClass('ApiDataSourceTodo');// TODO: @see Entity/Source/Mysql from feature-schema-install
  3. Lib::loadClass('Api_TransactionHelper');
  4. Lib::loadClass('ApiRouteBase');
  5. class Api_Table extends ApiRouteBase {
  6. public $_apiUser;
  7. public $_dataSourceName;
  8. public $_tblName;
  9. public $_tblSchema;
  10. public function execute($request) {
  11. if (!$this->_apiUser->isAdmin()) {
  12. throw new HttpException("Forbidden", 403);
  13. }
  14. 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>';}
  15. if (empty($request->segments) || !is_array($request->segments)) return;
  16. if (count($request->segments) < 2) {
  17. throw new HttpException("Data source and table name not specified", 400);
  18. }
  19. $this->_dataSourceName = array_shift($request->segments);
  20. $this->_tblName = array_shift($request->segments);
  21. if (empty($request->segments)) {
  22. return $this->tableSchemaAction($request);
  23. }
  24. else {
  25. $action = array_shift($request->segments);
  26. $actionName = "{$action}Action";
  27. if (!method_exists($this, $actionName)) {
  28. throw new HttpException("Method not exists", 404);
  29. }
  30. return $this->{$actionName}($request);
  31. }
  32. // return document tree - array of arrays
  33. }
  34. private function itemsAction($request) {
  35. 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>';}
  36. $dataSource = $this->getDataSource();
  37. if (!$dataSource) {
  38. throw new HttpException("DataSource not exists", 404);
  39. }
  40. $items = $dataSource->getItems($request->args);
  41. 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>';}
  42. $itemsDocument = new stdClass();
  43. $itemsDocument->items = array();
  44. foreach ($items as $item) {
  45. $itemWrap = new stdClass();
  46. $itemWrap->item = $item;
  47. $itemsDocument->items[] = $itemWrap;
  48. }
  49. 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>';}
  50. return $itemsDocument;
  51. }
  52. private function itemAction($request) {
  53. 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>';}
  54. if (empty($request->segments)) {
  55. throw new HttpException("Item id not set", 400);
  56. }
  57. $itemId = array_shift($request->segments);
  58. $dataSource = $this->getDataSource();
  59. if (!$dataSource) {
  60. throw new HttpException("DataSource not exists", 404);
  61. }
  62. $item = $dataSource->getItem($itemId);
  63. 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>';}
  64. $itemsDocument = new stdClass();
  65. $itemsDocument->items = array();
  66. $itemWrap = new stdClass();
  67. $itemWrap->item = $item;
  68. $itemsDocument->items[] = $itemWrap;
  69. 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>';}
  70. return $itemsDocument;
  71. }
  72. /**
  73. * Http PUT xml file like this:
  74. <new_record generate-id="d70e4" system_cache__appinfo:id="default_default_objects_types___d20e419">
  75. <ID/>
  76. <TYPE>default_default_objects_types:MAGAZYN</TYPE>
  77. </new_record>
  78. */
  79. private function newitemAction($request) {
  80. 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>';}
  81. $dataSource = $this->getDataSource();
  82. if (!$dataSource) {
  83. throw new HttpException("DataSource not exists", 404);
  84. }
  85. $requestBodyXml = Request::getRequestBody();
  86. IF(V::get('DBG_ERROR_LOG','',$_REQUEST)){trigger_error("request body:'{$requestBodyXml}'", E_USER_NOTICE);}
  87. IF(V::get('DBG_ERROR_LOG','',$_REQUEST)){trigger_error("request headers:'" . json_encode(getallheaders()) . "'", E_USER_NOTICE);}
  88. IF(V::get('DBG_ERROR_LOG','',$_REQUEST)){trigger_error("request post:'" . json_encode($_POST) . "'", E_USER_NOTICE);}
  89. //IF(V::get('DBG_ERROR_LOG','',$_REQUEST)){trigger_error("request server:" . json_encode($_SERVER), E_USER_NOTICE);}
  90. if (empty($requestBodyXml)) {
  91. throw new HttpException("Empty request body", 404);
  92. }
  93. $useTransactionKey = V::get('handle', '', $_GET);
  94. if (!empty($useTransactionKey)) {
  95. $handlerPattern = '/^([a-zA-Z0-9\-_]+)$/';
  96. $matches = array();
  97. if (!preg_match($handlerPattern, $useTransactionKey, $matches)) {
  98. throw new HttpException("Wrong handler argument", 400);
  99. }
  100. }
  101. $apiTransactionHelper = new Api_TransactionHelper();
  102. $actionId = $apiTransactionHelper->createAction($useTransactionKey, 'INSERT', $this->_tblName, $requestBodyXml);
  103. $item = array();
  104. $tags = array();
  105. $parserXml = xml_parser_create();
  106. xml_parser_set_option($parserXml, XML_OPTION_CASE_FOLDING, 0);
  107. xml_parser_set_option($parserXml, XML_OPTION_SKIP_WHITE, 1);
  108. if (0 == xml_parse_into_struct($parserXml, $requestBodyXml, $tags)) {
  109. throw new Exception("Error parsing xml");
  110. }
  111. xml_parser_free($parserXml);
  112. if (empty($tags)) {
  113. throw new Exception("Empty structure from request");
  114. }
  115. $rootTagName = V::get('tag', '', $tags[0]);
  116. if ('new_record' != $rootTagName) {
  117. throw new Exception("Empty structure from request");
  118. }
  119. $tagsTotal = count($tags);
  120. for ($i = 1; $i < $tagsTotal - 1; $i++) {
  121. $vTag = $tags[$i];
  122. if ($vTag['level'] != 2) {
  123. throw new Exception("Structure error: only 2 level is allowed");
  124. }
  125. $item[$vTag['tag']] = V::get('value', null, $vTag);
  126. }
  127. if (empty($item)) {
  128. throw new Exception("Empty item from request");
  129. }
  130. $apiTransactionHelper->setActionObject($actionId, $item);
  131. $newItemId = $dataSource->createItem($item);
  132. $apiTransactionHelper->setActionResult($actionId, $newItemId);
  133. 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>';}
  134. $itemCreated = $dataSource->getItem($newItemId);
  135. $itemsDocument = new stdClass();
  136. $itemsDocument->items = array();
  137. $itemWrap = new stdClass();
  138. $itemWrap->item = $itemCreated;
  139. $itemsDocument->items[] = $itemWrap;
  140. 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>';}
  141. return $itemsDocument;
  142. }
  143. private function newitemResultAction($request) {
  144. 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>';}
  145. $dataSource = $this->getDataSource();
  146. if (!$dataSource) {
  147. throw new HttpException("DataSource not exists", 404);
  148. }
  149. $useTransactionKey = array_shift($request->segments);
  150. if (empty($useTransactionKey)) {
  151. throw new HttpException("Handler argument not defined", 400);
  152. }
  153. if (!empty($useTransactionKey)) {
  154. $handlerPattern = '/^([a-zA-Z0-9\-_]+)$/';
  155. $matches = array();
  156. if (!preg_match($handlerPattern, $useTransactionKey, $matches)) {
  157. throw new HttpException("Wrong handler argument", 400);
  158. }
  159. }
  160. $apiTransactionHelper = new Api_TransactionHelper();
  161. $actionInfo = $apiTransactionHelper->getLastActionInfo($useTransactionKey);
  162. if (empty($actionInfo)) {
  163. throw new HttpException("Transaction not exists", 404);
  164. }
  165. $resultItemId = V::get('RESULT_ID', '', $actionInfo);
  166. $itemCreated = $dataSource->getItem($resultItemId);
  167. $itemsDocument = new stdClass();
  168. $itemsDocument->TransactionResult = new stdClass();
  169. $itemsDocument->TransactionResult->Status = 'SUCCESS';
  170. $itemsDocument->TransactionResponse = new stdClass();
  171. $itemsDocument->TransactionResponse->InsertResult = array();
  172. $itemWrap = new stdClass();
  173. $itemWrap->item = $itemCreated;
  174. $itemsDocument->TransactionResponse->InsertResult[] = $itemWrap;
  175. 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>';}
  176. return $itemsDocument;
  177. }
  178. private function tableSchemaAction($request) {
  179. 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>';}
  180. $dataSource = $this->getDataSource();
  181. if (!$dataSource) {
  182. throw new HttpException("DataSource not exists", 404);
  183. }
  184. $schema = $dataSource->getSchema();
  185. $schemaDocument = $schema;
  186. return $schemaDocument;
  187. }
  188. private function getDataSource() {
  189. if (!$this->_dataSource) {
  190. // TODO: get data source from Factory
  191. {
  192. if ('default_db' == $this->_dataSourceName) {
  193. $this->_dataSource = new ApiDataSourceTodo($this->_dataSourceName);
  194. $this->_dataSource->setTable($this->_tblName);
  195. }
  196. else if ('931' == $this->_dataSourceName) {
  197. $this->_dataSource = new ApiDataSourceTodo($this->_dataSourceName);
  198. $this->_dataSource->setTable($this->_tblName);
  199. }
  200. }
  201. }
  202. return $this->_dataSource;
  203. }
  204. }