Updated gui for user facility settings (#1327)
[openemr.git] / vendor / phpoffice / phpexcel / Classes / PHPExcel / Reader / CSV.php
blob22176fdc99f45195e7b0112b89a057cccb448a84
1 <?php
2 /**
3 * PHPExcel
5 * Copyright (c) 2006 - 2014 PHPExcel
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 * @category PHPExcel
22 * @package PHPExcel_Reader
23 * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
24 * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
25 * @version ##VERSION##, ##DATE##
29 /** PHPExcel root directory */
30 if (!defined('PHPEXCEL_ROOT')) {
31 /**
32 * @ignore
34 define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
35 require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
38 /**
39 * PHPExcel_Reader_CSV
41 * @category PHPExcel
42 * @package PHPExcel_Reader
43 * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
45 class PHPExcel_Reader_CSV extends PHPExcel_Reader_Abstract implements PHPExcel_Reader_IReader
47 /**
48 * Input encoding
50 * @access private
51 * @var string
53 private $_inputEncoding = 'UTF-8';
55 /**
56 * Delimiter
58 * @access private
59 * @var string
61 private $_delimiter = ',';
63 /**
64 * Enclosure
66 * @access private
67 * @var string
69 private $_enclosure = '"';
71 /**
72 * Sheet index to read
74 * @access private
75 * @var int
77 private $_sheetIndex = 0;
79 /**
80 * Load rows contiguously
82 * @access private
83 * @var int
85 private $_contiguous = false;
87 /**
88 * Row counter for loading rows contiguously
90 * @var int
92 private $_contiguousRow = -1;
95 /**
96 * Create a new PHPExcel_Reader_CSV
98 public function __construct() {
99 $this->_readFilter = new PHPExcel_Reader_DefaultReadFilter();
103 * Validate that the current file is a CSV file
105 * @return boolean
107 protected function _isValidFormat()
109 return TRUE;
113 * Set input encoding
115 * @param string $pValue Input encoding
117 public function setInputEncoding($pValue = 'UTF-8')
119 $this->_inputEncoding = $pValue;
120 return $this;
124 * Get input encoding
126 * @return string
128 public function getInputEncoding()
130 return $this->_inputEncoding;
134 * Move filepointer past any BOM marker
137 protected function _skipBOM()
139 rewind($this->_fileHandle);
141 switch ($this->_inputEncoding) {
142 case 'UTF-8':
143 fgets($this->_fileHandle, 4) == "\xEF\xBB\xBF" ?
144 fseek($this->_fileHandle, 3) : fseek($this->_fileHandle, 0);
145 break;
146 case 'UTF-16LE':
147 fgets($this->_fileHandle, 3) == "\xFF\xFE" ?
148 fseek($this->_fileHandle, 2) : fseek($this->_fileHandle, 0);
149 break;
150 case 'UTF-16BE':
151 fgets($this->_fileHandle, 3) == "\xFE\xFF" ?
152 fseek($this->_fileHandle, 2) : fseek($this->_fileHandle, 0);
153 break;
154 case 'UTF-32LE':
155 fgets($this->_fileHandle, 5) == "\xFF\xFE\x00\x00" ?
156 fseek($this->_fileHandle, 4) : fseek($this->_fileHandle, 0);
157 break;
158 case 'UTF-32BE':
159 fgets($this->_fileHandle, 5) == "\x00\x00\xFE\xFF" ?
160 fseek($this->_fileHandle, 4) : fseek($this->_fileHandle, 0);
161 break;
162 default:
163 break;
168 * Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns)
170 * @param string $pFilename
171 * @throws PHPExcel_Reader_Exception
173 public function listWorksheetInfo($pFilename)
175 // Open file
176 $this->_openFile($pFilename);
177 if (!$this->_isValidFormat()) {
178 fclose ($this->_fileHandle);
179 throw new PHPExcel_Reader_Exception($pFilename . " is an Invalid Spreadsheet file.");
181 $fileHandle = $this->_fileHandle;
183 // Skip BOM, if any
184 $this->_skipBOM();
186 $escapeEnclosures = array( "\\" . $this->_enclosure, $this->_enclosure . $this->_enclosure );
188 $worksheetInfo = array();
189 $worksheetInfo[0]['worksheetName'] = 'Worksheet';
190 $worksheetInfo[0]['lastColumnLetter'] = 'A';
191 $worksheetInfo[0]['lastColumnIndex'] = 0;
192 $worksheetInfo[0]['totalRows'] = 0;
193 $worksheetInfo[0]['totalColumns'] = 0;
195 // Loop through each line of the file in turn
196 while (($rowData = fgetcsv($fileHandle, 0, $this->_delimiter, $this->_enclosure)) !== FALSE) {
197 $worksheetInfo[0]['totalRows']++;
198 $worksheetInfo[0]['lastColumnIndex'] = max($worksheetInfo[0]['lastColumnIndex'], count($rowData) - 1);
201 $worksheetInfo[0]['lastColumnLetter'] = PHPExcel_Cell::stringFromColumnIndex($worksheetInfo[0]['lastColumnIndex']);
202 $worksheetInfo[0]['totalColumns'] = $worksheetInfo[0]['lastColumnIndex'] + 1;
204 // Close file
205 fclose($fileHandle);
207 return $worksheetInfo;
211 * Loads PHPExcel from file
213 * @param string $pFilename
214 * @return PHPExcel
215 * @throws PHPExcel_Reader_Exception
217 public function load($pFilename)
219 // Create new PHPExcel
220 $objPHPExcel = new PHPExcel();
222 // Load into this instance
223 return $this->loadIntoExisting($pFilename, $objPHPExcel);
227 * Loads PHPExcel from file into PHPExcel instance
229 * @param string $pFilename
230 * @param PHPExcel $objPHPExcel
231 * @return PHPExcel
232 * @throws PHPExcel_Reader_Exception
234 public function loadIntoExisting($pFilename, PHPExcel $objPHPExcel)
236 $lineEnding = ini_get('auto_detect_line_endings');
237 ini_set('auto_detect_line_endings', true);
239 // Open file
240 $this->_openFile($pFilename);
241 if (!$this->_isValidFormat()) {
242 fclose ($this->_fileHandle);
243 throw new PHPExcel_Reader_Exception($pFilename . " is an Invalid Spreadsheet file.");
245 $fileHandle = $this->_fileHandle;
247 // Skip BOM, if any
248 $this->_skipBOM();
250 // Create new PHPExcel object
251 while ($objPHPExcel->getSheetCount() <= $this->_sheetIndex) {
252 $objPHPExcel->createSheet();
254 $sheet = $objPHPExcel->setActiveSheetIndex($this->_sheetIndex);
256 $escapeEnclosures = array( "\\" . $this->_enclosure,
257 $this->_enclosure . $this->_enclosure
260 // Set our starting row based on whether we're in contiguous mode or not
261 $currentRow = 1;
262 if ($this->_contiguous) {
263 $currentRow = ($this->_contiguousRow == -1) ? $sheet->getHighestRow(): $this->_contiguousRow;
266 // Loop through each line of the file in turn
267 while (($rowData = fgetcsv($fileHandle, 0, $this->_delimiter, $this->_enclosure)) !== FALSE) {
268 $columnLetter = 'A';
269 foreach($rowData as $rowDatum) {
270 if ($rowDatum != '' && $this->_readFilter->readCell($columnLetter, $currentRow)) {
271 // Unescape enclosures
272 $rowDatum = str_replace($escapeEnclosures, $this->_enclosure, $rowDatum);
274 // Convert encoding if necessary
275 if ($this->_inputEncoding !== 'UTF-8') {
276 $rowDatum = PHPExcel_Shared_String::ConvertEncoding($rowDatum, 'UTF-8', $this->_inputEncoding);
279 // Set cell value
280 $sheet->getCell($columnLetter . $currentRow)->setValue($rowDatum);
282 ++$columnLetter;
284 ++$currentRow;
287 // Close file
288 fclose($fileHandle);
290 if ($this->_contiguous) {
291 $this->_contiguousRow = $currentRow;
294 ini_set('auto_detect_line_endings', $lineEnding);
296 // Return
297 return $objPHPExcel;
301 * Get delimiter
303 * @return string
305 public function getDelimiter() {
306 return $this->_delimiter;
310 * Set delimiter
312 * @param string $pValue Delimiter, defaults to ,
313 * @return PHPExcel_Reader_CSV
315 public function setDelimiter($pValue = ',') {
316 $this->_delimiter = $pValue;
317 return $this;
321 * Get enclosure
323 * @return string
325 public function getEnclosure() {
326 return $this->_enclosure;
330 * Set enclosure
332 * @param string $pValue Enclosure, defaults to "
333 * @return PHPExcel_Reader_CSV
335 public function setEnclosure($pValue = '"') {
336 if ($pValue == '') {
337 $pValue = '"';
339 $this->_enclosure = $pValue;
340 return $this;
344 * Get sheet index
346 * @return integer
348 public function getSheetIndex() {
349 return $this->_sheetIndex;
353 * Set sheet index
355 * @param integer $pValue Sheet index
356 * @return PHPExcel_Reader_CSV
358 public function setSheetIndex($pValue = 0) {
359 $this->_sheetIndex = $pValue;
360 return $this;
364 * Set Contiguous
366 * @param boolean $contiguous
368 public function setContiguous($contiguous = FALSE)
370 $this->_contiguous = (bool) $contiguous;
371 if (!$contiguous) {
372 $this->_contiguousRow = -1;
375 return $this;
379 * Get Contiguous
381 * @return boolean
383 public function getContiguous() {
384 return $this->_contiguous;