Storage.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. <?php
  2. // @requires $_SERVER['SERVER_NAME']
  3. Lib::loadClass('RouteBase');
  4. Lib::loadClass('Schema_TableFactory');
  5. Lib::loadClass('Response');
  6. /*
  7. # Storage:
  8. - [ ] view available storage (from Zasoby - type 'BAZA_DANYCH', 'DATABASE_MYSQL', ...)
  9. - [ ] check config for connection
  10. - [ ] add cells to Zasoby
  11. - [ ] create cells in Storage
  12. - [x] use PDO
  13. */
  14. class Route_Storage extends RouteBase {
  15. public function handleAuth() {
  16. if (!User::logged()) {
  17. User::authByRequest();
  18. }
  19. }
  20. public function defaultAction() {
  21. SE_Layout::gora();
  22. SE_Layout::menu();
  23. try {
  24. $storageList = $this->getStorageList();
  25. if (empty($storageList)) throw new Exception("No storage defined");
  26. $storageMenu = array();
  27. foreach ($storageList as $storage) {
  28. $menuItem = array();
  29. $menuItem['id'] = $id = $storage['ID'];
  30. $menuItem['nazwa'] = $storage['DESC'];
  31. $menuItem['typ'] = $storage['TYPE'];
  32. $menuItem['tabele i widoki'] = '<a href="index.php?_route=Storage&_task=tableList&idStorage=' . $id . '">' . "tabele" . '</a>';
  33. //$menuItem['views'] = '<a href="index.php?_route=Storage&_task=viewList&idStorage=' . $id . '">' . "views" . '</a>';
  34. $menuItem['raw info'] = '<a href="index.php?_route=Storage&_task=rawInfo&idStorage=' . $id . '">' . "raw info" . '</a>';
  35. $menuItem['xsd'] = '<a href="index.php?_route=Storage&_task=xsd&idStorage=' . $id . '" target="_blank">' . "xsd" . '</a>';
  36. $storageMenu[] = $menuItem;
  37. }
  38. $this->navView();
  39. DBG::table("storageMenu", $storageMenu, __CLASS__, __FUNCTION__, __LINE__);
  40. } catch (Exception $e) {
  41. SE_Layout::alert('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage());
  42. }
  43. SE_Layout::dol();
  44. }
  45. public function tableListAction() {
  46. SE_Layout::gora();
  47. SE_Layout::menu();
  48. try {
  49. $idStorage = V::get('idStorage', 0, $_REQUEST, 'int');
  50. if (empty($idStorage)) throw new Exception("No id storage");
  51. $storageList = $this->getStorageList();
  52. if (empty($storageList)) throw new Exception("No storage defined");
  53. if (!array_key_exists($idStorage, $storageList)) throw new Exception("No id storage not exists");
  54. $this->navView();
  55. $storagePdo = DB::getStorage($idStorage);
  56. $viewRealList = $storagePdo->getViewList();
  57. $tableRealList = $storagePdo->getTableList();
  58. $tableZasobList = array();
  59. foreach (DB::getPDO()->fetchAllByKey("
  60. select z.ID, z.`DESC`, z.A_STATUS
  61. from CRM_LISTA_ZASOBOW z
  62. where z.PARENT_ID = '{$idStorage}'
  63. ", $key = 'DESC') as $ind => $row) {
  64. $tableZasobList[strtolower($ind)] = $row;
  65. }
  66. $emptyItem = array();
  67. $emptyItem['nazwa'] = '';//->urlTblStruct = $this->buildActionUrl('tableStruct', array('storageId'=>$storageId, 'tblName'=>$tblName));
  68. $emptyItem['type'] = '';
  69. $emptyItem['dodaj zasób'] = '';//->urlTblAddToZasoby = $this->buildAjaxActionUrl('addTableToZasoby', $urlParams);
  70. $emptyItem['struktura'] = '';//->urlTblRawStruct = $this->buildActionUrl('tableStructRaw', array('storageId'=>$storageId, 'tblName'=>$tblName));
  71. $emptyItem['objectTest'] = '';//->urlObjectTest = $this->buildActionUrl('objectTest', array('storageId'=>$storageId, 'tblName'=>$tblName));
  72. $emptyItem['xsd'] = '';//->urlTableXsd = $this->buildActionUrl('tableXsd', array('storageId'=>$storageId, 'tblName'=>$tblName, 'HEADER_NOT_INIT'=>'YES'));
  73. $emptyItem['id_zasob'] = '';//->zasobId = $zasobTableId;
  74. $emptyItem['isHist'] = '';//->isHistTable = ('_HIST' == substr($tblName, -5)) ? true : false;
  75. $emptyItem['uwagi'] = '';
  76. $tableList = array();
  77. foreach ($tableRealList as $row) {
  78. $tblName = $row['table_name'];
  79. $tblItem = V::cloneArray($emptyItem);
  80. $tblItem['nazwa'] = $tblName;
  81. $tblItem['type'] = $row['table_type'];
  82. $tblItem['struktura'] = '<a href="index.php?_route=Storage&_task=tableStruct&idStorage=' . $idStorage . '&table=' . $tblName . '">' . "struct" . '</a>';
  83. $tblItem['xsd'] = '<a href="index.php?_route=Storage&_task=tableXsd&idStorage=' . $idStorage . '&table=' . $tblName . '" target="_blank">' . "xsd" . '</a>';
  84. $tblItem['objectTest'] = '<a href="index.php?_route=Storage&_task=objectTest&idStorage=' . $idStorage . '&table=' . $tblName . '">' . "objectTest" . '</a>';
  85. $tblItem['isHist'] = ('_hist' == substr($tblName, -5));
  86. $tblItem['uwagi'] = '';
  87. $tblZasob = V::get($tblName, '', $tableZasobList);
  88. if ($tblZasob) {
  89. $tableZasobList[$tblName]['_checked'] = true;
  90. $tblItem['id_zasob'] = $tblZasob['ID'];
  91. } else {
  92. $tblItem['uwagi'] .= 'TODO: ADD ZASOB';
  93. $ajaxAddZasobLink = Request::getUriDirName() . "/index.php?_route=Storage&_task=addTableToZasoby&idStorage={$idStorage}&tblName={$tblName}";
  94. $onClick = "return p5UI__ButtonAjax(this, { href: '{$ajaxAddZasobLink}' })";
  95. $tblItem['dodaj zasób'] = '<a onclick="'.$onClick.'" class="btn btn-xs btn-primary" href="#">TODO: ADD ZASOB</a>';
  96. }
  97. $tableList[] = $tblItem;
  98. }
  99. foreach ($viewRealList as $row) {
  100. $tblName = $row['table_name'];
  101. $tblItem = V::cloneArray($emptyItem);
  102. $tblItem['nazwa'] = $tblName;
  103. $tblItem['type'] = $row['table_type'];
  104. $tblItem['struktura'] = '<a href="index.php?_route=Storage&_task=viewStruct&idStorage=' . $idStorage . '&table=' . $tblName . '">' . "struct" . '</a>';
  105. $tblItem['uwagi'] = '';
  106. $tblZasob = V::get($tblName, '', $tableZasobList);
  107. if ($tblZasob) {
  108. $tableZasobList[$tblName]['_checked'] = true;
  109. $tblItem['id_zasob'] = $tblZasob['ID'];
  110. } else {
  111. $tblItem['uwagi'] .= 'TODO: ADD ZASOB';
  112. $tblItem['dodaj zasób'] = '<a href="#">TODO: ADD ZASOB</a>';
  113. }
  114. $tableList[] = $tblItem;
  115. }
  116. foreach ($tableZasobList as $tblName => $row) {
  117. if (!$row['_checked']) {
  118. $tblItem = V::cloneArray($emptyItem);
  119. $tblItem['nazwa'] = $tblName;
  120. $tblItem['id_zasob'] = $row['ID'];
  121. $tblItem['type'] = 'unknown';
  122. $tblItem['uwagi'] = 'TODO: nie istnieje w bazie danych';
  123. $tableList[] = $tblItem;
  124. }
  125. }
  126. DBG::table("tableList", $tableList, __CLASS__, __FUNCTION__, __LINE__);
  127. ?>
  128. <script>
  129. jQuery(document).on('p5UI__ButtonAjax:click', function(e, n, payload) {
  130. console.log('event p5UI__ButtonAjax:click', n, payload);
  131. });
  132. jQuery(document).on('p5UI__ButtonAjax:ajaxLoaded', function(e, n, payload) {
  133. console.log('event p5UI__ButtonAjax:ajaxLoaded', n, payload);
  134. if ('success' == payload.type && payload.body && payload.body.id > 0) {
  135. jQuery(n).parents('td').next('td').next('td').next('td').next('td').text(payload.body.id);
  136. jQuery(n).remove();
  137. }
  138. jQuery.notify(payload.msg, payload.type);
  139. });
  140. </script>
  141. <?php
  142. } catch (Exception $e) {
  143. SE_Layout::alert('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage());
  144. }
  145. SE_Layout::dol();
  146. }
  147. public function rawInfoAction() {
  148. SE_Layout::gora();
  149. SE_Layout::menu();
  150. try {
  151. $idStorage = V::get('idStorage', 0, $_REQUEST, 'int');
  152. if (empty($idStorage)) throw new Exception("No id storage");
  153. $storageList = $this->getStorageList();
  154. if (empty($storageList)) throw new Exception("No storage defined");
  155. if (!array_key_exists($idStorage, $storageList)) throw new Exception("No id storage not exists");
  156. $this->navView();
  157. $storagePdo = DB::getStorage($idStorage);
  158. $rawInfo = $storagePdo->getTableListWithInfo();
  159. DBG::table("rawInfo", $rawInfo, __CLASS__, __FUNCTION__, __LINE__);
  160. } catch (Exception $e) {
  161. SE_Layout::alert('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage());
  162. }
  163. SE_Layout::dol();
  164. }
  165. public function tableStructAction() {
  166. SE_Layout::gora();
  167. SE_Layout::menu();
  168. try {
  169. $idStorage = V::get('idStorage', 0, $_REQUEST, 'int');
  170. if (empty($idStorage)) throw new Exception("No id storage");
  171. $storageList = $this->getStorageList();
  172. if (empty($storageList)) throw new Exception("No storage defined");
  173. if (!array_key_exists($idStorage, $storageList)) throw new Exception("No id storage not exists");
  174. $tblName = V::get('table', '', $_REQUEST, 'word');
  175. if (empty($tblName)) throw new Exception("No table name");
  176. $this->navView();
  177. $storagePdo = DB::getStorage($idStorage);
  178. $tblStruct = $storagePdo->getTableStruct($tblName);
  179. $idTable = $this->fetchTableId($idStorage, $tblName);
  180. if ($idTable <= 0) {
  181. SE_Layout::alert('warning', "Zasob tabela '{$tblName}' nie istnieje");// TODO: add p5UI btn
  182. DBG::table("tblStruct", $tblStruct, __CLASS__, __FUNCTION__, __LINE__);
  183. throw new Exception("Zasob tabela '{$tblName}' nie istnieje");
  184. }
  185. $cellZasobList = array();
  186. foreach (DB::getPDO()->fetchAllByKey("
  187. select z.ID, z.`DESC`, z.A_STATUS
  188. from CRM_LISTA_ZASOBOW z
  189. where z.PARENT_ID = '{$idTable}'
  190. ", $key = 'DESC') as $ind => $row) {
  191. $cellZasobList[strtolower($ind)] = $row;
  192. }
  193. $emptyItem = array();
  194. $emptyItem['name'] = '';
  195. $emptyItem['dodaj zasób'] = '';
  196. $emptyItem['id_zasob'] = '';
  197. $emptyItem['uwagi'] = '';
  198. $emptyItem['type'] = '';
  199. $emptyItem['is_nullable'] = '';
  200. $emptyItem['default_value'] = '';
  201. $emptyItem['default_is_null'] = '';
  202. $emptyItem['max_length'] = '';
  203. $emptyItem['num_precision'] = '';
  204. $emptyItem['num_scale'] = '';
  205. $emptyItem['char_encoding'] = '';
  206. $emptyItem['char_collation'] = '';
  207. $emptyItem['extra'] = '';
  208. $emptyItem['raw_storage_type'] = '';
  209. $tableList = array();
  210. foreach ($tblStruct as $row) {
  211. $cellName = $row['name'];
  212. $tblItem = V::cloneArray($emptyItem);
  213. $tblItem['name'] = $cellName;
  214. foreach ($row as $fldName => $fldVal) {
  215. if (array_key_exists($fldName, $tblItem)) $tblItem[$fldName] = $fldVal;
  216. }
  217. $tblItem['uwagi'] = '';
  218. $lowerCellName = strtolower($cellName);
  219. $tblZasob = V::get($lowerCellName, '', $cellZasobList);
  220. if ($tblZasob) {
  221. $cellZasobList[$lowerCellName]['_checked'] = true;
  222. $tblItem['id_zasob'] = $tblZasob['ID'];
  223. } else {
  224. $tblItem['uwagi'] .= '!Zasob';//'TODO: ADD ZASOB';
  225. $ajaxAddZasobLink = Request::getUriDirName() . "/index.php?_route=Storage&_task=addCellToZasoby&idStorage={$idStorage}&tblName={$tblName}&cellName={$cellName}";
  226. $onClick = "return p5UI__ButtonAjax(this, { href: '{$ajaxAddZasobLink}' })";
  227. $tblItem['dodaj zasób'] = '<a onclick="'.$onClick.'" class="btn btn-xs btn-primary" href="#">TODO: ADD ZASOB</a>';
  228. }
  229. $tableList[] = $tblItem;
  230. }
  231. foreach ($cellZasobList as $cellName => $row) {
  232. if (!$row['_checked']) {
  233. $tblItem = V::cloneArray($emptyItem);
  234. $tblItem['name'] = $cellName;
  235. $tblItem['id_zasob'] = $row['ID'];
  236. $tblItem['uwagi'] = '!DB';//'TODO: nie istnieje w bazie danych';
  237. $tableList[] = $tblItem;
  238. }
  239. }
  240. DBG::table("tableList", $tableList, __CLASS__, __FUNCTION__, __LINE__);
  241. ?>
  242. <script>
  243. jQuery(document).on('p5UI__ButtonAjax:click', function(e, n, payload) {
  244. console.log('event p5UI__ButtonAjax:click', n, payload);
  245. });
  246. jQuery(document).on('p5UI__ButtonAjax:ajaxLoaded', function(e, n, payload) {
  247. console.log('event p5UI__ButtonAjax:ajaxLoaded', n, payload);
  248. if ('success' == payload.type && payload.body && payload.body.id > 0) {
  249. jQuery(n).parents('td').next('td').text(payload.body.id);
  250. jQuery(n).remove();
  251. }
  252. jQuery.notify(payload.msg, payload.type);
  253. });
  254. </script>
  255. <?php
  256. } catch (Exception $e) {
  257. SE_Layout::alert('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage());
  258. }
  259. SE_Layout::dol();
  260. }
  261. public function xsdAction() {
  262. $idStorage = V::get('idStorage', 0, $_REQUEST, 'int');
  263. $storage = DB::getStorage($idStorage);
  264. $tableRealList = $storage->getTableList();
  265. $srvName = $_SERVER['SERVER_NAME'];
  266. $storageZasobId = $storage->getZasobId();
  267. $objNs = "p5_{$storageZasobId}_{$tblName}";
  268. $objNsUri = "https://biuro.biall-net.pl/api/{$storageZasobId}/{$tblName}";
  269. $p5TypePrefix = "p5Type";
  270. $p5TypeNsUri = "http://biuro.biall-net.pl/p5/schema/types";
  271. $p5TypeNsLocation = "http://biuro.biall-net.pl/p5/schema/types.xsd";
  272. header('Content-type: text/plain; charset=utf-8');// TODO: test
  273. //header('Content-type: application/xml; charset=utf-8');
  274. $xmlWriter = new XMLWriter();
  275. $xmlWriter->openUri('php://output');
  276. $xmlWriter->setIndent(true);
  277. if (!$xmlWriter) throw new HttpException("Error no XMLWriter", 404);
  278. $xmlWriter->startDocument('1.0', 'UTF-8');
  279. $xmlWriter->startElement('xs:schema');
  280. $xmlWriter->writeAttribute('xmlns:xs', 'http://www.w3.org/2001/XMLSchema');
  281. $xmlWriter->writeAttribute('xmlns:vc', 'http://www.w3.org/2007/XMLSchema-versioning');
  282. $xmlWriter->writeAttribute("xmlns:{$p5TypePrefix}", $p5TypeNsUri);
  283. $xmlWriter->writeAttribute('elementFormDefault', 'qualified');
  284. $xmlWriter->writeAttribute('targetNamespace', $objNsUri);
  285. $xmlWriter->writeAttribute('vc:minVersion', '1.1');
  286. $xmlWriter->startElement('xs:import');
  287. $xmlWriter->writeAttribute('namespace', $p5TypeNsUri);
  288. $xmlWriter->writeAttribute('schemaLocation', $p5TypeNsLocation);
  289. $xmlWriter->endElement();// xs:import
  290. foreach ($tableRealList as $tbl) {
  291. $tblName = $tbl['table_name'];
  292. $this->tableXsdViewXmlWriter($xmlWriter, $idStorage, $tblName);
  293. }
  294. $xmlWriter->endElement();// xs:schema
  295. $xmlWriter->endDocument();
  296. }
  297. public function tableXsdAction() {
  298. $idStorage = V::get('idStorage', '', $_GET);
  299. $tblName = V::get('table', '', $_GET, 'word');
  300. if (empty($tblName)) die("Wrong table name");
  301. header('Content-type: text/plain; charset=utf-8');// TODO: test
  302. $storage = DB::getStorage($idStorage);
  303. $storageZasobId = $storage->getZasobId();
  304. $objNs = "p5_{$storageZasobId}_{$tblName}";
  305. $objNsUri = "https://biuro.biall-net.pl/api/{$storageZasobId}/{$tblName}";
  306. $p5TypePrefix = "p5Type";
  307. $p5TypeNsUri = "http://biuro.biall-net.pl/p5/schema/types";
  308. $p5TypeNsLocation = "http://biuro.biall-net.pl/p5/schema/types.xsd";
  309. //header('Content-type: application/xml; charset=utf-8');
  310. $xmlWriter = new XMLWriter();
  311. $xmlWriter->openUri('php://output');
  312. $xmlWriter->setIndent(true);
  313. if (!$xmlWriter) throw new HttpException("Error no XMLWriter", 404);
  314. $xmlWriter->startDocument('1.0', 'UTF-8');
  315. $xmlWriter->startElement('xs:schema');
  316. $xmlWriter->writeAttribute('xmlns:xs', 'http://www.w3.org/2001/XMLSchema');
  317. $xmlWriter->writeAttribute('xmlns:vc', 'http://www.w3.org/2007/XMLSchema-versioning');
  318. $xmlWriter->writeAttribute("xmlns:{$p5TypePrefix}", $p5TypeNsUri);
  319. $xmlWriter->writeAttribute('elementFormDefault', 'qualified');
  320. $xmlWriter->writeAttribute('targetNamespace', $objNsUri);
  321. $xmlWriter->writeAttribute('vc:minVersion', '1.1');
  322. $xmlWriter->startElement('xs:import');
  323. $xmlWriter->writeAttribute('namespace', $p5TypeNsUri);
  324. $xmlWriter->writeAttribute('schemaLocation', $p5TypeNsLocation);
  325. $xmlWriter->endElement();// xs:import
  326. $this->tableXsdViewXmlWriter($xmlWriter, $idStorage, $tblName);
  327. $xmlWriter->endElement();// xs:schema
  328. $xmlWriter->endDocument();
  329. }
  330. public function tableXsdViewXmlWriter(&$xmlWriter, $idStorage, $tblName) {
  331. $storage = DB::getStorage($idStorage);
  332. $schema = Schema_TableFactory::build($tblName, $idStorage, $_SERVER['SERVER_NAME']);
  333. $struct = $schema->getStruct();
  334. DBG::_('DBG', '>1', "struct", $struct, __CLASS__, __FUNCTION__, __LINE__);
  335. $typeName = "{$tblName}Type";
  336. $xmlWriter->startElement('xs:complexType');
  337. $xmlWriter->writeAttribute('name', $typeName);
  338. $xmlWriter->startElement('xs:sequence');
  339. foreach ($struct as $field) {
  340. $xmlWriter->startElement('xs:element');
  341. $xmlWriter->writeAttribute('name', $field['name']);
  342. $xmlWriter->writeAttribute('minOccurs', 0);// TODO: set minOccurs by default, etc.
  343. if ($field['is_nullable']) $xmlWriter->writeAttribute('nillable', 'true');
  344. if (null !== $field['default_value']) {
  345. $xmlWriter->writeAttribute('default', $field['default_value']);
  346. } else if (null === $field['default_value'] && $field['is_nullable']) {
  347. $xmlWriter->writeAttribute('default', $field['default_value']);
  348. } else {
  349. // TODO: Schema BUG?
  350. }
  351. if (empty($field['p5_restrictions'])) {
  352. $xmlWriter->writeAttribute('type', "p5Type:{$field['p5_type']}");
  353. } else {
  354. $xmlWriter->startElement('xs:simpleType');
  355. $xmlWriter->writeAttribute('base', "p5Type:{$field['p5_type']}");
  356. $xmlWriter->startElement('xs:restriction');
  357. if (!empty($field['p5_restrictions']['enumeration'])) {
  358. foreach ($field['p5_restrictions']['enumeration'] as $enumValue) {
  359. $xmlWriter->startElement('xs:enumeration');
  360. $xmlWriter->writeAttribute('value', $enumValue);
  361. $xmlWriter->endElement();// xs:enumeration
  362. }
  363. } else {
  364. // TODO: another restrictions...
  365. }
  366. $xmlWriter->endElement();// xs:restriction
  367. $xmlWriter->endElement();// xs:simpleType
  368. }
  369. $xmlWriter->endElement();// xs:element
  370. }
  371. $xmlWriter->endElement();// xs:sequence
  372. $xmlWriter->endElement();// xs:complexType
  373. return;
  374. // <xs:element maxOccurs="1" minOccurs="0" name="{$fldName}" nillable="true" type="xs:integer"/>
  375. $pKeyField = 'ID';//$storageObject->getPrimaryKeyFieldName();
  376. //DBG::_(true, true, "struct", $struct, __CLASS__, __FUNCTION__, __LINE__);
  377. foreach ($struct as $field) {
  378. $fldName = $vField->getName();
  379. $fldType = $vField->getType();
  380. $xsdType = $fldType->getTypeForXsd();
  381. if ($fldType->hasDefault()) {
  382. $fldDefault = $fldType->getDefault();
  383. if (!empty($fldDefault) || '0' === $fldDefault) {
  384. $elNode->setAttribute('default', $fldDefault);
  385. }
  386. }
  387. $fldRestrictions = $fldType->getRestrictions();
  388. if (empty($fldRestrictions)) {
  389. $elNode->setAttribute('type', "{$p5TypePrefix}:{$xsdType}");
  390. } else {
  391. $sType = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xs:simpleType');
  392. $elNode->appendChild($sType);
  393. $sTypeRes = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xs:restriction');
  394. $sType->appendChild($sTypeRes);
  395. $sTypeRes->setAttribute('base', "{$p5TypePrefix}:{$xsdType}");
  396. $enumList = $fldType->getEnumeration();
  397. if (empty($enumList)) {
  398. foreach ($fldRestrictions as $restricionName => $restrictionValue) {
  399. if ('maxLength' == $restricionName) {
  400. $sTypeResMaxLength = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xs:maxLength');
  401. $sTypeRes->appendChild($sTypeResMaxLength);
  402. $sTypeResMaxLength->setAttribute('value', $restrictionValue);
  403. } else if ('minLength' == $restricionName) {
  404. $sTypeResMinLength = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xs:minLength');
  405. $sTypeRes->appendChild($sTypeResMinLength);
  406. $sTypeResMinLength->setAttribute('value', $restrictionValue);
  407. } else if ('pattern' == $restricionName) {
  408. $sTypeResPattern = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xs:pattern');
  409. $sTypeRes->appendChild($sTypeResPattern);
  410. $sTypeResPattern->setAttribute('value', $restrictionValue);
  411. } else if ('fractionDigits' == $restricionName) {
  412. $sTypeResFractionDigits = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xs:fractionDigits');
  413. $sTypeRes->appendChild($sTypeResFractionDigits);
  414. $sTypeResFractionDigits->setAttribute('value', $restrictionValue);
  415. } else if ('totalDigits' == $restricionName) {
  416. $sTypeResTotalDigits = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xs:totalDigits');
  417. $sTypeRes->appendChild($sTypeResTotalDigits);
  418. $sTypeResTotalDigits->setAttribute('value', $restrictionValue);
  419. } else if ('maxExclusive' == $restricionName) {
  420. $sTypeResMaxExclusive = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xs:maxExclusive');
  421. $sTypeRes->appendChild($sTypeResMaxExclusive);
  422. $sTypeResMaxExclusive->setAttribute('value', $restrictionValue);
  423. } else if ('minExclusive' == $restricionName) {
  424. $sTypeResMinExclusive = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xs:minExclusive');
  425. $sTypeRes->appendChild($sTypeResMinExclusive);
  426. $sTypeResMinExclusive->setAttribute('value', $restrictionValue);
  427. } else if ('maxInclusive' == $restricionName) {
  428. $sTypeResMaxInclusive = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xs:maxInclusive');
  429. $sTypeRes->appendChild($sTypeResMaxInclusive);
  430. $sTypeResMaxInclusive->setAttribute('value', $restrictionValue);
  431. } else if ('minInclusive' == $restricionName) {
  432. $sTypeResMinInclusive = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xs:minInclusive');
  433. $sTypeRes->appendChild($sTypeResMinInclusive);
  434. $sTypeResMinInclusive->setAttribute('value', $restrictionValue);
  435. }
  436. /* TODO: xsd restrictions:
  437. enumeration Defines a list of acceptable values
  438. fractionDigits Specifies the maximum number of decimal places allowed. Must be equal to or greater than zero
  439. length Specifies the exact number of characters or list items allowed. Must be equal to or greater than zero
  440. maxExclusive Specifies the upper bounds for numeric values (the value must be less than this value)
  441. maxInclusive Specifies the upper bounds for numeric values (the value must be less than or equal to this value)
  442. maxLength Specifies the maximum number of characters or list items allowed. Must be equal to or greater than zero
  443. minExclusive Specifies the lower bounds for numeric values (the value must be greater than this value)
  444. minInclusive Specifies the lower bounds for numeric values (the value must be greater than or equal to this value)
  445. minLength Specifies the minimum number of characters or list items allowed. Must be equal to or greater than zero
  446. pattern Defines the exact sequence of characters that are acceptable
  447. totalDigits Specifies the exact number of digits allowed. Must be greater than zero
  448. whiteSpace Specifies how white space (line feeds, tabs, spaces, and carriage returns) is handled
  449. */
  450. }
  451. } else {
  452. foreach ($enumList as $enumValue) {
  453. $sTypeResEnum = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xs:enumeration');
  454. $sTypeRes->appendChild($sTypeResEnum);
  455. $sTypeResEnum->setAttribute('value', $enumValue);
  456. }
  457. }
  458. }
  459. }
  460. $elNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xs:element');
  461. $rootNode->appendChild($elNode);
  462. $elNode->setAttribute('name', $tblName);
  463. $elNode->setAttribute('type', "{$objNs}:{$typeName}");
  464. header('Content-type: application/xml');
  465. echo $dom->saveXML();
  466. exit;
  467. }
  468. public function navView() {
  469. $backLabel = 'back';
  470. $backLink = 'index.php?_route=Storage';
  471. $backDisabled = true;
  472. $currentLabel = 'Storage';
  473. $currentLink = 'index.php?_route=Storage';
  474. if ($task = V::get('_task', '', $_REQUEST)) {
  475. $currentLink = "index.php?_route=Storage&_task={$task}";
  476. $backDisabled = false;
  477. $idStorage = V::get('idStorage', 0, $_REQUEST, 'int');
  478. $tblName = V::get('table', '', $_REQUEST, 'word');
  479. switch ($task) {
  480. case 'tableList':
  481. case 'viewList':
  482. case 'rawInfo':
  483. $backLabel = 'Storage';
  484. $backLink = 'index.php?_route=Storage';
  485. $currentLink = "index.php?_route=Storage&_task={$task}&idStorage={$idStorage}";
  486. break;
  487. case 'tableStruct':
  488. $backLabel = "Tabele [{$idStorage}]";
  489. $backLink = "index.php?_route=Storage&_task=tableList&idStorage={$idStorage}";
  490. $currentLink = "index.php?_route=Storage&_task={$task}&idStorage={$idStorage}&table={$tblName}";
  491. break;
  492. }
  493. switch ($task) {
  494. case 'tableList': $currentLabel = "Tabele [{$idStorage}]"; break;
  495. case 'viewList': $currentLabel = "Widoki [{$idStorage}]"; break;
  496. case 'rawInfo': $currentLabel = "Raw info [{$idStorage}]"; break;
  497. case 'tableStruct': $currentLabel = "Struktura tabeli '{$tblName}'"; break;
  498. }
  499. }
  500. ?>
  501. <nav class="navbar navbar-default navbar-static-top">
  502. <div class="container-fluid">
  503. <div class="navbar-left">
  504. <ul class="nav navbar-nav navbar-center">
  505. <li><a href="<?php echo $backLink; ?>" class="btn <?php echo ($backDisabled)? 'disabled' : ''; ?>"><i class="glyphicon glyphicon-chevron-left"></i> <?php echo $backLabel; ?></a></li>
  506. </ul>
  507. </div>
  508. <div class="navbar-left">
  509. <ul class="nav navbar-nav navbar-center">
  510. <li><a class="btn" href="<?php echo $currentLink; ?>"><?php echo $currentLabel; ?></a></li>
  511. </ul>
  512. </div>
  513. <div class="navbar-right">
  514. <!--
  515. <ul class="nav navbar-nav navbar-right">
  516. <li><a href="#">Link</a></li>
  517. <li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
  518. <ul class="dropdown-menu">
  519. <li><a href="#">Action</a></li>
  520. <li><a href="#">Another action</a></li>
  521. <li><a href="#">Something else here</a></li>
  522. <li role="separator" class="divider"></li>
  523. <li><a href="#">Separated link</a></li>
  524. </ul>
  525. </li>
  526. </ul>
  527. -->
  528. </div>
  529. </div>
  530. </nav>
  531. <?php
  532. }
  533. public function getStorageList() {
  534. $storageList = array();
  535. $sth = DB::getPDO()->prepare("
  536. select z.ID, z.`DESC`, z.`TYPE`
  537. from CRM_LISTA_ZASOBOW z
  538. where z.TYPE in('BAZA_DANYCH','DATABASE_MYSQL','DATABASE_POSTGRESQL')
  539. ");
  540. $sth->execute();
  541. $rows = $sth->fetchAll();
  542. foreach ($rows as $row) {
  543. $storageList[$row['ID']] = $row;
  544. }
  545. return $storageList;
  546. }
  547. public function addTableToZasobyAction() {// sends JSON
  548. $response = new stdClass();
  549. try {
  550. $idStorage = V::get('storageId', '', $_GET);
  551. $tblName = V::get('tblName', '', $_GET, 'word');
  552. if (empty($tblName)) throw new HttpException("Wrong table name");
  553. // $response->zasobTblId = $zasobItemFound->TABLE_ID;
  554. // $response->zasobId = $zasobItemFound->ID;
  555. $storage = DB::getStorage($idStorage);
  556. $tableStruct = $storage->getTableStruct($tblName);
  557. $zasobStorageId = $storage->getZasobId();
  558. if (!is_numeric($zasobStorageId)) throw new HttpException("Storage id is not set in config file");
  559. $zasobItem = array();
  560. $zasobItem['PARENT_ID'] = $zasobStorageId;
  561. $zasobItem['TYPE'] = 'TABELA';
  562. $zasobItem['DESC'] = $tblName;
  563. $zasobItem['DESC_PL'] = $tblName;
  564. $zasobItemFound = null;
  565. {
  566. $rows = DB::getPDO()->fetchAll("
  567. select z.`ID`, z.`DESC`
  568. from `CRM_LISTA_ZASOBOW` z
  569. where z.`PARENT_ID`='{$zasobStorageId}'
  570. and z.`DESC`='{$tblName}'
  571. and z.`A_STATUS` in('NORMAL','WAITING')
  572. ");
  573. if (!empty($rows)) {
  574. $zasobItemFound = $rows[0]['ID'];
  575. }
  576. }
  577. if ($zasobItemFound > 0) {
  578. $response->_replaceButtonNode = "[{$zasobItemFound}]";
  579. throw new AlertInfoException("Zasob tabela '{$tblName}' już istnieje - nr '{$zasobItemFound}'");
  580. }
  581. $acl = User::getAcl()->getObjectAcl('default_db', 'crm_lista_zasobow');
  582. if (!$acl) throw new Exception("Brak dostępu do tabeli Zasoby");
  583. $item = array();
  584. $item['PARENT_ID'] = $zasobStorageId;
  585. $item['TYPE'] = 'TABELA';
  586. $item['DESC'] = $tblName;
  587. $item['DESC_PL'] = $tblName;
  588. if (DBG::isActive()) $response->_itemToCreate = $item;
  589. $createdId = $acl->addItem($item);
  590. if (!$createdId) throw new Exception("Nie udało się utworzyć nowego rekordu!");
  591. $response->id = $createdId;
  592. $response->record = $acl->getItem($createdId);
  593. $response->_replaceButtonNode = "[{$createdId}]";
  594. throw new AlertSuccessException("Utworzono pomyślnie rekord nr {$createdId}");
  595. } catch (AlertSuccessException $e) {
  596. $response->type = 'success';
  597. $response->msg = $e->getMessage();
  598. } catch (AlertInfoException $e) {
  599. $response->type = 'info';
  600. $response->msg = $e->getMessage();
  601. } catch (Exception $e) {
  602. $response->type = 'error';
  603. $response->msg = $e->getMessage();
  604. }
  605. Response::sendJsonExit($response);
  606. }
  607. public function addCellToZasobyAction() {// sends JSON
  608. $response = new stdClass();
  609. try {
  610. $idStorage = V::get('storageId', '', $_GET);
  611. $tblName = V::get('tblName', '', $_GET, 'word');
  612. $cellName = V::get('cellName', '', $_GET, 'word');
  613. if (empty($tblName)) throw new HttpException("Wrong table name");
  614. if (empty($tblName)) throw new HttpException("Wrong cell name");
  615. $storage = DB::getStorage($idStorage);
  616. $tableStruct = $storage->getTableStruct($tblName);
  617. $zasobStorageId = $storage->getZasobId();
  618. if (!is_numeric($zasobStorageId)) throw new HttpException("Storage id is not set in config file");
  619. $idTable = $this->fetchTableId($zasobStorageId, $tblName);
  620. if ($idTable <= 0) throw new Exception("Zasob tabela '{$tblName}' nie istnieje");
  621. $acl = User::getAcl()->getObjectAcl('default_db', 'crm_lista_zasobow');
  622. if (!$acl) throw new Exception("Brak dostępu do tabeli Zasoby");
  623. $item = array();
  624. $item['PARENT_ID'] = $idTable;
  625. $item['TYPE'] = 'KOMORKA';
  626. $item['DESC'] = $cellName;
  627. $item['DESC_PL'] = $cellName;
  628. if (DBG::isActive()) $response->_itemToCreate = $item;
  629. $createdId = $acl->addItem($item);
  630. if (!$createdId) throw new Exception("Nie udało się utworzyć nowego rekordu!");
  631. $response->id = $createdId;
  632. $response->record = $acl->getItem($createdId);
  633. $response->_replaceButtonNode = "[{$createdId}]";
  634. throw new AlertSuccessException("Utworzono pomyślnie rekord nr {$createdId}");
  635. } catch (AlertSuccessException $e) {
  636. $response->type = 'success';
  637. $response->msg = $e->getMessage();
  638. } catch (AlertInfoException $e) {
  639. $response->type = 'info';
  640. $response->msg = $e->getMessage();
  641. } catch (Exception $e) {
  642. $response->type = 'error';
  643. $response->msg = $e->getMessage();
  644. }
  645. Response::sendJsonExit($response);
  646. }
  647. public function fetchTableId($idZasobStorage, $tblName) {
  648. $rows = DB::getPDO()->fetchAll("
  649. select z.`ID`, z.`DESC`
  650. from `CRM_LISTA_ZASOBOW` z
  651. where z.`PARENT_ID`='{$idZasobStorage}'
  652. and z.`DESC`='{$tblName}'
  653. and z.`A_STATUS` in('NORMAL','WAITING')
  654. ");
  655. if (!empty($rows)) return $rows[0]['ID'];
  656. return null;
  657. }
  658. }