PHPExcel_Writer_Excel2007
[ class tree: PHPExcel_Writer_Excel2007 ] [ index: PHPExcel_Writer_Excel2007 ] [ all elements ]

Source for file Rels.php

Documentation is available at Rels.php

  1. <?php
  2. /**
  3.  * PHPExcel
  4.  *
  5.  * Copyright (c) 2006 - 2010 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_Writer_Excel2007
  23.  * @copyright  Copyright (c) 2006 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.7.3c, 2010-06-01
  26.  */
  27.  
  28.  
  29. /**
  30.  * PHPExcel_Writer_Excel2007_Rels
  31.  *
  32.  * @category   PHPExcel
  33.  * @package    PHPExcel_Writer_Excel2007
  34.  * @copyright  Copyright (c) 2006 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  35.  */
  36. {
  37.     /**
  38.      * Write relationships to XML format
  39.      *
  40.      * @param     PHPExcel    $pPHPExcel 
  41.      * @return     string         XML Output
  42.      * @throws     Exception
  43.      */
  44.     public function writeRelationships(PHPExcel $pPHPExcel null)
  45.     {
  46.         // Create XML writer
  47.         $objWriter null;
  48.         if ($this->getParentWriter()->getUseDiskCaching()) {
  49.             $objWriter new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK$this->getParentWriter()->getDiskCachingDirectory());
  50.         else {
  51.             $objWriter new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  52.         }
  53.  
  54.         // XML header
  55.         $objWriter->startDocument('1.0','UTF-8','yes');
  56.  
  57.         // Relationships
  58.         $objWriter->startElement('Relationships');
  59.         $objWriter->writeAttribute('xmlns''http://schemas.openxmlformats.org/package/2006/relationships');
  60.  
  61.             // Relationship docProps/app.xml
  62.             $this->_writeRelationship(
  63.                 $objWriter,
  64.                 3,
  65.                 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties',
  66.                 'docProps/app.xml'
  67.             );
  68.  
  69.             // Relationship docProps/core.xml
  70.             $this->_writeRelationship(
  71.                 $objWriter,
  72.                 2,
  73.                 'http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties',
  74.                 'docProps/core.xml'
  75.             );
  76.  
  77.             // Relationship xl/workbook.xml
  78.             $this->_writeRelationship(
  79.                 $objWriter,
  80.                 1,
  81.                 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument',
  82.                 'xl/workbook.xml'
  83.             );
  84.  
  85.         $objWriter->endElement();
  86.  
  87.         // Return
  88.         return $objWriter->getData();
  89.     }
  90.  
  91.     /**
  92.      * Write workbook relationships to XML format
  93.      *
  94.      * @param     PHPExcel    $pPHPExcel 
  95.      * @return     string         XML Output
  96.      * @throws     Exception
  97.      */
  98.     public function writeWorkbookRelationships(PHPExcel $pPHPExcel null)
  99.     {
  100.         // Create XML writer
  101.         $objWriter null;
  102.         if ($this->getParentWriter()->getUseDiskCaching()) {
  103.             $objWriter new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK$this->getParentWriter()->getDiskCachingDirectory());
  104.         else {
  105.             $objWriter new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  106.         }
  107.  
  108.         // XML header
  109.         $objWriter->startDocument('1.0','UTF-8','yes');
  110.  
  111.         // Relationships
  112.         $objWriter->startElement('Relationships');
  113.         $objWriter->writeAttribute('xmlns''http://schemas.openxmlformats.org/package/2006/relationships');
  114.  
  115.             // Relationship styles.xml
  116.             $this->_writeRelationship(
  117.                 $objWriter,
  118.                 1,
  119.                 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles',
  120.                 'styles.xml'
  121.             );
  122.  
  123.             // Relationship theme/theme1.xml
  124.             $this->_writeRelationship(
  125.                 $objWriter,
  126.                 2,
  127.                 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme',
  128.                 'theme/theme1.xml'
  129.             );
  130.  
  131.             // Relationship sharedStrings.xml
  132.             $this->_writeRelationship(
  133.                 $objWriter,
  134.                 3,
  135.                 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings',
  136.                 'sharedStrings.xml'
  137.             );
  138.  
  139.             // Relationships with sheets
  140.             $sheetCount $pPHPExcel->getSheetCount();
  141.             for ($i 0$i $sheetCount++$i{
  142.                 $this->_writeRelationship(
  143.                     $objWriter,
  144.                     ($i 3),
  145.                     'http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet',
  146.                     'worksheets/sheet' ($i 1'.xml'
  147.                 );
  148.             }
  149.  
  150.         $objWriter->endElement();
  151.  
  152.         // Return
  153.         return $objWriter->getData();
  154.     }
  155.  
  156.     /**
  157.      * Write worksheet relationships to XML format
  158.      *
  159.      * Numbering is as follows:
  160.      *     rId1                 - Drawings
  161.      *  rId_hyperlink_x     - Hyperlinks
  162.      *
  163.      * @param     PHPExcel_Worksheet        $pWorksheet 
  164.      * @param     int                        $pWorksheetId 
  165.      * @return     string                     XML Output
  166.      * @throws     Exception
  167.      */
  168.     public function writeWorksheetRelationships(PHPExcel_Worksheet $pWorksheet null$pWorksheetId 1)
  169.     {
  170.         // Create XML writer
  171.         $objWriter null;
  172.         if ($this->getParentWriter()->getUseDiskCaching()) {
  173.             $objWriter new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK$this->getParentWriter()->getDiskCachingDirectory());
  174.         else {
  175.             $objWriter new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  176.         }
  177.  
  178.         // XML header
  179.         $objWriter->startDocument('1.0','UTF-8','yes');
  180.  
  181.         // Relationships
  182.         $objWriter->startElement('Relationships');
  183.         $objWriter->writeAttribute('xmlns''http://schemas.openxmlformats.org/package/2006/relationships');
  184.  
  185.             // Write drawing relationships?
  186.             if ($pWorksheet->getDrawingCollection()->count(0{
  187.                 $this->_writeRelationship(
  188.                     $objWriter,
  189.                     1,
  190.                     'http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing',
  191.                     '../drawings/drawing' $pWorksheetId '.xml'
  192.                 );
  193.             }
  194.  
  195.             // Write hyperlink relationships?
  196.             $i 1;
  197.             foreach ($pWorksheet->getHyperlinkCollection(as $hyperlink{
  198.                 if (!$hyperlink->isInternal()) {
  199.                     $this->_writeRelationship(
  200.                         $objWriter,
  201.                         '_hyperlink_' $i,
  202.                         'http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink',
  203.                         $hyperlink->getUrl(),
  204.                         'External'
  205.                     );
  206.  
  207.                     ++$i;
  208.                 }
  209.             }
  210.  
  211.             // Write comments relationship?
  212.             $i 1;
  213.             if (count($pWorksheet->getComments()) 0{
  214.                 $this->_writeRelationship(
  215.                     $objWriter,
  216.                     '_comments_vml' $i,
  217.                     'http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing',
  218.                     '../drawings/vmlDrawing' $pWorksheetId '.vml'
  219.                 );
  220.  
  221.                 $this->_writeRelationship(
  222.                     $objWriter,
  223.                     '_comments' $i,
  224.                     'http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments',
  225.                     '../comments' $pWorksheetId '.xml'
  226.                 );
  227.             }
  228.  
  229.             // Write header/footer relationship?
  230.             $i 1;
  231.             if (count($pWorksheet->getHeaderFooter()->getImages()) 0{
  232.                 $this->_writeRelationship(
  233.                     $objWriter,
  234.                     '_headerfooter_vml' $i,
  235.                     'http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing',
  236.                     '../drawings/vmlDrawingHF' $pWorksheetId '.vml'
  237.                 );
  238.             }
  239.  
  240.         $objWriter->endElement();
  241.  
  242.         // Return
  243.         return $objWriter->getData();
  244.     }
  245.  
  246.     /**
  247.      * Write drawing relationships to XML format
  248.      *
  249.      * @param     PHPExcel_Worksheet            $pWorksheet 
  250.      * @return     string                         XML Output
  251.      * @throws     Exception
  252.      */
  253.     public function writeDrawingRelationships(PHPExcel_Worksheet $pWorksheet null)
  254.     {
  255.         // Create XML writer
  256.         $objWriter null;
  257.         if ($this->getParentWriter()->getUseDiskCaching()) {
  258.             $objWriter new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK$this->getParentWriter()->getDiskCachingDirectory());
  259.         else {
  260.             $objWriter new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  261.         }
  262.  
  263.         // XML header
  264.         $objWriter->startDocument('1.0','UTF-8','yes');
  265.  
  266.         // Relationships
  267.         $objWriter->startElement('Relationships');
  268.         $objWriter->writeAttribute('xmlns''http://schemas.openxmlformats.org/package/2006/relationships');
  269.  
  270.             // Loop through images and write relationships
  271.             $i 1;
  272.             $iterator $pWorksheet->getDrawingCollection()->getIterator();
  273.             while ($iterator->valid()) {
  274.                 if ($iterator->current(instanceof PHPExcel_Worksheet_Drawing
  275.                     || $iterator->current(instanceof PHPExcel_Worksheet_MemoryDrawing{
  276.                     // Write relationship for image drawing
  277.                     $this->_writeRelationship(
  278.                         $objWriter,
  279.                         $i,
  280.                         'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
  281.                         '../media/' str_replace(' '''$iterator->current()->getIndexedFilename())
  282.                     );
  283.                 }
  284.  
  285.                 $iterator->next();
  286.                 ++$i;
  287.             }
  288.  
  289.         $objWriter->endElement();
  290.  
  291.         // Return
  292.         return $objWriter->getData();
  293.     }
  294.  
  295.     /**
  296.      * Write header/footer drawing relationships to XML format
  297.      *
  298.      * @param     PHPExcel_Worksheet            $pWorksheet 
  299.      * @return     string                         XML Output
  300.      * @throws     Exception
  301.      */
  302.     public function writeHeaderFooterDrawingRelationships(PHPExcel_Worksheet $pWorksheet null)
  303.     {
  304.         // Create XML writer
  305.         $objWriter null;
  306.         if ($this->getParentWriter()->getUseDiskCaching()) {
  307.             $objWriter new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK$this->getParentWriter()->getDiskCachingDirectory());
  308.         else {
  309.             $objWriter new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  310.         }
  311.  
  312.         // XML header
  313.         $objWriter->startDocument('1.0','UTF-8','yes');
  314.  
  315.         // Relationships
  316.         $objWriter->startElement('Relationships');
  317.         $objWriter->writeAttribute('xmlns''http://schemas.openxmlformats.org/package/2006/relationships');
  318.  
  319.             // Loop through images and write relationships
  320.             foreach ($pWorksheet->getHeaderFooter()->getImages(as $key => $value{
  321.                 // Write relationship for image drawing
  322.                 $this->_writeRelationship(
  323.                     $objWriter,
  324.                     $key,
  325.                     'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
  326.                     '../media/' $value->getIndexedFilename()
  327.                 );
  328.             }
  329.  
  330.         $objWriter->endElement();
  331.  
  332.         // Return
  333.         return $objWriter->getData();
  334.     }
  335.  
  336.     /**
  337.      * Write Override content type
  338.      *
  339.      * @param     PHPExcel_Shared_XMLWriter     $objWriter         XML Writer
  340.      * @param     int                            $pId            Relationship ID. rId will be prepended!
  341.      * @param     string                        $pType            Relationship type
  342.      * @param     string                         $pTarget        Relationship target
  343.      * @param     string                         $pTargetMode    Relationship target mode
  344.      * @throws     Exception
  345.      */
  346.     private function _writeRelationship(PHPExcel_Shared_XMLWriter $objWriter null$pId 1$pType ''$pTarget ''$pTargetMode '')
  347.     {
  348.         if ($pType != '' && $pTarget != ''{
  349.             // Write relationship
  350.             $objWriter->startElement('Relationship');
  351.             $objWriter->writeAttribute('Id',         'rId' $pId);
  352.             $objWriter->writeAttribute('Type',         $pType);
  353.             $objWriter->writeAttribute('Target',    $pTarget);
  354.  
  355.             if ($pTargetMode != ''{
  356.                 $objWriter->writeAttribute('TargetMode',    $pTargetMode);
  357.             }
  358.  
  359.             $objWriter->endElement();
  360.         else {
  361.             throw new Exception("Invalid parameters passed.");
  362.         }
  363.     }
  364. }

Documentation generated on Tue, 01 Jun 2010 17:05:51 +0200 by phpDocumentor 1.4.3