base-export-controller.php
2 years ago
multiple-controller.php
2 months ago
single-controller.php
2 years ago
single-controller.php
80 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace JFB_Modules\Form_Record\Export; |
| 5 | |
| 6 | use JFB_Modules\Form_Record\Query_Views\Record_View; |
| 7 | use JFB_Modules\Form_Record\Tools; |
| 8 | |
| 9 | // If this file is called directly, abort. |
| 10 | if ( ! defined( 'WPINC' ) ) { |
| 11 | die; |
| 12 | } |
| 13 | |
| 14 | class Single_Controller extends Base_Export_Controller { |
| 15 | |
| 16 | protected $record_id; |
| 17 | protected $record; |
| 18 | protected $fields; |
| 19 | |
| 20 | /** |
| 21 | * @throws \Exception |
| 22 | */ |
| 23 | public function do_export() { |
| 24 | $this->record_id = $this->get_record_id(); |
| 25 | |
| 26 | $record_view = Record_View::findOne( array( 'id' => $this->record_id ) ); |
| 27 | $record_view->set_select( array_keys( $this->extra_columns ) ); |
| 28 | |
| 29 | $this->record = $record_view->query()->query_one(); |
| 30 | |
| 31 | // set fields & request |
| 32 | Tools::apply_context( $this->record ); |
| 33 | |
| 34 | $fields_columns = $this->get_field_columns(); |
| 35 | |
| 36 | $this->get_exporter()->set_title( $this->get_file_name() ); |
| 37 | $this->get_exporter()->open(); |
| 38 | |
| 39 | // headings |
| 40 | $this->get_exporter()->add_row( $this->prepare_row( $fields_columns, $this->extra_columns ) ); |
| 41 | |
| 42 | foreach ( jet_fb_context()->iterate_values_table() as $row ) { |
| 43 | $this->get_exporter()->add_row( |
| 44 | $this->prepare_row( iterator_to_array( $row ), $this->record ) |
| 45 | ); |
| 46 | } |
| 47 | |
| 48 | $this->get_exporter()->close(); |
| 49 | die; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * @return int |
| 54 | * @throws \Exception |
| 55 | */ |
| 56 | protected function get_record_id(): int { |
| 57 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 58 | $record_id = absint( $_GET['id'] ?? '' ); |
| 59 | |
| 60 | if ( ! $record_id ) { |
| 61 | throw new \Exception( |
| 62 | esc_html__( 'Record ID is empty', 'jet-form-builder' ) |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | return $record_id; |
| 67 | } |
| 68 | |
| 69 | protected function get_file_name(): string { |
| 70 | return sprintf( |
| 71 | /* translators: %1$s - form title, %2$d - record ID */ |
| 72 | __( '%1$s record (%2$d)', 'jet-form-builder' ), |
| 73 | get_the_title( $this->record['form_id'] ), |
| 74 | $this->record_id |
| 75 | ); |
| 76 | } |
| 77 | |
| 78 | |
| 79 | } |
| 80 |