phpDocumentor OpenDocumentPHP
[ class tree: OpenDocumentPHP ] [ index: OpenDocumentPHP ] [ all elements ]

Source for file OpenDocumentText.php

Documentation is available at OpenDocumentText.php

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  4.  
  5. /*
  6.  * Created on 04. Jan. 2007 by Norman Markgraf (nmarkgraf(at)user.sourceforge.net)
  7.  */
  8.  
  9. /**
  10.  * OpenDocumentText class file.
  11.  * 
  12.  * PHP versions 5
  13.  *   
  14.  * LICENSE:
  15.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19.  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25.  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26.  *
  27.  * This software consists of voluntary contributions made by many individuals
  28.  * and is licensed under the GPL. For more information please see
  29.  * <http://opendocumentphp.org>.
  30.  * 
  31.  * $Id: OpenDocumentText.php 263 2007-08-06 08:28:49Z nmarkgraf $
  32.  * 
  33.  * @category    File Formats
  34.  * @package        OpenDocumentPHP
  35.  * @author         Norman Markgraf (nmarkgraf(at)user.sourceforge.net)
  36.  * @copyright     Copyright in 2006, 2007 by The OpenDocumentPHP Team
  37.  * @license     http://www.gnu.org/licenses/gpl.html GNU General Public License 2.0.
  38.  * @version        SVN: $Id: OpenDocumentText.php 263 2007-08-06 08:28:49Z nmarkgraf $
  39.  * @link           http://opendocumentphp.org
  40.  * @since         0.5.0 - 08. Feb. 2007
  41.  */
  42.  
  43. /**
  44.  *
  45.  */
  46. require_once 'OpenDocumentPHP/OpenDocumentAbstract.php';
  47.  
  48. /**
  49.  * OpenDocumentText class.
  50.  *   
  51.  * You could uses this class as follows:
  52.  * 
  53.  * <code>
  54.  *         $text = new OpenDocumentText( 'YourFavoriteTextDocument.odt' );
  55.  *         // do some thing with it
  56.  *         ...
  57.  *         // And write it back
  58.  *         $text->close();
  59.  * </code>
  60.  * 
  61.  * If you want to revert all modifications and do not write anything back to the archive you can
  62.  * use the first parameter of this function and set it to <b>false</b>.
  63.  *
  64.  * <code>
  65.  *      $text = new OpenDocumentText($fullpath);
  66.  *      //... do something ...
  67.  *      // But we do not want to write it back to the archive
  68.  *      $text->close( false );
  69.  * </code>
  70.  *
  71.  * Be aware that even if <b>you</b> do not modifiy the OpenDocument, the library will!
  72.  * So do not expect the that the file is absolute the same after you run the close method.
  73.  * 
  74.  * You can use the <i>setDefaultMeta()</i> method to set up some meta datas. Also you can
  75.  * use the <i>setDefaultFontFace()</i> and <i>setDefaultStyles()</i> methods to bring in
  76.  * some fonts, so you can write a short text. You should take a look at this methods and
  77.  * write your own methods to match your needs.
  78.  * 
  79.  * @category    File Formats
  80.  * @package        OpenDocumentPHP
  81.  * @author         Norman Markgraf (nmarkgraf(at)user.sourceforge.net)
  82.  * @copyright     Copyright in 2006, 2007 by The OpenDocumentPHP Team
  83.  * @license     http://www.gnu.org/licenses/gpl.html GNU General Public License 2.0.
  84.  * @version     Release: @package_version@
  85.  * @link           http://opendocumentphp.org
  86.  * @since         0.5.0 - 08. Feb. 2007
  87.  */
  88. {
  89.     /**
  90.      * Namespace TEXT
  91.      */
  92.     const odmTextNamespace = 'application/vnd.oasis.opendocument.text';
  93.     
  94.     /**
  95.      * Constructor method.
  96.      * 
  97.      * Read (and if not exists create) an OpenDocument text file.
  98.      * 
  99.      * @param         string $fullpath Full path and name of the document
  100.      * @since         0.5.0 - 08. Feb. 2007
  101.      */
  102.     function __construct($fullpath=null
  103.     {
  104.         // Construct a text document
  105.         parent :: __construct(self :: odmTextNamespace);
  106.         // Is the variable $fullpath given?
  107.         if (isset($fullpath&& is_string($fullpath)) {            
  108.             if (file_exists($fullpath)) {
  109.                 // File does exist, so we can load it via open.
  110.                 parent :: open($fullpath);
  111.             else {
  112.                 // File does not exist, so we can create it.        
  113.                 parent :: open($fullpathself :: CREATEself :: odmTextNamespace);
  114.                 // Set everything to a OpenDocument TEXT file.
  115.                 $this->content->setText();
  116.             }
  117.         else {
  118.             // JUST A CLEAN FILE WITH NO FILE NAME JET!!!! DANGER!!!!
  119.             $this->init();
  120.             $this->content->setText();
  121.         }
  122.     }
  123.  
  124.     /**
  125.      * Setup some default data for the meta.xml.
  126.      * 
  127.      * We will setup some data for the meta.xml document. You can use this method and
  128.      * overwrite even the static given data in this method by calling the MetaFragment
  129.      * or DublinCoreFragment methods again.
  130.      * 
  131.      * Currently we set the following meta datas:
  132.      * 
  133.      * <i>DublinCore:</i>
  134.      * <ul>
  135.      * <li>The subject is set to 'A generated subject by OpenDocumentPHP.'.</li>
  136.      * <li>The title is set to 'This is a generated title by OpenDocumentPHP.'.</li>
  137.      * <li>The description is set to 'This is a short description by OpenDocumentPHP.'.</li>
  138.      * <li>The language is set up 'en' for an english text.</li>
  139.      * </ul>
  140.      * 
  141.      * <i>(OpenDocument-)Meta:</i>
  142.      * <b>currently nothing is set here.</b>
  143.      * 
  144.      * In your own code you can change the value very simple:
  145.      * <code>
  146.      *     $doc = new OpenDocumentText('YourFavoriteText.odt');
  147.      *     ...
  148.      *     // Retreive the DublinCoreFragment to change dublin core meta data
  149.      *     $dc = $doc->getMeta()->getDublinCoreFragment();
  150.      *     // Change title to new title
  151.      *     $dc->setTitle( 'This is a new title of the document.' );
  152.      *     ...
  153.      *     // Retreive the MetaFragment to change OpenDocument meta data
  154.      *     $meta = $doc->getMeta()->getMetaFragment();
  155.      *     // Change the initial creator of the document
  156.      *     $meta->setInitialCreator( 'Robert Duck' );
  157.      *     ...
  158.      * </code>
  159.      * 
  160.      * @access        public
  161.      * @since        0.5.2 - 21. Mar. 2007
  162.      */
  163.     function setDefaultMeta(
  164.     {
  165.         // =====================================================================
  166.         $dc $this->getMeta()->getDublinCoreFragment();
  167.         // ---------------------------------------------------------------------
  168.         $dc->setSubject('A generated subject by OpenDocumentPHP.');
  169.         $dc->setTitle('This is a generated title by OpenDocumentPHP.');
  170.         $dc->setDescription('This is a short description by OpenDocumentPHP.');
  171.         $dc->setLanguage('en');
  172.         // =====================================================================    
  173.         $meta $this->getMeta()->getMetaFragment();
  174.         // ---------------------------------------------------------------------        
  175.     }
  176.     
  177.     /**
  178.      * We set up some default font face declarations here.
  179.      * 
  180.      * We put the same font face declarations in the styles.xml and content.xml
  181.      * document.
  182.      * 
  183.      * There are two font faces declared by this method:
  184.      * <i>Tahoma1</i> and <i>Arial Unicode MS</i>.
  185.      * 
  186.      * @access         public
  187.      * @since        0.5.3 - 10. Jul. 2007
  188.      */
  189.     function setDefaultFontFaces(
  190.     {
  191.         // Get the StylesDocument object
  192.         $styles $this->getStyles();
  193.         // Retrieve the FontFaceDeclarations object
  194.         $ffd $styles->getFontFaceDeclarations();
  195.         /*
  196.          * Create a new font face.
  197.          * We will call it 'Tahoma1' which depends on the 'Tahoma' font family.
  198.          */
  199.         $fontface_Tahoma $ffd->nextFontFace();
  200.         $fontface_Tahoma->setStyleName('Tahoma1');
  201.         $fontface_Tahoma->setSVGFontFamily('Tahoma');
  202.         /*
  203.          * Create a new font face.
  204.          * We will call it 'Arial Unicode MS' which depends on the 'Arial Unicode MS' font family
  205.          * and we set the font pitch to 'variable'.         
  206.          */
  207.         $fontface_Arial $ffd->nextFontFace();
  208.         $fontface_Arial->setStyleName('Arial Unicode MS');
  209.         $fontface_Arial->setSVGFontFamily(utf8_encode("'Arial Unicode MS'"));
  210.         $fontface_Arial->setFontPitch('variable');
  211.         /*
  212.          * We need FontFaceDecl in content.xml too.
  213.          * So we make a copy of the font face declaration and import this 
  214.          * to the font face declaration part of the content.xml
  215.          * 
  216.          */
  217.         $content $this->getContent();
  218.         $cffd $content->getFontFaceDeclarations();
  219.         $cffd->importNode($ffd);        
  220.     }
  221.     
  222.     /**
  223.      * Set up some default styles.
  224.      * 
  225.      * We define the 'Standart' and 'Heading_20_1' fonts in this method.
  226.      * 
  227.      * @access         public
  228.      * @since        0.5.3 - 10. Jul. 2007
  229.      */
  230.      function setDefaultStyles(
  231.      {
  232.         $default_style $this->getStyles()->getStyles()->getDefaultStyle();
  233.         $default_style->setFamily('paragraph');
  234.         
  235.         // Set paragraph properties:
  236.  
  237.         $paragraph_properties $default_style->getParagraphProperties();
  238.         $paragraph_properties->setHyphenationLadderCount('no-limit');
  239.         $paragraph_properties->setTextAutospace('ideograph-alpha');
  240.         $paragraph_properties->setPunctuationWrap ('hanging');
  241.         $paragraph_properties->setLineBreak('strict');
  242.         $paragraph_properties->setTabStopDistance('1.251cm');
  243.         $paragraph_properties->setWritingMode('page');
  244.         
  245.         // Set text properties:
  246.  
  247.         $text_properties $default_style->getTextProperties();
  248.         $text_properties->setLanguage('de');
  249.         $text_properties->setCountry('DE');
  250.         $text_properties->setFontName('Times New Roman');
  251.         $text_properties->setFontSize('12pt');
  252.         $text_properties->setFontNameAsian('Arial Unicode MS');
  253.         $text_properties->setFontSizeAsian('12pt');
  254.         $text_properties->setFontNameComplex('Tahoma');
  255.         $text_properties->setFontSizeComplex('12pt');
  256.         $text_properties->setHyphenate('false');
  257.         $text_properties->setHyphenationRemainCharCount('2');
  258.         $text_properties->setHyphenationPushCharCount('2');
  259.         // ...
  260. /*
  261.             <style:text-properties 
  262.             style:use-window-font-color="true"
  263.                 style:language-asian="none"
  264.                 style:country-asian="none" 
  265.                 style:language-complex="none"
  266.                 style:country-complex="none" 
  267. */
  268.         $style_Standard $this->getStyles()->getStyles()->getStyle();
  269.         $style_Standard->setStyleName('Standard');
  270.         $style_Standard->setFamily('paragraph');
  271.         $style_Standard->setClass('text');
  272.         
  273.         $style_Heading_20_1 $this->getStyles()->getStyles()->getStyle();
  274.         $style_Heading_20_1->setStyleName('Heading_20_1');
  275.         $style_Heading_20_1->setDisplayName('Heading_1');
  276.         $style_Heading_20_1->setFamily('paragraph');
  277.         $style_Heading_20_1->setClass('text');
  278.         $style_Heading_20_1->setDefaultOutlineLevel(1);
  279.         
  280.         // Set paragraph properties:
  281.  
  282.         $style_Heading_20_1_paragraph_properties $style_Heading_20_1->getParagraphProperties();
  283.         $style_Heading_20_1_paragraph_properties->setMarginTop('0.423cm');
  284.         $style_Heading_20_1_paragraph_properties->setMarginBottom('0.212cm');
  285.         $style_Heading_20_1_paragraph_properties->setKeepWithNext('always');
  286.         
  287.         //    Set text properties:
  288.  
  289.         $style_Heading_20_1_text_properties $style_Heading_20_1->getTextProperties();
  290.         $style_Heading_20_1_text_properties->setFontName('Arial');
  291.         $style_Heading_20_1_text_properties->setFontSize('14pt');
  292.         $style_Heading_20_1_text_properties->setFontNameAsian('MS Mincho');
  293.         $style_Heading_20_1_text_properties->setFontSizeAsian('14pt');
  294.         $style_Heading_20_1_text_properties->setFontNameComplex('Tahoma');
  295.         $style_Heading_20_1_text_properties->setFontSizeComplex('14pt');    
  296.     }
  297. }
  298. ?>

Documentation generated on Wed, 18 Jun 2008 06:31:17 +0200 by phpDocumentor 1.3.2