1: <?php
2:
3: 4: 5:
6: class Quform_Admin_Page_Tools_ExportEntries extends Quform_Admin_Page_Tools
7: {
8: 9: 10:
11: protected $repository;
12:
13: 14: 15:
16: protected $exporter;
17:
18: 19: 20:
21: protected $formFactory;
22:
23: 24: 25: 26: 27: 28:
29: public function __construct(Quform_ViewFactory $viewFactory, Quform_Repository $repository,
30: Quform_Entry_Exporter $exporter, Quform_Form_Factory $formFactory)
31: {
32: parent::__construct($viewFactory, $repository);
33:
34: $this->exporter = $exporter;
35: $this->formFactory = $formFactory;
36: }
37:
38: public function init()
39: {
40: $this->template = QUFORM_TEMPLATE_PATH . '/admin/tools/export-entries.php';
41: }
42:
43: 44: 45:
46: protected function enqueueStyles()
47: {
48: wp_enqueue_style('qtip2', Quform::url('css/jquery.qtip.min.css'), array(), '3.0.4');
49: wp_enqueue_style('kendo-common-material', Quform::url('css/kendo.common-material.min.css'), array(), '2020.2.617');
50: wp_enqueue_style('kendo-material', Quform::url('css/kendo.material.min.css'), array(), '2020.2.617');
51:
52: parent::enqueueStyles();
53: }
54:
55: 56: 57:
58: protected function enqueueScripts()
59: {
60: wp_enqueue_script('qtip2', Quform::url('js/jquery.qtip.min.js'), array('jquery'), '3.0.4', true);
61: wp_enqueue_script('kendo-core', Quform::url('js/kendo.core.min.js'), array('jquery'), '2020.2.617', true);
62: wp_enqueue_script('kendo-calendar', Quform::url('js/kendo.calendar.min.js'), array('kendo-core'), '2020.2.617', true);
63: wp_enqueue_script('kendo-popup', Quform::url('js/kendo.popup.min.js'), array('kendo-core'), '2020.2.617', true);
64: wp_enqueue_script('kendo-datepicker', Quform::url('js/kendo.datepicker.min.js'), array('kendo-core', 'kendo-popup', 'kendo-calendar'), '2020.2.617', true);
65:
66: parent::enqueueScripts();
67:
68: wp_enqueue_script('quform-tools-export-entries', Quform::adminUrl('js/tools.export-entries.min.js'), array('jquery', 'jquery-ui-sortable'), QUFORM_VERSION, true);
69: wp_localize_script('quform-tools-export-entries', 'quformToolsExportEntriesL10n', $this->getScriptL10n());
70: }
71:
72: 73: 74: 75: 76:
77: protected function getScriptL10n()
78: {
79: return array(
80: 'exportEntriesNonce' => wp_create_nonce('quform_export_entries'),
81: 'errorExportingEntries' => __('An error occurred exporting entries', 'quform')
82: );
83: }
84:
85: 86: 87: 88: 89:
90: protected function getAdminTitle()
91: {
92: return __('Export Entries', 'quform');
93: }
94:
95: 96: 97: 98: 99: 100: 101:
102: public function getNavHtml(array $currentForm = null, array $extra = array())
103: {
104: $extra[40] = sprintf(
105: '<div class="qfb-nav-item qfb-nav-page-info"><i class="qfb-nav-page-icon qfb-icon qfb-icon-file-excel-o"></i><span class="qfb-nav-page-title">%s</span></div>',
106: esc_html__('Export entries', 'quform')
107: );
108:
109: return parent::getNavHtml($currentForm, $extra);
110: }
111:
112: 113: 114:
115: public function process()
116: {
117: if ( ! current_user_can('quform_export_entries')) {
118: wp_die(__( 'You do not have sufficient permissions to access this page.', 'quform'), 403);
119: }
120:
121: if (Quform::isPostRequest() && Quform::get($_POST, 'qfb_do_entries_export')) {
122: $_POST = wp_unslash($_POST);
123:
124: if ( ! wp_verify_nonce(Quform::get($_POST, '_wpnonce'), 'quform_export_entries')) {
125: $this->addMessage('error', __('Nonce check failed', 'quform'));
126: return;
127: }
128:
129: $this->saveFormatSettings();
130: $this->processExport();
131: } else {
132: if ( ! extension_loaded('zip')) {
133: $this->addMessage('error', __('The Zip extension for PHP was not detected. The exporter requires that this extension is enabled on the server, find out more at Forms → Settings → Tweaks & Troubleshooting → Server Compatibility.', 'quform'));
134: }
135: }
136:
137: $active = apply_filters('quform_export_entries_show_inactive', true) ? null : true;
138: $orderBy = get_user_meta(get_current_user_id(), 'quform_forms_order_by', true);
139: $order = get_user_meta(get_current_user_id(), 'quform_forms_order', true);
140:
141: $this->view->with(array(
142: 'formatSettings' => $this->loadFormatSettings(),
143: 'forms' => $this->repository->formsToSelectArray($active, $orderBy, $order)
144: ));
145: }
146:
147: 148: 149:
150: protected function processExport()
151: {
152: $id = (int) Quform::get($_POST, 'qfb_form_id');
153:
154: if ( ! is_numeric($id)) {
155: $this->addMessage('error', __('Select a form', 'quform'));
156: return;
157: }
158:
159: $config = $this->repository->getConfig($id);
160:
161: if ( ! is_array($config)) {
162: $this->addMessage('error', __('The selected form does not exist', 'quform'));
163: return;
164: }
165:
166: $this->exporter->generateExportFile(
167: $this->formFactory->create($config),
168: $this->sanitizeColumns(Quform::get($_POST, 'qfb_columns')),
169: $this->getPreparedFormatSettings(),
170: $this->sanitizeDate(Quform::get($_POST, 'qfb_date_from')),
171: $this->sanitizeDate(Quform::get($_POST, 'qfb_date_to'))
172: );
173: }
174:
175: 176: 177: 178: 179: 180:
181: protected function sanitizeColumns($columns)
182: {
183: if ( ! is_array($columns)) {
184: $columns = array();
185: }
186:
187: foreach ($columns as $key => $column) {
188: $columns[$key] = sanitize_text_field($columns[$key]);
189: }
190:
191: return $columns;
192: }
193:
194: 195: 196: 197: 198: 199: 200:
201: protected function sanitiseColumns($columns)
202: {
203: _deprecated_function(__METHOD__, '2.4.0', 'Quform_Admin_Page_Tools_ExportEntries::sanitizeColumns()');
204:
205: return $this->sanitizeColumns($columns);
206: }
207:
208: 209: 210: 211: 212: 213:
214: protected function sanitizeDate($date)
215: {
216: $sanitized = '';
217:
218: if (Quform::isNonEmptyString($date) && preg_match('/^\d{4}-\d{2}-\d{2}$/', $date)) {
219: $sanitized = $date;
220: }
221:
222: return $sanitized;
223: }
224:
225: 226: 227: 228: 229: 230: 231:
232: protected function sanitiseDate($date)
233: {
234: _deprecated_function(__METHOD__, '2.4.0', 'Quform_Admin_Page_Tools_ExportEntries::sanitizeDate()');
235:
236: return $this->sanitizeDate($date);
237: }
238:
239: 240: 241: 242: 243: 244: 245:
246: protected function loadFormatSettings()
247: {
248: $userSettings = get_user_meta(get_current_user_id(), 'quform_export_entries_format_settings', true);
249:
250: if ( ! is_array($userSettings)) {
251: $userSettings = array();
252: }
253:
254: $settings = array(
255: 'type' => Quform::get($userSettings, 'type', 'csv'),
256: 'excelCompatibility' => Quform::get($userSettings, 'excelCompatibility', false),
257: 'delimiter' => Quform::get($userSettings, 'delimiter', 'comma'),
258: 'delimiterCustom' => Quform::get($userSettings, 'delimiterCustom', ''),
259: 'enclosure' => Quform::get($userSettings, 'enclosure', 'double-quotes'),
260: 'enclosureCustom' => Quform::get($userSettings, 'enclosureCustom', ''),
261: 'useBom' => Quform::get($userSettings, 'useBom', false),
262: 'lineEndings' => Quform::get($userSettings, 'lineEndings', 'windows')
263: );
264:
265: return $settings;
266: }
267:
268: 269: 270:
271: protected function saveFormatSettings()
272: {
273: update_user_meta(get_current_user_id(), 'quform_export_entries_format_settings', $this->getSanitizedFormatSettings());
274: }
275:
276: 277: 278: 279: 280:
281: protected function getSanitizedFormatSettings()
282: {
283: return array(
284: 'type' => sanitize_key(Quform::get($_POST, 'qfb_format_type')),
285: 'excelCompatibility' => Quform::get($_POST, 'qfb_format_csv_excel_compatibility') == '1',
286: 'delimiter' => sanitize_key(Quform::get($_POST, 'qfb_format_csv_delimiter')),
287: 'delimiterCustom' => sanitize_text_field(Quform::get($_POST, 'qfb_format_csv_delimiter_custom')),
288: 'enclosure' => sanitize_key(Quform::get($_POST, 'qfb_format_csv_enclosure')),
289: 'enclosureCustom' => sanitize_text_field(Quform::get($_POST, 'qfb_format_csv_enclosure_custom')),
290: 'useBom' => Quform::get($_POST, 'qfb_format_csv_use_bom') == '1',
291: 'lineEndings' => sanitize_key(Quform::get($_POST, 'qfb_format_csv_line_endings'))
292: );
293: }
294:
295: 296: 297: 298: 299: 300:
301: protected function getSanitisedFormatSettings()
302: {
303: _deprecated_function(__METHOD__, '2.4.0', 'Quform_Admin_Page_Tools_ExportEntries::getSanitizedFormatSettings()');
304:
305: return $this->getSanitizedFormatSettings();
306: }
307:
308: 309: 310: 311: 312:
313: protected function getPreparedFormatSettings()
314: {
315: $formatSettings = $this->getSanitizedFormatSettings();
316:
317: switch ($formatSettings['delimiter']) {
318: default:
319: case 'comma':
320: $formatSettings['delimiter'] = ',';
321: break;
322: case 'semicolon':
323: $formatSettings['delimiter'] = ';';
324: break;
325: case 'tab':
326: $formatSettings['delimiter'] = "\t";
327: break;
328: case 'space':
329: $formatSettings['delimiter'] = ' ';
330: break;
331: case 'custom':
332: $formatSettings['delimiter'] = $formatSettings['delimiterCustom'];
333: break;
334: }
335:
336: unset($formatSettings['delimiterCustom']);
337:
338: switch ($formatSettings['enclosure']) {
339: default:
340: case 'double-quotes':
341: $formatSettings['enclosure'] = '"';
342: break;
343: case 'single-quotes':
344: $formatSettings['enclosure'] = "'";
345: break;
346: case 'custom':
347: $formatSettings['enclosure'] = $formatSettings['enclosureCustom'];
348: break;
349: }
350:
351: unset($formatSettings['enclosureCustom']);
352:
353: $formatSettings['lineEndings'] = $formatSettings['lineEndings'] == 'windows' ? "\r\n" : "\n";
354:
355: return $formatSettings;
356: }
357: }
358: