| @@ -21,9 +21,9 @@ | ||
| 21 | 21 | */ |
| 22 | 22 | public function __construct(CSVFile $file, $args = array()) |
| 23 | 23 | { |
| 24 | 24 | $this->file = $file; |
| 25 | - $this->file->processing(true); | |
| 25 | + // Do not enable processing mode — preview navigation needs the full record index. | |
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | /** |
| 29 | 29 | * Generate a return a table based on the CSV data being previewed. |
| @@ -59,20 +59,31 @@ | ||
| 59 | 59 | |
| 60 | 60 | public function data($record_index = 0, $show_headings = true) |
| 61 | 61 | { |
| 62 | 62 | $result = []; |
| 63 | - $headings = str_getcsv($this->file->getRecord(0), $this->file->getDelimiter(), $this->file->getEnclosure()); | |
| 63 | + $headings = str_getcsv($this->file->getRecord(0), $this->file->getDelimiter(), $this->file->getEnclosure(), $this->file->getEscape()); | |
| 64 | + $count = $this->file->getRecordCount(); | |
| 65 | + $total = true === $show_headings ? max(0, $count - 1) : $count; | |
| 66 | + $record_index = max(0, intval($record_index)); | |
| 67 | + if ($total > 0) { | |
| 68 | + $record_index = min($record_index, $total - 1); | |
| 69 | + } else { | |
| 70 | + $record_index = 0; | |
| 71 | + } | |
| 64 | 72 | |
| 65 | 73 | if (true === $show_headings) { |
| 66 | 74 | $result['headings'] = $headings; |
| 67 | - $record_index++; | |
| 75 | + $file_index = $record_index + 1; | |
| 68 | 76 | } else { |
| 69 | 77 | $result['headings'] = []; |
| 70 | 78 | for ($i = 0; $i < count($headings); $i++) { |
| 71 | 79 | $result['headings'][] = $i; |
| 72 | 80 | } |
| 81 | + $file_index = $record_index; | |
| 73 | 82 | } |
| 74 | 83 | |
| 75 | - $result['row'] = str_getcsv($this->file->getRecord($record_index), $this->file->getDelimiter(), $this->file->getEnclosure()); | |
| 84 | + $result['row'] = str_getcsv($this->file->getRecord($file_index), $this->file->getDelimiter(), $this->file->getEnclosure(), $this->file->getEscape()); | |
| 85 | + $result['record'] = $record_index; | |
| 86 | + $result['total'] = $total; | |
| 76 | 87 | return $result; |
| 77 | 88 | } |
| 78 | 89 | } |