ajax.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. <?php
  2. Lib::loadClass('DB_Image');
  3. if (!class_exists('Lib')) die('404');
  4. function ajax_get_subtree() {
  5. header('Content-Type: text/html; charset=utf8');//utf8
  6. $id = V::get('id', '', $_GET);
  7. $tbl = V::get('tbl', '', $_GET);
  8. Lib::loadClass('FilterFactory');
  9. $tree_filter = FilterFactory::build($tbl);
  10. $tree_filter->setArgs($_GET);
  11. $clbk = 'tree_callback__show_item_from_'.$tbl;
  12. if (!function_exists($clbk)) {
  13. $clbk = null;
  14. }
  15. $tree = new Tree($tbl);
  16. if ($clbk) $tree->set_param('show_item_callback', $clbk);
  17. // TODO: get all filters:
  18. $tree->set_param('rozwin', ($tree_filter->get_arg('filtr_drzewo') == 'ROZWIN'));
  19. $tree->set_param('rozwin_proces', ($tree_filter->get_arg('filtr_procesy') == 'ROZWIN'));
  20. $tree->set_param('rozwin_zasoby', ($tree_filter->get_arg('filtr_zasoby') == 'ROZWIN'));
  21. $tree->set_param('rozwin_opis', ($tree_filter->get_arg('filtr_opis') == 'ROZWIN'));
  22. $tree->set_param('filtr_sort_prio', ($tree_filter->get_arg('filtr_sort_prio') == 'SHOW'));
  23. $tree->set_param('filtr_img', ($tree_filter->get_arg('filtr_img') == 'SHOW'));
  24. $tree->set_param('filtr_ob', ($tree_filter->get_arg('filtr_ob') == 'SHOW'));
  25. $tree->set_param('filtr_ids', ($tree_filter->get_arg('filtr_ids') == 'SHOW'));
  26. $tree->set_param('style', $tree_filter->get_arg('filtr_view'));
  27. $tree->set_param('search_id', $tree_filter->get_arg('filtr_search_id'));
  28. //TODO: check perm to edit
  29. $tree->set_param('editable', ($tree_filter->get_arg('filtr_edit') == 'TAK'));
  30. $tree->_limit = 100;
  31. $tree->_deep_limit = 10;
  32. $open_rec = V::get('open_rec', '', $_REQUEST);
  33. if ($open_rec == '1') {
  34. $tree->show_rec_all($id);
  35. } else {
  36. $tree->show_rec($id);
  37. }
  38. exit;
  39. }
  40. function ajax_add_image() {
  41. //sleep(1);// TODO: TEST
  42. $tbl = V::get('tbl', '', $_REQUEST);
  43. $id = V::get('id', '', $_REQUEST);
  44. //echo $tbl;
  45. //fun_IMAGE_ADD();
  46. $remote_table = V::get('tbl', '', $_REQUEST);
  47. $remote_id = V::get('id', '', $_REQUEST, 'int');
  48. $task = V::get('task', '', $_REQUEST);
  49. $thiss = new stdClass();
  50. $thiss->DETECT_TABLE_NAME = $remote_table;
  51. $errors = array();
  52. $msgs = array();
  53. // check remote id
  54. if ($remote_id <= 0) {
  55. echo'<p>';
  56. echo "Error wrong ID";
  57. echo'<br />';
  58. //echo App::link();// TODO: link do ???
  59. echo'</p>';
  60. return;
  61. }
  62. // check remote table
  63. if (!DB_Image::tableIsAllowed($remote_table)) {
  64. $errors[] = "Error table not allowed to add image";
  65. }
  66. // task
  67. $task = V::get('task', '', $_REQUEST);
  68. if ($task == 'REMOVE') {// TODO: przerobic na funkcje, zwrocic msg, pokazac w body
  69. $req = array();
  70. $req['ID'] = V::get('ID', '', $_REQUEST, 'int');
  71. $req['REMOTE_ID'] = V::get('REMOTE_ID', '', $_REQUEST, 'int');
  72. $req['REMOTE_TABLE'] = V::get('REMOTE_TABLE', '', $_REQUEST);
  73. // validate
  74. if ($req['ID'] <= 0) {
  75. $errors[] = 'error ID';
  76. }
  77. if ($req['REMOTE_ID'] <= 0) {
  78. $errors[] = 'error REMOTE_ID';
  79. }
  80. if ($req['REMOTE_TABLE'] != $remote_table) {
  81. $errors[] = 'error REMOTE_TABLE';
  82. }
  83. if (empty($errors)) {
  84. $affected = DB_Image::deleteImage($req['ID'], $req['REMOTE_TABLE'], $req['REMOTE_ID']);
  85. if ($affected == 1) {
  86. $msgs[] = '<p>'."Usunieto zdjecie ID=".$req['ID'].'</p>';
  87. } else {
  88. $erros[] = '<p>'."Wystapil blad podczas usuwania zdjecia ID=".$req['ID'].'</p>';
  89. }
  90. }
  91. }
  92. // check if a file was submitted
  93. if (!isset($_FILES['userfile'])) {
  94. //echo '<p>Please select a file</p>';
  95. }
  96. else {
  97. $errors = DB_Image::uploadImage($remote_table, $remote_id, $_FILES['userfile']);
  98. // give praise and thanks to the php gods
  99. if (empty($errors)) {
  100. $msgs[] = '<p>Thank you for submitting</p>';
  101. } else {
  102. $msgs[] = '<p style="border:1px solid red;">' . "Sorry, could not upload file:" . '<br />' . implode('<br />', $errors) .'</p>';
  103. }
  104. }
  105. $title = 'Dodaj plik do rekordu ID='.$remote_id.' z tabeli '.$remote_table;
  106. echo'<h3>'.$title.'</h3>';
  107. // show masgs
  108. if (!empty($msgs)) {
  109. echo'<div class="box box-green">';
  110. echo implode('<br />', $msgs);
  111. echo'</div>';
  112. }
  113. // show errors
  114. if (!empty($errors)) {
  115. echo'<div class="box box-red">';
  116. echo implode('<br />', $errors);
  117. echo'</div>';
  118. return;
  119. }
  120. echo'<div class="box box-silver">';
  121. $js = "ajax_form(this, 'id', 'tip-popup-box');";
  122. $js = ' onsubmit="'.$js.'"';
  123. echo'<form enctype="multipart/form-data" action="" method="post"'.$js.'>';
  124. echo'<input type="hidden" name="function_init" value="'. __FUNCTION__ .'" />';
  125. echo'<input type="hidden" name="REMOTE_ID" value="'.$remote_id.'" />';
  126. echo'<input type="hidden" name="REMOTE_TABLE" value="'.$remote_table.'" />';
  127. //echo'<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />';
  128. echo"Rodzaj obrazka: ";
  129. echo'<input type="text" name="'."DEST".'" value="'."".'" />';
  130. $select_arr = array();
  131. $select_arr = DB_Image::conf_get_options($thiss->DETECT_TABLE_NAME);
  132. if (!empty($select_arr)) {
  133. echo' ';
  134. $js = "if(this.selectedIndex>0){this.form.DEST.value=this.value}";
  135. echo'<select onchange="'.$js.'">';
  136. echo'<option value="">'."Wybierz".'</option>';
  137. foreach ($select_arr as $key => $val) {
  138. echo'<option value="'.$key.'">'.$val.'</option>';
  139. }
  140. echo'</select>';
  141. }
  142. echo'<br />';
  143. echo'<input name="userfile" type="file" />';
  144. echo'<br />';
  145. echo'<input type="submit" value="'."Dodaj obrazek".'" />';
  146. echo'</form>';
  147. echo'</div>';// .box-silver
  148. // TODO: show images, mv to function {
  149. $images = DB_Image::getImages($thiss->DETECT_TABLE_NAME, $remote_id);
  150. if (empty($images)) {
  151. echo'<p class="err">'."Brak obrazkow".'</p>';
  152. } else {
  153. echo'<h3>'."Wgrane pliki:".'</h3>';
  154. foreach ($images as $r) {
  155. $link_src = '?function_init=fun_SHOW_IMAGE&image_id='.$r->ID.'&HEADER_NOT_INIT=YES';
  156. $st = array();
  157. if ($r->WIDTH > 100 || $r->HEIGHT > 100) {
  158. if ($r->WIDTH > $r->HEIGHT) {
  159. $st[] = 'width:100px';
  160. } else {
  161. $st[] = 'height:100px';
  162. }
  163. }
  164. $st = (empty($st))? '' : ' style="'.implode(';', $st).'"';
  165. echo'<table style="float:left;border:1px solid silver;">';
  166. echo'<tr>';
  167. echo'<td>';
  168. echo''.$r->NAME.'<br />';
  169. echo''.$r->DEST.'<br />';
  170. echo''.$r->A_CREATE_DATE.'<br />';
  171. echo''.DB_Image::show_size($r->SIZE).' '.$r->WIDTH.'x'.$r->HEIGHT.'<br />';
  172. echo'<img src="'.$link_src.'" '.$st.'/>';
  173. echo'</td>';
  174. echo'</tr>';
  175. echo'<tr>';
  176. echo'<td style="text-align:center;">';
  177. echo'<form action="" method="post" style="display:inline">';
  178. echo'<input type="hidden" name="task" value="'."REMOVE".'" />';
  179. echo'<input type="hidden" name="ID" value="'.$r->ID.'" />';
  180. echo'<input type="hidden" name="REMOTE_ID" value="'.$remote_id.'" />';
  181. echo'<input type="hidden" name="REMOTE_TABLE" value="'.$remote_table.'" />';
  182. $js = "if(!confirm('Czy na pewno chcesz usunac obrazek?')) return false;";
  183. echo'<input type="submit" value="Usun" onclick="'.$js.'" />';
  184. echo'</form>';
  185. echo'<form action="" method="post" style="display:inline">';
  186. echo'<input type="hidden" name="task" value="'."EDIT".'" />';
  187. echo'<input type="hidden" name="ID" value="'.$r->ID.'" />';
  188. echo'<input type="hidden" name="REMOTE_ID" value="'.$remote_id.'" />';
  189. echo'<input type="hidden" name="REMOTE_TABLE" value="'.$remote_table.'" />';
  190. echo'<input type="submit" value="Edytuj" />';
  191. echo'</form>';
  192. echo'</td>';
  193. echo'</tr>';
  194. echo'</table>';
  195. }
  196. }
  197. // } function show images
  198. }
  199. function ajax_zasob_search_external_ids() {
  200. header('Content-Type: text/html; charset=utf8');
  201. $zasob_id = V::get('zasob_id', '', $_REQUEST, 'int');
  202. if ($zasob_id <= 0) {
  203. echo'Error: Bad request';// TODO: header
  204. return;
  205. }
  206. $DBG = ('123' == V::get('DBG', '', $_REQUEST));
  207. $zasob = DB::get_by_id('CRM_LISTA_ZASOBOW', $zasob_id);
  208. if (!$zasob) {
  209. echo'Error: 404 not found';// TODO: header
  210. return;
  211. }
  212. $cnf = Config::getConfFile('external_ids');
  213. if (!$cnf) {
  214. echo'Config error (external_ids)';
  215. return;
  216. }
  217. if($DBG){ echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">cnf (F.' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($cnf);echo'</pre>'; }
  218. if (!empty($cnf)) {
  219. $external_ids = array();
  220. foreach ($cnf as $k_table_name => $v_cnf) {
  221. $cur_cnf = new stdClass();
  222. $cur_cnf->id_col = V::get('id_col', 'ID', $v_cnf);
  223. $cur_cnf->search_col = V::get('search_col', 'CRM_LISTA_ZASOBOW_ID', $v_cnf);
  224. $cur_cnf->search_col_regex = V::get('search_col_regex', '', $v_cnf);
  225. $sql_id_col = $cur_cnf->id_col;
  226. $sql_where = "";
  227. if ($cur_cnf->search_col_regex) {
  228. $sql_where = "`{$cur_cnf->search_col}` like '".str_replace('$ID', $zasob->ID, $cur_cnf->search_col_regex)."'";
  229. } else {
  230. $sql_where = "`{$cur_cnf->search_col}`='{$zasob->ID}'";
  231. }
  232. $sql = "select t.`ID`, {$sql_id_col} as id_col
  233. from `{$k_table_name}` as t
  234. where {$sql_where}
  235. ";
  236. if($DBG){ echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">sql (F.' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($sql);echo'</pre>'; }
  237. $db = DB::getDB();
  238. $res = $db->query($sql);
  239. while ($r = $db->fetch($res)) {
  240. if ('ID' != $sql_id_col) {
  241. $external_ids[] = "{$r->ID}({$r->id_col})";
  242. } else {
  243. $external_ids[] = "{$r->id_col}";
  244. }
  245. }
  246. }
  247. }
  248. $out_link = '';
  249. if (empty($external_ids)) {
  250. $out_link = '<span class="not-found">'."Brak danych".'</span>';
  251. } else {
  252. $out_link = implode(', ', $external_ids);
  253. }
  254. echo '<span>';
  255. echo $out_link;
  256. echo " ";
  257. if(strstr("'NARZEDZIE','PLIK','ZASOB_PLIKOW','SOFTWARE','APLIKACJA','SERWER','SERWER_PORT','DOKUMENTY','SEGREGATOR','SKOROSZYT','KUWETA','KUWETA_MAGAZYNOWA','MATERIAL_REKLAMOWY','RESOURCE_CONSUMABLE','MATERIAL_EKSPLOATACYJNY','ALARM-SATEL','ALARM-CZYTNIK-KART','ALARM-KLAWIATURA','DOSTEP-ZAMEK','DOSTEP-KLUCZ','DOSTEP-ZAMEK-ELEKTRONICZNY','DOSTEP-ZAMEK-GRUPA','DOSTEP-ZAMEK-GRUPA-GHZ','DOSTEP-ZAMEK-GRUPA-GS','DOSTEP-ZAMEK-GRUPA-ES','DOSTEP-KARTA','NOTATKA','URZADZENIE','KOMPUTER','WYPOSAZENIE_LOKALU','SZAFA','POLKA','REJESTRATOR','KAMERA','PIECZATKA','PASSWORD','URZADZENIA_I_OZNACZENIA_BHP','OZNACZENIE_LOKALIZACJI','DOSTEP-UPRAWNIENIA'", $zasob->TYPE)
  258. ) {
  259. echo App::link("dodaj", "index.php?MENU_INIT=VIEWTABLE_AJAX&ZASOB_ID=".ProcesHelper::getZasobTableID('DEVICES')."&ff_A_STATUS=WAITING&ff_T_DEVICE_SERIAL=".date('Y-m-d')."-".$zasob->ID."&ff_T_DEVICE_INFO=".urlencode($zasob->OPIS)."&ff_T_DEVICE_TYPE=".urlencode($zasob->DESC)."&ff_CRM_LISTA_ZASOBOW_ID=".$zasob->ID."#CREATE", array('class'=>'btn-p5', 'title'=>'Dodaj zapotrzebowanie', 'onclick'=>null));
  260. echo App::link("szukaj", "index.php?MENU_INIT=VIEWTABLE_AJAX&ZASOB_ID=".ProcesHelper::getZasobTableID('DEVICES')."&ff_CRM_LISTA_ZASOBOW_ID=".$zasob->ID, array('class'=>'btn-p5', 'title'=>'szukaj', 'onclick'=>null));
  261. }
  262. echo App::link_ajax("refresh", "ajax_zasob_search_external_ids", array('zasob_id'=>$zasob->ID), array('js_result_type'=>'override_parent', 'js_result'=>'', 'title'=>'refresh IDS', 'ico'=>"refresh.png"));
  263. echo '</span>';
  264. // TODO: add link to CRM_EXT_IDS - rysuj strukture
  265. }
  266. function ajax_zasob_stanowisko_search_kontakty() {
  267. header('Content-Type: text/html; charset=utf8');
  268. $zasob_id = V::get('zasob_id', '', $_REQUEST, 'int');
  269. if ($zasob_id <= 0) {
  270. echo'Error: Bad request';// TODO: header
  271. return;
  272. }
  273. $DBG = ('123' == V::get('DBG', '', $_REQUEST));
  274. $zasob = DB::get_by_id('CRM_LISTA_ZASOBOW', $zasob_id);
  275. if (!$zasob) {
  276. echo'Error: 404 not found';// TODO: header
  277. return;
  278. }
  279. $external_ids = array();
  280. $mailto_links = array();
  281. $db = DB::getDB();
  282. $sql = "select u.`ID`, u.`ADM_ACCOUNT`, u.`ADM_NAME`, u.`EMAIL`
  283. from `CRM_AUTH_PROFILE` as up
  284. left join `ADMIN_USERS` as u on (u.`ID`=up.`REMOTE_ID`)
  285. where
  286. up.`ID_ZASOB`='{$zasob->ID}'
  287. and up.`REMOTE_TABLE`='ADMIN_USERS'
  288. and up.`A_STATUS` in('WAITING', 'NORMAL')
  289. and u.`A_STATUS` in('WAITING', 'NORMAL')
  290. group by u.`ID`
  291. ";
  292. if($DBG){ echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">sql (F.' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($sql);echo'</pre>'; }
  293. $res = $db->query($sql);
  294. while ($r = $db->fetch($res)) {
  295. $external_ids[] = "{$r->ADM_ACCOUNT}({$r->ADM_NAME})";
  296. if (!empty($r->EMAIL)) {
  297. $mailto_links[$r->EMAIL] = true;
  298. }
  299. }
  300. if($DBG){ echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">external_ids (F.' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($external_ids);echo'</pre>'; }
  301. $out_link = '';
  302. if (empty($external_ids)) {
  303. $out_link = '<span class="not-found">'."Brak danych".'</span>';
  304. } else {
  305. $out_link = implode(', ', $external_ids);
  306. }
  307. echo '<span>';
  308. echo $out_link;
  309. echo " ";
  310. echo App::link_ajax("refresh", "ajax_zasob_stanowisko_search_kontakty", array('zasob_id'=>$zasob->ID), array('js_result_type'=>'override_parent', 'js_result'=>'', 'title'=>'refresh IDS', 'ico'=>"refresh.png"));
  311. echo " ";
  312. if (!empty($mailto_links)) {
  313. $mailto_links = array_keys($mailto_links);
  314. echo '<a href="' . "mailto:" . implode(', ', $mailto_links) . '"><img src="icon/mail_all.png" height="16" alt=" mail " /></a>';
  315. echo " ";
  316. }
  317. echo App::link(" więcej &raquo; ", array('task'=>"USERS", '_group'=>$zasob_id), array('target'=>"_blank"));
  318. echo '</span>';
  319. // TODO: add link to CRM_EXT_IDS - rysuj strukture
  320. }
  321. function ajax_zasob_get_obowiazki() {
  322. $zasob_id = V::get('zasob_id', '', $_REQUEST, 'int');
  323. if ($zasob_id <= 0) {
  324. echo'Error: Bad request';// TODO: header
  325. return;
  326. }
  327. $zasob = DB::get_by_id('CRM_LISTA_ZASOBOW', $zasob_id);
  328. if (!$zasob) {
  329. echo'Error: 404 not found';// TODO: header
  330. return;
  331. }
  332. $external_ids = array();
  333. $db = DB::getDB();
  334. $sql = "select t.`ID_PROCES` as ext_id
  335. from `CRM_WSKAZNIK` as t
  336. join `CRM_PROCES` as p on(p.`ID`=t.`ID_PROCES`)
  337. where
  338. t.`ID_ZASOB`='{$zasob->ID}'
  339. and t.`A_STATUS` in('WAITING','NORMAL')
  340. and p.`A_STATUS` in('WAITING','NORMAL')
  341. ";
  342. $res = $db->query($sql);
  343. while ($r = $db->fetch($res)) {
  344. $external_ids[] = $r->ext_id;
  345. }
  346. $out_link_ids_limit = 5;
  347. $out_link = '';
  348. $out_link_add = '';
  349. if (empty($external_ids)) {
  350. $out_link = '<span class="not-found">'."Brak danych".'</span>';
  351. } else {
  352. $cnt = count($external_ids);
  353. if ($cnt > $out_link_ids_limit) {
  354. $external_ids_tmp = array();
  355. for ($i = 0; $i < $out_link_ids_limit; $i++) {
  356. $external_ids_tmp[] = $external_ids[$i];
  357. }
  358. $out_link_add .= implode(' ', $external_ids_tmp);
  359. $out_link_add .= ' ... ';
  360. $out_link_add .= App::link("({$cnt})", "?task=CRM_WYSWIETL_OBOWIAZKI&CLZ_ID={$zasob->ID}", array('target'=>'_blank', 'title'=>'Procesy powiazane z tym zasobem'));
  361. } else {
  362. $out_link_add .= implode(' ', $external_ids);
  363. $out_link_add .= ' ';
  364. $out_link_add .= App::link("(OB)", "?task=CRM_WYSWIETL_OBOWIAZKI&CLZ_ID={$zasob->ID}", array('target'=>'_blank', 'title'=>'Procesy powiazane z tym zasobem'));
  365. }
  366. }
  367. if ($out_link) {
  368. echo App::link_ajax($out_link, "ajax_zasob_get_obowiazki", array('zasob_id'=>$zasob->ID), array('js_result_type'=>'override', 'js_result'=>'', 'title'=>'refresh OB'));
  369. }
  370. if ($out_link_add) {
  371. echo $out_link_add;
  372. }
  373. }
  374. function ajax_show_images() {
  375. $remote_table = V::get('tbl', '', $_REQUEST);
  376. $remote_id = V::get('id', '', $_REQUEST, 'int');
  377. $thiss = new stdClass();
  378. $thiss->DETECT_TABLE_NAME = $remote_table;
  379. // check remote id
  380. if ($remote_id <= 0) {
  381. header('HTTP/1.1 400: Bad Request');
  382. header('Warning: wrong ID L.' . __LINE__);
  383. exit;
  384. }
  385. // check remote table
  386. $remote_tables = DB_Image::conf_get('remote_tables');
  387. if (!in_array($remote_table, $remote_tables)) {
  388. header('HTTP/1.1 400: Bad Request');
  389. header('Warning: table not allowed L.' . __LINE__);
  390. exit;
  391. }
  392. $images = DB_Image::getImages($remote_table, $remote_id);
  393. if (!$images) {
  394. header('HTTP/1.1 400: Bad Request');
  395. header('Warning: images not found in db L.' . __LINE__);
  396. exit;
  397. }
  398. echo'<p>'."Zdjecia dla rekordu <b>{$remote_id}</b> z tabeli {$remote_table}".'</p>';
  399. foreach ($images as $r) {
  400. $src = "?function_init=fun_SHOW_IMAGE&image_id={$r->ID}";
  401. echo'<img src="'.$src.'" alt="'.$r->TYPE.'" />';
  402. }
  403. exit;
  404. }
  405. function ajax_zasob_check_table() {
  406. $zasob_id = V::get('zasob_id', '', $_REQUEST, 'int');
  407. if ($zasob_id <= 0) {
  408. echo'Error: Bad request';// TODO: header
  409. return;
  410. }
  411. $zasob = DB::get_by_id('CRM_LISTA_ZASOBOW', $zasob_id);
  412. if (!$zasob) {
  413. echo'Error: 404 not found';// TODO: header
  414. return;
  415. }
  416. // TODO: get config from parent DATABASE%
  417. $cnf_id = $zasob->PARENT_ID;
  418. //$ext_db = DB::getDB( $r->PARENT_ID );
  419. $cnf = Config::getZasobConf($cnf_id);
  420. if (!$cnf) {
  421. echo'<span class="err">'."Brak konfiguracji dla zasobu <b>{$cnf_id}</b>".'</span>';
  422. return;
  423. } else {
  424. $db = DB::getDB($cnf_id);
  425. if ($db->get_errors()) {
  426. echo'<span class="err">'."Wystapily bledy podczas polaczenia z baza danych (zasob {$cnf_id}):";
  427. echo '<br />'.implode('<br />', $db->get_errors());
  428. echo'</span>';
  429. return;
  430. } else {
  431. //$db;
  432. echo'CONN OK;';// TODO: msg OK
  433. //echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">';print_r($db);echo'</pre>';
  434. }
  435. }
  436. // conn ok, check if table exists
  437. $sql = "show tables like '{$zasob->DESC}' ; ";
  438. $res = $db->query($sql);
  439. if ($db->num_rows($res) == 1) {
  440. echo'TABLE NAME OK;';// TODO: msg OK
  441. } else {
  442. echo'<span class="err">'."Niepoprawna nazwa tabeli <b>{$zasob->DESC}</b>".'</span>';
  443. return;
  444. }
  445. // conn ok, table exists, check table fields
  446. $fields = array();
  447. $sql = "describe `{$zasob->DESC}` ; ";
  448. $res = $db->query($sql);
  449. // Field, Type, Null, Key, Default, Extra
  450. // ID, int(10), NO, PRI, NULL, auto_increment
  451. // A_RECORD_CREATE_DATE, varchar(30), NO
  452. while ($r = $db->fetch($res)) {
  453. $params = new stdClass();
  454. // TODO: analyze field parameters: Type,Length,Default,Null?
  455. $params->Type = $r->Type;
  456. if (substr($r->Type, 0, 3) == 'int') {
  457. $params->Type = 'int';
  458. $params->Length = substr(substr($r->Type, 3), 1, -1);
  459. } else if (substr($r->Type, 0, 7) == 'varchar') {
  460. $params->Type = 'varchar';
  461. $params->Length = substr(substr($r->Type, 7), 1, -1);
  462. } else if (substr($r->Type, 0, 4) == 'enum') {
  463. $params->Type = 'enum';
  464. $params->Values = substr(substr($r->Type, 4), 1, -1);// TODO: add DANE under KOMORKA
  465. } else if (substr($r->Type, 0, 4) == 'date') {
  466. $params->Type = 'date';
  467. } else if (substr($r->Type, 0, 4) == 'text') {
  468. $params->Type = 'text';
  469. } else {
  470. $params->Type = 'unknown';
  471. }
  472. // TODO: ...
  473. if ($r->Default) $params->Default = $r->Default;
  474. else if ($r->Null == 'YES') $params->Default = 'Null';
  475. $fields[$r->Field] = $params;
  476. }
  477. echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">fields=';
  478. foreach ($fields as $f_name => $f_params) {
  479. echo"\n".'['.$f_name.']='.json_encode($f_params);
  480. }
  481. echo'</pre>';
  482. $komorki = array();
  483. $db = DB::getDB();
  484. $sql = "select
  485. `ID`, `TYPE`, `DESC`
  486. -- TODO: , `PARAMS`
  487. from `CRM_LISTA_ZASOBOW`
  488. where `PARENT_ID`='{$zasob->ID}'
  489. ";
  490. $res = $db->query($sql);
  491. // Field, Type, Null, Key, Default, Extra
  492. // ID, int(10), NO, PRI, NULL, auto_increment
  493. // A_RECORD_CREATE_DATE, varchar(30), NO
  494. while ($r = $db->fetch($res)) {
  495. $komorki[$r->DESC] = $r;
  496. }
  497. $errors = array();
  498. foreach ($komorki as $z_name => $z) {
  499. if ($z->TYPE != 'KOMORKA') {
  500. $errors[$z->ID] = "Wrong type ({$z->TYPE},{$z->DESC})";
  501. } else if (!array_key_exists($z->DESC, $fields)) {
  502. $errors[$z->ID] = "Not exists ({$z->DESC})";
  503. } else {
  504. }
  505. }
  506. echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">1 ';print_r($errors);echo'</pre>';
  507. $errors = array();
  508. foreach ($fields as $f_name => $f_params) {
  509. if (!array_key_exists($f_name, $komorki)) {
  510. $errors[$f_name] = 'Not exists ('.json_encode($f_params).')';
  511. } else {
  512. }
  513. }
  514. echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">2 ';print_r($errors);echo'</pre>';
  515. }
  516. function ajax_get_proces() {
  517. echo'<p>TODO: test ajax function L' . __LINE__ . '</p>';
  518. }