Browse Source

First version

Frederic G. Marand 19 years ago
commit
847ebf447d
3 changed files with 232 additions and 0 deletions
  1. 100 0
      ooo/Calc_Cell.php
  2. 60 0
      ooo/Calc_Reader.php
  3. 72 0
      ooo/Calc_Row.php

+ 100 - 0
ooo/Calc_Cell.php

@@ -0,0 +1,100 @@
+<?php
+/**
+ * Wrap an OpenDocument spreadsheet cell
+ *
+ * Requires PHP 5.0.3
+ *
+ * @version $Id: Calc_Cell.php,v 1.1 2005-02-27 10:58:44 marand Exp $
+ * @license CeCILL 2.0
+ * @copyright 2005 Ouest Systemes Informatiques
+ */
+
+/**
+ * OpenDocument Spreadsheet cell
+ */
+$erCalc_Cell = error_reporting(E_ALL | E_STRICT);
+
+class Calc_Cell
+  {
+  /**
+   * Values of the 'office:value-type' attribute
+   */
+  const TYPE_STRING = 'string';
+  const TYPE_FLOAT  = 'float';
+  const TYPE_EMPTY  = 'empty'; // used if attribute is missing
+  
+  public $dataType;
+  public $dataValue;
+  public $displayValue;
+  
+  /**
+   * @param DOMNode $cellNode
+   */
+  function __construct(DOMNode $cellNode)
+    {
+    if (is_a($cellNode, 'DOMElement'))
+      {
+      $this->dataType  = $cellNode->getAttributeNS(Calc_Reader::NS_OFFICE, 'value-type');
+      /**
+       * Note: cells with office:value-type==string don't have a office:value 
+       * attribute, but it is clearer to just fetch it and get NULL than test.
+       * Maybe more sophistication will be needed in the future
+       */
+      $this->dataValue = $cellNode->getAttributeNS(Calc_Reader::NS_OFFICE, 'value');
+      $p = $cellNode->childNodes->item(0); 
+      $this->displayValue = $p->textContent;
+      unset($p);     
+      }
+    else
+      {
+      $this->dataType  = Calc_Cell::TYPE_EMPTY;
+      $this->dataValue = NULL;
+      $this->displayValue = '';
+      }
+    }
+    
+  /**
+   * Convert a PHP value to a wrapped OpenDocument cell value
+   * 
+   * @param mixed $x
+   */
+  function set($x = NULL)
+    {
+    $type = gettype($x);
+    switch ($type)
+      {
+      case 'NULL':
+        $this->dataType = Calc_Cell::TYPE_EMPTY; 
+        $this->dataValue = NULL;
+        $this->displayValue = NULL;
+        break;
+      case 'string':
+        $this->dataType = Calc_Cell::TYPE_STRING;
+        $this->dataValue = NULL;
+        $this->displayValue = $x;
+        break;
+      case 'double':
+        $this->dataType = Calc_Cell::TYPE_FLOAT;
+        $this->dataValue = $x;
+        $this->displayValue = number_format($x);
+        break;
+      default:
+        $this->set();
+        throw new Exception('Calc_Cell::set unsupported value type ', $type);
+        break;
+      }
+    }
+   
+  /**
+   * implement magic method __toString
+   * 
+   * @return string
+   */
+  function __toString()
+    {
+    return (string) $this->displayValue;
+    }
+  }
+
+error_reporting($erCalc_Cell);
+unset($erCalc_Cell);

+ 60 - 0
ooo/Calc_Reader.php

@@ -0,0 +1,60 @@
+<?php
+/**
+ * Utility class to access OpenDocument spreadsheets read-only
+ *
+ * Requires PHP 5.0.3
+ *
+ * @version $Id: Calc_Reader.php,v 1.1 2005-02-27 10:58:44 marand Exp $
+ * @license CeCILL 2.0
+ * @copyright 2005 Ouest Systemes Informatiques
+ */
+
+$erCalc_Reader = error_reporting(E_ALL | E_STRICT);
+
+/**
+ * Utility class to access OpenDocument spreadsheets read-only
+ */
+class Calc_Reader
+  {
+  const NS_TABLE  = "urn:oasis:names:tc:opendocument:xmlns:table:1.0";
+  const NS_OFFICE = "urn:oasis:names:tc:opendocument:xmlns:office:1.0";
+  
+  /**
+   * @var DOMDocument $xml
+   */
+  public $xml;
+  
+  /**
+   * @var DOMXPath $xpath
+   */
+  public $xpath;
+  
+  function __construct($path)
+    {
+    $this->xml = new DOMDocument();
+    $this->xml->preserveWhiteSpace = FALSE;
+    $this->xml->load($path);
+
+    $this->xpath = new DOMXPath($this->xml);
+    $this->xpath->registerNameSpace('office', Calc_Reader::NS_OFFICE);
+    $this->xpath->registerNameSpace('table', Calc_Reader::NS_TABLE);      
+    }
+
+  /**
+   * Return the rows in the spreadsheet as a DOMNodeList
+   * 
+   * @return DOMNodeList
+   */
+  function getRows ()
+    {
+    $query = '//table:table-row';
+    
+    $ret = $this->xpath->query($query);
+    unset($query);
+    
+    return $ret;  
+    }
+  }
+  
+error_reporting($erCalc_Reader);
+unset ($erCalc_Reader);

+ 72 - 0
ooo/Calc_Row.php

@@ -0,0 +1,72 @@
+<?php
+/**
+ * Wrap an OpenDocument spreadsheet row
+ *
+ * Requires PHP 5.0.3
+ *
+ * @version $Id: Calc_Row.php,v 1.1 2005-02-27 10:58:44 marand Exp $
+ * @license CeCILL 2.0
+ * @copyright 2005 Ouest Systemes Informatiques
+ */
+
+$erCalc_Row = error_reporting(E_ALL | E_STRICT);
+
+class Calc_Row
+  {
+  /**
+   * @var DOMElement $row
+   */
+  protected $row;
+  
+  /**
+   * Analyze a spreadsheet row and return the cells as an array of Calc_Cell
+   * 
+   * @return array
+   */
+  function getCells ()
+    {
+    $cellNodeList = $this->row->childNodes;
+    $cells = array();
+
+    $iCell = 0;
+    for ($icol = 0 ; $icol < $cellNodeList->length ; $icol++)
+      {
+      $cellElement = $cellNodeList->item($icol);
+      $count = NULL;
+      if (is_object($cellElement))
+        {
+        $count = $cellElement->getAttributeNS(Calc_Reader::NS_TABLE, 'number-columns-repeated');
+        }
+      if (empty($count))
+        { 
+        $count = 1;
+        }
+      for ($j = 0 ; $j < $count  ; $j++)
+        {
+        $cells[$iprop++] = new Calc_Cell($cellElement);  
+        }
+      }
+    
+    return $cells;
+    }
+    
+  /**
+   * Construct a Calc_Row from the DOMElement representing it.
+   * 
+   * This DOMElement must bear a table:table-row tag
+   * 
+   * @param DOMNodeList $row
+   * @return void
+   */
+  function __construct(DOMElement $row)
+    {
+    if ((!is_a($row, 'DOMElement')) || ($row->tagName != 'table:table-row'))
+      {
+      die('Calc_Row::__construct() needs a DOMElement parameter for a table-row element ');
+      }
+    $this->row = $row;
+    }
+  }
+  
+error_reporting($erCalc_Row);
+unset($erCalc_Row);