Storage.php 43 KB

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