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 - 2009 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 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.6.5, 2009-01-05
  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 - 2009 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.             $sheetCount $pPHPExcel->getSheetCount();
  157.             for ($i 0$i $sheetCount++$i{
  158.                 $this->_writeRelationship(
  159.                     $objWriter,
  160.                     ($i 3),
  161.                     'http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet',
  162.                     'worksheets/sheet' ($i 1'.xml'
  163.                 );
  164.             }
  165.  
  166.         $objWriter->endElement();
  167.  
  168.         // Return
  169.         return $objWriter->getData();
  170.     }
  171.  
  172.     /**
  173.      * Write worksheet relationships to XML format
  174.      *
  175.      * Numbering is as follows:
  176.      *     rId1                 - Drawings
  177.      *  rId_hyperlink_x     - Hyperlinks
  178.      *
  179.      * @param     PHPExcel_Worksheet        $pWorksheet 
  180.      * @param     int                        $pWorksheetId 
  181.      * @return     string                     XML Output
  182.      * @throws     Exception
  183.      */
  184.     public function writeWorksheetRelationships(PHPExcel_Worksheet $pWorksheet null$pWorksheetId 1)
  185.     {
  186.         // Create XML writer
  187.         $objWriter null;
  188.         if ($this->getParentWriter()->getUseDiskCaching()) {
  189.             $objWriter new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK);
  190.         else {
  191.             $objWriter new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  192.         }
  193.  
  194.         // XML header
  195.         $objWriter->startDocument('1.0','UTF-8','yes');
  196.  
  197.         // Relationships
  198.         $objWriter->startElement('Relationships');
  199.         $objWriter->writeAttribute('xmlns''http://schemas.openxmlformats.org/package/2006/relationships');
  200.  
  201.             // Write drawing relationships?
  202.             if ($pWorksheet->getDrawingCollection()->count(0{
  203.                 $this->_writeRelationship(
  204.                     $objWriter,
  205.                     1,
  206.                     'http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing',
  207.                     '../drawings/drawing' $pWorksheetId '.xml'
  208.                 );
  209.             }
  210.  
  211.             // Write hyperlink relationships?
  212.             $i 1;
  213.             foreach ($pWorksheet->getCellCollection(as $cell{
  214.                 if ($cell->hasHyperlink(&& !$cell->getHyperlink()->isInternal()) {
  215.                     $this->_writeRelationship(
  216.                         $objWriter,
  217.                         '_hyperlink_' $i,
  218.                         'http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink',
  219.                         $cell->getHyperlink()->getUrl(),
  220.                         'External'
  221.                     );
  222.  
  223.                     ++$i;
  224.                 }
  225.             }
  226.  
  227.             // Write comments relationship?
  228.             $i 1;
  229.             if (count($pWorksheet->getComments()) 0{
  230.                 $this->_writeRelationship(
  231.                     $objWriter,
  232.                     '_comments_vml' $i,
  233.                     'http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing',
  234.                     '../drawings/vmlDrawing' $pWorksheetId '.vml'
  235.                 );
  236.  
  237.                 $this->_writeRelationship(
  238.                     $objWriter,
  239.                     '_comments' $i,
  240.                     'http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments',
  241.                     '../comments' $pWorksheetId '.xml'
  242.                 );
  243.             }
  244.  
  245.             // Write header/footer relationship?
  246.             $i 1;
  247.             if (count($pWorksheet->getHeaderFooter()->getImages()) 0{
  248.                 $this->_writeRelationship(
  249.                     $objWriter,
  250.                     '_headerfooter_vml' $i,
  251.                     'http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing',
  252.                     '../drawings/vmlDrawingHF' $pWorksheetId '.vml'
  253.                 );
  254.             }
  255.  
  256.         $objWriter->endElement();
  257.  
  258.         // Return
  259.         return $objWriter->getData();
  260.     }
  261.  
  262.     /**
  263.      * Write drawing relationships to XML format
  264.      *
  265.      * @param     PHPExcel_Worksheet            $pWorksheet 
  266.      * @return     string                         XML Output
  267.      * @throws     Exception
  268.      */
  269.     public function writeDrawingRelationships(PHPExcel_Worksheet $pWorksheet null)
  270.     {
  271.         // Create XML writer
  272.         $objWriter null;
  273.         if ($this->getParentWriter()->getUseDiskCaching()) {
  274.             $objWriter new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK);
  275.         else {
  276.             $objWriter new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  277.         }
  278.  
  279.         // XML header
  280.         $objWriter->startDocument('1.0','UTF-8','yes');
  281.  
  282.         // Relationships
  283.         $objWriter->startElement('Relationships');
  284.         $objWriter->writeAttribute('xmlns''http://schemas.openxmlformats.org/package/2006/relationships');
  285.  
  286.             // Loop trough images and write relationships
  287.             $i 1;
  288.             $iterator $pWorksheet->getDrawingCollection()->getIterator();
  289.             while ($iterator->valid()) {
  290.                 if ($iterator->current(instanceof PHPExcel_Worksheet_Drawing
  291.                     || $iterator->current(instanceof PHPExcel_Worksheet_MemoryDrawing{
  292.                     // Write relationship for image drawing
  293.                     $this->_writeRelationship(
  294.                         $objWriter,
  295.                         $i,
  296.                         'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
  297.                         '../media/' str_replace(' '''$iterator->current()->getIndexedFilename())
  298.                     );
  299.                 }
  300.  
  301.                 $iterator->next();
  302.                 ++$i;
  303.             }
  304.  
  305.         $objWriter->endElement();
  306.  
  307.         // Return
  308.         return $objWriter->getData();
  309.     }
  310.  
  311.     /**
  312.      * Write header/footer drawing relationships to XML format
  313.      *
  314.      * @param     PHPExcel_Worksheet            $pWorksheet 
  315.      * @return     string                         XML Output
  316.      * @throws     Exception
  317.      */
  318.     public function writeHeaderFooterDrawingRelationships(PHPExcel_Worksheet $pWorksheet null)
  319.     {
  320.         // Create XML writer
  321.         $objWriter null;
  322.         if ($this->getParentWriter()->getUseDiskCaching()) {
  323.             $objWriter new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK);
  324.         else {
  325.             $objWriter new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  326.         }
  327.  
  328.         // XML header
  329.         $objWriter->startDocument('1.0','UTF-8','yes');
  330.  
  331.         // Relationships
  332.         $objWriter->startElement('Relationships');
  333.         $objWriter->writeAttribute('xmlns''http://schemas.openxmlformats.org/package/2006/relationships');
  334.  
  335.             // Loop trough images and write relationships
  336.             foreach ($pWorksheet->getHeaderFooter()->getImages(as $key => $value{
  337.                 // Write relationship for image drawing
  338.                 $this->_writeRelationship(
  339.                     $objWriter,
  340.                     $key,
  341.                     'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
  342.                     '../media/' $value->getIndexedFilename()
  343.                 );
  344.             }
  345.  
  346.         $objWriter->endElement();
  347.  
  348.         // Return
  349.         return $objWriter->getData();
  350.     }
  351.  
  352.     /**
  353.      * Write Override content type
  354.      *
  355.      * @param     PHPExcel_Shared_XMLWriter     $objWriter         XML Writer
  356.      * @param     int                            $pId            Relationship ID. rId will be prepended!
  357.      * @param     string                        $pType            Relationship type
  358.      * @param     string                         $pTarget        Relationship target
  359.      * @param     string                         $pTargetMode    Relationship target mode
  360.      * @throws     Exception
  361.      */
  362.     private function _writeRelationship(PHPExcel_Shared_XMLWriter $objWriter null$pId 1$pType ''$pTarget ''$pTargetMode '')
  363.     {
  364.         if ($pType != '' && $pTarget != ''{
  365.             // Write relationship
  366.             $objWriter->startElement('Relationship');
  367.             $objWriter->writeAttribute('Id',         'rId' $pId);
  368.             $objWriter->writeAttribute('Type',         $pType);
  369.             $objWriter->writeAttribute('Target',    $pTarget);
  370.  
  371.             if ($pTargetMode != ''{
  372.                 $objWriter->writeAttribute('TargetMode',    $pTargetMode);
  373.             }
  374.  
  375.             $objWriter->endElement();
  376.         else {
  377.             throw new Exception("Invalid parameters passed.");
  378.         }
  379.     }
  380. }

Documentation generated on Mon, 05 Jan 2009 20:38:26 +0100 by phpDocumentor 1.4.1