_apiUser->isAdmin()) { throw new HttpException("Forbidden", 403); } IF(V::get('DBG','',$_GET)){echo'
request->segments (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($request->segments);echo'
';} 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'
request (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($request);echo'
';} $dataSource = $this->getDataSource(); if (!$dataSource) { throw new HttpException("DataSource not exists", 404); } $items = $dataSource->getItems($request->args); IF(V::get('DBG','',$_GET)){echo'
items (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($items);echo'
';} $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'
itemsDocument (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($itemsDocument);echo'
';} return $itemsDocument; } private function itemAction($request) { IF(V::get('DBG','',$_GET)){echo'
request (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($request);echo'
';} 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'
item (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($item);echo'
';} $itemsDocument = new stdClass(); $itemsDocument->items = array(); $itemWrap = new stdClass(); $itemWrap->item = $item; $itemsDocument->items[] = $itemWrap; IF(V::get('DBG','',$_GET)){echo'
itemsDocument (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($itemsDocument);echo'
';} return $itemsDocument; } /** * Http PUT xml file like this: default_default_objects_types:MAGAZYN */ private function newitemAction($request) { IF(V::get('DBG','',$_GET)){echo'
request (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($request);echo'
';} $dataSource = $this->getDataSource(); if (!$dataSource) { throw new HttpException("DataSource not exists", 404); } $requestBodyXml = Request::getRequestBody(); 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'
item (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($item);echo'
';} $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'
itemsDocument (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($itemsDocument);echo'
';} return $itemsDocument; } private function newitemResultAction($request) { IF(V::get('DBG','',$_GET)){echo'
request (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($request);echo'
';} $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'
itemsDocument (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($itemsDocument);echo'
';} return $itemsDocument; } private function tableSchemaAction($request) { IF(V::get('DBG','',$_GET)){echo'
request (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($request);echo'
';} $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; } }