ext_ids.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. function task_CRM_EXT_IDS_TREE() {
  3. // fetch tree root
  4. $tree = array();
  5. $sql = "select z.`ID`, z.`DESC`
  6. from `CRM_LISTA_ZASOBOW` as z
  7. where
  8. z.`PARENT_ID`=0
  9. ";
  10. $res = DB::query( $sql );
  11. while ($r = DB::fetch( $res )) {
  12. $tree[] = $r;
  13. }
  14. echo '<div id="tree-ext-ids">';
  15. foreach ($tree as $r) {
  16. echo '<div class="node">';
  17. echo $r->ID . ' ' . $r->DESC;
  18. echo '</div>';
  19. }
  20. echo '</div>';
  21. echo '<script type="text/javascript">' . "
  22. //TODO: jQuery(document).ready(function(){
  23. });
  24. " . '</script>';
  25. }
  26. function task_CRM_EXT_IDS() {
  27. echo'<p>'."F." . __FUNCTION__ . "..." . '</p>';
  28. $cnf = Config::getConfFile('external_ids');
  29. if (!$cnf) {
  30. echo'Config error (external_ids)';
  31. return;
  32. }
  33. if (empty($cnf)) {
  34. echo'Config empty (external_ids)';
  35. return;
  36. }
  37. if (($remote_id = V::get('remote_id', '', $_REQUEST, 'int')) > 0 && ($remote_table = V::get('remote_table', '', $_REQUEST)) != '') {
  38. echo'<p>';
  39. echo App::link("CRM_EXT_IDS", "?task=CRM_EXT_IDS");
  40. echo " - Remote {$remote_table}.{$remote_id} ...";
  41. echo'</p>';
  42. if (!array_key_exists($remote_table, $cnf)) {
  43. echo'<p class="box box-red">'."Table not exists in config".'</p>';
  44. return;
  45. }
  46. $r = DB::get_by_id( $remote_table, $remote_id );
  47. echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;display:none">';print_r($r);echo'</pre>';
  48. echo'<dl style="border:1px solid #000;">';
  49. echo'<dt style="border-bottom:1px solid #000;background:#eee;">';
  50. echo $remote_table.'.';
  51. echo $r->ID;
  52. echo ' <em>'.$r->T_TELBOX_TYPE.'</em>';
  53. echo ' <b>'.$r->T_TELBOX_NAME.'</b>';
  54. echo' <span style="font-size:small">('.$r->S_ADDRESS_STREET.')</span>';
  55. echo'</dt>';
  56. echo'<dd>';
  57. if ($r->CRM_LISTA_ZASOBOW_ID > 0) {
  58. echo'<p>'."Przypisany do zasobu ".$r->CRM_LISTA_ZASOBOW_ID.".".'</p>';
  59. $tree = new Tree( 'CRM_LISTA_ZASOBOW' );
  60. $tree->show_rec( $r->CRM_LISTA_ZASOBOW_ID );
  61. } else {
  62. echo'<p class="err">'."Brak przypisanego zasobu.".'</p>';
  63. }
  64. echo'</dd>';
  65. echo'</dl>';
  66. return;
  67. }
  68. $external_tbls = array();
  69. foreach ($cnf as $k_table_name => $v_cnf) {
  70. $cur_cnf = new stdClass();
  71. $cur_cnf->table_name = $k_table_name;
  72. $cur_cnf->id_col = V::get('id_col', 'ID', $v_cnf);
  73. $cur_cnf->allowed_cols = V::get('allowed_cols', '', $v_cnf);
  74. $cur_cnf->search_col = V::get('search_col', 'CRM_LISTA_ZASOBOW_ID', $v_cnf);
  75. $cur_cnf->search_col_regex = V::get('search_col_regex', '', $v_cnf);
  76. $sql_id_col = $cur_cnf->id_col;
  77. $cur_cnf->cols = array();
  78. $cur_cnf->sql = "";
  79. $cur_cnf->cols ['ID']= 'ID';
  80. $cur_cnf->cols [$cur_cnf->id_col]= $cur_cnf->id_col;
  81. $cur_cnf->cols [$cur_cnf->search_col]= $cur_cnf->search_col;
  82. if (!empty($cur_cnf->allowed_cols)) {
  83. $cols = explode(',', $cur_cnf->allowed_cols);
  84. foreach ($cols as $v_col) {
  85. $v_col = trim($v_col);
  86. $k_col = $v_col;
  87. if (strpos($v_col, ':') !== false) {
  88. list($k_col, $v_col) = explode(':', $v_col, 2);
  89. }
  90. $cur_cnf->cols [$k_col]= $v_col;
  91. }
  92. }
  93. $sql_where = "";
  94. if ($cur_cnf->search_col_regex) {
  95. $sql_where = "`".$cur_cnf->search_col."` like '".str_replace('$ID', '%', $cur_cnf->search_col_regex)."'";
  96. } else {
  97. $sql_where = "`".$cur_cnf->search_col."` is not null";
  98. }
  99. $sql_cols = array();
  100. foreach ($cur_cnf->cols as $k_col => $v_col) {
  101. $sql_cols[] = "t.`".$k_col."` as ".$v_col."";
  102. }
  103. $cur_cnf->sql = "select ".implode(",", $sql_cols)."
  104. from `".$cur_cnf->table_name."` as t
  105. where ".$sql_where."
  106. ";
  107. $external_tbls[] = $cur_cnf;
  108. }
  109. foreach ($external_tbls as $cur_cnf) {
  110. echo'<p>'.$cur_cnf->table_name.'</p>';
  111. $res = DB::query( $cur_cnf->sql );
  112. echo'<table border=1>';
  113. echo'<thead>';
  114. echo'<tr>';
  115. foreach ($cur_cnf->cols as $k_col => $v_col) {
  116. echo'<th title="'.$k_col.'">'.$v_col.'</th>';
  117. }
  118. echo'</tr>';
  119. echo'</thead>';
  120. echo'<tbody>';
  121. while ($r = DB::fetch( $res )) {
  122. echo'<tr>';
  123. foreach (get_object_vars($r) as $k => $v) {
  124. echo'<td>';
  125. if ($k == 'ID') {
  126. echo App::link($v, "?task=CRM_EXT_IDS&remote_table=".$cur_cnf->table_name."&remote_id=".$v);
  127. } else {
  128. echo $v;
  129. }
  130. echo'</td>';
  131. }
  132. echo'</tr>';
  133. }
  134. echo'</tbody>';
  135. echo'</table>';
  136. echo'<hr />';
  137. }
  138. echo'<p>'."EOF F." . __FUNCTION__ . "..." . '</p>';
  139. }