Storage.php 42 KB

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