csv.php
163 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 | /** |
| 15 | * Class used to export a list of reservations in CSV format. |
| 16 | * |
| 17 | * @deprecated 1.8 Use VAPOrderExportFactory instead. |
| 18 | */ |
| 19 | class VikExporterCSV |
| 20 | { |
| 21 | /** |
| 22 | * An array of options. |
| 23 | * |
| 24 | * @var array |
| 25 | */ |
| 26 | private $options; |
| 27 | |
| 28 | /** |
| 29 | * Flag to start the auto-download or not. |
| 30 | * |
| 31 | * @var boolean |
| 32 | */ |
| 33 | private $auto_download = true; |
| 34 | |
| 35 | /** |
| 36 | * Class constructor. |
| 37 | * |
| 38 | * @param integer $from_ts The starting timestamp. |
| 39 | * @param integer $to_ts The ending timestamp. |
| 40 | * @param integer $id_emp The employee ID. |
| 41 | */ |
| 42 | public function __construct($from_ts, $to_ts, $id_emp = 0) |
| 43 | { |
| 44 | if (is_int($from_ts)) |
| 45 | { |
| 46 | $from_ts = date('Y-m-d', $from_ts); |
| 47 | } |
| 48 | |
| 49 | if (is_int($to_ts)) |
| 50 | { |
| 51 | $to_ts = date('Y-m-d', $to_ts); |
| 52 | } |
| 53 | |
| 54 | $this->options = array( |
| 55 | 'fromdate' => $from_ts, |
| 56 | 'todate' => $to_ts, |
| 57 | 'id_employee' => $id_emp, |
| 58 | ); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Forces the ID of the order to fetch. |
| 63 | * |
| 64 | * @param integer $id_order The order ID. |
| 65 | * |
| 66 | * @return self This object to support chaining. |
| 67 | */ |
| 68 | public function forceOrderID($id_order) |
| 69 | { |
| 70 | $this->options['cid'] = (array) $id_order; |
| 71 | |
| 72 | return $this; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Sets if the download starts automatically or not. |
| 77 | * |
| 78 | * @param boolean $enabled True for the auto-download, |
| 79 | * false to write the contents only. |
| 80 | * |
| 81 | * @return self This object to support chaining. |
| 82 | */ |
| 83 | public function setAutoDownload($enabled) |
| 84 | { |
| 85 | $this->auto_download = $enabled; |
| 86 | |
| 87 | return $this; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Forces the ID of the employee. |
| 92 | * |
| 93 | * @param integer $id_emp The ID of the employee. |
| 94 | * |
| 95 | * @return self This object to support chaining. |
| 96 | */ |
| 97 | public function setEmployee($id_emp) |
| 98 | { |
| 99 | $this->options['id_employee'] = $id_emp; |
| 100 | |
| 101 | return $this; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Sets if the CSV can contain sensitive data. |
| 106 | * For example the name of the employee (if not visible) will |
| 107 | * be shown only for an administrator. |
| 108 | * |
| 109 | * @param boolean True to allow sensitive data, otherwise false. |
| 110 | * |
| 111 | * @return self This object to support chaining. |
| 112 | */ |
| 113 | public function setAdminInterface($is) |
| 114 | { |
| 115 | $this->options['admin'] = $is; |
| 116 | |
| 117 | return $this; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Fetches the records to export. |
| 122 | * |
| 123 | * @return array The resulting array to export. |
| 124 | */ |
| 125 | public function getString() |
| 126 | { |
| 127 | return array(); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Exports the provided records using the CSV format. |
| 132 | * When the auto-download is enabled, the method will stop the flow (exit). |
| 133 | * |
| 134 | * @param array $csv The records to export, usually fetched |
| 135 | * with the getString() method. |
| 136 | * @param string $file_name The file name to use. In case the auto-download |
| 137 | * is disabled, the CSV will by written in this file (full path). |
| 138 | * |
| 139 | * @return void |
| 140 | */ |
| 141 | public function export(array $csv = array(), $file_name = '') |
| 142 | { |
| 143 | VAPLoader::import('libraries.order.export.factory'); |
| 144 | |
| 145 | // get ICS export driver |
| 146 | $driver = VAPOrderExportFactory::getInstance('csv', 'appointment', $this->options); |
| 147 | |
| 148 | // should we auto-download the CSV? |
| 149 | if ($this->auto_download) |
| 150 | { |
| 151 | // download file |
| 152 | $driver->download($file_name); |
| 153 | } |
| 154 | else |
| 155 | { |
| 156 | // export CSV |
| 157 | $csv = $driver->export(); |
| 158 | // put CSV into the specified file |
| 159 | file_put_contents($file_name, $csv); |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 |