Storage.php 34 KB

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