Example #1
0
 /**
  * export and download table data as *.csv
  * this is primarily used for static tables
  * @return bool
  */
 public function exportData()
 {
     $status = false;
     if (static::$enableDataExport) {
         $tableModifier = static::getTableModifier();
         $headers = $tableModifier->getCols();
         // just get the records with existing columns
         // -> no "virtual" fields or "new" columns
         $this->fields($headers);
         $allRecords = $this->find();
         if ($allRecords) {
             $tableData = $allRecords->castAll();
             // format data -> "id" must be first key
             foreach ($tableData as &$rowData) {
                 $rowData = [$this->primary => $rowData['_id']] + $rowData;
                 unset($rowData['_id']);
             }
             $sheet = \Sheet::instance();
             $data = $sheet->dumpCSV($tableData, $headers);
             header('Expires: 0');
             header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
             header('Content-Type: text/csv;charset=UTF-8');
             header('Content-Disposition: attachment;filename=' . $this->getTable() . '.csv');
             echo $data;
             exit;
         }
     }
     return $status;
 }