Storage.php 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  1. <?php
  2. // @requires $_SERVER['SERVER_NAME']
  3. Lib::loadClass('RouteBase');
  4. Lib::loadClass('Router');
  5. Lib::loadClass('Schema_TableFactory');
  6. Lib::loadClass('Response');
  7. Lib::loadClass('UI');
  8. Lib::loadClass('SchemaFactory');
  9. /*
  10. # Storage:
  11. - [x] view available storage (from Zasoby - type 'BAZA_DANYCH', 'DATABASE_MYSQL', ...)
  12. - [x] check config for connection
  13. - [x] add cells to Zasoby
  14. - [ ] create cells in Storage
  15. - [x] use PDO
  16. */
  17. class Route_Storage extends RouteBase {
  18. public function handleAuth() {
  19. if (!User::logged()) {
  20. User::authByRequest();
  21. }
  22. }
  23. public function defaultAction() {
  24. UI::gora();
  25. UI::menu();
  26. $this->navView();
  27. UI::startContainer();
  28. try {
  29. $sourceStorage = SchemaFactory::loadDefaultObject('SystemSource');
  30. try {
  31. $sourceStorage->getTotal();
  32. } catch (Exception $e) {
  33. UI::alert('warning', $e->getMessage());
  34. $sourceStorage->updateCache();
  35. UI::alert('info', "Lista dostępnych baz danych zaktualizowana");
  36. }
  37. if ('1' == V::get('refreshSourceList', '', $_POST)) {
  38. $sourceStorage->updateCache();
  39. UI::alert('info', "Lista dostępnych baz danych zaktualizowana");
  40. }
  41. UI::table([
  42. 'caption' => "Bazy danych " .
  43. UI::hButtonPost('<i class="glyphicon glyphicon-refresh"></i>' . " odśwież", [
  44. 'class' => "btn btn-xs btn-link",
  45. 'data' => [
  46. 'refreshSourceList' => '1'
  47. ]
  48. ]),
  49. 'rows' => array_merge(
  50. array_map(function ($item) {
  51. return [
  52. 'Nr zasobu' => $item['idZasob'],
  53. 'nazwa' => $item['name'],
  54. 'opis' => $item['description'],
  55. 'config?' => ($item['hasConfig']) ? '<span class="label label-success">TAK</span>' : '<span class="text text-muted">brak</span>',
  56. 'obiekty' => UI::h('a', [ 'href' => $this->getLink('tableList', [ 'idStorage' => $item['idZasob'] ]) ], "obiekty"),
  57. 'raw info' => UI::h('a', [ 'href' => $this->getLink('rawInfo', [ 'idStorage' => $item['idZasob'] ]) ], "raw info"),
  58. 'xsd' => UI::h('a', [ 'href' => Router::getRoute('Storage_TestXsd')->getLink('', [ 'idStorage' => $item['idZasob'] ]) ], "xsd"),
  59. ];
  60. }, $sourceStorage->getItems())
  61. , [
  62. [
  63. 'Nr zasobu' => '',
  64. 'nazwa' => "Narzędzia systemowe",
  65. 'opis' => "SystemObjects",
  66. 'config?' => '<span class="text text-muted">n/d</span>',
  67. 'obiekty' => UI::h('a', [ 'href' => Router::getRoute('Storage_Tools')->getLink() ], "narzędzia"),
  68. ],
  69. [
  70. 'Nr zasobu' => '',
  71. 'nazwa' => '<span style="color:silver">' . "Obiekty" . '</span>',
  72. 'opis' => '<span style="color:silver">' . "SystemObjects" . '</span>',
  73. 'config?' => '<span class="text text-muted">brak</span>',
  74. 'obiekty' => UI::h('a', [ 'href' => $this->getLink('systemObjects') ], "obiekty"),
  75. ],
  76. [
  77. 'Nr zasobu' => '',
  78. 'nazwa' => '<span style="color:silver">' . "Obiekty Test" . '</span>',
  79. 'opis' => '<span style="color:silver">' . "Obiekty podstawowe (test json)" . '</span>',
  80. 'config?' => '<span class="text text-muted">brak</span>',
  81. 'obiekty' => UI::h('a', [ 'href' => Router::getRoute('Storage_TestObj')->getLink('coreObjectList') ], "obiekty"),
  82. 'raw info' => UI::h('a', [ 'href' => Router::getRoute('Storage_TestObj')->getLink('coreObjectParseAll') ], "raw info"),
  83. ],
  84. [
  85. 'Nr zasobu' => '',
  86. 'nazwa' => '<span style="color:silver">' . "Obiekty Test" . '</span>',
  87. 'opis' => '<span style="color:silver">' . "Obiekty dla domeny '" . str_replace(array(".", "-"), '_', $_SERVER['SERVER_NAME']) . "' (test json)" . '</span>',
  88. 'config?' => '<span class="text text-muted">brak</span>',
  89. 'obiekty' => UI::h('a', [ 'href' => Router::getRoute('Storage_TestObj')->getLink('objectList') ], "obiekty"),
  90. // 'raw info' => UI::h('a', [ 'href' => $this->getLink('objectRawInfo') ], "raw info"),
  91. ],
  92. ]
  93. )
  94. ]);
  95. // $sourceStorage = SchemaFactory::loadDefaultObject('SystemObject');
  96. // $sourceStorage->updateCache();
  97. } catch (Exception $e) {
  98. UI::alert('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage());
  99. }
  100. UI::endContainer();
  101. UI::dol();
  102. }
  103. public function getTableRows($tblName, $fields) {
  104. $sqlFields = array();
  105. foreach ($fields as $fldName) {
  106. $sqlFields[] = "t.`{$fldName}`";
  107. }
  108. $sqlFields = (!empty($sqlFields))? implode(", ", $sqlFields) : "t.*";
  109. $rows = DB::getPDO()->fetchAll("
  110. select {$sqlFields}
  111. from `{$tblName}` t
  112. where 1=1
  113. limit 10
  114. ");
  115. return $rows;
  116. }
  117. public function showTableWidget($tblName, $fields) {
  118. $rows = $this->getTableRows($tblName, $fields);
  119. UI::table(array('caption' => "table({$tblName})", 'rows' => $rows));
  120. }
  121. public function tableListAction() {
  122. UI::gora();
  123. UI::menu();
  124. $this->navView();
  125. UI::startContainer();
  126. try {
  127. $idStorage = V::get('idStorage', 0, $_REQUEST, 'int');
  128. if (empty($idStorage)) throw new Exception("Missing id storage");
  129. $sourceStorage = SchemaFactory::loadDefaultObject('SystemSource');
  130. $sourceItem = $sourceStorage->getItem($idStorage);
  131. if (!$sourceItem) throw new Exception("Storage id='{$idStorage}' not exists");
  132. DBG::log($sourceItem, 'array', '$sourceItem');
  133. // TODO: fetch SystemObject childrens from $sourceItem:
  134. // 1. 'SystemSource' -> getItems([ 'cols' => 'SystemObject/*', 'featureId' => $sourceItem['idZasob'] ])
  135. // 2. 'SystemObject' -> getItems([ 'refFrom' => $sourceItem['idZasob'] ])
  136. // 'default_objects' => _task=systemObjects
  137. $objectStorage = SchemaFactory::loadDefaultObject('SystemObject');
  138. try {
  139. $objectStorage->getTotal();
  140. } catch (Exception $e) {
  141. UI::alert('warning', $e->getMessage());
  142. DBG::log($e);
  143. $objectStorage->updateCache();
  144. UI::alert('info', "Lista obiketów zaktualizowana");
  145. }
  146. if ('1' == V::get('refreshObjectList', '', $_POST)) {
  147. $objectStorage->updateCache();
  148. UI::alert('info', "Lista obiketów zaktualizowana");
  149. }
  150. {
  151. echo UI::h('style', ['type' => "text/css"], "
  152. .p5UI__dropdown-content { min-width:300px; padding:8px; background-color: #f6f6f6; box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2) }
  153. .p5UI__dropdown-content a { display:block; color:#000; padding:8px; text-decoration:none }
  154. .p5UI__dropdown-content a:hover { background-color:#ebebeb }
  155. ");
  156. echo UI::h('script', [], "
  157. function p5_Storage_actions_filterInput(n) {
  158. var input, filter, ul, li, a, i, div;
  159. input = n // .id-myInput
  160. filter = input.value.toUpperCase()
  161. div = n.parentNode // .id-myDropdown
  162. a = div.getElementsByTagName('a')
  163. for (i = 0; i < a.length; i++) {
  164. if (a[i].innerHTML.toUpperCase().indexOf(filter) > -1) {
  165. a[i].style.display = ''
  166. } else {
  167. a[i].style.display = 'none'
  168. }
  169. }
  170. }
  171. ");
  172. }
  173. $thisGetLink = array($this, 'getLink');
  174. UI::table([
  175. 'caption' => "Obiekty w bazie '{$sourceItem['name']}' " .
  176. UI::hButtonPost('<i class="glyphicon glyphicon-refresh"></i>' . " odśwież listę", [
  177. 'class' => "btn btn-link",
  178. 'data' => [
  179. 'refreshObjectList' => '1'
  180. ]
  181. ]) . (
  182. ($projectName = Config::getProjectName())
  183. ? " <i>project({$projectName})</i>"
  184. : ''
  185. ),
  186. 'rows' => array_map(function ($item) use ($idStorage, $thisGetLink) {
  187. $typeName = $item['typeName']; // $typeName = Api_WfsNs::typeName($item['namespace']);
  188. return [
  189. 'namespace' => '<span style="color:#888">' . substr($item['namespace'], 0, strlen($item['namespace']) - strlen($item['name'])) . '</span>' .
  190. '<span>' . $item['name'] . '</span>',
  191. '_type' => $item['_type'], // TODO: editable?
  192. 'Nr zasobu' => ($item['idZasob'] > 0)
  193. ? $item['idZasob']
  194. : UI::hButtonAjax("Dodaj do zasobów", 'addAclObjectToZasobyAjax', [
  195. 'class' => "btn btn-xs btn-primary",
  196. 'href' => $this->getLink('addAclObjectToZasobyAjax'),
  197. 'data' => [
  198. 'idStorage' => $idStorage,
  199. 'namespace' => $item['namespace'],
  200. ]
  201. ]),
  202. // 'opis' => $item['description'],
  203. 'hasStruct' => ($item['hasStruct']) ? '<span class="label label-success">TAK</span>' : '<span class="text text-muted">nie</span>',
  204. 'installed' => $item['isStructInstalled'] ? '<span class="label label-success">TAK</span>' : '<span class="text text-muted">nie</span>',
  205. 'active?' => $item['isObjectActive'] ? '<span class="label label-success">TAK</span>' : '<span class="text text-muted">nie</span>',
  206. 'menu' => UI::h('div', ['class'=>"p5UI__dropdown-wrap"], [
  207. UI::h('button', ['onClick' => "p5UI__dropdown(event, this, 'left bottom')", 'class' => "btn btn-xs btn-default p5UI__dropdown-btn"], [
  208. '<i class="glyphicon glyphicon-menu-hamburger"></i>',
  209. " menu"
  210. ]),
  211. UI::h('div', ['class' => "p5UI__dropdown-content"], [
  212. UI::h('input', ['type' => "text", 'placeholder' => "Search..", 'class' => "p5UI__dropdown-input", 'onkeyup' => "p5_Storage_actions_filterInput(this)"], null),
  213. UI::h('a', [ 'href' => Router::getRoute('Storage_AclStruct')->getLink('', [ 'namespace' => $item['namespace'] ]) ], "struktura"),
  214. UI::h('a', [ 'href' => $item['reinstallLink'] ], "reinstall"),
  215. UI::h('a', [ 'href' => $this->getLink('rawInfo', [ 'idStorage' => $idStorage, 'table' => $item['name'] ]) ], "raw info"),
  216. UI::h('a', [ 'href' => Router::getRoute('ViewTableAjax')->getLink('', ['namespace' => $item['namespace']]) ], "view table"),
  217. UI::h('a', [ 'href' => Router::getRoute('ViewObject')->getLink('', ['namespace' => $item['namespace']]) ], "view object"),
  218. // 'xsd' => UI::h('a', [ 'href' => Router::getRoute('Storage_TestXsd')->getLink('', [ 'idStorage' => $idStorage ]) ], "xsd"),
  219. UI::h('a', [ 'href' => "wfs-data.php/default_db/?SERVICE=WFS&VERSION=1.0.0&SRSNAME=EPSG:3003&REQUEST=DescribeFeatureType&TYPENAME={$typeName}" ], "wfs DescribeFeatureType"),
  220. UI::h('a', [ 'href' => "wfs-data.php/default_db/?SERVICE=WFS&VERSION=1.0.0&SRSNAME=EPSG:3003&REQUEST=DescribeFeatureTypeAdvanced&TYPENAME={$typeName}" ], "wfs DescribeFeatureTypeAdvanced"),
  221. UI::h('a', [ 'href' => "wfs-data.php/default_db/?SERVICE=WFS&VERSION=1.0.0&SRSNAME=EPSG:3003&REQUEST=GetFeature&TYPENAME={$typeName}&MAXFEATURES=10" ], "wfs GetFeature (max: 10)"),
  222. UI::h('a', [ 'href' => "wfs-data.php/default_db/?SERVICE=WFS&VERSION=1.0.0&SRSNAME=EPSG:3003&REQUEST=GetFeatureAdvanced&TYPENAME={$typeName}&MAXFEATURES=10" ], "wfs GetFeatureAdvanced (max: 10)"),
  223. UI::h('a', [ 'href' => "wfs-data.php/default_db/?SERVICE=WFS&VERSION=1.0.0&SRSNAME=EPSG:3003&REQUEST=GetFeature&TYPENAME={$typeName}&MAXFEATURES=3&resolve=all&resolveDepth=3" ], "wfs GetFeature (max: 3, resolveDepth: 3)"),
  224. UI::h('a', [ 'href' => "index.php?_route=WfsJsRequestPanel&namespace={$item['namespace']}" ], "JavaScript WFS Panel"),
  225. ])
  226. ]),
  227. ];
  228. }, $objectStorage->getItems([
  229. '#refFrom' => [
  230. 'namespace' => 'default_objects/SystemSource',
  231. 'primaryKey' => $sourceItem['idZasob']
  232. ],
  233. 'order_by' => 'namespace',
  234. 'order_dir' => 'asc'
  235. ]))
  236. ]);
  237. UI::hButtonAjaxOnResponse('addAclObjectToZasobyAjax', /* payload, n */ "
  238. if (!payload.type) return false
  239. if ('success' === payload.type || 'info' === payload.type) {
  240. if (payload.body && payload.body.id && payload.body.id > 0) {
  241. n.parentNode.replaceChild(document.createTextNode(payload.body.id), n)
  242. } else {
  243. console.log('TODO: addAclObjectToZasobyAjax unknown response', payload);
  244. }
  245. jQuery.notify(payload.msg, payload.type);
  246. }
  247. ");
  248. } catch (Exception $e) {
  249. UI::alert('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage());
  250. DBG::log($e);
  251. }
  252. UI::endContainer();
  253. UI::dol();
  254. }
  255. public function rawInfoAction() {
  256. UI::gora();
  257. UI::menu();
  258. $this->navView();
  259. try {
  260. $idStorage = V::get('idStorage', 0, $_REQUEST, 'int');
  261. if (empty($idStorage)) throw new Exception("Missing id storage");
  262. $storageList = $this->getStorageList();
  263. if (empty($storageList)) throw new Exception("No storage defined");
  264. if (!array_key_exists($idStorage, $storageList)) throw new Exception("Storage not exists");
  265. $storagePdo = DB::getStorage($idStorage);
  266. $rawInfo = $storagePdo->getTableListWithInfo();
  267. DBG::table("rawInfo", $rawInfo, __CLASS__, __FUNCTION__, __LINE__);
  268. } catch (Exception $e) {
  269. UI::alert('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage());
  270. }
  271. UI::dol();
  272. }
  273. public function fetchActionListAjaxAction() {
  274. Response::sendTryCatchJson(array($this, 'fetchActionListAjax'), $args = 'JSON_FROM_REQUEST_BODY');
  275. }
  276. public function fetchActionListAjax($args) {
  277. return [
  278. 'type' => 'success',
  279. '__args' => $args,
  280. 'options' => DB::getPDO()->fetchAll("
  281. select z.ID, z.`DESC`, z.OPIS
  282. from CRM_LISTA_ZASOBOW z
  283. where z.`TYPE` = 'URL_ACTION'
  284. and z.A_STATUS != 'DELETED'
  285. and z.ALIAS_ID = 0
  286. ")
  287. ];
  288. }
  289. public function addActionAjaxAction() {
  290. Response::sendTryCatchJson(array($this, 'addActionAjax'), $args = 'JSON_FROM_REQUEST_BODY');
  291. }
  292. public function addActionAjax($args) {
  293. $idStorage = V::get('idStorage', 0, $args, 'int');
  294. if ($idStorage <= 0) throw new Exception("Missing id storage");
  295. $idAction = V::get('idAction', 0, $args, 'int');
  296. if ($idAction <= 0) throw new Exception("Missing id action");
  297. $storageList = $this->getStorageList();
  298. if (empty($storageList)) throw new Exception("No storage defined");
  299. if (!array_key_exists($idStorage, $storageList)) throw new Exception("Storage not exists");
  300. $tblName = V::get('table', '', $args, 'word');
  301. if (empty($tblName)) throw new Exception("No table name");
  302. $storagePdo = DB::getStorage($idStorage);
  303. // $tblStruct = $storagePdo->getTableStruct($tblName);
  304. $idTable = $this->fetchTableId($idStorage, $tblName);
  305. if ($idTable <= 0) throw new Exception("Zasob tabela '{$tblName}' nie istnieje");
  306. $action = DB::getPDO()->fetchFirst("
  307. select z.ID, z.`DESC`, z.OPIS
  308. from CRM_LISTA_ZASOBOW z
  309. where z.ID = {$idAction}
  310. ");
  311. if (empty($action)) throw new Exception("Action '{$idAction}' not exists");
  312. $idInsertedAction = DB::getPDO()->insert('CRM_LISTA_ZASOBOW', [
  313. 'PARENT_ID' => $idTable,
  314. 'ALIAS_ID' => $idAction,
  315. 'TYPE' => 'URL_ACTION',
  316. 'DESC' => $action['DESC'],
  317. 'OPIS' => V::get('OPIS', $action['DESC'], $action),
  318. ]);
  319. if (!$idInsertedAction) throw new Exception("Nie udało się dodać akcji");
  320. try {
  321. DB::getPDO()->insert('CRM_LISTA_ZASOBOW_HIST', [
  322. 'ID_USERS2' => $idInsertedAction,
  323. 'PARENT_ID' => $idTable,
  324. 'ALIAS_ID' => $idAction,
  325. 'TYPE' => 'URL_ACTION',
  326. 'DESC' => $action['DESC'],
  327. 'OPIS' => V::get('OPIS', $action['DESC'], $action),
  328. ]);
  329. } catch (Exception $e) {
  330. DBG::log($e);
  331. }
  332. return [
  333. 'type' => 'success',
  334. 'msg' => "Dodano akcję - rekord nr {$idInsertedAction}",
  335. '__DBG__' => [
  336. '$args' => $args,
  337. '$idStorage' => $idStorage,
  338. '$tblName' => $tblName,
  339. // '$tblStruct' => $tblStruct,
  340. '$idTable' => $idTable,
  341. '$action' => $action,
  342. ]
  343. ];
  344. }
  345. public function addTableBaseProcesAction() {
  346. Response::sendTryCatchJson(array($this, 'addTableBaseProces'), $_REQUEST);
  347. }
  348. public function addTableBaseProces($args) {
  349. $return = [
  350. 'type' => 'error',
  351. 'msg' => 'todo: F.' . __FUNCTION__ . ' L.' . __LINE__,
  352. ];
  353. $idStorage = V::get('idStorage', 0, $args, 'int');
  354. if ($idStorage <= 0) throw new HttpException("Missing idStorage", 400);
  355. $storage = DB::getStorage($idStorage);
  356. $tblName = V::get('tblName', 0, $args, 'word');
  357. if (empty($tblName)) throw new HttpException("Missing tblName", 400);
  358. $tableStruct = $storage->getTableStruct($tblName);
  359. $return['$tableStruct'] = $tableStruct;
  360. $idTable = $this->fetchTableId($idStorage, $tblName);
  361. if ($idTable <= 0) {
  362. UI::alert('warning', "Zasob tabela '{$tblName}' nie istnieje");// TODO: add p5UI btn
  363. DBG::table("tblStruct", $tblStruct, __CLASS__, __FUNCTION__, __LINE__);
  364. throw new Exception("Zasob tabela '{$tblName}' nie istnieje");
  365. }
  366. $cellZasobList = array();
  367. foreach (DB::getPDO()->fetchAllByKey("
  368. select z.ID, z.`DESC`, z.A_STATUS
  369. from CRM_LISTA_ZASOBOW z
  370. where z.PARENT_ID = '{$idTable}'
  371. ", $key = 'DESC') as $ind => $row) {
  372. $cellZasobList[strtolower($ind)] = $row;
  373. }
  374. $return['$cellZasobList'] = $cellZasobList;
  375. $idProces = DB::getDB()->ADD_NEW_OBJ('CRM_PROCES', (object)[
  376. 'TYPE' => 'PROCES_INIT',
  377. 'DESC' => "Proces dla tabeli '{$tblName}'",
  378. ]);
  379. if (!$idProces) throw new Exception("DB ERROR - nie udało się dodać procesu");
  380. foreach ($cellZasobList as $loverName => $row) {
  381. DB::getDB()->ADD_NEW_OBJ('CRM_WSKAZNIK', (object)[
  382. 'ID_PROCES' => $idProces,
  383. 'ID_ZASOB' => $row['ID'],
  384. 'TYP' => 'P',
  385. 'ID_PRZYPADEK' => 2
  386. ]);
  387. }
  388. $return['type'] = 'success';
  389. $return['msg'] = "Utworzono proces {{$idProces}}";
  390. return $return;
  391. }
  392. public function addObjectBaseProcesAjaxAction() {
  393. Response::sendTryCatchJson(array($this, 'addObjectBaseProcesAjax'), $_REQUEST);
  394. }
  395. public function addObjectBaseProcesAjax($args) {
  396. $return = [
  397. 'type' => 'error',
  398. 'msg' => 'todo: F.' . __FUNCTION__ . ' L.' . __LINE__,
  399. ];
  400. $namespace = V::get('namespace', 0, $args);
  401. if (empty($namespace)) throw new HttpException("Missing namespace", 400);
  402. $item = SchemaFactory::loadDefaultObject('SystemObject')->getItem($namespace, [ 'propertyName' => '*,field' ]);
  403. DBG::log($item, 'array', "TODO: addObjectBaseProcesAjax for \$item");
  404. if (!$item['idZasob']) throw new Exception("Missing id zasob for object '{$namespace}'");
  405. if (!$item['idDatabase']) throw new Exception("Missing id database for object '{$namespace}'");
  406. if (!$item['_rootTableName']) throw new Exception("Missing root table name for object '{$namespace}'");
  407. if ('AntAcl' != $item['_type']) throw new Exception("Not implemented type '{$item['_type']}' for namespace '{$namespace}' - only AntAcl supported");
  408. if (!$item['hasStruct']) throw new Exception("Missing structure for object '{$namespace}'");
  409. if (!$item['isStructInstalled']) throw new Exception("Structure not installed for object '{$namespace}'");
  410. if (!$item['isObjectActive']) throw new Exception("Object is not active '{$namespace}'");
  411. $fieldsWithIdZasob = array_filter($item['field'], function ($field) {
  412. if (!$field['idZasob']) return false;
  413. return true;
  414. });
  415. if (empty($fieldsWithIdZasob)) throw new Exception("Missing fields with id zasob in object '{$namespace}'");
  416. $idProces = DB::getDB()->ADD_NEW_OBJ('CRM_PROCES', (object)[
  417. 'TYPE' => 'PROCES_INIT',
  418. 'DESC' => "TODO: Proces dla obiektu '{$namespace}'",
  419. ]);
  420. if (!$idProces) throw new Exception("DB ERROR - nie udało się dodać procesu");
  421. array_map(function ($field) use ($idProces) {
  422. DB::getDB()->ADD_NEW_OBJ('CRM_WSKAZNIK', (object)[
  423. 'ID_PROCES' => $idProces,
  424. 'ID_ZASOB' => $field['idZasob'],
  425. 'TYP' => 'P',
  426. 'ID_PRZYPADEK' => 2
  427. ]);
  428. }, $fieldsWithIdZasob);
  429. $return['type'] = 'success';
  430. $return['msg'] = "Utworzono proces {{$idProces}}";
  431. return $return;
  432. }
  433. public function addGeomEtykietaCellsAction() {
  434. Response::sendTryCatchJson(array($this, 'addGeomEtykietaCells'), $_REQUEST);
  435. }
  436. public function addGeomEtykietaCells($args) {
  437. $return = [
  438. 'type' => 'error',
  439. 'msg' => 'todo: F.' . __FUNCTION__ . ' L.' . __LINE__,
  440. ];
  441. $return['_DBG_request'] = $args;
  442. // idStorage: "36"
  443. // tblName: "test_geom_linestring"
  444. $idStorage = V::get('idStorage', 0, $args, 'int');
  445. if ($idStorage <= 0) throw new HttpException("Missing idStorage", 400);
  446. $storage = DB::getStorage($idStorage);
  447. $tblName = V::get('tblName', 0, $args, 'word');
  448. if (empty($tblName)) throw new HttpException("Missing tblName", 400);
  449. $tableStruct = $storage->getTableStruct($tblName);
  450. $return['_DBG_$tableStruct'] = $tableStruct;
  451. if ('mysql' == DB::getPDO($idStorage)->getType()) {
  452. $dbName = DB::getPDO($idStorage)->getDatabaseName();
  453. $fixedTableName = DB::getPDO($idStorage)->fetchValue("
  454. select t.TABLE_NAME
  455. from `information_schema`.`TABLES` t
  456. where t.TABLE_SCHEMA = '{$dbName}'
  457. and t.TABLE_NAME LIKE '{$tblName}'
  458. ");
  459. $return['_DBG_sql_fix__$tblName'] = "
  460. select t.TABLE_NAME
  461. from `information_schema`.`TABLES` t
  462. where t.TABLE_SCHEMA = '{$dbName}'
  463. and t.TABLE_NAME LIKE '{$tblName}'
  464. ";
  465. if (empty($fixedTableName)) return $return;
  466. if (empty($fixedTableName)) throw new HttpException("Database Error", 500);
  467. $return['_DBG_$fixedTableName'] = $fixedTableName;
  468. $tblName = $fixedTableName;
  469. }
  470. $return['_DBG_$tableStruct'] = $tableStruct;
  471. $return['_DBG_has_field__etykieta_x'] = (null != V::geti('etykieta_x', null, $tableStruct));
  472. $return['_DBG_has_field__etykieta_y'] = (null != V::geti('etykieta_y', null, $tableStruct));
  473. $return['_DBG_has_field__etykieta_obrot'] = (null != V::geti('etykieta_obrot', null, $tableStruct));
  474. if (!V::geti('etykieta_x', null, $tableStruct)) {
  475. try {
  476. DB::getPDO($idStorage)->exec("ALTER TABLE `{$tblName}_HIST` ADD `etykieta_x` varchar(16) DEFAULT 'N/S;'");
  477. } catch (Exception $e) {
  478. $return['__DBG_hist_errors__etykieta_x'] = $e->getMessage();
  479. }
  480. DB::getPDO($idStorage)->exec("ALTER TABLE `{$tblName}` ADD `etykieta_x` decimal(16,10) DEFAULT NULL COMMENT 'przesuniecie etykiety elementu w GIS'");
  481. }
  482. if (!V::geti('etykieta_y', null, $tableStruct)) {
  483. try {
  484. DB::getPDO($idStorage)->exec("ALTER TABLE `{$tblName}_HIST` ADD `etykieta_y` varchar(16) DEFAULT 'N/S;'");
  485. } catch (Exception $e) {
  486. $return['__DBG_hist_errors__etykieta_y'] = $e->getMessage();
  487. }
  488. DB::getPDO($idStorage)->exec("ALTER TABLE `{$tblName}` ADD `etykieta_y` decimal(16,10) DEFAULT NULL COMMENT 'przesuniecie etykiety elementu w GIS'");
  489. }
  490. if (!V::geti('etykieta_obrot', null, $tableStruct)) {
  491. try {
  492. DB::getPDO($idStorage)->exec("ALTER TABLE `{$tblName}_HIST` ADD `etykieta_obrot` varchar(16) DEFAULT 'N/S;'");
  493. } catch (Exception $e) {
  494. $return['__DBG_hist_errors__etykieta_obrot'] = $e->getMessage();
  495. }
  496. DB::getPDO($idStorage)->exec("ALTER TABLE `{$tblName}` ADD `etykieta_obrot` decimal(16,10) DEFAULT NULL");
  497. }
  498. return $return;
  499. }
  500. public function navView() {
  501. $backLabel = 'back';
  502. $backLink = 'index.php?_route=Storage';
  503. $backDisabled = true;
  504. $currentLabel = 'Storage';
  505. $currentLink = 'index.php?_route=Storage';
  506. if ($task = V::get('_task', '', $_REQUEST)) {
  507. $currentLink = "index.php?_route=Storage&_task={$task}";
  508. $backDisabled = false;
  509. $idStorage = V::get('idStorage', 0, $_REQUEST, 'int');
  510. $tblName = V::get('table', '', $_REQUEST, 'word');
  511. $objName = V::get('object', '', $_REQUEST, 'word');
  512. $namespace = V::get('namespace', '', $_REQUEST, 'word');
  513. switch ($task) {
  514. case 'obejctList':
  515. $backLabel = 'Storage';
  516. $backLink = $this->getLink();
  517. $currentLink = $this->getLink('obejctList', ['idStorage' => $idStorage]);
  518. break;
  519. case 'tableList':
  520. $backLabel = 'Storage';
  521. $backLink = $this->getLink();
  522. $currentLink = $this->getLink('tableList', ['idStorage' => $idStorage]);
  523. break;
  524. case 'viewList':
  525. $backLabel = 'Storage';
  526. $backLink = $this->getLink();
  527. $currentLink = $this->getLink('viewList', ['idStorage' => $idStorage]);
  528. break;
  529. case 'rawInfo':
  530. $backLabel = 'Storage';
  531. $backLink = $this->getLink();
  532. $currentLink = $this->getLink('rawInfo', ['idStorage' => $idStorage]);
  533. break;
  534. case 'tableStruct':
  535. $backLabel = "Tabele [{$idStorage}]";
  536. $backLink = $this->getLink('tableList', ['idStorage' => $idStorage]);
  537. $currentLink = $this->getLink('tableStruct', ['idStorage' => $idStorage, 'table' => $tblName]);
  538. break;
  539. case 'objectStruct':
  540. $backLabel = "Obiekty [{$idStorage}]";
  541. $backLink = $this->getLink('tableList', ['idStorage' => $idStorage]);// TODO: mv tableList to objectList
  542. $currentLink = $this->getLink('objectStruct', ['idStorage' => $idStorage, 'namespace' => $namespace]);
  543. break;
  544. case 'coreObjectStruct':
  545. $backLabel = "Obiekty podstawowe";
  546. $backLink = Router::getRoute('Storage_TestObj')->getLink('coreObjectList', ['idStorage' => $idStorage]);
  547. $currentLink = $this->getLink('coreObjectStruct', ['idStorage' => $idStorage, 'object' => $objName]);
  548. break;
  549. }
  550. switch ($task) {
  551. case 'tableList': $currentLabel = "Tabele [{$idStorage}]"; break;
  552. case 'viewList': $currentLabel = "Widoki [{$idStorage}]"; break;
  553. case 'rawInfo': $currentLabel = "Raw info [{$idStorage}]"; break;
  554. case 'tableStruct': $currentLabel = "Struktura tabeli '{$tblName}'"; break;
  555. case 'objectStruct': $currentLabel = "Obiekt '{$namespace}'"; break;
  556. case 'coreObjectList': $currentLabel = "Obiekty podstawowe"; break;
  557. case 'coreObjectStruct': $currentLabel = "Obiekt '{$objName}'"; break;
  558. case 'objectList': $currentLabel = "Obiekty z aktualnej domeny"; break;// TODO: domain from $_GET
  559. }
  560. }
  561. echo UI::h('nav', ['class'=>"navbar navbar-default navbar-static-top", 'style'=>"z-index:999"], [
  562. UI::h('div', ['class'=>"container-fluid"], [
  563. UI::h('div', ['class'=>"navbar-left"], [
  564. UI::h('ul', ['class'=>"nav navbar-nav navbar-center"], [
  565. UI::h('li', [], [
  566. UI::h('a', ['href'=>$backLink, 'class'=>"btn" . ($backDisabled ? ' disabled' : '')], '<i class="glyphicon glyphicon-chevron-left"></i> ' . $backLabel)
  567. ])
  568. ])
  569. ]),
  570. UI::h('div', ['class'=>"navbar-left"], [
  571. UI::h('ul', ['class'=>"nav navbar-nav navbar-center"], [
  572. UI::h('li', [], [
  573. UI::h('a', ['href'=>$currentLink, 'class'=>"btn"], $currentLabel)
  574. ])
  575. ])
  576. ]),
  577. UI::h('div', ['class'=>"navbar-right"], [
  578. // <ul class="nav navbar-nav navbar-right">
  579. // <li><a href="#">Link</a></li>
  580. // <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>
  581. // <ul class="dropdown-menu">
  582. // <li><a href="#">Action</a></li>
  583. // <li><a href="#">Another action</a></li>
  584. // <li><a href="#">Something else here</a></li>
  585. // <li role="separator" class="divider"></li>
  586. // <li><a href="#">Separated link</a></li>
  587. // </ul>
  588. // </li>
  589. // </ul>
  590. ])
  591. ])
  592. ]);
  593. }
  594. public function getStorageList() {
  595. $storageList = array();
  596. $sth = DB::getPDO()->prepare("
  597. select z.ID, z.`DESC`, z.`TYPE`
  598. from CRM_LISTA_ZASOBOW z
  599. where z.TYPE in('BAZA_DANYCH','DATABASE_MYSQL','DATABASE_POSTGRESQL')
  600. ");
  601. $sth->execute();
  602. $rows = $sth->fetchAll();
  603. foreach ($rows as $row) {
  604. $storageList[$row['ID']] = $row;
  605. }
  606. return $storageList;
  607. }
  608. public function addAclObjectToZasobyAction() {// sends JSON
  609. $response = new stdClass();
  610. try {
  611. $idStorage = V::get('idStorage', '', $_GET);
  612. $namespace = V::get('namespace', '', $_GET);
  613. if (empty($idStorage)) throw new HttpException("Missing idStorage param");
  614. if (empty($namespace)) throw new HttpException("Missing namespace param");
  615. $objectStorage = SchemaFactory::loadDefaultObject('SystemObject');
  616. $items = $objectStorage->getItems([
  617. 'f_namespace' => "={$namespace}",
  618. ]);
  619. if (empty($items)) throw new Exception("SystemObject '{$namespace}' not found");
  620. $objectItem = reset($items);
  621. if (empty($objectItem)) throw new Exception("SystemObject '{$namespace}' not found");
  622. DBG::log($objectItem, 'array', 'object acl $objectItem');
  623. if ($objectItem['idZasob'] > 0) {
  624. // TODO: check if realy exists? @see SchemaFactory::loadDefaultObject('SystemObject')::updateCache()
  625. $response->_replaceButtonNode = "[{$objectItem['idZasob']}]";
  626. throw new AlertInfoException("Zasob '{$objectItem['namespace']}' już istnieje - nr '{$objectItem['idZasob']}'");
  627. }
  628. $idZasobFound = 0;
  629. switch ($objectItem['_type']) {
  630. case 'TableAcl': $idZasobFound = DB::getPDO()->fetchValue(" select ID from CRM_LISTA_ZASOBOW where PARENT_ID = {$objectItem['idDatabase']} and `DESC` = '{$objectItem['name']}' "); break;
  631. case 'AntAcl': $idZasobFound = DB::getPDO()->fetchValue(" select ID from CRM_LISTA_ZASOBOW where PARENT_ID = {$objectItem['idDatabase']} and `DESC` = '{$objectItem['namespace']}' "); break;
  632. default: throw new Exception("Not implemented acl type '{$objectItem['_type']}'");
  633. }
  634. if ($idZasobFound > 0) {
  635. DB::getPDO()->update($objectStorage->_rootTableName, 'namespace', $objectItem['namespace'], [
  636. 'idZasob' => $idZasobFound
  637. ]);
  638. $response->id = $idZasobFound;
  639. throw new AlertSuccessException("Zasob '{$objectItem['namespace']}' już istnieje - nr '{$idZasobFound}' - cache zaktualizowany");
  640. }
  641. try {
  642. $acl = User::getAcl()->getObjectAcl('default_db', 'crm_lista_zasobow');
  643. } catch (Exception $e) {
  644. DBG::log($e);
  645. throw new Exception("Brak dostępu do tabeli Zasoby");
  646. }
  647. if (empty($objectItem['idDatabase'])) throw new Exception("Missing database id");
  648. if ($idStorage != $objectItem['idDatabase']) throw new Exception("Database id must be the same");
  649. if (empty($objectItem['_rootTableName'])) throw new Exception("Missing root table name");
  650. switch ($objectItem['_type']) {
  651. case 'TableAcl':
  652. $newZasobItem = [
  653. 'PARENT_ID' => $objectItem['idDatabase'],
  654. 'TYPE' => 'TABELA',
  655. 'DESC' => $objectItem['_rootTableName'],
  656. 'DESC_PL' => $objectItem['name'],
  657. ];
  658. break;
  659. case 'AntAcl':
  660. $newZasobItem = [
  661. 'PARENT_ID' => $objectItem['idDatabase'],
  662. 'TYPE' => 'TABELA',
  663. 'DESC' => $objectItem['namespace'],
  664. 'DESC_PL' => $objectItem['name'],
  665. ];
  666. break;
  667. default: throw new Exception("Not implemented acl type '{$objectItem['_type']}'");
  668. }
  669. $idCreated = $acl->addItem($newZasobItem);
  670. if (!$idCreated) throw new Exception("Nie udało się utworzyć nowego rekordu!");
  671. try {
  672. DB::getPDO()->update($objectStorage->_rootTableName, 'namespace', $objectItem['namespace'], [
  673. 'idZasob' => $idCreated
  674. ]);
  675. } catch (Exception $e) {
  676. DBG::log($e);
  677. }
  678. $response->id = $idCreated;
  679. $response->record = $acl->getItem($idCreated);
  680. $response->_replaceButtonNode = "[{$idCreated}]";
  681. throw new AlertSuccessException("Utworzono pomyślnie rekord nr {$idCreated}");
  682. } catch (AlertSuccessException $e) {
  683. $response->type = 'success';
  684. $response->msg = $e->getMessage();
  685. } catch (AlertInfoException $e) {
  686. $response->type = 'info';
  687. $response->msg = $e->getMessage();
  688. } catch (Exception $e) {
  689. $response->type = 'error';
  690. $response->msg = $e->getMessage();
  691. DBG::log($e);
  692. }
  693. Response::sendJsonExit($response);
  694. }
  695. public function addAclObjectToZasobyAjaxAction() {
  696. DBG::log($_REQUEST, 'array', '$_REQUEST');
  697. Response::sendTryCatchJson(array($this, 'addAclObjectToZasobyAjax'), $_REQUEST);
  698. }
  699. public function addAclObjectToZasobyAjax($args) {
  700. $namespace = V::get('namespace', '', $args);
  701. if (empty($namespace)) throw new HttpException("Missing namespace");
  702. $idStorage = V::get('idStorage', '', $args);
  703. if (empty($idStorage)) throw new HttpException("Missing idStorage");
  704. $objectStorage = SchemaFactory::loadDefaultObject('SystemObject');
  705. $items = $objectStorage->getItems([
  706. 'f_namespace' => "={$namespace}",
  707. ]);
  708. if (empty($items)) throw new Exception("SystemObject '{$namespace}' not found");
  709. $objectItem = reset($items);
  710. if (empty($objectItem)) throw new Exception("SystemObject '{$namespace}' not found");
  711. DBG::log($objectItem, 'array', 'object acl $objectItem');
  712. try {
  713. $acl = User::getAcl()->getObjectAcl('default_db', 'crm_lista_zasobow');
  714. } catch (Exception $e) {
  715. DBG::log($e);
  716. throw new Exception("Brak dostępu do tabeli Zasoby");
  717. }
  718. if ($objectItem['idZasob'] > 0) {
  719. // TODO: check if realy exists? @see SchemaFactory::loadDefaultObject('SystemObject')::updateCache()
  720. return [
  721. 'type' => 'info',
  722. 'msg' => "Zasob '{$objectItem['namespace']}' już istnieje - nr '{$objectItem['idZasob']}'",
  723. 'body' => [
  724. 'id' => $objectItem['idZasob'],
  725. 'record' => $acl->getItem($objectItem['idZasob']),
  726. '_replaceButtonNode' => "[{$objectItem['idZasob']}]",
  727. ]
  728. ];
  729. }
  730. $idZasobFound = 0;
  731. if ('StorageAcl' === $objectItem['_type']) {
  732. if (!$objectItem['idDatabase']) throw new Exception("Brak idDatabase dla '{$objectItem['namespace']}'");
  733. }
  734. switch ($objectItem['_type']) {
  735. case 'TableAcl': $idZasobFound = DB::getPDO()->fetchValue(" select ID from CRM_LISTA_ZASOBOW where PARENT_ID = {$objectItem['idDatabase']} and `DESC` = '{$objectItem['name']}' "); break;
  736. case 'AntAcl': $idZasobFound = DB::getPDO()->fetchValue(" select ID from CRM_LISTA_ZASOBOW where PARENT_ID = {$objectItem['idDatabase']} and `DESC` = '{$objectItem['namespace']}' "); break;
  737. case 'StorageAcl': $idZasobFound = DB::getPDO()->fetchValue(" select ID from CRM_LISTA_ZASOBOW where PARENT_ID = {$objectItem['idDatabase']} and `DESC` = '{$objectItem['namespace']}' "); break;
  738. default: throw new Exception("Not implemented acl type '{$objectItem['_type']}'");
  739. }
  740. if ($idZasobFound > 0) {
  741. DB::getPDO()->update($objectStorage->_rootTableName, 'namespace', $objectItem['namespace'], [
  742. 'idZasob' => $idZasobFound
  743. ]);
  744. return [
  745. 'type' => 'info',
  746. 'msg' => "Zasob '{$objectItem['namespace']}' już istnieje - nr '{$idZasobFound}' - cache zaktualizowany",
  747. 'body' => [
  748. 'id' => $idZasobFound,
  749. 'record' => $acl->getItem($idZasobFound),
  750. '_replaceButtonNode' => "[{$idZasobFound}]",
  751. ]
  752. ];
  753. }
  754. if (empty($objectItem['idDatabase'])) throw new Exception("Missing database id");
  755. if ($idStorage != $objectItem['idDatabase']) throw new Exception("Database id must be the same");
  756. if (empty($objectItem['_rootTableName'])) throw new Exception("Missing root table name");
  757. switch ($objectItem['_type']) {
  758. case 'TableAcl':
  759. $newZasobItem = [
  760. 'PARENT_ID' => $objectItem['idDatabase'],
  761. 'TYPE' => 'TABELA',
  762. 'DESC' => $objectItem['_rootTableName'],
  763. 'DESC_PL' => $objectItem['name'],
  764. ];
  765. break;
  766. case 'AntAcl':
  767. $newZasobItem = [
  768. 'PARENT_ID' => $objectItem['idDatabase'],
  769. 'TYPE' => 'TABELA',
  770. 'DESC' => $objectItem['namespace'],
  771. 'DESC_PL' => $objectItem['name'],
  772. ];
  773. break;
  774. case 'StorageAcl':
  775. $newZasobItem = [
  776. 'PARENT_ID' => $objectItem['idDatabase'],
  777. 'TYPE' => 'TABELA',
  778. 'DESC' => $objectItem['namespace'],
  779. 'DESC_PL' => $objectItem['name'],
  780. ];
  781. break;
  782. default: throw new Exception("Not implemented acl type '{$objectItem['_type']}'");
  783. }
  784. $idCreated = $acl->addItem($newZasobItem);
  785. if (!$idCreated) throw new Exception("Nie udało się utworzyć nowego rekordu!");
  786. try {
  787. DB::getPDO()->update($objectStorage->_rootTableName, 'namespace', $objectItem['namespace'], [
  788. 'idZasob' => $idCreated
  789. ]);
  790. } catch (Exception $e) {
  791. DBG::log($e);
  792. }
  793. return [
  794. 'type' => 'success',
  795. 'msg' => "Utworzono pomyślnie rekord nr {$idCreated}",
  796. 'body' => [
  797. 'id' => $idCreated,
  798. 'record' => $acl->getItem($idCreated),
  799. '_replaceButtonNode' => "[{$idCreated}]",
  800. ]
  801. ];
  802. }
  803. public function addCellToZasobyAction() {// sends JSON
  804. $response = new stdClass();
  805. try {
  806. $idStorage = V::get('storageId', '', $_GET);
  807. $tblName = V::get('tblName', '', $_GET, 'word');
  808. $cellName = V::get('cellName', '', $_GET, 'word');
  809. if (empty($tblName)) throw new HttpException("Wrong table name");
  810. if (empty($tblName)) throw new HttpException("Wrong cell name");
  811. $storage = DB::getStorage($idStorage);
  812. $tableStruct = $storage->getTableStruct($tblName);
  813. $zasobStorageId = $storage->getZasobId();
  814. if (!is_numeric($zasobStorageId)) throw new HttpException("Storage id is not set in config file");
  815. $idTable = $this->fetchTableId($zasobStorageId, $tblName);
  816. if ($idTable <= 0) throw new Exception("Zasob tabela '{$tblName}' nie istnieje");
  817. try {
  818. $acl = User::getAcl()->getObjectAcl('default_db', 'crm_lista_zasobow');
  819. } catch (Exception $e) {
  820. throw new Exception("Brak dostępu do tabeli Zasoby");
  821. }
  822. $item = array();
  823. $item['PARENT_ID'] = $idTable;
  824. $item['TYPE'] = 'KOMORKA';
  825. $item['DESC'] = $cellName;
  826. $item['DESC_PL'] = $cellName;
  827. if (DBG::isActive()) $response->_itemToCreate = $item;
  828. $createdId = $acl->addItem($item);
  829. if (!$createdId) throw new Exception("Nie udało się utworzyć nowego rekordu!");
  830. $response->id = $createdId;
  831. $response->record = $acl->getItem($createdId);
  832. $response->_replaceButtonNode = "[{$createdId}]";
  833. throw new AlertSuccessException("Utworzono pomyślnie rekord nr {$createdId}");
  834. } catch (AlertSuccessException $e) {
  835. DBG::log($e);
  836. $response->type = 'success';
  837. $response->msg = $e->getMessage();
  838. } catch (AlertInfoException $e) {
  839. DBG::log($e);
  840. $response->type = 'info';
  841. $response->msg = $e->getMessage();
  842. } catch (Exception $e) {
  843. DBG::log($e);
  844. $response->type = 'error';
  845. $response->msg = $e->getMessage();
  846. }
  847. Response::sendJsonExit($response);
  848. }
  849. public function fetchTableId($idZasobStorage, $tblName) {
  850. $rows = DB::getPDO()->fetchAll("
  851. select z.`ID`, z.`DESC`
  852. from `CRM_LISTA_ZASOBOW` z
  853. where z.`PARENT_ID`='{$idZasobStorage}'
  854. and z.`DESC`='{$tblName}'
  855. and z.`A_STATUS` in('NORMAL','WAITING')
  856. ");
  857. if (!empty($rows)) return (int)$rows[0]['ID'];
  858. return null;
  859. }
  860. public function systemObjectsStructAction() {
  861. UI::gora();
  862. UI::menu();
  863. $this->navView();
  864. try {
  865. throw new Exception("TODO: F." . __FUNCTION__ . ' L.' . __LINE__);
  866. // $coreObjlist = OBJXSD::getSystemObjectsStruct();
  867. // $objectList = array();
  868. // foreach ($coreObjlist as $objName) {
  869. // $objItem = array();
  870. // $objItem['name'] = $objName;
  871. // $objItem['struktura'] = '<a href="index.php?_route=Storage&_task=systemObjectsStruct&object=' . $objName . '">' . "struct" . '</a>';
  872. // // $objItem['label'] = "";// TODO: read from json
  873. // $objectList[] = $objItem;
  874. // }
  875. // usort($objectList, function($rowA, $rowB) {
  876. // $a = $rowA['nazwa']; $b = $rowB['nazwa'];
  877. // if ($a == $b) return 0;
  878. // return ($a < $b) ? -1 : 1;
  879. // });
  880. //
  881. // DBG::table("objectList", $objectList, __CLASS__, __FUNCTION__, __LINE__);
  882. } catch (Exception $e) {
  883. UI::alert('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage());
  884. }
  885. UI::dol();
  886. }
  887. public function systemObjectsAction() {// TableAjax view: index.php?_route=ViewTableAjax&namespace=default_objects/SystemObject
  888. UI::gora();
  889. UI::menu();
  890. $this->navView();
  891. try {
  892. Lib::loadClass('Schema_SystemObjectStorageAcl');
  893. $acl = new Schema_SystemObjectStorageAcl();
  894. UI::table([
  895. 'rows' => array_map(
  896. function ($item) {
  897. return [
  898. 'ns' => $item['namespace'],
  899. 'nazwa' => $item['name'],
  900. 'type' => $item['_type'],
  901. 'edit' => '<a href="index.php?_route=ViewTableAjax&namespace=' . $item['namespace'] . '">edit</a>',
  902. 'wfs Describe' => '<a href="wfs-data.php/default_db/?SERVICE=WFS&VERSION=1.0.0&SRSNAME=EPSG:3003&REQUEST=DescribeFeatureType&TYPENAME=' . $item['typeName'] . '">DescribeFeatureType</a>',
  903. 'wfs getFeature' => '<a href="wfs-data.php/default_db/?SERVICE=WFS&VERSION=1.0.0&SRSNAME=EPSG:3003&REQUEST=GetFeature&TYPENAME=' . $item['typeName'] . '&MAXFEATURES=10">GetFeature</a> (max:10)'
  904. ];
  905. }
  906. , $acl->getItems([
  907. 'order_by' => 'namespace',
  908. 'order_dir' => 'asc'
  909. ])
  910. )
  911. ]);
  912. } catch (Exception $e) {
  913. UI::alert('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage());
  914. DBG::log($e);
  915. }
  916. UI::dol();
  917. }
  918. public function activateObjectAjaxAction() {
  919. DBG::log($_REQUEST, 'array', '$_REQUEST');
  920. Response::sendTryCatchJson(array($this, 'activateObjectAjax'), $_REQUEST);
  921. }
  922. public function activateObjectAjax($args) {
  923. $namespace = V::get('namespace', '', $args);
  924. if (empty($namespace)) throw new Exception("Missing param namespace");
  925. $item = SchemaFactory::loadDefaultObject('SystemObject')->getItem($namespace, [ 'propertyName' => '*,field' ]);
  926. if (empty($item)) throw new HttpException("Namespace not found", 404);
  927. DBG::log($item, 'array', "activateObjectAjax \$item");
  928. if ($item['isObjectActive']) throw new AlertSuccessException("Namespace '{$namespace}' already active");
  929. if (!$item['hasStruct']) throw new Exception("Missing struct for namespace '{$namespace}'");
  930. if (!$item['isStructInstalled']) throw new Exception("Namespace struct not installed '{$namespace}'");
  931. $activeFields = array_filter($item['field'], function ($field) {
  932. if (!$field['isActive']) return false;
  933. if (!$field['idZasob']) return false;
  934. return true;
  935. });
  936. if (empty($activeFields)) throw new Exception("Missing active fields for namespace '{$namespace}'");
  937. $affected = SchemaFactory::loadDefaultObject('SystemObject')->updateItem([
  938. 'namespace' => $item['namespace'],
  939. 'isObjectActive' => 1
  940. ]);
  941. if ($affected < 0) throw new Exception("Nie udało się aktywować obiektu '{$namespace}'");
  942. return [
  943. 'type' => "success",
  944. 'msg' => "Aktywowano obiekt '{$namespace}'",
  945. 'body' => [
  946. 'isObjectActive' => 1
  947. ]
  948. ];
  949. }
  950. }