Storage.php 40 KB

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