ajax.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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. 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));
  258. 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));
  259. 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"));
  260. echo '</span>';
  261. // TODO: add link to CRM_EXT_IDS - rysuj strukture
  262. }
  263. function ajax_zasob_stanowisko_search_kontakty() {
  264. header('Content-Type: text/html; charset=utf8');
  265. $zasob_id = V::get('zasob_id', '', $_REQUEST, 'int');
  266. if ($zasob_id <= 0) {
  267. echo'Error: Bad request';// TODO: header
  268. return;
  269. }
  270. $DBG = ('123' == V::get('DBG', '', $_REQUEST));
  271. $zasob = DB::get_by_id('CRM_LISTA_ZASOBOW', $zasob_id);
  272. if (!$zasob) {
  273. echo'Error: 404 not found';// TODO: header
  274. return;
  275. }
  276. $external_ids = array();
  277. $mailto_links = array();
  278. $db = DB::getDB();
  279. $sql = "select u.`ID`, u.`ADM_ACCOUNT`, u.`ADM_NAME`, u.`EMAIL`
  280. from `CRM_AUTH_PROFILE` as up
  281. left join `ADMIN_USERS` as u on (u.`ID`=up.`REMOTE_ID`)
  282. where
  283. up.`ID_ZASOB`='{$zasob->ID}'
  284. and up.`REMOTE_TABLE`='ADMIN_USERS'
  285. and up.`A_STATUS` in('WAITING', 'NORMAL')
  286. and u.`A_STATUS` in('WAITING', 'NORMAL')
  287. group by u.`ID`
  288. ";
  289. 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>'; }
  290. $res = $db->query($sql);
  291. while ($r = $db->fetch($res)) {
  292. $external_ids[] = "{$r->ADM_ACCOUNT}({$r->ADM_NAME})";
  293. if (!empty($r->EMAIL)) {
  294. $mailto_links[$r->EMAIL] = true;
  295. }
  296. }
  297. 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>'; }
  298. $out_link = '';
  299. if (empty($external_ids)) {
  300. $out_link = '<span class="not-found">'."Brak danych".'</span>';
  301. } else {
  302. $out_link = implode(', ', $external_ids);
  303. }
  304. echo '<span>';
  305. echo $out_link;
  306. echo " ";
  307. 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"));
  308. echo " ";
  309. if (!empty($mailto_links)) {
  310. $mailto_links = array_keys($mailto_links);
  311. echo '<a href="' . "mailto:" . implode(', ', $mailto_links) . '"><img src="icon/mail_all.png" height="16" alt=" mail " /></a>';
  312. echo " ";
  313. }
  314. echo App::link(" więcej &raquo; ", array('task'=>"USERS", '_group'=>$zasob_id), array('target'=>"_blank"));
  315. echo '</span>';
  316. // TODO: add link to CRM_EXT_IDS - rysuj strukture
  317. }
  318. function ajax_zasob_get_obowiazki() {
  319. $zasob_id = V::get('zasob_id', '', $_REQUEST, 'int');
  320. if ($zasob_id <= 0) {
  321. echo'Error: Bad request';// TODO: header
  322. return;
  323. }
  324. $zasob = DB::get_by_id('CRM_LISTA_ZASOBOW', $zasob_id);
  325. if (!$zasob) {
  326. echo'Error: 404 not found';// TODO: header
  327. return;
  328. }
  329. $external_ids = array();
  330. $db = DB::getDB();
  331. $sql = "select t.`ID_PROCES` as ext_id
  332. from `CRM_WSKAZNIK` as t
  333. join `CRM_PROCES` as p on(p.`ID`=t.`ID_PROCES`)
  334. where
  335. t.`ID_ZASOB`='{$zasob->ID}'
  336. and t.`A_STATUS` in('WAITING','NORMAL')
  337. and p.`A_STATUS` in('WAITING','NORMAL')
  338. ";
  339. $res = $db->query($sql);
  340. while ($r = $db->fetch($res)) {
  341. $external_ids[] = $r->ext_id;
  342. }
  343. $out_link_ids_limit = 5;
  344. $out_link = '';
  345. $out_link_add = '';
  346. if (empty($external_ids)) {
  347. $out_link = '<span class="not-found">'."Brak danych".'</span>';
  348. } else {
  349. $cnt = count($external_ids);
  350. if ($cnt > $out_link_ids_limit) {
  351. $external_ids_tmp = array();
  352. for ($i = 0; $i < $out_link_ids_limit; $i++) {
  353. $external_ids_tmp[] = $external_ids[$i];
  354. }
  355. $out_link_add .= implode(' ', $external_ids_tmp);
  356. $out_link_add .= ' ... ';
  357. $out_link_add .= App::link("({$cnt})", "?task=CRM_WYSWIETL_OBOWIAZKI&CLZ_ID={$zasob->ID}", array('target'=>'_blank', 'title'=>'Procesy powiazane z tym zasobem'));
  358. } else {
  359. $out_link_add .= implode(' ', $external_ids);
  360. $out_link_add .= ' ';
  361. $out_link_add .= App::link("(OB)", "?task=CRM_WYSWIETL_OBOWIAZKI&CLZ_ID={$zasob->ID}", array('target'=>'_blank', 'title'=>'Procesy powiazane z tym zasobem'));
  362. }
  363. }
  364. if ($out_link) {
  365. 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'));
  366. }
  367. if ($out_link_add) {
  368. echo $out_link_add;
  369. }
  370. }
  371. function ajax_show_images() {
  372. $remote_table = V::get('tbl', '', $_REQUEST);
  373. $remote_id = V::get('id', '', $_REQUEST, 'int');
  374. $thiss = new stdClass();
  375. $thiss->DETECT_TABLE_NAME = $remote_table;
  376. // check remote id
  377. if ($remote_id <= 0) {
  378. header('HTTP/1.1 400: Bad Request');
  379. header('Warning: wrong ID L.' . __LINE__);
  380. exit;
  381. }
  382. // check remote table
  383. $remote_tables = DB_Image::conf_get('remote_tables');
  384. if (!in_array($remote_table, $remote_tables)) {
  385. header('HTTP/1.1 400: Bad Request');
  386. header('Warning: table not allowed L.' . __LINE__);
  387. exit;
  388. }
  389. $images = DB_Image::getImages($remote_table, $remote_id);
  390. if (!$images) {
  391. header('HTTP/1.1 400: Bad Request');
  392. header('Warning: images not found in db L.' . __LINE__);
  393. exit;
  394. }
  395. echo'<p>'."Zdjecia dla rekordu <b>{$remote_id}</b> z tabeli {$remote_table}".'</p>';
  396. foreach ($images as $r) {
  397. $src = "?function_init=fun_SHOW_IMAGE&image_id={$r->ID}";
  398. echo'<img src="'.$src.'" alt="'.$r->TYPE.'" />';
  399. }
  400. exit;
  401. }
  402. function ajax_zasob_check_table() {
  403. $zasob_id = V::get('zasob_id', '', $_REQUEST, 'int');
  404. if ($zasob_id <= 0) {
  405. echo'Error: Bad request';// TODO: header
  406. return;
  407. }
  408. $zasob = DB::get_by_id('CRM_LISTA_ZASOBOW', $zasob_id);
  409. if (!$zasob) {
  410. echo'Error: 404 not found';// TODO: header
  411. return;
  412. }
  413. // TODO: get config from parent DATABASE%
  414. $cnf_id = $zasob->PARENT_ID;
  415. //$ext_db = DB::getDB( $r->PARENT_ID );
  416. $cnf = Config::getZasobConf($cnf_id);
  417. if (!$cnf) {
  418. echo'<span class="err">'."Brak konfiguracji dla zasobu <b>{$cnf_id}</b>".'</span>';
  419. return;
  420. } else {
  421. $db = DB::getDB($cnf_id);
  422. if ($db->get_errors()) {
  423. echo'<span class="err">'."Wystapily bledy podczas polaczenia z baza danych (zasob {$cnf_id}):";
  424. echo '<br />'.implode('<br />', $db->get_errors());
  425. echo'</span>';
  426. return;
  427. } else {
  428. //$db;
  429. echo'CONN OK;';// TODO: msg OK
  430. //echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">';print_r($db);echo'</pre>';
  431. }
  432. }
  433. // conn ok, check if table exists
  434. $sql = "show tables like '{$zasob->DESC}' ; ";
  435. $res = $db->query($sql);
  436. if ($db->num_rows($res) == 1) {
  437. echo'TABLE NAME OK;';// TODO: msg OK
  438. } else {
  439. echo'<span class="err">'."Niepoprawna nazwa tabeli <b>{$zasob->DESC}</b>".'</span>';
  440. return;
  441. }
  442. // conn ok, table exists, check table fields
  443. $fields = array();
  444. $sql = "describe `{$zasob->DESC}` ; ";
  445. $res = $db->query($sql);
  446. // Field, Type, Null, Key, Default, Extra
  447. // ID, int(10), NO, PRI, NULL, auto_increment
  448. // A_RECORD_CREATE_DATE, varchar(30), NO
  449. while ($r = $db->fetch($res)) {
  450. $params = new stdClass();
  451. // TODO: analyze field parameters: Type,Length,Default,Null?
  452. $params->Type = $r->Type;
  453. if (substr($r->Type, 0, 3) == 'int') {
  454. $params->Type = 'int';
  455. $params->Length = substr(substr($r->Type, 3), 1, -1);
  456. } else if (substr($r->Type, 0, 7) == 'varchar') {
  457. $params->Type = 'varchar';
  458. $params->Length = substr(substr($r->Type, 7), 1, -1);
  459. } else if (substr($r->Type, 0, 4) == 'enum') {
  460. $params->Type = 'enum';
  461. $params->Values = substr(substr($r->Type, 4), 1, -1);// TODO: add DANE under KOMORKA
  462. } else if (substr($r->Type, 0, 4) == 'date') {
  463. $params->Type = 'date';
  464. } else if (substr($r->Type, 0, 4) == 'text') {
  465. $params->Type = 'text';
  466. } else {
  467. $params->Type = 'unknown';
  468. }
  469. // TODO: ...
  470. if ($r->Default) $params->Default = $r->Default;
  471. else if ($r->Null == 'YES') $params->Default = 'Null';
  472. $fields[$r->Field] = $params;
  473. }
  474. echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">fields=';
  475. foreach ($fields as $f_name => $f_params) {
  476. echo"\n".'['.$f_name.']='.json_encode($f_params);
  477. }
  478. echo'</pre>';
  479. $komorki = array();
  480. $db = DB::getDB();
  481. $sql = "select
  482. `ID`, `TYPE`, `DESC`
  483. -- TODO: , `PARAMS`
  484. from `CRM_LISTA_ZASOBOW`
  485. where `PARENT_ID`='{$zasob->ID}'
  486. ";
  487. $res = $db->query($sql);
  488. // Field, Type, Null, Key, Default, Extra
  489. // ID, int(10), NO, PRI, NULL, auto_increment
  490. // A_RECORD_CREATE_DATE, varchar(30), NO
  491. while ($r = $db->fetch($res)) {
  492. $komorki[$r->DESC] = $r;
  493. }
  494. $errors = array();
  495. foreach ($komorki as $z_name => $z) {
  496. if ($z->TYPE != 'KOMORKA') {
  497. $errors[$z->ID] = "Wrong type ({$z->TYPE},{$z->DESC})";
  498. } else if (!array_key_exists($z->DESC, $fields)) {
  499. $errors[$z->ID] = "Not exists ({$z->DESC})";
  500. } else {
  501. }
  502. }
  503. echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">1 ';print_r($errors);echo'</pre>';
  504. $errors = array();
  505. foreach ($fields as $f_name => $f_params) {
  506. if (!array_key_exists($f_name, $komorki)) {
  507. $errors[$f_name] = 'Not exists ('.json_encode($f_params).')';
  508. } else {
  509. }
  510. }
  511. echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">2 ';print_r($errors);echo'</pre>';
  512. }
  513. function ajax_get_proces() {
  514. echo'<p>TODO: test ajax function L' . __LINE__ . '</p>';
  515. }