superedit-VIEWTABLE_AJAX_EXPORT.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. function VIEWTABLE_AJAX_EXPORT() {
  3. $exportLimit = 10000;
  4. $zasobID = V::get('ZASOB_ID', 0, $_GET, 'int');
  5. if ($zasobID <= 0) {
  6. echo 'Wrong param ZASOB_ID';
  7. return;
  8. }
  9. Lib::loadClass('ProcesHelper');
  10. $zasobObj = ProcesHelper::getZasobTableInfo($zasobID);
  11. if (!$zasobObj) {
  12. echo "Zasob TABELA ID={$zasobID} nie istnieje";
  13. return;
  14. }
  15. $DBG = ('1' == V::get('DBG_EDS', '', $_GET));
  16. //echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">zasobObj (F.' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($zasobObj);echo'</pre>';
  17. $userAcl = User::getAcl();
  18. $userAcl->fetchGroups();
  19. //echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;display:none;">$userAcl (F.' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($userAcl);echo'</pre>';
  20. if (!$userAcl->hasTableAcl($zasobObj->ID)) {
  21. die("Brak uprawnień do tabeli ID={$zasobObj->ID}");
  22. }
  23. $tblAcl = $userAcl->getTableAcl($zasobObj->ID);
  24. //echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">tblAcl (F.' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($tblAcl);echo'</pre>';
  25. $forceTblAclInit = ('1' == V::get('_force', '', $_GET));
  26. $tblAcl->init($forceTblAclInit);
  27. if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">post (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($_POST);echo'</pre>';}
  28. if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">get (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($_GET);echo'</pre>';}
  29. $exportFlds = V::get('flds', '', $_GET);
  30. $exportFldList = explode(',', $exportFlds);
  31. if (!$exportFlds || 0 == count($exportFldList)) {
  32. echo "Nie wybrano żandych pól do exportu.";
  33. return;
  34. }
  35. $dataSource = $tblAcl->getExportDataSource($exportFldList);
  36. if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">dataSource (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($dataSource);echo'</pre>';}
  37. $args = $_GET;
  38. $currSortCol = V::get('sortCol', '', $args);
  39. $currSortFlip = V::get('sortDir', '', $args);
  40. $params = array();
  41. $params['limit'] = $exportLimit;
  42. // $params['limitstart'] = 0;
  43. $params['order_by'] = ($currSortCol)? $currSortCol : '';
  44. $params['order_dir'] = $currSortFlip;
  45. foreach ($args as $k => $v) {
  46. if (strlen($k) > 3 && substr($k, 0, 2) == 'f_' && strlen($v) > 0) {// filter prefix
  47. $params[$k] = $v;
  48. }
  49. else if (strlen($k) > 4 && substr($k, 0, 3) == 'sf_' && strlen($v) > 0) {// special filter prefix
  50. $params[$k] = $v;
  51. }
  52. }
  53. if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">params (F.' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($params);echo'</pre>';}
  54. $total = $dataSource->getTotal($params);
  55. if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">total (F.' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($total);echo'</pre>';}
  56. if ($total > $exportLimit) {
  57. $params['limit'] = $exportLimit;
  58. }
  59. $items = $dataSource->getItems($params);
  60. if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">items (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($items);echo'</pre>';}
  61. $flds = reset($items);
  62. $flds = get_object_vars($flds);
  63. $flds = array_keys($flds);
  64. $labels = array();
  65. foreach ($flds as $fldName) {
  66. $fldId = $tblAcl->getFieldIdByName($fldName);
  67. $label = $tblAcl->getFieldLabel($fldId);
  68. $labels[$fldName] = ($label)? $label : $fldName;
  69. }
  70. $format = V::get('format', 'html', $_GET);
  71. if ('html' == $format) {
  72. ?>
  73. <table class="table table-bordered">
  74. <thead>
  75. <tr>
  76. <?php foreach ($labels as $fldName => $label) : ?>
  77. <th><?php echo $label; ?></th>
  78. <?php endforeach; ?>
  79. </tr>
  80. </thead>
  81. <tbody>
  82. <?php foreach ($items as $item) : ?>
  83. <tr>
  84. <?php foreach ($labels as $fldName => $label) : ?>
  85. <td><?php echo $item->{$fldName}; ?></td>
  86. <?php endforeach; ?>
  87. </tr>
  88. <?php endforeach; ?>
  89. </tbody>
  90. </table>
  91. <?php
  92. }
  93. else if ('csv' == $format) {
  94. $tblName = $tblAcl->getName();
  95. $exportDate = date("Y-m-d_H_s");
  96. $csvFileName = "Tabela-{$tblName}-{$exportDate}";
  97. header('Content-Type: text/csv; charset=utf-8');
  98. header("Content-Disposition: attachment; filename={$csvFileName}.csv");
  99. //header('Content-Type: text/plain; charset=UTF-8');
  100. $csvSeparator = ';';
  101. $labelsLine = array();
  102. foreach ($labels as $fldName => $label) {
  103. $labelsLine[] = '"' . addslashes($label) . '"';
  104. }
  105. echo implode($csvSeparator, $labelsLine) . "\n";
  106. foreach ($items as $item) {
  107. $itemLine = array();
  108. foreach ($labels as $fldName => $label) {
  109. $itemLine[] = '"' . addslashes($item->{$fldName}) . '"';
  110. }
  111. echo implode($csvSeparator, $itemLine) . "\n";
  112. }
  113. }
  114. else {
  115. echo "Nieobsługiwany format danych.";
  116. }
  117. die();
  118. }