3611bae8ece61e9baae784fab75767b76129560a.svn-base 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2014 PHPExcel
  6. *
  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.
  11. *
  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.
  16. *
  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
  20. *
  21. * @category PHPExcel
  22. * @package PHPExcel_CachedObjectStorage
  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##
  26. */
  27. /**
  28. * PHPExcel_CachedObjectStorage_CacheBase
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_CachedObjectStorage
  32. * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. abstract class PHPExcel_CachedObjectStorage_CacheBase {
  35. /**
  36. * Parent worksheet
  37. *
  38. * @var PHPExcel_Worksheet
  39. */
  40. protected $_parent;
  41. /**
  42. * The currently active Cell
  43. *
  44. * @var PHPExcel_Cell
  45. */
  46. protected $_currentObject = null;
  47. /**
  48. * Coordinate address of the currently active Cell
  49. *
  50. * @var string
  51. */
  52. protected $_currentObjectID = null;
  53. /**
  54. * Flag indicating whether the currently active Cell requires saving
  55. *
  56. * @var boolean
  57. */
  58. protected $_currentCellIsDirty = true;
  59. /**
  60. * An array of cells or cell pointers for the worksheet cells held in this cache,
  61. * and indexed by their coordinate address within the worksheet
  62. *
  63. * @var array of mixed
  64. */
  65. protected $_cellCache = array();
  66. /**
  67. * Initialise this new cell collection
  68. *
  69. * @param PHPExcel_Worksheet $parent The worksheet for this cell collection
  70. */
  71. public function __construct(PHPExcel_Worksheet $parent) {
  72. // Set our parent worksheet.
  73. // This is maintained within the cache controller to facilitate re-attaching it to PHPExcel_Cell objects when
  74. // they are woken from a serialized state
  75. $this->_parent = $parent;
  76. } // function __construct()
  77. /**
  78. * Return the parent worksheet for this cell collection
  79. *
  80. * @return PHPExcel_Worksheet
  81. */
  82. public function getParent()
  83. {
  84. return $this->_parent;
  85. }
  86. /**
  87. * Is a value set in the current PHPExcel_CachedObjectStorage_ICache for an indexed cell?
  88. *
  89. * @param string $pCoord Coordinate address of the cell to check
  90. * @return boolean
  91. */
  92. public function isDataSet($pCoord) {
  93. if ($pCoord === $this->_currentObjectID) {
  94. return true;
  95. }
  96. // Check if the requested entry exists in the cache
  97. return isset($this->_cellCache[$pCoord]);
  98. } // function isDataSet()
  99. /**
  100. * Move a cell object from one address to another
  101. *
  102. * @param string $fromAddress Current address of the cell to move
  103. * @param string $toAddress Destination address of the cell to move
  104. * @return boolean
  105. */
  106. public function moveCell($fromAddress, $toAddress) {
  107. if ($fromAddress === $this->_currentObjectID) {
  108. $this->_currentObjectID = $toAddress;
  109. }
  110. $this->_currentCellIsDirty = true;
  111. if (isset($this->_cellCache[$fromAddress])) {
  112. $this->_cellCache[$toAddress] = &$this->_cellCache[$fromAddress];
  113. unset($this->_cellCache[$fromAddress]);
  114. }
  115. return TRUE;
  116. } // function moveCell()
  117. /**
  118. * Add or Update a cell in cache
  119. *
  120. * @param PHPExcel_Cell $cell Cell to update
  121. * @return PHPExcel_Cell
  122. * @throws PHPExcel_Exception
  123. */
  124. public function updateCacheData(PHPExcel_Cell $cell) {
  125. return $this->addCacheData($cell->getCoordinate(),$cell);
  126. } // function updateCacheData()
  127. /**
  128. * Delete a cell in cache identified by coordinate address
  129. *
  130. * @param string $pCoord Coordinate address of the cell to delete
  131. * @throws PHPExcel_Exception
  132. */
  133. public function deleteCacheData($pCoord) {
  134. if ($pCoord === $this->_currentObjectID && !is_null($this->_currentObject)) {
  135. $this->_currentObject->detach();
  136. $this->_currentObjectID = $this->_currentObject = null;
  137. }
  138. if (is_object($this->_cellCache[$pCoord])) {
  139. $this->_cellCache[$pCoord]->detach();
  140. unset($this->_cellCache[$pCoord]);
  141. }
  142. $this->_currentCellIsDirty = false;
  143. } // function deleteCacheData()
  144. /**
  145. * Get a list of all cell addresses currently held in cache
  146. *
  147. * @return string[]
  148. */
  149. public function getCellList() {
  150. return array_keys($this->_cellCache);
  151. } // function getCellList()
  152. /**
  153. * Sort the list of all cell addresses currently held in cache by row and column
  154. *
  155. * @return string[]
  156. */
  157. public function getSortedCellList() {
  158. $sortKeys = array();
  159. foreach ($this->getCellList() as $coord) {
  160. sscanf($coord,'%[A-Z]%d', $column, $row);
  161. $sortKeys[sprintf('%09d%3s',$row,$column)] = $coord;
  162. }
  163. ksort($sortKeys);
  164. return array_values($sortKeys);
  165. } // function sortCellList()
  166. /**
  167. * Get highest worksheet column and highest row that have cell records
  168. *
  169. * @return array Highest column name and highest row number
  170. */
  171. public function getHighestRowAndColumn()
  172. {
  173. // Lookup highest column and highest row
  174. $col = array('A' => '1A');
  175. $row = array(1);
  176. foreach ($this->getCellList() as $coord) {
  177. sscanf($coord,'%[A-Z]%d', $c, $r);
  178. $row[$r] = $r;
  179. $col[$c] = strlen($c).$c;
  180. }
  181. if (!empty($row)) {
  182. // Determine highest column and row
  183. $highestRow = max($row);
  184. $highestColumn = substr(max($col),1);
  185. }
  186. return array( 'row' => $highestRow,
  187. 'column' => $highestColumn
  188. );
  189. }
  190. /**
  191. * Return the cell address of the currently active cell object
  192. *
  193. * @return string
  194. */
  195. public function getCurrentAddress()
  196. {
  197. return $this->_currentObjectID;
  198. }
  199. /**
  200. * Return the column address of the currently active cell object
  201. *
  202. * @return string
  203. */
  204. public function getCurrentColumn()
  205. {
  206. sscanf($this->_currentObjectID, '%[A-Z]%d', $column, $row);
  207. return $column;
  208. }
  209. /**
  210. * Return the row address of the currently active cell object
  211. *
  212. * @return integer
  213. */
  214. public function getCurrentRow()
  215. {
  216. sscanf($this->_currentObjectID, '%[A-Z]%d', $column, $row);
  217. return (integer) $row;
  218. }
  219. /**
  220. * Get highest worksheet column
  221. *
  222. * @param string $row Return the highest column for the specified row,
  223. * or the highest column of any row if no row number is passed
  224. * @return string Highest column name
  225. */
  226. public function getHighestColumn($row = null)
  227. {
  228. if ($row == null) {
  229. $colRow = $this->getHighestRowAndColumn();
  230. return $colRow['column'];
  231. }
  232. $columnList = array(1);
  233. foreach ($this->getCellList() as $coord) {
  234. sscanf($coord,'%[A-Z]%d', $c, $r);
  235. if ($r != $row) {
  236. continue;
  237. }
  238. $columnList[] = PHPExcel_Cell::columnIndexFromString($c);
  239. }
  240. return PHPExcel_Cell::stringFromColumnIndex(max($columnList) - 1);
  241. }
  242. /**
  243. * Get highest worksheet row
  244. *
  245. * @param string $column Return the highest row for the specified column,
  246. * or the highest row of any column if no column letter is passed
  247. * @return int Highest row number
  248. */
  249. public function getHighestRow($column = null)
  250. {
  251. if ($column == null) {
  252. $colRow = $this->getHighestRowAndColumn();
  253. return $colRow['row'];
  254. }
  255. $rowList = array(0);
  256. foreach ($this->getCellList() as $coord) {
  257. sscanf($coord,'%[A-Z]%d', $c, $r);
  258. if ($c != $column) {
  259. continue;
  260. }
  261. $rowList[] = $r;
  262. }
  263. return max($rowList);
  264. }
  265. /**
  266. * Generate a unique ID for cache referencing
  267. *
  268. * @return string Unique Reference
  269. */
  270. protected function _getUniqueID() {
  271. if (function_exists('posix_getpid')) {
  272. $baseUnique = posix_getpid();
  273. } else {
  274. $baseUnique = mt_rand();
  275. }
  276. return uniqid($baseUnique,true);
  277. }
  278. /**
  279. * Clone the cell collection
  280. *
  281. * @param PHPExcel_Worksheet $parent The new worksheet
  282. * @return void
  283. */
  284. public function copyCellCollection(PHPExcel_Worksheet $parent) {
  285. $this->_currentCellIsDirty;
  286. $this->_storeData();
  287. $this->_parent = $parent;
  288. if (($this->_currentObject !== NULL) && (is_object($this->_currentObject))) {
  289. $this->_currentObject->attach($this);
  290. }
  291. } // function copyCellCollection()
  292. /**
  293. * Remove a row, deleting all cells in that row
  294. *
  295. * @param string $row Row number to remove
  296. * @return void
  297. */
  298. public function removeRow($row) {
  299. foreach ($this->getCellList() as $coord) {
  300. sscanf($coord,'%[A-Z]%d', $c, $r);
  301. if ($r == $row) {
  302. $this->deleteCacheData($coord);
  303. }
  304. }
  305. }
  306. /**
  307. * Remove a column, deleting all cells in that column
  308. *
  309. * @param string $column Column ID to remove
  310. * @return void
  311. */
  312. public function removeColumn($column) {
  313. foreach ($this->getCellList() as $coord) {
  314. sscanf($coord,'%[A-Z]%d', $c, $r);
  315. if ($c == $column) {
  316. $this->deleteCacheData($coord);
  317. }
  318. }
  319. }
  320. /**
  321. * Identify whether the caching method is currently available
  322. * Some methods are dependent on the availability of certain extensions being enabled in the PHP build
  323. *
  324. * @return boolean
  325. */
  326. public static function cacheMethodIsAvailable() {
  327. return true;
  328. }
  329. }