Storage.php 40 KB

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