| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- <?php
- function VIEWTABLE_AJAX_EXPORT() {
- $exportLimit = 10000;
- $zasobID = V::get('ZASOB_ID', 0, $_GET, 'int');
- if ($zasobID <= 0) {
- echo 'Wrong param ZASOB_ID';
- return;
- }
- Lib::loadClass('ProcesHelper');
- $zasobObj = ProcesHelper::getZasobTableInfo($zasobID);
- if (!$zasobObj) {
- echo "Zasob TABELA ID={$zasobID} nie istnieje";
- return;
- }
- $DBG = ('1' == V::get('DBG_EDS', '', $_GET));
- //echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">zasobObj (F.' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($zasobObj);echo'</pre>';
- $userAcl = User::getAcl();
- $userAcl->fetchGroups();
- //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>';
- if (!$userAcl->hasTableAcl($zasobObj->ID)) {
- die("Brak uprawnień do tabeli ID={$zasobObj->ID}");
- }
- $tblAcl = $userAcl->getTableAcl($zasobObj->ID);
- //echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">tblAcl (F.' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($tblAcl);echo'</pre>';
- $forceTblAclInit = ('1' == V::get('_force', '', $_GET));
- $tblAcl->init($forceTblAclInit);
- 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>';}
- 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>';}
- $exportFlds = V::get('flds', '', $_GET);
- $exportFldList = explode(',', $exportFlds);
- if (!$exportFlds || 0 == count($exportFldList)) {
- echo "Nie wybrano żandych pól do exportu.";
- return;
- }
- $dataSource = $tblAcl->getExportDataSource($exportFldList);
- 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>';}
- $args = $_GET;
- $currSortCol = V::get('sortCol', '', $args);
- $currSortFlip = V::get('sortDir', '', $args);
- $params = array();
- $params['limit'] = $exportLimit;
- // $params['limitstart'] = 0;
- $params['order_by'] = ($currSortCol)? $currSortCol : '';
- $params['order_dir'] = $currSortFlip;
- foreach ($args as $k => $v) {
- if (strlen($k) > 3 && substr($k, 0, 2) == 'f_' && strlen($v) > 0) {// filter prefix
- $params[$k] = $v;
- }
- else if (strlen($k) > 4 && substr($k, 0, 3) == 'sf_' && strlen($v) > 0) {// special filter prefix
- $params[$k] = $v;
- }
- }
- 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>';}
- $total = $dataSource->getTotal($params);
- 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>';}
- if ($total > $exportLimit) {
- $params['limit'] = $exportLimit;
- }
- $items = $dataSource->getItems($params);
- 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>';}
- $flds = reset($items);
- $flds = get_object_vars($flds);
- $flds = array_keys($flds);
- $labels = array();
- foreach ($flds as $fldName) {
- $fldId = $tblAcl->getFieldIdByName($fldName);
- $label = $tblAcl->getFieldLabel($fldId);
- $labels[$fldName] = ($label)? $label : $fldName;
- }
- $format = V::get('format', 'html', $_GET);
- if ('html' == $format) {
- ?>
- <table class="table table-bordered">
- <thead>
- <tr>
- <?php foreach ($labels as $fldName => $label) : ?>
- <th><?php echo $label; ?></th>
- <?php endforeach; ?>
- </tr>
- </thead>
- <tbody>
- <?php foreach ($items as $item) : ?>
- <tr>
- <?php foreach ($labels as $fldName => $label) : ?>
- <td><?php echo $item->{$fldName}; ?></td>
- <?php endforeach; ?>
- </tr>
- <?php endforeach; ?>
- </tbody>
- </table>
- <?php
- }
- else if ('csv' == $format) {
- $tblName = $tblAcl->getName();
- $exportDate = date("Y-m-d_H_s");
- $csvFileName = "Tabela-{$tblName}-{$exportDate}";
- header('Content-Type: text/csv; charset=utf-8');
- header("Content-Disposition: attachment; filename={$csvFileName}.csv");
- //header('Content-Type: text/plain; charset=UTF-8');
- $csvSeparator = ';';
- $labelsLine = array();
- foreach ($labels as $fldName => $label) {
- $labelsLine[] = '"' . addslashes($label) . '"';
- }
- echo implode($csvSeparator, $labelsLine) . "\n";
- foreach ($items as $item) {
- $itemLine = array();
- foreach ($labels as $fldName => $label) {
- $itemLine[] = '"' . addslashes($item->{$fldName}) . '"';
- }
- echo implode($csvSeparator, $itemLine) . "\n";
- }
- }
- else {
- echo "Nieobsługiwany format danych.";
- }
- die();
- }
|