| @@ -1,35 +1,21 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace FluentForm\App\Modules\Entries; |
| 4 | 4 | |
| 5 | -use FluentForm\App\Modules\Form\FormDataParser; | |
| 6 | -use FluentForm\App\Modules\Form\FormFieldsParser; | |
| 5 | +use FluentForm\App\Services\Transfer\TransferService; | |
| 7 | 6 | use FluentForm\Framework\Foundation\Application; |
| 8 | -use FluentForm\Framework\Helpers\ArrayHelper as Arr; | |
| 9 | 7 | |
| 8 | +/** | |
| 9 | + * @deprecated Use TransferService::exportEntries() instead. | |
| 10 | + * @todo Remove in next major version. | |
| 11 | + */ | |
| 10 | 12 | class Export |
| 11 | 13 | { |
| 12 | - /** | |
| 13 | - * @var \FluentForm\Framework\Foundation\Application | |
| 14 | - */ | |
| 15 | 14 | protected $app; |
| 16 | - | |
| 17 | - /** | |
| 18 | - * @var \FluentForm\Framework\Request\Request | |
| 19 | - */ | |
| 20 | 15 | protected $request; |
| 21 | - | |
| 22 | - /** | |
| 23 | - * @var String table/data source name | |
| 24 | - */ | |
| 25 | 16 | protected $tableName; |
| 26 | 17 | |
| 27 | - /** | |
| 28 | - * Export constructor. | |
| 29 | - * | |
| 30 | - * @param \FluentForm\Framework\Foundation\Application $application | |
| 31 | - */ | |
| 32 | 18 | public function __construct(Application $application, $tableName = 'fluentform_submissions') |
| 33 | 19 | { |
| 34 | 20 | $this->app = $application; |
| 35 | 21 | $this->request = $application->request; |
| @@ -36,183 +22,18 @@ | ||
| 36 | 22 | $this->tableName = $tableName; |
| 37 | 23 | } |
| 38 | 24 | |
| 39 | 25 | /** |
| 40 | - * Exports form entries as CSV. | |
| 41 | - * | |
| 42 | - * @todo:: refactor. | |
| 26 | + * @deprecated Use TransferService::exportEntries() instead. | |
| 43 | 27 | */ |
| 44 | 28 | public function index() |
| 45 | 29 | { |
| 46 | - $formId = intval($this->request->get('form_id')); | |
| 47 | 30 | |
| 48 | - $form = wpFluent()->table('fluentform_forms')->find($formId); | |
| 31 | + $args = $this->request->get(); | |
| 49 | 32 | |
| 50 | - if (!$form) { | |
| 51 | - exit('No Form Found'); | |
| 33 | + if ($this->tableName !== 'fluentform_submissions') { | |
| 34 | + $args['table'] = $this->tableName; | |
| 52 | 35 | } |
| 53 | 36 | |
| 54 | - $type = sanitize_text_field($this->request->get('format', 'csv')); | |
| 55 | - if (!in_array($type, ['csv', 'ods', 'xlsx', 'json'])) { | |
| 56 | - exit('Invalid requested format'); | |
| 57 | - } | |
| 58 | - | |
| 59 | - if ($type == 'json') { | |
| 60 | - $this->exportAsJSON($form); | |
| 61 | - } | |
| 62 | - | |
| 63 | - if (!defined('FLUENTFORM_DOING_CSV_EXPORT')) { | |
| 64 | - define('FLUENTFORM_DOING_CSV_EXPORT', true); | |
| 65 | - } | |
| 66 | - | |
| 67 | - $formInputs = FormFieldsParser::getEntryInputs($form, array('admin_label', 'raw')); | |
| 68 | - | |
| 69 | - $inputLabels = FormFieldsParser::getAdminLabels($form, $formInputs); | |
| 70 | - | |
| 71 | - $submissions = $this->getSubmissions($formId); | |
| 72 | - | |
| 73 | - $submissions = FormDataParser::parseFormEntries($submissions, $form, $formInputs); | |
| 74 | - $exportData = []; | |
| 75 | - | |
| 76 | - foreach ($submissions as $submission) { | |
| 77 | - $submission->response = json_decode($submission->response, true); | |
| 78 | - $temp = []; | |
| 79 | - foreach ($inputLabels as $field => $label) { | |
| 80 | - $temp[] = trim( | |
| 81 | - wp_strip_all_tags( | |
| 82 | - FormDataParser::formatValue( | |
| 83 | - Arr::get($submission->user_inputs, $field) | |
| 84 | - ) | |
| 85 | - ) | |
| 86 | - ); | |
| 87 | - } | |
| 88 | - | |
| 89 | - if ($form->has_payment && $this->tableName == 'fluentform_submissions') { | |
| 90 | - $temp[] = round($submission->payment_total / 100, 1); | |
| 91 | - $temp[] = $submission->payment_status; | |
| 92 | - $temp[] = $submission->currency; | |
| 93 | - } | |
| 94 | - | |
| 95 | - $temp[] = @$submission->id; | |
| 96 | - $temp[] = @$submission->status; | |
| 97 | - $temp[] = @$submission->created_at; | |
| 98 | - | |
| 99 | - $exportData[] = $temp; | |
| 100 | - } | |
| 101 | - | |
| 102 | - $extraLabels = []; | |
| 103 | - if ($form->has_payment && $this->tableName == 'fluentform_submissions') { | |
| 104 | - $extraLabels[] = 'payment_total'; | |
| 105 | - $extraLabels[] = 'payment_status'; | |
| 106 | - $extraLabels[] = 'currency'; | |
| 107 | - } | |
| 108 | - | |
| 109 | - $extraLabels[] = 'entry_id'; | |
| 110 | - $extraLabels[] = 'entry_status'; | |
| 111 | - $extraLabels[] = 'created_at'; | |
| 112 | - | |
| 113 | - $inputLabels = array_merge($inputLabels, $extraLabels); | |
| 114 | - | |
| 115 | - $data = array_merge([array_values($inputLabels)], $exportData); | |
| 116 | - | |
| 117 | - $data = apply_filters('fluentform_export_data', $data, $form, $exportData, $inputLabels); | |
| 118 | - | |
| 119 | - $fileName = sanitize_title($form->title, 'export', 'view') . '-' . date('Y-m-d'); | |
| 120 | - | |
| 121 | - $this->downloadOfficeDoc($data, $type, $fileName); | |
| 122 | - } | |
| 123 | - | |
| 124 | - | |
| 125 | - private function downloadOfficeDoc($data, $type = 'csv', $fileName = null) | |
| 126 | - { | |
| 127 | - $data = array_map(function ($item) { | |
| 128 | - return array_map(function ($itemValue) { | |
| 129 | - if (is_array($itemValue)) { | |
| 130 | - return implode(', ', $itemValue); | |
| 131 | - } | |
| 132 | - return $itemValue; | |
| 133 | - }, $item); | |
| 134 | - }, $data); | |
| 135 | - require_once $this->app->appPath() . 'Services/Spout/Autoloader/autoload.php'; | |
| 136 | - $fileName = ($fileName) ? $fileName . '.' . $type : 'export-data-' . date('d-m-Y') . '.' . $type; | |
| 137 | - $writer = \Box\Spout\Writer\WriterFactory::create($type); | |
| 138 | - $writer->openToBrowser($fileName); | |
| 139 | - $writer->addRows($data); | |
| 140 | - $writer->close(); | |
| 141 | - die(); | |
| 142 | - } | |
| 143 | - | |
| 144 | - private function exportAsJSON($form) | |
| 145 | - { | |
| 146 | - $formInputs = FormFieldsParser::getEntryInputs($form, array('admin_label', 'raw')); | |
| 147 | - | |
| 148 | - $inputLabels = FormFieldsParser::getAdminLabels($form, $formInputs); | |
| 149 | - | |
| 150 | - $submissions = $this->getSubmissions($form->id); | |
| 151 | - | |
| 152 | - $submissions = FormDataParser::parseFormEntries($submissions, $form, $formInputs); | |
| 153 | - $exportData = []; | |
| 154 | - | |
| 155 | - foreach ($submissions as $submission) { | |
| 156 | - $submission->response = json_decode($submission->response, true); | |
| 157 | - } | |
| 158 | - | |
| 159 | - header('Content-disposition: attachment; filename=' . sanitize_title($form->title, 'export', 'view') . '-' . date('Y-m-d') . '.json'); | |
| 160 | - header('Content-type: application/json'); | |
| 161 | - echo json_encode($submissions); | |
| 162 | - exit(); | |
| 163 | - } | |
| 164 | - | |
| 165 | - private function getSubmissions($formId) | |
| 166 | - { | |
| 167 | - $query = wpFluent()->table($this->tableName) | |
| 168 | - ->where('form_id', $formId) | |
| 169 | - ->orderBy('id', $this->request->get('sort_by', 'DESC')); | |
| 170 | - | |
| 171 | - if ($this->tableName == 'fluentform_submissions') { | |
| 172 | - $dateRange = $this->request->get('date_range'); | |
| 173 | - if ($dateRange) { | |
| 174 | - $query->where('created_at', '>=', $dateRange[0] . ' 00:00:01'); | |
| 175 | - $query->where('created_at', '<=', $dateRange[1] . ' 23:59:59'); | |
| 176 | - } | |
| 177 | - | |
| 178 | - $isFavourite = $this->request->get('is_favourite'); | |
| 179 | - | |
| 180 | - if ($isFavourite == 'yes') { | |
| 181 | - $query->where('is_favourite', '1'); | |
| 182 | - } | |
| 183 | - | |
| 184 | - $status = $this->request->get('entry_type'); | |
| 185 | - | |
| 186 | - if ($status == 'trashed') { | |
| 187 | - $query->where('status', 'trashed'); | |
| 188 | - } else if ($status && $status != 'all') { | |
| 189 | - $query->where('status', $status); | |
| 190 | - } else { | |
| 191 | - $query->where('status', '!=', 'trashed'); | |
| 192 | - } | |
| 193 | - | |
| 194 | - if ($paymentStatuses = $this->request->get('payment_statuses')) { | |
| 195 | - if (is_array($paymentStatuses)) { | |
| 196 | - $query->whereIn('payment_status', $paymentStatuses); | |
| 197 | - } | |
| 198 | - } | |
| 199 | - | |
| 200 | - } | |
| 201 | - | |
| 202 | - $searchString = $this->request->get('search'); | |
| 203 | - | |
| 204 | - if ($searchString) { | |
| 205 | - $query->where(function ($q) use ($searchString) { | |
| 206 | - $q->where('id', 'LIKE', "%{$searchString}%") | |
| 207 | - ->orWhere('response', 'LIKE', "%{$searchString}%"); | |
| 208 | - | |
| 209 | - if ($this->tableName == 'fluentform_submissions') { | |
| 210 | - $q->orWhere('status', 'LIKE', "%{$searchString}%") | |
| 211 | - ->orWhere('created_at', 'LIKE', "%{$searchString}%"); | |
| 212 | - } | |
| 213 | - }); | |
| 214 | - } | |
| 215 | - | |
| 216 | - return $query->get(); | |
| 37 | + TransferService::exportEntries($args); | |
| 217 | 38 | } |
| 218 | 39 | } |