PluginProbe
Timetics – Appointment Booking Calendar & Scheduling / 1.0.10
Timetics – Appointment Booking Calendar & Scheduling v1.0.10
1.0.61 1.0.60 1.0.59 1.0.58 1.0.57 1.0.56 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.2 1.0.20 1.0.21 1.0.22 1.0.23 1.0.24 All 62 releases
timetics / base / exporter.php

exporter.php in Timetics – Appointment Booking Calendar & Scheduling 1.0.10, at base/exporter.php

224 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Base Exporter Class
4 *
5 * @package Timetics
6 */
7 namespace Timetics\Base;
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit;
11 }
12
13 /**
14 * Exporter Class
15 */
16 abstract class Exporter {
17
18 /**
19 * Raw data to export
20 *
21 * @var array
22 */
23 protected $row_data = [];
24
25 /**
26 * Column name to export
27 *
28 * @var array
29 */
30 protected $colunms = [];
31
32 /**
33 * Store file name
34 *
35 * @var string
36 */
37 protected $file_name = '';
38
39 /**
40 * Store file format
41 *
42 * @var string
43 */
44 protected $format = '';
45
46 /**
47 * Store file type
48 *
49 * @var string
50 */
51 protected $file_type = '';
52
53 /**
54 * Prepare data that will be exported
55 *
56 * @return void
57 */
58 abstract protected function prepare_data();
59
60 /**
61 * Set file name that will be exported
62 *
63 * @return void
64 */
65 protected function set_file_name( $file_name ) {
66 $this->file_name = $file_name;
67 }
68
69 /**
70 * Set file format that will be exported
71 *
72 * @return void
73 */
74 protected function set_format( $format ) {
75 $this->format = $format;
76 }
77
78 /**
79 * Get column names
80 *
81 * @return array
82 */
83 protected function get_columns() {
84 return [];
85 }
86
87 /**
88 * Set content type
89 *
90 * @return void
91 */
92 protected function send_headers() {
93 header( 'Content-Type: application/' . $this->format . '; charset=utf-8' );
94 header( 'Content-Disposition: attachment; filename=' . $this->file_name );
95 header( 'Pragma: no-cache' );
96 header( 'Expires: 0' );
97 }
98 /**
99 * Get data to export
100 *
101 * @return array
102 */
103 protected function get_data() {
104 return $this->row_data;
105 }
106
107 /**
108 * Get columns as csv
109 *
110 * @return string
111 */
112 protected function export_columns() {
113 $colunms = $this->get_columns();
114 ob_clean();
115 $output = fopen( 'php://output', 'w' );
116 ob_start();
117
118 fputcsv( $output, $colunms );
119
120 fclose( $output );
121
122 return ob_get_clean();
123 }
124
125 /**
126 * Export rows
127 *
128 * @return string
129 */
130 protected function export_rows() {
131 $data = $this->get_data();
132 ob_clean();
133 $buffer = fopen( 'php://output', 'w' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fopen
134 ob_start();
135
136 array_walk( $data, array( $this, 'export_row' ), $buffer );
137
138 return ob_get_clean();
139 }
140
141 /**
142 * Export row
143 *
144 * @param array $row_data [$row_data description]
145 * @param [type] $key [$key description]
146 * @param file $buffer [$buffer description]
147 *
148 * @return void
149 */
150 protected function export_row( $row_data, $key, $buffer ) {
151 $colunms = $this->get_columns();
152 $export_row = [];
153
154 foreach ( $colunms as $colunm ) {
155 if ( ! empty( $row_data[$colunm] ) ) {
156 $export_row[] = $row_data[$colunm];
157 } else {
158 $export_row[] = '';
159 }
160 }
161
162 fputcsv( $buffer, $export_row );
163 }
164
165 /**
166 * Print the content that will be exported
167 *
168 * @param string $content
169 *
170 * @return void
171 */
172 protected function send_content( $content ) {
173 echo $content;
174 }
175
176 /**
177 * Export data to csv format
178 *
179 * @return void
180 */
181 public function export_csv() {
182 $this->set_file_name( "export-{$this->file_type}.csv" );
183 $this->set_format( 'csv' );
184 $this->prepare_data();
185 $this->send_headers();
186 $this->send_content( $this->export_columns() . $this->export_rows() );
187 die();
188 }
189
190 /**
191 * Export data to JSON format
192 *
193 * @return void
194 */
195 public function export_json() {
196 $this->set_file_name( "export-{$this->file_type}.json" );
197 $this->set_format( 'json' );
198 $this->prepare_data();
199 $this->send_headers();
200 $data = $this->get_data();
201
202 echo json_encode( $data, JSON_PRETTY_PRINT );
203 die();
204 }
205
206 /**
207 * Export file
208 *
209 * @return void
210 */
211 public function export() {
212 $format = $this->format;
213
214 switch ( $format ) {
215 case 'csv':
216 $this->export_csv();
217 break;
218 case 'json':
219 $this->export_json();
220 break;
221 }
222 }
223 }
224