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

Documentation generated on Mon, 25 Aug 2008 13:48:32 +0200 by phpDocumentor 1.4.1