datatable.directory codebase
				https://datatable.directory/
			
			
		
			You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							60 lines
						
					
					
						
							1.3 KiB
						
					
					
				
			
		
		
	
	
							60 lines
						
					
					
						
							1.3 KiB
						
					
					
				| <?php
 | |
| 
 | |
| 
 | |
| namespace App\Tables;
 | |
| 
 | |
| 
 | |
| class CsvExporter extends BaseExporter
 | |
| {
 | |
|     private $delimiter = ',';
 | |
| 
 | |
|     public function withDelimiter($delimiter)
 | |
|     {
 | |
|         $this->delimiter = $delimiter;
 | |
|         return $this;
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @return string - mime type for the downloaded file
 | |
|      */
 | |
|     protected function getMimeType()
 | |
|     {
 | |
|         return $this->wantDownload ? 'text/csv' : 'text/plain';
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @return string - file extension for the downloaded file
 | |
|      */
 | |
|     protected function getFileExtension()
 | |
|     {
 | |
|         return 'csv';
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * Write the document to stdout ('php://output')
 | |
|      */
 | |
|     protected function writeDocument()
 | |
|     {
 | |
|         $handle = fopen('php://output', 'w');
 | |
| 
 | |
|         $columnNames = array_map(function (Column $c) {
 | |
|             return $c->title;
 | |
|         }, $this->columns);
 | |
| 
 | |
|         fputcsv($handle, $columnNames, $this->delimiter);
 | |
| 
 | |
|         foreach ($this->iterateRows() as $row) {
 | |
|             $items = [];
 | |
|             foreach ($this->columns as $column) {
 | |
|                 if (isset($row->{$column->name})) {
 | |
|                     $items[] = $row->{$column->name};
 | |
|                 } else {
 | |
|                     $items[] = null;
 | |
|                 }
 | |
|             }
 | |
|             fputcsv($handle, $items, $this->delimiter);
 | |
|         }
 | |
| 
 | |
|         fclose($handle);
 | |
|     }
 | |
| }
 | |
| 
 |