Storage.php 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202
  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. - [x] view available storage (from Zasoby - type 'BAZA_DANYCH', 'DATABASE_MYSQL', ...)
  11. - [x] check config for connection
  12. - [x] 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=coreObjectParseAll">' . "parse All" . '</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 checkObjectInstallAjaxAction() {
  72. $response = array();
  73. try {
  74. $objectName = V::get('object', '', $_REQUEST, 'word');
  75. if (empty($objectName)) throw new Exception("Missing Object name");
  76. $response['object'] = $objectName;
  77. $json = OBJ::getCoreObjectFromFile($objectName);
  78. OBJ::checkInstall($json);
  79. $response['type'] = 'success';
  80. $response['msg'] = "OK - object installed";
  81. } catch (Exception $e) {
  82. $response['type'] = 'error';
  83. $response['msg'] = $e->getMessage();
  84. $response['code'] = $e->getCode();
  85. $response['line'] = $e->getLine();
  86. }
  87. Response::sendJsonExit($response);
  88. }
  89. public function coreObjectStructAction() {
  90. UI::gora();
  91. UI::menu();
  92. $this->navView();
  93. try {
  94. $objectName = V::get('object', '', $_REQUEST, 'word');
  95. if (empty($objectName)) throw new Exception("Missing Object name");
  96. $json = OBJ::getCoreObjectFromFile($objectName);
  97. $label = OBJ::getLabel($json);
  98. $parentList = OBJ::getParentList($json);
  99. $linksParentList = array(); foreach ($parentList as $parentName) {
  100. $parentLink = '<a href="index.php?_route=Storage&_task=coreObjectStruct&object=' . str_replace('/', '-', $parentName) . '">' . $parentName . '</a>';
  101. array_unshift($linksParentList, $parentLink);
  102. }
  103. $ajaxCheckInstall = Request::getPathUri() . "index.php?_route=Storage&_task=checkObjectInstallAjax&object={$objectName}";
  104. $onClick = "return p5UI__ButtonAjax(this, 'p5UIBtnAjax:Storage:checkObjectInstallAjax', { href: '{$ajaxCheckInstall}' })";
  105. $btnCheckInstall = '<a onclick="'.$onClick.'" href="#" title="check install db tables"><i class="glyphicon glyphicon-cog"></i></a>';
  106. ?>
  107. <div class="container">
  108. <h1>Obiekt <code><?php echo $label; ?></code> <small><?php echo $btnCheckInstall; ?></small></h1>
  109. <?php if (!empty($parentList)) : ?>
  110. <ol class="breadcrumb">
  111. <li>Dziedziczy z:</li>
  112. <?php foreach ($linksParentList as $parentLink) : ?>
  113. <li><?php echo $parentLink; ?></li>
  114. <?php endforeach; ?>
  115. <!-- <li class="active">Data</li> -->
  116. </ol>
  117. <!-- <p> <?php echo implode(", ", $parentList); ?></p> -->
  118. <?php endif; ?>
  119. <table class="table table-bordered table-hovered">
  120. <caption>Struktura:</caption>
  121. <thead>
  122. <tr>
  123. <th>nazwa</th>
  124. <th>typ</th>
  125. <th>label</th>
  126. <th>json</th>
  127. </tr>
  128. </thead>
  129. <tbody>
  130. <?php foreach (OBJ::getFields($json) as $fieldName => $field) : ?>
  131. <tr>
  132. <td><?php echo $fieldName; ?></td>
  133. <td><?php echo $field['type']; ?></td>
  134. <td><?php echo $field['label']; ?></td>
  135. <td><?php echo json_encode($field); ?></td>
  136. </tr>
  137. <?php endforeach; ?>
  138. </tbody>
  139. </table>
  140. <a href="index.php?_route=Storage&_task=coreObjectConnect&object=<?php echo $objectName; ?>" class="btn btn-primary">Przypisz rekordy do klasy</a>
  141. <?php
  142. $mainTable = OBJ::getMainTableName($json);
  143. $sqlFields = OBJ::getTableFields($json);
  144. $this->showTableWidget($mainTable, $sqlFields);
  145. ?>
  146. </div>
  147. <script>
  148. jQuery(document).on('p5UIBtnAjax:Storage:checkObjectInstallAjax:click', function(e, n, payload) {
  149. console.log('event p5UIBtnAjax:Storage:checkObjectInstallAjax:click', n, payload);
  150. });
  151. jQuery(document).on('p5UIBtnAjax:Storage:checkObjectInstallAjax:ajaxLoaded', function(e, n, payload) {
  152. console.log('event p5UIBtnAjax:Storage:checkObjectInstallAjax:ajaxLoaded', n, payload);
  153. if ('success' == payload.type) {
  154. // jQuery(n).parents('td').text(payload.body.id);
  155. }
  156. jQuery.notify(payload.msg, payload.type);
  157. });
  158. </script>
  159. <?php
  160. DBG::_(true, true, "json", $json, __CLASS__, __FUNCTION__, __LINE__);
  161. } catch (Exception $e) {
  162. UI::alert('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage());
  163. }
  164. UI::dol();
  165. }
  166. public function coreObjectConnectAction() {
  167. UI::gora();
  168. UI::menu();
  169. $this->navView();
  170. try {
  171. $objectName = V::get('object', '', $_REQUEST, 'word');
  172. if (empty($objectName)) throw new Exception("Missing Object name");
  173. $json = OBJ::getCoreObjectFromFile($objectName);
  174. $label = OBJ::getLabel($json);
  175. $parentList = OBJ::getParentList($json);
  176. $linksParentList = array(); foreach ($parentList as $parentName) {
  177. $parentLink = '<a href="index.php?_route=Storage&_task=coreObjectStruct&object=' . str_replace('/', '-', $parentName) . '">' . $parentName . '</a>';
  178. array_unshift($linksParentList, $parentLink);
  179. }
  180. $ajaxCheckInstall = Request::getPathUri() . "index.php?_route=Storage&_task=checkObjectInstallAjax&object={$objectName}";
  181. $onClick = "return p5UI__ButtonAjax(this, 'p5UIBtnAjax:Storage:checkObjectInstallAjax', { href: '{$ajaxCheckInstall}' })";
  182. $btnCheckInstall = '<a onclick="'.$onClick.'" href="#" title="check install db tables"><i class="glyphicon glyphicon-cog"></i></a>';
  183. ?>
  184. <div class="container">
  185. <h1>Obiekt <code><?php echo $label; ?></code> <small><?php echo $btnCheckInstall; ?></small></h1>
  186. <?php if (!empty($parentList)) : ?>
  187. <ol class="breadcrumb">
  188. <li>Dziedziczy z:</li>
  189. <?php foreach ($linksParentList as $parentLink) : ?>
  190. <li><?php echo $parentLink; ?></li>
  191. <?php endforeach; ?>
  192. <!-- <li class="active">Data</li> -->
  193. </ol>
  194. <!-- <p> <?php echo implode(", ", $parentList); ?></p> -->
  195. <?php endif; ?>
  196. <?php
  197. $tableOne = array();
  198. $tableOne['caption'] = 'Klasy';
  199. $tableOne['rows'] = array();
  200. $parentList = OBJ::getParentList($json);
  201. $tableOne['rows'][] = array('__primary_key' => $objectName, 'name' => $objectName);
  202. $linksParentList = array(); foreach ($parentList as $parentName) {
  203. // $parentLink = '<a href="index.php?_route=Storage&_task=coreObjectStruct&object=' . str_replace('/', '-', $parentName) . '">' . $parentName . '</a>';
  204. // array_unshift($linksParentList, $parentLink);
  205. $tableOne['rows'][] = array('__primary_key' => $parentName, 'name' => $parentName);
  206. }
  207. $tableTwo = array();
  208. $tableTwo['caption'] = 'Rekordy w tabeli głównej';
  209. $mainTable = OBJ::getMainTableName($json);
  210. $sqlFields = OBJ::getTableFields($json);
  211. $tableTwo['rows'] = $this->getTableRows($mainTable, $sqlFields);
  212. foreach ($tableTwo['rows'] as $idx => $row) {
  213. // $tableTwo['rows'][] = array('__primary_key' => $objectName, 'name' => $objectName);
  214. $tableTwo['rows'][$idx]['__primary_key'] = $row['ID'];
  215. }
  216. echo '<hr>';
  217. // TODO: filter connected rows
  218. // TODO: filter not connected rows
  219. // TODO: action connect rows - args(schema, rows_primary_key_list)
  220. // TODO: action un connect rows - args(schema, rows_primary_key_list)
  221. $this->showConnectSchemaToTableWidget(compact('tableOne', 'tableTwo'));
  222. ?>
  223. </div>
  224. <script>
  225. jQuery(document).on('p5UIBtnAjax:Storage:checkObjectInstallAjax:click', function(e, n, payload) {
  226. console.log('event p5UIBtnAjax:Storage:checkObjectInstallAjax:click', n, payload);
  227. });
  228. jQuery(document).on('p5UIBtnAjax:Storage:checkObjectInstallAjax:ajaxLoaded', function(e, n, payload) {
  229. console.log('event p5UIBtnAjax:Storage:checkObjectInstallAjax:ajaxLoaded', n, payload);
  230. if ('success' == payload.type) {
  231. // jQuery(n).parents('td').text(payload.body.id);
  232. }
  233. jQuery.notify(payload.msg, payload.type);
  234. });
  235. </script>
  236. <?php
  237. DBG::_(true, true, "json", $json, __CLASS__, __FUNCTION__, __LINE__);
  238. } catch (Exception $e) {
  239. UI::alert('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage());
  240. }
  241. UI::dol();
  242. }
  243. public function showConnectSchemaToTableWidget($params) {
  244. if (empty($params['tableOne'])) throw new Exception("Missing tableOne in Connect widget");
  245. if (empty($params['tableTwo'])) throw new Exception("Missing tableTwo in Connect widget");
  246. $tableOne = $params['tableOne'];
  247. $tableTwo = $params['tableTwo'];
  248. // TODO: add __js_on_click to $tableOne and $tableTwo
  249. // TODO: add p5BtnAjax to table filters button
  250. // TODO: add p5BtnAjax to conn button
  251. $jsEventNamespace = 'ConnectTableWidget' . time();
  252. $htmlIdWrap = "{$jsEventNamespace}-wrap";
  253. $stateLogId = "{$jsEventNamespace}-state-log";
  254. $stateSelectedTotalId = "{$jsEventNamespace}-state-selected";
  255. $stateClearSelectedBtnId = "{$jsEventNamespace}-state-clear-selected-btn";
  256. foreach ($tableOne['rows'] as $idx => $r) {
  257. $tableOne['rows'][$idx]['__js_on_click'] = "return p5UI__Clickable(this, '{$jsEventNamespace}:tableOne', { primary_key: '{$r['__primary_key']}' });";
  258. }
  259. foreach ($tableTwo['rows'] as $idx => $r) {
  260. $tableTwo['rows'][$idx]['__js_on_click'] = "return p5UI__Clickable(this, '{$jsEventNamespace}:tableTwo', { primary_key: '{$r['__primary_key']}' });";
  261. }
  262. $tableOne['hidden_cols'] = $tableTwo['hidden_cols'] = array('__primary_key', '__js_on_click', '__html_id');
  263. $tableTwo['__html_id'] = "{$jsEventNamespace}-table-two";
  264. ?>
  265. <div id="<?php echo $htmlIdWrap; ?>">
  266. <?php UI::table($tableOne); ?>
  267. <div class="btn-group">
  268. <button class="btn btn-default" title="Wybierz rekordy wg instancji"><i class="glyphicon glyphicon-question-sign"></i></button>
  269. <button class="btn btn-default conn-btn-filter-yes">Przypisani</button>
  270. <button class="btn btn-default conn-btn-filter-no active">Nieprzypisani</button>
  271. <button class="btn btn-default conn-btn-filter-clear" title="Wszystkie"><i class="glyphicon glyphicon-remove"></i></button>
  272. </div>
  273. <div class="btn-group">
  274. <span style="padding-left:30px;">Zaznaczono
  275. <span class="conn-selected-total">0</span>
  276. <button class="conn-btn-clear-selected btn btn-link"
  277. onClick="return p5UI__Clickable(this, '<?php echo $jsEventNamespace; ?>:clearSelection', {});"
  278. style="display:none"><i class="glyphicon glyphicon-remove" style="color:red"></i></button>
  279. </span>
  280. </div>
  281. <?php UI::jsAjaxTable($tableTwo); ?>
  282. <pre class="conn-log"></pre>
  283. </div>
  284. <script>
  285. (function(){
  286. var state = {
  287. selectedTableOne: null,
  288. selectedTableTwo: [],
  289. filterSelected: 'no'
  290. }
  291. var props = {
  292. jsEventNamespace: '<?php echo $jsEventNamespace; ?>',
  293. htmlIdWrap: '<?php echo $htmlIdWrap; ?>',
  294. tableTwoId: '#<?php echo $tableTwo['__html_id']; ?>',
  295. stateSelectedTotalId: '#<?php echo $stateSelectedTotalId; ?>',
  296. stateClearSelectedBtnId: '#<?php echo $stateClearSelectedBtnId; ?>',
  297. stateLogId: '#<?php echo $stateLogId; ?>'
  298. }
  299. var wrap = jQuery('#' + props.htmlIdWrap);
  300. var jqNodes = {
  301. wrap: wrap,
  302. filter_yes: wrap.find('.conn-btn-filter-yes'),
  303. filter_no: wrap.find('.conn-btn-filter-no'),
  304. filter_clear: wrap.find('.conn-btn-filter-clear'),
  305. total_selected: wrap.find('.conn-selected-total'),
  306. btn_clear_selected: wrap.find('.conn-btn-clear-selected'),
  307. state_log: wrap.find('.conn-log')
  308. }
  309. function render() {
  310. jqNodes['total_selected'].text(state.selectedTableTwo.length);
  311. if (state.selectedTableTwo.length > 0) {
  312. jQuery(props.stateClearSelectedBtnId).show();
  313. } else {
  314. jQuery(props.stateClearSelectedBtnId).hide();
  315. }
  316. jqNodes['state_log'].text(JSON.stringify(state));
  317. switch (state.filterSelected) {
  318. case 'yes':
  319. jqNodes['filter_yes'].addClass('active');
  320. jqNodes['filter_no'].removeClass('active');
  321. jqNodes['filter_clear'].removeClass('active');
  322. break;
  323. case 'no':
  324. jqNodes['filter_yes'].removeClass('active');
  325. jqNodes['filter_no'].addClass('active');
  326. jqNodes['filter_clear'].removeClass('active');
  327. break;
  328. case 'clear':
  329. jqNodes['filter_yes'].removeClass('active');
  330. jqNodes['filter_no'].removeClass('active');
  331. jqNodes['filter_clear'].addClass('active');
  332. break;
  333. }
  334. }
  335. function action__selectTableOneRow(pk) {
  336. state.selectedTableOne = pk;
  337. render();
  338. }
  339. function action__unselectTableOneRow() {
  340. state.selectedTableOne = null;
  341. render();
  342. }
  343. function action__selectTableTwoRow(pk) {
  344. var idx = state.selectedTableTwo.indexOf(pk);
  345. if (-1 === idx) state.selectedTableTwo.push(pk);
  346. else console.log("Error selectedTableTwo - should be not set");
  347. render();
  348. }
  349. function action__unselectTableTwoRow(pk) {
  350. var idx = state.selectedTableTwo.indexOf(pk);
  351. if (-1 === idx) state.selectedTableTwo.push(pk);
  352. else state.selectedTableTwo.splice(idx, 1);
  353. render();
  354. }
  355. function action__clearSelection() {
  356. state.selectedTableTwo = [];
  357. jQuery(props.tableTwoId).find('tbody').children('.info').removeClass('info');
  358. render();
  359. }
  360. function action__filterSet(fltr) {
  361. state.filterSelected = fltr;
  362. render();
  363. // TODO: ajax load tableTwo based on filter
  364. }
  365. jQuery(document).on(props.jsEventNamespace + ':tableOne:click', function(e, n, payload) {
  366. var n$ = jQuery(n);
  367. console.log('event '+props.jsEventNamespace+':tableOne:click', n, payload);
  368. jQuery.notify('tableOne row clicked [' + payload.props.primary_key + ']', 'info');
  369. if (n$.hasClass('info')) {
  370. n$.removeClass('info');
  371. action__unselectTableOneRow();
  372. } else {
  373. n$.parent().children('.info').removeClass('info');
  374. n$.addClass('info');
  375. action__selectTableOneRow(payload.props.primary_key);
  376. }
  377. });
  378. jQuery(document).on(props.jsEventNamespace + ':tableTwo:click', function(e, n, payload) {
  379. var n$ = jQuery(n);
  380. console.log('event '+props.jsEventNamespace+':tableTwo:click', n, payload);
  381. jQuery.notify('tableTwo row clicked [' + payload.props.primary_key + ']', 'info');
  382. if (n$.hasClass('info')) {
  383. n$.removeClass('info');
  384. action__unselectTableTwoRow(payload.props.primary_key);
  385. } else {
  386. n$.addClass('info');
  387. action__selectTableTwoRow(payload.props.primary_key);
  388. }
  389. });
  390. jQuery(document).on(props.jsEventNamespace + ':clearSelection:click', function(e, n, payload) {
  391. action__clearSelection();
  392. });
  393. jqNodes['filter_yes'].on('click', function(e) {
  394. action__filterSet('yes');
  395. });
  396. jqNodes['filter_no'].on('click', function(e) {
  397. action__filterSet('no');
  398. });
  399. jqNodes['filter_clear'].on('click', function(e) {
  400. action__filterSet('clear');
  401. });
  402. })();
  403. </script>
  404. <?php
  405. }
  406. public function getTableRows($tblName, $fields) {
  407. $sqlFields = array();
  408. foreach ($fields as $fldName) {
  409. $sqlFields[] = "t.`{$fldName}`";
  410. }
  411. $sqlFields = (!empty($sqlFields))? implode(", ", $sqlFields) : "t.*";
  412. $rows = DB::getPDO()->fetchAll("
  413. select {$sqlFields}
  414. from `{$tblName}` t
  415. where 1=1
  416. limit 10
  417. ");
  418. return $rows;
  419. }
  420. public function showTableWidget($tblName, $fields) {
  421. $rows = $this->getTableRows($tblName, $fields);
  422. UI::table(array('caption' => "table({$tblName})", 'rows' => $rows));
  423. }
  424. public function coreObjectParseAllAction() {
  425. UI::gora();
  426. UI::menu();
  427. $this->navView();
  428. try {
  429. OBJ::parseAll();
  430. } catch (Exception $e) {
  431. UI::alert('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage());
  432. }
  433. UI::dol();
  434. }
  435. public function coreObjectListAction() {
  436. UI::gora();
  437. UI::menu();
  438. $this->navView();
  439. try {
  440. $coreObjlist = OBJ::getCoreObjectList();
  441. $objectList = array();
  442. foreach ($coreObjlist as $objName) {
  443. $objItem = array();
  444. $objItem['name'] = $objName;
  445. $objItem['struktura'] = '<a href="index.php?_route=Storage&_task=coreObjectStruct&object=' . $objName . '">' . "struct" . '</a>';
  446. // $objItem['label'] = "";// TODO: read from json
  447. $objectList[] = $objItem;
  448. }
  449. usort($objectList, function($rowA, $rowB) {
  450. $a = $rowA['nazwa']; $b = $rowB['nazwa'];
  451. if ($a == $b) return 0;
  452. return ($a < $b) ? -1 : 1;
  453. });
  454. DBG::table("objectList", $objectList, __CLASS__, __FUNCTION__, __LINE__);
  455. } catch (Exception $e) {
  456. UI::alert('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage());
  457. }
  458. UI::dol();
  459. }
  460. public function objectListAction() {
  461. UI::gora();
  462. UI::menu();
  463. try {
  464. } catch (Exception $e) {
  465. UI::alert('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage());
  466. }
  467. UI::dol();
  468. }
  469. public function tableListAction() {
  470. UI::gora();
  471. UI::menu();
  472. $this->navView();
  473. try {
  474. $idStorage = V::get('idStorage', 0, $_REQUEST, 'int');
  475. if (empty($idStorage)) throw new Exception("No id storage");
  476. $storageList = $this->getStorageList();
  477. if (empty($storageList)) throw new Exception("No storage defined");
  478. if (!array_key_exists($idStorage, $storageList)) throw new Exception("Storage id='{$idStorage}' not exists");
  479. $storagePdo = DB::getStorage($idStorage);
  480. $viewRealList = $storagePdo->getViewList();
  481. $tableRealList = $storagePdo->getTableList();
  482. $tableZasobList = array();
  483. foreach (DB::getPDO()->fetchAllByKey("
  484. select z.ID, z.`DESC`, z.A_STATUS
  485. from CRM_LISTA_ZASOBOW z
  486. where z.PARENT_ID = '{$idStorage}'
  487. ", $key = 'DESC') as $ind => $row) {
  488. $tableZasobList[strtolower($ind)] = $row;
  489. }
  490. $emptyItem = array();
  491. $emptyItem['nazwa'] = '';
  492. $emptyItem['type'] = '';
  493. $emptyItem['id_zasob'] = '';
  494. $emptyItem['struktura'] = '';
  495. $emptyItem['objectTest'] = '';
  496. $emptyItem['xsd'] = '';
  497. $emptyItem['isHist'] = '';
  498. $emptyItem['uwagi'] = '';
  499. $tableList = array();
  500. foreach ($tableRealList as $row) {
  501. $tblName = $row['table_name'];
  502. $tblItem = V::cloneArray($emptyItem);
  503. $tblItem['nazwa'] = $tblName;
  504. $tblItem['type'] = $row['table_type'];
  505. $tblItem['struktura'] = '<a href="index.php?_route=Storage&_task=tableStruct&idStorage=' . $idStorage . '&table=' . $tblName . '">' . "struct" . '</a>';
  506. $tblItem['xsd'] = '<a href="index.php?_route=Storage&_task=tableXsd&idStorage=' . $idStorage . '&table=' . $tblName . '" target="_blank">' . "xsd" . '</a>';
  507. $tblItem['objectTest'] = '<a href="index.php?_route=Storage&_task=objectTest&idStorage=' . $idStorage . '&table=' . $tblName . '">' . "objectTest" . '</a>';
  508. $tblItem['isHist'] = ('_hist' == substr($tblName, -5));
  509. $tblItem['uwagi'] = '';
  510. $tblZasob = V::get($tblName, '', $tableZasobList);
  511. if ($tblZasob) {
  512. $tableZasobList[$tblName]['_checked'] = true;
  513. $tblItem['id_zasob'] = $tblZasob['ID'];
  514. } else {
  515. $tblItem['uwagi'] .= 'TODO: ADD ZASOB';
  516. $ajaxAddZasobLink = Request::getPathUri() . "index.php?_route=Storage&_task=addTableToZasoby&idStorage={$idStorage}&tblName={$tblName}";
  517. $onClick = "return p5UI__ButtonAjax(this, 'p5UIBtnAjax:Storage:addTableToZasoby', { href: '{$ajaxAddZasobLink}' })";
  518. $tblItem['id_zasob'] = '<a onclick="'.$onClick.'" class="btn btn-xs btn-primary" href="#">TODO: ADD ZASOB</a>';
  519. }
  520. $tableList[] = $tblItem;
  521. }
  522. foreach ($viewRealList as $row) {
  523. $tblName = $row['table_name'];
  524. $tblItem = V::cloneArray($emptyItem);
  525. $tblItem['nazwa'] = $tblName;
  526. $tblItem['type'] = $row['table_type'];
  527. $tblItem['struktura'] = '<a href="index.php?_route=Storage&_task=viewStruct&idStorage=' . $idStorage . '&table=' . $tblName . '">' . "struct" . '</a>';
  528. $tblItem['uwagi'] = '';
  529. $tblZasob = V::get($tblName, '', $tableZasobList);
  530. if ($tblZasob) {
  531. $tableZasobList[$tblName]['_checked'] = true;
  532. $tblItem['id_zasob'] = $tblZasob['ID'];
  533. } else {
  534. $tblItem['uwagi'] .= 'TODO: ADD ZASOB';
  535. $tblItem['id_zasob'] = 'TODO: ADD ZASOB';
  536. }
  537. $tableList[] = $tblItem;
  538. }
  539. foreach ($tableZasobList as $tblName => $row) {
  540. if (!$row['_checked']) {
  541. $tblItem = V::cloneArray($emptyItem);
  542. $tblItem['nazwa'] = $tblName;
  543. $tblItem['id_zasob'] = $row['ID'];
  544. $tblItem['type'] = 'unknown';
  545. $tblItem['uwagi'] = 'TODO: nie istnieje w bazie danych';
  546. $tableList[] = $tblItem;
  547. }
  548. }
  549. usort($tableList, function($rowA, $rowB) {
  550. $a = $rowA['nazwa']; $b = $rowB['nazwa'];
  551. if ($a == $b) return 0;
  552. $a1 = substr($a, 0, 1); $b1 = substr($b, 0, 1);
  553. if (('_' == $a1 || '_' == $b1) && $a1 != $b1) {
  554. return ($a1 < $b1) ? 1 : -1;
  555. }
  556. return ($a < $b) ? -1 : 1;
  557. });
  558. DBG::table("tableList", $tableList, __CLASS__, __FUNCTION__, __LINE__);
  559. ?>
  560. <script>
  561. jQuery(document).on('p5UIBtnAjax:Storage:addTableToZasoby:click', function(e, n, payload) {
  562. console.log('event p5UIBtnAjax:Storage:addTableToZasoby:click', n, payload);
  563. });
  564. jQuery(document).on('p5UIBtnAjax:Storage:addTableToZasoby:ajaxLoaded', function(e, n, payload) {
  565. console.log('event p5UIBtnAjax:Storage:addTableToZasoby:ajaxLoaded', n, payload);
  566. if ('success' == payload.type && payload.body && payload.body.id > 0) {
  567. jQuery(n).parents('td').text(payload.body.id);
  568. }
  569. jQuery.notify(payload.msg, payload.type);
  570. });
  571. </script>
  572. <?php
  573. } catch (Exception $e) {
  574. UI::alert('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage());
  575. }
  576. UI::dol();
  577. }
  578. public function rawInfoAction() {
  579. UI::gora();
  580. UI::menu();
  581. $this->navView();
  582. try {
  583. $idStorage = V::get('idStorage', 0, $_REQUEST, 'int');
  584. if (empty($idStorage)) throw new Exception("No id storage");
  585. $storageList = $this->getStorageList();
  586. if (empty($storageList)) throw new Exception("No storage defined");
  587. if (!array_key_exists($idStorage, $storageList)) throw new Exception("No id storage not exists");
  588. $storagePdo = DB::getStorage($idStorage);
  589. $rawInfo = $storagePdo->getTableListWithInfo();
  590. DBG::table("rawInfo", $rawInfo, __CLASS__, __FUNCTION__, __LINE__);
  591. } catch (Exception $e) {
  592. UI::alert('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage());
  593. }
  594. UI::dol();
  595. }
  596. public function tableStructAction() {
  597. UI::gora();
  598. UI::menu();
  599. $this->navView();
  600. try {
  601. $idStorage = V::get('idStorage', 0, $_REQUEST, 'int');
  602. if (empty($idStorage)) throw new Exception("No id storage");
  603. $storageList = $this->getStorageList();
  604. if (empty($storageList)) throw new Exception("No storage defined");
  605. if (!array_key_exists($idStorage, $storageList)) throw new Exception("No id storage not exists");
  606. $tblName = V::get('table', '', $_REQUEST, 'word');
  607. if (empty($tblName)) throw new Exception("No table name");
  608. $storagePdo = DB::getStorage($idStorage);
  609. $tblStruct = $storagePdo->getTableStruct($tblName);
  610. $idTable = $this->fetchTableId($idStorage, $tblName);
  611. if ($idTable <= 0) {
  612. UI::alert('warning', "Zasob tabela '{$tblName}' nie istnieje");// TODO: add p5UI btn
  613. DBG::table("tblStruct", $tblStruct, __CLASS__, __FUNCTION__, __LINE__);
  614. throw new Exception("Zasob tabela '{$tblName}' nie istnieje");
  615. }
  616. $cellZasobList = array();
  617. foreach (DB::getPDO()->fetchAllByKey("
  618. select z.ID, z.`DESC`, z.A_STATUS
  619. from CRM_LISTA_ZASOBOW z
  620. where z.PARENT_ID = '{$idTable}'
  621. ", $key = 'DESC') as $ind => $row) {
  622. $cellZasobList[strtolower($ind)] = $row;
  623. }
  624. $emptyItem = array();
  625. $emptyItem['name'] = '';
  626. $emptyItem['id_zasob'] = '';
  627. $emptyItem['uwagi'] = '';
  628. $emptyItem['type'] = '';
  629. $emptyItem['is_nullable'] = '';
  630. $emptyItem['default_value'] = '';
  631. $emptyItem['default_is_null'] = '';
  632. $emptyItem['max_length'] = '';
  633. $emptyItem['num_precision'] = '';
  634. $emptyItem['num_scale'] = '';
  635. $emptyItem['char_encoding'] = '';
  636. $emptyItem['char_collation'] = '';
  637. $emptyItem['extra'] = '';
  638. $emptyItem['raw_storage_type'] = '';
  639. $tableList = array();
  640. foreach ($tblStruct as $row) {
  641. $cellName = $row['name'];
  642. $tblItem = V::cloneArray($emptyItem);
  643. $tblItem['name'] = $cellName;
  644. foreach ($row as $fldName => $fldVal) {
  645. if (array_key_exists($fldName, $tblItem)) $tblItem[$fldName] = $fldVal;
  646. }
  647. $tblItem['uwagi'] = '';
  648. $lowerCellName = strtolower($cellName);
  649. $tblZasob = V::get($lowerCellName, '', $cellZasobList);
  650. if ($tblZasob) {
  651. $cellZasobList[$lowerCellName]['_checked'] = true;
  652. $tblItem['id_zasob'] = $tblZasob['ID'];
  653. } else {
  654. $tblItem['uwagi'] .= '!Zasob';//'TODO: ADD ZASOB';
  655. $ajaxAddZasobLink = Request::getPathUri() . "index.php?_route=Storage&_task=addCellToZasoby&idStorage={$idStorage}&tblName={$tblName}&cellName={$cellName}";
  656. $onClick = "return p5UI__ButtonAjax(this, 'p5UIBtnAjax:Storage:addCellToZasoby', { href: '{$ajaxAddZasobLink}' })";
  657. $tblItem['id_zasob'] = '<a onclick="'.$onClick.'" class="btn btn-xs btn-primary" href="#">TODO: ADD ZASOB</a>';
  658. }
  659. $tableList[] = $tblItem;
  660. }
  661. foreach ($cellZasobList as $cellName => $row) {
  662. if (!$row['_checked']) {
  663. $tblItem = V::cloneArray($emptyItem);
  664. $tblItem['name'] = $cellName;
  665. $tblItem['id_zasob'] = $row['ID'];
  666. $tblItem['uwagi'] = '!DB';//'TODO: nie istnieje w bazie danych';
  667. $tableList[] = $tblItem;
  668. }
  669. }
  670. usort($tableList, function($rowA, $rowB) {
  671. $a = $rowA['name']; $b = $rowB['name'];
  672. if ('ID' == $a) return -1;
  673. if ('ID' == $b) return 1;
  674. if ($a == $b) return 0;
  675. $a1 = substr($a, 0, 1); $b1 = substr($b, 0, 1);
  676. if (('_' == $a1 || '_' == $b1) && $a1 != $b1) {
  677. return ($a1 < $b1) ? 1 : -1;
  678. }
  679. return ($a < $b) ? -1 : 1;
  680. });
  681. DBG::table("tableList", $tableList, __CLASS__, __FUNCTION__, __LINE__);
  682. ?>
  683. <script>
  684. jQuery(document).on('p5UIBtnAjax:Storage:addCellToZasoby:click', function(e, n, payload) {
  685. console.log('event p5UIBtnAjax:Storage:addCellToZasoby:click', n, payload);
  686. });
  687. jQuery(document).on('p5UIBtnAjax:Storage:addCellToZasoby:ajaxLoaded', function(e, n, payload) {
  688. console.log('event p5UIBtnAjax:Storage:addCellToZasoby:ajaxLoaded', n, payload);
  689. if ('success' == payload.type && payload.body && payload.body.id > 0) {
  690. var cellUwagi$Node = jQuery(n).parents('td').next('td');
  691. cellUwagi$Node.text(cellUwagi$Node.text().replace('!Zasob', ''))
  692. jQuery(n).parents('td').text(payload.body.id);
  693. jQuery(n).remove();
  694. }
  695. jQuery.notify(payload.msg, payload.type);
  696. });
  697. </script>
  698. <?php
  699. } catch (Exception $e) {
  700. UI::alert('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage());
  701. }
  702. UI::dol();
  703. }
  704. public function xsdAction() {
  705. $idStorage = V::get('idStorage', 0, $_REQUEST, 'int');
  706. $storage = DB::getStorage($idStorage);
  707. $tableRealList = $storage->getTableList();
  708. $srvName = $_SERVER['SERVER_NAME'];
  709. $storageZasobId = $storage->getZasobId();
  710. $objNs = "p5_{$storageZasobId}_{$tblName}";
  711. $objNsUri = "https://biuro.biall-net.pl/api/{$storageZasobId}/{$tblName}";
  712. $p5TypePrefix = "p5Type";
  713. $p5TypeNsUri = "http://biuro.biall-net.pl/p5/schema/types";
  714. $p5TypeNsLocation = "http://biuro.biall-net.pl/p5/schema/types.xsd";
  715. header('Content-type: text/plain; charset=utf-8');// TODO: test
  716. //header('Content-type: application/xml; charset=utf-8');
  717. $xmlWriter = new XMLWriter();
  718. $xmlWriter->openUri('php://output');
  719. $xmlWriter->setIndent(true);
  720. if (!$xmlWriter) throw new HttpException("Error no XMLWriter", 404);
  721. $xmlWriter->startDocument('1.0', 'UTF-8');
  722. $xmlWriter->startElement('xs:schema');
  723. $xmlWriter->writeAttribute('xmlns:xs', 'http://www.w3.org/2001/XMLSchema');
  724. $xmlWriter->writeAttribute('xmlns:vc', 'http://www.w3.org/2007/XMLSchema-versioning');
  725. $xmlWriter->writeAttribute("xmlns:{$p5TypePrefix}", $p5TypeNsUri);
  726. $xmlWriter->writeAttribute('elementFormDefault', 'qualified');
  727. $xmlWriter->writeAttribute('targetNamespace', $objNsUri);
  728. $xmlWriter->writeAttribute('vc:minVersion', '1.1');
  729. $xmlWriter->startElement('xs:import');
  730. $xmlWriter->writeAttribute('namespace', $p5TypeNsUri);
  731. $xmlWriter->writeAttribute('schemaLocation', $p5TypeNsLocation);
  732. $xmlWriter->endElement();// xs:import
  733. foreach ($tableRealList as $tbl) {
  734. $tblName = $tbl['table_name'];
  735. $this->tableXsdViewXmlWriter($xmlWriter, $idStorage, $tblName);
  736. }
  737. $xmlWriter->endElement();// xs:schema
  738. $xmlWriter->endDocument();
  739. }
  740. public function tableXsdAction() {
  741. $idStorage = V::get('idStorage', '', $_GET);
  742. $tblName = V::get('table', '', $_GET, 'word');
  743. if (empty($tblName)) die("Wrong table name");
  744. header('Content-type: text/plain; charset=utf-8');// TODO: test
  745. $storage = DB::getStorage($idStorage);
  746. $storageZasobId = $storage->getZasobId();
  747. $objNs = "p5_{$storageZasobId}_{$tblName}";
  748. $objNsUri = "https://biuro.biall-net.pl/api/{$storageZasobId}/{$tblName}";
  749. $p5TypePrefix = "p5Type";
  750. $p5TypeNsUri = "http://biuro.biall-net.pl/p5/schema/types";
  751. $p5TypeNsLocation = "http://biuro.biall-net.pl/p5/schema/types.xsd";
  752. //header('Content-type: application/xml; charset=utf-8');
  753. $xmlWriter = new XMLWriter();
  754. $xmlWriter->openUri('php://output');
  755. $xmlWriter->setIndent(true);
  756. if (!$xmlWriter) throw new HttpException("Error no XMLWriter", 404);
  757. $xmlWriter->startDocument('1.0', 'UTF-8');
  758. $xmlWriter->startElement('xs:schema');
  759. $xmlWriter->writeAttribute('xmlns:xs', 'http://www.w3.org/2001/XMLSchema');
  760. $xmlWriter->writeAttribute('xmlns:vc', 'http://www.w3.org/2007/XMLSchema-versioning');
  761. $xmlWriter->writeAttribute("xmlns:{$p5TypePrefix}", $p5TypeNsUri);
  762. $xmlWriter->writeAttribute('elementFormDefault', 'qualified');
  763. $xmlWriter->writeAttribute('targetNamespace', $objNsUri);
  764. $xmlWriter->writeAttribute('vc:minVersion', '1.1');
  765. $xmlWriter->startElement('xs:import');
  766. $xmlWriter->writeAttribute('namespace', $p5TypeNsUri);
  767. $xmlWriter->writeAttribute('schemaLocation', $p5TypeNsLocation);
  768. $xmlWriter->endElement();// xs:import
  769. $this->tableXsdViewXmlWriter($xmlWriter, $idStorage, $tblName);
  770. $xmlWriter->endElement();// xs:schema
  771. $xmlWriter->endDocument();
  772. }
  773. public function tableXsdViewXmlWriter(&$xmlWriter, $idStorage, $tblName) {
  774. $storage = DB::getStorage($idStorage);
  775. $schema = Schema_TableFactory::build($tblName, $idStorage, $_SERVER['SERVER_NAME']);
  776. $struct = $schema->getStruct();
  777. DBG::_('DBG', '>1', "struct", $struct, __CLASS__, __FUNCTION__, __LINE__);
  778. $typeName = "{$tblName}Type";
  779. $xmlWriter->startElement('xs:complexType');
  780. $xmlWriter->writeAttribute('name', $typeName);
  781. $xmlWriter->startElement('xs:sequence');
  782. foreach ($struct as $field) {
  783. $xmlWriter->startElement('xs:element');
  784. $xmlWriter->writeAttribute('name', $field['name']);
  785. $xmlWriter->writeAttribute('minOccurs', 0);// TODO: set minOccurs by default, etc.
  786. if ($field['is_nullable']) $xmlWriter->writeAttribute('nillable', 'true');
  787. if (null !== $field['default_value']) {
  788. $xmlWriter->writeAttribute('default', $field['default_value']);
  789. } else if (null === $field['default_value'] && $field['is_nullable']) {
  790. $xmlWriter->writeAttribute('default', $field['default_value']);
  791. } else {
  792. // TODO: Schema BUG?
  793. }
  794. if (empty($field['p5_restrictions'])) {
  795. $xmlWriter->writeAttribute('type', "p5Type:{$field['p5_type']}");
  796. } else {
  797. $xmlWriter->startElement('xs:simpleType');
  798. $xmlWriter->writeAttribute('base', "p5Type:{$field['p5_type']}");
  799. $xmlWriter->startElement('xs:restriction');
  800. if (!empty($field['p5_restrictions']['enumeration'])) {
  801. foreach ($field['p5_restrictions']['enumeration'] as $enumValue) {
  802. $xmlWriter->startElement('xs:enumeration');
  803. $xmlWriter->writeAttribute('value', $enumValue);
  804. $xmlWriter->endElement();// xs:enumeration
  805. }
  806. } else {
  807. // TODO: another restrictions...
  808. }
  809. $xmlWriter->endElement();// xs:restriction
  810. $xmlWriter->endElement();// xs:simpleType
  811. }
  812. $xmlWriter->endElement();// xs:element
  813. }
  814. $xmlWriter->endElement();// xs:sequence
  815. $xmlWriter->endElement();// xs:complexType
  816. return;
  817. // <xs:element maxOccurs="1" minOccurs="0" name="{$fldName}" nillable="true" type="xs:integer"/>
  818. $pKeyField = 'ID';//$storageObject->getPrimaryKeyFieldName();
  819. //DBG::_(true, true, "struct", $struct, __CLASS__, __FUNCTION__, __LINE__);
  820. foreach ($struct as $field) {
  821. $fldName = $vField->getName();
  822. $fldType = $vField->getType();
  823. $xsdType = $fldType->getTypeForXsd();
  824. if ($fldType->hasDefault()) {
  825. $fldDefault = $fldType->getDefault();
  826. if (!empty($fldDefault) || '0' === $fldDefault) {
  827. $elNode->setAttribute('default', $fldDefault);
  828. }
  829. }
  830. $fldRestrictions = $fldType->getRestrictions();
  831. if (empty($fldRestrictions)) {
  832. $elNode->setAttribute('type', "{$p5TypePrefix}:{$xsdType}");
  833. } else {
  834. $sType = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xs:simpleType');
  835. $elNode->appendChild($sType);
  836. $sTypeRes = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xs:restriction');
  837. $sType->appendChild($sTypeRes);
  838. $sTypeRes->setAttribute('base', "{$p5TypePrefix}:{$xsdType}");
  839. $enumList = $fldType->getEnumeration();
  840. if (empty($enumList)) {
  841. foreach ($fldRestrictions as $restricionName => $restrictionValue) {
  842. if ('maxLength' == $restricionName) {
  843. $sTypeResMaxLength = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xs:maxLength');
  844. $sTypeRes->appendChild($sTypeResMaxLength);
  845. $sTypeResMaxLength->setAttribute('value', $restrictionValue);
  846. } else if ('minLength' == $restricionName) {
  847. $sTypeResMinLength = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xs:minLength');
  848. $sTypeRes->appendChild($sTypeResMinLength);
  849. $sTypeResMinLength->setAttribute('value', $restrictionValue);
  850. } else if ('pattern' == $restricionName) {
  851. $sTypeResPattern = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xs:pattern');
  852. $sTypeRes->appendChild($sTypeResPattern);
  853. $sTypeResPattern->setAttribute('value', $restrictionValue);
  854. } else if ('fractionDigits' == $restricionName) {
  855. $sTypeResFractionDigits = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xs:fractionDigits');
  856. $sTypeRes->appendChild($sTypeResFractionDigits);
  857. $sTypeResFractionDigits->setAttribute('value', $restrictionValue);
  858. } else if ('totalDigits' == $restricionName) {
  859. $sTypeResTotalDigits = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xs:totalDigits');
  860. $sTypeRes->appendChild($sTypeResTotalDigits);
  861. $sTypeResTotalDigits->setAttribute('value', $restrictionValue);
  862. } else if ('maxExclusive' == $restricionName) {
  863. $sTypeResMaxExclusive = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xs:maxExclusive');
  864. $sTypeRes->appendChild($sTypeResMaxExclusive);
  865. $sTypeResMaxExclusive->setAttribute('value', $restrictionValue);
  866. } else if ('minExclusive' == $restricionName) {
  867. $sTypeResMinExclusive = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xs:minExclusive');
  868. $sTypeRes->appendChild($sTypeResMinExclusive);
  869. $sTypeResMinExclusive->setAttribute('value', $restrictionValue);
  870. } else if ('maxInclusive' == $restricionName) {
  871. $sTypeResMaxInclusive = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xs:maxInclusive');
  872. $sTypeRes->appendChild($sTypeResMaxInclusive);
  873. $sTypeResMaxInclusive->setAttribute('value', $restrictionValue);
  874. } else if ('minInclusive' == $restricionName) {
  875. $sTypeResMinInclusive = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xs:minInclusive');
  876. $sTypeRes->appendChild($sTypeResMinInclusive);
  877. $sTypeResMinInclusive->setAttribute('value', $restrictionValue);
  878. }
  879. /* TODO: xsd restrictions:
  880. enumeration Defines a list of acceptable values
  881. fractionDigits Specifies the maximum number of decimal places allowed. Must be equal to or greater than zero
  882. length Specifies the exact number of characters or list items allowed. Must be equal to or greater than zero
  883. maxExclusive Specifies the upper bounds for numeric values (the value must be less than this value)
  884. maxInclusive Specifies the upper bounds for numeric values (the value must be less than or equal to this value)
  885. maxLength Specifies the maximum number of characters or list items allowed. Must be equal to or greater than zero
  886. minExclusive Specifies the lower bounds for numeric values (the value must be greater than this value)
  887. minInclusive Specifies the lower bounds for numeric values (the value must be greater than or equal to this value)
  888. minLength Specifies the minimum number of characters or list items allowed. Must be equal to or greater than zero
  889. pattern Defines the exact sequence of characters that are acceptable
  890. totalDigits Specifies the exact number of digits allowed. Must be greater than zero
  891. whiteSpace Specifies how white space (line feeds, tabs, spaces, and carriage returns) is handled
  892. */
  893. }
  894. } else {
  895. foreach ($enumList as $enumValue) {
  896. $sTypeResEnum = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xs:enumeration');
  897. $sTypeRes->appendChild($sTypeResEnum);
  898. $sTypeResEnum->setAttribute('value', $enumValue);
  899. }
  900. }
  901. }
  902. }
  903. $elNode = $dom->createElementNS('http://www.w3.org/2001/XMLSchema', 'xs:element');
  904. $rootNode->appendChild($elNode);
  905. $elNode->setAttribute('name', $tblName);
  906. $elNode->setAttribute('type', "{$objNs}:{$typeName}");
  907. header('Content-type: application/xml');
  908. echo $dom->saveXML();
  909. exit;
  910. }
  911. public function navView() {
  912. $backLabel = 'back';
  913. $backLink = 'index.php?_route=Storage';
  914. $backDisabled = true;
  915. $currentLabel = 'Storage';
  916. $currentLink = 'index.php?_route=Storage';
  917. if ($task = V::get('_task', '', $_REQUEST)) {
  918. $currentLink = "index.php?_route=Storage&_task={$task}";
  919. $backDisabled = false;
  920. $idStorage = V::get('idStorage', 0, $_REQUEST, 'int');
  921. $tblName = V::get('table', '', $_REQUEST, 'word');
  922. $objName = V::get('object', '', $_REQUEST, 'word');
  923. switch ($task) {
  924. case 'tableList':
  925. case 'viewList':
  926. case 'rawInfo':
  927. $backLabel = 'Storage';
  928. $backLink = 'index.php?_route=Storage';
  929. $currentLink = "index.php?_route=Storage&_task={$task}&idStorage={$idStorage}";
  930. break;
  931. case 'tableStruct':
  932. $backLabel = "Tabele [{$idStorage}]";
  933. $backLink = "index.php?_route=Storage&_task=tableList&idStorage={$idStorage}";
  934. $currentLink = "index.php?_route=Storage&_task={$task}&idStorage={$idStorage}&table={$tblName}";
  935. break;
  936. case 'objectStruct': break;
  937. case 'coreObjectStruct':
  938. $backLabel = "Obiekty podstawowe";
  939. $backLink = "index.php?_route=Storage&_task=coreObjectList";
  940. $currentLink = "index.php?_route=Storage&_task={$task}&object={$objName}";
  941. break;
  942. }
  943. switch ($task) {
  944. case 'tableList': $currentLabel = "Tabele [{$idStorage}]"; break;
  945. case 'viewList': $currentLabel = "Widoki [{$idStorage}]"; break;
  946. case 'rawInfo': $currentLabel = "Raw info [{$idStorage}]"; break;
  947. case 'tableStruct': $currentLabel = "Struktura tabeli '{$tblName}'"; break;
  948. case 'coreObjectList': $currentLabel = "Obiekty podstawowe"; break;
  949. case 'coreObjectStruct': $currentLabel = "Obiekt '{$objName}'"; break;
  950. case 'objectList': $currentLabel = "Obiekty z aktualnej domeny"; break;// TODO: domain from $_GET
  951. }
  952. }
  953. ?>
  954. <nav class="navbar navbar-default navbar-static-top">
  955. <div class="container-fluid">
  956. <div class="navbar-left">
  957. <ul class="nav navbar-nav navbar-center">
  958. <li><a href="<?php echo $backLink; ?>" class="btn <?php echo ($backDisabled)? 'disabled' : ''; ?>"><i class="glyphicon glyphicon-chevron-left"></i> <?php echo $backLabel; ?></a></li>
  959. </ul>
  960. </div>
  961. <div class="navbar-left">
  962. <ul class="nav navbar-nav navbar-center">
  963. <li><a class="btn" href="<?php echo $currentLink; ?>"><?php echo $currentLabel; ?></a></li>
  964. </ul>
  965. </div>
  966. <div class="navbar-right">
  967. <!--
  968. <ul class="nav navbar-nav navbar-right">
  969. <li><a href="#">Link</a></li>
  970. <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>
  971. <ul class="dropdown-menu">
  972. <li><a href="#">Action</a></li>
  973. <li><a href="#">Another action</a></li>
  974. <li><a href="#">Something else here</a></li>
  975. <li role="separator" class="divider"></li>
  976. <li><a href="#">Separated link</a></li>
  977. </ul>
  978. </li>
  979. </ul>
  980. -->
  981. </div>
  982. </div>
  983. </nav>
  984. <?php
  985. }
  986. public function getStorageList() {
  987. $storageList = array();
  988. $sth = DB::getPDO()->prepare("
  989. select z.ID, z.`DESC`, z.`TYPE`
  990. from CRM_LISTA_ZASOBOW z
  991. where z.TYPE in('BAZA_DANYCH','DATABASE_MYSQL','DATABASE_POSTGRESQL')
  992. ");
  993. $sth->execute();
  994. $rows = $sth->fetchAll();
  995. foreach ($rows as $row) {
  996. $storageList[$row['ID']] = $row;
  997. }
  998. return $storageList;
  999. }
  1000. public function addTableToZasobyAction() {// sends JSON
  1001. $response = new stdClass();
  1002. try {
  1003. $idStorage = V::get('storageId', '', $_GET);
  1004. $tblName = V::get('tblName', '', $_GET, 'word');
  1005. if (empty($tblName)) throw new HttpException("Wrong table name");
  1006. // $response->zasobTblId = $zasobItemFound->TABLE_ID;
  1007. // $response->zasobId = $zasobItemFound->ID;
  1008. $storage = DB::getStorage($idStorage);
  1009. $tableStruct = $storage->getTableStruct($tblName);
  1010. $zasobStorageId = $storage->getZasobId();
  1011. if (!is_numeric($zasobStorageId)) throw new HttpException("Storage id is not set in config file");
  1012. $zasobItem = array();
  1013. $zasobItem['PARENT_ID'] = $zasobStorageId;
  1014. $zasobItem['TYPE'] = 'TABELA';
  1015. $zasobItem['DESC'] = $tblName;
  1016. $zasobItem['DESC_PL'] = $tblName;
  1017. $zasobItemFound = null;
  1018. {
  1019. $rows = DB::getPDO()->fetchAll("
  1020. select z.`ID`, z.`DESC`
  1021. from `CRM_LISTA_ZASOBOW` z
  1022. where z.`PARENT_ID`='{$zasobStorageId}'
  1023. and z.`DESC`='{$tblName}'
  1024. and z.`A_STATUS` in('NORMAL','WAITING')
  1025. ");
  1026. if (!empty($rows)) {
  1027. $zasobItemFound = $rows[0]['ID'];
  1028. }
  1029. }
  1030. if ($zasobItemFound > 0) {
  1031. $response->_replaceButtonNode = "[{$zasobItemFound}]";
  1032. throw new AlertInfoException("Zasob tabela '{$tblName}' już istnieje - nr '{$zasobItemFound}'");
  1033. }
  1034. $acl = User::getAcl()->getObjectAcl('default_db', 'crm_lista_zasobow');
  1035. if (!$acl) throw new Exception("Brak dostępu do tabeli Zasoby");
  1036. $item = array();
  1037. $item['PARENT_ID'] = $zasobStorageId;
  1038. $item['TYPE'] = 'TABELA';
  1039. $item['DESC'] = $tblName;
  1040. $item['DESC_PL'] = $tblName;
  1041. if (DBG::isActive()) $response->_itemToCreate = $item;
  1042. $createdId = $acl->addItem($item);
  1043. if (!$createdId) throw new Exception("Nie udało się utworzyć nowego rekordu!");
  1044. $response->id = $createdId;
  1045. $response->record = $acl->getItem($createdId);
  1046. $response->_replaceButtonNode = "[{$createdId}]";
  1047. throw new AlertSuccessException("Utworzono pomyślnie rekord nr {$createdId}");
  1048. } catch (AlertSuccessException $e) {
  1049. $response->type = 'success';
  1050. $response->msg = $e->getMessage();
  1051. } catch (AlertInfoException $e) {
  1052. $response->type = 'info';
  1053. $response->msg = $e->getMessage();
  1054. } catch (Exception $e) {
  1055. $response->type = 'error';
  1056. $response->msg = $e->getMessage();
  1057. }
  1058. Response::sendJsonExit($response);
  1059. }
  1060. public function addCellToZasobyAction() {// sends JSON
  1061. $response = new stdClass();
  1062. try {
  1063. $idStorage = V::get('storageId', '', $_GET);
  1064. $tblName = V::get('tblName', '', $_GET, 'word');
  1065. $cellName = V::get('cellName', '', $_GET, 'word');
  1066. if (empty($tblName)) throw new HttpException("Wrong table name");
  1067. if (empty($tblName)) throw new HttpException("Wrong cell name");
  1068. $storage = DB::getStorage($idStorage);
  1069. $tableStruct = $storage->getTableStruct($tblName);
  1070. $zasobStorageId = $storage->getZasobId();
  1071. if (!is_numeric($zasobStorageId)) throw new HttpException("Storage id is not set in config file");
  1072. $idTable = $this->fetchTableId($zasobStorageId, $tblName);
  1073. if ($idTable <= 0) throw new Exception("Zasob tabela '{$tblName}' nie istnieje");
  1074. $acl = User::getAcl()->getObjectAcl('default_db', 'crm_lista_zasobow');
  1075. if (!$acl) throw new Exception("Brak dostępu do tabeli Zasoby");
  1076. $item = array();
  1077. $item['PARENT_ID'] = $idTable;
  1078. $item['TYPE'] = 'KOMORKA';
  1079. $item['DESC'] = $cellName;
  1080. $item['DESC_PL'] = $cellName;
  1081. if (DBG::isActive()) $response->_itemToCreate = $item;
  1082. $createdId = $acl->addItem($item);
  1083. if (!$createdId) throw new Exception("Nie udało się utworzyć nowego rekordu!");
  1084. $response->id = $createdId;
  1085. $response->record = $acl->getItem($createdId);
  1086. $response->_replaceButtonNode = "[{$createdId}]";
  1087. throw new AlertSuccessException("Utworzono pomyślnie rekord nr {$createdId}");
  1088. } catch (AlertSuccessException $e) {
  1089. $response->type = 'success';
  1090. $response->msg = $e->getMessage();
  1091. } catch (AlertInfoException $e) {
  1092. $response->type = 'info';
  1093. $response->msg = $e->getMessage();
  1094. } catch (Exception $e) {
  1095. $response->type = 'error';
  1096. $response->msg = $e->getMessage();
  1097. }
  1098. Response::sendJsonExit($response);
  1099. }
  1100. public function fetchTableId($idZasobStorage, $tblName) {
  1101. $rows = DB::getPDO()->fetchAll("
  1102. select z.`ID`, z.`DESC`
  1103. from `CRM_LISTA_ZASOBOW` z
  1104. where z.`PARENT_ID`='{$idZasobStorage}'
  1105. and z.`DESC`='{$tblName}'
  1106. and z.`A_STATUS` in('NORMAL','WAITING')
  1107. ");
  1108. if (!empty($rows)) return $rows[0]['ID'];
  1109. return null;
  1110. }
  1111. }