superedit-VIEWTABLE_AJAX_EXPORT.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. $label = $tblAcl->getFieldLabel($fldName);
  67. $labels[$fldName] = ($label)? $label : $fldName;
  68. }
  69. $format = V::get('format', 'html', $_GET);
  70. if ('html' == $format) {
  71. ?>
  72. <table class="table table-bordered">
  73. <thead>
  74. <tr>
  75. <?php foreach ($labels as $fldName => $label) : ?>
  76. <th><?php echo $label; ?></th>
  77. <?php endforeach; ?>
  78. </tr>
  79. </thead>
  80. <tbody>
  81. <?php foreach ($items as $item) : ?>
  82. <tr>
  83. <?php foreach ($labels as $fldName => $label) : ?>
  84. <td><?php echo $item->{$fldName}; ?></td>
  85. <?php endforeach; ?>
  86. </tr>
  87. <?php endforeach; ?>
  88. </tbody>
  89. </table>
  90. <?php
  91. }
  92. else if ('csv' == $format) {
  93. $tblName = $tblAcl->getName();
  94. $exportDate = date("Y-m-d_H_s");
  95. $csvFileName = "Tabela-{$tblName}-{$exportDate}";
  96. header('Content-Type: text/csv; charset=utf-8');
  97. header("Content-Disposition: attachment; filename={$csvFileName}.csv");
  98. //header('Content-Type: text/plain; charset=UTF-8');
  99. $csvSeparator = ';';
  100. $labelsLine = array();
  101. foreach ($labels as $fldName => $label) {
  102. $labelsLine[] = '"' . addslashes($label) . '"';
  103. }
  104. echo implode($csvSeparator, $labelsLine) . "\n";
  105. foreach ($items as $item) {
  106. $itemLine = array();
  107. foreach ($labels as $fldName => $label) {
  108. $itemLine[] = '"' . addslashes($item->{$fldName}) . '"';
  109. }
  110. echo implode($csvSeparator, $itemLine) . "\n";
  111. }
  112. }
  113. else {
  114. echo "Nieobsługiwany format danych.";
  115. }
  116. die();
  117. }