ajax.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. <?php
  2. if (!class_exists('Lib')) die('404');
  3. function ajax_table() {
  4. $ajax_req_id = V::get('_ajax_req_id', '', $_REQUEST);
  5. if (!$ajax_req_id) {
  6. die('<div>'."Req id error".'</div>');
  7. }
  8. Lib::loadClass( 'AjaxReq' );
  9. $conf = AjaxReq::get_conf( $ajax_req_id );
  10. if (!$conf) {
  11. die('<div>'."Req id error - session".'</div>');
  12. }
  13. $sql_table = V::get('sql_table', '', $conf);
  14. if (!$sql_table) {
  15. die('<div>'."conf error - table".'</div>');
  16. }
  17. $sql_cols_conf = V::get('sql_cols', '', $conf);
  18. if (!$sql_cols_conf) {
  19. die('<div>'."conf error - cols".'</div>');
  20. }
  21. $subtask = V::get('subtask', '', $_REQUEST);
  22. if ($subtask) {
  23. // get data by configuration
  24. header('Content-Type: text/javascript; charset=utf8');
  25. //echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">';print_r($_REQUEST);echo'</pre>';
  26. /* Array (
  27. [_ajax_request] => ajax_table
  28. [_ajax_req_id] => _tbl_4e9ebfeead7e2
  29. [subtask] => get_data
  30. [request_mode] => json
  31. [page] => 1
  32. [rp] => 20
  33. [sortname] => id
  34. [sortorder] => asc
  35. [query] =>
  36. [qtype] =>
  37. */
  38. $data = new stdClass();
  39. $data->page = 1;
  40. $data->total = 0;
  41. $data->rows = array();
  42. // get cols from session config
  43. $sql_cols = array();
  44. foreach ($sql_cols_conf as $key => $val) {
  45. $sql_cols []= $key;
  46. }//end foreach
  47. $data->page = V::get('page', '1', $_REQUEST, 'int');
  48. $limit = V::get('rp', '20', $_REQUEST, 'int');
  49. $sql_limit = 'limit '.$limit.' ';
  50. if ($data->page > 0) {
  51. $sql_limit = 'limit '.$limit.' OFFSET '.(($data->page - 1) * $limit);
  52. }
  53. $sql_order_by = strtoupper( V::get('sortname', 'ID', $_REQUEST) );
  54. $sql_order_sort = strtolower( V::get('sortorder', 'asc', $_REQUEST) );
  55. if (!in_array($sql_order_by, $sql_cols) || !in_array($sql_order_sort, array('asc', 'desc'))) {
  56. $sql_order_by = 'ID';
  57. $sql_order_sort = 'asc';
  58. }
  59. $sql_order = 'order by p.`'.$sql_order_by.'` '.$sql_order_sort;
  60. $sql_where = 'where 1';
  61. $sql_where_and = array();
  62. $query = V::get('query', '', $_REQUEST);
  63. //trigger_error($query, E_USER_NOTICE);
  64. if ($query) {
  65. $query = explode('&', $query);
  66. foreach ($query as $v) {
  67. $tmp = explode('=', $v);
  68. if (count($tmp) != 2) continue;
  69. if (!in_array(strtoupper($tmp[0]), $sql_cols)) continue;
  70. $sql_where_and []= " p.`" . $tmp[0] . "` like '" . $tmp[1] . "'";
  71. }//end foreach
  72. }
  73. if (!empty($sql_where_and)) {
  74. $sql_where = 'where '.implode(' and ', $sql_where_and);
  75. }
  76. $db = DB::getDB();
  77. $sql = "select count(1) as cnt from `".$sql_table."` as p $sql_where ";
  78. //trigger_error($sql, E_USER_NOTICE);
  79. $res = $db->query( $sql );
  80. if ($r = $db->fetch( $res )) {
  81. $data->total = $r->cnt;
  82. }
  83. $sql_cols_p = array();
  84. foreach ($sql_cols as $v) {
  85. $sql_cols_p []= "p.`".$v."`";
  86. }//end foreach
  87. $sql_cols_p = implode(",", $sql_cols_p);
  88. $sql = "select ".$sql_cols_p." from `".$sql_table."` as p $sql_where $sql_order $sql_limit ";
  89. //trigger_error($sql, E_USER_NOTICE);
  90. $res = $db->query( $sql );
  91. while ($r = $db->fetch( $res )) {
  92. $cell = array('id'=>$r->ID, 'cell'=>array());
  93. foreach ($sql_cols as $v_col_name) {
  94. $cell['cell'] []= str_replace('"', '&quot;', $r->$v_col_name);
  95. }//end foreach
  96. $data->rows []= (object)$cell;
  97. }
  98. //echo json_encode($data);
  99. //echo"\n\n\n";
  100. echo V::json_encode_latin2( $data );
  101. die('');
  102. }
  103. $tbl = new stdClass();
  104. $tbl->title = $conf['title'];// TODO: read from session
  105. $tbl->width = '1000';
  106. $tbl->height = '250';
  107. $tbl->id = 'table_view_'.$ajax_req_id;
  108. $tbl_ajax_request = array();
  109. $tbl_ajax_request []= "_ajax_request="."ajax_table";
  110. $tbl_ajax_request []= "_ajax_req_id=".$ajax_req_id;
  111. $tbl_ajax_request []= "subtask=get_data";
  112. $tbl_ajax_request []= "request_mode=json";
  113. $tbl->ajax_request = $_SERVER['PHP_SELF'] . "?" . implode('&', $tbl_ajax_request);
  114. // scripts with table config: fields,filters,functions, etc
  115. $tbl->searchitems = array();
  116. /*
  117. [
  118. {label: 'ID', name : 'id', def:'%'},
  119. {label: 'P_ID', name : 'parent_id', def:'%'},
  120. {label: 'Name', name : 'desc', def:'%'},
  121. {label: 'Opis', name : 'opis', def:'%'}
  122. ]
  123. */
  124. $tbl->fields = array();
  125. /*
  126. [
  127. {display: 'ID', name : 'id', width : 40, sortable : true, align: 'center'},
  128. {display: 'P_ID', name : 'parent_id', width : 40, sortable : true, align: 'center'},
  129. {display: 'TYPE', name : 'type', width : 70, sortable : true, align: 'center'},
  130. {display: 'Desc', name : 'desc', width : 300, sortable : true, align: 'left'},
  131. {display: 'Opis', name : 'opis', width : 350, sortable : true, align: 'left'}
  132. ]
  133. */
  134. // determine columns width
  135. $used_width = 0;
  136. $used_cols = 0;
  137. $col_width = array();
  138. foreach ($sql_cols_conf as $k => $v_conf) {
  139. $v_type = $v_conf['Type'];
  140. if (substr($v_type, 0, 3) == 'int') {
  141. $col_width[$k] = 60;
  142. $used_width += $col_width[$k] + 7;
  143. $used_cols += 1;
  144. } else {
  145. //$col_width [$k]= 0;
  146. }
  147. }
  148. $width = round( ($tbl->width - $used_width) / (count($sql_cols_conf) - $used_cols)) - 7;
  149. $perm_create = false;
  150. $perm_update = false;
  151. foreach ($sql_cols_conf as $k => $v_conf) {
  152. $v_name = $v_conf['Label'];
  153. $v_width = (isset($col_width[$k]))? $col_width[$k] : $width;
  154. $tbl->fields []= (object)array('display'=>$v_name, 'name'=>$k, 'width'=>$v_width, 'sortable'=>'true', 'align'=>'center');
  155. $tbl->searchitems []= (object)array('label'=>$v_name, 'name'=>$k, 'def'=>'%');
  156. if (strpos($v_conf['Perm'],'C') !== false) $perm_create = true;
  157. if (strpos($v_conf['Perm'],'W') !== false) $perm_update = true;
  158. }
  159. $tbl->js_buttons_callback = 'js_'.$ajax_req_id.'_cmd';
  160. $tbl->buttons = array();
  161. if ($perm_create) {// if has perm C
  162. $tbl->buttons []= (object)array('name'=>'add', 'label'=>'Dodaj', 'bclass'=>'add', 'onpress'=>$tbl->js_buttons_callback);
  163. $tbl->buttons []= (object)array('separator'=>'true');
  164. }
  165. if ($perm_update) {// if has perm W
  166. $tbl->buttons []= (object)array('name'=>'edit', 'label'=>'Edytuj', 'bclass'=>'edit', 'onpress'=>$tbl->js_buttons_callback);
  167. $tbl->buttons []= (object)array('separator'=>'true');
  168. }
  169. // $tbl->buttons []= (object)array('name'=>'delete', 'label'=>'Delete', 'bclass'=>'delete', 'onpress'=>$tbl->js_buttons_callback);
  170. // $tbl->buttons []= (object)array('separator'=>'true');
  171. $tbl->buttons []= (object)array('name'=>'text', 'label'=>'Pełny tekst', 'bclass'=>'edit', 'onpress'=>$tbl->js_buttons_callback);
  172. $tbl->buttons []= (object)array('separator'=>'true');
  173. echo"\n".'<link rel="stylesheet" type="text/css" href="./stuff/flexigrid/css/flexigrid.css" />';
  174. //echo'<script type="text/javascript" src="./stuff/jquery.js"></script>';
  175. echo"\n".'<script type="text/javascript" src="./stuff/flexigrid/js/flexigrid.js"></script>';
  176. echo"\n".'<script type="text/javascript" src="./stuff/jquery.cookie.js"></script>';
  177. echo"\n".'<script type="text/javascript">'."
  178. function ".$tbl->js_buttons_callback."(btn, grid){
  179. //console.log('function callback ".$tbl->js_buttons_callback."');
  180. var com = btn.title;
  181. if (com == 'delete') {
  182. confirm('Delete ' + jQuery('.trSelected', grid).length + ' items?')
  183. } else if (com == 'edit') {
  184. confirm('Edit ' + jQuery('.trSelected', grid).length + ' items?')
  185. } else if (com == 'add') {
  186. //console.log('Add New Item');
  187. } else if (com == 'text') {
  188. var hasCls = jQuery(grid).toggleClass('long-desc').hasClass('long-desc');
  189. if(hasCls) btn.innerHTML='Skrócony tekst';
  190. else btn.innerHTML='Pełny tekst';
  191. }
  192. }
  193. jQuery(document).ready(function(){
  194. jQuery('#".$tbl->id."').flexigrid({
  195. id: '".$tbl->id."'
  196. , url: '".$tbl->ajax_request."'
  197. , dataType: 'json'
  198. , colModel : " . V::json_encode_latin2( $tbl->fields ) . "
  199. , buttons : " . V::json_encode_latin2( $tbl->buttons ) . "
  200. , searchitems : " . V::json_encode_latin2( $tbl->searchitems ) . "
  201. , sortname: 'id'
  202. , sortorder: 'asc'
  203. , usepager: true
  204. , title: '".$tbl->title."'
  205. , useRp: true
  206. , rp: 10
  207. , rpOptions: [10, 20, 50]
  208. , showTableToggleBtn: true
  209. , width: ".$tbl->width."
  210. , height: ".$tbl->height."
  211. });
  212. });
  213. ".'</script>';
  214. echo"\n".'<div id="'.$ajax_req_id.'">';
  215. //echo'['.$ajax_req_id.']<br />';
  216. //echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">sql_cols_conf = ';print_r($sql_cols_conf);echo'</pre>';
  217. //echo'<br />';
  218. echo'<table id="'.$tbl->id.'" style="display:none"></table>';
  219. echo'</div>'."\n";
  220. }
  221. function ajax_get_subtree() {
  222. header('Content-Type: text/html; charset=utf8');//utf8
  223. $id = V::get('id', '', $_GET);
  224. $tbl = V::get('tbl', '', $_GET);
  225. //sleep(1);// TODO: DBG
  226. $tree_filter = get_filter_for_table( $tbl );
  227. $clbk = 'tree_callback__show_item_from_'.$tbl;
  228. if (!function_exists($clbk)) {
  229. $clbk = null;
  230. }
  231. $tree = new Tree( $tbl );
  232. if ($clbk) $tree->set_param('show_item_callback', $clbk);
  233. // TODO: get all filters:
  234. $tree->set_param('rozwin', ($tree_filter->get_arg('filtr_drzewo') == 'ROZWIN'));
  235. $tree->set_param('rozwin_proces', ($tree_filter->get_arg('filtr_procesy') == 'ROZWIN'));
  236. $tree->set_param('rozwin_zasoby', ($tree_filter->get_arg('filtr_zasoby') == 'ROZWIN'));
  237. $tree->set_param('rozwin_opis', ($tree_filter->get_arg('filtr_opis') == 'ROZWIN'));
  238. $tree->set_param('filtr_sort_prio', ($tree_filter->get_arg('filtr_sort_prio') == 'SHOW'));
  239. $tree->set_param('filtr_img', ($tree_filter->get_arg('filtr_img') == 'SHOW'));
  240. $tree->set_param('filtr_ob', ($tree_filter->get_arg('filtr_ob') == 'SHOW'));
  241. $tree->set_param('filtr_ids', ($tree_filter->get_arg('filtr_ids') == 'SHOW'));
  242. $tree->set_param('style', $tree_filter->get_arg('filtr_view'));
  243. $tree->set_param('search_id', $tree_filter->get_arg('filtr_search_id'));
  244. //TODO: check perm to edit
  245. $tree->set_param('editable', ($tree_filter->get_arg('filtr_edit') == 'TAK'));
  246. $tree->_limit = 100;
  247. $tree->_deep_limit = 10;
  248. $open_rec = V::get('open_rec', '', $_REQUEST);
  249. if ($open_rec == '1') {
  250. $tree->show_rec_all( $id );
  251. } else {
  252. $tree->show_rec( $id );
  253. }
  254. exit;
  255. }
  256. function ajax_add_image() {
  257. //sleep(1);// TODO: TEST
  258. $tbl = V::get('tbl', '', $_REQUEST);
  259. $id = V::get('id', '', $_REQUEST);
  260. //echo $tbl;
  261. //fun_IMAGE_ADD();
  262. $remote_table = V::get('tbl', '', $_REQUEST);
  263. $remote_id = V::get('id', '', $_REQUEST, 'int');
  264. $task = V::get('task', '', $_REQUEST);
  265. $thiss = new stdClass();
  266. $thiss->DETECT_TABLE_NAME = $remote_table;
  267. $errors = array();
  268. $msgs = array();
  269. Lib::loadClass('DB_Image');
  270. // check remote id
  271. if ($remote_id <= 0) {
  272. echo'<p>';
  273. echo "Error wrong ID";
  274. echo'<br />';
  275. //echo App::link();// TODO: link do ???
  276. echo'</p>';
  277. return;
  278. }
  279. // check remote table
  280. $remote_tables = DB_Image::conf_get('remote_tables');
  281. if (!in_array($remote_table, $remote_tables)) {
  282. $errors []= "Error table not allowed to add image";
  283. }
  284. // task
  285. $task = V::get('task', '', $_REQUEST);
  286. if ($task == 'REMOVE') {// TODO: przerobic na funkcje, zwrocic msg, pokazac w body
  287. $req = array();
  288. $req['ID'] = V::get('ID', '', $_REQUEST, 'int');
  289. $req['REMOTE_ID'] = V::get('REMOTE_ID', '', $_REQUEST, 'int');
  290. $req['REMOTE_TABLE'] = V::get('REMOTE_TABLE', '', $_REQUEST);
  291. // validate
  292. if ($req['ID'] <= 0) {
  293. $errors []= 'error ID';
  294. }
  295. if ($req['REMOTE_ID'] <= 0) {
  296. $errors []= 'error REMOTE_ID';
  297. }
  298. if ($req['REMOTE_TABLE'] != $remote_table) {
  299. $errors []= 'error REMOTE_TABLE';
  300. }
  301. if (empty($errors)) {
  302. $affected = DB_Image::delete_image($req['ID'], $req['REMOTE_TABLE'], $req['REMOTE_ID']);
  303. if ($affected == 1) {
  304. $msgs []= '<p>'."Usunieto zdjecie ID=".$req['ID'].'</p>';
  305. } else {
  306. $erros []= '<p>'."Wystapil blad podczas usuwania zdjecia ID=".$req['ID'].'</p>';
  307. }
  308. }
  309. }
  310. // check if a file was submitted
  311. if (!isset($_FILES['userfile'])) {
  312. //echo '<p>Please select a file</p>';
  313. }
  314. else {
  315. $errors = DB_Image::upload_image($remote_table, $remote_id, $_FILES['userfile']);
  316. // give praise and thanks to the php gods
  317. if (empty($errors)) {
  318. $msgs []= '<p>Thank you for submitting</p>';
  319. } else {
  320. $msgs []= '<p style="border:1px solid red;">' . "Sorry, could not upload file:" . '<br />' . implode('<br />', $errors) .'</p>';
  321. }
  322. }
  323. $title = 'Dodaj plik do rekordu ID='.$remote_id.' z tabeli '.$remote_table;
  324. echo'<h3>'.$title.'</h3>';
  325. // show masgs
  326. if (!empty($msgs)) {
  327. echo'<div class="box box-green">';
  328. echo implode('<br />', $msgs);
  329. echo'</div>';
  330. }
  331. // show errors
  332. if (!empty($errors)) {
  333. echo'<div class="box box-red">';
  334. echo implode('<br />', $errors);
  335. echo'</div>';
  336. return;
  337. }
  338. echo'<div class="box box-silver">';
  339. $js = "ajax_form(this, 'id', 'tip-popup-box');";
  340. $js = ' onsubmit="'.$js.'"';
  341. echo'<form enctype="multipart/form-data" action="" method="post"'.$js.'>';
  342. echo'<input type="hidden" name="function_init" value="'. __FUNCTION__ .'" />';
  343. echo'<input type="hidden" name="REMOTE_ID" value="'.$remote_id.'" />';
  344. echo'<input type="hidden" name="REMOTE_TABLE" value="'.$remote_table.'" />';
  345. //echo'<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />';
  346. echo"Rodzaj obrazka: ";
  347. echo'<input type="text" name="'."DEST".'" value="'."".'" />';
  348. $select_arr = array();
  349. $select_arr = DB_Image::conf_get_options( $thiss->DETECT_TABLE_NAME );
  350. if (!empty($select_arr)) {
  351. echo' ';
  352. $js = "if(this.selectedIndex>0){this.form.DEST.value=this.value}";
  353. echo'<select onchange="'.$js.'">';
  354. echo'<option value="">'."Wybierz".'</option>';
  355. foreach ($select_arr as $key => $val) {
  356. echo'<option value="'.$key.'">'.$val.'</option>';
  357. }//end foreach
  358. echo'</select>';
  359. }
  360. echo'<br />';
  361. echo'<input name="userfile" type="file" />';
  362. echo'<br />';
  363. echo'<input type="submit" value="'."Dodaj obrazek".'" />';
  364. echo'</form>';
  365. echo'</div>';// .box-silver
  366. // TODO: show images, mv to function {
  367. $images = DB_Image::get_images($thiss->DETECT_TABLE_NAME, $remote_id);
  368. if (empty($images)) {
  369. echo'<p class="err">'."Brak obrazkow".'</p>';
  370. } else {
  371. echo'<h3>'."Wgrane pliki:".'</h3>';
  372. foreach ($images as $r) {
  373. $link_src = '?function_init=fun_SHOW_IMAGE&image_id='.$r->ID.'&HEADER_NOT_INIT=YES';
  374. $st = array();
  375. if ($r->WIDTH > 100 || $r->HEIGHT > 100) {
  376. if ($r->WIDTH > $r->HEIGHT) {
  377. $st []= 'width:100px';
  378. } else {
  379. $st []= 'height:100px';
  380. }
  381. }
  382. $st = (empty($st))? '' : ' style="'.implode(';', $st).'"';
  383. echo'<table style="float:left;border:1px solid silver;">';
  384. echo'<tr>';
  385. echo'<td>';
  386. echo''.$r->NAME.'<br />';
  387. echo''.$r->DEST.'<br />';
  388. echo''.$r->A_CREATE_DATE.'<br />';
  389. echo''.DB_Image::show_size($r->SIZE).' '.$r->WIDTH.'x'.$r->HEIGHT.'<br />';
  390. echo'<img src="'.$link_src.'" '.$st.'/>';
  391. echo'</td>';
  392. echo'</tr>';
  393. echo'<tr>';
  394. echo'<td style="text-align:center;">';
  395. echo'<form action="" method="post" style="display:inline">';
  396. echo'<input type="hidden" name="task" value="'."REMOVE".'" />';
  397. echo'<input type="hidden" name="ID" value="'.$r->ID.'" />';
  398. echo'<input type="hidden" name="REMOTE_ID" value="'.$remote_id.'" />';
  399. echo'<input type="hidden" name="REMOTE_TABLE" value="'.$remote_table.'" />';
  400. $js = "if(!confirm('Czy na pewno chcesz usunac obrazek?')) return false;";
  401. echo'<input type="submit" value="Usun" onclick="'.$js.'" />';
  402. echo'</form>';
  403. echo'<form action="" method="post" style="display:inline">';
  404. echo'<input type="hidden" name="task" value="'."EDIT".'" />';
  405. echo'<input type="hidden" name="ID" value="'.$r->ID.'" />';
  406. echo'<input type="hidden" name="REMOTE_ID" value="'.$remote_id.'" />';
  407. echo'<input type="hidden" name="REMOTE_TABLE" value="'.$remote_table.'" />';
  408. echo'<input type="submit" value="Edytuj" />';
  409. echo'</form>';
  410. echo'</td>';
  411. echo'</tr>';
  412. echo'</table>';
  413. }//end foreach
  414. }
  415. // } function show images
  416. }
  417. function ajax_zasob_search_external_ids() {
  418. header('Content-Type: text/html; charset=utf8');
  419. $zasob_id = V::get('zasob_id', '', $_REQUEST, 'int');
  420. if ($zasob_id <= 0) {
  421. echo'Error: Bad request';// TODO: header
  422. return;
  423. }
  424. $DBG = (123 == V::get('DBG', '', $_REQUEST, 'int'));
  425. $zasob = DB::get_by_id( 'CRM_LISTA_ZASOBOW', $zasob_id );
  426. if (!$zasob) {
  427. echo'Error: 404 not found';// TODO: header
  428. return;
  429. }
  430. $cnf = Config::getConfFile('external_ids');
  431. if (!$cnf) {
  432. echo'Config error (external_ids)';
  433. return;
  434. }
  435. 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>'; }
  436. if (!empty($cnf)) {
  437. $external_ids = array();
  438. foreach ($cnf as $k_table_name => $v_cnf) {
  439. $cur_cnf = new stdClass();
  440. $cur_cnf->id_col = V::get('id_col', 'ID', $v_cnf);
  441. $cur_cnf->search_col = V::get('search_col', 'CRM_LISTA_ZASOBOW_ID', $v_cnf);
  442. $cur_cnf->search_col_regex = V::get('search_col_regex', '', $v_cnf);
  443. $sql_id_col = $cur_cnf->id_col;
  444. $sql_where = "";
  445. if ($cur_cnf->search_col_regex) {
  446. $sql_where = "`".$cur_cnf->search_col."` like '".str_replace('$ID', $zasob->ID, $cur_cnf->search_col_regex)."'";
  447. } else {
  448. $sql_where = "`".$cur_cnf->search_col."`='".$zasob->ID."'";
  449. }
  450. $sql = "select t.`ID`, {$sql_id_col} as id_col
  451. from `" . $k_table_name . "` as t
  452. where " . $sql_where . "
  453. ";
  454. 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>'; }
  455. $res = DB::query( $sql );
  456. while ($r = DB::fetch( $res )) {
  457. if ('ID' != $sql_id_col) {
  458. $external_ids []= $r->ID . "(" . $r->id_col . ")";
  459. } else {
  460. $external_ids []= "" . $r->id_col;
  461. }
  462. }
  463. }//end foreach
  464. }
  465. $out_link = '';
  466. if (empty($external_ids)) {
  467. $out_link = '<span class="not-found">'."Brak danych".'</span>';
  468. } else {
  469. $out_link = implode(', ', $external_ids);
  470. }
  471. echo '<span>';
  472. echo $out_link;
  473. echo " ";
  474. 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"));
  475. echo '</span>';
  476. // TODO: add link to CRM_EXT_IDS - rysuj strukture
  477. }
  478. function ajax_zasob_stanowisko_search_kontakty() {
  479. header('Content-Type: text/html; charset=utf8');
  480. $zasob_id = V::get('zasob_id', '', $_REQUEST, 'int');
  481. if ($zasob_id <= 0) {
  482. echo'Error: Bad request';// TODO: header
  483. return;
  484. }
  485. $DBG = (123 == V::get('DBG', '', $_REQUEST, 'int'));
  486. $zasob = DB::get_by_id( 'CRM_LISTA_ZASOBOW', $zasob_id );
  487. if (!$zasob) {
  488. echo'Error: 404 not found';// TODO: header
  489. return;
  490. }
  491. $external_ids = array();
  492. $mailto_links = array();
  493. $sql = "select u.`ID`, u.`ADM_ACCOUNT`, u.`ADM_NAME`, u.`EMAIL`
  494. from `CRM_AUTH_PROFILE` as up
  495. left join `ADMIN_USERS` as u on (u.`ID`=up.`REMOTE_ID`)
  496. where
  497. up.`ID_ZASOB`='" . $zasob->ID . "'
  498. and up.`REMOTE_TABLE`='ADMIN_USERS'
  499. and up.`A_STATUS` in('WAITING', 'NORMAL')
  500. and u.`A_STATUS` in('WAITING', 'NORMAL')
  501. group by u.`ID`
  502. ";
  503. 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>'; }
  504. $res = DB::query( $sql );
  505. while ($r = DB::fetch( $res )) {
  506. $external_ids []= $r->ADM_ACCOUNT . "(" . $r->ADM_NAME . ")";
  507. if (!empty($r->EMAIL)) {
  508. $mailto_links[$r->EMAIL] = true;
  509. }
  510. }
  511. 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>'; }
  512. $out_link = '';
  513. if (empty($external_ids)) {
  514. $out_link = '<span class="not-found">'."Brak danych".'</span>';
  515. } else {
  516. $out_link = implode(', ', $external_ids);
  517. }
  518. echo '<span>';
  519. echo $out_link;
  520. echo " ";
  521. 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"));
  522. echo " ";
  523. if (!empty($mailto_links)) {
  524. $mailto_links = array_keys($mailto_links);
  525. echo '<a href="' . "mailto:" . implode(', ', $mailto_links) . '"><img src="icon/mail_all.png" height="16" alt=" mail " /></a>';
  526. echo " ";
  527. }
  528. echo App::link(" więcej &raquo; ", array('task'=>"USERS", '_group'=>$zasob_id), array('target'=>"_blank"));
  529. echo '</span>';
  530. // TODO: add link to CRM_EXT_IDS - rysuj strukture
  531. }
  532. function ajax_zasob_get_obowiazki() {
  533. //sleep(1);
  534. $zasob_id = V::get('zasob_id', '', $_REQUEST, 'int');
  535. if ($zasob_id <= 0) {
  536. echo'Error: Bad request';// TODO: header
  537. return;
  538. }
  539. $zasob = DB::get_by_id( 'CRM_LISTA_ZASOBOW', $zasob_id );
  540. if (!$zasob) {
  541. echo'Error: 404 not found';// TODO: header
  542. return;
  543. }
  544. $external_ids = array();
  545. $db = DB::getDB();
  546. $sql = "select t.`ID_PROCES` as ext_id
  547. from `CRM_WSKAZNIK` as t
  548. join `CRM_PROCES` as p on(p.`ID`=t.`ID_PROCES`)
  549. where
  550. t.`ID_ZASOB`='{$zasob->ID}'
  551. and t.`A_STATUS` in('WAITING','NORMAL')
  552. and p.`A_STATUS` in('WAITING','NORMAL')
  553. ";
  554. $res = $db->query($sql);
  555. while ($r = $db->fetch($res)) {
  556. $external_ids []= $r->ext_id;
  557. }
  558. $out_link_ids_limit = 5;
  559. $out_link = '';
  560. $out_link_add = '';
  561. if (empty($external_ids)) {
  562. $out_link = '<span class="not-found">'."Brak danych".'</span>';
  563. } else {
  564. $cnt = count($external_ids);
  565. if ($cnt > $out_link_ids_limit) {
  566. $external_ids_tmp = array();
  567. for ($i = 0; $i < $out_link_ids_limit; $i++) {
  568. $external_ids_tmp []= $external_ids[$i];
  569. }//end foreach
  570. $out_link_add .= implode(' ', $external_ids_tmp);
  571. $out_link_add .= ' ... ';
  572. $out_link_add .= App::link("($cnt)", "?task=CRM_WYSWIETL_OBOWIAZKI&CLZ_ID=".$zasob->ID, array('target'=>'_blank', 'title'=>'Procesy powiazane z tym zasobem'));
  573. } else {
  574. $out_link_add .= implode(' ', $external_ids);
  575. $out_link_add .= ' ';
  576. $out_link_add .= App::link("(OB)", "?task=CRM_WYSWIETL_OBOWIAZKI&CLZ_ID=".$zasob->ID, array('target'=>'_blank', 'title'=>'Procesy powiazane z tym zasobem'));
  577. }
  578. }
  579. if ($out_link) {
  580. 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'));
  581. }
  582. if ($out_link_add) {
  583. echo $out_link_add;
  584. }
  585. }
  586. function ajax_show_images() {
  587. $remote_table = V::get('tbl', '', $_REQUEST);
  588. $remote_id = V::get('id', '', $_REQUEST, 'int');
  589. $thiss = new stdClass();
  590. $thiss->DETECT_TABLE_NAME = $remote_table;
  591. Lib::loadClass('DB_Image');
  592. // check remote id
  593. if ($remote_id <= 0) {
  594. header('HTTP/1.1 400: Bad Request');
  595. header('Warning: wrong ID L.' . __LINE__);
  596. exit;
  597. }
  598. // check remote table
  599. $remote_tables = DB_Image::conf_get('remote_tables');
  600. if (!in_array($remote_table, $remote_tables)) {
  601. header('HTTP/1.1 400: Bad Request');
  602. header('Warning: table not allowed L.' . __LINE__);
  603. exit;
  604. }
  605. $db = DB::getDB();
  606. $sql = "select `ID`, `TYPE`, `SIZE`, `DEST`
  607. from `".DB_Image::conf_get_table_name()."`
  608. where
  609. `REMOTE_ID`='".$remote_id."'
  610. and `REMOTE_TABLE`='".$remote_table."'
  611. ";
  612. $res = $db->query($sql);
  613. if (!$db->num_rows($res)) {
  614. header('HTTP/1.1 400: Bad Request');
  615. header('Warning: images not found in db L.' . __LINE__);
  616. exit;
  617. }
  618. echo'<p>'."Zdjecia dla rekordu <b>".$remote_id."</b> z tabeli ".$remote_table.'</p>';
  619. while ($r = $db->fetch( $res )) {
  620. $src = "?function_init=fun_SHOW_IMAGE&image_id=".$r->ID;
  621. echo'<img src="'.$src.'" alt="'.$r->TYPE.'" />';
  622. }
  623. exit;
  624. }
  625. function ajax_zasob_check_table() {
  626. //sleep(1);
  627. $zasob_id = V::get('zasob_id', '', $_REQUEST, 'int');
  628. if ($zasob_id <= 0) {
  629. echo'Error: Bad request';// TODO: header
  630. return;
  631. }
  632. $zasob = DB::get_by_id( 'CRM_LISTA_ZASOBOW', $zasob_id );
  633. if (!$zasob) {
  634. echo'Error: 404 not found';// TODO: header
  635. return;
  636. }
  637. // TODO: get config from parent DATABASE%
  638. $cnf_id = $zasob->PARENT_ID;
  639. //$ext_db = DB::getDB( $r->PARENT_ID );
  640. $cnf = Config::getZasobConf( $cnf_id );
  641. if (!$cnf) {
  642. echo'<span class="err">'."Brak konfiguracji dla zasobu <b>$cnf_id</b>".'</span>';
  643. return;
  644. } else {
  645. $db = DB::getDB( $cnf_id );
  646. if ($db->get_errors()) {
  647. echo'<span class="err">'."Wystapily bledy podczas polaczenia z baza danych (zasob $cnf_id):";
  648. echo '<br />'.implode('<br />', $db->get_errors());
  649. echo'</span>';
  650. return;
  651. } else {
  652. //$db;
  653. echo'CONN OK;';// TODO: msg OK
  654. //echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">';print_r($db);echo'</pre>';
  655. }
  656. }
  657. // conn ok, check if table exists
  658. $sql = "show tables like '".$zasob->DESC."' ; ";
  659. $res = $db->query( $sql );
  660. if ($db->num_rows( $res ) == 1) {
  661. echo'TABLE NAME OK;';// TODO: msg OK
  662. } else {
  663. echo'<span class="err">'."Niepoprawna nazwa tabeli <b>".$zasob->DESC."</b>".'</span>';
  664. return;
  665. }
  666. // conn ok, table exists, check table fields
  667. $fields = array();
  668. $sql = "describe `".$zasob->DESC."` ; ";
  669. $res = $db->query( $sql );
  670. // Field, Type, Null, Key, Default, Extra
  671. // ID, int(10), NO, PRI, NULL, auto_increment
  672. // A_RECORD_CREATE_DATE, varchar(30), NO
  673. while ($r = $db->fetch( $res )) {
  674. $params = new stdClass();
  675. // TODO: analyze field parameters: Type,Length,Default,Null?
  676. $params->Type = $r->Type;
  677. if (substr($r->Type, 0, 3) == 'int') {
  678. $params->Type = 'int';
  679. $params->Length = substr(substr($r->Type, 3), 1, -1);
  680. } else if (substr($r->Type, 0, 7) == 'varchar') {
  681. $params->Type = 'varchar';
  682. $params->Length = substr(substr($r->Type, 7), 1, -1);
  683. } else if (substr($r->Type, 0, 4) == 'enum') {
  684. $params->Type = 'enum';
  685. $params->Values = substr(substr($r->Type, 4), 1, -1);// TODO: add DANE under KOMORKA
  686. } else if (substr($r->Type, 0, 4) == 'date') {
  687. $params->Type = 'date';
  688. } else if (substr($r->Type, 0, 4) == 'text') {
  689. $params->Type = 'text';
  690. } else {
  691. $params->Type = 'unknown';
  692. }
  693. // TODO: ...
  694. if ($r->Default) $params->Default = $r->Default;
  695. else if ($r->Null == 'YES') $params->Default = 'Null';
  696. $fields[ $r->Field ] = $params;
  697. }
  698. echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">fields=';
  699. foreach ($fields as $f_name => $f_params) {
  700. echo"\n".'['.$f_name.']='.json_encode($f_params);
  701. }//end foreach
  702. echo'</pre>';
  703. $komorki = array();
  704. $sql = "select
  705. `ID`, `TYPE`, `DESC`
  706. -- TODO: , `PARAMS`
  707. from `CRM_LISTA_ZASOBOW`
  708. where `PARENT_ID`='".$zasob->ID."'
  709. ";
  710. $res = DB::query( $sql );
  711. // Field, Type, Null, Key, Default, Extra
  712. // ID, int(10), NO, PRI, NULL, auto_increment
  713. // A_RECORD_CREATE_DATE, varchar(30), NO
  714. while ($r = DB::fetch( $res )) {
  715. $komorki [$r->DESC] = $r;
  716. }
  717. $errors = array();
  718. foreach ($komorki as $z_name => $z) {
  719. if ($z->TYPE != 'KOMORKA') {
  720. $errors [$z->ID] = 'Wrong type ('.$z->TYPE.','.$z->DESC.')';
  721. } else if (!array_key_exists($z->DESC, $fields)) {
  722. $errors [$z->ID] = 'Not exists ('.$z->DESC.')';
  723. } else {
  724. }
  725. }//end foreach
  726. echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">1 ';print_r($errors);echo'</pre>';
  727. $errors = array();
  728. foreach ($fields as $f_name => $f_params) {
  729. if (!array_key_exists($f_name, $komorki)) {
  730. $errors [$f_name] = 'Not exists ('.json_encode($f_params).')';
  731. } else {
  732. }
  733. }//end foreach
  734. echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">2 ';print_r($errors);echo'</pre>';
  735. }
  736. function ajax_get_proces() {
  737. echo'<p>TODO: test ajax function L' . __LINE__ . '</p>';
  738. }