csv.php
6 days ago
csv.xml
6 days ago
excel.php
6 days ago
index.html
6 days ago
sql.php
6 days ago
sql.xml
6 days ago
csv.php
84 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package VikAppointments |
| 4 | * @subpackage core |
| 5 | * @author E4J s.r.l. |
| 6 | * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved. |
| 7 | * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 | * @link https://vikwp.com |
| 9 | */ |
| 10 | |
| 11 | // No direct access |
| 12 | defined('ABSPATH') or die('No script kiddies please!'); |
| 13 | |
| 14 | VAPLoader::import('libraries.import.exportable'); |
| 15 | |
| 16 | /** |
| 17 | * This class implements the abstract methods defined by the |
| 18 | * exportable interface in order to export the records in CSV format. |
| 19 | * |
| 20 | * @since 1.6 |
| 21 | */ |
| 22 | class ExportableCsv extends Exportable |
| 23 | { |
| 24 | /** |
| 25 | * Creates the file with the given records and downloads it |
| 26 | * using the specified filename. |
| 27 | * |
| 28 | * @param string $name The file name. |
| 29 | * @param array $records The records to export. |
| 30 | * @param ImportObject $obj The object handler. |
| 31 | * |
| 32 | * @return void |
| 33 | */ |
| 34 | public function download($name, array $records = array(), ?ImportObject $obj = null) |
| 35 | { |
| 36 | $head = array(); |
| 37 | |
| 38 | // get supported columns |
| 39 | $columns = $obj->getColumns(); |
| 40 | |
| 41 | /** |
| 42 | * Check whether we need to exclude some columns from the CSV. |
| 43 | * |
| 44 | * @since 1.7 |
| 45 | */ |
| 46 | if ($selectedColumns = $this->options->get('columns', array())) |
| 47 | { |
| 48 | // take only the columns included within the list |
| 49 | $columns = array_filter($columns, function($col) use ($selectedColumns) |
| 50 | { |
| 51 | return in_array($col->name, $selectedColumns); |
| 52 | }); |
| 53 | } |
| 54 | |
| 55 | // iterate the columns to build the header |
| 56 | foreach ($columns as $col) |
| 57 | { |
| 58 | $head[] = $col->label; |
| 59 | } |
| 60 | |
| 61 | // prepend the CSV heading at the beginning of the records list |
| 62 | array_unshift($records, $head); |
| 63 | |
| 64 | $delimiter = $this->options->get('delimiter', ','); |
| 65 | $enclosure = $this->options->get('enclosure', '"'); |
| 66 | |
| 67 | $app = JFactory::getApplication(); |
| 68 | |
| 69 | $app->setHeader('Cache-Control', 'no-store, no-cache'); |
| 70 | $app->setHeader('Content-Type', 'text/csv; charset=UTF-8'); |
| 71 | $app->setHeader('Content-Disposition', 'attachment; filename="' . $name . '.csv"'); |
| 72 | $app->sendHeaders(); |
| 73 | |
| 74 | $handle = fopen('php://output', 'w'); |
| 75 | |
| 76 | foreach ($records as $row) |
| 77 | { |
| 78 | fputcsv($handle, $row, $delimiter, $enclosure, $escape = ''); |
| 79 | } |
| 80 | |
| 81 | fclose($handle); |
| 82 | } |
| 83 | } |
| 84 |