1
0

Reader.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * Utility class to access OpenDocument spreadsheets read-only
  4. *
  5. * Requires PHP 5.3
  6. *
  7. * @license CeCILL 2.0
  8. * @copyright 2005-2012 Ouest Systemes Informatiques
  9. */
  10. namespace OSInet\Open_Document\Calc;
  11. /**
  12. * Utility class to access OpenDocument spreadsheets read-only
  13. */
  14. class Reader {
  15. const NS_TABLE = "urn:oasis:names:tc:opendocument:xmlns:table:1.0";
  16. const NS_OFFICE = "urn:oasis:names:tc:opendocument:xmlns:office:1.0";
  17. /**
  18. * @var DOMDocument $xml
  19. */
  20. public $xml;
  21. /**
  22. * @var DOMXPath $xpath
  23. */
  24. public $xpath;
  25. function __construct($path) {
  26. $this->xml = new \DOMDocument();
  27. $this->xml->preserveWhiteSpace = FALSE;
  28. $this->xml->load($path);
  29. $this->xpath = new \DOMXPath($this->xml);
  30. $this->xpath->registerNameSpace('office', self::NS_OFFICE);
  31. $this->xpath->registerNameSpace('table', self::NS_TABLE);
  32. }
  33. /**
  34. * Return a DOMNodeList of the worksheets available in the document
  35. *
  36. * @return \DOMNodeList
  37. */
  38. function getCalcSheets() {
  39. $sq = '//table:table';
  40. $list = $this->xpath->query($sq);
  41. unset($sq);
  42. return $list;
  43. }
  44. /**
  45. * Return the names of the worksheets available in the document
  46. *
  47. * @return array
  48. */
  49. function getCalcSheetNames() {
  50. $dnlSheets = $this->getCalcSheets();
  51. $ret = array();
  52. foreach ($dnlSheets as $node) {
  53. $ret[] = $node->getAttributeNS(self::NS_TABLE, 'name');
  54. }
  55. unset($dnlSheets);
  56. return $ret;
  57. }
  58. /**
  59. * Return the rows in the spreadsheet as a DOMNodeList.
  60. *
  61. * WARNING: this means ALL the rows, in all the sheets, not just the first sheet
  62. *
  63. * @param string $namedRange
  64. * Optional name of range, notably Sheet name
  65. * @return \DOMNodeList
  66. */
  67. function getRows ($namedRange = NULL) {
  68. $query = empty($namedRange)
  69. ? '//table:table-row'
  70. : '//table:table[@table:name="' . $namedRange . '"]/table:table-row';
  71. $ret = $this->xpath->query($query);
  72. unset($query);
  73. return $ret;
  74. }
  75. }