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('sortablejs', Quform::adminUrl('js/Sortable.min.js'), array(), '1.15.7', true);
69: wp_enqueue_script('quform-tools-export-entries', Quform::adminUrl('js/tools.export-entries.min.js'), array('jquery', 'sortablejs'), QUFORM_VERSION, true);
70: wp_localize_script('quform-tools-export-entries', 'quformToolsExportEntriesL10n', $this->getScriptL10n());
71: }
72:
73: 74: 75: 76: 77:
78: protected function getScriptL10n()
79: {
80: return array(
81: 'exportEntriesNonce' => wp_create_nonce('quform_export_entries'),
82: 'errorExportingEntries' => __('An error occurred exporting entries', 'quform')
83: );
84: }
85:
86: 87: 88: 89: 90:
91: protected function getAdminTitle()
92: {
93: return __('Export Entries', 'quform');
94: }
95:
96: 97: 98: 99: 100: 101: 102:
103: public function getNavHtml(?array $currentForm = null, array $extra = array())
104: {
105: $extra[40] = sprintf(
106: '<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>',
107: esc_html__('Export entries', 'quform')
108: );
109:
110: return parent::getNavHtml($currentForm, $extra);
111: }
112:
113: 114: 115:
116: public function process()
117: {
118: if ( ! current_user_can('quform_export_entries')) {
119: wp_die(__( 'You do not have sufficient permissions to access this page.', 'quform'), 403);
120: }
121:
122: if (Quform::isPostRequest() && Quform::get($_POST, 'qfb_do_entries_export')) {
123: $_POST = wp_unslash($_POST);
124:
125: if ( ! wp_verify_nonce(Quform::get($_POST, '_wpnonce'), 'quform_export_entries')) {
126: $this->addMessage('error', __('Nonce check failed', 'quform'));
127: return;
128: }
129:
130: $this->saveFormatSettings();
131: $this->processExport();
132: } else {
133: if ( ! extension_loaded('zip')) {
134: $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'));
135: }
136: }
137:
138: $active = apply_filters('quform_export_entries_show_inactive', true) ? null : true;
139: $orderBy = get_user_meta(get_current_user_id(), 'quform_forms_order_by', true);
140: $order = get_user_meta(get_current_user_id(), 'quform_forms_order', true);
141:
142: $this->view->with(array(
143: 'formatSettings' => $this->loadFormatSettings(),
144: 'forms' => $this->repository->formsToSelectArray($active, $orderBy, $order)
145: ));
146: }
147:
148: 149: 150:
151: protected function processExport()
152: {
153: $id = (int) Quform::get($_POST, 'qfb_form_id');
154:
155: if ( ! is_numeric($id)) {
156: $this->addMessage('error', __('Select a form', 'quform'));
157: return;
158: }
159:
160: $config = $this->repository->getConfig($id);
161:
162: if ( ! is_array($config)) {
163: $this->addMessage('error', __('The selected form does not exist', 'quform'));
164: return;
165: }
166:
167: $this->exporter->generateExportFile(
168: $this->formFactory->create($config),
169: $this->sanitizeColumns(Quform::get($_POST, 'qfb_columns')),
170: $this->getPreparedFormatSettings(),
171: $this->sanitizeDate(Quform::get($_POST, 'qfb_date_from')),
172: $this->sanitizeDate(Quform::get($_POST, 'qfb_date_to'))
173: );
174: }
175:
176: 177: 178: 179: 180: 181:
182: protected function sanitizeColumns($columns)
183: {
184: if ( ! is_array($columns)) {
185: $columns = array();
186: }
187:
188: foreach ($columns as $key => $column) {
189: $columns[$key] = sanitize_text_field($columns[$key]);
190: }
191:
192: return $columns;
193: }
194:
195: 196: 197: 198: 199: 200: 201:
202: protected function sanitiseColumns($columns)
203: {
204: _deprecated_function(__METHOD__, '2.4.0', 'Quform_Admin_Page_Tools_ExportEntries::sanitizeColumns()');
205:
206: return $this->sanitizeColumns($columns);
207: }
208:
209: 210: 211: 212: 213: 214:
215: protected function sanitizeDate($date)
216: {
217: $sanitized = '';
218:
219: if (Quform::isNonEmptyString($date) && preg_match('/^\d{4}-\d{2}-\d{2}$/', $date)) {
220: $sanitized = $date;
221: }
222:
223: return $sanitized;
224: }
225:
226: 227: 228: 229: 230: 231: 232:
233: protected function sanitiseDate($date)
234: {
235: _deprecated_function(__METHOD__, '2.4.0', 'Quform_Admin_Page_Tools_ExportEntries::sanitizeDate()');
236:
237: return $this->sanitizeDate($date);
238: }
239:
240: 241: 242: 243: 244: 245: 246:
247: protected function loadFormatSettings()
248: {
249: $userSettings = get_user_meta(get_current_user_id(), 'quform_export_entries_format_settings', true);
250:
251: if ( ! is_array($userSettings)) {
252: $userSettings = array();
253: }
254:
255: $settings = array(
256: 'type' => Quform::get($userSettings, 'type', 'csv'),
257: 'excelCompatibility' => Quform::get($userSettings, 'excelCompatibility', false),
258: 'delimiter' => Quform::get($userSettings, 'delimiter', 'comma'),
259: 'delimiterCustom' => Quform::get($userSettings, 'delimiterCustom', ''),
260: 'enclosure' => Quform::get($userSettings, 'enclosure', 'double-quotes'),
261: 'enclosureCustom' => Quform::get($userSettings, 'enclosureCustom', ''),
262: 'useBom' => Quform::get($userSettings, 'useBom', false),
263: 'lineEndings' => Quform::get($userSettings, 'lineEndings', 'windows')
264: );
265:
266: return $settings;
267: }
268:
269: 270: 271:
272: protected function saveFormatSettings()
273: {
274: update_user_meta(get_current_user_id(), 'quform_export_entries_format_settings', $this->getSanitizedFormatSettings());
275: }
276:
277: 278: 279: 280: 281:
282: protected function getSanitizedFormatSettings()
283: {
284: return array(
285: 'type' => sanitize_key(Quform::get($_POST, 'qfb_format_type')),
286: 'excelCompatibility' => Quform::get($_POST, 'qfb_format_csv_excel_compatibility') == '1',
287: 'delimiter' => sanitize_key(Quform::get($_POST, 'qfb_format_csv_delimiter')),
288: 'delimiterCustom' => sanitize_text_field(Quform::get($_POST, 'qfb_format_csv_delimiter_custom')),
289: 'enclosure' => sanitize_key(Quform::get($_POST, 'qfb_format_csv_enclosure')),
290: 'enclosureCustom' => sanitize_text_field(Quform::get($_POST, 'qfb_format_csv_enclosure_custom')),
291: 'useBom' => Quform::get($_POST, 'qfb_format_csv_use_bom') == '1',
292: 'lineEndings' => sanitize_key(Quform::get($_POST, 'qfb_format_csv_line_endings'))
293: );
294: }
295:
296: 297: 298: 299: 300: 301:
302: protected function getSanitisedFormatSettings()
303: {
304: _deprecated_function(__METHOD__, '2.4.0', 'Quform_Admin_Page_Tools_ExportEntries::getSanitizedFormatSettings()');
305:
306: return $this->getSanitizedFormatSettings();
307: }
308:
309: 310: 311: 312: 313:
314: protected function getPreparedFormatSettings()
315: {
316: $formatSettings = $this->getSanitizedFormatSettings();
317:
318: switch ($formatSettings['delimiter']) {
319: default:
320: case 'comma':
321: $formatSettings['delimiter'] = ',';
322: break;
323: case 'semicolon':
324: $formatSettings['delimiter'] = ';';
325: break;
326: case 'tab':
327: $formatSettings['delimiter'] = "\t";
328: break;
329: case 'space':
330: $formatSettings['delimiter'] = ' ';
331: break;
332: case 'custom':
333: $formatSettings['delimiter'] = $formatSettings['delimiterCustom'];
334: break;
335: }
336:
337: unset($formatSettings['delimiterCustom']);
338:
339: switch ($formatSettings['enclosure']) {
340: default:
341: case 'double-quotes':
342: $formatSettings['enclosure'] = '"';
343: break;
344: case 'single-quotes':
345: $formatSettings['enclosure'] = "'";
346: break;
347: case 'custom':
348: $formatSettings['enclosure'] = $formatSettings['enclosureCustom'];
349: break;
350: }
351:
352: unset($formatSettings['enclosureCustom']);
353:
354: $formatSettings['lineEndings'] = $formatSettings['lineEndings'] == 'windows' ? "\r\n" : "\n";
355:
356: return $formatSettings;
357: }
358: }
359: