Commit 5b617617 by Tobin

daily commit

parent 6d302e54
.DS_Store
test
materials
\ No newline at end of file
### v.0.5.11 2015-04-30
- Added a special case for cells formatted as text in XLSX. Previously leading zeros would get truncated if a text cell contained only numbers.
### v.0.5.10 2015-04-18
- Implemented SeekableIterator. Thanks to [paales](https://github.com/paales) for suggestion ([Issue #54](https://github.com/nuovo/spreadsheet-reader/issues/54) and [Pull request #55](https://github.com/nuovo/spreadsheet-reader/pull/55)).
- Fixed a bug in CSV and ODS reading where reading position 0 multiple times in a row would result in internal pointer being advanced and reading the next line. (E.g. reading row #0 three times would result in rows #0, #1, and #2.). This could have happened on multiple calls to `current()` while in #0 position, or calls to `seek(0)` and `current()`.
### v.0.5.9 2015-04-18
- [Pull request #85](https://github.com/nuovo/spreadsheet-reader/pull/85): Fixed an index check. (Thanks to [pa-m](https://github.com/pa-m)).
### v.0.5.8 2015-01-31
- [Issue #50](https://github.com/nuovo/spreadsheet-reader/issues/50): Fixed an XLSX rewind issue. (Thanks to [osuwariboy](https://github.com/osuwariboy))
- [Issue #52](https://github.com/nuovo/spreadsheet-reader/issues/52), [#53](https://github.com/nuovo/spreadsheet-reader/issues/53): Apache POI compatibility for XLSX. (Thanks to [dimapashkov](https://github.com/dimapashkov))
- [Issue #61](https://github.com/nuovo/spreadsheet-reader/issues/61): Autoload fix in the main class. (Thanks to [i-bash](https://github.com/i-bash))
- [Issue #60](https://github.com/nuovo/spreadsheet-reader/issues/60), [#69](https://github.com/nuovo/spreadsheet-reader/issues/69), [#72](https://github.com/nuovo/spreadsheet-reader/issues/72): Fixed an issue where XLSX ChangeSheet may not work. (Thanks to [jtresponse](https://github.com/jtresponse), [osuwariboy](https://github.com/osuwariboy))
- [Issue #70](https://github.com/nuovo/spreadsheet-reader/issues/70): Added a check for constructor parameter correctness.
### v.0.5.7 2013-10-29
- Attempt to replicate Excel's "General" format in XLSX files that is applied to otherwise unformatted cells.
Currently only decimal number values are converted to PHP's floats.
### v.0.5.6 2013-09-04
- Fix for formulas being returned along with values in XLSX files. (Thanks to [marktag](https://github.com/marktag))
### v.0.5.5 2013-08-23
- Fix for macro sheets appearing when parsing XLS files. (Thanks to [osuwariboy](https://github.com/osuwariboy))
### v.0.5.4 2013-08-22
- Fix for a PHP warning that occurs with completely empty sheets in XLS files.
- XLSM (macro-enabled XLSX) files are recognized and read, too.
- composer.json file is added to the repository (thanks to [matej116](https://github.com/matej116))
### v.0.5.3 2013-08-12
- Fix for repeated columns in ODS files not reading correctly (thanks to [etfb](https://github.com/etfb))
- Fix for filename extension reading (Thanks to [osuwariboy](https://github.com/osuwariboy))
### v.0.5.2 2013-06-28
- A fix for the case when row count wasn't read correctly from the sheet in a XLS file.
### v.0.5.1 2013-06-27
- Fixed file type choice when using mime-types (previously there were problems with
XLSX and ODS mime-types) (Thanks to [incratec](https://github.com/incratec))
- Fixed an error in XLSX iterator where `current()` would advance the iterator forward
with each call. (Thanks to [osuwariboy](https://github.com/osuwariboy))
### v.0.5.0 2013-06-17
- Multiple sheet reading is now supported:
- The `Sheets()` method lets you retrieve a list of all sheets present in the file.
- `ChangeSheet($Index)` method changes the sheet in the reader to the one specified.
- Previously temporary files that were extracted, were deleted after the SpreadsheetReader
was destroyed but the empty directories remained. Now those are cleaned up as well.
### v.0.4.3 2013-06-14
- Bugfix for shared string caching in XLSX files. When the shared string count was larger
than the caching limit, instead of them being read from file, empty strings were returned.
### v.0.4.2 2013-06-02
- XLS file reading relies on the external Spreadsheet_Excel_Reader class which, by default,
reads additional information about cells like fonts, styles, etc. Now that is disabled
to save some memory since the style data is unnecessary anyway.
(Thanks to [ChALkeR](https://github.com/ChALkeR) for the tip.)
Martins Pilsetnieks <[email protected]>
\ No newline at end of file
### spreadsheet-reader is licensed under the MIT License
Copyright (C) 2012-2015 Martins Pilsetnieks
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Spreadsheet_Excel_Reader is licensed under the PHP license as noted in the excel_reader2.php file
> LICENSE: This source file is subject to version 3.0 of the PHP license
> that is available through the world-wide-web at the following URI:
> http://www.php.net/license/3_0.txt. If you did not receive a copy of
> the PHP License and are unable to obtain it through the web, please
> send a note to [email protected] so we can mail you a copy immediately.
**spreadsheet-reader** is a PHP spreadsheet reader that differs from others in that the main goal for it was efficient
data extraction that could handle large (as in really large) files. So far it may not definitely be CPU, time
or I/O-efficient but at least it won't run out of memory (except maybe for XLS files).
So far XLSX, ODS and text/CSV file parsing should be memory-efficient. XLS file parsing is done with php-excel-reader
from http://code.google.com/p/php-excel-reader/ which, sadly, has memory issues with bigger spreadsheets, as it reads the
data all at once and keeps it all in memory.
### Requirements:
* PHP 5.3.0 or newer
* PHP must have Zip file support (see http://php.net/manual/en/zip.installation.php)
### Usage:
All data is read from the file sequentially, with each row being returned as a numeric array.
This is about the easiest way to read a file:
<?php
// If you need to parse XLS files, include php-excel-reader
require('php-excel-reader/excel_reader2.php');
require('SpreadsheetReader.php');
$Reader = new SpreadsheetReader('example.xlsx');
foreach ($Reader as $Row)
{
print_r($Row);
}
?>
However, now also multiple sheet reading is supported for file formats where it is possible. (In case of CSV, it is handled as if
it only has one sheet.)
You can retrieve information about sheets contained in the file by calling the `Sheets()` method which returns an array with
sheet indexes as keys and sheet names as values. Then you can change the sheet that's currently being read by passing that index
to the `ChangeSheet($Index)` method.
Example:
<?php
$Reader = new SpreadsheetReader('example.xlsx');
$Sheets = $Reader -> Sheets();
foreach ($Sheets as $Index => $Name)
{
echo 'Sheet #'.$Index.': '.$Name;
$Reader -> ChangeSheet($Index);
foreach ($Reader as $Row)
{
print_r($Row);
}
}
?>
If a sheet is changed to the same that is currently open, the position in the file still reverts to the beginning, so as to conform
to the same behavior as when changed to a different sheet.
### Testing
From the command line:
php test.php path-to-spreadsheet.xls
In the browser:
http://path-to-library/test.php?File=/path/to/spreadsheet.xls
### Notes about library performance
* CSV and text files are read strictly sequentially so performance should be O(n);
* When parsing XLS files, all of the file content is read into memory so large XLS files can lead to "out of memory" errors;
* XLSX files use so called "shared strings" internally to optimize for cases where the same string is repeated multiple times.
Internally XLSX is an XML text that is parsed sequentially to extract data from it, however, in some cases these shared strings are a problem -
sometimes Excel may put all, or nearly all of the strings from the spreadsheet in the shared string file (which is a separate XML text), and not necessarily in the same
order. Worst case scenario is when it is in reverse order - for each string we need to parse the shared string XML from the beginning, if we want to avoid keeping the data in memory.
To that end, the XLSX parser has a cache for shared strings that is used if the total shared string count is not too high. In case you get out of memory errors, you can
try adjusting the *SHARED_STRING_CACHE_LIMIT* constant in SpreadsheetReader_XLSX to a lower one.
### TODOs:
* ODS date formats;
### Licensing
All of the code in this library is licensed under the MIT license as included in the LICENSE file, however, for now the library
relies on php-excel-reader library for XLS file parsing which is licensed under the PHP license.
\ No newline at end of file
<?php
/**
* Main class for spreadsheet reading
*
* @version 0.5.10
* @author Martins Pilsetnieks
*/
class SpreadsheetReader implements SeekableIterator, Countable
{
const TYPE_XLSX = 'XLSX';
const TYPE_XLS = 'XLS';
const TYPE_CSV = 'CSV';
const TYPE_ODS = 'ODS';
private $Options = array(
'Delimiter' => '',
'Enclosure' => '"'
);
/**
* @var int Current row in the file
*/
private $Index = 0;
/**
* @var SpreadsheetReader_* Handle for the reader object
*/
private $Handle = array();
/**
* @var TYPE_* Type of the contained spreadsheet
*/
private $Type = false;
/**
* @param string Path to file
* @param string Original filename (in case of an uploaded file), used to determine file type, optional
* @param string MIME type from an upload, used to determine file type, optional
*/
public function __construct($Filepath, $OriginalFilename = false, $MimeType = false)
{
if (!is_readable($Filepath))
{
throw new Exception('SpreadsheetReader: File ('.$Filepath.') not readable');
}
// To avoid timezone warnings and exceptions for formatting dates retrieved from files
$DefaultTZ = @date_default_timezone_get();
if ($DefaultTZ)
{
date_default_timezone_set($DefaultTZ);
}
// Checking the other parameters for correctness
// This should be a check for string but we're lenient
if (!empty($OriginalFilename) && !is_scalar($OriginalFilename))
{
throw new Exception('SpreadsheetReader: Original file (2nd parameter) path is not a string or a scalar value.');
}
if (!empty($MimeType) && !is_scalar($MimeType))
{
throw new Exception('SpreadsheetReader: Mime type (3nd parameter) path is not a string or a scalar value.');
}
// 1. Determine type
if (!$OriginalFilename)
{
$OriginalFilename = $Filepath;
}
$Extension = strtolower(pathinfo($OriginalFilename, PATHINFO_EXTENSION));
switch ($MimeType)
{
case 'text/csv':
case 'text/comma-separated-values':
case 'text/plain':
$this -> Type = self::TYPE_CSV;
break;
case 'application/vnd.ms-excel':
case 'application/msexcel':
case 'application/x-msexcel':
case 'application/x-ms-excel':
case 'application/vnd.ms-excel':
case 'application/x-excel':
case 'application/x-dos_ms_excel':
case 'application/xls':
case 'application/xlt':
case 'application/x-xls':
// Excel does weird stuff
if (in_array($Extension, array('csv', 'tsv', 'txt')))
{
$this -> Type = self::TYPE_CSV;
}
else
{
$this -> Type = self::TYPE_XLS;
}
break;
case 'application/vnd.oasis.opendocument.spreadsheet':
case 'application/vnd.oasis.opendocument.spreadsheet-template':
$this -> Type = self::TYPE_ODS;
break;
case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
case 'application/vnd.openxmlformats-officedocument.spreadsheetml.template':
case 'application/xlsx':
case 'application/xltx':
$this -> Type = self::TYPE_XLSX;
break;
case 'application/xml':
// Excel 2004 xml format uses this
break;
}
if (!$this -> Type)
{
switch ($Extension)
{
case 'xlsx':
case 'xltx': // XLSX template
case 'xlsm': // Macro-enabled XLSX
case 'xltm': // Macro-enabled XLSX template
$this -> Type = self::TYPE_XLSX;
break;
case 'xls':
case 'xlt':
$this -> Type = self::TYPE_XLS;
break;
case 'ods':
case 'odt':
$this -> Type = self::TYPE_ODS;
break;
default:
$this -> Type = self::TYPE_CSV;
break;
}
}
// Pre-checking XLS files, in case they are renamed CSV or XLSX files
if ($this -> Type == self::TYPE_XLS)
{
self::Load(self::TYPE_XLS);
$this -> Handle = new SpreadsheetReader_XLS($Filepath);
if ($this -> Handle -> Error)
{
$this -> Handle -> __destruct();
if (is_resource($ZipHandle = zip_open($Filepath)))
{
$this -> Type = self::TYPE_XLSX;
zip_close($ZipHandle);
}
else
{
$this -> Type = self::TYPE_CSV;
}
}
}
// 2. Create handle
switch ($this -> Type)
{
case self::TYPE_XLSX:
self::Load(self::TYPE_XLSX);
$this -> Handle = new SpreadsheetReader_XLSX($Filepath);
break;
case self::TYPE_CSV:
self::Load(self::TYPE_CSV);
$this -> Handle = new SpreadsheetReader_CSV($Filepath, $this -> Options);
break;
case self::TYPE_XLS:
// Everything already happens above
break;
case self::TYPE_ODS:
self::Load(self::TYPE_ODS);
$this -> Handle = new SpreadsheetReader_ODS($Filepath, $this -> Options);
break;
}
}
/**
* Gets information about separate sheets in the given file
*
* @return array Associative array where key is sheet index and value is sheet name
*/
public function Sheets()
{
return $this -> Handle -> Sheets();
}
/**
* Changes the current sheet to another from the file.
* Note that changing the sheet will rewind the file to the beginning, even if
* the current sheet index is provided.
*
* @param int Sheet index
*
* @return bool True if sheet could be changed to the specified one,
* false if not (for example, if incorrect index was provided.
*/
public function ChangeSheet($Index)
{
return $this -> Handle -> ChangeSheet($Index);
}
/**
* Autoloads the required class for the particular spreadsheet type
*
* @param TYPE_* Spreadsheet type, one of TYPE_* constants of this class
*/
private static function Load($Type)
{
if (!in_array($Type, array(self::TYPE_XLSX, self::TYPE_XLS, self::TYPE_CSV, self::TYPE_ODS)))
{
throw new Exception('SpreadsheetReader: Invalid type ('.$Type.')');
}
// 2nd parameter is to prevent autoloading for the class.
// If autoload works, the require line is unnecessary, if it doesn't, it ends badly.
if (!class_exists('SpreadsheetReader_'.$Type, false))
{
require(dirname(__FILE__).DIRECTORY_SEPARATOR.'SpreadsheetReader_'.$Type.'.php');
}
}
// !Iterator interface methods
/**
* Rewind the Iterator to the first element.
* Similar to the reset() function for arrays in PHP
*/
public function rewind()
{
$this -> Index = 0;
if ($this -> Handle)
{
$this -> Handle -> rewind();
}
}
/**
* Return the current element.
* Similar to the current() function for arrays in PHP
*
* @return mixed current element from the collection
*/
public function current()
{
if ($this -> Handle)
{
return $this -> Handle -> current();
}
return null;
}
/**
* Move forward to next element.
* Similar to the next() function for arrays in PHP
*/
public function next()
{
if ($this -> Handle)
{
$this -> Index++;
return $this -> Handle -> next();
}
return null;
}
/**
* Return the identifying key of the current element.
* Similar to the key() function for arrays in PHP
*
* @return mixed either an integer or a string
*/
public function key()
{
if ($this -> Handle)
{
return $this -> Handle -> key();
}
return null;
}
/**
* Check if there is a current element after calls to rewind() or next().
* Used to check if we've iterated to the end of the collection
*
* @return boolean FALSE if there's nothing more to iterate over
*/
public function valid()
{
if ($this -> Handle)
{
return $this -> Handle -> valid();
}
return false;
}
// !Countable interface method
public function count()
{
if ($this -> Handle)
{
return $this -> Handle -> count();
}
return 0;
}
/**
* Method for SeekableIterator interface. Takes a posiiton and traverses the file to that position
* The value can be retrieved with a `current()` call afterwards.
*
* @param int Position in file
*/
public function seek($Position)
{
if (!$this -> Handle)
{
throw new OutOfBoundsException('SpreadsheetReader: No file opened');
}
$CurrentIndex = $this -> Handle -> key();
if ($CurrentIndex != $Position)
{
if ($Position < $CurrentIndex || is_null($CurrentIndex) || $Position == 0)
{
$this -> rewind();
}
while ($this -> Handle -> valid() && ($Position > $this -> Handle -> key()))
{
$this -> Handle -> next();
}
if (!$this -> Handle -> valid())
{
throw new OutOfBoundsException('SpreadsheetError: Position '.$Position.' not found');
}
}
return null;
}
}
?>
<?php
/**
* Class for parsing CSV files
*
* @author Martins Pilsetnieks
*/
class SpreadsheetReader_CSV implements Iterator, Countable
{
/**
* @var array Options array, pre-populated with the default values.
*/
private $Options = array(
'Delimiter' => ';',
'Enclosure' => '"'
);
private $Encoding = 'UTF-8';
private $BOMLength = 0;
/**
* @var resource File handle
*/
private $Handle = false;
private $Filepath = '';
private $Index = 0;
private $CurrentRow = null;
/**
* @param string Path to file
* @param array Options:
* Enclosure => string CSV enclosure
* Separator => string CSV separator
*/
public function __construct($Filepath, array $Options = null)
{
$this -> Filepath = $Filepath;
if (!is_readable($Filepath))
{
throw new Exception('SpreadsheetReader_CSV: File not readable ('.$Filepath.')');
}
// For safety's sake
@ini_set('auto_detect_line_endings', true);
$this -> Options = array_merge($this -> Options, $Options);
$this -> Handle = fopen($Filepath, 'r');
// Checking the file for byte-order mark to determine encoding
$BOM16 = bin2hex(fread($this -> Handle, 2));
if ($BOM16 == 'fffe')
{
$this -> Encoding = 'UTF-16LE';
//$this -> Encoding = 'UTF-16';
$this -> BOMLength = 2;
}
elseif ($BOM16 == 'feff')
{
$this -> Encoding = 'UTF-16BE';
//$this -> Encoding = 'UTF-16';
$this -> BOMLength = 2;
}
if (!$this -> BOMLength)
{
fseek($this -> Handle, 0);
$BOM32 = bin2hex(fread($this -> Handle, 4));
if ($BOM32 == '0000feff')
{
//$this -> Encoding = 'UTF-32BE';
$this -> Encoding = 'UTF-32';
$this -> BOMLength = 4;
}
elseif ($BOM32 == 'fffe0000')
{
//$this -> Encoding = 'UTF-32LE';
$this -> Encoding = 'UTF-32';
$this -> BOMLength = 4;
}
}
fseek($this -> Handle, 0);
$BOM8 = bin2hex(fread($this -> Handle, 3));
if ($BOM8 == 'efbbbf')
{
$this -> Encoding = 'UTF-8';
$this -> BOMLength = 3;
}
// Seeking the place right after BOM as the start of the real content
if ($this -> BOMLength)
{
fseek($this -> Handle, $this -> BOMLength);
}
// Checking for the delimiter if it should be determined automatically
if (!$this -> Options['Delimiter'])
{
// fgetcsv needs single-byte separators
$Semicolon = ';';
$Tab = "\t";
$Comma = ',';
// Reading the first row and checking if a specific separator character
// has more columns than others (it means that most likely that is the delimiter).
$SemicolonCount = count(fgetcsv($this -> Handle, null, $Semicolon));
fseek($this -> Handle, $this -> BOMLength);
$TabCount = count(fgetcsv($this -> Handle, null, $Tab));
fseek($this -> Handle, $this -> BOMLength);
$CommaCount = count(fgetcsv($this -> Handle, null, $Comma));
fseek($this -> Handle, $this -> BOMLength);
$Delimiter = $Semicolon;
if ($TabCount > $SemicolonCount || $CommaCount > $SemicolonCount)
{
$Delimiter = $CommaCount > $TabCount ? $Comma : $Tab;
}
$this -> Options['Delimiter'] = $Delimiter;
}
}
/**
* Returns information about sheets in the file.
* Because CSV doesn't have any, it's just a single entry.
*
* @return array Sheet data
*/
public function Sheets()
{
return array(0 => basename($this -> Filepath));
}
/**
* Changes sheet to another. Because CSV doesn't have any sheets
* it just rewinds the file so the behaviour is compatible with other
* sheet readers. (If an invalid index is given, it doesn't do anything.)
*
* @param bool Status
*/
public function ChangeSheet($Index)
{
if ($Index == 0)
{
$this -> rewind();
return true;
}
return false;
}
// !Iterator interface methods
/**
* Rewind the Iterator to the first element.
* Similar to the reset() function for arrays in PHP
*/
public function rewind()
{
fseek($this -> Handle, $this -> BOMLength);
$this -> CurrentRow = null;
$this -> Index = 0;
}
/**
* Return the current element.
* Similar to the current() function for arrays in PHP
*
* @return mixed current element from the collection
*/
public function current()
{
if ($this -> Index == 0 && is_null($this -> CurrentRow))
{
$this -> next();
$this -> Index--;
}
return $this -> CurrentRow;
}
/**
* Move forward to next element.
* Similar to the next() function for arrays in PHP
*/
public function next()
{
$this -> CurrentRow = array();
// Finding the place the next line starts for UTF-16 encoded files
// Line breaks could be 0x0D 0x00 0x0A 0x00 and PHP could split lines on the
// first or the second linebreak leaving unnecessary \0 characters that mess up
// the output.
if ($this -> Encoding == 'UTF-16LE' || $this -> Encoding == 'UTF-16BE')
{
while (!feof($this -> Handle))
{
// While bytes are insignificant whitespace, do nothing
$Char = ord(fgetc($this -> Handle));
if (!$Char || $Char == 10 || $Char == 13)
{
continue;
}
else
{
// When significant bytes are found, step back to the last place before them
if ($this -> Encoding == 'UTF-16LE')
{
fseek($this -> Handle, ftell($this -> Handle) - 1);
}
else
{
fseek($this -> Handle, ftell($this -> Handle) - 2);
}
break;
}
}
}
$this -> Index++;
$this -> CurrentRow = fgetcsv($this -> Handle, null, $this -> Options['Delimiter'], $this -> Options['Enclosure']);
if ($this -> CurrentRow)
{
// Converting multi-byte unicode strings
// and trimming enclosure symbols off of them because those aren't recognized
// in the relevan encodings.
if ($this -> Encoding != 'ASCII' && $this -> Encoding != 'UTF-8')
{
$Encoding = $this -> Encoding;
foreach ($this -> CurrentRow as $Key => $Value)
{
$this -> CurrentRow[$Key] = trim(trim(
mb_convert_encoding($Value, 'UTF-8', $this -> Encoding),
$this -> Options['Enclosure']
));
}
}
}
return $this -> CurrentRow;
}
/**
* Return the identifying key of the current element.
* Similar to the key() function for arrays in PHP
*
* @return mixed either an integer or a string
*/
public function key()
{
return $this -> Index;
}
/**
* Check if there is a current element after calls to rewind() or next().
* Used to check if we've iterated to the end of the collection
*
* @return boolean FALSE if there's nothing more to iterate over
*/
public function valid()
{
return ($this -> CurrentRow || !feof($this -> Handle));
}
// !Countable interface method
/**
* Ostensibly should return the count of the contained items but this just returns the number
* of rows read so far. It's not really correct but at least coherent.
*/
public function count()
{
return $this -> Index + 1;
}
}
?>
\ No newline at end of file
<?php
/**
* Class for parsing ODS files
*
* @author Martins Pilsetnieks
*/
class SpreadsheetReader_ODS implements Iterator, Countable
{
private $Options = array(
'TempDir' => '',
'ReturnDateTimeObjects' => false
);
/**
* @var string Path to temporary content file
*/
private $ContentPath = '';
/**
* @var XMLReader XML reader object
*/
private $Content = false;
/**
* @var array Data about separate sheets in the file
*/
private $Sheets = false;
private $CurrentRow = null;
/**
* @var int Number of the sheet we're currently reading
*/
private $CurrentSheet = 0;
private $Index = 0;
private $TableOpen = false;
private $RowOpen = false;
/**
* @param string Path to file
* @param array Options:
* TempDir => string Temporary directory path
* ReturnDateTimeObjects => bool True => dates and times will be returned as PHP DateTime objects, false => as strings
*/
public function __construct($Filepath, array $Options = null)
{
if (!is_readable($Filepath))
{
throw new Exception('SpreadsheetReader_ODS: File not readable ('.$Filepath.')');
}
$this -> TempDir = isset($Options['TempDir']) && is_writable($Options['TempDir']) ?
$Options['TempDir'] :
sys_get_temp_dir();
$this -> TempDir = rtrim($this -> TempDir, DIRECTORY_SEPARATOR);
$this -> TempDir = $this -> TempDir.DIRECTORY_SEPARATOR.uniqid().DIRECTORY_SEPARATOR;
$Zip = new ZipArchive;
$Status = $Zip -> open($Filepath);
if ($Status !== true)
{
throw new Exception('SpreadsheetReader_ODS: File not readable ('.$Filepath.') (Error '.$Status.')');
}
if ($Zip -> locateName('content.xml') !== false)
{
$Zip -> extractTo($this -> TempDir, 'content.xml');
$this -> ContentPath = $this -> TempDir.'content.xml';
}
$Zip -> close();
if ($this -> ContentPath && is_readable($this -> ContentPath))
{
$this -> Content = new XMLReader;
$this -> Content -> open($this -> ContentPath);
$this -> Valid = true;
}
}
/**
* Destructor, destroys all that remains (closes and deletes temp files)
*/
public function __destruct()
{
if ($this -> Content && $this -> Content instanceof XMLReader)
{
$this -> Content -> close();
unset($this -> Content);
}
if (file_exists($this -> ContentPath))
{
@unlink($this -> ContentPath);
unset($this -> ContentPath);
}
}
/**
* Retrieves an array with information about sheets in the current file
*
* @return array List of sheets (key is sheet index, value is name)
*/
public function Sheets()
{
if ($this -> Sheets === false)
{
$this -> Sheets = array();
if ($this -> Valid)
{
$this -> SheetReader = new XMLReader;
$this -> SheetReader -> open($this -> ContentPath);
while ($this -> SheetReader -> read())
{
if ($this -> SheetReader -> name == 'table:table')
{
$this -> Sheets[] = $this -> SheetReader -> getAttribute('table:name');
$this -> SheetReader -> next();
}
}
$this -> SheetReader -> close();
}
}
return $this -> Sheets;
}
/**
* Changes the current sheet in the file to another
*
* @param int Sheet index
*
* @return bool True if sheet was successfully changed, false otherwise.
*/
public function ChangeSheet($Index)
{
$Index = (int)$Index;
$Sheets = $this -> Sheets();
if (isset($Sheets[$Index]))
{
$this -> CurrentSheet = $Index;
$this -> rewind();
return true;
}
return false;
}
// !Iterator interface methods
/**
* Rewind the Iterator to the first element.
* Similar to the reset() function for arrays in PHP
*/
public function rewind()
{
if ($this -> Index > 0)
{
// If the worksheet was already iterated, XML file is reopened.
// Otherwise it should be at the beginning anyway
$this -> Content -> close();
$this -> Content -> open($this -> ContentPath);
$this -> Valid = true;
$this -> TableOpen = false;
$this -> RowOpen = false;
$this -> CurrentRow = null;
}
$this -> Index = 0;
}
/**
* Return the current element.
* Similar to the current() function for arrays in PHP
*
* @return mixed current element from the collection
*/
public function current()
{
if ($this -> Index == 0 && is_null($this -> CurrentRow))
{
$this -> next();
$this -> Index--;
}
return $this -> CurrentRow;
}
/**
* Move forward to next element.
* Similar to the next() function for arrays in PHP
*/
public function next()
{
$this -> Index++;
$this -> CurrentRow = array();
if (!$this -> TableOpen)
{
$TableCounter = 0;
$SkipRead = false;
while ($this -> Valid = ($SkipRead || $this -> Content -> read()))
{
if ($SkipRead)
{
$SkipRead = false;
}
if ($this -> Content -> name == 'table:table' && $this -> Content -> nodeType != XMLReader::END_ELEMENT)
{
if ($TableCounter == $this -> CurrentSheet)
{
$this -> TableOpen = true;
break;
}
$TableCounter++;
$this -> Content -> next();
$SkipRead = true;
}
}
}
if ($this -> TableOpen && !$this -> RowOpen)
{
while ($this -> Valid = $this -> Content -> read())
{
switch ($this -> Content -> name)
{
case 'table:table':
$this -> TableOpen = false;
$this -> Content -> next('office:document-content');
$this -> Valid = false;
break 2;
case 'table:table-row':
if ($this -> Content -> nodeType != XMLReader::END_ELEMENT)
{
$this -> RowOpen = true;
break 2;
}
break;
}
}
}
if ($this -> RowOpen)
{
$LastCellContent = '';
while ($this -> Valid = $this -> Content -> read())
{
switch ($this -> Content -> name)
{
case 'table:table-cell':
if ($this -> Content -> nodeType == XMLReader::END_ELEMENT || $this -> Content -> isEmptyElement)
{
if ($this -> Content -> nodeType == XMLReader::END_ELEMENT)
{
$CellValue = $LastCellContent;
}
elseif ($this -> Content -> isEmptyElement)
{
$LastCellContent = '';
$CellValue = $LastCellContent;
}
$this -> CurrentRow[] = $LastCellContent;
if ($this -> Content -> getAttribute('table:number-columns-repeated') !== null)
{
$RepeatedColumnCount = $this -> Content -> getAttribute('table:number-columns-repeated');
// Checking if larger than one because the value is already added to the row once before
if ($RepeatedColumnCount > 1)
{
$this -> CurrentRow = array_pad($this -> CurrentRow, count($this -> CurrentRow) + $RepeatedColumnCount - 1, $LastCellContent);
}
}
}
else
{
$LastCellContent = '';
}
case 'text:p':
if ($this -> Content -> nodeType != XMLReader::END_ELEMENT)
{
$LastCellContent = $this -> Content -> readString();
}
break;
case 'table:table-row':
$this -> RowOpen = false;
break 2;
}
}
}
return $this -> CurrentRow;
}
/**
* Return the identifying key of the current element.
* Similar to the key() function for arrays in PHP
*
* @return mixed either an integer or a string
*/
public function key()
{
return $this -> Index;
}
/**
* Check if there is a current element after calls to rewind() or next().
* Used to check if we've iterated to the end of the collection
*
* @return boolean FALSE if there's nothing more to iterate over
*/
public function valid()
{
return $this -> Valid;
}
// !Countable interface method
/**
* Ostensibly should return the count of the contained items but this just returns the number
* of rows read so far. It's not really correct but at least coherent.
*/
public function count()
{
return $this -> Index + 1;
}
}
?>
\ No newline at end of file
<?php
/**
* Class for parsing XLS files
*
* @author Martins Pilsetnieks
*/
class SpreadsheetReader_XLS implements Iterator, Countable
{
/**
* @var array Options array, pre-populated with the default values.
*/
private $Options = array(
);
/**
* @var resource File handle
*/
private $Handle = false;
private $Index = 0;
private $Error = false;
/**
* @var array Sheet information
*/
private $Sheets = false;
private $SheetIndexes = array();
/**
* @var int Current sheet index
*/
private $CurrentSheet = 0;
/**
* @var array Content of the current row
*/
private $CurrentRow = array();
/**
* @var int Column count in the sheet
*/
private $ColumnCount = 0;
/**
* @var int Row count in the sheet
*/
private $RowCount = 0;
/**
* @var array Template to use for empty rows. Retrieved rows are merged
* with this so that empty cells are added, too
*/
private $EmptyRow = array();
/**
* @param string Path to file
* @param array Options
*/
public function __construct($Filepath, array $Options = null)
{
if (!is_readable($Filepath))
{
throw new Exception('SpreadsheetReader_XLS: File not readable ('.$Filepath.')');
}
if (!class_exists('Spreadsheet_Excel_Reader'))
{
throw new Exception('SpreadsheetReader_XLS: Spreadsheet_Excel_Reader class not available');
}
$this -> Handle = new Spreadsheet_Excel_Reader($Filepath, false, 'UTF-8');
if (function_exists('mb_convert_encoding'))
{
$this -> Handle -> setUTFEncoder('mb');
}
if (empty($this -> Handle -> sheets))
{
$this -> Error = true;
return null;
}
$this -> ChangeSheet(0);
}
public function __destruct()
{
unset($this -> Handle);
}
/**
* Retrieves an array with information about sheets in the current file
*
* @return array List of sheets (key is sheet index, value is name)
*/
public function Sheets()
{
if ($this -> Sheets === false)
{
$this -> Sheets = array();
$this -> SheetIndexes = array_keys($this -> Handle -> sheets);
foreach ($this -> SheetIndexes as $SheetIndex)
{
$this -> Sheets[] = $this -> Handle -> boundsheets[$SheetIndex]['name'];
}
}
return $this -> Sheets;
}
/**
* Changes the current sheet in the file to another
*
* @param int Sheet index
*
* @return bool True if sheet was successfully changed, false otherwise.
*/
public function ChangeSheet($Index)
{
$Index = (int)$Index;
$Sheets = $this -> Sheets();
if (isset($this -> Sheets[$Index]))
{
$this -> rewind();
$this -> CurrentSheet = $this -> SheetIndexes[$Index];
$this -> ColumnCount = $this -> Handle -> sheets[$this -> CurrentSheet]['numCols'];
$this -> RowCount = $this -> Handle -> sheets[$this -> CurrentSheet]['numRows'];
// For the case when Spreadsheet_Excel_Reader doesn't have the row count set correctly.
if (!$this -> RowCount && count($this -> Handle -> sheets[$this -> CurrentSheet]['cells']))
{
end($this -> Handle -> sheets[$this -> CurrentSheet]['cells']);
$this -> RowCount = (int)key($this -> Handle -> sheets[$this -> CurrentSheet]['cells']);
}
if ($this -> ColumnCount)
{
$this -> EmptyRow = array_fill(1, $this -> ColumnCount, '');
}
else
{
$this -> EmptyRow = array();
}
}
return false;
}
public function __get($Name)
{
switch ($Name)
{
case 'Error':
return $this -> Error;
break;
}
return null;
}
// !Iterator interface methods
/**
* Rewind the Iterator to the first element.
* Similar to the reset() function for arrays in PHP
*/
public function rewind()
{
$this -> Index = 0;
}
/**
* Return the current element.
* Similar to the current() function for arrays in PHP
*
* @return mixed current element from the collection
*/
public function current()
{
if ($this -> Index == 0)
{
$this -> next();
}
return $this -> CurrentRow;
}
/**
* Move forward to next element.
* Similar to the next() function for arrays in PHP
*/
public function next()
{
// Internal counter is advanced here instead of the if statement
// because apparently it's fully possible that an empty row will not be
// present at all
$this -> Index++;
if ($this -> Error)
{
return array();
}
elseif (isset($this -> Handle -> sheets[$this -> CurrentSheet]['cells'][$this -> Index]))
{
$this -> CurrentRow = $this -> Handle -> sheets[$this -> CurrentSheet]['cells'][$this -> Index];
if (!$this -> CurrentRow)
{
return array();
}
$this -> CurrentRow = $this -> CurrentRow + $this -> EmptyRow;
ksort($this -> CurrentRow);
$this -> CurrentRow = array_values($this -> CurrentRow);
return $this -> CurrentRow;
}
else
{
$this -> CurrentRow = $this -> EmptyRow;
return $this -> CurrentRow;
}
}
/**
* Return the identifying key of the current element.
* Similar to the key() function for arrays in PHP
*
* @return mixed either an integer or a string
*/
public function key()
{
return $this -> Index;
}
/**
* Check if there is a current element after calls to rewind() or next().
* Used to check if we've iterated to the end of the collection
*
* @return boolean FALSE if there's nothing more to iterate over
*/
public function valid()
{
if ($this -> Error)
{
return false;
}
return ($this -> Index <= $this -> RowCount);
}
// !Countable interface method
/**
* Ostensibly should return the count of the contained items but this just returns the number
* of rows read so far. It's not really correct but at least coherent.
*/
public function count()
{
if ($this -> Error)
{
return 0;
}
return $this -> RowCount;
}
}
?>
\ No newline at end of file
{
"name": "nuovo/spreadsheet-reader",
"description": "Spreadsheet reader library for Excel, OpenOffice and structured text files",
"keywords": ["spreadsheet", "xls", "xlsx", "ods", "csv", "excel", "openoffice"],
"homepage": "https://github.com/nuovo/spreadsheet-reader",
"version": "0.5.11",
"time": "2015-04-30",
"type": "library",
"license": ["MIT"],
"authors": [
{
"name": "Martins Pilsetnieks",
"email": "[email protected]",
"homepage": "http://www.nuovo.lv/"
}
],
"support": {
"email": "[email protected]"
},
"require": {
"php": ">= 5.3.0",
"ext-zip": "*"
},
"autoload": {
"classmap": ["./"]
}
}
<?php
/**
* XLS parsing uses php-excel-reader from http://code.google.com/p/php-excel-reader/
*/
header('Content-Type: text/plain');
if (isset($argv[1]))
{
$Filepath = $argv[1];
}
elseif (isset($_GET['File']))
{
$Filepath = $_GET['File'];
}
else
{
if (php_sapi_name() == 'cli')
{
echo 'Please specify filename as the first argument'.PHP_EOL;
}
else
{
echo 'Please specify filename as a HTTP GET parameter "File", e.g., "/test.php?File=test.xlsx"';
}
exit;
}
// Excel reader from http://code.google.com/p/php-excel-reader/
require('php-excel-reader/excel_reader2.php');
require('SpreadsheetReader.php');
date_default_timezone_set('UTC');
$StartMem = memory_get_usage();
echo '---------------------------------'.PHP_EOL;
echo 'Starting memory: '.$StartMem.PHP_EOL;
echo '---------------------------------'.PHP_EOL;
try
{
$Spreadsheet = new SpreadsheetReader($Filepath);
$BaseMem = memory_get_usage();
$Sheets = $Spreadsheet -> Sheets();
echo '---------------------------------'.PHP_EOL;
echo 'Spreadsheets:'.PHP_EOL;
print_r($Sheets);
echo '---------------------------------'.PHP_EOL;
echo '---------------------------------'.PHP_EOL;
foreach ($Sheets as $Index => $Name)
{
echo '---------------------------------'.PHP_EOL;
echo '*** Sheet '.$Name.' ***'.PHP_EOL;
echo '---------------------------------'.PHP_EOL;
$Time = microtime(true);
$Spreadsheet -> ChangeSheet($Index);
foreach ($Spreadsheet as $Key => $Row)
{
echo $Key.': ';
if ($Row)
{
print_r($Row);
}
else
{
var_dump($Row);
}
$CurrentMem = memory_get_usage();
echo 'Memory: '.($CurrentMem - $BaseMem).' current, '.$CurrentMem.' base'.PHP_EOL;
echo '---------------------------------'.PHP_EOL;
if ($Key && ($Key % 500 == 0))
{
echo '---------------------------------'.PHP_EOL;
echo 'Time: '.(microtime(true) - $Time);
echo '---------------------------------'.PHP_EOL;
}
}
echo PHP_EOL.'---------------------------------'.PHP_EOL;
echo 'Time: '.(microtime(true) - $Time);
echo PHP_EOL;
echo '---------------------------------'.PHP_EOL;
echo '*** End of sheet '.$Name.' ***'.PHP_EOL;
echo '---------------------------------'.PHP_EOL;
}
}
catch (Exception $E)
{
echo $E -> getMessage();
}
?>
...@@ -28,7 +28,7 @@ class User_model extends CI_Model { ...@@ -28,7 +28,7 @@ class User_model extends CI_Model {
if(empty($user_id) || empty($user_type) || empty($user_data)){ if(empty($user_id) || empty($user_type) || empty($user_data)){
return 0; return 0;
} }
$emailChk = $this->db->get_where('admin_users',array('username'=>$user_data['email_id'],'id !'=>$user_id,'status !='=>'2')); $emailChk = $this->db->get_where('admin_users',array('username'=>$user_data['email_id'],'id !='=>$user_id,'status !='=>'2'));
if(!empty($emailChk) && $emailChk->num_rows() > 0){ if(!empty($emailChk) && $emailChk->num_rows() > 0){
return 2; return 2;
} }
......
...@@ -46,11 +46,11 @@ ...@@ -46,11 +46,11 @@
</div> </div>
<div class="box-body"> <div class="box-body">
<div class="col-md-7"> <div class="col-md-7">
<div style="overflow:scroll;height:400px;"> <div style="overflow:scroll;height:400px;">
<table id="mappedHeaders" class="border-cls" style="width:98%;"> <table id="mappedHeaders" class="border-cls" style="width:98%;">
<?php <?php
foreach($headerArr AS $id => $header){ foreach($headerArr AS $id => $header){
echo '<tr class="border-cls"> echo '<tr class="border-cls tr-cls">
<td class="headtag-td">'.str_replace('_',' ',$header).'</td> <td class="headtag-td">'.str_replace('_',' ',$header).'</td>
<td class="headtag-td"> <td class="headtag-td">
<div type="child" class="drag-box" id="order_'.$id.'" ondrop="dropElement(event)" <div type="child" class="drag-box" id="order_'.$id.'" ondrop="dropElement(event)"
......
...@@ -245,23 +245,23 @@ ...@@ -245,23 +245,23 @@
} }
.overlay { .overlay {
position: fixed; /* Sit on top of the page content */ position: fixed;
display: none; /* Hidden by default */ display: none;
width: 100%; /* Full width (cover the whole page) */ width: 100%;
height: 100%; /* Full height (cover the whole page) */ height: 100%;
top: 0; top: 0;
left: 0; left: 0;
right: 0; right: 0;
bottom: 0; bottom: 0;
background-color: rgba(0,0,0,0.5); /* Black background with opacity */ background-color: rgba(0,0,0,0.5);
z-index: 2; /* Specify a stack order in case you're using a different order for other elements */ z-index: 2;
cursor: pointer; /* Add a pointer on hover */ cursor: pointer;
} }
.drag-box { .drag-box {
float: left; float: left;
width: 100%; width: 100%;
min-height: 25px; min-height: 28px;
margin: 2px; margin: 2px;
border: 1px solid #A8A8A8; border: 1px solid #A8A8A8;
} }
...@@ -297,4 +297,8 @@ ...@@ -297,4 +297,8 @@
.btn-mapping { .btn-mapping {
text-align: center; text-align: center;
padding: 15px; padding: 15px;
}
.tr-cls {
height: 43px;
} }
\ No newline at end of file
...@@ -249,6 +249,9 @@ function markAsScheduled(transport_id,is_scheduled){ ...@@ -249,6 +249,9 @@ function markAsScheduled(transport_id,is_scheduled){
jQuery(jQuery('[id="markSchedule_'+value+'"]')).children().css( "color", color ); jQuery(jQuery('[id="markSchedule_'+value+'"]')).children().css( "color", color );
}); });
remFullScreenLoader(); remFullScreenLoader();
} else {
remFullScreenLoader();
setErrModal('Error Scheduling Rides','Something went wrong, please try again..!');
} }
}, },
error: function (jqXHR, exception) { error: function (jqXHR, exception) {
...@@ -292,8 +295,8 @@ function dropElement(ev) { ...@@ -292,8 +295,8 @@ function dropElement(ev) {
jQuery('[id="upload_excell"]').click(function(){ jQuery('[id="upload_excell"]').click(function(){
var thisObj = jQuery(this), childThisObj = '', headerOrder = [], fileType = jQuery('[name="fileType"]').val(), var thisObj = jQuery(this), childThisObj = '', headerOrder = [], fileType = jQuery('[name="fileType"]').val(),
broker_id = jQuery('[name="broker_id"]').val(), company_id = jQuery('[name="company_id"]').val(), broker_id = jQuery('[name="broker_id"]').val(), company_id = jQuery('[name="company_id"]').val(),
import_file = jQuery('[name="import_file"]').val(); import_file = jQuery('[name="import_file"]').val(), childHead = '', errMsg = '';
if(thisObj.attr('dmclick') != 0){ if(thisObj.attr('dmclick') != 0){
return false; return false;
...@@ -318,7 +321,7 @@ jQuery('[id="upload_excell"]').click(function(){ ...@@ -318,7 +321,7 @@ jQuery('[id="upload_excell"]').click(function(){
setErrModal('Error Uploading Excell','Something went wrong, please try again later..!'); setErrModal('Error Uploading Excell','Something went wrong, please try again later..!');
return false; return false;
} }
var childHead = childThisObj.children(); childHead = childThisObj.children();
headerOrder[childThisObj.attr('headOrder')] = childHead.attr('tmporder'); headerOrder[childThisObj.attr('headOrder')] = childHead.attr('tmporder');
}); });
jQuery.ajax({ jQuery.ajax({
...@@ -334,20 +337,22 @@ jQuery('[id="upload_excell"]').click(function(){ ...@@ -334,20 +337,22 @@ jQuery('[id="upload_excell"]').click(function(){
} }
resp = jQuery.parseJSON(resp); resp = jQuery.parseJSON(resp);
if(resp['status'] == 1){ if(resp['status'] == 1){
var color = ''; window.location.replace(base_url+'Ride/view_rides');
if(is_scheduled == 1){ } else {
color = 'red' if (resp['status'] == 0) {
is_scheduled = 0; errMsg = 'Something went wrong, please try again..!';
} else { } else if (resp['status'] == 2) {
color = 'green' errMsg = 'Invalid File given, please try again later..!';
is_scheduled = 1; } else if (resp['status'] == 3) {
} errMsg = 'No data found, please try again later..!';
jQuery.each(resp['succArr'], function(index,value) { } else if (resp['status'] == 4) {
jQuery('[id="markSchedule_'+value+'"]').attr('is_scheduled',is_scheduled); errMsg = 'Invalid header given, please try again later..!';
jQuery(jQuery('[id="markSchedule_'+value+'"]')).children().css( "color", color ); }
});
remFullScreenLoader(); remFullScreenLoader();
thisObj.attr('dmclick',0);
setErrModal('Error Uploading Excell',errMsg);
} }
return false;
}, },
error: function (jqXHR, exception) { error: function (jqXHR, exception) {
remFullScreenLoader(); remFullScreenLoader();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment