Table.php 9.7 KB

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