ics.php
216 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 ICS format. |
| 16 | * |
| 17 | * The ICS format is compatible with any calendar client/cloud service, |
| 18 | * such as Apple iCal or Google Calendar. |
| 19 | * |
| 20 | * Since Microsoft Outlook Calendar uses non-standard syntax, this ICS |
| 21 | * may not display the correct timezone when the DST is on. |
| 22 | * |
| 23 | * @deprecated 1.8 Use VAPOrderExportFactory instead. |
| 24 | */ |
| 25 | class VikExporterICS |
| 26 | { |
| 27 | /** |
| 28 | * An array of options. |
| 29 | * |
| 30 | * @var array |
| 31 | */ |
| 32 | private $options; |
| 33 | |
| 34 | /** |
| 35 | * Flag to start the auto-download or not. |
| 36 | * |
| 37 | * @var boolean |
| 38 | */ |
| 39 | private $auto_download = true; |
| 40 | |
| 41 | /** |
| 42 | * Class constructor. |
| 43 | * |
| 44 | * @param integer $from_ts The starting timestamp. |
| 45 | * @param integer $to_ts The ending timestamp. |
| 46 | * @param integer $id_emp The employee ID. |
| 47 | */ |
| 48 | public function __construct($from_ts, $to_ts, $id_emp) |
| 49 | { |
| 50 | if (is_int($from_ts)) |
| 51 | { |
| 52 | $from_ts = date('Y-m-d', $from_ts); |
| 53 | } |
| 54 | |
| 55 | if (is_int($to_ts)) |
| 56 | { |
| 57 | $to_ts = date('Y-m-d', $to_ts); |
| 58 | } |
| 59 | |
| 60 | $this->options = array( |
| 61 | 'fromdate' => $from_ts, |
| 62 | 'todate' => $to_ts, |
| 63 | 'id_employee' => $id_emp, |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Sets the ICS heading information. |
| 69 | * |
| 70 | * @param string $version The ICS syntax version. |
| 71 | * @param string $calscale The scale of the calendar. |
| 72 | * @param string $timezone The timezone to use. |
| 73 | */ |
| 74 | public function setHeader($version = '2.0', $calscale = 'GREGORIAN', $timezone = null) |
| 75 | { |
| 76 | $this->head = "VERSION:{$version}\n"; |
| 77 | $this->head .= "PRODID:-//e4j//VikAppointments " . VIKAPPOINTMENTS_SOFTWARE_VERSION . "//EN\n"; |
| 78 | $this->head .= "CALSCALE:{$calscale}\n"; |
| 79 | |
| 80 | if (empty($timezone)) |
| 81 | { |
| 82 | $timezone = date_default_timezone_get(); |
| 83 | } |
| 84 | |
| 85 | $this->head .= "X-WR-TIMEZONE:{$timezone}\n"; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Forces the ID of the order to fetch. |
| 90 | * |
| 91 | * @param integer $id_order The order ID. |
| 92 | * |
| 93 | * @return self This object to support chaining. |
| 94 | */ |
| 95 | public function forceOrderID($id_order) |
| 96 | { |
| 97 | $this->options['cid'] = (array) $id_order; |
| 98 | |
| 99 | return $this; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Sets if the download starts automatically or not. |
| 104 | * |
| 105 | * @param boolean $enabled True for the auto-download, |
| 106 | * false to write the contents only. |
| 107 | * |
| 108 | * @return self This object to support chaining. |
| 109 | */ |
| 110 | public function setAutoDownload($enabled) |
| 111 | { |
| 112 | $this->auto_download = $enabled; |
| 113 | |
| 114 | return $this; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Forces the ID of the employee. |
| 119 | * |
| 120 | * @param integer $id_emp The ID of the employee. |
| 121 | * |
| 122 | * @return self This object to support chaining. |
| 123 | */ |
| 124 | public function setEmployee($id_emp) |
| 125 | { |
| 126 | $this->options['id_employee'] = $id_emp; |
| 127 | |
| 128 | return $this; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Used to set a different title depending on the client. |
| 133 | * For administrator: SERVICE_NAME - FIRST_NAME LAST_NAME |
| 134 | * For customers: SERVICE_NAME |
| 135 | * |
| 136 | * Also used to display sensitive data or not. |
| 137 | * For example the name of the employee (if not visible) will |
| 138 | * be shown only for an administrator. |
| 139 | * |
| 140 | * @param boolean True for administrator, otherwise false. |
| 141 | * |
| 142 | * @return self This object to support chaining. |
| 143 | */ |
| 144 | public function setAdminInterface($is) |
| 145 | { |
| 146 | $this->options['admin'] = $is; |
| 147 | |
| 148 | return $this; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Fetches the records to export. |
| 153 | * |
| 154 | * @return string The resulting string to export. |
| 155 | */ |
| 156 | public function getString() |
| 157 | { |
| 158 | VAPLoader::import('libraries.order.export.factory'); |
| 159 | |
| 160 | // get ICS export driver |
| 161 | $driver = VAPOrderExportFactory::getInstance('ics', 'appointment', $this->options); |
| 162 | |
| 163 | // export string |
| 164 | return $driver->export(); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Exports the provided ICS string in the apposite format. |
| 169 | * When the auto-download is enabled, the method will stop the flow (exit). |
| 170 | * |
| 171 | * @param string $ics The ICS string to export, usually fetched |
| 172 | * with the getString() method. |
| 173 | * @param string $file_name The file name to use. In case the auto-download |
| 174 | * is disabled, the ICS will by written in this file (full path). |
| 175 | * |
| 176 | * @return void |
| 177 | */ |
| 178 | public function export($ics = '', $file_name = '') |
| 179 | { |
| 180 | if ($this->auto_download) |
| 181 | { |
| 182 | header("Content-Type: application/octet-stream;"); |
| 183 | header("Content-Disposition: attachment;filename=\"{$file_name}\""); |
| 184 | header("Cache-Control: no-store, no-cache"); |
| 185 | |
| 186 | $f = fopen('php://output', 'w'); |
| 187 | fwrite($f, $ics); |
| 188 | fclose($f); |
| 189 | |
| 190 | exit; |
| 191 | } |
| 192 | else |
| 193 | { |
| 194 | $f = fopen($file_name, 'w+'); |
| 195 | $w = fwrite($f, $ics); |
| 196 | fclose($f); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Displays the ICS string on a blank web page. |
| 202 | * |
| 203 | * @param string $ics The ICS string to export, usually fetched |
| 204 | * with the getString() method. |
| 205 | * @param string $file_name The file name to use. |
| 206 | * |
| 207 | * @return void |
| 208 | */ |
| 209 | public function renderBrowser($ics, $file_name) |
| 210 | { |
| 211 | header("Content-Type: text/calendar; charset=utf-8"); |
| 212 | header("Content-Disposition: attachment; filename=\"{$file_name}\""); |
| 213 | echo $ics; |
| 214 | } |
| 215 | } |
| 216 |