|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Source manager for local XLSX files. |
| 4 | + * |
| 5 | + * Reads the first sheet of an XLSX file using the OpenSpout library, |
| 6 | + * which is already bundled as a dependency of this plugin. |
| 7 | + * Legacy .xls (BIFF) files are not supported; OpenSpout's XLSX reader |
| 8 | + * only handles the Office Open XML (.xlsx) format. |
| 9 | + * |
| 10 | + * Expected sheet layout (same convention as CSV import): |
| 11 | + * Row 1 – column labels |
| 12 | + * Row 2 – column types (string, number, boolean, date, datetime, timeofday) |
| 13 | + * Row 3+ – data rows |
| 14 | + * |
| 15 | + * @category Visualizer |
| 16 | + * @package Source |
| 17 | + * |
| 18 | + * @since 3.11.0 |
| 19 | + */ |
| 20 | +class Visualizer_Source_Xlsx extends Visualizer_Source { |
| 21 | + |
| 22 | + /** |
| 23 | + * The path to the XLSX file. |
| 24 | + * |
| 25 | + * @access protected |
| 26 | + * @var string |
| 27 | + */ |
| 28 | + protected $_filename; |
| 29 | + |
| 30 | + /** |
| 31 | + * Constructor. |
| 32 | + * |
| 33 | + * @access public |
| 34 | + * @param string $filename Path to the XLSX file. |
| 35 | + */ |
| 36 | + public function __construct( $filename = '' ) { |
| 37 | + $this->_filename = trim( (string) $filename ); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Fetches information from the XLSX file and builds the series/data arrays. |
| 42 | + * |
| 43 | + * @access public |
| 44 | + * @return boolean TRUE on success, FALSE on failure. |
| 45 | + */ |
| 46 | + public function fetch() { |
| 47 | + if ( empty( $this->_filename ) ) { |
| 48 | + $this->_error = esc_html__( 'No file provided. Please try again.', 'visualizer' ); |
| 49 | + return false; |
| 50 | + } |
| 51 | + |
| 52 | + // Ensure the OpenSpout autoloader is available. |
| 53 | + $vendor_file = VISUALIZER_ABSPATH . 'vendor/autoload.php'; |
| 54 | + if ( is_readable( $vendor_file ) ) { |
| 55 | + include_once $vendor_file; |
| 56 | + } |
| 57 | + |
| 58 | + if ( ! class_exists( 'OpenSpout\Reader\Common\Creator\ReaderEntityFactory' ) ) { |
| 59 | + $this->_error = esc_html__( 'The OpenSpout library is required to import XLSX files but could not be found. Please contact support.', 'visualizer' ); |
| 60 | + return false; |
| 61 | + } |
| 62 | + |
| 63 | + $reader = \OpenSpout\Reader\Common\Creator\ReaderEntityFactory::createXLSXReader(); |
| 64 | + try { |
| 65 | + $reader->open( $this->_get_file_path() ); |
| 66 | + |
| 67 | + $all_rows = array(); |
| 68 | + foreach ( $reader->getSheetIterator() as $sheet ) { |
| 69 | + foreach ( $sheet->getRowIterator() as $row ) { |
| 70 | + $row_data = array(); |
| 71 | + foreach ( $row->getCells() as $cell ) { |
| 72 | + $value = $cell->getValue(); |
| 73 | + // Convert non-string scalars to string for uniform handling; |
| 74 | + // _normalizeData() will cast them to the correct type later. |
| 75 | + $row_data[] = is_null( $value ) ? null : (string) $value; |
| 76 | + } |
| 77 | + $all_rows[] = $row_data; |
| 78 | + } |
| 79 | + break; // Only read the first sheet. |
| 80 | + } |
| 81 | + } catch ( \Exception $e ) { |
| 82 | + $reader->close(); |
| 83 | + $this->_error = sprintf( |
| 84 | + /* translators: %s - the exception message. */ |
| 85 | + esc_html__( 'Could not read the XLSX file: %s', 'visualizer' ), |
| 86 | + $e->getMessage() |
| 87 | + ); |
| 88 | + return false; |
| 89 | + } |
| 90 | + |
| 91 | + $reader->close(); |
| 92 | + |
| 93 | + if ( count( $all_rows ) < 2 ) { |
| 94 | + $this->_error = esc_html__( 'File should have a heading row (1st row) and a data type row (2nd row). Please try again.', 'visualizer' ); |
| 95 | + return false; |
| 96 | + } |
| 97 | + |
| 98 | + $labels = array_filter( $all_rows[0] ); |
| 99 | + $types = array_filter( $all_rows[1] ); |
| 100 | + |
| 101 | + if ( ! $labels || ! $types ) { |
| 102 | + $this->_error = esc_html__( 'File should have a heading row (1st row) and a data type row (2nd row). Please try again.', 'visualizer' ); |
| 103 | + return false; |
| 104 | + } |
| 105 | + |
| 106 | + $types = array_map( 'trim', $types ); |
| 107 | + if ( ! self::_validateTypes( $types ) ) { |
| 108 | + $this->_error = esc_html__( 'Invalid data types detected in the data type row (2nd row). Please try again.', 'visualizer' ); |
| 109 | + return false; |
| 110 | + } |
| 111 | + |
| 112 | + // Build the series array from row 1 (labels) and row 2 (types). |
| 113 | + $label_values = $all_rows[0]; |
| 114 | + $type_values = $all_rows[1]; |
| 115 | + $col_count = count( $label_values ); |
| 116 | + |
| 117 | + for ( $i = 0; $i < $col_count; $i++ ) { |
| 118 | + $default_type = ( $i === 0 ) ? 'string' : 'number'; |
| 119 | + $label = isset( $label_values[ $i ] ) ? $this->toUTF8( (string) $label_values[ $i ] ) : ''; |
| 120 | + $type = isset( $type_values[ $i ] ) && ! empty( $type_values[ $i ] ) ? trim( $type_values[ $i ] ) : $default_type; |
| 121 | + |
| 122 | + $this->_series[] = array( |
| 123 | + 'label' => sanitize_text_field( wp_strip_all_tags( $label ) ), |
| 124 | + 'type' => $type, |
| 125 | + ); |
| 126 | + } |
| 127 | + |
| 128 | + // Parse data rows (row 3 onwards). |
| 129 | + for ( $r = 2, $total = count( $all_rows ); $r < $total; $r++ ) { |
| 130 | + $this->_data[] = $this->_normalizeData( $all_rows[ $r ] ); |
| 131 | + } |
| 132 | + |
| 133 | + return true; |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Returns the file path to open with the reader. |
| 138 | + * Subclasses may override this to supply a locally-downloaded copy. |
| 139 | + * |
| 140 | + * @access protected |
| 141 | + * @return string |
| 142 | + */ |
| 143 | + protected function _get_file_path() { |
| 144 | + return $this->_filename; |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Returns the source name. |
| 149 | + * |
| 150 | + * @access public |
| 151 | + * @return string |
| 152 | + */ |
| 153 | + public function getSourceName() { |
| 154 | + return __CLASS__; |
| 155 | + } |
| 156 | +} |
0 commit comments